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