xref: /freebsd-13.1/sys/fs/nfsclient/nfs_clvnops.c (revision 3c3b641a)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Rick Macklem at The University of Guelph.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	from nfs_vnops.c	8.16 (Berkeley) 5/27/95
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 /*
41  * vnode op calls for Sun NFS version 2, 3 and 4
42  */
43 
44 #include "opt_inet.h"
45 
46 #include <sys/param.h>
47 #include <sys/kernel.h>
48 #include <sys/systm.h>
49 #include <sys/resourcevar.h>
50 #include <sys/proc.h>
51 #include <sys/mount.h>
52 #include <sys/bio.h>
53 #include <sys/buf.h>
54 #include <sys/extattr.h>
55 #include <sys/filio.h>
56 #include <sys/jail.h>
57 #include <sys/malloc.h>
58 #include <sys/mbuf.h>
59 #include <sys/namei.h>
60 #include <sys/socket.h>
61 #include <sys/vnode.h>
62 #include <sys/dirent.h>
63 #include <sys/fcntl.h>
64 #include <sys/lockf.h>
65 #include <sys/stat.h>
66 #include <sys/sysctl.h>
67 #include <sys/signalvar.h>
68 
69 #include <vm/vm.h>
70 #include <vm/vm_extern.h>
71 #include <vm/vm_object.h>
72 
73 #include <fs/nfs/nfsport.h>
74 #include <fs/nfsclient/nfsnode.h>
75 #include <fs/nfsclient/nfsmount.h>
76 #include <fs/nfsclient/nfs.h>
77 #include <fs/nfsclient/nfs_kdtrace.h>
78 
79 #include <net/if.h>
80 #include <netinet/in.h>
81 #include <netinet/in_var.h>
82 
83 #include <nfs/nfs_lock.h>
84 
85 #ifdef KDTRACE_HOOKS
86 #include <sys/dtrace_bsd.h>
87 
88 dtrace_nfsclient_accesscache_flush_probe_func_t
89 		dtrace_nfscl_accesscache_flush_done_probe;
90 uint32_t	nfscl_accesscache_flush_done_id;
91 
92 dtrace_nfsclient_accesscache_get_probe_func_t
93 		dtrace_nfscl_accesscache_get_hit_probe,
94 		dtrace_nfscl_accesscache_get_miss_probe;
95 uint32_t	nfscl_accesscache_get_hit_id;
96 uint32_t	nfscl_accesscache_get_miss_id;
97 
98 dtrace_nfsclient_accesscache_load_probe_func_t
99 		dtrace_nfscl_accesscache_load_done_probe;
100 uint32_t	nfscl_accesscache_load_done_id;
101 #endif /* !KDTRACE_HOOKS */
102 
103 /* Defs */
104 #define	TRUE	1
105 #define	FALSE	0
106 
107 extern struct nfsstatsv1 nfsstatsv1;
108 extern int nfsrv_useacl;
109 extern int nfscl_debuglevel;
110 MALLOC_DECLARE(M_NEWNFSREQ);
111 
112 static vop_read_t	nfsfifo_read;
113 static vop_write_t	nfsfifo_write;
114 static vop_close_t	nfsfifo_close;
115 static int	nfs_setattrrpc(struct vnode *, struct vattr *, struct ucred *,
116 		    struct thread *);
117 static vop_lookup_t	nfs_lookup;
118 static vop_create_t	nfs_create;
119 static vop_mknod_t	nfs_mknod;
120 static vop_open_t	nfs_open;
121 static vop_pathconf_t	nfs_pathconf;
122 static vop_close_t	nfs_close;
123 static vop_access_t	nfs_access;
124 static vop_getattr_t	nfs_getattr;
125 static vop_setattr_t	nfs_setattr;
126 static vop_read_t	nfs_read;
127 static vop_fsync_t	nfs_fsync;
128 static vop_remove_t	nfs_remove;
129 static vop_link_t	nfs_link;
130 static vop_rename_t	nfs_rename;
131 static vop_mkdir_t	nfs_mkdir;
132 static vop_rmdir_t	nfs_rmdir;
133 static vop_symlink_t	nfs_symlink;
134 static vop_readdir_t	nfs_readdir;
135 static vop_strategy_t	nfs_strategy;
136 static	int	nfs_lookitup(struct vnode *, char *, int,
137 		    struct ucred *, struct thread *, struct nfsnode **);
138 static	int	nfs_sillyrename(struct vnode *, struct vnode *,
139 		    struct componentname *);
140 static vop_access_t	nfsspec_access;
141 static vop_readlink_t	nfs_readlink;
142 static vop_print_t	nfs_print;
143 static vop_advlock_t	nfs_advlock;
144 static vop_advlockasync_t nfs_advlockasync;
145 static vop_getacl_t nfs_getacl;
146 static vop_setacl_t nfs_setacl;
147 static vop_advise_t nfs_advise;
148 static vop_allocate_t nfs_allocate;
149 static vop_copy_file_range_t nfs_copy_file_range;
150 static vop_ioctl_t nfs_ioctl;
151 static vop_getextattr_t nfs_getextattr;
152 static vop_setextattr_t nfs_setextattr;
153 static vop_listextattr_t nfs_listextattr;
154 static vop_deleteextattr_t nfs_deleteextattr;
155 static vop_lock1_t	nfs_lock;
156 
157 /*
158  * Global vfs data structures for nfs
159  */
160 
161 static struct vop_vector newnfs_vnodeops_nosig = {
162 	.vop_default =		&default_vnodeops,
163 	.vop_access =		nfs_access,
164 	.vop_advlock =		nfs_advlock,
165 	.vop_advlockasync =	nfs_advlockasync,
166 	.vop_close =		nfs_close,
167 	.vop_create =		nfs_create,
168 	.vop_fsync =		nfs_fsync,
169 	.vop_getattr =		nfs_getattr,
170 	.vop_getpages =		ncl_getpages,
171 	.vop_putpages =		ncl_putpages,
172 	.vop_inactive =		ncl_inactive,
173 	.vop_link =		nfs_link,
174 	.vop_lock1 =		nfs_lock,
175 	.vop_lookup =		nfs_lookup,
176 	.vop_mkdir =		nfs_mkdir,
177 	.vop_mknod =		nfs_mknod,
178 	.vop_open =		nfs_open,
179 	.vop_pathconf =		nfs_pathconf,
180 	.vop_print =		nfs_print,
181 	.vop_read =		nfs_read,
182 	.vop_readdir =		nfs_readdir,
183 	.vop_readlink =		nfs_readlink,
184 	.vop_reclaim =		ncl_reclaim,
185 	.vop_remove =		nfs_remove,
186 	.vop_rename =		nfs_rename,
187 	.vop_rmdir =		nfs_rmdir,
188 	.vop_setattr =		nfs_setattr,
189 	.vop_strategy =		nfs_strategy,
190 	.vop_symlink =		nfs_symlink,
191 	.vop_write =		ncl_write,
192 	.vop_getacl =		nfs_getacl,
193 	.vop_setacl =		nfs_setacl,
194 	.vop_advise =		nfs_advise,
195 	.vop_allocate =		nfs_allocate,
196 	.vop_copy_file_range =	nfs_copy_file_range,
197 	.vop_ioctl =		nfs_ioctl,
198 	.vop_getextattr =	nfs_getextattr,
199 	.vop_setextattr =	nfs_setextattr,
200 	.vop_listextattr =	nfs_listextattr,
201 	.vop_deleteextattr =	nfs_deleteextattr,
202 };
203 VFS_VOP_VECTOR_REGISTER(newnfs_vnodeops_nosig);
204 
205 static int
nfs_vnodeops_bypass(struct vop_generic_args * a)206 nfs_vnodeops_bypass(struct vop_generic_args *a)
207 {
208 
209 	return (vop_sigdefer(&newnfs_vnodeops_nosig, a));
210 }
211 
212 struct vop_vector newnfs_vnodeops = {
213 	.vop_default =		&default_vnodeops,
214 	.vop_bypass =		nfs_vnodeops_bypass,
215 };
216 VFS_VOP_VECTOR_REGISTER(newnfs_vnodeops);
217 
218 static struct vop_vector newnfs_fifoops_nosig = {
219 	.vop_default =		&fifo_specops,
220 	.vop_access =		nfsspec_access,
221 	.vop_close =		nfsfifo_close,
222 	.vop_fsync =		nfs_fsync,
223 	.vop_getattr =		nfs_getattr,
224 	.vop_inactive =		ncl_inactive,
225 	.vop_pathconf =		nfs_pathconf,
226 	.vop_print =		nfs_print,
227 	.vop_read =		nfsfifo_read,
228 	.vop_reclaim =		ncl_reclaim,
229 	.vop_setattr =		nfs_setattr,
230 	.vop_write =		nfsfifo_write,
231 };
232 VFS_VOP_VECTOR_REGISTER(newnfs_fifoops_nosig);
233 
234 static int
nfs_fifoops_bypass(struct vop_generic_args * a)235 nfs_fifoops_bypass(struct vop_generic_args *a)
236 {
237 
238 	return (vop_sigdefer(&newnfs_fifoops_nosig, a));
239 }
240 
241 struct vop_vector newnfs_fifoops = {
242 	.vop_default =		&default_vnodeops,
243 	.vop_bypass =		nfs_fifoops_bypass,
244 };
245 VFS_VOP_VECTOR_REGISTER(newnfs_fifoops);
246 
247 static int nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp,
248     struct componentname *cnp, struct vattr *vap);
249 static int nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
250     int namelen, struct ucred *cred, struct thread *td);
251 static int nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp,
252     char *fnameptr, int fnamelen, struct vnode *tdvp, struct vnode *tvp,
253     char *tnameptr, int tnamelen, struct ucred *cred, struct thread *td);
254 static int nfs_renameit(struct vnode *sdvp, struct vnode *svp,
255     struct componentname *scnp, struct sillyrename *sp);
256 
257 /*
258  * Global variables
259  */
260 SYSCTL_DECL(_vfs_nfs);
261 
262 static int	nfsaccess_cache_timeout = NFS_MAXATTRTIMO;
263 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
264 	   &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
265 
266 static int	nfs_prime_access_cache = 0;
267 SYSCTL_INT(_vfs_nfs, OID_AUTO, prime_access_cache, CTLFLAG_RW,
268 	   &nfs_prime_access_cache, 0,
269 	   "Prime NFS ACCESS cache when fetching attributes");
270 
271 static int	newnfs_commit_on_close = 0;
272 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_on_close, CTLFLAG_RW,
273     &newnfs_commit_on_close, 0, "write+commit on close, else only write");
274 
275 static int	nfs_clean_pages_on_close = 1;
276 SYSCTL_INT(_vfs_nfs, OID_AUTO, clean_pages_on_close, CTLFLAG_RW,
277 	   &nfs_clean_pages_on_close, 0, "NFS clean dirty pages on close");
278 
279 int newnfs_directio_enable = 0;
280 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_directio_enable, CTLFLAG_RW,
281 	   &newnfs_directio_enable, 0, "Enable NFS directio");
282 
283 int nfs_keep_dirty_on_error;
284 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_keep_dirty_on_error, CTLFLAG_RW,
285     &nfs_keep_dirty_on_error, 0, "Retry pageout if error returned");
286 
287 /*
288  * This sysctl allows other processes to mmap a file that has been opened
289  * O_DIRECT by a process.  In general, having processes mmap the file while
290  * Direct IO is in progress can lead to Data Inconsistencies.  But, we allow
291  * this by default to prevent DoS attacks - to prevent a malicious user from
292  * opening up files O_DIRECT preventing other users from mmap'ing these
293  * files.  "Protected" environments where stricter consistency guarantees are
294  * required can disable this knob.  The process that opened the file O_DIRECT
295  * cannot mmap() the file, because mmap'ed IO on an O_DIRECT open() is not
296  * meaningful.
297  */
298 int newnfs_directio_allow_mmap = 1;
299 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs_directio_allow_mmap, CTLFLAG_RW,
300 	   &newnfs_directio_allow_mmap, 0, "Enable mmaped IO on file with O_DIRECT opens");
301 
302 static uint64_t	nfs_maxalloclen = 64 * 1024 * 1024;
303 SYSCTL_U64(_vfs_nfs, OID_AUTO, maxalloclen, CTLFLAG_RW,
304 	   &nfs_maxalloclen, 0, "NFS max allocate/deallocate length");
305 
306 #define	NFSACCESS_ALL (NFSACCESS_READ | NFSACCESS_MODIFY		\
307 			 | NFSACCESS_EXTEND | NFSACCESS_EXECUTE	\
308 			 | NFSACCESS_DELETE | NFSACCESS_LOOKUP)
309 
310 /*
311  * SMP Locking Note :
312  * The list of locks after the description of the lock is the ordering
313  * of other locks acquired with the lock held.
314  * np->n_mtx : Protects the fields in the nfsnode.
315        VM Object Lock
316        VI_MTX (acquired indirectly)
317  * nmp->nm_mtx : Protects the fields in the nfsmount.
318        rep->r_mtx
319  * ncl_iod_mutex : Global lock, protects shared nfsiod state.
320  * nfs_reqq_mtx : Global lock, protects the nfs_reqq list.
321        nmp->nm_mtx
322        rep->r_mtx
323  * rep->r_mtx : Protects the fields in an nfsreq.
324  */
325 
326 static int
nfs_lock(struct vop_lock1_args * ap)327 nfs_lock(struct vop_lock1_args *ap)
328 {
329 	struct vnode *vp;
330 	struct nfsnode *np;
331 	u_quad_t nsize;
332 	int error, lktype;
333 	bool onfault;
334 
335 	vp = ap->a_vp;
336 	lktype = ap->a_flags & LK_TYPE_MASK;
337 	error = VOP_LOCK1_APV(&default_vnodeops, ap);
338 	if (error != 0 || vp->v_op != &newnfs_vnodeops)
339 		return (error);
340 	np = VTONFS(vp);
341 	if (np == NULL)
342 		return (0);
343 	NFSLOCKNODE(np);
344 	if ((np->n_flag & NVNSETSZSKIP) == 0 || (lktype != LK_SHARED &&
345 	    lktype != LK_EXCLUSIVE && lktype != LK_UPGRADE &&
346 	    lktype != LK_TRYUPGRADE)) {
347 		NFSUNLOCKNODE(np);
348 		return (0);
349 	}
350 	onfault = (ap->a_flags & LK_EATTR_MASK) == LK_NOWAIT &&
351 	    (ap->a_flags & LK_INIT_MASK) == LK_CANRECURSE &&
352 	    (lktype == LK_SHARED || lktype == LK_EXCLUSIVE);
353 	if (onfault && vp->v_vnlock->lk_recurse == 0) {
354 		/*
355 		 * Force retry in vm_fault(), to make the lock request
356 		 * sleepable, which allows us to piggy-back the
357 		 * sleepable call to vnode_pager_setsize().
358 		 */
359 		NFSUNLOCKNODE(np);
360 		VOP_UNLOCK(vp);
361 		return (EBUSY);
362 	}
363 	if ((ap->a_flags & LK_NOWAIT) != 0 ||
364 	    (lktype == LK_SHARED && vp->v_vnlock->lk_recurse > 0)) {
365 		NFSUNLOCKNODE(np);
366 		return (0);
367 	}
368 	if (lktype == LK_SHARED) {
369 		NFSUNLOCKNODE(np);
370 		VOP_UNLOCK(vp);
371 		ap->a_flags &= ~(LK_TYPE_MASK | LK_INTERLOCK);
372 		ap->a_flags |= LK_EXCLUSIVE;
373 		error = VOP_LOCK1_APV(&default_vnodeops, ap);
374 		if (error != 0 || vp->v_op != &newnfs_vnodeops)
375 			return (error);
376 		if (vp->v_data == NULL)
377 			goto downgrade;
378 		MPASS(vp->v_data == np);
379 		NFSLOCKNODE(np);
380 		if ((np->n_flag & NVNSETSZSKIP) == 0) {
381 			NFSUNLOCKNODE(np);
382 			goto downgrade;
383 		}
384 	}
385 	np->n_flag &= ~NVNSETSZSKIP;
386 	nsize = np->n_size;
387 	NFSUNLOCKNODE(np);
388 	vnode_pager_setsize(vp, nsize);
389 downgrade:
390 	if (lktype == LK_SHARED) {
391 		ap->a_flags &= ~(LK_TYPE_MASK | LK_INTERLOCK);
392 		ap->a_flags |= LK_DOWNGRADE;
393 		(void)VOP_LOCK1_APV(&default_vnodeops, ap);
394 	}
395 	return (0);
396 }
397 
398 static int
nfs34_access_otw(struct vnode * vp,int wmode,struct thread * td,struct ucred * cred,u_int32_t * retmode)399 nfs34_access_otw(struct vnode *vp, int wmode, struct thread *td,
400     struct ucred *cred, u_int32_t *retmode)
401 {
402 	int error = 0, attrflag, i, lrupos;
403 	u_int32_t rmode;
404 	struct nfsnode *np = VTONFS(vp);
405 	struct nfsvattr nfsva;
406 
407 	error = nfsrpc_accessrpc(vp, wmode, cred, td, &nfsva, &attrflag,
408 	    &rmode, NULL);
409 	if (attrflag)
410 		(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
411 	if (!error) {
412 		lrupos = 0;
413 		NFSLOCKNODE(np);
414 		for (i = 0; i < NFS_ACCESSCACHESIZE; i++) {
415 			if (np->n_accesscache[i].uid == cred->cr_uid) {
416 				np->n_accesscache[i].mode = rmode;
417 				np->n_accesscache[i].stamp = time_second;
418 				break;
419 			}
420 			if (i > 0 && np->n_accesscache[i].stamp <
421 			    np->n_accesscache[lrupos].stamp)
422 				lrupos = i;
423 		}
424 		if (i == NFS_ACCESSCACHESIZE) {
425 			np->n_accesscache[lrupos].uid = cred->cr_uid;
426 			np->n_accesscache[lrupos].mode = rmode;
427 			np->n_accesscache[lrupos].stamp = time_second;
428 		}
429 		NFSUNLOCKNODE(np);
430 		if (retmode != NULL)
431 			*retmode = rmode;
432 		KDTRACE_NFS_ACCESSCACHE_LOAD_DONE(vp, cred->cr_uid, rmode, 0);
433 	} else if (NFS_ISV4(vp)) {
434 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
435 	}
436 #ifdef KDTRACE_HOOKS
437 	if (error != 0)
438 		KDTRACE_NFS_ACCESSCACHE_LOAD_DONE(vp, cred->cr_uid, 0,
439 		    error);
440 #endif
441 	return (error);
442 }
443 
444 /*
445  * nfs access vnode op.
446  * For nfs version 2, just return ok. File accesses may fail later.
447  * For nfs version 3, use the access rpc to check accessibility. If file modes
448  * are changed on the server, accesses might still fail later.
449  */
450 static int
nfs_access(struct vop_access_args * ap)451 nfs_access(struct vop_access_args *ap)
452 {
453 	struct vnode *vp = ap->a_vp;
454 	int error = 0, i, gotahit;
455 	u_int32_t mode, wmode, rmode;
456 	int v34 = NFS_ISV34(vp);
457 	struct nfsnode *np = VTONFS(vp);
458 
459 	/*
460 	 * Disallow write attempts on filesystems mounted read-only;
461 	 * unless the file is a socket, fifo, or a block or character
462 	 * device resident on the filesystem.
463 	 */
464 	if ((ap->a_accmode & (VWRITE | VAPPEND | VWRITE_NAMED_ATTRS |
465 	    VDELETE_CHILD | VWRITE_ATTRIBUTES | VDELETE | VWRITE_ACL |
466 	    VWRITE_OWNER)) != 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) != 0) {
467 		switch (vp->v_type) {
468 		case VREG:
469 		case VDIR:
470 		case VLNK:
471 			return (EROFS);
472 		default:
473 			break;
474 		}
475 	}
476 	/*
477 	 * For nfs v3 or v4, check to see if we have done this recently, and if
478 	 * so return our cached result instead of making an ACCESS call.
479 	 * If not, do an access rpc, otherwise you are stuck emulating
480 	 * ufs_access() locally using the vattr. This may not be correct,
481 	 * since the server may apply other access criteria such as
482 	 * client uid-->server uid mapping that we do not know about.
483 	 */
484 	if (v34) {
485 		if (ap->a_accmode & VREAD)
486 			mode = NFSACCESS_READ;
487 		else
488 			mode = 0;
489 		if (vp->v_type != VDIR) {
490 			if (ap->a_accmode & VWRITE)
491 				mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
492 			if (ap->a_accmode & VAPPEND)
493 				mode |= NFSACCESS_EXTEND;
494 			if (ap->a_accmode & VEXEC)
495 				mode |= NFSACCESS_EXECUTE;
496 			if (ap->a_accmode & VDELETE)
497 				mode |= NFSACCESS_DELETE;
498 		} else {
499 			if (ap->a_accmode & VWRITE)
500 				mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND);
501 			if (ap->a_accmode & VAPPEND)
502 				mode |= NFSACCESS_EXTEND;
503 			if (ap->a_accmode & VEXEC)
504 				mode |= NFSACCESS_LOOKUP;
505 			if (ap->a_accmode & VDELETE)
506 				mode |= NFSACCESS_DELETE;
507 			if (ap->a_accmode & VDELETE_CHILD)
508 				mode |= NFSACCESS_MODIFY;
509 		}
510 		/* XXX safety belt, only make blanket request if caching */
511 		if (nfsaccess_cache_timeout > 0) {
512 			wmode = NFSACCESS_READ | NFSACCESS_MODIFY |
513 				NFSACCESS_EXTEND | NFSACCESS_EXECUTE |
514 				NFSACCESS_DELETE | NFSACCESS_LOOKUP;
515 		} else {
516 			wmode = mode;
517 		}
518 
519 		/*
520 		 * Does our cached result allow us to give a definite yes to
521 		 * this request?
522 		 */
523 		gotahit = 0;
524 		NFSLOCKNODE(np);
525 		for (i = 0; i < NFS_ACCESSCACHESIZE; i++) {
526 			if (ap->a_cred->cr_uid == np->n_accesscache[i].uid) {
527 			    if (time_second < (np->n_accesscache[i].stamp
528 				+ nfsaccess_cache_timeout) &&
529 				(np->n_accesscache[i].mode & mode) == mode) {
530 				NFSINCRGLOBAL(nfsstatsv1.accesscache_hits);
531 				gotahit = 1;
532 			    }
533 			    break;
534 			}
535 		}
536 		NFSUNLOCKNODE(np);
537 #ifdef KDTRACE_HOOKS
538 		if (gotahit != 0)
539 			KDTRACE_NFS_ACCESSCACHE_GET_HIT(vp,
540 			    ap->a_cred->cr_uid, mode);
541 		else
542 			KDTRACE_NFS_ACCESSCACHE_GET_MISS(vp,
543 			    ap->a_cred->cr_uid, mode);
544 #endif
545 		if (gotahit == 0) {
546 			/*
547 			 * Either a no, or a don't know.  Go to the wire.
548 			 */
549 			NFSINCRGLOBAL(nfsstatsv1.accesscache_misses);
550 		        error = nfs34_access_otw(vp, wmode, ap->a_td,
551 			    ap->a_cred, &rmode);
552 			if (!error &&
553 			    (rmode & mode) != mode)
554 				error = EACCES;
555 		}
556 		return (error);
557 	} else {
558 		if ((error = nfsspec_access(ap)) != 0) {
559 			return (error);
560 		}
561 		/*
562 		 * Attempt to prevent a mapped root from accessing a file
563 		 * which it shouldn't.  We try to read a byte from the file
564 		 * if the user is root and the file is not zero length.
565 		 * After calling nfsspec_access, we should have the correct
566 		 * file size cached.
567 		 */
568 		NFSLOCKNODE(np);
569 		if (ap->a_cred->cr_uid == 0 && (ap->a_accmode & VREAD)
570 		    && VTONFS(vp)->n_size > 0) {
571 			struct iovec aiov;
572 			struct uio auio;
573 			char buf[1];
574 
575 			NFSUNLOCKNODE(np);
576 			aiov.iov_base = buf;
577 			aiov.iov_len = 1;
578 			auio.uio_iov = &aiov;
579 			auio.uio_iovcnt = 1;
580 			auio.uio_offset = 0;
581 			auio.uio_resid = 1;
582 			auio.uio_segflg = UIO_SYSSPACE;
583 			auio.uio_rw = UIO_READ;
584 			auio.uio_td = ap->a_td;
585 
586 			if (vp->v_type == VREG)
587 				error = ncl_readrpc(vp, &auio, ap->a_cred);
588 			else if (vp->v_type == VDIR) {
589 				char* bp;
590 				bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
591 				aiov.iov_base = bp;
592 				aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
593 				error = ncl_readdirrpc(vp, &auio, ap->a_cred,
594 				    ap->a_td);
595 				free(bp, M_TEMP);
596 			} else if (vp->v_type == VLNK)
597 				error = ncl_readlinkrpc(vp, &auio, ap->a_cred);
598 			else
599 				error = EACCES;
600 		} else
601 			NFSUNLOCKNODE(np);
602 		return (error);
603 	}
604 }
605 
606 /*
607  * nfs open vnode op
608  * Check to see if the type is ok
609  * and that deletion is not in progress.
610  * For paged in text files, you will need to flush the page cache
611  * if consistency is lost.
612  */
613 /* ARGSUSED */
614 static int
nfs_open(struct vop_open_args * ap)615 nfs_open(struct vop_open_args *ap)
616 {
617 	struct vnode *vp = ap->a_vp;
618 	struct nfsnode *np = VTONFS(vp);
619 	struct vattr vattr;
620 	int error;
621 	int fmode = ap->a_mode;
622 	struct ucred *cred;
623 	vm_object_t obj;
624 
625 	if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK)
626 		return (EOPNOTSUPP);
627 
628 	/*
629 	 * For NFSv4, we need to do the Open Op before cache validation,
630 	 * so that we conform to RFC3530 Sec. 9.3.1.
631 	 */
632 	if (NFS_ISV4(vp)) {
633 		error = nfsrpc_open(vp, fmode, ap->a_cred, ap->a_td);
634 		if (error) {
635 			error = nfscl_maperr(ap->a_td, error, (uid_t)0,
636 			    (gid_t)0);
637 			return (error);
638 		}
639 	}
640 
641 	/*
642 	 * Now, if this Open will be doing reading, re-validate/flush the
643 	 * cache, so that Close/Open coherency is maintained.
644 	 */
645 	NFSLOCKNODE(np);
646 	if (np->n_flag & NMODIFIED) {
647 		NFSUNLOCKNODE(np);
648 		if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
649 			NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
650 			if (VN_IS_DOOMED(vp))
651 				return (EBADF);
652 		}
653 		error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
654 		if (error == EINTR || error == EIO) {
655 			if (NFS_ISV4(vp))
656 				(void) nfsrpc_close(vp, 0, ap->a_td);
657 			return (error);
658 		}
659 		NFSLOCKNODE(np);
660 		np->n_attrstamp = 0;
661 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
662 		if (vp->v_type == VDIR)
663 			np->n_direofoffset = 0;
664 		NFSUNLOCKNODE(np);
665 		error = VOP_GETATTR(vp, &vattr, ap->a_cred);
666 		if (error) {
667 			if (NFS_ISV4(vp))
668 				(void) nfsrpc_close(vp, 0, ap->a_td);
669 			return (error);
670 		}
671 		NFSLOCKNODE(np);
672 		np->n_mtime = vattr.va_mtime;
673 		if (NFS_ISV4(vp))
674 			np->n_change = vattr.va_filerev;
675 	} else {
676 		NFSUNLOCKNODE(np);
677 		error = VOP_GETATTR(vp, &vattr, ap->a_cred);
678 		if (error) {
679 			if (NFS_ISV4(vp))
680 				(void) nfsrpc_close(vp, 0, ap->a_td);
681 			return (error);
682 		}
683 		NFSLOCKNODE(np);
684 		if ((NFS_ISV4(vp) && np->n_change != vattr.va_filerev) ||
685 		    NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
686 			if (vp->v_type == VDIR)
687 				np->n_direofoffset = 0;
688 			NFSUNLOCKNODE(np);
689 			if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
690 				NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
691 				if (VN_IS_DOOMED(vp))
692 					return (EBADF);
693 			}
694 			error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
695 			if (error == EINTR || error == EIO) {
696 				if (NFS_ISV4(vp))
697 					(void) nfsrpc_close(vp, 0, ap->a_td);
698 				return (error);
699 			}
700 			NFSLOCKNODE(np);
701 			np->n_mtime = vattr.va_mtime;
702 			if (NFS_ISV4(vp))
703 				np->n_change = vattr.va_filerev;
704 		}
705 	}
706 
707 	/*
708 	 * If the object has >= 1 O_DIRECT active opens, we disable caching.
709 	 */
710 	if (newnfs_directio_enable && (fmode & O_DIRECT) &&
711 	    (vp->v_type == VREG)) {
712 		if (np->n_directio_opens == 0) {
713 			NFSUNLOCKNODE(np);
714 			if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
715 				NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
716 				if (VN_IS_DOOMED(vp))
717 					return (EBADF);
718 			}
719 			error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
720 			if (error) {
721 				if (NFS_ISV4(vp))
722 					(void) nfsrpc_close(vp, 0, ap->a_td);
723 				return (error);
724 			}
725 			NFSLOCKNODE(np);
726 			np->n_flag |= NNONCACHE;
727 		}
728 		np->n_directio_opens++;
729 	}
730 
731 	/* If opened for writing via NFSv4.1 or later, mark that for pNFS. */
732 	if (NFSHASPNFS(VFSTONFS(vp->v_mount)) && (fmode & FWRITE) != 0)
733 		np->n_flag |= NWRITEOPENED;
734 
735 	/*
736 	 * If this is an open for writing, capture a reference to the
737 	 * credentials, so they can be used by ncl_putpages(). Using
738 	 * these write credentials is preferable to the credentials of
739 	 * whatever thread happens to be doing the VOP_PUTPAGES() since
740 	 * the write RPCs are less likely to fail with EACCES.
741 	 */
742 	if ((fmode & FWRITE) != 0) {
743 		cred = np->n_writecred;
744 		np->n_writecred = crhold(ap->a_cred);
745 	} else
746 		cred = NULL;
747 	NFSUNLOCKNODE(np);
748 
749 	if (cred != NULL)
750 		crfree(cred);
751 	vnode_create_vobject(vp, vattr.va_size, ap->a_td);
752 
753 	/*
754 	 * If the text file has been mmap'd, flush any dirty pages to the
755 	 * buffer cache and then...
756 	 * Make sure all writes are pushed to the NFS server.  If this is not
757 	 * done, the modify time of the file can change while the text
758 	 * file is being executed.  This will cause the process that is
759 	 * executing the text file to be terminated.
760 	 */
761 	if (vp->v_writecount <= -1) {
762 		if ((obj = vp->v_object) != NULL &&
763 		    vm_object_mightbedirty(obj)) {
764 			if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
765 				NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
766 				if (VN_IS_DOOMED(vp))
767 					return (EBADF);
768 			}
769 			VM_OBJECT_WLOCK(obj);
770 			vm_object_page_clean(obj, 0, 0, OBJPC_SYNC);
771 			VM_OBJECT_WUNLOCK(obj);
772 		}
773 
774 		/* Now, flush the buffer cache. */
775 		ncl_flush(vp, MNT_WAIT, curthread, 0, 0);
776 
777 		/* And, finally, make sure that n_mtime is up to date. */
778 		np = VTONFS(vp);
779 		NFSLOCKNODE(np);
780 		np->n_mtime = np->n_vattr.na_mtime;
781 		NFSUNLOCKNODE(np);
782 	}
783 	return (0);
784 }
785 
786 /*
787  * nfs close vnode op
788  * What an NFS client should do upon close after writing is a debatable issue.
789  * Most NFS clients push delayed writes to the server upon close, basically for
790  * two reasons:
791  * 1 - So that any write errors may be reported back to the client process
792  *     doing the close system call. By far the two most likely errors are
793  *     NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
794  * 2 - To put a worst case upper bound on cache inconsistency between
795  *     multiple clients for the file.
796  * There is also a consistency problem for Version 2 of the protocol w.r.t.
797  * not being able to tell if other clients are writing a file concurrently,
798  * since there is no way of knowing if the changed modify time in the reply
799  * is only due to the write for this client.
800  * (NFS Version 3 provides weak cache consistency data in the reply that
801  *  should be sufficient to detect and handle this case.)
802  *
803  * The current code does the following:
804  * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
805  * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
806  *                     or commit them (this satisfies 1 and 2 except for the
807  *                     case where the server crashes after this close but
808  *                     before the commit RPC, which is felt to be "good
809  *                     enough". Changing the last argument to ncl_flush() to
810  *                     a 1 would force a commit operation, if it is felt a
811  *                     commit is necessary now.
812  * for NFS Version 4 - flush the dirty buffers and commit them, if
813  *		       nfscl_mustflush() says this is necessary.
814  *                     It is necessary if there is no write delegation held,
815  *                     in order to satisfy open/close coherency.
816  *                     If the file isn't cached on local stable storage,
817  *                     it may be necessary in order to detect "out of space"
818  *                     errors from the server, if the write delegation
819  *                     issued by the server doesn't allow the file to grow.
820  */
821 /* ARGSUSED */
822 static int
nfs_close(struct vop_close_args * ap)823 nfs_close(struct vop_close_args *ap)
824 {
825 	struct vnode *vp = ap->a_vp;
826 	struct nfsnode *np = VTONFS(vp);
827 	struct nfsvattr nfsva;
828 	struct ucred *cred;
829 	int error = 0, ret, localcred = 0;
830 	int fmode = ap->a_fflag;
831 
832 	if (NFSCL_FORCEDISM(vp->v_mount))
833 		return (0);
834 	/*
835 	 * During shutdown, a_cred isn't valid, so just use root.
836 	 */
837 	if (ap->a_cred == NOCRED) {
838 		cred = newnfs_getcred();
839 		localcred = 1;
840 	} else {
841 		cred = ap->a_cred;
842 	}
843 	if (vp->v_type == VREG) {
844 	    /*
845 	     * Examine and clean dirty pages, regardless of NMODIFIED.
846 	     * This closes a major hole in close-to-open consistency.
847 	     * We want to push out all dirty pages (and buffers) on
848 	     * close, regardless of whether they were dirtied by
849 	     * mmap'ed writes or via write().
850 	     */
851 	    if (nfs_clean_pages_on_close && vp->v_object) {
852 		if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
853 			NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
854 			if (VN_IS_DOOMED(vp) && ap->a_fflag != FNONBLOCK)
855 				return (EBADF);
856 		}
857 		VM_OBJECT_WLOCK(vp->v_object);
858 		vm_object_page_clean(vp->v_object, 0, 0, 0);
859 		VM_OBJECT_WUNLOCK(vp->v_object);
860 	    }
861 	    NFSLOCKNODE(np);
862 	    if (np->n_flag & NMODIFIED) {
863 		NFSUNLOCKNODE(np);
864 		if (NFS_ISV3(vp)) {
865 		    /*
866 		     * Under NFSv3 we have dirty buffers to dispose of.  We
867 		     * must flush them to the NFS server.  We have the option
868 		     * of waiting all the way through the commit rpc or just
869 		     * waiting for the initial write.  The default is to only
870 		     * wait through the initial write so the data is in the
871 		     * server's cache, which is roughly similar to the state
872 		     * a standard disk subsystem leaves the file in on close().
873 		     *
874 		     * We cannot clear the NMODIFIED bit in np->n_flag due to
875 		     * potential races with other processes, and certainly
876 		     * cannot clear it if we don't commit.
877 		     * These races occur when there is no longer the old
878 		     * traditional vnode locking implemented for Vnode Ops.
879 		     */
880 		    int cm = newnfs_commit_on_close ? 1 : 0;
881 		    if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
882 			    NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
883 			    if (VN_IS_DOOMED(vp) && ap->a_fflag != FNONBLOCK)
884 				    return (EBADF);
885 		    }
886 		    error = ncl_flush(vp, MNT_WAIT, ap->a_td, cm, 0);
887 		    /* np->n_flag &= ~NMODIFIED; */
888 		} else if (NFS_ISV4(vp)) {
889 			if (nfscl_mustflush(vp) != 0) {
890 				int cm = newnfs_commit_on_close ? 1 : 0;
891 				if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
892 					NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
893 					if (VN_IS_DOOMED(vp) && ap->a_fflag !=
894 					    FNONBLOCK)
895 						return (EBADF);
896 				}
897 				error = ncl_flush(vp, MNT_WAIT, ap->a_td,
898 				    cm, 0);
899 				/*
900 				 * as above w.r.t races when clearing
901 				 * NMODIFIED.
902 				 * np->n_flag &= ~NMODIFIED;
903 				 */
904 			}
905 		} else {
906 			if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
907 				NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
908 				if (VN_IS_DOOMED(vp) && ap->a_fflag !=
909 				    FNONBLOCK)
910 					return (EBADF);
911 			}
912 			error = ncl_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
913 		}
914 		NFSLOCKNODE(np);
915 	    }
916  	    /*
917  	     * Invalidate the attribute cache in all cases.
918  	     * An open is going to fetch fresh attrs any way, other procs
919  	     * on this node that have file open will be forced to do an
920  	     * otw attr fetch, but this is safe.
921 	     * --> A user found that their RPC count dropped by 20% when
922 	     *     this was commented out and I can't see any requirement
923 	     *     for it, so I've disabled it when negative lookups are
924 	     *     enabled. (What does this have to do with negative lookup
925 	     *     caching? Well nothing, except it was reported by the
926 	     *     same user that needed negative lookup caching and I wanted
927 	     *     there to be a way to disable it to see if it
928 	     *     is the cause of some caching/coherency issue that might
929 	     *     crop up.)
930  	     */
931 	    if (VFSTONFS(vp->v_mount)->nm_negnametimeo == 0) {
932 		    np->n_attrstamp = 0;
933 		    KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
934 	    }
935 	    if (np->n_flag & NWRITEERR) {
936 		np->n_flag &= ~NWRITEERR;
937 		error = np->n_error;
938 	    }
939 	    NFSUNLOCKNODE(np);
940 	}
941 
942 	if (NFS_ISV4(vp)) {
943 		/*
944 		 * Get attributes so "change" is up to date.
945 		 */
946 		if (error == 0 && nfscl_mustflush(vp) != 0 &&
947 		    vp->v_type == VREG &&
948 		    (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOCTO) == 0) {
949 			ret = nfsrpc_getattr(vp, cred, ap->a_td, &nfsva,
950 			    NULL);
951 			if (!ret) {
952 				np->n_change = nfsva.na_filerev;
953 				(void) nfscl_loadattrcache(&vp, &nfsva, NULL,
954 				    NULL, 0, 0);
955 			}
956 		}
957 
958 		/*
959 		 * and do the close.
960 		 */
961 		ret = nfsrpc_close(vp, 0, ap->a_td);
962 		if (!error && ret)
963 			error = ret;
964 		if (error)
965 			error = nfscl_maperr(ap->a_td, error, (uid_t)0,
966 			    (gid_t)0);
967 	}
968 	if (newnfs_directio_enable)
969 		KASSERT((np->n_directio_asyncwr == 0),
970 			("nfs_close: dirty unflushed (%d) directio buffers\n",
971 			 np->n_directio_asyncwr));
972 	if (newnfs_directio_enable && (fmode & O_DIRECT) && (vp->v_type == VREG)) {
973 		NFSLOCKNODE(np);
974 		KASSERT((np->n_directio_opens > 0),
975 			("nfs_close: unexpectedly value (0) of n_directio_opens\n"));
976 		np->n_directio_opens--;
977 		if (np->n_directio_opens == 0)
978 			np->n_flag &= ~NNONCACHE;
979 		NFSUNLOCKNODE(np);
980 	}
981 	if (localcred)
982 		NFSFREECRED(cred);
983 	return (error);
984 }
985 
986 /*
987  * nfs getattr call from vfs.
988  */
989 static int
nfs_getattr(struct vop_getattr_args * ap)990 nfs_getattr(struct vop_getattr_args *ap)
991 {
992 	struct vnode *vp = ap->a_vp;
993 	struct thread *td = curthread;	/* XXX */
994 	struct nfsnode *np = VTONFS(vp);
995 	int error = 0;
996 	struct nfsvattr nfsva;
997 	struct vattr *vap = ap->a_vap;
998 	struct vattr vattr;
999 
1000 	/*
1001 	 * Update local times for special files.
1002 	 */
1003 	NFSLOCKNODE(np);
1004 	if (np->n_flag & (NACC | NUPD))
1005 		np->n_flag |= NCHG;
1006 	NFSUNLOCKNODE(np);
1007 	/*
1008 	 * First look in the cache.
1009 	 */
1010 	if (ncl_getattrcache(vp, &vattr) == 0) {
1011 		ncl_copy_vattr(vap, &vattr);
1012 
1013 		/*
1014 		 * Get the local modify time for the case of a write
1015 		 * delegation.
1016 		 */
1017 		nfscl_deleggetmodtime(vp, &vap->va_mtime);
1018 		return (0);
1019 	}
1020 
1021 	if (NFS_ISV34(vp) && nfs_prime_access_cache &&
1022 	    nfsaccess_cache_timeout > 0) {
1023 		NFSINCRGLOBAL(nfsstatsv1.accesscache_misses);
1024 		nfs34_access_otw(vp, NFSACCESS_ALL, td, ap->a_cred, NULL);
1025 		if (ncl_getattrcache(vp, ap->a_vap) == 0) {
1026 			nfscl_deleggetmodtime(vp, &ap->a_vap->va_mtime);
1027 			return (0);
1028 		}
1029 	}
1030 	error = nfsrpc_getattr(vp, ap->a_cred, td, &nfsva, NULL);
1031 	if (!error)
1032 		error = nfscl_loadattrcache(&vp, &nfsva, vap, NULL, 0, 0);
1033 	if (!error) {
1034 		/*
1035 		 * Get the local modify time for the case of a write
1036 		 * delegation.
1037 		 */
1038 		nfscl_deleggetmodtime(vp, &vap->va_mtime);
1039 	} else if (NFS_ISV4(vp)) {
1040 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
1041 	}
1042 	return (error);
1043 }
1044 
1045 /*
1046  * nfs setattr call.
1047  */
1048 static int
nfs_setattr(struct vop_setattr_args * ap)1049 nfs_setattr(struct vop_setattr_args *ap)
1050 {
1051 	struct vnode *vp = ap->a_vp;
1052 	struct nfsnode *np = VTONFS(vp);
1053 	struct thread *td = curthread;	/* XXX */
1054 	struct vattr *vap = ap->a_vap;
1055 	int error = 0;
1056 	u_quad_t tsize;
1057 	struct timespec ts;
1058 
1059 #ifndef nolint
1060 	tsize = (u_quad_t)0;
1061 #endif
1062 
1063 	/*
1064 	 * Setting of flags and marking of atimes are not supported.
1065 	 */
1066 	if (vap->va_flags != VNOVAL)
1067 		return (EOPNOTSUPP);
1068 
1069 	/*
1070 	 * Disallow write attempts if the filesystem is mounted read-only.
1071 	 */
1072   	if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
1073 	    vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
1074 	    vap->va_mtime.tv_sec != VNOVAL ||
1075 	    vap->va_birthtime.tv_sec != VNOVAL ||
1076 	    vap->va_mode != (mode_t)VNOVAL) &&
1077 	    (vp->v_mount->mnt_flag & MNT_RDONLY))
1078 		return (EROFS);
1079 	if (vap->va_size != VNOVAL) {
1080  		switch (vp->v_type) {
1081  		case VDIR:
1082  			return (EISDIR);
1083  		case VCHR:
1084  		case VBLK:
1085  		case VSOCK:
1086  		case VFIFO:
1087 			if (vap->va_mtime.tv_sec == VNOVAL &&
1088 			    vap->va_atime.tv_sec == VNOVAL &&
1089 			    vap->va_birthtime.tv_sec == VNOVAL &&
1090 			    vap->va_mode == (mode_t)VNOVAL &&
1091 			    vap->va_uid == (uid_t)VNOVAL &&
1092 			    vap->va_gid == (gid_t)VNOVAL)
1093 				return (0);
1094  			vap->va_size = VNOVAL;
1095  			break;
1096  		default:
1097 			/*
1098 			 * Disallow write attempts if the filesystem is
1099 			 * mounted read-only.
1100 			 */
1101 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
1102 				return (EROFS);
1103 			/*
1104 			 *  We run vnode_pager_setsize() early (why?),
1105 			 * we must set np->n_size now to avoid vinvalbuf
1106 			 * V_SAVE races that might setsize a lower
1107 			 * value.
1108 			 */
1109 			NFSLOCKNODE(np);
1110 			tsize = np->n_size;
1111 			NFSUNLOCKNODE(np);
1112 			error = ncl_meta_setsize(vp, td, vap->va_size);
1113 			NFSLOCKNODE(np);
1114  			if (np->n_flag & NMODIFIED) {
1115 			    tsize = np->n_size;
1116 			    NFSUNLOCKNODE(np);
1117 			    error = ncl_vinvalbuf(vp, vap->va_size == 0 ?
1118 			        0 : V_SAVE, td, 1);
1119 			    if (error != 0) {
1120 				    vnode_pager_setsize(vp, tsize);
1121 				    return (error);
1122 			    }
1123 			    /*
1124 			     * Call nfscl_delegmodtime() to set the modify time
1125 			     * locally, as required.
1126 			     */
1127 			    nfscl_delegmodtime(vp);
1128  			} else
1129 			    NFSUNLOCKNODE(np);
1130 			/*
1131 			 * np->n_size has already been set to vap->va_size
1132 			 * in ncl_meta_setsize(). We must set it again since
1133 			 * nfs_loadattrcache() could be called through
1134 			 * ncl_meta_setsize() and could modify np->n_size.
1135 			 */
1136 			NFSLOCKNODE(np);
1137  			np->n_vattr.na_size = np->n_size = vap->va_size;
1138 			NFSUNLOCKNODE(np);
1139   		}
1140   	} else {
1141 		NFSLOCKNODE(np);
1142 		if ((vap->va_mtime.tv_sec != VNOVAL || vap->va_atime.tv_sec != VNOVAL) &&
1143 		    (np->n_flag & NMODIFIED) && vp->v_type == VREG) {
1144 			NFSUNLOCKNODE(np);
1145 			error = ncl_vinvalbuf(vp, V_SAVE, td, 1);
1146 			if (error == EINTR || error == EIO)
1147 				return (error);
1148 		} else
1149 			NFSUNLOCKNODE(np);
1150 	}
1151 	error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
1152 	if (vap->va_size != VNOVAL) {
1153 		if (error == 0) {
1154 			nanouptime(&ts);
1155 			NFSLOCKNODE(np);
1156 			np->n_localmodtime = ts;
1157 			NFSUNLOCKNODE(np);
1158 		} else {
1159 			NFSLOCKNODE(np);
1160 			np->n_size = np->n_vattr.na_size = tsize;
1161 			vnode_pager_setsize(vp, tsize);
1162 			NFSUNLOCKNODE(np);
1163 		}
1164 	}
1165 	return (error);
1166 }
1167 
1168 /*
1169  * Do an nfs setattr rpc.
1170  */
1171 static int
nfs_setattrrpc(struct vnode * vp,struct vattr * vap,struct ucred * cred,struct thread * td)1172 nfs_setattrrpc(struct vnode *vp, struct vattr *vap, struct ucred *cred,
1173     struct thread *td)
1174 {
1175 	struct nfsnode *np = VTONFS(vp);
1176 	int error, ret, attrflag, i;
1177 	struct nfsvattr nfsva;
1178 
1179 	if (NFS_ISV34(vp)) {
1180 		NFSLOCKNODE(np);
1181 		for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
1182 			np->n_accesscache[i].stamp = 0;
1183 		np->n_flag |= NDELEGMOD;
1184 		NFSUNLOCKNODE(np);
1185 		KDTRACE_NFS_ACCESSCACHE_FLUSH_DONE(vp);
1186 	}
1187 	error = nfsrpc_setattr(vp, vap, NULL, cred, td, &nfsva, &attrflag,
1188 	    NULL);
1189 	if (attrflag) {
1190 		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1191 		if (ret && !error)
1192 			error = ret;
1193 	}
1194 	if (error && NFS_ISV4(vp))
1195 		error = nfscl_maperr(td, error, vap->va_uid, vap->va_gid);
1196 	return (error);
1197 }
1198 
1199 /*
1200  * nfs lookup call, one step at a time...
1201  * First look in cache
1202  * If not found, unlock the directory nfsnode and do the rpc
1203  */
1204 static int
nfs_lookup(struct vop_lookup_args * ap)1205 nfs_lookup(struct vop_lookup_args *ap)
1206 {
1207 	struct componentname *cnp = ap->a_cnp;
1208 	struct vnode *dvp = ap->a_dvp;
1209 	struct vnode **vpp = ap->a_vpp;
1210 	struct mount *mp = dvp->v_mount;
1211 	int flags = cnp->cn_flags;
1212 	struct vnode *newvp;
1213 	struct nfsmount *nmp;
1214 	struct nfsnode *np, *newnp;
1215 	int error = 0, attrflag, dattrflag, ltype, ncticks;
1216 	struct thread *td = cnp->cn_thread;
1217 	struct nfsfh *nfhp;
1218 	struct nfsvattr dnfsva, nfsva;
1219 	struct vattr vattr;
1220 	struct timespec nctime, ts;
1221 
1222 	*vpp = NULLVP;
1223 	if ((flags & ISLASTCN) && (mp->mnt_flag & MNT_RDONLY) &&
1224 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
1225 		return (EROFS);
1226 	if (dvp->v_type != VDIR)
1227 		return (ENOTDIR);
1228 	nmp = VFSTONFS(mp);
1229 	np = VTONFS(dvp);
1230 
1231 	/* For NFSv4, wait until any remove is done. */
1232 	NFSLOCKNODE(np);
1233 	while (NFSHASNFSV4(nmp) && (np->n_flag & NREMOVEINPROG)) {
1234 		np->n_flag |= NREMOVEWANT;
1235 		(void) msleep((caddr_t)np, &np->n_mtx, PZERO, "nfslkup", 0);
1236 	}
1237 	NFSUNLOCKNODE(np);
1238 
1239 	error = vn_dir_check_exec(dvp, cnp);
1240 	if (error != 0)
1241 		return (error);
1242 	error = cache_lookup(dvp, vpp, cnp, &nctime, &ncticks);
1243 	if (error > 0 && error != ENOENT)
1244 		return (error);
1245 	if (error == -1) {
1246 		/*
1247 		 * Lookups of "." are special and always return the
1248 		 * current directory.  cache_lookup() already handles
1249 		 * associated locking bookkeeping, etc.
1250 		 */
1251 		if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
1252 			/* XXX: Is this really correct? */
1253 			if (cnp->cn_nameiop != LOOKUP &&
1254 			    (flags & ISLASTCN))
1255 				cnp->cn_flags |= SAVENAME;
1256 			return (0);
1257 		}
1258 
1259 		/*
1260 		 * We only accept a positive hit in the cache if the
1261 		 * change time of the file matches our cached copy.
1262 		 * Otherwise, we discard the cache entry and fallback
1263 		 * to doing a lookup RPC.  We also only trust cache
1264 		 * entries for less than nm_nametimeo seconds.
1265 		 *
1266 		 * To better handle stale file handles and attributes,
1267 		 * clear the attribute cache of this node if it is a
1268 		 * leaf component, part of an open() call, and not
1269 		 * locally modified before fetching the attributes.
1270 		 * This should allow stale file handles to be detected
1271 		 * here where we can fall back to a LOOKUP RPC to
1272 		 * recover rather than having nfs_open() detect the
1273 		 * stale file handle and failing open(2) with ESTALE.
1274 		 */
1275 		newvp = *vpp;
1276 		newnp = VTONFS(newvp);
1277 		if (!(nmp->nm_flag & NFSMNT_NOCTO) &&
1278 		    (flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) &&
1279 		    !(newnp->n_flag & NMODIFIED)) {
1280 			NFSLOCKNODE(newnp);
1281 			newnp->n_attrstamp = 0;
1282 			KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
1283 			NFSUNLOCKNODE(newnp);
1284 		}
1285 		if (nfscl_nodeleg(newvp, 0) == 0 ||
1286 		    ((u_int)(ticks - ncticks) < (nmp->nm_nametimeo * hz) &&
1287 		    VOP_GETATTR(newvp, &vattr, cnp->cn_cred) == 0 &&
1288 		    timespeccmp(&vattr.va_ctime, &nctime, ==))) {
1289 			NFSINCRGLOBAL(nfsstatsv1.lookupcache_hits);
1290 			if (cnp->cn_nameiop != LOOKUP &&
1291 			    (flags & ISLASTCN))
1292 				cnp->cn_flags |= SAVENAME;
1293 			return (0);
1294 		}
1295 		cache_purge(newvp);
1296 		if (dvp != newvp)
1297 			vput(newvp);
1298 		else
1299 			vrele(newvp);
1300 		*vpp = NULLVP;
1301 	} else if (error == ENOENT) {
1302 		if (VN_IS_DOOMED(dvp))
1303 			return (ENOENT);
1304 		/*
1305 		 * We only accept a negative hit in the cache if the
1306 		 * modification time of the parent directory matches
1307 		 * the cached copy in the name cache entry.
1308 		 * Otherwise, we discard all of the negative cache
1309 		 * entries for this directory.  We also only trust
1310 		 * negative cache entries for up to nm_negnametimeo
1311 		 * seconds.
1312 		 */
1313 		if ((u_int)(ticks - ncticks) < (nmp->nm_negnametimeo * hz) &&
1314 		    VOP_GETATTR(dvp, &vattr, cnp->cn_cred) == 0 &&
1315 		    timespeccmp(&vattr.va_mtime, &nctime, ==)) {
1316 			NFSINCRGLOBAL(nfsstatsv1.lookupcache_hits);
1317 			return (ENOENT);
1318 		}
1319 		cache_purge_negative(dvp);
1320 	}
1321 
1322 	newvp = NULLVP;
1323 	NFSINCRGLOBAL(nfsstatsv1.lookupcache_misses);
1324 	nanouptime(&ts);
1325 	error = nfsrpc_lookup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1326 	    cnp->cn_cred, td, &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1327 	    NULL);
1328 	if (dattrflag)
1329 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1330 	if (error) {
1331 		if (newvp != NULLVP) {
1332 			vput(newvp);
1333 			*vpp = NULLVP;
1334 		}
1335 
1336 		if (error != ENOENT) {
1337 			if (NFS_ISV4(dvp))
1338 				error = nfscl_maperr(td, error, (uid_t)0,
1339 				    (gid_t)0);
1340 			return (error);
1341 		}
1342 
1343 		/* The requested file was not found. */
1344 		if ((cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME) &&
1345 		    (flags & ISLASTCN)) {
1346 			/*
1347 			 * XXX: UFS does a full VOP_ACCESS(dvp,
1348 			 * VWRITE) here instead of just checking
1349 			 * MNT_RDONLY.
1350 			 */
1351 			if (mp->mnt_flag & MNT_RDONLY)
1352 				return (EROFS);
1353 			cnp->cn_flags |= SAVENAME;
1354 			return (EJUSTRETURN);
1355 		}
1356 
1357 		if ((cnp->cn_flags & MAKEENTRY) != 0 && dattrflag) {
1358 			/*
1359 			 * Cache the modification time of the parent
1360 			 * directory from the post-op attributes in
1361 			 * the name cache entry.  The negative cache
1362 			 * entry will be ignored once the directory
1363 			 * has changed.  Don't bother adding the entry
1364 			 * if the directory has already changed.
1365 			 */
1366 			NFSLOCKNODE(np);
1367 			if (timespeccmp(&np->n_vattr.na_mtime,
1368 			    &dnfsva.na_mtime, ==)) {
1369 				NFSUNLOCKNODE(np);
1370 				cache_enter_time(dvp, NULL, cnp,
1371 				    &dnfsva.na_mtime, NULL);
1372 			} else
1373 				NFSUNLOCKNODE(np);
1374 		}
1375 		return (ENOENT);
1376 	}
1377 
1378 	/*
1379 	 * Handle RENAME case...
1380 	 */
1381 	if (cnp->cn_nameiop == RENAME && (flags & ISLASTCN)) {
1382 		if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1383 			free(nfhp, M_NFSFH);
1384 			return (EISDIR);
1385 		}
1386 		error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1387 		    LK_EXCLUSIVE);
1388 		if (error)
1389 			return (error);
1390 		newvp = NFSTOV(np);
1391 		/*
1392 		 * If n_localmodtime >= time before RPC, then
1393 		 * a file modification operation, such as
1394 		 * VOP_SETATTR() of size, has occurred while
1395 		 * the Lookup RPC and acquisition of the vnode
1396 		 * happened.  As such, the attributes might
1397 		 * be stale, with possibly an incorrect size.
1398 		 */
1399 		NFSLOCKNODE(np);
1400 		if (timespecisset(&np->n_localmodtime) &&
1401 		    timespeccmp(&np->n_localmodtime, &ts, >=)) {
1402 			NFSCL_DEBUG(4, "nfs_lookup: rename localmod "
1403 			    "stale attributes\n");
1404 			attrflag = 0;
1405 		}
1406 		NFSUNLOCKNODE(np);
1407 		if (attrflag)
1408 			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1409 			    0, 1);
1410 		*vpp = newvp;
1411 		cnp->cn_flags |= SAVENAME;
1412 		return (0);
1413 	}
1414 
1415 	if (flags & ISDOTDOT) {
1416 		ltype = NFSVOPISLOCKED(dvp);
1417 		error = vfs_busy(mp, MBF_NOWAIT);
1418 		if (error != 0) {
1419 			vfs_ref(mp);
1420 			NFSVOPUNLOCK(dvp);
1421 			error = vfs_busy(mp, 0);
1422 			NFSVOPLOCK(dvp, ltype | LK_RETRY);
1423 			vfs_rel(mp);
1424 			if (error == 0 && VN_IS_DOOMED(dvp)) {
1425 				vfs_unbusy(mp);
1426 				error = ENOENT;
1427 			}
1428 			if (error != 0)
1429 				return (error);
1430 		}
1431 		NFSVOPUNLOCK(dvp);
1432 		error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1433 		    cnp->cn_lkflags);
1434 		if (error == 0)
1435 			newvp = NFSTOV(np);
1436 		vfs_unbusy(mp);
1437 		if (newvp != dvp)
1438 			NFSVOPLOCK(dvp, ltype | LK_RETRY);
1439 		if (VN_IS_DOOMED(dvp)) {
1440 			if (error == 0) {
1441 				if (newvp == dvp)
1442 					vrele(newvp);
1443 				else
1444 					vput(newvp);
1445 			}
1446 			error = ENOENT;
1447 		}
1448 		if (error != 0)
1449 			return (error);
1450 		if (attrflag)
1451 			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1452 			    0, 1);
1453 	} else if (NFS_CMPFH(np, nfhp->nfh_fh, nfhp->nfh_len)) {
1454 		free(nfhp, M_NFSFH);
1455 		VREF(dvp);
1456 		newvp = dvp;
1457 		if (attrflag)
1458 			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1459 			    0, 1);
1460 	} else {
1461 		error = nfscl_nget(mp, dvp, nfhp, cnp, td, &np, NULL,
1462 		    cnp->cn_lkflags);
1463 		if (error)
1464 			return (error);
1465 		newvp = NFSTOV(np);
1466 		/*
1467 		 * If n_localmodtime >= time before RPC, then
1468 		 * a file modification operation, such as
1469 		 * VOP_SETATTR() of size, has occurred while
1470 		 * the Lookup RPC and acquisition of the vnode
1471 		 * happened.  As such, the attributes might
1472 		 * be stale, with possibly an incorrect size.
1473 		 */
1474 		NFSLOCKNODE(np);
1475 		if (timespecisset(&np->n_localmodtime) &&
1476 		    timespeccmp(&np->n_localmodtime, &ts, >=)) {
1477 			NFSCL_DEBUG(4, "nfs_lookup: localmod "
1478 			    "stale attributes\n");
1479 			attrflag = 0;
1480 		}
1481 		NFSUNLOCKNODE(np);
1482 		if (attrflag)
1483 			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1484 			    0, 1);
1485 		else if ((flags & (ISLASTCN | ISOPEN)) == (ISLASTCN | ISOPEN) &&
1486 		    !(np->n_flag & NMODIFIED)) {
1487 			/*
1488 			 * Flush the attribute cache when opening a
1489 			 * leaf node to ensure that fresh attributes
1490 			 * are fetched in nfs_open() since we did not
1491 			 * fetch attributes from the LOOKUP reply.
1492 			 */
1493 			NFSLOCKNODE(np);
1494 			np->n_attrstamp = 0;
1495 			KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(newvp);
1496 			NFSUNLOCKNODE(np);
1497 		}
1498 	}
1499 	if (cnp->cn_nameiop != LOOKUP && (flags & ISLASTCN))
1500 		cnp->cn_flags |= SAVENAME;
1501 	if ((cnp->cn_flags & MAKEENTRY) && dvp != newvp &&
1502 	    (cnp->cn_nameiop != DELETE || !(flags & ISLASTCN)) &&
1503 	    attrflag != 0 && (newvp->v_type != VDIR || dattrflag != 0))
1504 		cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
1505 		    newvp->v_type != VDIR ? NULL : &dnfsva.na_ctime);
1506 	*vpp = newvp;
1507 	return (0);
1508 }
1509 
1510 /*
1511  * nfs read call.
1512  * Just call ncl_bioread() to do the work.
1513  */
1514 static int
nfs_read(struct vop_read_args * ap)1515 nfs_read(struct vop_read_args *ap)
1516 {
1517 	struct vnode *vp = ap->a_vp;
1518 
1519 	switch (vp->v_type) {
1520 	case VREG:
1521 		return (ncl_bioread(vp, ap->a_uio, ap->a_ioflag, ap->a_cred));
1522 	case VDIR:
1523 		return (EISDIR);
1524 	default:
1525 		return (EOPNOTSUPP);
1526 	}
1527 }
1528 
1529 /*
1530  * nfs readlink call
1531  */
1532 static int
nfs_readlink(struct vop_readlink_args * ap)1533 nfs_readlink(struct vop_readlink_args *ap)
1534 {
1535 	struct vnode *vp = ap->a_vp;
1536 
1537 	if (vp->v_type != VLNK)
1538 		return (EINVAL);
1539 	return (ncl_bioread(vp, ap->a_uio, 0, ap->a_cred));
1540 }
1541 
1542 /*
1543  * Do a readlink rpc.
1544  * Called by ncl_doio() from below the buffer cache.
1545  */
1546 int
ncl_readlinkrpc(struct vnode * vp,struct uio * uiop,struct ucred * cred)1547 ncl_readlinkrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1548 {
1549 	int error, ret, attrflag;
1550 	struct nfsvattr nfsva;
1551 
1552 	error = nfsrpc_readlink(vp, uiop, cred, uiop->uio_td, &nfsva,
1553 	    &attrflag, NULL);
1554 	if (attrflag) {
1555 		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1556 		if (ret && !error)
1557 			error = ret;
1558 	}
1559 	if (error && NFS_ISV4(vp))
1560 		error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1561 	return (error);
1562 }
1563 
1564 /*
1565  * nfs read rpc call
1566  * Ditto above
1567  */
1568 int
ncl_readrpc(struct vnode * vp,struct uio * uiop,struct ucred * cred)1569 ncl_readrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred)
1570 {
1571 	int error, ret, attrflag;
1572 	struct nfsvattr nfsva;
1573 	struct nfsmount *nmp;
1574 
1575 	nmp = VFSTONFS(vp->v_mount);
1576 	error = EIO;
1577 	attrflag = 0;
1578 	if (NFSHASPNFS(nmp))
1579 		error = nfscl_doiods(vp, uiop, NULL, NULL,
1580 		    NFSV4OPEN_ACCESSREAD, 0, cred, uiop->uio_td);
1581 	NFSCL_DEBUG(4, "readrpc: aft doiods=%d\n", error);
1582 	if (error != 0)
1583 		error = nfsrpc_read(vp, uiop, cred, uiop->uio_td, &nfsva,
1584 		    &attrflag, NULL);
1585 	if (attrflag) {
1586 		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
1587 		if (ret && !error)
1588 			error = ret;
1589 	}
1590 	if (error && NFS_ISV4(vp))
1591 		error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1592 	return (error);
1593 }
1594 
1595 /*
1596  * nfs write call
1597  */
1598 int
ncl_writerpc(struct vnode * vp,struct uio * uiop,struct ucred * cred,int * iomode,int * must_commit,int called_from_strategy)1599 ncl_writerpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
1600     int *iomode, int *must_commit, int called_from_strategy)
1601 {
1602 	struct nfsvattr nfsva;
1603 	int error, attrflag, ret;
1604 	struct nfsmount *nmp;
1605 
1606 	nmp = VFSTONFS(vp->v_mount);
1607 	error = EIO;
1608 	attrflag = 0;
1609 	if (NFSHASPNFS(nmp))
1610 		error = nfscl_doiods(vp, uiop, iomode, must_commit,
1611 		    NFSV4OPEN_ACCESSWRITE, 0, cred, uiop->uio_td);
1612 	NFSCL_DEBUG(4, "writerpc: aft doiods=%d\n", error);
1613 	if (error != 0)
1614 		error = nfsrpc_write(vp, uiop, iomode, must_commit, cred,
1615 		    uiop->uio_td, &nfsva, &attrflag, NULL,
1616 		    called_from_strategy);
1617 	if (attrflag) {
1618 		if (VTONFS(vp)->n_flag & ND_NFSV4)
1619 			ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 1,
1620 			    1);
1621 		else
1622 			ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0,
1623 			    1);
1624 		if (ret && !error)
1625 			error = ret;
1626 	}
1627 	if (DOINGASYNC(vp))
1628 		*iomode = NFSWRITE_FILESYNC;
1629 	if (error && NFS_ISV4(vp))
1630 		error = nfscl_maperr(uiop->uio_td, error, (uid_t)0, (gid_t)0);
1631 	return (error);
1632 }
1633 
1634 /*
1635  * nfs mknod rpc
1636  * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1637  * mode set to specify the file type and the size field for rdev.
1638  */
1639 static int
nfs_mknodrpc(struct vnode * dvp,struct vnode ** vpp,struct componentname * cnp,struct vattr * vap)1640 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1641     struct vattr *vap)
1642 {
1643 	struct nfsvattr nfsva, dnfsva;
1644 	struct vnode *newvp = NULL;
1645 	struct nfsnode *np = NULL, *dnp;
1646 	struct nfsfh *nfhp;
1647 	struct vattr vattr;
1648 	int error = 0, attrflag, dattrflag;
1649 	u_int32_t rdev;
1650 
1651 	if (vap->va_type == VCHR || vap->va_type == VBLK)
1652 		rdev = vap->va_rdev;
1653 	else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1654 		rdev = 0xffffffff;
1655 	else
1656 		return (EOPNOTSUPP);
1657 	if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1658 		return (error);
1659 	error = nfsrpc_mknod(dvp, cnp->cn_nameptr, cnp->cn_namelen, vap,
1660 	    rdev, vap->va_type, cnp->cn_cred, cnp->cn_thread, &dnfsva,
1661 	    &nfsva, &nfhp, &attrflag, &dattrflag, NULL);
1662 	if (!error) {
1663 		if (!nfhp)
1664 			(void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1665 			    cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread,
1666 			    &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1667 			    NULL);
1668 		if (nfhp)
1669 			error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1670 			    cnp->cn_thread, &np, NULL, LK_EXCLUSIVE);
1671 	}
1672 	if (dattrflag)
1673 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1674 	if (!error) {
1675 		newvp = NFSTOV(np);
1676 		if (attrflag != 0) {
1677 			error = nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1678 			    0, 1);
1679 			if (error != 0)
1680 				vput(newvp);
1681 		}
1682 	}
1683 	if (!error) {
1684 		*vpp = newvp;
1685 	} else if (NFS_ISV4(dvp)) {
1686 		error = nfscl_maperr(cnp->cn_thread, error, vap->va_uid,
1687 		    vap->va_gid);
1688 	}
1689 	dnp = VTONFS(dvp);
1690 	NFSLOCKNODE(dnp);
1691 	dnp->n_flag |= NMODIFIED;
1692 	if (!dattrflag) {
1693 		dnp->n_attrstamp = 0;
1694 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1695 	}
1696 	NFSUNLOCKNODE(dnp);
1697 	return (error);
1698 }
1699 
1700 /*
1701  * nfs mknod vop
1702  * just call nfs_mknodrpc() to do the work.
1703  */
1704 /* ARGSUSED */
1705 static int
nfs_mknod(struct vop_mknod_args * ap)1706 nfs_mknod(struct vop_mknod_args *ap)
1707 {
1708 	return (nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap));
1709 }
1710 
1711 static struct mtx nfs_cverf_mtx;
1712 MTX_SYSINIT(nfs_cverf_mtx, &nfs_cverf_mtx, "NFS create verifier mutex",
1713     MTX_DEF);
1714 
1715 static nfsquad_t
nfs_get_cverf(void)1716 nfs_get_cverf(void)
1717 {
1718 	static nfsquad_t cverf;
1719 	nfsquad_t ret;
1720 	static int cverf_initialized = 0;
1721 
1722 	mtx_lock(&nfs_cverf_mtx);
1723 	if (cverf_initialized == 0) {
1724 		cverf.lval[0] = arc4random();
1725 		cverf.lval[1] = arc4random();
1726 		cverf_initialized = 1;
1727 	} else
1728 		cverf.qval++;
1729 	ret = cverf;
1730 	mtx_unlock(&nfs_cverf_mtx);
1731 
1732 	return (ret);
1733 }
1734 
1735 /*
1736  * nfs file create call
1737  */
1738 static int
nfs_create(struct vop_create_args * ap)1739 nfs_create(struct vop_create_args *ap)
1740 {
1741 	struct vnode *dvp = ap->a_dvp;
1742 	struct vattr *vap = ap->a_vap;
1743 	struct componentname *cnp = ap->a_cnp;
1744 	struct nfsnode *np = NULL, *dnp;
1745 	struct vnode *newvp = NULL;
1746 	struct nfsmount *nmp;
1747 	struct nfsvattr dnfsva, nfsva;
1748 	struct nfsfh *nfhp;
1749 	nfsquad_t cverf;
1750 	int error = 0, attrflag, dattrflag, fmode = 0;
1751 	struct vattr vattr;
1752 
1753 	/*
1754 	 * Oops, not for me..
1755 	 */
1756 	if (vap->va_type == VSOCK)
1757 		return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1758 
1759 	if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)))
1760 		return (error);
1761 	if (vap->va_vaflags & VA_EXCLUSIVE)
1762 		fmode |= O_EXCL;
1763 	dnp = VTONFS(dvp);
1764 	nmp = VFSTONFS(dvp->v_mount);
1765 again:
1766 	/* For NFSv4, wait until any remove is done. */
1767 	NFSLOCKNODE(dnp);
1768 	while (NFSHASNFSV4(nmp) && (dnp->n_flag & NREMOVEINPROG)) {
1769 		dnp->n_flag |= NREMOVEWANT;
1770 		(void) msleep((caddr_t)dnp, &dnp->n_mtx, PZERO, "nfscrt", 0);
1771 	}
1772 	NFSUNLOCKNODE(dnp);
1773 
1774 	cverf = nfs_get_cverf();
1775 	error = nfsrpc_create(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1776 	    vap, cverf, fmode, cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva,
1777 	    &nfhp, &attrflag, &dattrflag, NULL);
1778 	if (!error) {
1779 		if (nfhp == NULL)
1780 			(void) nfsrpc_lookup(dvp, cnp->cn_nameptr,
1781 			    cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread,
1782 			    &dnfsva, &nfsva, &nfhp, &attrflag, &dattrflag,
1783 			    NULL);
1784 		if (nfhp != NULL)
1785 			error = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp,
1786 			    cnp->cn_thread, &np, NULL, LK_EXCLUSIVE);
1787 	}
1788 	if (dattrflag)
1789 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1790 	if (!error) {
1791 		newvp = NFSTOV(np);
1792 		if (attrflag == 0)
1793 			error = nfsrpc_getattr(newvp, cnp->cn_cred,
1794 			    cnp->cn_thread, &nfsva, NULL);
1795 		if (error == 0)
1796 			error = nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
1797 			    0, 1);
1798 	}
1799 	if (error) {
1800 		if (newvp != NULL) {
1801 			vput(newvp);
1802 			newvp = NULL;
1803 		}
1804 		if (NFS_ISV34(dvp) && (fmode & O_EXCL) &&
1805 		    error == NFSERR_NOTSUPP) {
1806 			fmode &= ~O_EXCL;
1807 			goto again;
1808 		}
1809 	} else if (NFS_ISV34(dvp) && (fmode & O_EXCL)) {
1810 		if (nfscl_checksattr(vap, &nfsva)) {
1811 			error = nfsrpc_setattr(newvp, vap, NULL, cnp->cn_cred,
1812 			    cnp->cn_thread, &nfsva, &attrflag, NULL);
1813 			if (error && (vap->va_uid != (uid_t)VNOVAL ||
1814 			    vap->va_gid != (gid_t)VNOVAL)) {
1815 				/* try again without setting uid/gid */
1816 				vap->va_uid = (uid_t)VNOVAL;
1817 				vap->va_gid = (uid_t)VNOVAL;
1818 				error = nfsrpc_setattr(newvp, vap, NULL,
1819 				    cnp->cn_cred, cnp->cn_thread, &nfsva,
1820 				    &attrflag, NULL);
1821 			}
1822 			if (attrflag)
1823 				(void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
1824 				    NULL, 0, 1);
1825 			if (error != 0)
1826 				vput(newvp);
1827 		}
1828 	}
1829 	if (!error) {
1830 		if ((cnp->cn_flags & MAKEENTRY) && attrflag) {
1831 			if (dvp != newvp)
1832 				cache_enter_time(dvp, newvp, cnp,
1833 				    &nfsva.na_ctime, NULL);
1834 			else
1835 				printf("nfs_create: bogus NFS server returned "
1836 				    "the directory as the new file object\n");
1837 		}
1838 		*ap->a_vpp = newvp;
1839 	} else if (NFS_ISV4(dvp)) {
1840 		error = nfscl_maperr(cnp->cn_thread, error, vap->va_uid,
1841 		    vap->va_gid);
1842 	}
1843 	NFSLOCKNODE(dnp);
1844 	dnp->n_flag |= NMODIFIED;
1845 	if (!dattrflag) {
1846 		dnp->n_attrstamp = 0;
1847 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1848 	}
1849 	NFSUNLOCKNODE(dnp);
1850 	return (error);
1851 }
1852 
1853 /*
1854  * nfs file remove call
1855  * To try and make nfs semantics closer to ufs semantics, a file that has
1856  * other processes using the vnode is renamed instead of removed and then
1857  * removed later on the last close.
1858  * - If v_usecount > 1
1859  *	  If a rename is not already in the works
1860  *	     call nfs_sillyrename() to set it up
1861  *     else
1862  *	  do the remove rpc
1863  */
1864 static int
nfs_remove(struct vop_remove_args * ap)1865 nfs_remove(struct vop_remove_args *ap)
1866 {
1867 	struct vnode *vp = ap->a_vp;
1868 	struct vnode *dvp = ap->a_dvp;
1869 	struct componentname *cnp = ap->a_cnp;
1870 	struct nfsnode *np = VTONFS(vp);
1871 	int error = 0;
1872 	struct vattr vattr;
1873 
1874 	KASSERT((cnp->cn_flags & HASBUF) != 0, ("nfs_remove: no name"));
1875 	KASSERT(vrefcnt(vp) > 0, ("nfs_remove: bad v_usecount"));
1876 	if (vp->v_type == VDIR)
1877 		error = EPERM;
1878 	else if (vrefcnt(vp) == 1 || (np->n_sillyrename &&
1879 	    VOP_GETATTR(vp, &vattr, cnp->cn_cred) == 0 &&
1880 	    vattr.va_nlink > 1)) {
1881 		/*
1882 		 * Purge the name cache so that the chance of a lookup for
1883 		 * the name succeeding while the remove is in progress is
1884 		 * minimized. Without node locking it can still happen, such
1885 		 * that an I/O op returns ESTALE, but since you get this if
1886 		 * another host removes the file..
1887 		 */
1888 		cache_purge(vp);
1889 		/*
1890 		 * throw away biocache buffers, mainly to avoid
1891 		 * unnecessary delayed writes later.
1892 		 */
1893 		error = ncl_vinvalbuf(vp, 0, cnp->cn_thread, 1);
1894 		if (error != EINTR && error != EIO)
1895 			/* Do the rpc */
1896 			error = nfs_removerpc(dvp, vp, cnp->cn_nameptr,
1897 			    cnp->cn_namelen, cnp->cn_cred, cnp->cn_thread);
1898 		/*
1899 		 * Kludge City: If the first reply to the remove rpc is lost..
1900 		 *   the reply to the retransmitted request will be ENOENT
1901 		 *   since the file was in fact removed
1902 		 *   Therefore, we cheat and return success.
1903 		 */
1904 		if (error == ENOENT)
1905 			error = 0;
1906 	} else if (!np->n_sillyrename)
1907 		error = nfs_sillyrename(dvp, vp, cnp);
1908 	NFSLOCKNODE(np);
1909 	np->n_attrstamp = 0;
1910 	NFSUNLOCKNODE(np);
1911 	KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
1912 	return (error);
1913 }
1914 
1915 /*
1916  * nfs file remove rpc called from nfs_inactive
1917  */
1918 int
ncl_removeit(struct sillyrename * sp,struct vnode * vp)1919 ncl_removeit(struct sillyrename *sp, struct vnode *vp)
1920 {
1921 	/*
1922 	 * Make sure that the directory vnode is still valid.
1923 	 * XXX we should lock sp->s_dvp here.
1924 	 */
1925 	if (sp->s_dvp->v_type == VBAD)
1926 		return (0);
1927 	return (nfs_removerpc(sp->s_dvp, vp, sp->s_name, sp->s_namlen,
1928 	    sp->s_cred, NULL));
1929 }
1930 
1931 /*
1932  * Nfs remove rpc, called from nfs_remove() and ncl_removeit().
1933  */
1934 static int
nfs_removerpc(struct vnode * dvp,struct vnode * vp,char * name,int namelen,struct ucred * cred,struct thread * td)1935 nfs_removerpc(struct vnode *dvp, struct vnode *vp, char *name,
1936     int namelen, struct ucred *cred, struct thread *td)
1937 {
1938 	struct nfsvattr dnfsva;
1939 	struct nfsnode *dnp = VTONFS(dvp);
1940 	int error = 0, dattrflag;
1941 
1942 	NFSLOCKNODE(dnp);
1943 	dnp->n_flag |= NREMOVEINPROG;
1944 	NFSUNLOCKNODE(dnp);
1945 	error = nfsrpc_remove(dvp, name, namelen, vp, cred, td, &dnfsva,
1946 	    &dattrflag, NULL);
1947 	NFSLOCKNODE(dnp);
1948 	if ((dnp->n_flag & NREMOVEWANT)) {
1949 		dnp->n_flag &= ~(NREMOVEWANT | NREMOVEINPROG);
1950 		NFSUNLOCKNODE(dnp);
1951 		wakeup((caddr_t)dnp);
1952 	} else {
1953 		dnp->n_flag &= ~NREMOVEINPROG;
1954 		NFSUNLOCKNODE(dnp);
1955 	}
1956 	if (dattrflag)
1957 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
1958 	NFSLOCKNODE(dnp);
1959 	dnp->n_flag |= NMODIFIED;
1960 	if (!dattrflag) {
1961 		dnp->n_attrstamp = 0;
1962 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
1963 	}
1964 	NFSUNLOCKNODE(dnp);
1965 	if (error && NFS_ISV4(dvp))
1966 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
1967 	return (error);
1968 }
1969 
1970 /*
1971  * nfs file rename call
1972  */
1973 static int
nfs_rename(struct vop_rename_args * ap)1974 nfs_rename(struct vop_rename_args *ap)
1975 {
1976 	struct vnode *fvp = ap->a_fvp;
1977 	struct vnode *tvp = ap->a_tvp;
1978 	struct vnode *fdvp = ap->a_fdvp;
1979 	struct vnode *tdvp = ap->a_tdvp;
1980 	struct componentname *tcnp = ap->a_tcnp;
1981 	struct componentname *fcnp = ap->a_fcnp;
1982 	struct nfsnode *fnp = VTONFS(ap->a_fvp);
1983 	struct nfsnode *tdnp = VTONFS(ap->a_tdvp);
1984 	struct nfsv4node *newv4 = NULL;
1985 	int error;
1986 
1987 	KASSERT((tcnp->cn_flags & HASBUF) != 0 &&
1988 	    (fcnp->cn_flags & HASBUF) != 0, ("nfs_rename: no name"));
1989 	/* Check for cross-device rename */
1990 	if ((fvp->v_mount != tdvp->v_mount) ||
1991 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
1992 		error = EXDEV;
1993 		goto out;
1994 	}
1995 
1996 	if (fvp == tvp) {
1997 		printf("nfs_rename: fvp == tvp (can't happen)\n");
1998 		error = 0;
1999 		goto out;
2000 	}
2001 	if ((error = NFSVOPLOCK(fvp, LK_EXCLUSIVE)) != 0)
2002 		goto out;
2003 
2004 	/*
2005 	 * We have to flush B_DELWRI data prior to renaming
2006 	 * the file.  If we don't, the delayed-write buffers
2007 	 * can be flushed out later after the file has gone stale
2008 	 * under NFSV3.  NFSV2 does not have this problem because
2009 	 * ( as far as I can tell ) it flushes dirty buffers more
2010 	 * often.
2011 	 *
2012 	 * Skip the rename operation if the fsync fails, this can happen
2013 	 * due to the server's volume being full, when we pushed out data
2014 	 * that was written back to our cache earlier. Not checking for
2015 	 * this condition can result in potential (silent) data loss.
2016 	 */
2017 	error = VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_thread);
2018 	NFSVOPUNLOCK(fvp);
2019 	if (!error && tvp)
2020 		error = VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_thread);
2021 	if (error)
2022 		goto out;
2023 
2024 	/*
2025 	 * If the tvp exists and is in use, sillyrename it before doing the
2026 	 * rename of the new file over it.
2027 	 * XXX Can't sillyrename a directory.
2028 	 */
2029 	if (tvp && vrefcnt(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
2030 		tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
2031 		vput(tvp);
2032 		tvp = NULL;
2033 	}
2034 
2035 	error = nfs_renamerpc(fdvp, fvp, fcnp->cn_nameptr, fcnp->cn_namelen,
2036 	    tdvp, tvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
2037 	    tcnp->cn_thread);
2038 
2039 	if (error == 0 && NFS_ISV4(tdvp)) {
2040 		/*
2041 		 * For NFSv4, check to see if it is the same name and
2042 		 * replace the name, if it is different.
2043 		 */
2044 		newv4 = malloc(
2045 		    sizeof (struct nfsv4node) +
2046 		    tdnp->n_fhp->nfh_len + tcnp->cn_namelen - 1,
2047 		    M_NFSV4NODE, M_WAITOK);
2048 		NFSLOCKNODE(tdnp);
2049 		NFSLOCKNODE(fnp);
2050 		if (fnp->n_v4 != NULL && fvp->v_type == VREG &&
2051 		    (fnp->n_v4->n4_namelen != tcnp->cn_namelen ||
2052 		      NFSBCMP(tcnp->cn_nameptr, NFS4NODENAME(fnp->n_v4),
2053 		      tcnp->cn_namelen) ||
2054 		      tdnp->n_fhp->nfh_len != fnp->n_v4->n4_fhlen ||
2055 		      NFSBCMP(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
2056 			tdnp->n_fhp->nfh_len))) {
2057 #ifdef notdef
2058 { char nnn[100]; int nnnl;
2059 nnnl = (tcnp->cn_namelen < 100) ? tcnp->cn_namelen : 99;
2060 bcopy(tcnp->cn_nameptr, nnn, nnnl);
2061 nnn[nnnl] = '\0';
2062 printf("ren replace=%s\n",nnn);
2063 }
2064 #endif
2065 			free(fnp->n_v4, M_NFSV4NODE);
2066 			fnp->n_v4 = newv4;
2067 			newv4 = NULL;
2068 			fnp->n_v4->n4_fhlen = tdnp->n_fhp->nfh_len;
2069 			fnp->n_v4->n4_namelen = tcnp->cn_namelen;
2070 			NFSBCOPY(tdnp->n_fhp->nfh_fh, fnp->n_v4->n4_data,
2071 			    tdnp->n_fhp->nfh_len);
2072 			NFSBCOPY(tcnp->cn_nameptr,
2073 			    NFS4NODENAME(fnp->n_v4), tcnp->cn_namelen);
2074 		}
2075 		NFSUNLOCKNODE(tdnp);
2076 		NFSUNLOCKNODE(fnp);
2077 		if (newv4 != NULL)
2078 			free(newv4, M_NFSV4NODE);
2079 	}
2080 
2081 	if (fvp->v_type == VDIR) {
2082 		if (tvp != NULL && tvp->v_type == VDIR)
2083 			cache_purge(tdvp);
2084 		cache_purge(fdvp);
2085 	}
2086 
2087 out:
2088 	if (tdvp == tvp)
2089 		vrele(tdvp);
2090 	else
2091 		vput(tdvp);
2092 	if (tvp)
2093 		vput(tvp);
2094 	vrele(fdvp);
2095 	vrele(fvp);
2096 	/*
2097 	 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
2098 	 */
2099 	if (error == ENOENT)
2100 		error = 0;
2101 	return (error);
2102 }
2103 
2104 /*
2105  * nfs file rename rpc called from nfs_remove() above
2106  */
2107 static int
nfs_renameit(struct vnode * sdvp,struct vnode * svp,struct componentname * scnp,struct sillyrename * sp)2108 nfs_renameit(struct vnode *sdvp, struct vnode *svp, struct componentname *scnp,
2109     struct sillyrename *sp)
2110 {
2111 
2112 	return (nfs_renamerpc(sdvp, svp, scnp->cn_nameptr, scnp->cn_namelen,
2113 	    sdvp, NULL, sp->s_name, sp->s_namlen, scnp->cn_cred,
2114 	    scnp->cn_thread));
2115 }
2116 
2117 /*
2118  * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
2119  */
2120 static int
nfs_renamerpc(struct vnode * fdvp,struct vnode * fvp,char * fnameptr,int fnamelen,struct vnode * tdvp,struct vnode * tvp,char * tnameptr,int tnamelen,struct ucred * cred,struct thread * td)2121 nfs_renamerpc(struct vnode *fdvp, struct vnode *fvp, char *fnameptr,
2122     int fnamelen, struct vnode *tdvp, struct vnode *tvp, char *tnameptr,
2123     int tnamelen, struct ucred *cred, struct thread *td)
2124 {
2125 	struct nfsvattr fnfsva, tnfsva;
2126 	struct nfsnode *fdnp = VTONFS(fdvp);
2127 	struct nfsnode *tdnp = VTONFS(tdvp);
2128 	int error = 0, fattrflag, tattrflag;
2129 
2130 	error = nfsrpc_rename(fdvp, fvp, fnameptr, fnamelen, tdvp, tvp,
2131 	    tnameptr, tnamelen, cred, td, &fnfsva, &tnfsva, &fattrflag,
2132 	    &tattrflag, NULL, NULL);
2133 	NFSLOCKNODE(fdnp);
2134 	fdnp->n_flag |= NMODIFIED;
2135 	if (fattrflag != 0) {
2136 		NFSUNLOCKNODE(fdnp);
2137 		(void) nfscl_loadattrcache(&fdvp, &fnfsva, NULL, NULL, 0, 1);
2138 	} else {
2139 		fdnp->n_attrstamp = 0;
2140 		NFSUNLOCKNODE(fdnp);
2141 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(fdvp);
2142 	}
2143 	NFSLOCKNODE(tdnp);
2144 	tdnp->n_flag |= NMODIFIED;
2145 	if (tattrflag != 0) {
2146 		NFSUNLOCKNODE(tdnp);
2147 		(void) nfscl_loadattrcache(&tdvp, &tnfsva, NULL, NULL, 0, 1);
2148 	} else {
2149 		tdnp->n_attrstamp = 0;
2150 		NFSUNLOCKNODE(tdnp);
2151 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
2152 	}
2153 	if (error && NFS_ISV4(fdvp))
2154 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2155 	return (error);
2156 }
2157 
2158 /*
2159  * nfs hard link create call
2160  */
2161 static int
nfs_link(struct vop_link_args * ap)2162 nfs_link(struct vop_link_args *ap)
2163 {
2164 	struct vnode *vp = ap->a_vp;
2165 	struct vnode *tdvp = ap->a_tdvp;
2166 	struct componentname *cnp = ap->a_cnp;
2167 	struct nfsnode *np, *tdnp;
2168 	struct nfsvattr nfsva, dnfsva;
2169 	int error = 0, attrflag, dattrflag;
2170 
2171 	/*
2172 	 * Push all writes to the server, so that the attribute cache
2173 	 * doesn't get "out of sync" with the server.
2174 	 * XXX There should be a better way!
2175 	 */
2176 	VOP_FSYNC(vp, MNT_WAIT, cnp->cn_thread);
2177 
2178 	error = nfsrpc_link(tdvp, vp, cnp->cn_nameptr, cnp->cn_namelen,
2179 	    cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva, &attrflag,
2180 	    &dattrflag, NULL);
2181 	tdnp = VTONFS(tdvp);
2182 	NFSLOCKNODE(tdnp);
2183 	tdnp->n_flag |= NMODIFIED;
2184 	if (dattrflag != 0) {
2185 		NFSUNLOCKNODE(tdnp);
2186 		(void) nfscl_loadattrcache(&tdvp, &dnfsva, NULL, NULL, 0, 1);
2187 	} else {
2188 		tdnp->n_attrstamp = 0;
2189 		NFSUNLOCKNODE(tdnp);
2190 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(tdvp);
2191 	}
2192 	if (attrflag)
2193 		(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
2194 	else {
2195 		np = VTONFS(vp);
2196 		NFSLOCKNODE(np);
2197 		np->n_attrstamp = 0;
2198 		NFSUNLOCKNODE(np);
2199 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
2200 	}
2201 	/*
2202 	 * If negative lookup caching is enabled, I might as well
2203 	 * add an entry for this node. Not necessary for correctness,
2204 	 * but if negative caching is enabled, then the system
2205 	 * must care about lookup caching hit rate, so...
2206 	 */
2207 	if (VFSTONFS(vp->v_mount)->nm_negnametimeo != 0 &&
2208 	    (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
2209 		if (tdvp != vp)
2210 			cache_enter_time(tdvp, vp, cnp, &nfsva.na_ctime, NULL);
2211 		else
2212 			printf("nfs_link: bogus NFS server returned "
2213 			    "the directory as the new link\n");
2214 	}
2215 	if (error && NFS_ISV4(vp))
2216 		error = nfscl_maperr(cnp->cn_thread, error, (uid_t)0,
2217 		    (gid_t)0);
2218 	return (error);
2219 }
2220 
2221 /*
2222  * nfs symbolic link create call
2223  */
2224 static int
nfs_symlink(struct vop_symlink_args * ap)2225 nfs_symlink(struct vop_symlink_args *ap)
2226 {
2227 	struct vnode *dvp = ap->a_dvp;
2228 	struct vattr *vap = ap->a_vap;
2229 	struct componentname *cnp = ap->a_cnp;
2230 	struct nfsvattr nfsva, dnfsva;
2231 	struct nfsfh *nfhp;
2232 	struct nfsnode *np = NULL, *dnp;
2233 	struct vnode *newvp = NULL;
2234 	int error = 0, attrflag, dattrflag, ret;
2235 
2236 	vap->va_type = VLNK;
2237 	error = nfsrpc_symlink(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2238 	    ap->a_target, vap, cnp->cn_cred, cnp->cn_thread, &dnfsva,
2239 	    &nfsva, &nfhp, &attrflag, &dattrflag, NULL);
2240 	if (nfhp) {
2241 		ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, cnp->cn_thread,
2242 		    &np, NULL, LK_EXCLUSIVE);
2243 		if (!ret)
2244 			newvp = NFSTOV(np);
2245 		else if (!error)
2246 			error = ret;
2247 	}
2248 	if (newvp != NULL) {
2249 		if (attrflag)
2250 			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
2251 			    0, 1);
2252 	} else if (!error) {
2253 		/*
2254 		 * If we do not have an error and we could not extract the
2255 		 * newvp from the response due to the request being NFSv2, we
2256 		 * have to do a lookup in order to obtain a newvp to return.
2257 		 */
2258 		error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2259 		    cnp->cn_cred, cnp->cn_thread, &np);
2260 		if (!error)
2261 			newvp = NFSTOV(np);
2262 	}
2263 	if (error) {
2264 		if (newvp)
2265 			vput(newvp);
2266 		if (NFS_ISV4(dvp))
2267 			error = nfscl_maperr(cnp->cn_thread, error,
2268 			    vap->va_uid, vap->va_gid);
2269 	} else {
2270 		*ap->a_vpp = newvp;
2271 	}
2272 
2273 	dnp = VTONFS(dvp);
2274 	NFSLOCKNODE(dnp);
2275 	dnp->n_flag |= NMODIFIED;
2276 	if (dattrflag != 0) {
2277 		NFSUNLOCKNODE(dnp);
2278 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2279 	} else {
2280 		dnp->n_attrstamp = 0;
2281 		NFSUNLOCKNODE(dnp);
2282 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2283 	}
2284 	/*
2285 	 * If negative lookup caching is enabled, I might as well
2286 	 * add an entry for this node. Not necessary for correctness,
2287 	 * but if negative caching is enabled, then the system
2288 	 * must care about lookup caching hit rate, so...
2289 	 */
2290 	if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2291 	    (cnp->cn_flags & MAKEENTRY) && attrflag != 0 && error == 0) {
2292 		if (dvp != newvp)
2293 			cache_enter_time(dvp, newvp, cnp, &nfsva.na_ctime,
2294 			    NULL);
2295 		else
2296 			printf("nfs_symlink: bogus NFS server returned "
2297 			    "the directory as the new file object\n");
2298 	}
2299 	return (error);
2300 }
2301 
2302 /*
2303  * nfs make dir call
2304  */
2305 static int
nfs_mkdir(struct vop_mkdir_args * ap)2306 nfs_mkdir(struct vop_mkdir_args *ap)
2307 {
2308 	struct vnode *dvp = ap->a_dvp;
2309 	struct vattr *vap = ap->a_vap;
2310 	struct componentname *cnp = ap->a_cnp;
2311 	struct nfsnode *np = NULL, *dnp;
2312 	struct vnode *newvp = NULL;
2313 	struct vattr vattr;
2314 	struct nfsfh *nfhp;
2315 	struct nfsvattr nfsva, dnfsva;
2316 	int error = 0, attrflag, dattrflag, ret;
2317 
2318 	if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_cred)) != 0)
2319 		return (error);
2320 	vap->va_type = VDIR;
2321 	error = nfsrpc_mkdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2322 	    vap, cnp->cn_cred, cnp->cn_thread, &dnfsva, &nfsva, &nfhp,
2323 	    &attrflag, &dattrflag, NULL);
2324 	dnp = VTONFS(dvp);
2325 	NFSLOCKNODE(dnp);
2326 	dnp->n_flag |= NMODIFIED;
2327 	if (dattrflag != 0) {
2328 		NFSUNLOCKNODE(dnp);
2329 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2330 	} else {
2331 		dnp->n_attrstamp = 0;
2332 		NFSUNLOCKNODE(dnp);
2333 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2334 	}
2335 	if (nfhp) {
2336 		ret = nfscl_nget(dvp->v_mount, dvp, nfhp, cnp, cnp->cn_thread,
2337 		    &np, NULL, LK_EXCLUSIVE);
2338 		if (!ret) {
2339 			newvp = NFSTOV(np);
2340 			if (attrflag)
2341 			   (void) nfscl_loadattrcache(&newvp, &nfsva, NULL,
2342 				NULL, 0, 1);
2343 		} else if (!error)
2344 			error = ret;
2345 	}
2346 	if (!error && newvp == NULL) {
2347 		error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2348 		    cnp->cn_cred, cnp->cn_thread, &np);
2349 		if (!error) {
2350 			newvp = NFSTOV(np);
2351 			if (newvp->v_type != VDIR)
2352 				error = EEXIST;
2353 		}
2354 	}
2355 	if (error) {
2356 		if (newvp)
2357 			vput(newvp);
2358 		if (NFS_ISV4(dvp))
2359 			error = nfscl_maperr(cnp->cn_thread, error,
2360 			    vap->va_uid, vap->va_gid);
2361 	} else {
2362 		/*
2363 		 * If negative lookup caching is enabled, I might as well
2364 		 * add an entry for this node. Not necessary for correctness,
2365 		 * but if negative caching is enabled, then the system
2366 		 * must care about lookup caching hit rate, so...
2367 		 */
2368 		if (VFSTONFS(dvp->v_mount)->nm_negnametimeo != 0 &&
2369 		    (cnp->cn_flags & MAKEENTRY) &&
2370 		    attrflag != 0 && dattrflag != 0) {
2371 			if (dvp != newvp)
2372 				cache_enter_time(dvp, newvp, cnp,
2373 				    &nfsva.na_ctime, &dnfsva.na_ctime);
2374 			else
2375 				printf("nfs_mkdir: bogus NFS server returned "
2376 				    "the directory that the directory was "
2377 				    "created in as the new file object\n");
2378 		}
2379 		*ap->a_vpp = newvp;
2380 	}
2381 	return (error);
2382 }
2383 
2384 /*
2385  * nfs remove directory call
2386  */
2387 static int
nfs_rmdir(struct vop_rmdir_args * ap)2388 nfs_rmdir(struct vop_rmdir_args *ap)
2389 {
2390 	struct vnode *vp = ap->a_vp;
2391 	struct vnode *dvp = ap->a_dvp;
2392 	struct componentname *cnp = ap->a_cnp;
2393 	struct nfsnode *dnp;
2394 	struct nfsvattr dnfsva;
2395 	int error, dattrflag;
2396 
2397 	if (dvp == vp)
2398 		return (EINVAL);
2399 	error = nfsrpc_rmdir(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2400 	    cnp->cn_cred, cnp->cn_thread, &dnfsva, &dattrflag, NULL);
2401 	dnp = VTONFS(dvp);
2402 	NFSLOCKNODE(dnp);
2403 	dnp->n_flag |= NMODIFIED;
2404 	if (dattrflag != 0) {
2405 		NFSUNLOCKNODE(dnp);
2406 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2407 	} else {
2408 		dnp->n_attrstamp = 0;
2409 		NFSUNLOCKNODE(dnp);
2410 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(dvp);
2411 	}
2412 
2413 	cache_purge(dvp);
2414 	cache_purge(vp);
2415 	if (error && NFS_ISV4(dvp))
2416 		error = nfscl_maperr(cnp->cn_thread, error, (uid_t)0,
2417 		    (gid_t)0);
2418 	/*
2419 	 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2420 	 */
2421 	if (error == ENOENT)
2422 		error = 0;
2423 	return (error);
2424 }
2425 
2426 /*
2427  * nfs readdir call
2428  */
2429 static int
nfs_readdir(struct vop_readdir_args * ap)2430 nfs_readdir(struct vop_readdir_args *ap)
2431 {
2432 	struct vnode *vp = ap->a_vp;
2433 	struct nfsnode *np = VTONFS(vp);
2434 	struct uio *uio = ap->a_uio;
2435 	ssize_t tresid, left;
2436 	int error = 0;
2437 	struct vattr vattr;
2438 
2439 	if (ap->a_eofflag != NULL)
2440 		*ap->a_eofflag = 0;
2441 	if (vp->v_type != VDIR)
2442 		return(EPERM);
2443 
2444 	/*
2445 	 * First, check for hit on the EOF offset cache
2446 	 */
2447 	NFSLOCKNODE(np);
2448 	if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2449 	    (np->n_flag & NMODIFIED) == 0) {
2450 		NFSUNLOCKNODE(np);
2451 		if (VOP_GETATTR(vp, &vattr, ap->a_cred) == 0) {
2452 			NFSLOCKNODE(np);
2453 			if ((NFS_ISV4(vp) && np->n_change == vattr.va_filerev) ||
2454 			    !NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime)) {
2455 				NFSUNLOCKNODE(np);
2456 				NFSINCRGLOBAL(nfsstatsv1.direofcache_hits);
2457 				if (ap->a_eofflag != NULL)
2458 					*ap->a_eofflag = 1;
2459 				return (0);
2460 			} else
2461 				NFSUNLOCKNODE(np);
2462 		}
2463 	} else
2464 		NFSUNLOCKNODE(np);
2465 
2466 	/*
2467 	 * NFS always guarantees that directory entries don't straddle
2468 	 * DIRBLKSIZ boundaries.  As such, we need to limit the size
2469 	 * to an exact multiple of DIRBLKSIZ, to avoid copying a partial
2470 	 * directory entry.
2471 	 */
2472 	left = uio->uio_resid % DIRBLKSIZ;
2473 	if (left == uio->uio_resid)
2474 		return (EINVAL);
2475 	uio->uio_resid -= left;
2476 
2477 	/*
2478 	 * Call ncl_bioread() to do the real work.
2479 	 */
2480 	tresid = uio->uio_resid;
2481 	error = ncl_bioread(vp, uio, 0, ap->a_cred);
2482 
2483 	if (!error && uio->uio_resid == tresid) {
2484 		NFSINCRGLOBAL(nfsstatsv1.direofcache_misses);
2485 		if (ap->a_eofflag != NULL)
2486 			*ap->a_eofflag = 1;
2487 	}
2488 
2489 	/* Add the partial DIRBLKSIZ (left) back in. */
2490 	uio->uio_resid += left;
2491 	return (error);
2492 }
2493 
2494 /*
2495  * Readdir rpc call.
2496  * Called from below the buffer cache by ncl_doio().
2497  */
2498 int
ncl_readdirrpc(struct vnode * vp,struct uio * uiop,struct ucred * cred,struct thread * td)2499 ncl_readdirrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2500     struct thread *td)
2501 {
2502 	struct nfsvattr nfsva;
2503 	nfsuint64 *cookiep, cookie;
2504 	struct nfsnode *dnp = VTONFS(vp);
2505 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2506 	int error = 0, eof, attrflag;
2507 
2508 	KASSERT(uiop->uio_iovcnt == 1 &&
2509 	    (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2510 	    (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2511 	    ("nfs readdirrpc bad uio"));
2512 
2513 	/*
2514 	 * If there is no cookie, assume directory was stale.
2515 	 */
2516 	ncl_dircookie_lock(dnp);
2517 	NFSUNLOCKNODE(dnp);
2518 	cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2519 	if (cookiep) {
2520 		cookie = *cookiep;
2521 		ncl_dircookie_unlock(dnp);
2522 	} else {
2523 		ncl_dircookie_unlock(dnp);
2524 		return (NFSERR_BAD_COOKIE);
2525 	}
2526 
2527 	if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2528 		(void)ncl_fsinfo(nmp, vp, cred, td);
2529 
2530 	error = nfsrpc_readdir(vp, uiop, &cookie, cred, td, &nfsva,
2531 	    &attrflag, &eof, NULL);
2532 	if (attrflag)
2533 		(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
2534 
2535 	if (!error) {
2536 		/*
2537 		 * We are now either at the end of the directory or have filled
2538 		 * the block.
2539 		 */
2540 		if (eof) {
2541 			NFSLOCKNODE(dnp);
2542 			dnp->n_direofoffset = uiop->uio_offset;
2543 			NFSUNLOCKNODE(dnp);
2544 		} else {
2545 			if (uiop->uio_resid > 0)
2546 				printf("EEK! readdirrpc resid > 0\n");
2547 			ncl_dircookie_lock(dnp);
2548 			NFSUNLOCKNODE(dnp);
2549 			cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2550 			*cookiep = cookie;
2551 			ncl_dircookie_unlock(dnp);
2552 		}
2553 	} else if (NFS_ISV4(vp)) {
2554 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2555 	}
2556 	return (error);
2557 }
2558 
2559 /*
2560  * NFS V3 readdir plus RPC. Used in place of ncl_readdirrpc().
2561  */
2562 int
ncl_readdirplusrpc(struct vnode * vp,struct uio * uiop,struct ucred * cred,struct thread * td)2563 ncl_readdirplusrpc(struct vnode *vp, struct uio *uiop, struct ucred *cred,
2564     struct thread *td)
2565 {
2566 	struct nfsvattr nfsva;
2567 	nfsuint64 *cookiep, cookie;
2568 	struct nfsnode *dnp = VTONFS(vp);
2569 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2570 	int error = 0, attrflag, eof;
2571 
2572 	KASSERT(uiop->uio_iovcnt == 1 &&
2573 	    (uiop->uio_offset & (DIRBLKSIZ - 1)) == 0 &&
2574 	    (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0,
2575 	    ("nfs readdirplusrpc bad uio"));
2576 
2577 	/*
2578 	 * If there is no cookie, assume directory was stale.
2579 	 */
2580 	ncl_dircookie_lock(dnp);
2581 	NFSUNLOCKNODE(dnp);
2582 	cookiep = ncl_getcookie(dnp, uiop->uio_offset, 0);
2583 	if (cookiep) {
2584 		cookie = *cookiep;
2585 		ncl_dircookie_unlock(dnp);
2586 	} else {
2587 		ncl_dircookie_unlock(dnp);
2588 		return (NFSERR_BAD_COOKIE);
2589 	}
2590 
2591 	if (NFSHASNFSV3(nmp) && !NFSHASGOTFSINFO(nmp))
2592 		(void)ncl_fsinfo(nmp, vp, cred, td);
2593 	error = nfsrpc_readdirplus(vp, uiop, &cookie, cred, td, &nfsva,
2594 	    &attrflag, &eof, NULL);
2595 	if (attrflag)
2596 		(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
2597 
2598 	if (!error) {
2599 		/*
2600 		 * We are now either at end of the directory or have filled the
2601 		 * the block.
2602 		 */
2603 		if (eof) {
2604 			NFSLOCKNODE(dnp);
2605 			dnp->n_direofoffset = uiop->uio_offset;
2606 			NFSUNLOCKNODE(dnp);
2607 		} else {
2608 			if (uiop->uio_resid > 0)
2609 				printf("EEK! readdirplusrpc resid > 0\n");
2610 			ncl_dircookie_lock(dnp);
2611 			NFSUNLOCKNODE(dnp);
2612 			cookiep = ncl_getcookie(dnp, uiop->uio_offset, 1);
2613 			*cookiep = cookie;
2614 			ncl_dircookie_unlock(dnp);
2615 		}
2616 	} else if (NFS_ISV4(vp)) {
2617 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2618 	}
2619 	return (error);
2620 }
2621 
2622 /*
2623  * Silly rename. To make the NFS filesystem that is stateless look a little
2624  * more like the "ufs" a remove of an active vnode is translated to a rename
2625  * to a funny looking filename that is removed by nfs_inactive on the
2626  * nfsnode. There is the potential for another process on a different client
2627  * to create the same funny name between the nfs_lookitup() fails and the
2628  * nfs_rename() completes, but...
2629  */
2630 static int
nfs_sillyrename(struct vnode * dvp,struct vnode * vp,struct componentname * cnp)2631 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2632 {
2633 	struct sillyrename *sp;
2634 	struct nfsnode *np;
2635 	int error;
2636 	short pid;
2637 	unsigned int lticks;
2638 
2639 	cache_purge(dvp);
2640 	np = VTONFS(vp);
2641 	KASSERT(vp->v_type != VDIR, ("nfs: sillyrename dir"));
2642 	sp = malloc(sizeof (struct sillyrename),
2643 	    M_NEWNFSREQ, M_WAITOK);
2644 	sp->s_cred = crhold(cnp->cn_cred);
2645 	sp->s_dvp = dvp;
2646 	VREF(dvp);
2647 
2648 	/*
2649 	 * Fudge together a funny name.
2650 	 * Changing the format of the funny name to accommodate more
2651 	 * sillynames per directory.
2652 	 * The name is now changed to .nfs.<ticks>.<pid>.4, where ticks is
2653 	 * CPU ticks since boot.
2654 	 */
2655 	pid = cnp->cn_thread->td_proc->p_pid;
2656 	lticks = (unsigned int)ticks;
2657 	for ( ; ; ) {
2658 		sp->s_namlen = sprintf(sp->s_name,
2659 				       ".nfs.%08x.%04x4.4", lticks,
2660 				       pid);
2661 		if (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2662 				 cnp->cn_thread, NULL))
2663 			break;
2664 		lticks++;
2665 	}
2666 	error = nfs_renameit(dvp, vp, cnp, sp);
2667 	if (error)
2668 		goto bad;
2669 	error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2670 		cnp->cn_thread, &np);
2671 	np->n_sillyrename = sp;
2672 	return (0);
2673 bad:
2674 	vrele(sp->s_dvp);
2675 	crfree(sp->s_cred);
2676 	free(sp, M_NEWNFSREQ);
2677 	return (error);
2678 }
2679 
2680 /*
2681  * Look up a file name and optionally either update the file handle or
2682  * allocate an nfsnode, depending on the value of npp.
2683  * npp == NULL	--> just do the lookup
2684  * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2685  *			handled too
2686  * *npp != NULL --> update the file handle in the vnode
2687  */
2688 static int
nfs_lookitup(struct vnode * dvp,char * name,int len,struct ucred * cred,struct thread * td,struct nfsnode ** npp)2689 nfs_lookitup(struct vnode *dvp, char *name, int len, struct ucred *cred,
2690     struct thread *td, struct nfsnode **npp)
2691 {
2692 	struct vnode *newvp = NULL, *vp;
2693 	struct nfsnode *np, *dnp = VTONFS(dvp);
2694 	struct nfsfh *nfhp, *onfhp;
2695 	struct nfsvattr nfsva, dnfsva;
2696 	struct componentname cn;
2697 	int error = 0, attrflag, dattrflag;
2698 	u_int hash;
2699 	struct timespec ts;
2700 
2701 	nanouptime(&ts);
2702 	error = nfsrpc_lookup(dvp, name, len, cred, td, &dnfsva, &nfsva,
2703 	    &nfhp, &attrflag, &dattrflag, NULL);
2704 	if (dattrflag)
2705 		(void) nfscl_loadattrcache(&dvp, &dnfsva, NULL, NULL, 0, 1);
2706 	if (npp && !error) {
2707 		if (*npp != NULL) {
2708 		    np = *npp;
2709 		    vp = NFSTOV(np);
2710 		    /*
2711 		     * For NFSv4, check to see if it is the same name and
2712 		     * replace the name, if it is different.
2713 		     */
2714 		    if (np->n_v4 != NULL && nfsva.na_type == VREG &&
2715 			(np->n_v4->n4_namelen != len ||
2716 			 NFSBCMP(name, NFS4NODENAME(np->n_v4), len) ||
2717 			 dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen ||
2718 			 NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2719 			 dnp->n_fhp->nfh_len))) {
2720 #ifdef notdef
2721 { char nnn[100]; int nnnl;
2722 nnnl = (len < 100) ? len : 99;
2723 bcopy(name, nnn, nnnl);
2724 nnn[nnnl] = '\0';
2725 printf("replace=%s\n",nnn);
2726 }
2727 #endif
2728 			    free(np->n_v4, M_NFSV4NODE);
2729 			    np->n_v4 = malloc(
2730 				sizeof (struct nfsv4node) +
2731 				dnp->n_fhp->nfh_len + len - 1,
2732 				M_NFSV4NODE, M_WAITOK);
2733 			    np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
2734 			    np->n_v4->n4_namelen = len;
2735 			    NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
2736 				dnp->n_fhp->nfh_len);
2737 			    NFSBCOPY(name, NFS4NODENAME(np->n_v4), len);
2738 		    }
2739 		    hash = fnv_32_buf(nfhp->nfh_fh, nfhp->nfh_len,
2740 			FNV1_32_INIT);
2741 		    onfhp = np->n_fhp;
2742 		    /*
2743 		     * Rehash node for new file handle.
2744 		     */
2745 		    vfs_hash_rehash(vp, hash);
2746 		    np->n_fhp = nfhp;
2747 		    if (onfhp != NULL)
2748 			free(onfhp, M_NFSFH);
2749 		    newvp = NFSTOV(np);
2750 		} else if (NFS_CMPFH(dnp, nfhp->nfh_fh, nfhp->nfh_len)) {
2751 		    free(nfhp, M_NFSFH);
2752 		    VREF(dvp);
2753 		    newvp = dvp;
2754 		} else {
2755 		    cn.cn_nameptr = name;
2756 		    cn.cn_namelen = len;
2757 		    error = nfscl_nget(dvp->v_mount, dvp, nfhp, &cn, td,
2758 			&np, NULL, LK_EXCLUSIVE);
2759 		    if (error)
2760 			return (error);
2761 		    newvp = NFSTOV(np);
2762 		    /*
2763 		     * If n_localmodtime >= time before RPC, then
2764 		     * a file modification operation, such as
2765 		     * VOP_SETATTR() of size, has occurred while
2766 		     * the Lookup RPC and acquisition of the vnode
2767 		     * happened.  As such, the attributes might
2768 		     * be stale, with possibly an incorrect size.
2769 		     */
2770 		    NFSLOCKNODE(np);
2771 		    if (timespecisset(&np->n_localmodtime) &&
2772 			timespeccmp(&np->n_localmodtime, &ts, >=)) {
2773 			NFSCL_DEBUG(4, "nfs_lookitup: localmod "
2774 			    "stale attributes\n");
2775 			attrflag = 0;
2776 		    }
2777 		    NFSUNLOCKNODE(np);
2778 		}
2779 		if (!attrflag && *npp == NULL) {
2780 			if (newvp == dvp)
2781 				vrele(newvp);
2782 			else
2783 				vput(newvp);
2784 			return (ENOENT);
2785 		}
2786 		if (attrflag)
2787 			(void) nfscl_loadattrcache(&newvp, &nfsva, NULL, NULL,
2788 			    0, 1);
2789 	}
2790 	if (npp && *npp == NULL) {
2791 		if (error) {
2792 			if (newvp) {
2793 				if (newvp == dvp)
2794 					vrele(newvp);
2795 				else
2796 					vput(newvp);
2797 			}
2798 		} else
2799 			*npp = np;
2800 	}
2801 	if (error && NFS_ISV4(dvp))
2802 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2803 	return (error);
2804 }
2805 
2806 /*
2807  * Nfs Version 3 and 4 commit rpc
2808  */
2809 int
ncl_commit(struct vnode * vp,u_quad_t offset,int cnt,struct ucred * cred,struct thread * td)2810 ncl_commit(struct vnode *vp, u_quad_t offset, int cnt, struct ucred *cred,
2811    struct thread *td)
2812 {
2813 	struct nfsvattr nfsva;
2814 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2815 	struct nfsnode *np;
2816 	struct uio uio;
2817 	int error, attrflag;
2818 
2819 	np = VTONFS(vp);
2820 	error = EIO;
2821 	attrflag = 0;
2822 	if (NFSHASPNFS(nmp) && (np->n_flag & NDSCOMMIT) != 0) {
2823 		uio.uio_offset = offset;
2824 		uio.uio_resid = cnt;
2825 		error = nfscl_doiods(vp, &uio, NULL, NULL,
2826 		    NFSV4OPEN_ACCESSWRITE, 1, cred, td);
2827 		if (error != 0) {
2828 			NFSLOCKNODE(np);
2829 			np->n_flag &= ~NDSCOMMIT;
2830 			NFSUNLOCKNODE(np);
2831 		}
2832 	}
2833 	if (error != 0) {
2834 		mtx_lock(&nmp->nm_mtx);
2835 		if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
2836 			mtx_unlock(&nmp->nm_mtx);
2837 			return (0);
2838 		}
2839 		mtx_unlock(&nmp->nm_mtx);
2840 		error = nfsrpc_commit(vp, offset, cnt, cred, td, &nfsva,
2841 		    &attrflag, NULL);
2842 	}
2843 	if (attrflag != 0)
2844 		(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL,
2845 		    0, 1);
2846 	if (error != 0 && NFS_ISV4(vp))
2847 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
2848 	return (error);
2849 }
2850 
2851 /*
2852  * Strategy routine.
2853  * For async requests when nfsiod(s) are running, queue the request by
2854  * calling ncl_asyncio(), otherwise just all ncl_doio() to do the
2855  * request.
2856  */
2857 static int
nfs_strategy(struct vop_strategy_args * ap)2858 nfs_strategy(struct vop_strategy_args *ap)
2859 {
2860 	struct buf *bp;
2861 	struct vnode *vp;
2862 	struct ucred *cr;
2863 
2864 	bp = ap->a_bp;
2865 	vp = ap->a_vp;
2866 	KASSERT(bp->b_vp == vp, ("missing b_getvp"));
2867 	KASSERT(!(bp->b_flags & B_DONE),
2868 	    ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2869 
2870 	if (vp->v_type == VREG && bp->b_blkno == bp->b_lblkno)
2871 		bp->b_blkno = bp->b_lblkno * (vp->v_bufobj.bo_bsize /
2872 		    DEV_BSIZE);
2873 	if (bp->b_iocmd == BIO_READ)
2874 		cr = bp->b_rcred;
2875 	else
2876 		cr = bp->b_wcred;
2877 
2878 	/*
2879 	 * If the op is asynchronous and an i/o daemon is waiting
2880 	 * queue the request, wake it up and wait for completion
2881 	 * otherwise just do it ourselves.
2882 	 */
2883 	if ((bp->b_flags & B_ASYNC) == 0 ||
2884 	    ncl_asyncio(VFSTONFS(vp->v_mount), bp, NOCRED, curthread))
2885 		(void) ncl_doio(vp, bp, cr, curthread, 1);
2886 	return (0);
2887 }
2888 
2889 /*
2890  * fsync vnode op. Just call ncl_flush() with commit == 1.
2891  */
2892 /* ARGSUSED */
2893 static int
nfs_fsync(struct vop_fsync_args * ap)2894 nfs_fsync(struct vop_fsync_args *ap)
2895 {
2896 
2897 	if (ap->a_vp->v_type != VREG) {
2898 		/*
2899 		 * For NFS, metadata is changed synchronously on the server,
2900 		 * so there is nothing to flush. Also, ncl_flush() clears
2901 		 * the NMODIFIED flag and that shouldn't be done here for
2902 		 * directories.
2903 		 */
2904 		return (0);
2905 	}
2906 	return (ncl_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1, 0));
2907 }
2908 
2909 /*
2910  * Flush all the blocks associated with a vnode.
2911  * 	Walk through the buffer pool and push any dirty pages
2912  *	associated with the vnode.
2913  * If the called_from_renewthread argument is TRUE, it has been called
2914  * from the NFSv4 renew thread and, as such, cannot block indefinitely
2915  * waiting for a buffer write to complete.
2916  */
2917 int
ncl_flush(struct vnode * vp,int waitfor,struct thread * td,int commit,int called_from_renewthread)2918 ncl_flush(struct vnode *vp, int waitfor, struct thread *td,
2919     int commit, int called_from_renewthread)
2920 {
2921 	struct nfsnode *np = VTONFS(vp);
2922 	struct buf *bp;
2923 	int i;
2924 	struct buf *nbp;
2925 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2926 	int error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2927 	int passone = 1, trycnt = 0;
2928 	u_quad_t off, endoff, toff;
2929 	struct ucred* wcred = NULL;
2930 	struct buf **bvec = NULL;
2931 	struct bufobj *bo;
2932 #ifndef NFS_COMMITBVECSIZ
2933 #define	NFS_COMMITBVECSIZ	20
2934 #endif
2935 	struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2936 	u_int bvecsize = 0, bveccount;
2937 	struct timespec ts;
2938 
2939 	if (called_from_renewthread != 0)
2940 		slptimeo = hz;
2941 	if (nmp->nm_flag & NFSMNT_INT)
2942 		slpflag = PCATCH;
2943 	if (!commit)
2944 		passone = 0;
2945 	bo = &vp->v_bufobj;
2946 	/*
2947 	 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2948 	 * server, but has not been committed to stable storage on the server
2949 	 * yet. On the first pass, the byte range is worked out and the commit
2950 	 * rpc is done. On the second pass, ncl_writebp() is called to do the
2951 	 * job.
2952 	 */
2953 again:
2954 	off = (u_quad_t)-1;
2955 	endoff = 0;
2956 	bvecpos = 0;
2957 	if (NFS_ISV34(vp) && commit) {
2958 		if (bvec != NULL && bvec != bvec_on_stack)
2959 			free(bvec, M_TEMP);
2960 		/*
2961 		 * Count up how many buffers waiting for a commit.
2962 		 */
2963 		bveccount = 0;
2964 		BO_LOCK(bo);
2965 		TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2966 			if (!BUF_ISLOCKED(bp) &&
2967 			    (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2968 				== (B_DELWRI | B_NEEDCOMMIT))
2969 				bveccount++;
2970 		}
2971 		/*
2972 		 * Allocate space to remember the list of bufs to commit.  It is
2973 		 * important to use M_NOWAIT here to avoid a race with nfs_write.
2974 		 * If we can't get memory (for whatever reason), we will end up
2975 		 * committing the buffers one-by-one in the loop below.
2976 		 */
2977 		if (bveccount > NFS_COMMITBVECSIZ) {
2978 			/*
2979 			 * Release the vnode interlock to avoid a lock
2980 			 * order reversal.
2981 			 */
2982 			BO_UNLOCK(bo);
2983 			bvec = (struct buf **)
2984 				malloc(bveccount * sizeof(struct buf *),
2985 				       M_TEMP, M_NOWAIT);
2986 			BO_LOCK(bo);
2987 			if (bvec == NULL) {
2988 				bvec = bvec_on_stack;
2989 				bvecsize = NFS_COMMITBVECSIZ;
2990 			} else
2991 				bvecsize = bveccount;
2992 		} else {
2993 			bvec = bvec_on_stack;
2994 			bvecsize = NFS_COMMITBVECSIZ;
2995 		}
2996 		TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2997 			if (bvecpos >= bvecsize)
2998 				break;
2999 			if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
3000 				nbp = TAILQ_NEXT(bp, b_bobufs);
3001 				continue;
3002 			}
3003 			if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3004 			    (B_DELWRI | B_NEEDCOMMIT)) {
3005 				BUF_UNLOCK(bp);
3006 				nbp = TAILQ_NEXT(bp, b_bobufs);
3007 				continue;
3008 			}
3009 			BO_UNLOCK(bo);
3010 			bremfree(bp);
3011 			/*
3012 			 * Work out if all buffers are using the same cred
3013 			 * so we can deal with them all with one commit.
3014 			 *
3015 			 * NOTE: we are not clearing B_DONE here, so we have
3016 			 * to do it later on in this routine if we intend to
3017 			 * initiate I/O on the bp.
3018 			 *
3019 			 * Note: to avoid loopback deadlocks, we do not
3020 			 * assign b_runningbufspace.
3021 			 */
3022 			if (wcred == NULL)
3023 				wcred = bp->b_wcred;
3024 			else if (wcred != bp->b_wcred)
3025 				wcred = NOCRED;
3026 			vfs_busy_pages(bp, 1);
3027 
3028 			BO_LOCK(bo);
3029 			/*
3030 			 * bp is protected by being locked, but nbp is not
3031 			 * and vfs_busy_pages() may sleep.  We have to
3032 			 * recalculate nbp.
3033 			 */
3034 			nbp = TAILQ_NEXT(bp, b_bobufs);
3035 
3036 			/*
3037 			 * A list of these buffers is kept so that the
3038 			 * second loop knows which buffers have actually
3039 			 * been committed. This is necessary, since there
3040 			 * may be a race between the commit rpc and new
3041 			 * uncommitted writes on the file.
3042 			 */
3043 			bvec[bvecpos++] = bp;
3044 			toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
3045 				bp->b_dirtyoff;
3046 			if (toff < off)
3047 				off = toff;
3048 			toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
3049 			if (toff > endoff)
3050 				endoff = toff;
3051 		}
3052 		BO_UNLOCK(bo);
3053 	}
3054 	if (bvecpos > 0) {
3055 		/*
3056 		 * Commit data on the server, as required.
3057 		 * If all bufs are using the same wcred, then use that with
3058 		 * one call for all of them, otherwise commit each one
3059 		 * separately.
3060 		 */
3061 		if (wcred != NOCRED)
3062 			retv = ncl_commit(vp, off, (int)(endoff - off),
3063 					  wcred, td);
3064 		else {
3065 			retv = 0;
3066 			for (i = 0; i < bvecpos; i++) {
3067 				off_t off, size;
3068 				bp = bvec[i];
3069 				off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
3070 					bp->b_dirtyoff;
3071 				size = (u_quad_t)(bp->b_dirtyend
3072 						  - bp->b_dirtyoff);
3073 				retv = ncl_commit(vp, off, (int)size,
3074 						  bp->b_wcred, td);
3075 				if (retv) break;
3076 			}
3077 		}
3078 
3079 		if (retv == NFSERR_STALEWRITEVERF)
3080 			ncl_clearcommit(vp->v_mount);
3081 
3082 		/*
3083 		 * Now, either mark the blocks I/O done or mark the
3084 		 * blocks dirty, depending on whether the commit
3085 		 * succeeded.
3086 		 */
3087 		for (i = 0; i < bvecpos; i++) {
3088 			bp = bvec[i];
3089 			bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3090 			if (!NFSCL_FORCEDISM(vp->v_mount) && retv) {
3091 				/*
3092 				 * Error, leave B_DELWRI intact
3093 				 */
3094 				vfs_unbusy_pages(bp);
3095 				brelse(bp);
3096 			} else {
3097 				/*
3098 				 * Success, remove B_DELWRI ( bundirty() ).
3099 				 *
3100 				 * b_dirtyoff/b_dirtyend seem to be NFS
3101 				 * specific.  We should probably move that
3102 				 * into bundirty(). XXX
3103 				 */
3104 				bufobj_wref(bo);
3105 				bp->b_flags |= B_ASYNC;
3106 				bundirty(bp);
3107 				bp->b_flags &= ~B_DONE;
3108 				bp->b_ioflags &= ~BIO_ERROR;
3109 				bp->b_dirtyoff = bp->b_dirtyend = 0;
3110 				bufdone(bp);
3111 			}
3112 		}
3113 	}
3114 
3115 	/*
3116 	 * Start/do any write(s) that are required.
3117 	 */
3118 loop:
3119 	BO_LOCK(bo);
3120 	TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
3121 		if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL)) {
3122 			if (waitfor != MNT_WAIT || passone)
3123 				continue;
3124 
3125 			error = BUF_TIMELOCK(bp,
3126 			    LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
3127 			    BO_LOCKPTR(bo), "nfsfsync", slpflag, slptimeo);
3128 			if (error == 0) {
3129 				BUF_UNLOCK(bp);
3130 				goto loop;
3131 			}
3132 			if (error == ENOLCK) {
3133 				error = 0;
3134 				goto loop;
3135 			}
3136 			if (called_from_renewthread != 0) {
3137 				/*
3138 				 * Return EIO so the flush will be retried
3139 				 * later.
3140 				 */
3141 				error = EIO;
3142 				goto done;
3143 			}
3144 			if (newnfs_sigintr(nmp, td)) {
3145 				error = EINTR;
3146 				goto done;
3147 			}
3148 			if (slpflag == PCATCH) {
3149 				slpflag = 0;
3150 				slptimeo = 2 * hz;
3151 			}
3152 			goto loop;
3153 		}
3154 		if ((bp->b_flags & B_DELWRI) == 0)
3155 			panic("nfs_fsync: not dirty");
3156 		if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
3157 			BUF_UNLOCK(bp);
3158 			continue;
3159 		}
3160 		BO_UNLOCK(bo);
3161 		bremfree(bp);
3162 		bp->b_flags |= B_ASYNC;
3163 		bwrite(bp);
3164 		if (newnfs_sigintr(nmp, td)) {
3165 			error = EINTR;
3166 			goto done;
3167 		}
3168 		goto loop;
3169 	}
3170 	if (passone) {
3171 		passone = 0;
3172 		BO_UNLOCK(bo);
3173 		goto again;
3174 	}
3175 	if (waitfor == MNT_WAIT) {
3176 		while (bo->bo_numoutput) {
3177 			error = bufobj_wwait(bo, slpflag, slptimeo);
3178 			if (error) {
3179 			    BO_UNLOCK(bo);
3180 			    if (called_from_renewthread != 0) {
3181 				/*
3182 				 * Return EIO so that the flush will be
3183 				 * retried later.
3184 				 */
3185 				error = EIO;
3186 				goto done;
3187 			    }
3188 			    error = newnfs_sigintr(nmp, td);
3189 			    if (error)
3190 				goto done;
3191 			    if (slpflag == PCATCH) {
3192 				slpflag = 0;
3193 				slptimeo = 2 * hz;
3194 			    }
3195 			    BO_LOCK(bo);
3196 			}
3197 		}
3198 		if (bo->bo_dirty.bv_cnt != 0 && commit) {
3199 			BO_UNLOCK(bo);
3200 			goto loop;
3201 		}
3202 		/*
3203 		 * Wait for all the async IO requests to drain
3204 		 */
3205 		BO_UNLOCK(bo);
3206 		NFSLOCKNODE(np);
3207 		while (np->n_directio_asyncwr > 0) {
3208 			np->n_flag |= NFSYNCWAIT;
3209 			error = newnfs_msleep(td, &np->n_directio_asyncwr,
3210 			    &np->n_mtx, slpflag | (PRIBIO + 1),
3211 			    "nfsfsync", 0);
3212 			if (error) {
3213 				if (newnfs_sigintr(nmp, td)) {
3214 					NFSUNLOCKNODE(np);
3215 					error = EINTR;
3216 					goto done;
3217 				}
3218 			}
3219 		}
3220 		NFSUNLOCKNODE(np);
3221 	} else
3222 		BO_UNLOCK(bo);
3223 	if (NFSHASPNFS(nmp)) {
3224 		nfscl_layoutcommit(vp, td);
3225 		/*
3226 		 * Invalidate the attribute cache, since writes to a DS
3227 		 * won't update the size attribute.
3228 		 */
3229 		NFSLOCKNODE(np);
3230 		np->n_attrstamp = 0;
3231 	} else
3232 		NFSLOCKNODE(np);
3233 	if (np->n_flag & NWRITEERR) {
3234 		error = np->n_error;
3235 		np->n_flag &= ~NWRITEERR;
3236 	}
3237   	if (commit && bo->bo_dirty.bv_cnt == 0 &&
3238 	    bo->bo_numoutput == 0 && np->n_directio_asyncwr == 0)
3239   		np->n_flag &= ~NMODIFIED;
3240 	NFSUNLOCKNODE(np);
3241 done:
3242 	if (bvec != NULL && bvec != bvec_on_stack)
3243 		free(bvec, M_TEMP);
3244 	if (error == 0 && commit != 0 && waitfor == MNT_WAIT &&
3245 	    (bo->bo_dirty.bv_cnt != 0 || bo->bo_numoutput != 0 ||
3246 	    np->n_directio_asyncwr != 0)) {
3247 		if (trycnt++ < 5) {
3248 			/* try, try again... */
3249 			passone = 1;
3250 			wcred = NULL;
3251 			bvec = NULL;
3252 			bvecsize = 0;
3253 			goto again;
3254 		}
3255 		vn_printf(vp, "ncl_flush failed");
3256 		error = called_from_renewthread != 0 ? EIO : EBUSY;
3257 	}
3258 	if (error == 0) {
3259 		nanouptime(&ts);
3260 		NFSLOCKNODE(np);
3261 		np->n_localmodtime = ts;
3262 		NFSUNLOCKNODE(np);
3263 	}
3264 	return (error);
3265 }
3266 
3267 /*
3268  * NFS advisory byte-level locks.
3269  */
3270 static int
nfs_advlock(struct vop_advlock_args * ap)3271 nfs_advlock(struct vop_advlock_args *ap)
3272 {
3273 	struct vnode *vp = ap->a_vp;
3274 	struct ucred *cred;
3275 	struct nfsnode *np = VTONFS(ap->a_vp);
3276 	struct proc *p = (struct proc *)ap->a_id;
3277 	struct thread *td = curthread;	/* XXX */
3278 	struct vattr va;
3279 	int ret, error;
3280 	u_quad_t size;
3281 	struct nfsmount *nmp;
3282 
3283 	error = NFSVOPLOCK(vp, LK_SHARED);
3284 	if (error != 0)
3285 		return (EBADF);
3286 	if (NFS_ISV4(vp) && (ap->a_flags & (F_POSIX | F_FLOCK)) != 0) {
3287 		if (vp->v_type != VREG) {
3288 			error = EINVAL;
3289 			goto out;
3290 		}
3291 		if ((ap->a_flags & F_POSIX) != 0)
3292 			cred = p->p_ucred;
3293 		else
3294 			cred = td->td_ucred;
3295 		NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
3296 		if (VN_IS_DOOMED(vp)) {
3297 			error = EBADF;
3298 			goto out;
3299 		}
3300 
3301 		/*
3302 		 * If this is unlocking a write locked region, flush and
3303 		 * commit them before unlocking. This is required by
3304 		 * RFC3530 Sec. 9.3.2.
3305 		 */
3306 		if (ap->a_op == F_UNLCK &&
3307 		    nfscl_checkwritelocked(vp, ap->a_fl, cred, td, ap->a_id,
3308 		    ap->a_flags))
3309 			(void) ncl_flush(vp, MNT_WAIT, td, 1, 0);
3310 
3311 		/*
3312 		 * Mark NFS node as might have acquired a lock.
3313 		 * This is separate from NHASBEENLOCKED, because it must
3314 		 * be done before the nfsrpc_advlock() call, which might
3315 		 * add a nfscllock structure to the client state.
3316 		 * It is used to check for the case where a nfscllock
3317 		 * state structure cannot exist for the file.
3318 		 * Only done for "oneopenown" NFSv4.1/4.2 mounts.
3319 		 */
3320 		nmp = VFSTONFS(vp->v_mount);
3321 		if (NFSHASNFSV4N(nmp) && NFSHASONEOPENOWN(nmp)) {
3322 			NFSLOCKNODE(np);
3323 			np->n_flag |= NMIGHTBELOCKED;
3324 			NFSUNLOCKNODE(np);
3325 		}
3326 
3327 		/*
3328 		 * Loop around doing the lock op, while a blocking lock
3329 		 * must wait for the lock op to succeed.
3330 		 */
3331 		do {
3332 			ret = nfsrpc_advlock(vp, np->n_size, ap->a_op,
3333 			    ap->a_fl, 0, cred, td, ap->a_id, ap->a_flags);
3334 			if (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
3335 			    ap->a_op == F_SETLK) {
3336 				NFSVOPUNLOCK(vp);
3337 				error = nfs_catnap(PZERO | PCATCH, ret,
3338 				    "ncladvl");
3339 				if (error)
3340 					return (EINTR);
3341 				NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
3342 				if (VN_IS_DOOMED(vp)) {
3343 					error = EBADF;
3344 					goto out;
3345 				}
3346 			}
3347 		} while (ret == NFSERR_DENIED && (ap->a_flags & F_WAIT) &&
3348 		     ap->a_op == F_SETLK);
3349 		if (ret == NFSERR_DENIED) {
3350 			error = EAGAIN;
3351 			goto out;
3352 		} else if (ret == EINVAL || ret == EBADF || ret == EINTR) {
3353 			error = ret;
3354 			goto out;
3355 		} else if (ret != 0) {
3356 			error = EACCES;
3357 			goto out;
3358 		}
3359 
3360 		/*
3361 		 * Now, if we just got a lock, invalidate data in the buffer
3362 		 * cache, as required, so that the coherency conforms with
3363 		 * RFC3530 Sec. 9.3.2.
3364 		 */
3365 		if (ap->a_op == F_SETLK) {
3366 			if ((np->n_flag & NMODIFIED) == 0) {
3367 				np->n_attrstamp = 0;
3368 				KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
3369 				ret = VOP_GETATTR(vp, &va, cred);
3370 			}
3371 			if ((np->n_flag & NMODIFIED) || ret ||
3372 			    np->n_change != va.va_filerev) {
3373 				(void) ncl_vinvalbuf(vp, V_SAVE, td, 1);
3374 				np->n_attrstamp = 0;
3375 				KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
3376 				ret = VOP_GETATTR(vp, &va, cred);
3377 				if (!ret) {
3378 					np->n_mtime = va.va_mtime;
3379 					np->n_change = va.va_filerev;
3380 				}
3381 			}
3382 			/* Mark that a file lock has been acquired. */
3383 			NFSLOCKNODE(np);
3384 			np->n_flag |= NHASBEENLOCKED;
3385 			NFSUNLOCKNODE(np);
3386 		}
3387 	} else if (!NFS_ISV4(vp)) {
3388 		if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3389 			size = VTONFS(vp)->n_size;
3390 			NFSVOPUNLOCK(vp);
3391 			error = lf_advlock(ap, &(vp->v_lockf), size);
3392 		} else {
3393 			if (nfs_advlock_p != NULL)
3394 				error = nfs_advlock_p(ap);
3395 			else {
3396 				NFSVOPUNLOCK(vp);
3397 				error = ENOLCK;
3398 			}
3399 		}
3400 		if (error == 0 && ap->a_op == F_SETLK) {
3401 			error = NFSVOPLOCK(vp, LK_SHARED);
3402 			if (error == 0) {
3403 				/* Mark that a file lock has been acquired. */
3404 				NFSLOCKNODE(np);
3405 				np->n_flag |= NHASBEENLOCKED;
3406 				NFSUNLOCKNODE(np);
3407 				NFSVOPUNLOCK(vp);
3408 			}
3409 		}
3410 		return (error);
3411 	} else
3412 		error = EOPNOTSUPP;
3413 out:
3414 	NFSVOPUNLOCK(vp);
3415 	return (error);
3416 }
3417 
3418 /*
3419  * NFS advisory byte-level locks.
3420  */
3421 static int
nfs_advlockasync(struct vop_advlockasync_args * ap)3422 nfs_advlockasync(struct vop_advlockasync_args *ap)
3423 {
3424 	struct vnode *vp = ap->a_vp;
3425 	u_quad_t size;
3426 	int error;
3427 
3428 	if (NFS_ISV4(vp))
3429 		return (EOPNOTSUPP);
3430 	error = NFSVOPLOCK(vp, LK_SHARED);
3431 	if (error)
3432 		return (error);
3433 	if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NOLOCKD) != 0) {
3434 		size = VTONFS(vp)->n_size;
3435 		NFSVOPUNLOCK(vp);
3436 		error = lf_advlockasync(ap, &(vp->v_lockf), size);
3437 	} else {
3438 		NFSVOPUNLOCK(vp);
3439 		error = EOPNOTSUPP;
3440 	}
3441 	return (error);
3442 }
3443 
3444 /*
3445  * Print out the contents of an nfsnode.
3446  */
3447 static int
nfs_print(struct vop_print_args * ap)3448 nfs_print(struct vop_print_args *ap)
3449 {
3450 	struct vnode *vp = ap->a_vp;
3451 	struct nfsnode *np = VTONFS(vp);
3452 
3453 	printf("\tfileid %jd fsid 0x%jx", (uintmax_t)np->n_vattr.na_fileid,
3454 	    (uintmax_t)np->n_vattr.na_fsid);
3455 	if (vp->v_type == VFIFO)
3456 		fifo_printinfo(vp);
3457 	printf("\n");
3458 	return (0);
3459 }
3460 
3461 /*
3462  * This is the "real" nfs::bwrite(struct buf*).
3463  * We set B_CACHE if this is a VMIO buffer.
3464  */
3465 int
ncl_writebp(struct buf * bp,int force __unused,struct thread * td)3466 ncl_writebp(struct buf *bp, int force __unused, struct thread *td)
3467 {
3468 	int oldflags, rtval;
3469 
3470 	if (bp->b_flags & B_INVAL) {
3471 		brelse(bp);
3472 		return (0);
3473 	}
3474 
3475 	oldflags = bp->b_flags;
3476 	bp->b_flags |= B_CACHE;
3477 
3478 	/*
3479 	 * Undirty the bp.  We will redirty it later if the I/O fails.
3480 	 */
3481 	bundirty(bp);
3482 	bp->b_flags &= ~B_DONE;
3483 	bp->b_ioflags &= ~BIO_ERROR;
3484 	bp->b_iocmd = BIO_WRITE;
3485 
3486 	bufobj_wref(bp->b_bufobj);
3487 	curthread->td_ru.ru_oublock++;
3488 
3489 	/*
3490 	 * Note: to avoid loopback deadlocks, we do not
3491 	 * assign b_runningbufspace.
3492 	 */
3493 	vfs_busy_pages(bp, 1);
3494 
3495 	BUF_KERNPROC(bp);
3496 	bp->b_iooffset = dbtob(bp->b_blkno);
3497 	bstrategy(bp);
3498 
3499 	if ((oldflags & B_ASYNC) != 0)
3500 		return (0);
3501 
3502 	rtval = bufwait(bp);
3503 	if (oldflags & B_DELWRI)
3504 		reassignbuf(bp);
3505 	brelse(bp);
3506 	return (rtval);
3507 }
3508 
3509 /*
3510  * nfs special file access vnode op.
3511  * Essentially just get vattr and then imitate iaccess() since the device is
3512  * local to the client.
3513  */
3514 static int
nfsspec_access(struct vop_access_args * ap)3515 nfsspec_access(struct vop_access_args *ap)
3516 {
3517 	struct vattr *vap;
3518 	struct ucred *cred = ap->a_cred;
3519 	struct vnode *vp = ap->a_vp;
3520 	accmode_t accmode = ap->a_accmode;
3521 	struct vattr vattr;
3522 	int error;
3523 
3524 	/*
3525 	 * Disallow write attempts on filesystems mounted read-only;
3526 	 * unless the file is a socket, fifo, or a block or character
3527 	 * device resident on the filesystem.
3528 	 */
3529 	if ((accmode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3530 		switch (vp->v_type) {
3531 		case VREG:
3532 		case VDIR:
3533 		case VLNK:
3534 			return (EROFS);
3535 		default:
3536 			break;
3537 		}
3538 	}
3539 	vap = &vattr;
3540 	error = VOP_GETATTR(vp, vap, cred);
3541 	if (error)
3542 		goto out;
3543 	error = vaccess(vp->v_type, vap->va_mode, vap->va_uid, vap->va_gid,
3544 	    accmode, cred);
3545 out:
3546 	return error;
3547 }
3548 
3549 /*
3550  * Read wrapper for fifos.
3551  */
3552 static int
nfsfifo_read(struct vop_read_args * ap)3553 nfsfifo_read(struct vop_read_args *ap)
3554 {
3555 	struct nfsnode *np = VTONFS(ap->a_vp);
3556 	int error;
3557 
3558 	/*
3559 	 * Set access flag.
3560 	 */
3561 	NFSLOCKNODE(np);
3562 	np->n_flag |= NACC;
3563 	vfs_timestamp(&np->n_atim);
3564 	NFSUNLOCKNODE(np);
3565 	error = fifo_specops.vop_read(ap);
3566 	return error;
3567 }
3568 
3569 /*
3570  * Write wrapper for fifos.
3571  */
3572 static int
nfsfifo_write(struct vop_write_args * ap)3573 nfsfifo_write(struct vop_write_args *ap)
3574 {
3575 	struct nfsnode *np = VTONFS(ap->a_vp);
3576 
3577 	/*
3578 	 * Set update flag.
3579 	 */
3580 	NFSLOCKNODE(np);
3581 	np->n_flag |= NUPD;
3582 	vfs_timestamp(&np->n_mtim);
3583 	NFSUNLOCKNODE(np);
3584 	return(fifo_specops.vop_write(ap));
3585 }
3586 
3587 /*
3588  * Close wrapper for fifos.
3589  *
3590  * Update the times on the nfsnode then do fifo close.
3591  */
3592 static int
nfsfifo_close(struct vop_close_args * ap)3593 nfsfifo_close(struct vop_close_args *ap)
3594 {
3595 	struct vnode *vp = ap->a_vp;
3596 	struct nfsnode *np = VTONFS(vp);
3597 	struct vattr vattr;
3598 	struct timespec ts;
3599 
3600 	NFSLOCKNODE(np);
3601 	if (np->n_flag & (NACC | NUPD)) {
3602 		vfs_timestamp(&ts);
3603 		if (np->n_flag & NACC)
3604 			np->n_atim = ts;
3605 		if (np->n_flag & NUPD)
3606 			np->n_mtim = ts;
3607 		np->n_flag |= NCHG;
3608 		if (vrefcnt(vp) == 1 &&
3609 		    (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3610 			VATTR_NULL(&vattr);
3611 			if (np->n_flag & NACC)
3612 				vattr.va_atime = np->n_atim;
3613 			if (np->n_flag & NUPD)
3614 				vattr.va_mtime = np->n_mtim;
3615 			NFSUNLOCKNODE(np);
3616 			(void)VOP_SETATTR(vp, &vattr, ap->a_cred);
3617 			goto out;
3618 		}
3619 	}
3620 	NFSUNLOCKNODE(np);
3621 out:
3622 	return (fifo_specops.vop_close(ap));
3623 }
3624 
3625 /*
3626  * Just call ncl_writebp() with the force argument set to 1.
3627  *
3628  * NOTE: B_DONE may or may not be set in a_bp on call.
3629  */
3630 static int
nfs_bwrite(struct buf * bp)3631 nfs_bwrite(struct buf *bp)
3632 {
3633 
3634 	return (ncl_writebp(bp, 1, curthread));
3635 }
3636 
3637 struct buf_ops buf_ops_newnfs = {
3638 	.bop_name	=	"buf_ops_nfs",
3639 	.bop_write	=	nfs_bwrite,
3640 	.bop_strategy	=	bufstrategy,
3641 	.bop_sync	=	bufsync,
3642 	.bop_bdflush	=	bufbdflush,
3643 };
3644 
3645 static int
nfs_getacl(struct vop_getacl_args * ap)3646 nfs_getacl(struct vop_getacl_args *ap)
3647 {
3648 	int error;
3649 
3650 	if (ap->a_type != ACL_TYPE_NFS4)
3651 		return (EOPNOTSUPP);
3652 	error = nfsrpc_getacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp,
3653 	    NULL);
3654 	if (error > NFSERR_STALE) {
3655 		(void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3656 		error = EPERM;
3657 	}
3658 	return (error);
3659 }
3660 
3661 static int
nfs_setacl(struct vop_setacl_args * ap)3662 nfs_setacl(struct vop_setacl_args *ap)
3663 {
3664 	int error;
3665 
3666 	if (ap->a_type != ACL_TYPE_NFS4)
3667 		return (EOPNOTSUPP);
3668 	error = nfsrpc_setacl(ap->a_vp, ap->a_cred, ap->a_td, ap->a_aclp,
3669 	    NULL);
3670 	if (error > NFSERR_STALE) {
3671 		(void) nfscl_maperr(ap->a_td, error, (uid_t)0, (gid_t)0);
3672 		error = EPERM;
3673 	}
3674 	return (error);
3675 }
3676 
3677 /*
3678  * VOP_ADVISE for NFS.
3679  * Just return 0 for any errors, since it is just a hint.
3680  */
3681 static int
nfs_advise(struct vop_advise_args * ap)3682 nfs_advise(struct vop_advise_args *ap)
3683 {
3684 	struct thread *td = curthread;
3685 	struct nfsmount *nmp;
3686 	uint64_t len;
3687 	int error;
3688 
3689 	/*
3690 	 * First do vop_stdadvise() to handle the buffer cache.
3691 	 */
3692 	error = vop_stdadvise(ap);
3693 	if (error != 0)
3694 		return (error);
3695 	if (ap->a_start < 0 || ap->a_end < 0)
3696 		return (0);
3697 	if (ap->a_end == OFF_MAX)
3698 		len = 0;
3699 	else if (ap->a_end < ap->a_start)
3700 		return (0);
3701 	else
3702 		len = ap->a_end - ap->a_start + 1;
3703 	nmp = VFSTONFS(ap->a_vp->v_mount);
3704 	mtx_lock(&nmp->nm_mtx);
3705 	if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
3706 	    (NFSHASPNFS(nmp) && (nmp->nm_privflag & NFSMNTP_IOADVISETHRUMDS) ==
3707 	    0) || (nmp->nm_privflag & NFSMNTP_NOADVISE) != 0) {
3708 		mtx_unlock(&nmp->nm_mtx);
3709 		return (0);
3710 	}
3711 	mtx_unlock(&nmp->nm_mtx);
3712 	error = nfsrpc_advise(ap->a_vp, ap->a_start, len, ap->a_advice,
3713 	    td->td_ucred, td);
3714 	if (error == NFSERR_NOTSUPP) {
3715 		mtx_lock(&nmp->nm_mtx);
3716 		nmp->nm_privflag |= NFSMNTP_NOADVISE;
3717 		mtx_unlock(&nmp->nm_mtx);
3718 	}
3719 	return (0);
3720 }
3721 
3722 /*
3723  * nfs allocate call
3724  */
3725 static int
nfs_allocate(struct vop_allocate_args * ap)3726 nfs_allocate(struct vop_allocate_args *ap)
3727 {
3728 	struct vnode *vp = ap->a_vp;
3729 	struct thread *td = curthread;
3730 	struct nfsvattr nfsva;
3731 	struct nfsmount *nmp;
3732 	struct nfsnode *np;
3733 	off_t alen;
3734 	int attrflag, error, ret;
3735 	struct timespec ts;
3736 	struct uio io;
3737 
3738 	attrflag = 0;
3739 	nmp = VFSTONFS(vp->v_mount);
3740 	np = VTONFS(vp);
3741 	mtx_lock(&nmp->nm_mtx);
3742 	if (NFSHASNFSV4(nmp) && nmp->nm_minorvers >= NFSV42_MINORVERSION &&
3743 	    (nmp->nm_privflag & NFSMNTP_NOALLOCATE) == 0) {
3744 		mtx_unlock(&nmp->nm_mtx);
3745 		alen = *ap->a_len;
3746 		if ((uint64_t)alen > nfs_maxalloclen)
3747 			alen = nfs_maxalloclen;
3748 
3749 		/* Check the file size limit. */
3750 		io.uio_offset = *ap->a_offset;
3751 		io.uio_resid = alen;
3752 		error = vn_rlimit_fsize(vp, &io, td);
3753 
3754 		/*
3755 		 * Flush first to ensure that the allocate adds to the
3756 		 * file's allocation on the server.
3757 		 */
3758 		if (error == 0)
3759 			error = ncl_flush(vp, MNT_WAIT, td, 1, 0);
3760 		if (error == 0)
3761 			error = nfsrpc_allocate(vp, *ap->a_offset, alen,
3762 			    &nfsva, &attrflag, ap->a_cred, td, NULL);
3763 		if (error == 0) {
3764 			*ap->a_offset += alen;
3765 			*ap->a_len -= alen;
3766 			nanouptime(&ts);
3767 			NFSLOCKNODE(np);
3768 			np->n_localmodtime = ts;
3769 			NFSUNLOCKNODE(np);
3770 		} else if (error == NFSERR_NOTSUPP) {
3771 			mtx_lock(&nmp->nm_mtx);
3772 			nmp->nm_privflag |= NFSMNTP_NOALLOCATE;
3773 			mtx_unlock(&nmp->nm_mtx);
3774 			error = EINVAL;
3775 		}
3776 	} else {
3777 		mtx_unlock(&nmp->nm_mtx);
3778 		error = EINVAL;
3779 	}
3780 	if (attrflag != 0) {
3781 		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
3782 		if (error == 0 && ret != 0)
3783 			error = ret;
3784 	}
3785 	if (error != 0)
3786 		error = nfscl_maperr(td, error, (uid_t)0, (gid_t)0);
3787 	return (error);
3788 }
3789 
3790 /*
3791  * nfs copy_file_range call
3792  */
3793 static int
nfs_copy_file_range(struct vop_copy_file_range_args * ap)3794 nfs_copy_file_range(struct vop_copy_file_range_args *ap)
3795 {
3796 	struct vnode *invp = ap->a_invp;
3797 	struct vnode *outvp = ap->a_outvp;
3798 	struct mount *mp;
3799 	struct nfsvattr innfsva, outnfsva;
3800 	struct vattr *vap;
3801 	struct uio io;
3802 	struct nfsmount *nmp;
3803 	size_t len, len2;
3804 	int error, inattrflag, outattrflag, ret, ret2;
3805 	off_t inoff, outoff;
3806 	bool consecutive, must_commit, tryoutcred;
3807 
3808 	ret = ret2 = 0;
3809 	nmp = VFSTONFS(invp->v_mount);
3810 	mtx_lock(&nmp->nm_mtx);
3811 	/* NFSv4.2 Copy is not permitted for infile == outfile. */
3812 	if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
3813 	    (nmp->nm_privflag & NFSMNTP_NOCOPY) != 0 || invp == outvp) {
3814 		mtx_unlock(&nmp->nm_mtx);
3815 		error = vn_generic_copy_file_range(ap->a_invp, ap->a_inoffp,
3816 		    ap->a_outvp, ap->a_outoffp, ap->a_lenp, ap->a_flags,
3817 		    ap->a_incred, ap->a_outcred, ap->a_fsizetd);
3818 		return (error);
3819 	}
3820 	mtx_unlock(&nmp->nm_mtx);
3821 
3822 	/* Lock both vnodes, avoiding risk of deadlock. */
3823 	do {
3824 		mp = NULL;
3825 		error = vn_start_write(outvp, &mp, V_WAIT);
3826 		if (error == 0) {
3827 			error = vn_lock(outvp, LK_EXCLUSIVE);
3828 			if (error == 0) {
3829 				error = vn_lock(invp, LK_SHARED | LK_NOWAIT);
3830 				if (error == 0)
3831 					break;
3832 				VOP_UNLOCK(outvp);
3833 				if (mp != NULL)
3834 					vn_finished_write(mp);
3835 				mp = NULL;
3836 				error = vn_lock(invp, LK_SHARED);
3837 				if (error == 0)
3838 					VOP_UNLOCK(invp);
3839 			}
3840 		}
3841 		if (mp != NULL)
3842 			vn_finished_write(mp);
3843 	} while (error == 0);
3844 	if (error != 0)
3845 		return (error);
3846 
3847 	/*
3848 	 * Do the vn_rlimit_fsize() check.  Should this be above the VOP layer?
3849 	 */
3850 	io.uio_offset = *ap->a_outoffp;
3851 	io.uio_resid = *ap->a_lenp;
3852 	error = vn_rlimit_fsize(outvp, &io, ap->a_fsizetd);
3853 
3854 	/*
3855 	 * Flush the input file so that the data is up to date before
3856 	 * the copy.  Flush writes for the output file so that they
3857 	 * do not overwrite the data copied to the output file by the Copy.
3858 	 * Set the commit argument for both flushes so that the data is on
3859 	 * stable storage before the Copy RPC.  This is done in case the
3860 	 * server reboots during the Copy and needs to be redone.
3861 	 */
3862 	if (error == 0)
3863 		error = ncl_flush(invp, MNT_WAIT, curthread, 1, 0);
3864 	if (error == 0)
3865 		error = ncl_flush(outvp, MNT_WAIT, curthread, 1, 0);
3866 
3867 	/* Do the actual NFSv4.2 RPC. */
3868 	len = *ap->a_lenp;
3869 	mtx_lock(&nmp->nm_mtx);
3870 	if ((nmp->nm_privflag & NFSMNTP_NOCONSECUTIVE) == 0)
3871 		consecutive = true;
3872 	else
3873 		consecutive = false;
3874 	mtx_unlock(&nmp->nm_mtx);
3875 	inoff = *ap->a_inoffp;
3876 	outoff = *ap->a_outoffp;
3877 	tryoutcred = true;
3878 	must_commit = false;
3879 	if (error == 0) {
3880 		vap = &VTONFS(invp)->n_vattr.na_vattr;
3881 		error = VOP_GETATTR(invp, vap, ap->a_incred);
3882 		if (error == 0) {
3883 			/*
3884 			 * Clip "len" at va_size so that RFC compliant servers
3885 			 * will not reply NFSERR_INVAL.
3886 			 * Setting "len == 0" for the RPC would be preferred,
3887 			 * but some Linux servers do not support that.
3888 			 */
3889 			if (inoff >= vap->va_size)
3890 				*ap->a_lenp = len = 0;
3891 			else if (inoff + len > vap->va_size)
3892 				*ap->a_lenp = len = vap->va_size - inoff;
3893 		} else
3894 			error = 0;
3895 	}
3896 
3897 	/*
3898 	 * len will be set to 0 upon a successful Copy RPC.
3899 	 * As such, this only loops when the Copy RPC needs to be retried.
3900 	 */
3901 	while (len > 0 && error == 0) {
3902 		inattrflag = outattrflag = 0;
3903 		len2 = len;
3904 		if (tryoutcred)
3905 			error = nfsrpc_copy_file_range(invp, ap->a_inoffp,
3906 			    outvp, ap->a_outoffp, &len2, ap->a_flags,
3907 			    &inattrflag, &innfsva, &outattrflag, &outnfsva,
3908 			    ap->a_outcred, consecutive, &must_commit);
3909 		else
3910 			error = nfsrpc_copy_file_range(invp, ap->a_inoffp,
3911 			    outvp, ap->a_outoffp, &len2, ap->a_flags,
3912 			    &inattrflag, &innfsva, &outattrflag, &outnfsva,
3913 			    ap->a_incred, consecutive, &must_commit);
3914 		if (inattrflag != 0)
3915 			ret = nfscl_loadattrcache(&invp, &innfsva, NULL, NULL,
3916 			    0, 1);
3917 		if (outattrflag != 0)
3918 			ret2 = nfscl_loadattrcache(&outvp, &outnfsva, NULL,
3919 			    NULL, 1, 1);
3920 		if (error == 0) {
3921 			if (consecutive == false) {
3922 				if (len2 == len) {
3923 					mtx_lock(&nmp->nm_mtx);
3924 					nmp->nm_privflag |=
3925 					    NFSMNTP_NOCONSECUTIVE;
3926 					mtx_unlock(&nmp->nm_mtx);
3927 				} else
3928 					error = NFSERR_OFFLOADNOREQS;
3929 			}
3930 			*ap->a_lenp = len2;
3931 			len = 0;
3932 			if (len2 > 0 && must_commit && error == 0)
3933 				error = ncl_commit(outvp, outoff, *ap->a_lenp,
3934 				    ap->a_outcred, curthread);
3935 			if (error == 0 && ret != 0)
3936 				error = ret;
3937 			if (error == 0 && ret2 != 0)
3938 				error = ret2;
3939 		} else if (error == NFSERR_OFFLOADNOREQS && consecutive) {
3940 			/*
3941 			 * Try consecutive == false, which is ok only if all
3942 			 * bytes are copied.
3943 			 * If only some bytes were copied when consecutive
3944 			 * is false, there is no way to know which bytes
3945 			 * still need to be written.
3946 			 */
3947 			consecutive = false;
3948 			error = 0;
3949 		} else if (error == NFSERR_ACCES && tryoutcred) {
3950 			/* Try again with incred. */
3951 			tryoutcred = false;
3952 			error = 0;
3953 		}
3954 		if (error == NFSERR_STALEWRITEVERF) {
3955 			/*
3956 			 * Server rebooted, so do it all again.
3957 			 */
3958 			*ap->a_inoffp = inoff;
3959 			*ap->a_outoffp = outoff;
3960 			len = *ap->a_lenp;
3961 			must_commit = false;
3962 			error = 0;
3963 		}
3964 	}
3965 	VOP_UNLOCK(invp);
3966 	VOP_UNLOCK(outvp);
3967 	if (mp != NULL)
3968 		vn_finished_write(mp);
3969 	if (error == NFSERR_NOTSUPP || error == NFSERR_OFFLOADNOREQS ||
3970 	    error == NFSERR_ACCES) {
3971 		/*
3972 		 * Unlike the NFSv4.2 Copy, vn_generic_copy_file_range() can
3973 		 * use a_incred for the read and a_outcred for the write, so
3974 		 * try this for NFSERR_ACCES failures for the Copy.
3975 		 * For NFSERR_NOTSUPP and NFSERR_OFFLOADNOREQS, the Copy can
3976 		 * never succeed, so disable it.
3977 		 */
3978 		if (error != NFSERR_ACCES) {
3979 			/* Can never do Copy on this mount. */
3980 			mtx_lock(&nmp->nm_mtx);
3981 			nmp->nm_privflag |= NFSMNTP_NOCOPY;
3982 			mtx_unlock(&nmp->nm_mtx);
3983 		}
3984 		*ap->a_inoffp = inoff;
3985 		*ap->a_outoffp = outoff;
3986 		error = vn_generic_copy_file_range(ap->a_invp, ap->a_inoffp,
3987 		    ap->a_outvp, ap->a_outoffp, ap->a_lenp, ap->a_flags,
3988 		    ap->a_incred, ap->a_outcred, ap->a_fsizetd);
3989 	} else if (error != 0)
3990 		*ap->a_lenp = 0;
3991 
3992 	if (error != 0)
3993 		error = nfscl_maperr(curthread, error, (uid_t)0, (gid_t)0);
3994 	return (error);
3995 }
3996 
3997 /*
3998  * nfs ioctl call
3999  */
4000 static int
nfs_ioctl(struct vop_ioctl_args * ap)4001 nfs_ioctl(struct vop_ioctl_args *ap)
4002 {
4003 	struct vnode *vp = ap->a_vp;
4004 	struct nfsvattr nfsva;
4005 	struct nfsmount *nmp;
4006 	int attrflag, content, error, ret;
4007 	bool eof = false;			/* shut up compiler. */
4008 
4009 	if (vp->v_type != VREG)
4010 		return (ENOTTY);
4011 	nmp = VFSTONFS(vp->v_mount);
4012 	if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION) {
4013 		error = vop_stdioctl(ap);
4014 		return (error);
4015 	}
4016 
4017 	/* Do the actual NFSv4.2 RPC. */
4018 	switch (ap->a_command) {
4019 	case FIOSEEKDATA:
4020 		content = NFSV4CONTENT_DATA;
4021 		break;
4022 	case FIOSEEKHOLE:
4023 		content = NFSV4CONTENT_HOLE;
4024 		break;
4025 	default:
4026 		return (ENOTTY);
4027 	}
4028 
4029 	error = vn_lock(vp, LK_SHARED);
4030 	if (error != 0)
4031 		return (EBADF);
4032 	attrflag = 0;
4033 	if (*((off_t *)ap->a_data) >= VTONFS(vp)->n_size)
4034 		error = ENXIO;
4035 	else {
4036 		/*
4037 		 * Flush all writes, so that the server is up to date.
4038 		 * Although a Commit is not required, the commit argument
4039 		 * is set so that, for a pNFS File/Flexible File Layout
4040 		 * server, the LayoutCommit will be done to ensure the file
4041 		 * size is up to date on the Metadata Server.
4042 		 */
4043 		error = ncl_flush(vp, MNT_WAIT, ap->a_td, 1, 0);
4044 		if (error == 0)
4045 			error = nfsrpc_seek(vp, (off_t *)ap->a_data, &eof,
4046 			    content, ap->a_cred, &nfsva, &attrflag);
4047 		/* If at eof for FIOSEEKDATA, return ENXIO. */
4048 		if (eof && error == 0 && content == NFSV4CONTENT_DATA)
4049 			error = ENXIO;
4050 	}
4051 	if (attrflag != 0) {
4052 		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
4053 		if (error == 0 && ret != 0)
4054 			error = ret;
4055 	}
4056 	NFSVOPUNLOCK(vp);
4057 
4058 	if (error != 0)
4059 		error = ENXIO;
4060 	return (error);
4061 }
4062 
4063 /*
4064  * nfs getextattr call
4065  */
4066 static int
nfs_getextattr(struct vop_getextattr_args * ap)4067 nfs_getextattr(struct vop_getextattr_args *ap)
4068 {
4069 	struct vnode *vp = ap->a_vp;
4070 	struct nfsmount *nmp;
4071 	struct ucred *cred;
4072 	struct thread *td = ap->a_td;
4073 	struct nfsvattr nfsva;
4074 	ssize_t len;
4075 	int attrflag, error, ret;
4076 
4077 	nmp = VFSTONFS(vp->v_mount);
4078 	mtx_lock(&nmp->nm_mtx);
4079 	if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4080 	    (nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 ||
4081 	    ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) {
4082 		mtx_unlock(&nmp->nm_mtx);
4083 		return (EOPNOTSUPP);
4084 	}
4085 	mtx_unlock(&nmp->nm_mtx);
4086 
4087 	cred = ap->a_cred;
4088 	if (cred == NULL)
4089 		cred = td->td_ucred;
4090 	/* Do the actual NFSv4.2 Optional Extended Attribute (RFC-8276) RPC. */
4091 	attrflag = 0;
4092 	error = nfsrpc_getextattr(vp, ap->a_name, ap->a_uio, &len, &nfsva,
4093 	    &attrflag, cred, td);
4094 	if (attrflag != 0) {
4095 		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
4096 		if (error == 0 && ret != 0)
4097 			error = ret;
4098 	}
4099 	if (error == 0 && ap->a_size != NULL)
4100 		*ap->a_size = len;
4101 
4102 	switch (error) {
4103 	case NFSERR_NOTSUPP:
4104 	case NFSERR_OPILLEGAL:
4105 		mtx_lock(&nmp->nm_mtx);
4106 		nmp->nm_privflag |= NFSMNTP_NOXATTR;
4107 		mtx_unlock(&nmp->nm_mtx);
4108 		error = EOPNOTSUPP;
4109 		break;
4110 	case NFSERR_NOXATTR:
4111 	case NFSERR_XATTR2BIG:
4112 		error = ENOATTR;
4113 		break;
4114 	default:
4115 		error = nfscl_maperr(td, error, 0, 0);
4116 		break;
4117 	}
4118 	return (error);
4119 }
4120 
4121 /*
4122  * nfs setextattr call
4123  */
4124 static int
nfs_setextattr(struct vop_setextattr_args * ap)4125 nfs_setextattr(struct vop_setextattr_args *ap)
4126 {
4127 	struct vnode *vp = ap->a_vp;
4128 	struct nfsmount *nmp;
4129 	struct ucred *cred;
4130 	struct thread *td = ap->a_td;
4131 	struct nfsvattr nfsva;
4132 	int attrflag, error, ret;
4133 
4134 	nmp = VFSTONFS(vp->v_mount);
4135 	mtx_lock(&nmp->nm_mtx);
4136 	if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4137 	    (nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 ||
4138 	    ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) {
4139 		mtx_unlock(&nmp->nm_mtx);
4140 		return (EOPNOTSUPP);
4141 	}
4142 	mtx_unlock(&nmp->nm_mtx);
4143 
4144 	if (ap->a_uio->uio_resid < 0)
4145 		return (EINVAL);
4146 	cred = ap->a_cred;
4147 	if (cred == NULL)
4148 		cred = td->td_ucred;
4149 	/* Do the actual NFSv4.2 Optional Extended Attribute (RFC-8276) RPC. */
4150 	attrflag = 0;
4151 	error = nfsrpc_setextattr(vp, ap->a_name, ap->a_uio, &nfsva,
4152 	    &attrflag, cred, td);
4153 	if (attrflag != 0) {
4154 		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
4155 		if (error == 0 && ret != 0)
4156 			error = ret;
4157 	}
4158 
4159 	switch (error) {
4160 	case NFSERR_NOTSUPP:
4161 	case NFSERR_OPILLEGAL:
4162 		mtx_lock(&nmp->nm_mtx);
4163 		nmp->nm_privflag |= NFSMNTP_NOXATTR;
4164 		mtx_unlock(&nmp->nm_mtx);
4165 		error = EOPNOTSUPP;
4166 		break;
4167 	case NFSERR_NOXATTR:
4168 	case NFSERR_XATTR2BIG:
4169 		error = ENOATTR;
4170 		break;
4171 	default:
4172 		error = nfscl_maperr(td, error, 0, 0);
4173 		break;
4174 	}
4175 	return (error);
4176 }
4177 
4178 /*
4179  * nfs listextattr call
4180  */
4181 static int
nfs_listextattr(struct vop_listextattr_args * ap)4182 nfs_listextattr(struct vop_listextattr_args *ap)
4183 {
4184 	struct vnode *vp = ap->a_vp;
4185 	struct nfsmount *nmp;
4186 	struct ucred *cred;
4187 	struct thread *td = ap->a_td;
4188 	struct nfsvattr nfsva;
4189 	size_t len, len2;
4190 	uint64_t cookie;
4191 	int attrflag, error, ret;
4192 	bool eof;
4193 
4194 	nmp = VFSTONFS(vp->v_mount);
4195 	mtx_lock(&nmp->nm_mtx);
4196 	if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4197 	    (nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 ||
4198 	    ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) {
4199 		mtx_unlock(&nmp->nm_mtx);
4200 		return (EOPNOTSUPP);
4201 	}
4202 	mtx_unlock(&nmp->nm_mtx);
4203 
4204 	cred = ap->a_cred;
4205 	if (cred == NULL)
4206 		cred = td->td_ucred;
4207 
4208 	/* Loop around doing List Extended Attribute RPCs. */
4209 	eof = false;
4210 	cookie = 0;
4211 	len2 = 0;
4212 	error = 0;
4213 	while (!eof && error == 0) {
4214 		len = nmp->nm_rsize;
4215 		attrflag = 0;
4216 		error = nfsrpc_listextattr(vp, &cookie, ap->a_uio, &len, &eof,
4217 		    &nfsva, &attrflag, cred, td);
4218 		if (attrflag != 0) {
4219 			ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0,
4220 			    1);
4221 			if (error == 0 && ret != 0)
4222 				error = ret;
4223 		}
4224 		if (error == 0) {
4225 			len2 += len;
4226 			if (len2 > SSIZE_MAX)
4227 				error = ENOATTR;
4228 		}
4229 	}
4230 	if (error == 0 && ap->a_size != NULL)
4231 		*ap->a_size = len2;
4232 
4233 	switch (error) {
4234 	case NFSERR_NOTSUPP:
4235 	case NFSERR_OPILLEGAL:
4236 		mtx_lock(&nmp->nm_mtx);
4237 		nmp->nm_privflag |= NFSMNTP_NOXATTR;
4238 		mtx_unlock(&nmp->nm_mtx);
4239 		error = EOPNOTSUPP;
4240 		break;
4241 	case NFSERR_NOXATTR:
4242 	case NFSERR_XATTR2BIG:
4243 		error = ENOATTR;
4244 		break;
4245 	default:
4246 		error = nfscl_maperr(td, error, 0, 0);
4247 		break;
4248 	}
4249 	return (error);
4250 }
4251 
4252 /*
4253  * nfs setextattr call
4254  */
4255 static int
nfs_deleteextattr(struct vop_deleteextattr_args * ap)4256 nfs_deleteextattr(struct vop_deleteextattr_args *ap)
4257 {
4258 	struct vnode *vp = ap->a_vp;
4259 	struct nfsmount *nmp;
4260 	struct nfsvattr nfsva;
4261 	int attrflag, error, ret;
4262 
4263 	nmp = VFSTONFS(vp->v_mount);
4264 	mtx_lock(&nmp->nm_mtx);
4265 	if (!NFSHASNFSV4(nmp) || nmp->nm_minorvers < NFSV42_MINORVERSION ||
4266 	    (nmp->nm_privflag & NFSMNTP_NOXATTR) != 0 ||
4267 	    ap->a_attrnamespace != EXTATTR_NAMESPACE_USER) {
4268 		mtx_unlock(&nmp->nm_mtx);
4269 		return (EOPNOTSUPP);
4270 	}
4271 	mtx_unlock(&nmp->nm_mtx);
4272 
4273 	/* Do the actual NFSv4.2 Optional Extended Attribute (RFC-8276) RPC. */
4274 	attrflag = 0;
4275 	error = nfsrpc_rmextattr(vp, ap->a_name, &nfsva, &attrflag, ap->a_cred,
4276 	    ap->a_td);
4277 	if (attrflag != 0) {
4278 		ret = nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0, 1);
4279 		if (error == 0 && ret != 0)
4280 			error = ret;
4281 	}
4282 
4283 	switch (error) {
4284 	case NFSERR_NOTSUPP:
4285 	case NFSERR_OPILLEGAL:
4286 		mtx_lock(&nmp->nm_mtx);
4287 		nmp->nm_privflag |= NFSMNTP_NOXATTR;
4288 		mtx_unlock(&nmp->nm_mtx);
4289 		error = EOPNOTSUPP;
4290 		break;
4291 	case NFSERR_NOXATTR:
4292 	case NFSERR_XATTR2BIG:
4293 		error = ENOATTR;
4294 		break;
4295 	default:
4296 		error = nfscl_maperr(ap->a_td, error, 0, 0);
4297 		break;
4298 	}
4299 	return (error);
4300 }
4301 
4302 /*
4303  * Return POSIX pathconf information applicable to nfs filesystems.
4304  */
4305 static int
nfs_pathconf(struct vop_pathconf_args * ap)4306 nfs_pathconf(struct vop_pathconf_args *ap)
4307 {
4308 	struct nfsv3_pathconf pc;
4309 	struct nfsvattr nfsva;
4310 	struct vnode *vp = ap->a_vp;
4311 	struct nfsmount *nmp;
4312 	struct thread *td = curthread;
4313 	off_t off;
4314 	bool eof;
4315 	int attrflag, error;
4316 
4317 	if ((NFS_ISV34(vp) && (ap->a_name == _PC_LINK_MAX ||
4318 	    ap->a_name == _PC_NAME_MAX || ap->a_name == _PC_CHOWN_RESTRICTED ||
4319 	    ap->a_name == _PC_NO_TRUNC)) ||
4320 	    (NFS_ISV4(vp) && ap->a_name == _PC_ACL_NFS4)) {
4321 		/*
4322 		 * Since only the above 4 a_names are returned by the NFSv3
4323 		 * Pathconf RPC, there is no point in doing it for others.
4324 		 * For NFSv4, the Pathconf RPC (actually a Getattr Op.) can
4325 		 * be used for _PC_NFS4_ACL as well.
4326 		 */
4327 		error = nfsrpc_pathconf(vp, &pc, td->td_ucred, td, &nfsva,
4328 		    &attrflag, NULL);
4329 		if (attrflag != 0)
4330 			(void) nfscl_loadattrcache(&vp, &nfsva, NULL, NULL, 0,
4331 			    1);
4332 		if (error != 0)
4333 			return (error);
4334 	} else {
4335 		/*
4336 		 * For NFSv2 (or NFSv3 when not one of the above 4 a_names),
4337 		 * just fake them.
4338 		 */
4339 		pc.pc_linkmax = NFS_LINK_MAX;
4340 		pc.pc_namemax = NFS_MAXNAMLEN;
4341 		pc.pc_notrunc = 1;
4342 		pc.pc_chownrestricted = 1;
4343 		pc.pc_caseinsensitive = 0;
4344 		pc.pc_casepreserving = 1;
4345 		error = 0;
4346 	}
4347 	switch (ap->a_name) {
4348 	case _PC_LINK_MAX:
4349 #ifdef _LP64
4350 		*ap->a_retval = pc.pc_linkmax;
4351 #else
4352 		*ap->a_retval = MIN(LONG_MAX, pc.pc_linkmax);
4353 #endif
4354 		break;
4355 	case _PC_NAME_MAX:
4356 		*ap->a_retval = pc.pc_namemax;
4357 		break;
4358 	case _PC_PIPE_BUF:
4359 		if (ap->a_vp->v_type == VDIR || ap->a_vp->v_type == VFIFO)
4360 			*ap->a_retval = PIPE_BUF;
4361 		else
4362 			error = EINVAL;
4363 		break;
4364 	case _PC_CHOWN_RESTRICTED:
4365 		*ap->a_retval = pc.pc_chownrestricted;
4366 		break;
4367 	case _PC_NO_TRUNC:
4368 		*ap->a_retval = pc.pc_notrunc;
4369 		break;
4370 	case _PC_ACL_NFS4:
4371 		if (NFS_ISV4(vp) && nfsrv_useacl != 0 && attrflag != 0 &&
4372 		    NFSISSET_ATTRBIT(&nfsva.na_suppattr, NFSATTRBIT_ACL))
4373 			*ap->a_retval = 1;
4374 		else
4375 			*ap->a_retval = 0;
4376 		break;
4377 	case _PC_ACL_PATH_MAX:
4378 		if (NFS_ISV4(vp))
4379 			*ap->a_retval = ACL_MAX_ENTRIES;
4380 		else
4381 			*ap->a_retval = 3;
4382 		break;
4383 	case _PC_PRIO_IO:
4384 		*ap->a_retval = 0;
4385 		break;
4386 	case _PC_SYNC_IO:
4387 		*ap->a_retval = 0;
4388 		break;
4389 	case _PC_ALLOC_SIZE_MIN:
4390 		*ap->a_retval = vp->v_mount->mnt_stat.f_bsize;
4391 		break;
4392 	case _PC_FILESIZEBITS:
4393 		if (NFS_ISV34(vp))
4394 			*ap->a_retval = 64;
4395 		else
4396 			*ap->a_retval = 32;
4397 		break;
4398 	case _PC_REC_INCR_XFER_SIZE:
4399 		*ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
4400 		break;
4401 	case _PC_REC_MAX_XFER_SIZE:
4402 		*ap->a_retval = -1; /* means ``unlimited'' */
4403 		break;
4404 	case _PC_REC_MIN_XFER_SIZE:
4405 		*ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
4406 		break;
4407 	case _PC_REC_XFER_ALIGN:
4408 		*ap->a_retval = PAGE_SIZE;
4409 		break;
4410 	case _PC_SYMLINK_MAX:
4411 		*ap->a_retval = NFS_MAXPATHLEN;
4412 		break;
4413 	case _PC_MIN_HOLE_SIZE:
4414 		/* Only some NFSv4.2 servers support Seek for Holes. */
4415 		*ap->a_retval = 0;
4416 		nmp = VFSTONFS(vp->v_mount);
4417 		if (NFS_ISV4(vp) && nmp->nm_minorvers == NFSV42_MINORVERSION) {
4418 			/*
4419 			 * NFSv4.2 doesn't have an attribute for hole size,
4420 			 * so all we can do is see if the Seek operation is
4421 			 * supported and then use f_iosize as a "best guess".
4422 			 */
4423 			mtx_lock(&nmp->nm_mtx);
4424 			if ((nmp->nm_privflag & NFSMNTP_SEEKTESTED) == 0) {
4425 				mtx_unlock(&nmp->nm_mtx);
4426 				off = 0;
4427 				attrflag = 0;
4428 				error = nfsrpc_seek(vp, &off, &eof,
4429 				    NFSV4CONTENT_HOLE, td->td_ucred, &nfsva,
4430 				    &attrflag);
4431 				if (attrflag != 0)
4432 					nfscl_loadattrcache(&vp, &nfsva,
4433 					    NULL, NULL, 0, 1);
4434 				mtx_lock(&nmp->nm_mtx);
4435 				if (error == NFSERR_NOTSUPP)
4436 					nmp->nm_privflag |= NFSMNTP_SEEKTESTED;
4437 				else
4438 					nmp->nm_privflag |= NFSMNTP_SEEKTESTED |
4439 					    NFSMNTP_SEEK;
4440 				error = 0;
4441 			}
4442 			if ((nmp->nm_privflag & NFSMNTP_SEEK) != 0)
4443 				*ap->a_retval = vp->v_mount->mnt_stat.f_iosize;
4444 			mtx_unlock(&nmp->nm_mtx);
4445 		}
4446 		break;
4447 
4448 	default:
4449 		error = vop_stdpathconf(ap);
4450 		break;
4451 	}
4452 	return (error);
4453 }
4454