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