xref: /illumos-gate/usr/src/uts/common/fs/nfs/nfs3_vnops.c (revision 06e6833a)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  *	Copyright (c) 1983,1984,1985,1986,1987,1988,1989 AT&T.
28  *	All rights reserved.
29  */
30 
31 /*
32  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
33  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
34  */
35 
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/systm.h>
39 #include <sys/cred.h>
40 #include <sys/time.h>
41 #include <sys/vnode.h>
42 #include <sys/vfs.h>
43 #include <sys/vfs_opreg.h>
44 #include <sys/file.h>
45 #include <sys/filio.h>
46 #include <sys/uio.h>
47 #include <sys/buf.h>
48 #include <sys/mman.h>
49 #include <sys/pathname.h>
50 #include <sys/dirent.h>
51 #include <sys/debug.h>
52 #include <sys/vmsystm.h>
53 #include <sys/fcntl.h>
54 #include <sys/flock.h>
55 #include <sys/swap.h>
56 #include <sys/errno.h>
57 #include <sys/strsubr.h>
58 #include <sys/sysmacros.h>
59 #include <sys/kmem.h>
60 #include <sys/cmn_err.h>
61 #include <sys/pathconf.h>
62 #include <sys/utsname.h>
63 #include <sys/dnlc.h>
64 #include <sys/acl.h>
65 #include <sys/systeminfo.h>
66 #include <sys/atomic.h>
67 #include <sys/policy.h>
68 #include <sys/sdt.h>
69 #include <sys/zone.h>
70 
71 #include <rpc/types.h>
72 #include <rpc/auth.h>
73 #include <rpc/clnt.h>
74 #include <rpc/rpc_rdma.h>
75 
76 #include <nfs/nfs.h>
77 #include <nfs/nfs_clnt.h>
78 #include <nfs/rnode.h>
79 #include <nfs/nfs_acl.h>
80 #include <nfs/lm.h>
81 
82 #include <vm/hat.h>
83 #include <vm/as.h>
84 #include <vm/page.h>
85 #include <vm/pvn.h>
86 #include <vm/seg.h>
87 #include <vm/seg_map.h>
88 #include <vm/seg_kpm.h>
89 #include <vm/seg_vn.h>
90 
91 #include <fs/fs_subr.h>
92 
93 #include <sys/ddi.h>
94 
95 static int	nfs3_rdwrlbn(vnode_t *, page_t *, u_offset_t, size_t, int,
96 			cred_t *);
97 static int	nfs3write(vnode_t *, caddr_t, u_offset_t, int, cred_t *,
98 			stable_how *);
99 static int	nfs3read(vnode_t *, caddr_t, offset_t, int, size_t *, cred_t *);
100 static int	nfs3setattr(vnode_t *, struct vattr *, int, cred_t *);
101 static int	nfs3_accessx(void *, int, cred_t *);
102 static int	nfs3lookup_dnlc(vnode_t *, char *, vnode_t **, cred_t *);
103 static int	nfs3lookup_otw(vnode_t *, char *, vnode_t **, cred_t *, int);
104 static int	nfs3create(vnode_t *, char *, struct vattr *, enum vcexcl,
105 			int, vnode_t **, cred_t *, int);
106 static int	nfs3excl_create_settimes(vnode_t *, struct vattr *, cred_t *);
107 static int	nfs3mknod(vnode_t *, char *, struct vattr *, enum vcexcl,
108 			int, vnode_t **, cred_t *);
109 static int	nfs3rename(vnode_t *, char *, vnode_t *, char *, cred_t *,
110 			caller_context_t *);
111 static int	do_nfs3readdir(vnode_t *, rddir_cache *, cred_t *);
112 static void	nfs3readdir(vnode_t *, rddir_cache *, cred_t *);
113 static void	nfs3readdirplus(vnode_t *, rddir_cache *, cred_t *);
114 static int	nfs3_bio(struct buf *, stable_how *, cred_t *);
115 static int	nfs3_getapage(vnode_t *, u_offset_t, size_t, uint_t *,
116 			page_t *[], size_t, struct seg *, caddr_t,
117 			enum seg_rw, cred_t *);
118 static void	nfs3_readahead(vnode_t *, u_offset_t, caddr_t, struct seg *,
119 			cred_t *);
120 static int	nfs3_sync_putapage(vnode_t *, page_t *, u_offset_t, size_t,
121 			int, cred_t *);
122 static int	nfs3_sync_pageio(vnode_t *, page_t *, u_offset_t, size_t,
123 			int, cred_t *);
124 static int	nfs3_commit(vnode_t *, offset3, count3, cred_t *);
125 static void	nfs3_set_mod(vnode_t *);
126 static void	nfs3_get_commit(vnode_t *);
127 static void	nfs3_get_commit_range(vnode_t *, u_offset_t, size_t);
128 static int	nfs3_putpage_commit(vnode_t *, offset_t, size_t, cred_t *);
129 static int	nfs3_commit_vp(vnode_t *, u_offset_t, size_t,  cred_t *);
130 static int	nfs3_sync_commit(vnode_t *, page_t *, offset3, count3,
131 			cred_t *);
132 static void	nfs3_async_commit(vnode_t *, page_t *, offset3, count3,
133 			cred_t *);
134 static void	nfs3_delmap_callback(struct as *, void *, uint_t);
135 
136 /*
137  * Error flags used to pass information about certain special errors
138  * which need to be handled specially.
139  */
140 #define	NFS_EOF			-98
141 #define	NFS_VERF_MISMATCH	-97
142 
143 /* ALIGN64 aligns the given buffer and adjust buffer size to 64 bit */
144 #define	ALIGN64(x, ptr, sz)						\
145 	x = ((uintptr_t)(ptr)) & (sizeof (uint64_t) - 1);		\
146 	if (x) {							\
147 		x = sizeof (uint64_t) - (x);				\
148 		sz -= (x);						\
149 		ptr += (x);						\
150 	}
151 
152 /*
153  * These are the vnode ops routines which implement the vnode interface to
154  * the networked file system.  These routines just take their parameters,
155  * make them look networkish by putting the right info into interface structs,
156  * and then calling the appropriate remote routine(s) to do the work.
157  *
158  * Note on directory name lookup cacheing:  If we detect a stale fhandle,
159  * we purge the directory cache relative to that vnode.  This way, the
160  * user won't get burned by the cache repeatedly.  See <nfs/rnode.h> for
161  * more details on rnode locking.
162  */
163 
164 static int	nfs3_open(vnode_t **, int, cred_t *, caller_context_t *);
165 static int	nfs3_close(vnode_t *, int, int, offset_t, cred_t *,
166 			caller_context_t *);
167 static int	nfs3_read(vnode_t *, struct uio *, int, cred_t *,
168 			caller_context_t *);
169 static int	nfs3_write(vnode_t *, struct uio *, int, cred_t *,
170 			caller_context_t *);
171 static int	nfs3_ioctl(vnode_t *, int, intptr_t, int, cred_t *, int *,
172 			caller_context_t *);
173 static int	nfs3_getattr(vnode_t *, struct vattr *, int, cred_t *,
174 			caller_context_t *);
175 static int	nfs3_setattr(vnode_t *, struct vattr *, int, cred_t *,
176 			caller_context_t *);
177 static int	nfs3_access(vnode_t *, int, int, cred_t *, caller_context_t *);
178 static int	nfs3_readlink(vnode_t *, struct uio *, cred_t *,
179 			caller_context_t *);
180 static int	nfs3_fsync(vnode_t *, int, cred_t *, caller_context_t *);
181 static void	nfs3_inactive(vnode_t *, cred_t *, caller_context_t *);
182 static int	nfs3_lookup(vnode_t *, char *, vnode_t **,
183 			struct pathname *, int, vnode_t *, cred_t *,
184 			caller_context_t *, int *, pathname_t *);
185 static int	nfs3_create(vnode_t *, char *, struct vattr *, enum vcexcl,
186 			int, vnode_t **, cred_t *, int, caller_context_t *,
187 			vsecattr_t *);
188 static int	nfs3_remove(vnode_t *, char *, cred_t *, caller_context_t *,
189 			int);
190 static int	nfs3_link(vnode_t *, vnode_t *, char *, cred_t *,
191 			caller_context_t *, int);
192 static int	nfs3_rename(vnode_t *, char *, vnode_t *, char *, cred_t *,
193 			caller_context_t *, int);
194 static int	nfs3_mkdir(vnode_t *, char *, struct vattr *, vnode_t **,
195 			cred_t *, caller_context_t *, int, vsecattr_t *);
196 static int	nfs3_rmdir(vnode_t *, char *, vnode_t *, cred_t *,
197 			caller_context_t *, int);
198 static int	nfs3_symlink(vnode_t *, char *, struct vattr *, char *,
199 			cred_t *, caller_context_t *, int);
200 static int	nfs3_readdir(vnode_t *, struct uio *, cred_t *, int *,
201 			caller_context_t *, int);
202 static int	nfs3_fid(vnode_t *, fid_t *, caller_context_t *);
203 static int	nfs3_rwlock(vnode_t *, int, caller_context_t *);
204 static void	nfs3_rwunlock(vnode_t *, int, caller_context_t *);
205 static int	nfs3_seek(vnode_t *, offset_t, offset_t *, caller_context_t *);
206 static int	nfs3_getpage(vnode_t *, offset_t, size_t, uint_t *,
207 			page_t *[], size_t, struct seg *, caddr_t,
208 			enum seg_rw, cred_t *, caller_context_t *);
209 static int	nfs3_putpage(vnode_t *, offset_t, size_t, int, cred_t *,
210 			caller_context_t *);
211 static int	nfs3_map(vnode_t *, offset_t, struct as *, caddr_t *, size_t,
212 			uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *);
213 static int	nfs3_addmap(vnode_t *, offset_t, struct as *, caddr_t, size_t,
214 			uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *);
215 static int	nfs3_frlock(vnode_t *, int, struct flock64 *, int, offset_t,
216 			struct flk_callback *, cred_t *, caller_context_t *);
217 static int	nfs3_space(vnode_t *, int, struct flock64 *, int, offset_t,
218 			cred_t *, caller_context_t *);
219 static int	nfs3_realvp(vnode_t *, vnode_t **, caller_context_t *);
220 static int	nfs3_delmap(vnode_t *, offset_t, struct as *, caddr_t, size_t,
221 			uint_t, uint_t, uint_t, cred_t *, caller_context_t *);
222 static int	nfs3_pathconf(vnode_t *, int, ulong_t *, cred_t *,
223 			caller_context_t *);
224 static int	nfs3_pageio(vnode_t *, page_t *, u_offset_t, size_t, int,
225 			cred_t *, caller_context_t *);
226 static void	nfs3_dispose(vnode_t *, page_t *, int, int, cred_t *,
227 			caller_context_t *);
228 static int	nfs3_setsecattr(vnode_t *, vsecattr_t *, int, cred_t *,
229 			caller_context_t *);
230 static int	nfs3_getsecattr(vnode_t *, vsecattr_t *, int, cred_t *,
231 			caller_context_t *);
232 static int	nfs3_shrlock(vnode_t *, int, struct shrlock *, int, cred_t *,
233 			caller_context_t *);
234 
235 struct vnodeops *nfs3_vnodeops;
236 
237 const fs_operation_def_t nfs3_vnodeops_template[] = {
238 	VOPNAME_OPEN,		{ .vop_open = nfs3_open },
239 	VOPNAME_CLOSE,		{ .vop_close = nfs3_close },
240 	VOPNAME_READ,		{ .vop_read = nfs3_read },
241 	VOPNAME_WRITE,		{ .vop_write = nfs3_write },
242 	VOPNAME_IOCTL,		{ .vop_ioctl = nfs3_ioctl },
243 	VOPNAME_GETATTR,	{ .vop_getattr = nfs3_getattr },
244 	VOPNAME_SETATTR,	{ .vop_setattr = nfs3_setattr },
245 	VOPNAME_ACCESS,		{ .vop_access = nfs3_access },
246 	VOPNAME_LOOKUP,		{ .vop_lookup = nfs3_lookup },
247 	VOPNAME_CREATE,		{ .vop_create = nfs3_create },
248 	VOPNAME_REMOVE,		{ .vop_remove = nfs3_remove },
249 	VOPNAME_LINK,		{ .vop_link = nfs3_link },
250 	VOPNAME_RENAME,		{ .vop_rename = nfs3_rename },
251 	VOPNAME_MKDIR,		{ .vop_mkdir = nfs3_mkdir },
252 	VOPNAME_RMDIR,		{ .vop_rmdir = nfs3_rmdir },
253 	VOPNAME_READDIR,	{ .vop_readdir = nfs3_readdir },
254 	VOPNAME_SYMLINK,	{ .vop_symlink = nfs3_symlink },
255 	VOPNAME_READLINK,	{ .vop_readlink = nfs3_readlink },
256 	VOPNAME_FSYNC,		{ .vop_fsync = nfs3_fsync },
257 	VOPNAME_INACTIVE,	{ .vop_inactive = nfs3_inactive },
258 	VOPNAME_FID,		{ .vop_fid = nfs3_fid },
259 	VOPNAME_RWLOCK,		{ .vop_rwlock = nfs3_rwlock },
260 	VOPNAME_RWUNLOCK,	{ .vop_rwunlock = nfs3_rwunlock },
261 	VOPNAME_SEEK,		{ .vop_seek = nfs3_seek },
262 	VOPNAME_FRLOCK,		{ .vop_frlock = nfs3_frlock },
263 	VOPNAME_SPACE,		{ .vop_space = nfs3_space },
264 	VOPNAME_REALVP,		{ .vop_realvp = nfs3_realvp },
265 	VOPNAME_GETPAGE,	{ .vop_getpage = nfs3_getpage },
266 	VOPNAME_PUTPAGE,	{ .vop_putpage = nfs3_putpage },
267 	VOPNAME_MAP,		{ .vop_map = nfs3_map },
268 	VOPNAME_ADDMAP,		{ .vop_addmap = nfs3_addmap },
269 	VOPNAME_DELMAP,		{ .vop_delmap = nfs3_delmap },
270 	/* no separate nfs3_dump */
271 	VOPNAME_DUMP,		{ .vop_dump = nfs_dump },
272 	VOPNAME_PATHCONF,	{ .vop_pathconf = nfs3_pathconf },
273 	VOPNAME_PAGEIO,		{ .vop_pageio = nfs3_pageio },
274 	VOPNAME_DISPOSE,	{ .vop_dispose = nfs3_dispose },
275 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = nfs3_setsecattr },
276 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = nfs3_getsecattr },
277 	VOPNAME_SHRLOCK,	{ .vop_shrlock = nfs3_shrlock },
278 	VOPNAME_VNEVENT, 	{ .vop_vnevent = fs_vnevent_support },
279 	NULL,			NULL
280 };
281 
282 /*
283  * XXX:  This is referenced in modstubs.s
284  */
285 struct vnodeops *
nfs3_getvnodeops(void)286 nfs3_getvnodeops(void)
287 {
288 	return (nfs3_vnodeops);
289 }
290 
291 /* ARGSUSED */
292 static int
nfs3_open(vnode_t ** vpp,int flag,cred_t * cr,caller_context_t * ct)293 nfs3_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
294 {
295 	int error;
296 	struct vattr va;
297 	rnode_t *rp;
298 	vnode_t *vp;
299 
300 	vp = *vpp;
301 	if (nfs_zone() != VTOMI(vp)->mi_zone)
302 		return (EIO);
303 	rp = VTOR(vp);
304 	mutex_enter(&rp->r_statelock);
305 	if (rp->r_cred == NULL) {
306 		crhold(cr);
307 		rp->r_cred = cr;
308 	}
309 	mutex_exit(&rp->r_statelock);
310 
311 	/*
312 	 * If there is no cached data or if close-to-open
313 	 * consistency checking is turned off, we can avoid
314 	 * the over the wire getattr.  Otherwise, if the
315 	 * file system is mounted readonly, then just verify
316 	 * the caches are up to date using the normal mechanism.
317 	 * Else, if the file is not mmap'd, then just mark
318 	 * the attributes as timed out.  They will be refreshed
319 	 * and the caches validated prior to being used.
320 	 * Else, the file system is mounted writeable so
321 	 * force an over the wire GETATTR in order to ensure
322 	 * that all cached data is valid.
323 	 */
324 	if (vp->v_count > 1 ||
325 	    ((vn_has_cached_data(vp) || HAVE_RDDIR_CACHE(rp)) &&
326 	    !(VTOMI(vp)->mi_flags & MI_NOCTO))) {
327 		if (vn_is_readonly(vp))
328 			error = nfs3_validate_caches(vp, cr);
329 		else if (rp->r_mapcnt == 0 && vp->v_count == 1) {
330 			PURGE_ATTRCACHE(vp);
331 			error = 0;
332 		} else {
333 			va.va_mask = AT_ALL;
334 			error = nfs3_getattr_otw(vp, &va, cr);
335 		}
336 	} else
337 		error = 0;
338 
339 	return (error);
340 }
341 
342 /* ARGSUSED */
343 static int
nfs3_close(vnode_t * vp,int flag,int count,offset_t offset,cred_t * cr,caller_context_t * ct)344 nfs3_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
345 		caller_context_t *ct)
346 {
347 	rnode_t *rp;
348 	int error;
349 	struct vattr va;
350 
351 	/*
352 	 * zone_enter(2) prevents processes from changing zones with NFS files
353 	 * open; if we happen to get here from the wrong zone we can't do
354 	 * anything over the wire.
355 	 */
356 	if (VTOMI(vp)->mi_zone != nfs_zone()) {
357 		/*
358 		 * We could attempt to clean up locks, except we're sure
359 		 * that the current process didn't acquire any locks on
360 		 * the file: any attempt to lock a file belong to another zone
361 		 * will fail, and one can't lock an NFS file and then change
362 		 * zones, as that fails too.
363 		 *
364 		 * Returning an error here is the sane thing to do.  A
365 		 * subsequent call to VN_RELE() which translates to a
366 		 * nfs3_inactive() will clean up state: if the zone of the
367 		 * vnode's origin is still alive and kicking, an async worker
368 		 * thread will handle the request (from the correct zone), and
369 		 * everything (minus the commit and final nfs3_getattr_otw()
370 		 * call) should be OK. If the zone is going away
371 		 * nfs_async_inactive() will throw away cached pages inline.
372 		 */
373 		return (EIO);
374 	}
375 
376 	/*
377 	 * If we are using local locking for this filesystem, then
378 	 * release all of the SYSV style record locks.  Otherwise,
379 	 * we are doing network locking and we need to release all
380 	 * of the network locks.  All of the locks held by this
381 	 * process on this file are released no matter what the
382 	 * incoming reference count is.
383 	 */
384 	if (VTOMI(vp)->mi_flags & MI_LLOCK) {
385 		cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
386 		cleanshares(vp, ttoproc(curthread)->p_pid);
387 	} else
388 		nfs_lockrelease(vp, flag, offset, cr);
389 
390 	if (count > 1)
391 		return (0);
392 
393 	/*
394 	 * If the file has been `unlinked', then purge the
395 	 * DNLC so that this vnode will get reycled quicker
396 	 * and the .nfs* file on the server will get removed.
397 	 */
398 	rp = VTOR(vp);
399 	if (rp->r_unldvp != NULL)
400 		dnlc_purge_vp(vp);
401 
402 	/*
403 	 * If the file was open for write and there are pages,
404 	 * then if the file system was mounted using the "no-close-
405 	 *	to-open" semantics, then start an asynchronous flush
406 	 *	of the all of the pages in the file.
407 	 * else the file system was not mounted using the "no-close-
408 	 *	to-open" semantics, then do a synchronous flush and
409 	 *	commit of all of the dirty and uncommitted pages.
410 	 *
411 	 * The asynchronous flush of the pages in the "nocto" path
412 	 * mostly just associates a cred pointer with the rnode so
413 	 * writes which happen later will have a better chance of
414 	 * working.  It also starts the data being written to the
415 	 * server, but without unnecessarily delaying the application.
416 	 */
417 	if ((flag & FWRITE) && vn_has_cached_data(vp)) {
418 		if (VTOMI(vp)->mi_flags & MI_NOCTO) {
419 			error = nfs3_putpage(vp, (offset_t)0, 0, B_ASYNC,
420 			    cr, ct);
421 			if (error == EAGAIN)
422 				error = 0;
423 		} else
424 			error = nfs3_putpage_commit(vp, (offset_t)0, 0, cr);
425 		if (!error) {
426 			mutex_enter(&rp->r_statelock);
427 			error = rp->r_error;
428 			rp->r_error = 0;
429 			mutex_exit(&rp->r_statelock);
430 		}
431 	} else {
432 		mutex_enter(&rp->r_statelock);
433 		error = rp->r_error;
434 		rp->r_error = 0;
435 		mutex_exit(&rp->r_statelock);
436 	}
437 
438 	/*
439 	 * If RWRITEATTR is set, then issue an over the wire GETATTR to
440 	 * refresh the attribute cache with a set of attributes which
441 	 * weren't returned from a WRITE.  This will enable the close-
442 	 * to-open processing to work.
443 	 */
444 	if (rp->r_flags & RWRITEATTR)
445 		(void) nfs3_getattr_otw(vp, &va, cr);
446 
447 	return (error);
448 }
449 
450 /* ARGSUSED */
451 static int
nfs3_directio_read(vnode_t * vp,struct uio * uiop,cred_t * cr)452 nfs3_directio_read(vnode_t *vp, struct uio *uiop, cred_t *cr)
453 {
454 	mntinfo_t *mi;
455 	READ3args args;
456 	READ3uiores res;
457 	int tsize;
458 	offset_t offset;
459 	ssize_t count;
460 	int error;
461 	int douprintf;
462 	failinfo_t fi;
463 	char *sv_hostname;
464 
465 	mi = VTOMI(vp);
466 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
467 	sv_hostname = VTOR(vp)->r_server->sv_hostname;
468 
469 	douprintf = 1;
470 	args.file = *VTOFH3(vp);
471 	fi.vp = vp;
472 	fi.fhp = (caddr_t)&args.file;
473 	fi.copyproc = nfs3copyfh;
474 	fi.lookupproc = nfs3lookup;
475 	fi.xattrdirproc = acl_getxattrdir3;
476 
477 	res.uiop = uiop;
478 
479 	res.wlist = NULL;
480 
481 	offset = uiop->uio_loffset;
482 	count = uiop->uio_resid;
483 
484 	do {
485 		if (mi->mi_io_kstats) {
486 			mutex_enter(&mi->mi_lock);
487 			kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
488 			mutex_exit(&mi->mi_lock);
489 		}
490 
491 		do {
492 			tsize = MIN(mi->mi_tsize, count);
493 			args.offset = (offset3)offset;
494 			args.count = (count3)tsize;
495 			res.size = (uint_t)tsize;
496 			args.res_uiop = uiop;
497 			args.res_data_val_alt = NULL;
498 
499 			error = rfs3call(mi, NFSPROC3_READ,
500 			    xdr_READ3args, (caddr_t)&args,
501 			    xdr_READ3uiores, (caddr_t)&res, cr,
502 			    &douprintf, &res.status, 0, &fi);
503 		} while (error == ENFS_TRYAGAIN);
504 
505 		if (mi->mi_io_kstats) {
506 			mutex_enter(&mi->mi_lock);
507 			kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
508 			mutex_exit(&mi->mi_lock);
509 		}
510 
511 		if (error)
512 			return (error);
513 
514 		error = geterrno3(res.status);
515 		if (error)
516 			return (error);
517 
518 		if (res.count != res.size) {
519 			zcmn_err(getzoneid(), CE_WARN,
520 "nfs3_directio_read: server %s returned incorrect amount",
521 			    sv_hostname);
522 			return (EIO);
523 		}
524 		count -= res.count;
525 		offset += res.count;
526 		if (mi->mi_io_kstats) {
527 			mutex_enter(&mi->mi_lock);
528 			KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
529 			KSTAT_IO_PTR(mi->mi_io_kstats)->nread += res.count;
530 			mutex_exit(&mi->mi_lock);
531 		}
532 		lwp_stat_update(LWP_STAT_INBLK, 1);
533 	} while (count && !res.eof);
534 
535 	return (0);
536 }
537 
538 /* ARGSUSED */
539 static int
nfs3_read(vnode_t * vp,struct uio * uiop,int ioflag,cred_t * cr,caller_context_t * ct)540 nfs3_read(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr,
541 	caller_context_t *ct)
542 {
543 	rnode_t *rp;
544 	u_offset_t off;
545 	offset_t diff;
546 	int on;
547 	size_t n;
548 	caddr_t base;
549 	uint_t flags;
550 	int error = 0;
551 	mntinfo_t *mi;
552 
553 	rp = VTOR(vp);
554 	mi = VTOMI(vp);
555 
556 	ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER));
557 
558 	if (nfs_zone() != mi->mi_zone)
559 		return (EIO);
560 
561 	if (vp->v_type != VREG)
562 		return (EISDIR);
563 
564 	if (uiop->uio_resid == 0)
565 		return (0);
566 
567 	if (uiop->uio_loffset < 0 || uiop->uio_loffset + uiop->uio_resid < 0)
568 		return (EINVAL);
569 
570 	/*
571 	 * Bypass VM if caching has been disabled (e.g., locking) or if
572 	 * using client-side direct I/O and the file is not mmap'd and
573 	 * there are no cached pages.
574 	 */
575 	if ((vp->v_flag & VNOCACHE) ||
576 	    (((rp->r_flags & RDIRECTIO) || (mi->mi_flags & MI_DIRECTIO)) &&
577 	    rp->r_mapcnt == 0 && rp->r_inmap == 0 &&
578 	    !vn_has_cached_data(vp))) {
579 		return (nfs3_directio_read(vp, uiop, cr));
580 	}
581 
582 	do {
583 		off = uiop->uio_loffset & MAXBMASK; /* mapping offset */
584 		on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */
585 		n = MIN(MAXBSIZE - on, uiop->uio_resid);
586 
587 		error = nfs3_validate_caches(vp, cr);
588 		if (error)
589 			break;
590 
591 		mutex_enter(&rp->r_statelock);
592 		while (rp->r_flags & RINCACHEPURGE) {
593 			if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
594 				mutex_exit(&rp->r_statelock);
595 				return (EINTR);
596 			}
597 		}
598 		diff = rp->r_size - uiop->uio_loffset;
599 		mutex_exit(&rp->r_statelock);
600 		if (diff <= 0)
601 			break;
602 		if (diff < n)
603 			n = (size_t)diff;
604 
605 		if (vpm_enable) {
606 			/*
607 			 * Copy data.
608 			 */
609 			error = vpm_data_copy(vp, off + on, n, uiop,
610 			    1, NULL, 0, S_READ);
611 		} else {
612 			base = segmap_getmapflt(segkmap, vp, off + on, n, 1,
613 			    S_READ);
614 
615 			error = uiomove(base + on, n, UIO_READ, uiop);
616 		}
617 
618 		if (!error) {
619 			/*
620 			 * If read a whole block or read to eof,
621 			 * won't need this buffer again soon.
622 			 */
623 			mutex_enter(&rp->r_statelock);
624 			if (n + on == MAXBSIZE ||
625 			    uiop->uio_loffset == rp->r_size)
626 				flags = SM_DONTNEED;
627 			else
628 				flags = 0;
629 			mutex_exit(&rp->r_statelock);
630 			if (vpm_enable) {
631 				error = vpm_sync_pages(vp, off, n, flags);
632 			} else {
633 				error = segmap_release(segkmap, base, flags);
634 			}
635 		} else {
636 			if (vpm_enable) {
637 				(void) vpm_sync_pages(vp, off, n, 0);
638 			} else {
639 				(void) segmap_release(segkmap, base, 0);
640 			}
641 		}
642 	} while (!error && uiop->uio_resid > 0);
643 
644 	return (error);
645 }
646 
647 /* ARGSUSED */
648 static int
nfs3_write(vnode_t * vp,struct uio * uiop,int ioflag,cred_t * cr,caller_context_t * ct)649 nfs3_write(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr,
650 	caller_context_t *ct)
651 {
652 	rlim64_t limit = uiop->uio_llimit;
653 	rnode_t *rp;
654 	u_offset_t off;
655 	caddr_t base;
656 	uint_t flags;
657 	int remainder;
658 	size_t n;
659 	int on;
660 	int error;
661 	int resid;
662 	offset_t offset;
663 	mntinfo_t *mi;
664 	uint_t bsize;
665 
666 	rp = VTOR(vp);
667 
668 	if (vp->v_type != VREG)
669 		return (EISDIR);
670 
671 	mi = VTOMI(vp);
672 	if (nfs_zone() != mi->mi_zone)
673 		return (EIO);
674 	if (uiop->uio_resid == 0)
675 		return (0);
676 
677 	if (ioflag & FAPPEND) {
678 		struct vattr va;
679 
680 		/*
681 		 * Must serialize if appending.
682 		 */
683 		if (nfs_rw_lock_held(&rp->r_rwlock, RW_READER)) {
684 			nfs_rw_exit(&rp->r_rwlock);
685 			if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER,
686 			    INTR(vp)))
687 				return (EINTR);
688 		}
689 
690 		va.va_mask = AT_SIZE;
691 		error = nfs3getattr(vp, &va, cr);
692 		if (error)
693 			return (error);
694 		uiop->uio_loffset = va.va_size;
695 	}
696 
697 	offset = uiop->uio_loffset + uiop->uio_resid;
698 
699 	if (uiop->uio_loffset < 0 || offset < 0)
700 		return (EINVAL);
701 
702 	if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
703 		limit = MAXOFFSET_T;
704 
705 	/*
706 	 * Check to make sure that the process will not exceed
707 	 * its limit on file size.  It is okay to write up to
708 	 * the limit, but not beyond.  Thus, the write which
709 	 * reaches the limit will be short and the next write
710 	 * will return an error.
711 	 */
712 	remainder = 0;
713 	if (offset > limit) {
714 		remainder = offset - limit;
715 		uiop->uio_resid = limit - uiop->uio_loffset;
716 		if (uiop->uio_resid <= 0) {
717 			proc_t *p = ttoproc(curthread);
718 
719 			uiop->uio_resid += remainder;
720 			mutex_enter(&p->p_lock);
721 			(void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE],
722 			    p->p_rctls, p, RCA_UNSAFE_SIGINFO);
723 			mutex_exit(&p->p_lock);
724 			return (EFBIG);
725 		}
726 	}
727 
728 	if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR(vp)))
729 		return (EINTR);
730 
731 	/*
732 	 * Bypass VM if caching has been disabled (e.g., locking) or if
733 	 * using client-side direct I/O and the file is not mmap'd and
734 	 * there are no cached pages.
735 	 */
736 	if ((vp->v_flag & VNOCACHE) ||
737 	    (((rp->r_flags & RDIRECTIO) || (mi->mi_flags & MI_DIRECTIO)) &&
738 	    rp->r_mapcnt == 0 && rp->r_inmap == 0 &&
739 	    !vn_has_cached_data(vp))) {
740 		size_t bufsize;
741 		int count;
742 		u_offset_t org_offset;
743 		stable_how stab_comm;
744 
745 nfs3_fwrite:
746 		if (rp->r_flags & RSTALE) {
747 			resid = uiop->uio_resid;
748 			offset = uiop->uio_loffset;
749 			error = rp->r_error;
750 			/*
751 			 * A close may have cleared r_error, if so,
752 			 * propagate ESTALE error return properly
753 			 */
754 			if (error == 0)
755 				error = ESTALE;
756 			goto bottom;
757 		}
758 		bufsize = MIN(uiop->uio_resid, mi->mi_stsize);
759 		base = kmem_alloc(bufsize, KM_SLEEP);
760 		do {
761 			if (ioflag & FDSYNC)
762 				stab_comm = DATA_SYNC;
763 			else
764 				stab_comm = FILE_SYNC;
765 			resid = uiop->uio_resid;
766 			offset = uiop->uio_loffset;
767 			count = MIN(uiop->uio_resid, bufsize);
768 			org_offset = uiop->uio_loffset;
769 			error = uiomove(base, count, UIO_WRITE, uiop);
770 			if (!error) {
771 				error = nfs3write(vp, base, org_offset,
772 				    count, cr, &stab_comm);
773 			}
774 		} while (!error && uiop->uio_resid > 0);
775 		kmem_free(base, bufsize);
776 		goto bottom;
777 	}
778 
779 
780 	bsize = vp->v_vfsp->vfs_bsize;
781 
782 	do {
783 		off = uiop->uio_loffset & MAXBMASK; /* mapping offset */
784 		on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */
785 		n = MIN(MAXBSIZE - on, uiop->uio_resid);
786 
787 		resid = uiop->uio_resid;
788 		offset = uiop->uio_loffset;
789 
790 		if (rp->r_flags & RSTALE) {
791 			error = rp->r_error;
792 			/*
793 			 * A close may have cleared r_error, if so,
794 			 * propagate ESTALE error return properly
795 			 */
796 			if (error == 0)
797 				error = ESTALE;
798 			break;
799 		}
800 
801 		/*
802 		 * Don't create dirty pages faster than they
803 		 * can be cleaned so that the system doesn't
804 		 * get imbalanced.  If the async queue is
805 		 * maxed out, then wait for it to drain before
806 		 * creating more dirty pages.  Also, wait for
807 		 * any threads doing pagewalks in the vop_getattr
808 		 * entry points so that they don't block for
809 		 * long periods.
810 		 */
811 		mutex_enter(&rp->r_statelock);
812 		while ((mi->mi_max_threads != 0 &&
813 		    rp->r_awcount > 2 * mi->mi_max_threads) ||
814 		    rp->r_gcount > 0) {
815 			if (INTR(vp)) {
816 				klwp_t *lwp = ttolwp(curthread);
817 
818 				if (lwp != NULL)
819 					lwp->lwp_nostop++;
820 				if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
821 					mutex_exit(&rp->r_statelock);
822 					if (lwp != NULL)
823 						lwp->lwp_nostop--;
824 					error = EINTR;
825 					goto bottom;
826 				}
827 				if (lwp != NULL)
828 					lwp->lwp_nostop--;
829 			} else
830 				cv_wait(&rp->r_cv, &rp->r_statelock);
831 		}
832 		mutex_exit(&rp->r_statelock);
833 
834 		/*
835 		 * Touch the page and fault it in if it is not in core
836 		 * before segmap_getmapflt or vpm_data_copy can lock it.
837 		 * This is to avoid the deadlock if the buffer is mapped
838 		 * to the same file through mmap which we want to write.
839 		 */
840 		uio_prefaultpages((long)n, uiop);
841 
842 		if (vpm_enable) {
843 			/*
844 			 * It will use kpm mappings, so no need to
845 			 * pass an address.
846 			 */
847 			error = writerp(rp, NULL, n, uiop, 0);
848 		} else  {
849 			if (segmap_kpm) {
850 				int pon = uiop->uio_loffset & PAGEOFFSET;
851 				size_t pn = MIN(PAGESIZE - pon,
852 				    uiop->uio_resid);
853 				int pagecreate;
854 
855 				mutex_enter(&rp->r_statelock);
856 				pagecreate = (pon == 0) && (pn == PAGESIZE ||
857 				    uiop->uio_loffset + pn >= rp->r_size);
858 				mutex_exit(&rp->r_statelock);
859 
860 				base = segmap_getmapflt(segkmap, vp, off + on,
861 				    pn, !pagecreate, S_WRITE);
862 
863 				error = writerp(rp, base + pon, n, uiop,
864 				    pagecreate);
865 
866 			} else {
867 				base = segmap_getmapflt(segkmap, vp, off + on,
868 				    n, 0, S_READ);
869 				error = writerp(rp, base + on, n, uiop, 0);
870 			}
871 		}
872 
873 		if (!error) {
874 			if (mi->mi_flags & MI_NOAC)
875 				flags = SM_WRITE;
876 			else if ((uiop->uio_loffset % bsize) == 0 ||
877 			    IS_SWAPVP(vp)) {
878 				/*
879 				 * Have written a whole block.
880 				 * Start an asynchronous write
881 				 * and mark the buffer to
882 				 * indicate that it won't be
883 				 * needed again soon.
884 				 */
885 				flags = SM_WRITE | SM_ASYNC | SM_DONTNEED;
886 			} else
887 				flags = 0;
888 			if ((ioflag & (FSYNC|FDSYNC)) ||
889 			    (rp->r_flags & ROUTOFSPACE)) {
890 				flags &= ~SM_ASYNC;
891 				flags |= SM_WRITE;
892 			}
893 			if (vpm_enable) {
894 				error = vpm_sync_pages(vp, off, n, flags);
895 			} else {
896 				error = segmap_release(segkmap, base, flags);
897 			}
898 		} else {
899 			if (vpm_enable) {
900 				(void) vpm_sync_pages(vp, off, n, 0);
901 			} else {
902 				(void) segmap_release(segkmap, base, 0);
903 			}
904 			/*
905 			 * In the event that we got an access error while
906 			 * faulting in a page for a write-only file just
907 			 * force a write.
908 			 */
909 			if (error == EACCES)
910 				goto nfs3_fwrite;
911 		}
912 	} while (!error && uiop->uio_resid > 0);
913 
914 bottom:
915 	if (error) {
916 		uiop->uio_resid = resid + remainder;
917 		uiop->uio_loffset = offset;
918 	} else
919 		uiop->uio_resid += remainder;
920 
921 	nfs_rw_exit(&rp->r_lkserlock);
922 
923 	return (error);
924 }
925 
926 /*
927  * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED}
928  */
929 static int
nfs3_rdwrlbn(vnode_t * vp,page_t * pp,u_offset_t off,size_t len,int flags,cred_t * cr)930 nfs3_rdwrlbn(vnode_t *vp, page_t *pp, u_offset_t off, size_t len,
931 	int flags, cred_t *cr)
932 {
933 	struct buf *bp;
934 	int error;
935 	page_t *savepp;
936 	uchar_t fsdata;
937 	stable_how stab_comm;
938 
939 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
940 	bp = pageio_setup(pp, len, vp, flags);
941 	ASSERT(bp != NULL);
942 
943 	/*
944 	 * pageio_setup should have set b_addr to 0.  This
945 	 * is correct since we want to do I/O on a page
946 	 * boundary.  bp_mapin will use this addr to calculate
947 	 * an offset, and then set b_addr to the kernel virtual
948 	 * address it allocated for us.
949 	 */
950 	ASSERT(bp->b_un.b_addr == 0);
951 
952 	bp->b_edev = 0;
953 	bp->b_dev = 0;
954 	bp->b_lblkno = lbtodb(off);
955 	bp->b_file = vp;
956 	bp->b_offset = (offset_t)off;
957 	bp_mapin(bp);
958 
959 	/*
960 	 * Calculate the desired level of stability to write data
961 	 * on the server and then mark all of the pages to reflect
962 	 * this.
963 	 */
964 	if ((flags & (B_WRITE|B_ASYNC)) == (B_WRITE|B_ASYNC) &&
965 	    freemem > desfree) {
966 		stab_comm = UNSTABLE;
967 		fsdata = C_DELAYCOMMIT;
968 	} else {
969 		stab_comm = FILE_SYNC;
970 		fsdata = C_NOCOMMIT;
971 	}
972 
973 	savepp = pp;
974 	do {
975 		pp->p_fsdata = fsdata;
976 	} while ((pp = pp->p_next) != savepp);
977 
978 	error = nfs3_bio(bp, &stab_comm, cr);
979 
980 	bp_mapout(bp);
981 	pageio_done(bp);
982 
983 	/*
984 	 * If the server wrote pages in a more stable fashion than
985 	 * was requested, then clear all of the marks in the pages
986 	 * indicating that COMMIT operations were required.
987 	 */
988 	if (stab_comm != UNSTABLE && fsdata == C_DELAYCOMMIT) {
989 		do {
990 			pp->p_fsdata = C_NOCOMMIT;
991 		} while ((pp = pp->p_next) != savepp);
992 	}
993 
994 	return (error);
995 }
996 
997 /*
998  * Write to file.  Writes to remote server in largest size
999  * chunks that the server can handle.  Write is synchronous.
1000  */
1001 static int
nfs3write(vnode_t * vp,caddr_t base,u_offset_t offset,int count,cred_t * cr,stable_how * stab_comm)1002 nfs3write(vnode_t *vp, caddr_t base, u_offset_t offset, int count, cred_t *cr,
1003 	stable_how *stab_comm)
1004 {
1005 	mntinfo_t *mi;
1006 	WRITE3args args;
1007 	WRITE3res res;
1008 	int error;
1009 	int tsize;
1010 	rnode_t *rp;
1011 	int douprintf;
1012 
1013 	rp = VTOR(vp);
1014 	mi = VTOMI(vp);
1015 
1016 	ASSERT(nfs_zone() == mi->mi_zone);
1017 
1018 	args.file = *VTOFH3(vp);
1019 	args.stable = *stab_comm;
1020 
1021 	*stab_comm = FILE_SYNC;
1022 
1023 	douprintf = 1;
1024 
1025 	do {
1026 		if ((vp->v_flag & VNOCACHE) ||
1027 		    (rp->r_flags & RDIRECTIO) ||
1028 		    (mi->mi_flags & MI_DIRECTIO))
1029 			tsize = MIN(mi->mi_stsize, count);
1030 		else
1031 			tsize = MIN(mi->mi_curwrite, count);
1032 		args.offset = (offset3)offset;
1033 		args.count = (count3)tsize;
1034 		args.data.data_len = (uint_t)tsize;
1035 		args.data.data_val = base;
1036 
1037 		if (mi->mi_io_kstats) {
1038 			mutex_enter(&mi->mi_lock);
1039 			kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
1040 			mutex_exit(&mi->mi_lock);
1041 		}
1042 		args.mblk = NULL;
1043 		do {
1044 			error = rfs3call(mi, NFSPROC3_WRITE,
1045 			    xdr_WRITE3args, (caddr_t)&args,
1046 			    xdr_WRITE3res, (caddr_t)&res, cr,
1047 			    &douprintf, &res.status, 0, NULL);
1048 		} while (error == ENFS_TRYAGAIN);
1049 		if (mi->mi_io_kstats) {
1050 			mutex_enter(&mi->mi_lock);
1051 			kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
1052 			mutex_exit(&mi->mi_lock);
1053 		}
1054 
1055 		if (error)
1056 			return (error);
1057 		error = geterrno3(res.status);
1058 		if (!error) {
1059 			if (res.resok.count > args.count) {
1060 				zcmn_err(getzoneid(), CE_WARN,
1061 				    "nfs3write: server %s wrote %u, "
1062 				    "requested was %u",
1063 				    rp->r_server->sv_hostname,
1064 				    res.resok.count, args.count);
1065 				return (EIO);
1066 			}
1067 			if (res.resok.committed == UNSTABLE) {
1068 				*stab_comm = UNSTABLE;
1069 				if (args.stable == DATA_SYNC ||
1070 				    args.stable == FILE_SYNC) {
1071 					zcmn_err(getzoneid(), CE_WARN,
1072 			"nfs3write: server %s did not commit to stable storage",
1073 					    rp->r_server->sv_hostname);
1074 					return (EIO);
1075 				}
1076 			}
1077 			tsize = (int)res.resok.count;
1078 			count -= tsize;
1079 			base += tsize;
1080 			offset += tsize;
1081 			if (mi->mi_io_kstats) {
1082 				mutex_enter(&mi->mi_lock);
1083 				KSTAT_IO_PTR(mi->mi_io_kstats)->writes++;
1084 				KSTAT_IO_PTR(mi->mi_io_kstats)->nwritten +=
1085 				    tsize;
1086 				mutex_exit(&mi->mi_lock);
1087 			}
1088 			lwp_stat_update(LWP_STAT_OUBLK, 1);
1089 			mutex_enter(&rp->r_statelock);
1090 			if (rp->r_flags & RHAVEVERF) {
1091 				if (rp->r_verf != res.resok.verf) {
1092 					nfs3_set_mod(vp);
1093 					rp->r_verf = res.resok.verf;
1094 					/*
1095 					 * If the data was written UNSTABLE,
1096 					 * then might as well stop because
1097 					 * the whole block will have to get
1098 					 * rewritten anyway.
1099 					 */
1100 					if (*stab_comm == UNSTABLE) {
1101 						mutex_exit(&rp->r_statelock);
1102 						break;
1103 					}
1104 				}
1105 			} else {
1106 				rp->r_verf = res.resok.verf;
1107 				rp->r_flags |= RHAVEVERF;
1108 			}
1109 			/*
1110 			 * Mark the attribute cache as timed out and
1111 			 * set RWRITEATTR to indicate that the file
1112 			 * was modified with a WRITE operation and
1113 			 * that the attributes can not be trusted.
1114 			 */
1115 			PURGE_ATTRCACHE_LOCKED(rp);
1116 			rp->r_flags |= RWRITEATTR;
1117 			mutex_exit(&rp->r_statelock);
1118 		}
1119 	} while (!error && count);
1120 
1121 	return (error);
1122 }
1123 
1124 /*
1125  * Read from a file.  Reads data in largest chunks our interface can handle.
1126  */
1127 static int
nfs3read(vnode_t * vp,caddr_t base,offset_t offset,int count,size_t * residp,cred_t * cr)1128 nfs3read(vnode_t *vp, caddr_t base, offset_t offset, int count,
1129 	size_t *residp, cred_t *cr)
1130 {
1131 	mntinfo_t *mi;
1132 	READ3args args;
1133 	READ3vres res;
1134 	int tsize;
1135 	int error;
1136 	int douprintf;
1137 	failinfo_t fi;
1138 	rnode_t *rp;
1139 	struct vattr va;
1140 	hrtime_t t;
1141 
1142 	rp = VTOR(vp);
1143 	mi = VTOMI(vp);
1144 	ASSERT(nfs_zone() == mi->mi_zone);
1145 	douprintf = 1;
1146 
1147 	args.file = *VTOFH3(vp);
1148 	fi.vp = vp;
1149 	fi.fhp = (caddr_t)&args.file;
1150 	fi.copyproc = nfs3copyfh;
1151 	fi.lookupproc = nfs3lookup;
1152 	fi.xattrdirproc = acl_getxattrdir3;
1153 
1154 	res.pov.fres.vp = vp;
1155 	res.pov.fres.vap = &va;
1156 
1157 	res.wlist = NULL;
1158 	*residp = count;
1159 	do {
1160 		if (mi->mi_io_kstats) {
1161 			mutex_enter(&mi->mi_lock);
1162 			kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
1163 			mutex_exit(&mi->mi_lock);
1164 		}
1165 
1166 		do {
1167 			if ((vp->v_flag & VNOCACHE) ||
1168 			    (rp->r_flags & RDIRECTIO) ||
1169 			    (mi->mi_flags & MI_DIRECTIO))
1170 				tsize = MIN(mi->mi_tsize, count);
1171 			else
1172 				tsize = MIN(mi->mi_curread, count);
1173 			res.data.data_val = base;
1174 			res.data.data_len = tsize;
1175 			args.offset = (offset3)offset;
1176 			args.count = (count3)tsize;
1177 			args.res_uiop = NULL;
1178 			args.res_data_val_alt = base;
1179 
1180 			t = gethrtime();
1181 			error = rfs3call(mi, NFSPROC3_READ,
1182 			    xdr_READ3args, (caddr_t)&args,
1183 			    xdr_READ3vres, (caddr_t)&res, cr,
1184 			    &douprintf, &res.status, 0, &fi);
1185 		} while (error == ENFS_TRYAGAIN);
1186 
1187 		if (mi->mi_io_kstats) {
1188 			mutex_enter(&mi->mi_lock);
1189 			kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
1190 			mutex_exit(&mi->mi_lock);
1191 		}
1192 
1193 		if (error)
1194 			return (error);
1195 
1196 		error = geterrno3(res.status);
1197 		if (error)
1198 			return (error);
1199 
1200 		if (res.count != res.data.data_len) {
1201 			zcmn_err(getzoneid(), CE_WARN,
1202 			    "nfs3read: server %s returned incorrect amount",
1203 			    rp->r_server->sv_hostname);
1204 			return (EIO);
1205 		}
1206 
1207 		count -= res.count;
1208 		*residp = count;
1209 		base += res.count;
1210 		offset += res.count;
1211 		if (mi->mi_io_kstats) {
1212 			mutex_enter(&mi->mi_lock);
1213 			KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
1214 			KSTAT_IO_PTR(mi->mi_io_kstats)->nread += res.count;
1215 			mutex_exit(&mi->mi_lock);
1216 		}
1217 		lwp_stat_update(LWP_STAT_INBLK, 1);
1218 	} while (count && !res.eof);
1219 
1220 	if (res.pov.attributes) {
1221 		mutex_enter(&rp->r_statelock);
1222 		if (!CACHE_VALID(rp, va.va_mtime, va.va_size)) {
1223 			mutex_exit(&rp->r_statelock);
1224 			PURGE_ATTRCACHE(vp);
1225 		} else {
1226 			if (rp->r_mtime <= t)
1227 				nfs_attrcache_va(vp, &va);
1228 			mutex_exit(&rp->r_statelock);
1229 		}
1230 	}
1231 
1232 	return (0);
1233 }
1234 
1235 /* ARGSUSED */
1236 static int
nfs3_ioctl(vnode_t * vp,int cmd,intptr_t arg,int flag,cred_t * cr,int * rvalp,caller_context_t * ct)1237 nfs3_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp,
1238 	caller_context_t *ct)
1239 {
1240 
1241 	if (nfs_zone() != VTOMI(vp)->mi_zone)
1242 		return (EIO);
1243 	switch (cmd) {
1244 		case _FIODIRECTIO:
1245 			return (nfs_directio(vp, (int)arg, cr));
1246 		default:
1247 			return (ENOTTY);
1248 	}
1249 }
1250 
1251 /* ARGSUSED */
1252 static int
nfs3_getattr(vnode_t * vp,struct vattr * vap,int flags,cred_t * cr,caller_context_t * ct)1253 nfs3_getattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
1254 	caller_context_t *ct)
1255 {
1256 	int error;
1257 	rnode_t *rp;
1258 
1259 	if (nfs_zone() != VTOMI(vp)->mi_zone)
1260 		return (EIO);
1261 	/*
1262 	 * If it has been specified that the return value will
1263 	 * just be used as a hint, and we are only being asked
1264 	 * for size, fsid or rdevid, then return the client's
1265 	 * notion of these values without checking to make sure
1266 	 * that the attribute cache is up to date.
1267 	 * The whole point is to avoid an over the wire GETATTR
1268 	 * call.
1269 	 */
1270 	rp = VTOR(vp);
1271 	if (flags & ATTR_HINT) {
1272 		if (vap->va_mask ==
1273 		    (vap->va_mask & (AT_SIZE | AT_FSID | AT_RDEV))) {
1274 			mutex_enter(&rp->r_statelock);
1275 			if (vap->va_mask | AT_SIZE)
1276 				vap->va_size = rp->r_size;
1277 			if (vap->va_mask | AT_FSID)
1278 				vap->va_fsid = rp->r_attr.va_fsid;
1279 			if (vap->va_mask | AT_RDEV)
1280 				vap->va_rdev = rp->r_attr.va_rdev;
1281 			mutex_exit(&rp->r_statelock);
1282 			return (0);
1283 		}
1284 	}
1285 
1286 	/*
1287 	 * Only need to flush pages if asking for the mtime
1288 	 * and if there any dirty pages or any outstanding
1289 	 * asynchronous (write) requests for this file.
1290 	 */
1291 	if (vap->va_mask & AT_MTIME) {
1292 		if (vn_has_cached_data(vp) &&
1293 		    ((rp->r_flags & RDIRTY) || rp->r_awcount > 0)) {
1294 			mutex_enter(&rp->r_statelock);
1295 			rp->r_gcount++;
1296 			mutex_exit(&rp->r_statelock);
1297 			error = nfs3_putpage(vp, (offset_t)0, 0, 0, cr, ct);
1298 			mutex_enter(&rp->r_statelock);
1299 			if (error && (error == ENOSPC || error == EDQUOT)) {
1300 				if (!rp->r_error)
1301 					rp->r_error = error;
1302 			}
1303 			if (--rp->r_gcount == 0)
1304 				cv_broadcast(&rp->r_cv);
1305 			mutex_exit(&rp->r_statelock);
1306 		}
1307 	}
1308 
1309 	return (nfs3getattr(vp, vap, cr));
1310 }
1311 
1312 /*ARGSUSED4*/
1313 static int
nfs3_setattr(vnode_t * vp,struct vattr * vap,int flags,cred_t * cr,caller_context_t * ct)1314 nfs3_setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
1315 		caller_context_t *ct)
1316 {
1317 	int error;
1318 	struct vattr va;
1319 
1320 	if (vap->va_mask & AT_NOSET)
1321 		return (EINVAL);
1322 	if (nfs_zone() != VTOMI(vp)->mi_zone)
1323 		return (EIO);
1324 
1325 	va.va_mask = AT_UID | AT_MODE;
1326 	error = nfs3getattr(vp, &va, cr);
1327 	if (error)
1328 		return (error);
1329 
1330 	error = secpolicy_vnode_setattr(cr, vp, vap, &va, flags, nfs3_accessx,
1331 	    vp);
1332 	if (error)
1333 		return (error);
1334 
1335 	error = nfs3setattr(vp, vap, flags, cr);
1336 
1337 	if (error == 0 && (vap->va_mask & AT_SIZE) && vap->va_size == 0)
1338 		vnevent_truncate(vp, ct);
1339 
1340 	return (error);
1341 }
1342 
1343 static int
nfs3setattr(vnode_t * vp,struct vattr * vap,int flags,cred_t * cr)1344 nfs3setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr)
1345 {
1346 	int error;
1347 	uint_t mask;
1348 	SETATTR3args args;
1349 	SETATTR3res res;
1350 	int douprintf;
1351 	rnode_t *rp;
1352 	struct vattr va;
1353 	mode_t omode;
1354 	vsecattr_t *vsp;
1355 	hrtime_t t;
1356 
1357 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
1358 	mask = vap->va_mask;
1359 
1360 	rp = VTOR(vp);
1361 
1362 	/*
1363 	 * Only need to flush pages if there are any pages and
1364 	 * if the file is marked as dirty in some fashion.  The
1365 	 * file must be flushed so that we can accurately
1366 	 * determine the size of the file and the cached data
1367 	 * after the SETATTR returns.  A file is considered to
1368 	 * be dirty if it is either marked with RDIRTY, has
1369 	 * outstanding i/o's active, or is mmap'd.  In this
1370 	 * last case, we can't tell whether there are dirty
1371 	 * pages, so we flush just to be sure.
1372 	 */
1373 	if (vn_has_cached_data(vp) &&
1374 	    ((rp->r_flags & RDIRTY) ||
1375 	    rp->r_count > 0 ||
1376 	    rp->r_mapcnt > 0)) {
1377 		ASSERT(vp->v_type != VCHR);
1378 		error = nfs3_putpage(vp, (offset_t)0, 0, 0, cr, NULL);
1379 		if (error && (error == ENOSPC || error == EDQUOT)) {
1380 			mutex_enter(&rp->r_statelock);
1381 			if (!rp->r_error)
1382 				rp->r_error = error;
1383 			mutex_exit(&rp->r_statelock);
1384 		}
1385 	}
1386 
1387 	args.object = *RTOFH3(rp);
1388 	/*
1389 	 * If the intent is for the server to set the times,
1390 	 * there is no point in have the mask indicating set mtime or
1391 	 * atime, because the vap values may be junk, and so result
1392 	 * in an overflow error. Remove these flags from the vap mask
1393 	 * before calling in this case, and restore them afterwards.
1394 	 */
1395 	if ((mask & (AT_ATIME | AT_MTIME)) && !(flags & ATTR_UTIME)) {
1396 		/* Use server times, so don't set the args time fields */
1397 		vap->va_mask &= ~(AT_ATIME | AT_MTIME);
1398 		error = vattr_to_sattr3(vap, &args.new_attributes);
1399 		vap->va_mask |= (mask & (AT_ATIME | AT_MTIME));
1400 		if (mask & AT_ATIME) {
1401 			args.new_attributes.atime.set_it = SET_TO_SERVER_TIME;
1402 		}
1403 		if (mask & AT_MTIME) {
1404 			args.new_attributes.mtime.set_it = SET_TO_SERVER_TIME;
1405 		}
1406 	} else {
1407 		/* Either do not set times or use the client specified times */
1408 		error = vattr_to_sattr3(vap, &args.new_attributes);
1409 	}
1410 
1411 	if (error) {
1412 		/* req time field(s) overflow - return immediately */
1413 		return (error);
1414 	}
1415 
1416 	va.va_mask = AT_MODE | AT_CTIME;
1417 	error = nfs3getattr(vp, &va, cr);
1418 	if (error)
1419 		return (error);
1420 	omode = va.va_mode;
1421 
1422 tryagain:
1423 	if (mask & AT_SIZE) {
1424 		args.guard.check = TRUE;
1425 		args.guard.obj_ctime.seconds = va.va_ctime.tv_sec;
1426 		args.guard.obj_ctime.nseconds = va.va_ctime.tv_nsec;
1427 	} else
1428 		args.guard.check = FALSE;
1429 
1430 	douprintf = 1;
1431 
1432 	t = gethrtime();
1433 
1434 	error = rfs3call(VTOMI(vp), NFSPROC3_SETATTR,
1435 	    xdr_SETATTR3args, (caddr_t)&args,
1436 	    xdr_SETATTR3res, (caddr_t)&res, cr,
1437 	    &douprintf, &res.status, 0, NULL);
1438 
1439 	/*
1440 	 * Purge the access cache and ACL cache if changing either the
1441 	 * owner of the file, the group owner, or the mode.  These may
1442 	 * change the access permissions of the file, so purge old
1443 	 * information and start over again.
1444 	 */
1445 	if (mask & (AT_UID | AT_GID | AT_MODE)) {
1446 		(void) nfs_access_purge_rp(rp);
1447 		if (rp->r_secattr != NULL) {
1448 			mutex_enter(&rp->r_statelock);
1449 			vsp = rp->r_secattr;
1450 			rp->r_secattr = NULL;
1451 			mutex_exit(&rp->r_statelock);
1452 			if (vsp != NULL)
1453 				nfs_acl_free(vsp);
1454 		}
1455 	}
1456 
1457 	if (error) {
1458 		PURGE_ATTRCACHE(vp);
1459 		return (error);
1460 	}
1461 
1462 	error = geterrno3(res.status);
1463 	if (!error) {
1464 		/*
1465 		 * If changing the size of the file, invalidate
1466 		 * any local cached data which is no longer part
1467 		 * of the file.  We also possibly invalidate the
1468 		 * last page in the file.  We could use
1469 		 * pvn_vpzero(), but this would mark the page as
1470 		 * modified and require it to be written back to
1471 		 * the server for no particularly good reason.
1472 		 * This way, if we access it, then we bring it
1473 		 * back in.  A read should be cheaper than a
1474 		 * write.
1475 		 */
1476 		if (mask & AT_SIZE) {
1477 			nfs_invalidate_pages(vp,
1478 			    (vap->va_size & PAGEMASK), cr);
1479 		}
1480 		nfs3_cache_wcc_data(vp, &res.resok.obj_wcc, t, cr);
1481 		/*
1482 		 * Some servers will change the mode to clear the setuid
1483 		 * and setgid bits when changing the uid or gid.  The
1484 		 * client needs to compensate appropriately.
1485 		 */
1486 		if (mask & (AT_UID | AT_GID)) {
1487 			int terror;
1488 
1489 			va.va_mask = AT_MODE;
1490 			terror = nfs3getattr(vp, &va, cr);
1491 			if (!terror &&
1492 			    (((mask & AT_MODE) && va.va_mode != vap->va_mode) ||
1493 			    (!(mask & AT_MODE) && va.va_mode != omode))) {
1494 				va.va_mask = AT_MODE;
1495 				if (mask & AT_MODE)
1496 					va.va_mode = vap->va_mode;
1497 				else
1498 					va.va_mode = omode;
1499 				(void) nfs3setattr(vp, &va, 0, cr);
1500 			}
1501 		}
1502 	} else {
1503 		nfs3_cache_wcc_data(vp, &res.resfail.obj_wcc, t, cr);
1504 		/*
1505 		 * If we got back a "not synchronized" error, then
1506 		 * we need to retry with a new guard value.  The
1507 		 * guard value used is the change time.  If the
1508 		 * server returned post_op_attr, then we can just
1509 		 * retry because we have the latest attributes.
1510 		 * Otherwise, we issue a GETATTR to get the latest
1511 		 * attributes and then retry.  If we couldn't get
1512 		 * the attributes this way either, then we give
1513 		 * up because we can't complete the operation as
1514 		 * required.
1515 		 */
1516 		if (res.status == NFS3ERR_NOT_SYNC) {
1517 			va.va_mask = AT_CTIME;
1518 			if (nfs3getattr(vp, &va, cr) == 0)
1519 				goto tryagain;
1520 		}
1521 		PURGE_STALE_FH(error, vp, cr);
1522 	}
1523 
1524 	return (error);
1525 }
1526 
1527 static int
nfs3_accessx(void * vp,int mode,cred_t * cr)1528 nfs3_accessx(void *vp, int mode, cred_t *cr)
1529 {
1530 	ASSERT(nfs_zone() == VTOMI((vnode_t *)vp)->mi_zone);
1531 	return (nfs3_access(vp, mode, 0, cr, NULL));
1532 }
1533 
1534 /* ARGSUSED */
1535 static int
nfs3_access(vnode_t * vp,int mode,int flags,cred_t * cr,caller_context_t * ct)1536 nfs3_access(vnode_t *vp, int mode, int flags, cred_t *cr, caller_context_t *ct)
1537 {
1538 	int error;
1539 	ACCESS3args args;
1540 	ACCESS3res res;
1541 	int douprintf;
1542 	uint32 acc;
1543 	rnode_t *rp;
1544 	cred_t *cred, *ncr, *ncrfree = NULL;
1545 	failinfo_t fi;
1546 	nfs_access_type_t cacc;
1547 	hrtime_t t;
1548 
1549 	acc = 0;
1550 	if (nfs_zone() != VTOMI(vp)->mi_zone)
1551 		return (EIO);
1552 	if (mode & VREAD)
1553 		acc |= ACCESS3_READ;
1554 	if (mode & VWRITE) {
1555 		if (vn_is_readonly(vp) && !IS_DEVVP(vp))
1556 			return (EROFS);
1557 		if (vp->v_type == VDIR)
1558 			acc |= ACCESS3_DELETE;
1559 		acc |= ACCESS3_MODIFY | ACCESS3_EXTEND;
1560 	}
1561 	if (mode & VEXEC) {
1562 		if (vp->v_type == VDIR)
1563 			acc |= ACCESS3_LOOKUP;
1564 		else
1565 			acc |= ACCESS3_EXECUTE;
1566 	}
1567 
1568 	rp = VTOR(vp);
1569 	args.object = *VTOFH3(vp);
1570 	if (vp->v_type == VDIR) {
1571 		args.access = ACCESS3_READ | ACCESS3_DELETE | ACCESS3_MODIFY |
1572 		    ACCESS3_EXTEND | ACCESS3_LOOKUP;
1573 	} else {
1574 		args.access = ACCESS3_READ | ACCESS3_MODIFY | ACCESS3_EXTEND |
1575 		    ACCESS3_EXECUTE;
1576 	}
1577 	fi.vp = vp;
1578 	fi.fhp = (caddr_t)&args.object;
1579 	fi.copyproc = nfs3copyfh;
1580 	fi.lookupproc = nfs3lookup;
1581 	fi.xattrdirproc = acl_getxattrdir3;
1582 
1583 	cred = cr;
1584 	/*
1585 	 * ncr and ncrfree both initially
1586 	 * point to the memory area returned
1587 	 * by crnetadjust();
1588 	 * ncrfree not NULL when exiting means
1589 	 * that we need to release it
1590 	 */
1591 	ncr = crnetadjust(cred);
1592 	ncrfree = ncr;
1593 tryagain:
1594 	if (rp->r_acache != NULL) {
1595 		cacc = nfs_access_check(rp, acc, cred);
1596 		if (cacc == NFS_ACCESS_ALLOWED) {
1597 			if (ncrfree != NULL)
1598 				crfree(ncrfree);
1599 			return (0);
1600 		}
1601 		if (cacc == NFS_ACCESS_DENIED) {
1602 			/*
1603 			 * If the cred can be adjusted, try again
1604 			 * with the new cred.
1605 			 */
1606 			if (ncr != NULL) {
1607 				cred = ncr;
1608 				ncr = NULL;
1609 				goto tryagain;
1610 			}
1611 			if (ncrfree != NULL)
1612 				crfree(ncrfree);
1613 			return (EACCES);
1614 		}
1615 	}
1616 
1617 	douprintf = 1;
1618 
1619 	t = gethrtime();
1620 
1621 	error = rfs3call(VTOMI(vp), NFSPROC3_ACCESS,
1622 	    xdr_ACCESS3args, (caddr_t)&args,
1623 	    xdr_ACCESS3res, (caddr_t)&res, cred,
1624 	    &douprintf, &res.status, 0, &fi);
1625 
1626 	if (error) {
1627 		if (ncrfree != NULL)
1628 			crfree(ncrfree);
1629 		return (error);
1630 	}
1631 
1632 	error = geterrno3(res.status);
1633 	if (!error) {
1634 		nfs3_cache_post_op_attr(vp, &res.resok.obj_attributes, t, cr);
1635 		nfs_access_cache(rp, args.access, res.resok.access, cred);
1636 		/*
1637 		 * we just cached results with cred; if cred is the
1638 		 * adjusted credentials from crnetadjust, we do not want
1639 		 * to release them before exiting: hence setting ncrfree
1640 		 * to NULL
1641 		 */
1642 		if (cred != cr)
1643 			ncrfree = NULL;
1644 		if ((acc & res.resok.access) != acc) {
1645 			/*
1646 			 * If the cred can be adjusted, try again
1647 			 * with the new cred.
1648 			 */
1649 			if (ncr != NULL) {
1650 				cred = ncr;
1651 				ncr = NULL;
1652 				goto tryagain;
1653 			}
1654 			error = EACCES;
1655 		}
1656 	} else {
1657 		nfs3_cache_post_op_attr(vp, &res.resfail.obj_attributes, t, cr);
1658 		PURGE_STALE_FH(error, vp, cr);
1659 	}
1660 
1661 	if (ncrfree != NULL)
1662 		crfree(ncrfree);
1663 
1664 	return (error);
1665 }
1666 
1667 static int nfs3_do_symlink_cache = 1;
1668 
1669 /* ARGSUSED */
1670 static int
nfs3_readlink(vnode_t * vp,struct uio * uiop,cred_t * cr,caller_context_t * ct)1671 nfs3_readlink(vnode_t *vp, struct uio *uiop, cred_t *cr, caller_context_t *ct)
1672 {
1673 	int error;
1674 	READLINK3args args;
1675 	READLINK3res res;
1676 	nfspath3 resdata_backup;
1677 	rnode_t *rp;
1678 	int douprintf;
1679 	int len;
1680 	failinfo_t fi;
1681 	hrtime_t t;
1682 
1683 	/*
1684 	 * Can't readlink anything other than a symbolic link.
1685 	 */
1686 	if (vp->v_type != VLNK)
1687 		return (EINVAL);
1688 	if (nfs_zone() != VTOMI(vp)->mi_zone)
1689 		return (EIO);
1690 
1691 	rp = VTOR(vp);
1692 	if (nfs3_do_symlink_cache && rp->r_symlink.contents != NULL) {
1693 		error = nfs3_validate_caches(vp, cr);
1694 		if (error)
1695 			return (error);
1696 		mutex_enter(&rp->r_statelock);
1697 		if (rp->r_symlink.contents != NULL) {
1698 			error = uiomove(rp->r_symlink.contents,
1699 			    rp->r_symlink.len, UIO_READ, uiop);
1700 			mutex_exit(&rp->r_statelock);
1701 			return (error);
1702 		}
1703 		mutex_exit(&rp->r_statelock);
1704 	}
1705 
1706 	args.symlink = *VTOFH3(vp);
1707 	fi.vp = vp;
1708 	fi.fhp = (caddr_t)&args.symlink;
1709 	fi.copyproc = nfs3copyfh;
1710 	fi.lookupproc = nfs3lookup;
1711 	fi.xattrdirproc = acl_getxattrdir3;
1712 
1713 	res.resok.data = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1714 
1715 	resdata_backup = res.resok.data;
1716 
1717 	douprintf = 1;
1718 
1719 	t = gethrtime();
1720 
1721 	error = rfs3call(VTOMI(vp), NFSPROC3_READLINK,
1722 	    xdr_READLINK3args, (caddr_t)&args,
1723 	    xdr_READLINK3res, (caddr_t)&res, cr,
1724 	    &douprintf, &res.status, 0, &fi);
1725 
1726 	if (res.resok.data == nfs3nametoolong)
1727 		error = EINVAL;
1728 
1729 	if (error) {
1730 		kmem_free(resdata_backup, MAXPATHLEN);
1731 		return (error);
1732 	}
1733 
1734 	error = geterrno3(res.status);
1735 	if (!error) {
1736 		nfs3_cache_post_op_attr(vp, &res.resok.symlink_attributes, t,
1737 		    cr);
1738 		len = strlen(res.resok.data);
1739 		error = uiomove(res.resok.data, len, UIO_READ, uiop);
1740 		if (nfs3_do_symlink_cache && rp->r_symlink.contents == NULL) {
1741 			mutex_enter(&rp->r_statelock);
1742 				if (rp->r_symlink.contents == NULL) {
1743 				rp->r_symlink.contents = res.resok.data;
1744 				rp->r_symlink.len = len;
1745 				rp->r_symlink.size = MAXPATHLEN;
1746 				mutex_exit(&rp->r_statelock);
1747 			} else {
1748 				mutex_exit(&rp->r_statelock);
1749 
1750 				kmem_free((void *)res.resok.data, MAXPATHLEN);
1751 			}
1752 		} else {
1753 			kmem_free((void *)res.resok.data, MAXPATHLEN);
1754 		}
1755 	} else {
1756 		nfs3_cache_post_op_attr(vp,
1757 		    &res.resfail.symlink_attributes, t, cr);
1758 		PURGE_STALE_FH(error, vp, cr);
1759 
1760 		kmem_free((void *)res.resok.data, MAXPATHLEN);
1761 
1762 	}
1763 
1764 	/*
1765 	 * The over the wire error for attempting to readlink something
1766 	 * other than a symbolic link is ENXIO.  However, we need to
1767 	 * return EINVAL instead of ENXIO, so we map it here.
1768 	 */
1769 	return (error == ENXIO ? EINVAL : error);
1770 }
1771 
1772 /*
1773  * Flush local dirty pages to stable storage on the server.
1774  *
1775  * If FNODSYNC is specified, then there is nothing to do because
1776  * metadata changes are not cached on the client before being
1777  * sent to the server.
1778  */
1779 /* ARGSUSED */
1780 static int
nfs3_fsync(vnode_t * vp,int syncflag,cred_t * cr,caller_context_t * ct)1781 nfs3_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
1782 {
1783 	int error;
1784 
1785 	if ((syncflag & FNODSYNC) || IS_SWAPVP(vp))
1786 		return (0);
1787 	if (nfs_zone() != VTOMI(vp)->mi_zone)
1788 		return (EIO);
1789 
1790 	error = nfs3_putpage_commit(vp, (offset_t)0, 0, cr);
1791 	if (!error)
1792 		error = VTOR(vp)->r_error;
1793 	return (error);
1794 }
1795 
1796 /*
1797  * Weirdness: if the file was removed or the target of a rename
1798  * operation while it was open, it got renamed instead.  Here we
1799  * remove the renamed file.
1800  */
1801 /* ARGSUSED */
1802 static void
nfs3_inactive(vnode_t * vp,cred_t * cr,caller_context_t * ct)1803 nfs3_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
1804 {
1805 	rnode_t *rp;
1806 
1807 	ASSERT(vp != DNLC_NO_VNODE);
1808 
1809 	/*
1810 	 * If this is coming from the wrong zone, we let someone in the right
1811 	 * zone take care of it asynchronously.  We can get here due to
1812 	 * VN_RELE() being called from pageout() or fsflush().  This call may
1813 	 * potentially turn into an expensive no-op if, for instance, v_count
1814 	 * gets incremented in the meantime, but it's still correct.
1815 	 */
1816 	if (nfs_zone() != VTOMI(vp)->mi_zone) {
1817 		nfs_async_inactive(vp, cr, nfs3_inactive);
1818 		return;
1819 	}
1820 
1821 	rp = VTOR(vp);
1822 redo:
1823 	if (rp->r_unldvp != NULL) {
1824 		/*
1825 		 * Save the vnode pointer for the directory where the
1826 		 * unlinked-open file got renamed, then set it to NULL
1827 		 * to prevent another thread from getting here before
1828 		 * we're done with the remove.  While we have the
1829 		 * statelock, make local copies of the pertinent rnode
1830 		 * fields.  If we weren't to do this in an atomic way, the
1831 		 * the unl* fields could become inconsistent with respect
1832 		 * to each other due to a race condition between this
1833 		 * code and nfs_remove().  See bug report 1034328.
1834 		 */
1835 		mutex_enter(&rp->r_statelock);
1836 		if (rp->r_unldvp != NULL) {
1837 			vnode_t *unldvp;
1838 			char *unlname;
1839 			cred_t *unlcred;
1840 			REMOVE3args args;
1841 			REMOVE3res res;
1842 			int douprintf;
1843 			int error;
1844 			hrtime_t t;
1845 
1846 			unldvp = rp->r_unldvp;
1847 			rp->r_unldvp = NULL;
1848 			unlname = rp->r_unlname;
1849 			rp->r_unlname = NULL;
1850 			unlcred = rp->r_unlcred;
1851 			rp->r_unlcred = NULL;
1852 			mutex_exit(&rp->r_statelock);
1853 
1854 			/*
1855 			 * If there are any dirty pages left, then flush
1856 			 * them.  This is unfortunate because they just
1857 			 * may get thrown away during the remove operation,
1858 			 * but we have to do this for correctness.
1859 			 */
1860 			if (vn_has_cached_data(vp) &&
1861 			    ((rp->r_flags & RDIRTY) || rp->r_count > 0)) {
1862 				ASSERT(vp->v_type != VCHR);
1863 				error = nfs3_putpage(vp, (offset_t)0, 0, 0,
1864 				    cr, ct);
1865 				if (error) {
1866 					mutex_enter(&rp->r_statelock);
1867 					if (!rp->r_error)
1868 						rp->r_error = error;
1869 					mutex_exit(&rp->r_statelock);
1870 				}
1871 			}
1872 
1873 			/*
1874 			 * Do the remove operation on the renamed file
1875 			 */
1876 			setdiropargs3(&args.object, unlname, unldvp);
1877 
1878 			douprintf = 1;
1879 
1880 			t = gethrtime();
1881 
1882 			error = rfs3call(VTOMI(unldvp), NFSPROC3_REMOVE,
1883 			    xdr_diropargs3, (caddr_t)&args,
1884 			    xdr_REMOVE3res, (caddr_t)&res, unlcred,
1885 			    &douprintf, &res.status, 0, NULL);
1886 
1887 			if (error) {
1888 				PURGE_ATTRCACHE(unldvp);
1889 			} else {
1890 				error = geterrno3(res.status);
1891 				if (!error) {
1892 					nfs3_cache_wcc_data(unldvp,
1893 					    &res.resok.dir_wcc, t, cr);
1894 					if (HAVE_RDDIR_CACHE(VTOR(unldvp)))
1895 						nfs_purge_rddir_cache(unldvp);
1896 				} else {
1897 					nfs3_cache_wcc_data(unldvp,
1898 					    &res.resfail.dir_wcc, t, cr);
1899 					PURGE_STALE_FH(error, unldvp, cr);
1900 				}
1901 			}
1902 
1903 			/*
1904 			 * Release stuff held for the remove
1905 			 */
1906 			VN_RELE(unldvp);
1907 			kmem_free(unlname, MAXNAMELEN);
1908 			crfree(unlcred);
1909 			goto redo;
1910 		}
1911 		mutex_exit(&rp->r_statelock);
1912 	}
1913 
1914 	rp_addfree(rp, cr);
1915 }
1916 
1917 /*
1918  * Remote file system operations having to do with directory manipulation.
1919  */
1920 
1921 /* ARGSUSED */
1922 static int
nfs3_lookup(vnode_t * dvp,char * nm,vnode_t ** vpp,struct pathname * pnp,int flags,vnode_t * rdir,cred_t * cr,caller_context_t * ct,int * direntflags,pathname_t * realpnp)1923 nfs3_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
1924 	int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
1925 	int *direntflags, pathname_t *realpnp)
1926 {
1927 	int error;
1928 	vnode_t *vp;
1929 	vnode_t *avp = NULL;
1930 	rnode_t *drp;
1931 
1932 	if (nfs_zone() != VTOMI(dvp)->mi_zone)
1933 		return (EPERM);
1934 
1935 	drp = VTOR(dvp);
1936 
1937 	/*
1938 	 * Are we looking up extended attributes?  If so, "dvp" is
1939 	 * the file or directory for which we want attributes, and
1940 	 * we need a lookup of the hidden attribute directory
1941 	 * before we lookup the rest of the path.
1942 	 */
1943 	if (flags & LOOKUP_XATTR) {
1944 		bool_t cflag = ((flags & CREATE_XATTR_DIR) != 0);
1945 		mntinfo_t *mi;
1946 
1947 		mi = VTOMI(dvp);
1948 		if (!(mi->mi_flags & MI_EXTATTR))
1949 			return (EINVAL);
1950 
1951 		if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR(dvp)))
1952 			return (EINTR);
1953 
1954 		(void) nfs3lookup_dnlc(dvp, XATTR_DIR_NAME, &avp, cr);
1955 		if (avp == NULL)
1956 			error = acl_getxattrdir3(dvp, &avp, cflag, cr, 0);
1957 		else
1958 			error = 0;
1959 
1960 		nfs_rw_exit(&drp->r_rwlock);
1961 
1962 		if (error) {
1963 			if (mi->mi_flags & MI_EXTATTR)
1964 				return (error);
1965 			return (EINVAL);
1966 		}
1967 		dvp = avp;
1968 		drp = VTOR(dvp);
1969 	}
1970 
1971 	if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR(dvp))) {
1972 		error = EINTR;
1973 		goto out;
1974 	}
1975 
1976 	error = nfs3lookup(dvp, nm, vpp, pnp, flags, rdir, cr, 0);
1977 
1978 	nfs_rw_exit(&drp->r_rwlock);
1979 
1980 	/*
1981 	 * If vnode is a device, create special vnode.
1982 	 */
1983 	if (!error && IS_DEVVP(*vpp)) {
1984 		vp = *vpp;
1985 		*vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
1986 		VN_RELE(vp);
1987 	}
1988 
1989 out:
1990 	if (avp != NULL)
1991 		VN_RELE(avp);
1992 
1993 	return (error);
1994 }
1995 
1996 static int nfs3_lookup_neg_cache = 1;
1997 
1998 #ifdef DEBUG
1999 static int nfs3_lookup_dnlc_hits = 0;
2000 static int nfs3_lookup_dnlc_misses = 0;
2001 static int nfs3_lookup_dnlc_neg_hits = 0;
2002 static int nfs3_lookup_dnlc_disappears = 0;
2003 static int nfs3_lookup_dnlc_lookups = 0;
2004 #endif
2005 
2006 /* ARGSUSED */
2007 int
nfs3lookup(vnode_t * dvp,char * nm,vnode_t ** vpp,struct pathname * pnp,int flags,vnode_t * rdir,cred_t * cr,int rfscall_flags)2008 nfs3lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
2009 	int flags, vnode_t *rdir, cred_t *cr, int rfscall_flags)
2010 {
2011 	int error;
2012 	rnode_t *drp;
2013 
2014 	ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
2015 	/*
2016 	 * If lookup is for "", just return dvp.  Don't need
2017 	 * to send it over the wire, look it up in the dnlc,
2018 	 * or perform any access checks.
2019 	 */
2020 	if (*nm == '\0') {
2021 		VN_HOLD(dvp);
2022 		*vpp = dvp;
2023 		return (0);
2024 	}
2025 
2026 	/*
2027 	 * Can't do lookups in non-directories.
2028 	 */
2029 	if (dvp->v_type != VDIR)
2030 		return (ENOTDIR);
2031 
2032 	/*
2033 	 * If we're called with RFSCALL_SOFT, it's important that
2034 	 * the only rfscall is one we make directly; if we permit
2035 	 * an access call because we're looking up "." or validating
2036 	 * a dnlc hit, we'll deadlock because that rfscall will not
2037 	 * have the RFSCALL_SOFT set.
2038 	 */
2039 	if (rfscall_flags & RFSCALL_SOFT)
2040 		goto callit;
2041 
2042 	/*
2043 	 * If lookup is for ".", just return dvp.  Don't need
2044 	 * to send it over the wire or look it up in the dnlc,
2045 	 * just need to check access.
2046 	 */
2047 	if (strcmp(nm, ".") == 0) {
2048 		error = nfs3_access(dvp, VEXEC, 0, cr, NULL);
2049 		if (error)
2050 			return (error);
2051 		VN_HOLD(dvp);
2052 		*vpp = dvp;
2053 		return (0);
2054 	}
2055 
2056 	drp = VTOR(dvp);
2057 	if (!(drp->r_flags & RLOOKUP)) {
2058 		mutex_enter(&drp->r_statelock);
2059 		drp->r_flags |= RLOOKUP;
2060 		mutex_exit(&drp->r_statelock);
2061 	}
2062 
2063 	/*
2064 	 * Lookup this name in the DNLC.  If there was a valid entry,
2065 	 * then return the results of the lookup.
2066 	 */
2067 	error = nfs3lookup_dnlc(dvp, nm, vpp, cr);
2068 	if (error || *vpp != NULL)
2069 		return (error);
2070 
2071 callit:
2072 	error = nfs3lookup_otw(dvp, nm, vpp, cr, rfscall_flags);
2073 
2074 	return (error);
2075 }
2076 
2077 static int
nfs3lookup_dnlc(vnode_t * dvp,char * nm,vnode_t ** vpp,cred_t * cr)2078 nfs3lookup_dnlc(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr)
2079 {
2080 	int error;
2081 	vnode_t *vp;
2082 
2083 	ASSERT(*nm != '\0');
2084 	ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
2085 	/*
2086 	 * Lookup this name in the DNLC.  If successful, then validate
2087 	 * the caches and then recheck the DNLC.  The DNLC is rechecked
2088 	 * just in case this entry got invalidated during the call
2089 	 * to nfs3_validate_caches.
2090 	 *
2091 	 * An assumption is being made that it is safe to say that a
2092 	 * file exists which may not on the server.  Any operations to
2093 	 * the server will fail with ESTALE.
2094 	 */
2095 #ifdef DEBUG
2096 	nfs3_lookup_dnlc_lookups++;
2097 #endif
2098 	vp = dnlc_lookup(dvp, nm);
2099 	if (vp != NULL) {
2100 		VN_RELE(vp);
2101 		if (vp == DNLC_NO_VNODE && !vn_is_readonly(dvp)) {
2102 			PURGE_ATTRCACHE(dvp);
2103 		}
2104 		error = nfs3_validate_caches(dvp, cr);
2105 		if (error)
2106 			return (error);
2107 		vp = dnlc_lookup(dvp, nm);
2108 		if (vp != NULL) {
2109 			error = nfs3_access(dvp, VEXEC, 0, cr, NULL);
2110 			if (error) {
2111 				VN_RELE(vp);
2112 				return (error);
2113 			}
2114 			if (vp == DNLC_NO_VNODE) {
2115 				VN_RELE(vp);
2116 #ifdef DEBUG
2117 				nfs3_lookup_dnlc_neg_hits++;
2118 #endif
2119 				return (ENOENT);
2120 			}
2121 			*vpp = vp;
2122 #ifdef DEBUG
2123 			nfs3_lookup_dnlc_hits++;
2124 #endif
2125 			return (0);
2126 		}
2127 #ifdef DEBUG
2128 		nfs3_lookup_dnlc_disappears++;
2129 #endif
2130 	}
2131 #ifdef DEBUG
2132 	else
2133 		nfs3_lookup_dnlc_misses++;
2134 #endif
2135 
2136 	*vpp = NULL;
2137 
2138 	return (0);
2139 }
2140 
2141 static int
nfs3lookup_otw(vnode_t * dvp,char * nm,vnode_t ** vpp,cred_t * cr,int rfscall_flags)2142 nfs3lookup_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr,
2143 	int rfscall_flags)
2144 {
2145 	int error;
2146 	LOOKUP3args args;
2147 	LOOKUP3vres res;
2148 	int douprintf;
2149 	struct vattr vattr;
2150 	struct vattr dvattr;
2151 	vnode_t *vp;
2152 	failinfo_t fi;
2153 	hrtime_t t;
2154 
2155 	ASSERT(*nm != '\0');
2156 	ASSERT(dvp->v_type == VDIR);
2157 	ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
2158 
2159 	setdiropargs3(&args.what, nm, dvp);
2160 
2161 	fi.vp = dvp;
2162 	fi.fhp = (caddr_t)&args.what.dir;
2163 	fi.copyproc = nfs3copyfh;
2164 	fi.lookupproc = nfs3lookup;
2165 	fi.xattrdirproc = acl_getxattrdir3;
2166 	res.obj_attributes.fres.vp = dvp;
2167 	res.obj_attributes.fres.vap = &vattr;
2168 	res.dir_attributes.fres.vp = dvp;
2169 	res.dir_attributes.fres.vap = &dvattr;
2170 
2171 	douprintf = 1;
2172 
2173 	t = gethrtime();
2174 
2175 	error = rfs3call(VTOMI(dvp), NFSPROC3_LOOKUP,
2176 	    xdr_diropargs3, (caddr_t)&args,
2177 	    xdr_LOOKUP3vres, (caddr_t)&res, cr,
2178 	    &douprintf, &res.status, rfscall_flags, &fi);
2179 
2180 	if (error)
2181 		return (error);
2182 
2183 	nfs3_cache_post_op_vattr(dvp, &res.dir_attributes, t, cr);
2184 
2185 	error = geterrno3(res.status);
2186 	if (error) {
2187 		PURGE_STALE_FH(error, dvp, cr);
2188 		if (error == ENOENT && nfs3_lookup_neg_cache)
2189 			dnlc_enter(dvp, nm, DNLC_NO_VNODE);
2190 		return (error);
2191 	}
2192 
2193 	if (res.obj_attributes.attributes) {
2194 		vp = makenfs3node_va(&res.object, res.obj_attributes.fres.vap,
2195 		    dvp->v_vfsp, t, cr, VTOR(dvp)->r_path, nm);
2196 	} else {
2197 		vp = makenfs3node_va(&res.object, NULL,
2198 		    dvp->v_vfsp, t, cr, VTOR(dvp)->r_path, nm);
2199 		if (vp->v_type == VNON) {
2200 			vattr.va_mask = AT_TYPE;
2201 			error = nfs3getattr(vp, &vattr, cr);
2202 			if (error) {
2203 				VN_RELE(vp);
2204 				return (error);
2205 			}
2206 			vp->v_type = vattr.va_type;
2207 		}
2208 	}
2209 
2210 	if (!(rfscall_flags & RFSCALL_SOFT))
2211 		dnlc_update(dvp, nm, vp);
2212 
2213 	*vpp = vp;
2214 
2215 	return (error);
2216 }
2217 
2218 #ifdef DEBUG
2219 static int nfs3_create_misses = 0;
2220 #endif
2221 
2222 /* ARGSUSED */
2223 static int
nfs3_create(vnode_t * dvp,char * nm,struct vattr * va,enum vcexcl exclusive,int mode,vnode_t ** vpp,cred_t * cr,int lfaware,caller_context_t * ct,vsecattr_t * vsecp)2224 nfs3_create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
2225 	int mode, vnode_t **vpp, cred_t *cr, int lfaware, caller_context_t *ct,
2226 	vsecattr_t *vsecp)
2227 {
2228 	int error;
2229 	vnode_t *vp;
2230 	rnode_t *rp;
2231 	struct vattr vattr;
2232 	rnode_t *drp;
2233 	vnode_t *tempvp;
2234 
2235 	drp = VTOR(dvp);
2236 	if (nfs_zone() != VTOMI(dvp)->mi_zone)
2237 		return (EPERM);
2238 	if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
2239 		return (EINTR);
2240 
2241 top:
2242 	/*
2243 	 * We make a copy of the attributes because the caller does not
2244 	 * expect us to change what va points to.
2245 	 */
2246 	vattr = *va;
2247 
2248 	/*
2249 	 * If the pathname is "", just use dvp.  Don't need
2250 	 * to send it over the wire, look it up in the dnlc,
2251 	 * or perform any access checks.
2252 	 */
2253 	if (*nm == '\0') {
2254 		error = 0;
2255 		VN_HOLD(dvp);
2256 		vp = dvp;
2257 	/*
2258 	 * If the pathname is ".", just use dvp.  Don't need
2259 	 * to send it over the wire or look it up in the dnlc,
2260 	 * just need to check access.
2261 	 */
2262 	} else if (strcmp(nm, ".") == 0) {
2263 		error = nfs3_access(dvp, VEXEC, 0, cr, ct);
2264 		if (error) {
2265 			nfs_rw_exit(&drp->r_rwlock);
2266 			return (error);
2267 		}
2268 		VN_HOLD(dvp);
2269 		vp = dvp;
2270 	/*
2271 	 * We need to go over the wire, just to be sure whether the
2272 	 * file exists or not.  Using the DNLC can be dangerous in
2273 	 * this case when making a decision regarding existence.
2274 	 */
2275 	} else {
2276 		error = nfs3lookup_otw(dvp, nm, &vp, cr, 0);
2277 	}
2278 	if (!error) {
2279 		if (exclusive == EXCL)
2280 			error = EEXIST;
2281 		else if (vp->v_type == VDIR && (mode & VWRITE))
2282 			error = EISDIR;
2283 		else {
2284 			/*
2285 			 * If vnode is a device, create special vnode.
2286 			 */
2287 			if (IS_DEVVP(vp)) {
2288 				tempvp = vp;
2289 				vp = specvp(vp, vp->v_rdev, vp->v_type, cr);
2290 				VN_RELE(tempvp);
2291 			}
2292 			if (!(error = VOP_ACCESS(vp, mode, 0, cr, ct))) {
2293 				if ((vattr.va_mask & AT_SIZE) &&
2294 				    vp->v_type == VREG) {
2295 					rp = VTOR(vp);
2296 					/*
2297 					 * Check here for large file handled
2298 					 * by LF-unaware process (as
2299 					 * ufs_create() does)
2300 					 */
2301 					if (!(lfaware & FOFFMAX)) {
2302 						mutex_enter(&rp->r_statelock);
2303 						if (rp->r_size > MAXOFF32_T)
2304 							error = EOVERFLOW;
2305 						mutex_exit(&rp->r_statelock);
2306 					}
2307 					if (!error) {
2308 						vattr.va_mask = AT_SIZE;
2309 						error = nfs3setattr(vp,
2310 						    &vattr, 0, cr);
2311 
2312 						/*
2313 						 * Existing file was truncated;
2314 						 * emit a create event.
2315 						 */
2316 						vnevent_create(vp, ct);
2317 					}
2318 				}
2319 			}
2320 		}
2321 		nfs_rw_exit(&drp->r_rwlock);
2322 		if (error) {
2323 			VN_RELE(vp);
2324 		} else {
2325 			*vpp = vp;
2326 		}
2327 
2328 		return (error);
2329 	}
2330 
2331 	dnlc_remove(dvp, nm);
2332 
2333 	/*
2334 	 * Decide what the group-id of the created file should be.
2335 	 * Set it in attribute list as advisory...
2336 	 */
2337 	error = setdirgid(dvp, &vattr.va_gid, cr);
2338 	if (error) {
2339 		nfs_rw_exit(&drp->r_rwlock);
2340 		return (error);
2341 	}
2342 	vattr.va_mask |= AT_GID;
2343 
2344 	ASSERT(vattr.va_mask & AT_TYPE);
2345 	if (vattr.va_type == VREG) {
2346 		ASSERT(vattr.va_mask & AT_MODE);
2347 		if (MANDMODE(vattr.va_mode)) {
2348 			nfs_rw_exit(&drp->r_rwlock);
2349 			return (EACCES);
2350 		}
2351 		error = nfs3create(dvp, nm, &vattr, exclusive, mode, vpp, cr,
2352 		    lfaware);
2353 		/*
2354 		 * If this is not an exclusive create, then the CREATE
2355 		 * request will be made with the GUARDED mode set.  This
2356 		 * means that the server will return EEXIST if the file
2357 		 * exists.  The file could exist because of a retransmitted
2358 		 * request.  In this case, we recover by starting over and
2359 		 * checking to see whether the file exists.  This second
2360 		 * time through it should and a CREATE request will not be
2361 		 * sent.
2362 		 *
2363 		 * This handles the problem of a dangling CREATE request
2364 		 * which contains attributes which indicate that the file
2365 		 * should be truncated.  This retransmitted request could
2366 		 * possibly truncate valid data in the file if not caught
2367 		 * by the duplicate request mechanism on the server or if
2368 		 * not caught by other means.  The scenario is:
2369 		 *
2370 		 * Client transmits CREATE request with size = 0
2371 		 * Client times out, retransmits request.
2372 		 * Response to the first request arrives from the server
2373 		 *  and the client proceeds on.
2374 		 * Client writes data to the file.
2375 		 * The server now processes retransmitted CREATE request
2376 		 *  and truncates file.
2377 		 *
2378 		 * The use of the GUARDED CREATE request prevents this from
2379 		 * happening because the retransmitted CREATE would fail
2380 		 * with EEXIST and would not truncate the file.
2381 		 */
2382 		if (error == EEXIST && exclusive == NONEXCL) {
2383 #ifdef DEBUG
2384 			nfs3_create_misses++;
2385 #endif
2386 			goto top;
2387 		}
2388 		nfs_rw_exit(&drp->r_rwlock);
2389 		return (error);
2390 	}
2391 	error = nfs3mknod(dvp, nm, &vattr, exclusive, mode, vpp, cr);
2392 	nfs_rw_exit(&drp->r_rwlock);
2393 	return (error);
2394 }
2395 
2396 /* ARGSUSED */
2397 static int
nfs3create(vnode_t * dvp,char * nm,struct vattr * va,enum vcexcl exclusive,int mode,vnode_t ** vpp,cred_t * cr,int lfaware)2398 nfs3create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
2399 	int mode, vnode_t **vpp, cred_t *cr, int lfaware)
2400 {
2401 	int error;
2402 	CREATE3args args;
2403 	CREATE3res res;
2404 	int douprintf;
2405 	vnode_t *vp;
2406 	struct vattr vattr;
2407 	nfstime3 *verfp;
2408 	rnode_t *rp;
2409 	timestruc_t now;
2410 	hrtime_t t;
2411 
2412 	ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
2413 	setdiropargs3(&args.where, nm, dvp);
2414 	if (exclusive == EXCL) {
2415 		args.how.mode = EXCLUSIVE;
2416 		/*
2417 		 * Construct the create verifier.  This verifier needs
2418 		 * to be unique between different clients.  It also needs
2419 		 * to vary for each exclusive create request generated
2420 		 * from the client to the server.
2421 		 *
2422 		 * The first attempt is made to use the hostid and a
2423 		 * unique number on the client.  If the hostid has not
2424 		 * been set, the high resolution time that the exclusive
2425 		 * create request is being made is used.  This will work
2426 		 * unless two different clients, both with the hostid
2427 		 * not set, attempt an exclusive create request on the
2428 		 * same file, at exactly the same clock time.  The
2429 		 * chances of this happening seem small enough to be
2430 		 * reasonable.
2431 		 */
2432 		verfp = (nfstime3 *)&args.how.createhow3_u.verf;
2433 		verfp->seconds = zone_get_hostid(NULL);
2434 		if (verfp->seconds != 0)
2435 			verfp->nseconds = newnum();
2436 		else {
2437 			gethrestime(&now);
2438 			verfp->seconds = now.tv_sec;
2439 			verfp->nseconds = now.tv_nsec;
2440 		}
2441 		/*
2442 		 * Since the server will use this value for the mtime,
2443 		 * make sure that it can't overflow. Zero out the MSB.
2444 		 * The actual value does not matter here, only its uniqeness.
2445 		 */
2446 		verfp->seconds %= INT32_MAX;
2447 	} else {
2448 		/*
2449 		 * Issue the non-exclusive create in guarded mode.  This
2450 		 * may result in some false EEXIST responses for
2451 		 * retransmitted requests, but these will be handled at
2452 		 * a higher level.  By using GUARDED, duplicate requests
2453 		 * to do file truncation and possible access problems
2454 		 * can be avoided.
2455 		 */
2456 		args.how.mode = GUARDED;
2457 		error = vattr_to_sattr3(va,
2458 		    &args.how.createhow3_u.obj_attributes);
2459 		if (error) {
2460 			/* req time field(s) overflow - return immediately */
2461 			return (error);
2462 		}
2463 	}
2464 
2465 	douprintf = 1;
2466 
2467 	t = gethrtime();
2468 
2469 	error = rfs3call(VTOMI(dvp), NFSPROC3_CREATE,
2470 	    xdr_CREATE3args, (caddr_t)&args,
2471 	    xdr_CREATE3res, (caddr_t)&res, cr,
2472 	    &douprintf, &res.status, 0, NULL);
2473 
2474 	if (error) {
2475 		PURGE_ATTRCACHE(dvp);
2476 		return (error);
2477 	}
2478 
2479 	error = geterrno3(res.status);
2480 	if (!error) {
2481 		nfs3_cache_wcc_data(dvp, &res.resok.dir_wcc, t, cr);
2482 		if (HAVE_RDDIR_CACHE(VTOR(dvp)))
2483 			nfs_purge_rddir_cache(dvp);
2484 
2485 		/*
2486 		 * On exclusive create the times need to be explicitly
2487 		 * set to clear any potential verifier that may be stored
2488 		 * in one of these fields (see comment below).  This
2489 		 * is done here to cover the case where no post op attrs
2490 		 * were returned or a 'invalid' time was returned in
2491 		 * the attributes.
2492 		 */
2493 		if (exclusive == EXCL)
2494 			va->va_mask |= (AT_MTIME | AT_ATIME);
2495 
2496 		if (!res.resok.obj.handle_follows) {
2497 			error = nfs3lookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0);
2498 			if (error)
2499 				return (error);
2500 		} else {
2501 			if (res.resok.obj_attributes.attributes) {
2502 				vp = makenfs3node(&res.resok.obj.handle,
2503 				    &res.resok.obj_attributes.attr,
2504 				    dvp->v_vfsp, t, cr, NULL, NULL);
2505 			} else {
2506 				vp = makenfs3node(&res.resok.obj.handle, NULL,
2507 				    dvp->v_vfsp, t, cr, NULL, NULL);
2508 
2509 				/*
2510 				 * On an exclusive create, it is possible
2511 				 * that attributes were returned but those
2512 				 * postop attributes failed to decode
2513 				 * properly.  If this is the case,
2514 				 * then most likely the atime or mtime
2515 				 * were invalid for our client; this
2516 				 * is caused by the server storing the
2517 				 * create verifier in one of the time
2518 				 * fields(most likely mtime).
2519 				 * So... we are going to setattr just the
2520 				 * atime/mtime to clear things up.
2521 				 */
2522 				if (exclusive == EXCL) {
2523 					if (error =
2524 					    nfs3excl_create_settimes(vp,
2525 					    va, cr)) {
2526 						/*
2527 						 * Setting the times failed.
2528 						 * Remove the file and return
2529 						 * the error.
2530 						 */
2531 						VN_RELE(vp);
2532 						(void) nfs3_remove(dvp,
2533 						    nm, cr, NULL, 0);
2534 						return (error);
2535 					}
2536 				}
2537 
2538 				/*
2539 				 * This handles the non-exclusive case
2540 				 * and the exclusive case where no post op
2541 				 * attrs were returned.
2542 				 */
2543 				if (vp->v_type == VNON) {
2544 					vattr.va_mask = AT_TYPE;
2545 					error = nfs3getattr(vp, &vattr, cr);
2546 					if (error) {
2547 						VN_RELE(vp);
2548 						return (error);
2549 					}
2550 					vp->v_type = vattr.va_type;
2551 				}
2552 			}
2553 			dnlc_update(dvp, nm, vp);
2554 		}
2555 
2556 		rp = VTOR(vp);
2557 
2558 		/*
2559 		 * Check here for large file handled by
2560 		 * LF-unaware process (as ufs_create() does)
2561 		 */
2562 		if ((va->va_mask & AT_SIZE) && vp->v_type == VREG &&
2563 		    !(lfaware & FOFFMAX)) {
2564 			mutex_enter(&rp->r_statelock);
2565 			if (rp->r_size > MAXOFF32_T) {
2566 				mutex_exit(&rp->r_statelock);
2567 				VN_RELE(vp);
2568 				return (EOVERFLOW);
2569 			}
2570 			mutex_exit(&rp->r_statelock);
2571 		}
2572 
2573 		if (exclusive == EXCL &&
2574 		    (va->va_mask & ~(AT_GID | AT_SIZE))) {
2575 			/*
2576 			 * If doing an exclusive create, then generate
2577 			 * a SETATTR to set the initial attributes.
2578 			 * Try to set the mtime and the atime to the
2579 			 * server's current time.  It is somewhat
2580 			 * expected that these fields will be used to
2581 			 * store the exclusive create cookie.  If not,
2582 			 * server implementors will need to know that
2583 			 * a SETATTR will follow an exclusive create
2584 			 * and the cookie should be destroyed if
2585 			 * appropriate. This work may have been done
2586 			 * earlier in this function if post op attrs
2587 			 * were not available.
2588 			 *
2589 			 * The AT_GID and AT_SIZE bits are turned off
2590 			 * so that the SETATTR request will not attempt
2591 			 * to process these.  The gid will be set
2592 			 * separately if appropriate.  The size is turned
2593 			 * off because it is assumed that a new file will
2594 			 * be created empty and if the file wasn't empty,
2595 			 * then the exclusive create will have failed
2596 			 * because the file must have existed already.
2597 			 * Therefore, no truncate operation is needed.
2598 			 */
2599 			va->va_mask &= ~(AT_GID | AT_SIZE);
2600 			error = nfs3setattr(vp, va, 0, cr);
2601 			if (error) {
2602 				/*
2603 				 * Couldn't correct the attributes of
2604 				 * the newly created file and the
2605 				 * attributes are wrong.  Remove the
2606 				 * file and return an error to the
2607 				 * application.
2608 				 */
2609 				VN_RELE(vp);
2610 				(void) nfs3_remove(dvp, nm, cr, NULL, 0);
2611 				return (error);
2612 			}
2613 		}
2614 
2615 		if (va->va_gid != rp->r_attr.va_gid) {
2616 			/*
2617 			 * If the gid on the file isn't right, then
2618 			 * generate a SETATTR to attempt to change
2619 			 * it.  This may or may not work, depending
2620 			 * upon the server's semantics for allowing
2621 			 * file ownership changes.
2622 			 */
2623 			va->va_mask = AT_GID;
2624 			(void) nfs3setattr(vp, va, 0, cr);
2625 		}
2626 
2627 		/*
2628 		 * If vnode is a device create special vnode
2629 		 */
2630 		if (IS_DEVVP(vp)) {
2631 			*vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
2632 			VN_RELE(vp);
2633 		} else
2634 			*vpp = vp;
2635 	} else {
2636 		nfs3_cache_wcc_data(dvp, &res.resfail.dir_wcc, t, cr);
2637 		PURGE_STALE_FH(error, dvp, cr);
2638 	}
2639 
2640 	return (error);
2641 }
2642 
2643 /*
2644  * Special setattr function to take care of rest of atime/mtime
2645  * after successful exclusive create.  This function exists to avoid
2646  * handling attributes from the server; exclusive the atime/mtime fields
2647  * may be 'invalid' in client's view and therefore can not be trusted.
2648  */
2649 static int
nfs3excl_create_settimes(vnode_t * vp,struct vattr * vap,cred_t * cr)2650 nfs3excl_create_settimes(vnode_t *vp, struct vattr *vap, cred_t *cr)
2651 {
2652 	int error;
2653 	uint_t mask;
2654 	SETATTR3args args;
2655 	SETATTR3res res;
2656 	int douprintf;
2657 	rnode_t *rp;
2658 	hrtime_t t;
2659 
2660 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
2661 	/* save the caller's mask so that it can be reset later */
2662 	mask = vap->va_mask;
2663 
2664 	rp = VTOR(vp);
2665 
2666 	args.object = *RTOFH3(rp);
2667 	args.guard.check = FALSE;
2668 
2669 	/* Use the mask to initialize the arguments */
2670 	vap->va_mask = 0;
2671 	error = vattr_to_sattr3(vap, &args.new_attributes);
2672 
2673 	/* We want to set just atime/mtime on this request */
2674 	args.new_attributes.atime.set_it = SET_TO_SERVER_TIME;
2675 	args.new_attributes.mtime.set_it = SET_TO_SERVER_TIME;
2676 
2677 	douprintf = 1;
2678 
2679 	t = gethrtime();
2680 
2681 	error = rfs3call(VTOMI(vp), NFSPROC3_SETATTR,
2682 	    xdr_SETATTR3args, (caddr_t)&args,
2683 	    xdr_SETATTR3res, (caddr_t)&res, cr,
2684 	    &douprintf, &res.status, 0, NULL);
2685 
2686 	if (error) {
2687 		vap->va_mask = mask;
2688 		return (error);
2689 	}
2690 
2691 	error = geterrno3(res.status);
2692 	if (!error) {
2693 		/*
2694 		 * It is important to pick up the attributes.
2695 		 * Since this is the exclusive create path, the
2696 		 * attributes on the initial create were ignored
2697 		 * and we need these to have the correct info.
2698 		 */
2699 		nfs3_cache_wcc_data(vp, &res.resok.obj_wcc, t, cr);
2700 		/*
2701 		 * No need to do the atime/mtime work again so clear
2702 		 * the bits.
2703 		 */
2704 		mask &= ~(AT_ATIME | AT_MTIME);
2705 	} else {
2706 		nfs3_cache_wcc_data(vp, &res.resfail.obj_wcc, t, cr);
2707 	}
2708 
2709 	vap->va_mask = mask;
2710 
2711 	return (error);
2712 }
2713 
2714 /* ARGSUSED */
2715 static int
nfs3mknod(vnode_t * dvp,char * nm,struct vattr * va,enum vcexcl exclusive,int mode,vnode_t ** vpp,cred_t * cr)2716 nfs3mknod(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
2717 	int mode, vnode_t **vpp, cred_t *cr)
2718 {
2719 	int error;
2720 	MKNOD3args args;
2721 	MKNOD3res res;
2722 	int douprintf;
2723 	vnode_t *vp;
2724 	struct vattr vattr;
2725 	hrtime_t t;
2726 
2727 	ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
2728 	switch (va->va_type) {
2729 	case VCHR:
2730 	case VBLK:
2731 		setdiropargs3(&args.where, nm, dvp);
2732 		args.what.type = (va->va_type == VCHR) ? NF3CHR : NF3BLK;
2733 		error = vattr_to_sattr3(va,
2734 		    &args.what.mknoddata3_u.device.dev_attributes);
2735 		if (error) {
2736 			/* req time field(s) overflow - return immediately */
2737 			return (error);
2738 		}
2739 		args.what.mknoddata3_u.device.spec.specdata1 =
2740 		    getmajor(va->va_rdev);
2741 		args.what.mknoddata3_u.device.spec.specdata2 =
2742 		    getminor(va->va_rdev);
2743 		break;
2744 
2745 	case VFIFO:
2746 	case VSOCK:
2747 		setdiropargs3(&args.where, nm, dvp);
2748 		args.what.type = (va->va_type == VFIFO) ? NF3FIFO : NF3SOCK;
2749 		error = vattr_to_sattr3(va,
2750 		    &args.what.mknoddata3_u.pipe_attributes);
2751 		if (error) {
2752 			/* req time field(s) overflow - return immediately */
2753 			return (error);
2754 		}
2755 		break;
2756 
2757 	default:
2758 		return (EINVAL);
2759 	}
2760 
2761 	douprintf = 1;
2762 
2763 	t = gethrtime();
2764 
2765 	error = rfs3call(VTOMI(dvp), NFSPROC3_MKNOD,
2766 	    xdr_MKNOD3args, (caddr_t)&args,
2767 	    xdr_MKNOD3res, (caddr_t)&res, cr,
2768 	    &douprintf, &res.status, 0, NULL);
2769 
2770 	if (error) {
2771 		PURGE_ATTRCACHE(dvp);
2772 		return (error);
2773 	}
2774 
2775 	error = geterrno3(res.status);
2776 	if (!error) {
2777 		nfs3_cache_wcc_data(dvp, &res.resok.dir_wcc, t, cr);
2778 		if (HAVE_RDDIR_CACHE(VTOR(dvp)))
2779 			nfs_purge_rddir_cache(dvp);
2780 
2781 		if (!res.resok.obj.handle_follows) {
2782 			error = nfs3lookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0);
2783 			if (error)
2784 				return (error);
2785 		} else {
2786 			if (res.resok.obj_attributes.attributes) {
2787 				vp = makenfs3node(&res.resok.obj.handle,
2788 				    &res.resok.obj_attributes.attr,
2789 				    dvp->v_vfsp, t, cr, NULL, NULL);
2790 			} else {
2791 				vp = makenfs3node(&res.resok.obj.handle, NULL,
2792 				    dvp->v_vfsp, t, cr, NULL, NULL);
2793 				if (vp->v_type == VNON) {
2794 					vattr.va_mask = AT_TYPE;
2795 					error = nfs3getattr(vp, &vattr, cr);
2796 					if (error) {
2797 						VN_RELE(vp);
2798 						return (error);
2799 					}
2800 					vp->v_type = vattr.va_type;
2801 				}
2802 
2803 			}
2804 			dnlc_update(dvp, nm, vp);
2805 		}
2806 
2807 		if (va->va_gid != VTOR(vp)->r_attr.va_gid) {
2808 			va->va_mask = AT_GID;
2809 			(void) nfs3setattr(vp, va, 0, cr);
2810 		}
2811 
2812 		/*
2813 		 * If vnode is a device create special vnode
2814 		 */
2815 		if (IS_DEVVP(vp)) {
2816 			*vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
2817 			VN_RELE(vp);
2818 		} else
2819 			*vpp = vp;
2820 	} else {
2821 		nfs3_cache_wcc_data(dvp, &res.resfail.dir_wcc, t, cr);
2822 		PURGE_STALE_FH(error, dvp, cr);
2823 	}
2824 	return (error);
2825 }
2826 
2827 /*
2828  * Weirdness: if the vnode to be removed is open
2829  * we rename it instead of removing it and nfs_inactive
2830  * will remove the new name.
2831  */
2832 /* ARGSUSED */
2833 static int
nfs3_remove(vnode_t * dvp,char * nm,cred_t * cr,caller_context_t * ct,int flags)2834 nfs3_remove(vnode_t *dvp, char *nm, cred_t *cr, caller_context_t *ct, int flags)
2835 {
2836 	int error;
2837 	REMOVE3args args;
2838 	REMOVE3res res;
2839 	vnode_t *vp;
2840 	char *tmpname;
2841 	int douprintf;
2842 	rnode_t *rp;
2843 	rnode_t *drp;
2844 	hrtime_t t;
2845 
2846 	if (nfs_zone() != VTOMI(dvp)->mi_zone)
2847 		return (EPERM);
2848 	drp = VTOR(dvp);
2849 	if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
2850 		return (EINTR);
2851 
2852 	error = nfs3lookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0);
2853 	if (error) {
2854 		nfs_rw_exit(&drp->r_rwlock);
2855 		return (error);
2856 	}
2857 
2858 	if (vp->v_type == VDIR && secpolicy_fs_linkdir(cr, dvp->v_vfsp)) {
2859 		VN_RELE(vp);
2860 		nfs_rw_exit(&drp->r_rwlock);
2861 		return (EPERM);
2862 	}
2863 
2864 	/*
2865 	 * First just remove the entry from the name cache, as it
2866 	 * is most likely the only entry for this vp.
2867 	 */
2868 	dnlc_remove(dvp, nm);
2869 
2870 	/*
2871 	 * If the file has a v_count > 1 then there may be more than one
2872 	 * entry in the name cache due multiple links or an open file,
2873 	 * but we don't have the real reference count so flush all
2874 	 * possible entries.
2875 	 */
2876 	if (vp->v_count > 1)
2877 		dnlc_purge_vp(vp);
2878 
2879 	/*
2880 	 * Now we have the real reference count on the vnode
2881 	 */
2882 	rp = VTOR(vp);
2883 	mutex_enter(&rp->r_statelock);
2884 	if (vp->v_count > 1 &&
2885 	    (rp->r_unldvp == NULL || strcmp(nm, rp->r_unlname) == 0)) {
2886 		mutex_exit(&rp->r_statelock);
2887 		tmpname = newname();
2888 		error = nfs3rename(dvp, nm, dvp, tmpname, cr, ct);
2889 		if (error)
2890 			kmem_free(tmpname, MAXNAMELEN);
2891 		else {
2892 			mutex_enter(&rp->r_statelock);
2893 			if (rp->r_unldvp == NULL) {
2894 				VN_HOLD(dvp);
2895 				rp->r_unldvp = dvp;
2896 				if (rp->r_unlcred != NULL)
2897 					crfree(rp->r_unlcred);
2898 				crhold(cr);
2899 				rp->r_unlcred = cr;
2900 				rp->r_unlname = tmpname;
2901 			} else {
2902 				kmem_free(rp->r_unlname, MAXNAMELEN);
2903 				rp->r_unlname = tmpname;
2904 			}
2905 			mutex_exit(&rp->r_statelock);
2906 		}
2907 	} else {
2908 		mutex_exit(&rp->r_statelock);
2909 		/*
2910 		 * We need to flush any dirty pages which happen to
2911 		 * be hanging around before removing the file.  This
2912 		 * shouldn't happen very often and mostly on file
2913 		 * systems mounted "nocto".
2914 		 */
2915 		if (vn_has_cached_data(vp) &&
2916 		    ((rp->r_flags & RDIRTY) || rp->r_count > 0)) {
2917 			error = nfs3_putpage(vp, (offset_t)0, 0, 0, cr, ct);
2918 			if (error && (error == ENOSPC || error == EDQUOT)) {
2919 				mutex_enter(&rp->r_statelock);
2920 				if (!rp->r_error)
2921 					rp->r_error = error;
2922 				mutex_exit(&rp->r_statelock);
2923 			}
2924 		}
2925 
2926 		setdiropargs3(&args.object, nm, dvp);
2927 
2928 		douprintf = 1;
2929 
2930 		t = gethrtime();
2931 
2932 		error = rfs3call(VTOMI(dvp), NFSPROC3_REMOVE,
2933 		    xdr_diropargs3, (caddr_t)&args,
2934 		    xdr_REMOVE3res, (caddr_t)&res, cr,
2935 		    &douprintf, &res.status, 0, NULL);
2936 
2937 		/*
2938 		 * The xattr dir may be gone after last attr is removed,
2939 		 * so flush it from dnlc.
2940 		 */
2941 		if (dvp->v_flag & V_XATTRDIR)
2942 			dnlc_purge_vp(dvp);
2943 
2944 		PURGE_ATTRCACHE(vp);
2945 
2946 		if (error) {
2947 			PURGE_ATTRCACHE(dvp);
2948 		} else {
2949 			error = geterrno3(res.status);
2950 			if (!error) {
2951 				nfs3_cache_wcc_data(dvp, &res.resok.dir_wcc, t,
2952 				    cr);
2953 				if (HAVE_RDDIR_CACHE(drp))
2954 					nfs_purge_rddir_cache(dvp);
2955 			} else {
2956 				nfs3_cache_wcc_data(dvp, &res.resfail.dir_wcc,
2957 				    t, cr);
2958 				PURGE_STALE_FH(error, dvp, cr);
2959 			}
2960 		}
2961 	}
2962 
2963 	if (error == 0) {
2964 		vnevent_remove(vp, dvp, nm, ct);
2965 	}
2966 	VN_RELE(vp);
2967 
2968 	nfs_rw_exit(&drp->r_rwlock);
2969 
2970 	return (error);
2971 }
2972 
2973 /* ARGSUSED */
2974 static int
nfs3_link(vnode_t * tdvp,vnode_t * svp,char * tnm,cred_t * cr,caller_context_t * ct,int flags)2975 nfs3_link(vnode_t *tdvp, vnode_t *svp, char *tnm, cred_t *cr,
2976 	caller_context_t *ct, int flags)
2977 {
2978 	int error;
2979 	LINK3args args;
2980 	LINK3res res;
2981 	vnode_t *realvp;
2982 	int douprintf;
2983 	mntinfo_t *mi;
2984 	rnode_t *tdrp;
2985 	hrtime_t t;
2986 
2987 	if (nfs_zone() != VTOMI(tdvp)->mi_zone)
2988 		return (EPERM);
2989 	if (VOP_REALVP(svp, &realvp, ct) == 0)
2990 		svp = realvp;
2991 
2992 	mi = VTOMI(svp);
2993 
2994 	if (!(mi->mi_flags & MI_LINK))
2995 		return (EOPNOTSUPP);
2996 
2997 	args.file = *VTOFH3(svp);
2998 	setdiropargs3(&args.link, tnm, tdvp);
2999 
3000 	tdrp = VTOR(tdvp);
3001 	if (nfs_rw_enter_sig(&tdrp->r_rwlock, RW_WRITER, INTR(tdvp)))
3002 		return (EINTR);
3003 
3004 	dnlc_remove(tdvp, tnm);
3005 
3006 	douprintf = 1;
3007 
3008 	t = gethrtime();
3009 
3010 	error = rfs3call(mi, NFSPROC3_LINK,
3011 	    xdr_LINK3args, (caddr_t)&args,
3012 	    xdr_LINK3res, (caddr_t)&res, cr,
3013 	    &douprintf, &res.status, 0, NULL);
3014 
3015 	if (error) {
3016 		PURGE_ATTRCACHE(tdvp);
3017 		PURGE_ATTRCACHE(svp);
3018 		nfs_rw_exit(&tdrp->r_rwlock);
3019 		return (error);
3020 	}
3021 
3022 	error = geterrno3(res.status);
3023 
3024 	if (!error) {
3025 		nfs3_cache_post_op_attr(svp, &res.resok.file_attributes, t, cr);
3026 		nfs3_cache_wcc_data(tdvp, &res.resok.linkdir_wcc, t, cr);
3027 		if (HAVE_RDDIR_CACHE(tdrp))
3028 			nfs_purge_rddir_cache(tdvp);
3029 		dnlc_update(tdvp, tnm, svp);
3030 	} else {
3031 		nfs3_cache_post_op_attr(svp, &res.resfail.file_attributes, t,
3032 		    cr);
3033 		nfs3_cache_wcc_data(tdvp, &res.resfail.linkdir_wcc, t, cr);
3034 		if (error == EOPNOTSUPP) {
3035 			mutex_enter(&mi->mi_lock);
3036 			mi->mi_flags &= ~MI_LINK;
3037 			mutex_exit(&mi->mi_lock);
3038 		}
3039 	}
3040 
3041 	nfs_rw_exit(&tdrp->r_rwlock);
3042 
3043 	if (!error) {
3044 		/*
3045 		 * Notify the source file of this link operation.
3046 		 */
3047 		vnevent_link(svp, ct);
3048 	}
3049 	return (error);
3050 }
3051 
3052 /* ARGSUSED */
3053 static int
nfs3_rename(vnode_t * odvp,char * onm,vnode_t * ndvp,char * nnm,cred_t * cr,caller_context_t * ct,int flags)3054 nfs3_rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr,
3055 	caller_context_t *ct, int flags)
3056 {
3057 	vnode_t *realvp;
3058 
3059 	if (nfs_zone() != VTOMI(odvp)->mi_zone)
3060 		return (EPERM);
3061 	if (VOP_REALVP(ndvp, &realvp, ct) == 0)
3062 		ndvp = realvp;
3063 
3064 	return (nfs3rename(odvp, onm, ndvp, nnm, cr, ct));
3065 }
3066 
3067 /*
3068  * nfs3rename does the real work of renaming in NFS Version 3.
3069  */
3070 static int
nfs3rename(vnode_t * odvp,char * onm,vnode_t * ndvp,char * nnm,cred_t * cr,caller_context_t * ct)3071 nfs3rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr,
3072     caller_context_t *ct)
3073 {
3074 	int error;
3075 	RENAME3args args;
3076 	RENAME3res res;
3077 	int douprintf;
3078 	vnode_t *nvp = NULL;
3079 	vnode_t *ovp = NULL;
3080 	char *tmpname;
3081 	rnode_t *rp;
3082 	rnode_t *odrp;
3083 	rnode_t *ndrp;
3084 	hrtime_t t;
3085 
3086 	ASSERT(nfs_zone() == VTOMI(odvp)->mi_zone);
3087 
3088 	if (strcmp(onm, ".") == 0 || strcmp(onm, "..") == 0 ||
3089 	    strcmp(nnm, ".") == 0 || strcmp(nnm, "..") == 0)
3090 		return (EINVAL);
3091 
3092 	odrp = VTOR(odvp);
3093 	ndrp = VTOR(ndvp);
3094 	if ((intptr_t)odrp < (intptr_t)ndrp) {
3095 		if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR(odvp)))
3096 			return (EINTR);
3097 		if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR(ndvp))) {
3098 			nfs_rw_exit(&odrp->r_rwlock);
3099