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