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