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