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