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