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