xref: /dragonfly/sys/vfs/nfs/nfs_vnops.c (revision d8082429)
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  * 3. 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  *	@(#)nfs_vnops.c	8.16 (Berkeley) 5/27/95
33  * $FreeBSD: src/sys/nfs/nfs_vnops.c,v 1.150.2.5 2001/12/20 19:56:28 dillon Exp $
34  */
35 
36 
37 /*
38  * vnode op calls for Sun NFS version 2 and 3
39  */
40 
41 #include "opt_inet.h"
42 
43 #include <sys/param.h>
44 #include <sys/kernel.h>
45 #include <sys/systm.h>
46 #include <sys/uio.h>
47 #include <sys/resourcevar.h>
48 #include <sys/proc.h>
49 #include <sys/mount.h>
50 #include <sys/buf.h>
51 #include <sys/malloc.h>
52 #include <sys/mbuf.h>
53 #include <sys/namei.h>
54 #include <sys/nlookup.h>
55 #include <sys/socket.h>
56 #include <sys/vnode.h>
57 #include <sys/dirent.h>
58 #include <sys/fcntl.h>
59 #include <sys/lockf.h>
60 #include <sys/stat.h>
61 #include <sys/sysctl.h>
62 #include <sys/conf.h>
63 
64 #include <vm/vm.h>
65 #include <vm/vm_extern.h>
66 
67 #include <sys/buf2.h>
68 
69 #include <vfs/fifofs/fifo.h>
70 #include <vfs/ufs/dir.h>
71 
72 #undef DIRBLKSIZ
73 
74 #include "rpcv2.h"
75 #include "nfsproto.h"
76 #include "nfs.h"
77 #include "nfsmount.h"
78 #include "nfsnode.h"
79 #include "xdr_subs.h"
80 #include "nfsm_subs.h"
81 
82 #include <net/if.h>
83 #include <netinet/in.h>
84 #include <netinet/in_var.h>
85 
86 /* Defs */
87 #define	TRUE	1
88 #define	FALSE	0
89 
90 static int	nfsfifo_read (struct vop_read_args *);
91 static int	nfsfifo_write (struct vop_write_args *);
92 static int	nfsfifo_close (struct vop_close_args *);
93 static int	nfs_setattrrpc (struct vnode *,struct vattr *,struct ucred *,struct thread *);
94 static	int	nfs_lookup (struct vop_old_lookup_args *);
95 static	int	nfs_create (struct vop_old_create_args *);
96 static	int	nfs_mknod (struct vop_old_mknod_args *);
97 static	int	nfs_open (struct vop_open_args *);
98 static	int	nfs_close (struct vop_close_args *);
99 static	int	nfs_access (struct vop_access_args *);
100 static	int	nfs_getattr (struct vop_getattr_args *);
101 static	int	nfs_setattr (struct vop_setattr_args *);
102 static	int	nfs_read (struct vop_read_args *);
103 static	int	nfs_fsync (struct vop_fsync_args *);
104 static	int	nfs_remove (struct vop_old_remove_args *);
105 static	int	nfs_link (struct vop_old_link_args *);
106 static	int	nfs_rename (struct vop_old_rename_args *);
107 static	int	nfs_mkdir (struct vop_old_mkdir_args *);
108 static	int	nfs_rmdir (struct vop_old_rmdir_args *);
109 static	int	nfs_symlink (struct vop_old_symlink_args *);
110 static	int	nfs_readdir (struct vop_readdir_args *);
111 static	int	nfs_bmap (struct vop_bmap_args *);
112 static	int	nfs_strategy (struct vop_strategy_args *);
113 static	int	nfs_lookitup (struct vnode *, const char *, int,
114 			struct ucred *, struct thread *, struct nfsnode **);
115 static	int	nfs_sillyrename (struct vnode *,struct vnode *,struct componentname *);
116 static int	nfs_laccess (struct vop_access_args *);
117 static int	nfs_readlink (struct vop_readlink_args *);
118 static int	nfs_print (struct vop_print_args *);
119 static int	nfs_advlock (struct vop_advlock_args *);
120 static int	nfs_kqfilter (struct vop_kqfilter_args *ap);
121 
122 static	int	nfs_nresolve (struct vop_nresolve_args *);
123 /*
124  * Global vfs data structures for nfs
125  */
126 struct vop_ops nfsv2_vnode_vops = {
127 	.vop_default =		vop_defaultop,
128 	.vop_access =		nfs_access,
129 	.vop_advlock =		nfs_advlock,
130 	.vop_bmap =		nfs_bmap,
131 	.vop_close =		nfs_close,
132 	.vop_old_create =	nfs_create,
133 	.vop_fsync =		nfs_fsync,
134 	.vop_getattr =		nfs_getattr,
135 	.vop_getpages =		vop_stdgetpages,
136 	.vop_putpages =		vop_stdputpages,
137 	.vop_inactive =		nfs_inactive,
138 	.vop_old_link =		nfs_link,
139 	.vop_old_lookup =	nfs_lookup,
140 	.vop_old_mkdir =	nfs_mkdir,
141 	.vop_old_mknod =	nfs_mknod,
142 	.vop_open =		nfs_open,
143 	.vop_print =		nfs_print,
144 	.vop_read =		nfs_read,
145 	.vop_readdir =		nfs_readdir,
146 	.vop_readlink =		nfs_readlink,
147 	.vop_reclaim =		nfs_reclaim,
148 	.vop_old_remove =	nfs_remove,
149 	.vop_old_rename =	nfs_rename,
150 	.vop_old_rmdir =	nfs_rmdir,
151 	.vop_setattr =		nfs_setattr,
152 	.vop_strategy =		nfs_strategy,
153 	.vop_old_symlink =	nfs_symlink,
154 	.vop_write =		nfs_write,
155 	.vop_nresolve =		nfs_nresolve,
156 	.vop_kqfilter =		nfs_kqfilter
157 };
158 
159 /*
160  * Special device vnode ops
161  */
162 struct vop_ops nfsv2_spec_vops = {
163 	.vop_default =		vop_defaultop,
164 	.vop_access =		nfs_laccess,
165 	.vop_close =		nfs_close,
166 	.vop_fsync =		nfs_fsync,
167 	.vop_getattr =		nfs_getattr,
168 	.vop_inactive =		nfs_inactive,
169 	.vop_print =		nfs_print,
170 	.vop_read =		vop_stdnoread,
171 	.vop_reclaim =		nfs_reclaim,
172 	.vop_setattr =		nfs_setattr,
173 	.vop_write =		vop_stdnowrite
174 };
175 
176 struct vop_ops nfsv2_fifo_vops = {
177 	.vop_default =		fifo_vnoperate,
178 	.vop_access =		nfs_laccess,
179 	.vop_close =		nfsfifo_close,
180 	.vop_fsync =		nfs_fsync,
181 	.vop_getattr =		nfs_getattr,
182 	.vop_inactive =		nfs_inactive,
183 	.vop_print =		nfs_print,
184 	.vop_read =		nfsfifo_read,
185 	.vop_reclaim =		nfs_reclaim,
186 	.vop_setattr =		nfs_setattr,
187 	.vop_write =		nfsfifo_write
188 };
189 
190 static int	nfs_mknodrpc (struct vnode *dvp, struct vnode **vpp,
191 				  struct componentname *cnp,
192 				  struct vattr *vap);
193 static int	nfs_removerpc (struct vnode *dvp, const char *name,
194 				   int namelen,
195 				   struct ucred *cred, struct thread *td);
196 static int	nfs_renamerpc (struct vnode *fdvp, const char *fnameptr,
197 				   int fnamelen, struct vnode *tdvp,
198 				   const char *tnameptr, int tnamelen,
199 				   struct ucred *cred, struct thread *td);
200 static int	nfs_renameit (struct vnode *sdvp,
201 				  struct componentname *scnp,
202 				  struct sillyrename *sp);
203 
204 SYSCTL_DECL(_vfs_nfs);
205 
206 static int nfs_flush_on_rename = 1;
207 SYSCTL_INT(_vfs_nfs, OID_AUTO, flush_on_rename, CTLFLAG_RW,
208 	   &nfs_flush_on_rename, 0, "flush fvp prior to rename");
209 static int nfs_flush_on_hlink = 0;
210 SYSCTL_INT(_vfs_nfs, OID_AUTO, flush_on_hlink, CTLFLAG_RW,
211 	   &nfs_flush_on_hlink, 0, "flush fvp prior to hard link");
212 
213 static int	nfsaccess_cache_timeout = NFS_DEFATTRTIMO;
214 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
215 	   &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
216 
217 static int	nfsneg_cache_timeout = NFS_MINATTRTIMO;
218 SYSCTL_INT(_vfs_nfs, OID_AUTO, neg_cache_timeout, CTLFLAG_RW,
219 	   &nfsneg_cache_timeout, 0, "NFS NEGATIVE NAMECACHE timeout");
220 
221 static int	nfspos_cache_timeout = NFS_MINATTRTIMO;
222 SYSCTL_INT(_vfs_nfs, OID_AUTO, pos_cache_timeout, CTLFLAG_RW,
223 	   &nfspos_cache_timeout, 0, "NFS POSITIVE NAMECACHE timeout");
224 
225 static int	nfsv3_commit_on_close = 0;
226 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfsv3_commit_on_close, CTLFLAG_RW,
227 	   &nfsv3_commit_on_close, 0, "write+commit on close, else only write");
228 #if 0
229 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_hits, CTLFLAG_RD,
230 	   &nfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
231 
232 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_misses, CTLFLAG_RD,
233 	   &nfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
234 #endif
235 
236 #define	NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY		\
237 			 | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE	\
238 			 | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
239 
240 static __inline
241 void
242 nfs_knote(struct vnode *vp, int flags)
243 {
244 	if (flags)
245 		KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags);
246 }
247 
248 /*
249  * Returns whether a name component is a degenerate '.' or '..'.
250  */
251 static __inline
252 int
253 nlcdegenerate(struct nlcomponent *nlc)
254 {
255 	if (nlc->nlc_namelen == 1 && nlc->nlc_nameptr[0] == '.')
256 		return(1);
257 	if (nlc->nlc_namelen == 2 &&
258 	    nlc->nlc_nameptr[0] == '.' && nlc->nlc_nameptr[1] == '.')
259 		return(1);
260 	return(0);
261 }
262 
263 static int
264 nfs3_access_otw(struct vnode *vp, int wmode,
265 		struct thread *td, struct ucred *cred)
266 {
267 	struct nfsnode *np = VTONFS(vp);
268 	int attrflag;
269 	int error = 0;
270 	u_int32_t *tl;
271 	u_int32_t rmode;
272 	struct nfsm_info info;
273 
274 	info.mrep = NULL;
275 	info.v3 = 1;
276 
277 	nfsstats.rpccnt[NFSPROC_ACCESS]++;
278 	nfsm_reqhead(&info, vp, NFSPROC_ACCESS,
279 		     NFSX_FH(info.v3) + NFSX_UNSIGNED);
280 	ERROROUT(nfsm_fhtom(&info, vp));
281 	tl = nfsm_build(&info, NFSX_UNSIGNED);
282 	*tl = txdr_unsigned(wmode);
283 	NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_ACCESS, td, cred, &error));
284 	ERROROUT(nfsm_postop_attr(&info, vp, &attrflag, NFS_LATTR_NOSHRINK));
285 	if (error == 0) {
286 		NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
287 		rmode = fxdr_unsigned(u_int32_t, *tl);
288 		np->n_mode = rmode;
289 		np->n_modeuid = cred->cr_uid;
290 		np->n_modestamp = mycpu->gd_time_seconds;
291 	}
292 	m_freem(info.mrep);
293 	info.mrep = NULL;
294 nfsmout:
295 	return error;
296 }
297 
298 /*
299  * nfs access vnode op.
300  * For nfs version 2, just return ok. File accesses may fail later.
301  * For nfs version 3, use the access rpc to check accessibility. If file modes
302  * are changed on the server, accesses might still fail later.
303  *
304  * nfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
305  */
306 static int
307 nfs_access(struct vop_access_args *ap)
308 {
309 	struct ucred *cred;
310 	struct ucred *ncred;
311 	struct ucred *ocred;
312 	struct vnode *vp = ap->a_vp;
313 	thread_t td = curthread;
314 	int error = 0;
315 	u_int32_t mode, wmode;
316 	struct nfsnode *np = VTONFS(vp);
317 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
318 	int v3 = NFS_ISV3(vp);
319 
320 	lwkt_gettoken(&nmp->nm_token);
321 
322 	/*
323 	 * Disallow write attempts on filesystems mounted read-only;
324 	 * unless the file is a socket, fifo, or a block or character
325 	 * device resident on the filesystem.
326 	 */
327 	if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
328 		switch (vp->v_type) {
329 		case VREG:
330 		case VDIR:
331 		case VLNK:
332 			lwkt_reltoken(&nmp->nm_token);
333 			return (EROFS);
334 		default:
335 			break;
336 		}
337 	}
338 
339 	/*
340 	 * The NFS protocol passes only the effective uid/gid over the wire but
341 	 * we need to check access against real ids if AT_EACCESS not set.
342 	 * Handle this case by cloning the credentials and setting the
343 	 * effective ids to the real ones.
344 	 *
345 	 * The crdup() here can cause a lot of ucred structures to build-up
346 	 * (up to maxvnodes), so do our best to avoid it.
347 	 */
348 	if (ap->a_flags & AT_EACCESS) {
349 		cred = crhold(ap->a_cred);
350 	} else {
351 		cred = ap->a_cred;
352 		if (cred->cr_uid == cred->cr_ruid &&
353 		    cred->cr_gid == cred->cr_rgid) {
354 			cred = crhold(ap->a_cred);
355 		} else {
356 			cred = crdup(ap->a_cred);
357 			cred->cr_uid = cred->cr_ruid;
358 			cred->cr_gid = cred->cr_rgid;
359 		}
360 	}
361 
362 	/*
363 	 * For nfs v3, check to see if we have done this recently, and if
364 	 * so return our cached result instead of making an ACCESS call.
365 	 * If not, do an access rpc, otherwise you are stuck emulating
366 	 * ufs_access() locally using the vattr. This may not be correct,
367 	 * since the server may apply other access criteria such as
368 	 * client uid-->server uid mapping that we do not know about.
369 	 */
370 	if (v3) {
371 		if (ap->a_mode & VREAD)
372 			mode = NFSV3ACCESS_READ;
373 		else
374 			mode = 0;
375 		if (vp->v_type != VDIR) {
376 			if (ap->a_mode & VWRITE)
377 				mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
378 			if (ap->a_mode & VEXEC)
379 				mode |= NFSV3ACCESS_EXECUTE;
380 		} else {
381 			if (ap->a_mode & VWRITE)
382 				mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
383 					 NFSV3ACCESS_DELETE);
384 			if (ap->a_mode & VEXEC)
385 				mode |= NFSV3ACCESS_LOOKUP;
386 		}
387 		/* XXX safety belt, only make blanket request if caching */
388 		if (nfsaccess_cache_timeout > 0) {
389 			wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY |
390 				NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE |
391 				NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
392 		} else {
393 			wmode = mode;
394 		}
395 
396 		/*
397 		 * Does our cached result allow us to give a definite yes to
398 		 * this request?
399 		 */
400 		if (np->n_modestamp &&
401 		   (mycpu->gd_time_seconds < (np->n_modestamp + nfsaccess_cache_timeout)) &&
402 		   (cred->cr_uid == np->n_modeuid) &&
403 		   ((np->n_mode & mode) == mode)) {
404 			nfsstats.accesscache_hits++;
405 		} else {
406 			/*
407 			 * Either a no, or a don't know.  Go to the wire.
408 			 */
409 			nfsstats.accesscache_misses++;
410 		        error = nfs3_access_otw(vp, wmode, td, cred);
411 			if (!error) {
412 				if ((np->n_mode & mode) != mode) {
413 					error = EACCES;
414 				}
415 			}
416 		}
417 	} else {
418 		if ((error = nfs_laccess(ap)) != 0) {
419 			crfree(cred);
420 			lwkt_reltoken(&nmp->nm_token);
421 			return (error);
422 		}
423 
424 		/*
425 		 * Attempt to prevent a mapped root from accessing a file
426 		 * which it shouldn't.  We try to read a byte from the file
427 		 * if the user is root and the file is not zero length.
428 		 * After calling nfs_laccess, we should have the correct
429 		 * file size cached.
430 		 */
431 		if (cred->cr_uid == 0 && (ap->a_mode & VREAD)
432 		    && VTONFS(vp)->n_size > 0) {
433 			struct iovec aiov;
434 			struct uio auio;
435 			char buf[1];
436 
437 			aiov.iov_base = buf;
438 			aiov.iov_len = 1;
439 			auio.uio_iov = &aiov;
440 			auio.uio_iovcnt = 1;
441 			auio.uio_offset = 0;
442 			auio.uio_resid = 1;
443 			auio.uio_segflg = UIO_SYSSPACE;
444 			auio.uio_rw = UIO_READ;
445 			auio.uio_td = td;
446 
447 			if (vp->v_type == VREG) {
448 				error = nfs_readrpc_uio(vp, &auio);
449 			} else if (vp->v_type == VDIR) {
450 				char* bp;
451 				bp = kmalloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
452 				aiov.iov_base = bp;
453 				aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
454 				error = nfs_readdirrpc_uio(vp, &auio);
455 				kfree(bp, M_TEMP);
456 			} else if (vp->v_type == VLNK) {
457 				error = nfs_readlinkrpc_uio(vp, &auio);
458 			} else {
459 				error = EACCES;
460 			}
461 		}
462 	}
463 	/*
464 	 * [re]record creds for reading and/or writing if access
465 	 * was granted.  Assume the NFS server will grant read access
466 	 * for execute requests.
467 	 */
468 	if (error == 0) {
469 		if ((ap->a_mode & (VREAD|VEXEC)) &&
470 		    !nfs_crsame(cred, np->n_rucred)) {
471 			ncred = nfs_crhold(cred);
472 			ocred = np->n_rucred;
473 			np->n_rucred = ncred;
474 			if (ocred)
475 				crfree(ocred);
476 		}
477 		if ((ap->a_mode & VWRITE) &&
478 		    !nfs_crsame(cred, np->n_wucred)) {
479 			ncred = nfs_crhold(cred);
480 			ocred = np->n_wucred;
481 			np->n_wucred = ncred;
482 			if (ocred)
483 				crfree(ocred);
484 		}
485 	}
486 	lwkt_reltoken(&nmp->nm_token);
487 	crfree(cred);
488 
489 	return(error);
490 }
491 
492 /*
493  * nfs open vnode op
494  * Check to see if the type is ok
495  * and that deletion is not in progress.
496  * For paged in text files, you will need to flush the page cache
497  * if consistency is lost.
498  *
499  * nfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
500  *	    struct file *a_fp)
501  */
502 /* ARGSUSED */
503 static int
504 nfs_open(struct vop_open_args *ap)
505 {
506 	struct vnode *vp = ap->a_vp;
507 	struct nfsnode *np = VTONFS(vp);
508 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
509 	struct vattr vattr;
510 	struct ucred *ncred;
511 	struct ucred *ocred;
512 	int error;
513 
514 	lwkt_gettoken(&nmp->nm_token);
515 
516 	if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) {
517 #ifdef DIAGNOSTIC
518 		kprintf("open eacces vtyp=%d\n",vp->v_type);
519 #endif
520 		lwkt_reltoken(&nmp->nm_token);
521 		return (EOPNOTSUPP);
522 	}
523 
524 	/*
525 	 * Save valid creds for reading and writing for later RPCs.
526 	 */
527 	if ((ap->a_mode & FREAD) && !nfs_crsame(ap->a_cred, np->n_rucred)) {
528 		ncred = nfs_crhold(ap->a_cred);
529 		ocred = np->n_rucred;
530 		np->n_rucred = ncred;
531 		if (ocred)
532 			crfree(ocred);
533 	}
534 	if ((ap->a_mode & FWRITE) && !nfs_crsame(ap->a_cred, np->n_wucred)) {
535 		ncred = nfs_crhold(ap->a_cred);
536 		ocred = np->n_wucred;
537 		np->n_wucred = ncred;
538 		if (ocred)
539 			crfree(ocred);
540 	}
541 
542 	/*
543 	 * Clear the attribute cache only if opening with write access.  It
544 	 * is unclear if we should do this at all here, but we certainly
545 	 * should not clear the cache unconditionally simply because a file
546 	 * is being opened.
547 	 */
548 	if (ap->a_mode & FWRITE)
549 		np->n_attrstamp = 0;
550 
551 	/*
552 	 * For normal NFS, reconcile changes made locally verses
553 	 * changes made remotely.  Note that VOP_GETATTR only goes
554 	 * to the wire if the cached attribute has timed out or been
555 	 * cleared.
556 	 *
557 	 * If local modifications have been made clear the attribute
558 	 * cache to force an attribute and modified time check.  If
559 	 * GETATTR detects that the file has been changed by someone
560 	 * other then us it will set NRMODIFIED.
561 	 *
562 	 * If we are opening a directory and local changes have been
563 	 * made we have to invalidate the cache in order to ensure
564 	 * that we get the most up-to-date information from the
565 	 * server.  XXX
566 	 */
567 	if (np->n_flag & NLMODIFIED) {
568 		np->n_attrstamp = 0;
569 		if (vp->v_type == VDIR) {
570 			error = nfs_vinvalbuf(vp, V_SAVE, 1);
571 			if (error == EINTR) {
572 				lwkt_reltoken(&nmp->nm_token);
573 				return (error);
574 			}
575 			nfs_invaldir(vp);
576 		}
577 	}
578 	error = VOP_GETATTR(vp, &vattr);
579 	if (error) {
580 		lwkt_reltoken(&nmp->nm_token);
581 		return (error);
582 	}
583 	if (np->n_flag & NRMODIFIED) {
584 		if (vp->v_type == VDIR)
585 			nfs_invaldir(vp);
586 		error = nfs_vinvalbuf(vp, V_SAVE, 1);
587 		if (error == EINTR) {
588 			lwkt_reltoken(&nmp->nm_token);
589 			return (error);
590 		}
591 		np->n_flag &= ~NRMODIFIED;
592 	}
593 	error = vop_stdopen(ap);
594 	lwkt_reltoken(&nmp->nm_token);
595 
596 	return error;
597 }
598 
599 /*
600  * nfs close vnode op
601  * What an NFS client should do upon close after writing is a debatable issue.
602  * Most NFS clients push delayed writes to the server upon close, basically for
603  * two reasons:
604  * 1 - So that any write errors may be reported back to the client process
605  *     doing the close system call. By far the two most likely errors are
606  *     NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
607  * 2 - To put a worst case upper bound on cache inconsistency between
608  *     multiple clients for the file.
609  * There is also a consistency problem for Version 2 of the protocol w.r.t.
610  * not being able to tell if other clients are writing a file concurrently,
611  * since there is no way of knowing if the changed modify time in the reply
612  * is only due to the write for this client.
613  * (NFS Version 3 provides weak cache consistency data in the reply that
614  *  should be sufficient to detect and handle this case.)
615  *
616  * The current code does the following:
617  * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
618  * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
619  *                     or commit them (this satisfies 1 and 2 except for the
620  *                     case where the server crashes after this close but
621  *                     before the commit RPC, which is felt to be "good
622  *                     enough". Changing the last argument to nfs_flush() to
623  *                     a 1 would force a commit operation, if it is felt a
624  *                     commit is necessary now.
625  * for NQNFS         - do nothing now, since 2 is dealt with via leases and
626  *                     1 should be dealt with via an fsync() system call for
627  *                     cases where write errors are important.
628  *
629  * nfs_close(struct vnode *a_vp, int a_fflag)
630  */
631 /* ARGSUSED */
632 static int
633 nfs_close(struct vop_close_args *ap)
634 {
635 	struct vnode *vp = ap->a_vp;
636 	struct nfsnode *np = VTONFS(vp);
637 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
638 	int error = 0;
639 	thread_t td = curthread;
640 
641 	vn_lock(vp, LK_UPGRADE | LK_RETRY); /* XXX */
642 	lwkt_gettoken(&nmp->nm_token);
643 
644 	if (vp->v_type == VREG) {
645 	    if (np->n_flag & NLMODIFIED) {
646 		if (NFS_ISV3(vp)) {
647 		    /*
648 		     * Under NFSv3 we have dirty buffers to dispose of.  We
649 		     * must flush them to the NFS server.  We have the option
650 		     * of waiting all the way through the commit rpc or just
651 		     * waiting for the initial write.  The default is to only
652 		     * wait through the initial write so the data is in the
653 		     * server's cache, which is roughly similar to the state
654 		     * a standard disk subsystem leaves the file in on close().
655 		     *
656 		     * We cannot clear the NLMODIFIED bit in np->n_flag due to
657 		     * potential races with other processes, and certainly
658 		     * cannot clear it if we don't commit.
659 		     */
660 		    int cm = nfsv3_commit_on_close ? 1 : 0;
661 		    error = nfs_flush(vp, MNT_WAIT, td, cm);
662 		    /* np->n_flag &= ~NLMODIFIED; */
663 		} else {
664 		    error = nfs_vinvalbuf(vp, V_SAVE, 1);
665 		}
666 		np->n_attrstamp = 0;
667 	    }
668 	    if (np->n_flag & NWRITEERR) {
669 		np->n_flag &= ~NWRITEERR;
670 		error = np->n_error;
671 	    }
672 	}
673 	vop_stdclose(ap);
674 	lwkt_reltoken(&nmp->nm_token);
675 
676 	return (error);
677 }
678 
679 /*
680  * nfs getattr call from vfs.
681  *
682  * nfs_getattr(struct vnode *a_vp, struct vattr *a_vap)
683  */
684 static int
685 nfs_getattr(struct vop_getattr_args *ap)
686 {
687 	struct vnode *vp = ap->a_vp;
688 	struct nfsnode *np = VTONFS(vp);
689 	struct nfsmount *nmp;
690 	int error = 0;
691 	thread_t td = curthread;
692 	struct nfsm_info info;
693 
694 	info.mrep = NULL;
695 	info.v3 = NFS_ISV3(vp);
696 	nmp = VFSTONFS(vp->v_mount);
697 
698 	lwkt_gettoken(&nmp->nm_token);
699 
700 	/*
701 	 * Update local times for special files.
702 	 */
703 	if (np->n_flag & (NACC | NUPD))
704 		np->n_flag |= NCHG;
705 	/*
706 	 * First look in the cache.
707 	 */
708 	if (nfs_getattrcache(vp, ap->a_vap) == 0)
709 		goto done;
710 
711 	if (info.v3 && nfsaccess_cache_timeout > 0) {
712 		nfsstats.accesscache_misses++;
713 		nfs3_access_otw(vp, NFSV3ACCESS_ALL, td, nfs_vpcred(vp, ND_CHECK));
714 		if (nfs_getattrcache(vp, ap->a_vap) == 0)
715 			goto done;
716 	}
717 
718 	nfsstats.rpccnt[NFSPROC_GETATTR]++;
719 	nfsm_reqhead(&info, vp, NFSPROC_GETATTR, NFSX_FH(info.v3));
720 	ERROROUT(nfsm_fhtom(&info, vp));
721 	NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_GETATTR, td,
722 				nfs_vpcred(vp, ND_CHECK), &error));
723 	if (error == 0) {
724 		ERROROUT(nfsm_loadattr(&info, vp, ap->a_vap));
725 	}
726 	m_freem(info.mrep);
727 	info.mrep = NULL;
728 done:
729 	/*
730 	 * NFS doesn't support chflags flags.  If the nfs mount was
731 	 * made -o cache set the UF_CACHE bit for swapcache.
732 	 */
733 	if ((nmp->nm_flag & NFSMNT_CACHE) && (vp->v_flag & VROOT))
734 		ap->a_vap->va_flags |= UF_CACHE;
735 nfsmout:
736 	lwkt_reltoken(&nmp->nm_token);
737 	return (error);
738 }
739 
740 /*
741  * nfs setattr call.
742  *
743  * nfs_setattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred)
744  */
745 static int
746 nfs_setattr(struct vop_setattr_args *ap)
747 {
748 	struct vnode *vp = ap->a_vp;
749 	struct nfsnode *np = VTONFS(vp);
750 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
751 	struct vattr *vap = ap->a_vap;
752 	int error = 0;
753 	int kflags = 0;
754 	off_t tsize;
755 	thread_t td = curthread;
756 
757 #ifndef nolint
758 	tsize = (off_t)0;
759 #endif
760 	/*
761 	 * Setting of flags is not supported.
762 	 */
763 	if (vap->va_flags != VNOVAL)
764 		return (EOPNOTSUPP);
765 
766 	/*
767 	 * Disallow write attempts if the filesystem is mounted read-only.
768 	 */
769   	if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
770 	    vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
771 	    vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
772 	    (vp->v_mount->mnt_flag & MNT_RDONLY))
773 		return (EROFS);
774 
775 	lwkt_gettoken(&nmp->nm_token);
776 
777 	/*
778 	 * Handle size changes
779 	 */
780 	if (vap->va_size != VNOVAL) {
781 		/*
782 		 * truncation requested
783 		 */
784  		switch (vp->v_type) {
785  		case VDIR:
786 			lwkt_reltoken(&nmp->nm_token);
787  			return (EISDIR);
788  		case VCHR:
789  		case VBLK:
790  		case VSOCK:
791  		case VFIFO:
792 			if (vap->va_mtime.tv_sec == VNOVAL &&
793 			    vap->va_atime.tv_sec == VNOVAL &&
794 			    vap->va_mode == (mode_t)VNOVAL &&
795 			    vap->va_uid == (uid_t)VNOVAL &&
796 			    vap->va_gid == (gid_t)VNOVAL) {
797 				lwkt_reltoken(&nmp->nm_token);
798 				return (0);
799 			}
800  			vap->va_size = VNOVAL;
801  			break;
802  		default:
803 			/*
804 			 * Disallow write attempts if the filesystem is
805 			 * mounted read-only.
806 			 */
807 			if (vp->v_mount->mnt_flag & MNT_RDONLY) {
808 				lwkt_reltoken(&nmp->nm_token);
809 				return (EROFS);
810 			}
811 
812 			tsize = np->n_size;
813 again:
814 			error = nfs_meta_setsize(vp, td, vap->va_size, 0);
815 
816 #if 0
817  			if (np->n_flag & NLMODIFIED) {
818  			    if (vap->va_size == 0)
819  				error = nfs_vinvalbuf(vp, 0, 1);
820  			    else
821  				error = nfs_vinvalbuf(vp, V_SAVE, 1);
822  			}
823 #endif
824 			/*
825 			 * note: this loop case almost always happens at
826 			 * least once per truncation.
827 			 */
828 			if (error == 0 && np->n_size != vap->va_size)
829 				goto again;
830 			np->n_vattr.va_size = vap->va_size;
831 			kflags |= NOTE_WRITE;
832 			if (tsize < vap->va_size)
833 				kflags |= NOTE_EXTEND;
834 			break;
835 		}
836 	}
837 
838 	/*
839 	 * If setting the mtime or if server/other-client modifications have
840 	 * been detected, we must fully flush any pending writes.
841 	 *
842 	 * This will slow down cp/cpdup/rdist/rsync and other operations which
843 	 * might call [l]utimes() to set the mtime after writing to a file,
844 	 * but honestly there is no way to properly defer the write flush
845 	 * and still get reasonably accurate/dependable synchronization of
846 	 * [l]utimes().
847 	 */
848 	if ((np->n_flag & NLMODIFIED) && vp->v_type == VREG) {
849 		if ((np->n_flag & NRMODIFIED) ||
850 		    (vap->va_mtime.tv_sec != VNOVAL)) {
851 			error = nfs_vinvalbuf(vp, V_SAVE, 1);
852 			if (error == EINTR) {
853 				lwkt_reltoken(&nmp->nm_token);
854 				return (error);
855 			}
856 		}
857 	}
858 
859 	/*
860 	 * Get the blasted mtime to report properly.
861 	 */
862 	if (vap->va_mtime.tv_sec != VNOVAL) {
863 		np->n_mtime = vap->va_mtime.tv_sec;
864 		np->n_flag &= ~NUPD;
865 		np->n_vattr.va_mtime = vap->va_mtime;
866 	}
867 
868 	/*
869 	 * Issue the setattr rpc, adjust our mtime and make sure NUPD
870 	 * has been cleared so it does not get overridden.
871 	 */
872 	error = nfs_setattrrpc(vp, vap, ap->a_cred, td);
873 	if (error == 0)
874 		kflags |= NOTE_EXTEND;
875 
876 	/*
877 	 * Sanity check if a truncation was issued.  This should only occur
878 	 * if multiple processes are racing on the same file.
879 	 */
880 	if (error == 0 && vap->va_size != VNOVAL &&
881 	    np->n_size != vap->va_size) {
882 		kprintf("NFS ftruncate: server disagrees on the file size: "
883 			"%jd/%jd/%jd\n",
884 			(intmax_t)tsize,
885 			(intmax_t)vap->va_size,
886 			(intmax_t)np->n_size);
887 		goto again;
888 	}
889 	if (error && vap->va_size != VNOVAL) {
890 		np->n_size = np->n_vattr.va_size = tsize;
891 		nfs_meta_setsize(vp, td, np->n_size, 0);
892 	}
893 	lwkt_reltoken(&nmp->nm_token);
894 	nfs_knote(vp, kflags);
895 
896 	return (error);
897 }
898 
899 /*
900  * Do an nfs setattr rpc.
901  */
902 static int
903 nfs_setattrrpc(struct vnode *vp, struct vattr *vap,
904 	       struct ucred *cred, struct thread *td)
905 {
906 	struct nfsv2_sattr *sp;
907 	struct nfsnode *np = VTONFS(vp);
908 	u_int32_t *tl;
909 	int error = 0, wccflag = NFSV3_WCCRATTR;
910 	struct nfsm_info info;
911 
912 	info.mrep = NULL;
913 	info.v3 = NFS_ISV3(vp);
914 
915 	nfsstats.rpccnt[NFSPROC_SETATTR]++;
916 	nfsm_reqhead(&info, vp, NFSPROC_SETATTR,
917 		     NFSX_FH(info.v3) + NFSX_SATTR(info.v3));
918 	ERROROUT(nfsm_fhtom(&info, vp));
919 	if (info.v3) {
920 		nfsm_v3attrbuild(&info, vap, TRUE);
921 		tl = nfsm_build(&info, NFSX_UNSIGNED);
922 		*tl = nfs_false;
923 	} else {
924 		sp = nfsm_build(&info, NFSX_V2SATTR);
925 		if (vap->va_mode == (mode_t)VNOVAL)
926 			sp->sa_mode = nfs_xdrneg1;
927 		else
928 			sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
929 		if (vap->va_uid == (uid_t)VNOVAL)
930 			sp->sa_uid = nfs_xdrneg1;
931 		else
932 			sp->sa_uid = txdr_unsigned(vap->va_uid);
933 		if (vap->va_gid == (gid_t)VNOVAL)
934 			sp->sa_gid = nfs_xdrneg1;
935 		else
936 			sp->sa_gid = txdr_unsigned(vap->va_gid);
937 		sp->sa_size = txdr_unsigned(vap->va_size);
938 		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
939 		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
940 	}
941 	NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_SETATTR, td, cred, &error));
942 	if (info.v3) {
943 		np->n_modestamp = 0;
944 		ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
945 	} else {
946 		ERROROUT(nfsm_loadattr(&info, vp, NULL));
947 	}
948 	m_freem(info.mrep);
949 	info.mrep = NULL;
950 nfsmout:
951 	return (error);
952 }
953 
954 static
955 void
956 nfs_cache_setvp(struct nchandle *nch, struct vnode *vp, int nctimeout)
957 {
958 	if (nctimeout == 0)
959 		nctimeout = 1;
960 	else
961 		nctimeout *= hz;
962 	cache_setvp(nch, vp);
963 	cache_settimeout(nch, nctimeout);
964 }
965 
966 /*
967  * NEW API CALL - replaces nfs_lookup().  However, we cannot remove
968  * nfs_lookup() until all remaining new api calls are implemented.
969  *
970  * Resolve a namecache entry.  This function is passed a locked ncp and
971  * must call nfs_cache_setvp() on it as appropriate to resolve the entry.
972  */
973 static int
974 nfs_nresolve(struct vop_nresolve_args *ap)
975 {
976 	struct thread *td = curthread;
977 	struct namecache *ncp;
978 	struct nfsmount *nmp;
979 	struct nfsnode *np;
980 	struct vnode *dvp;
981 	struct vnode *nvp;
982 	nfsfh_t *fhp;
983 	int attrflag;
984 	int fhsize;
985 	int error;
986 	int tmp_error;
987 	int len;
988 	struct nfsm_info info;
989 
990 	dvp = ap->a_dvp;
991 	nmp = VFSTONFS(dvp->v_mount);
992 
993 	lwkt_gettoken(&nmp->nm_token);
994 
995 	if ((error = vget(dvp, LK_SHARED)) != 0) {
996 		lwkt_reltoken(&nmp->nm_token);
997 		return (error);
998 	}
999 
1000 	info.mrep = NULL;
1001 	info.v3 = NFS_ISV3(dvp);
1002 
1003 	nvp = NULL;
1004 	nfsstats.lookupcache_misses++;
1005 	nfsstats.rpccnt[NFSPROC_LOOKUP]++;
1006 	ncp = ap->a_nch->ncp;
1007 	len = ncp->nc_nlen;
1008 	nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP,
1009 		     NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len));
1010 	ERROROUT(nfsm_fhtom(&info, dvp));
1011 	ERROROUT(nfsm_strtom(&info, ncp->nc_name, len, NFS_MAXNAMLEN));
1012 	NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, td,
1013 				ap->a_cred, &error));
1014 	if (error) {
1015 		/*
1016 		 * Cache negatve lookups to reduce NFS traffic, but use
1017 		 * a fast timeout.  Otherwise use a timeout of 1 tick.
1018 		 * XXX we should add a namecache flag for no-caching
1019 		 * to uncache the negative hit as soon as possible, but
1020 		 * we cannot simply destroy the entry because it is used
1021 		 * as a placeholder by the caller.
1022 		 *
1023 		 * The refactored nfs code will overwrite a non-zero error
1024 		 * with 0 when we use ERROROUT(), so don't here.
1025 		 */
1026 		if (error == ENOENT)
1027 			nfs_cache_setvp(ap->a_nch, NULL, nfsneg_cache_timeout);
1028 		tmp_error = nfsm_postop_attr(&info, dvp, &attrflag,
1029 					     NFS_LATTR_NOSHRINK);
1030 		if (tmp_error) {
1031 			error = tmp_error;
1032 			goto nfsmout;
1033 		}
1034 		m_freem(info.mrep);
1035 		info.mrep = NULL;
1036 		goto nfsmout;
1037 	}
1038 
1039 	/*
1040 	 * Success, get the file handle, do various checks, and load
1041 	 * post-operation data from the reply packet.  Theoretically
1042 	 * we should never be looking up "." so, theoretically, we
1043 	 * should never get the same file handle as our directory.  But
1044 	 * we check anyway. XXX
1045 	 *
1046 	 * Note that no timeout is set for the positive cache hit.  We
1047 	 * assume, theoretically, that ESTALE returns will be dealt with
1048 	 * properly to handle NFS races and in anycase we cannot depend
1049 	 * on a timeout to deal with NFS open/create/excl issues so instead
1050 	 * of a bad hack here the rest of the NFS client code needs to do
1051 	 * the right thing.
1052 	 */
1053 	NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp));
1054 
1055 	np = VTONFS(dvp);
1056 	if (NFS_CMPFH(np, fhp, fhsize)) {
1057 		vref(dvp);
1058 		nvp = dvp;
1059 	} else {
1060 		error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, NULL);
1061 		if (error) {
1062 			m_freem(info.mrep);
1063 			info.mrep = NULL;
1064 			vput(dvp);
1065 			lwkt_reltoken(&nmp->nm_token);
1066 			return (error);
1067 		}
1068 		nvp = NFSTOV(np);
1069 	}
1070 	if (info.v3) {
1071 		ERROROUT(nfsm_postop_attr(&info, nvp, &attrflag,
1072 					  NFS_LATTR_NOSHRINK));
1073 		ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag,
1074 					  NFS_LATTR_NOSHRINK));
1075 	} else {
1076 		ERROROUT(nfsm_loadattr(&info, nvp, NULL));
1077 	}
1078 	nfs_cache_setvp(ap->a_nch, nvp, nfspos_cache_timeout);
1079 	m_freem(info.mrep);
1080 	info.mrep = NULL;
1081 nfsmout:
1082 	lwkt_reltoken(&nmp->nm_token);
1083 	vput(dvp);
1084 	if (nvp) {
1085 		if (nvp == dvp)
1086 			vrele(nvp);
1087 		else
1088 			vput(nvp);
1089 	}
1090 	return (error);
1091 }
1092 
1093 /*
1094  * 'cached' nfs directory lookup
1095  *
1096  * NOTE: cannot be removed until NFS implements all the new n*() API calls.
1097  *
1098  * nfs_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
1099  *	      struct componentname *a_cnp)
1100  */
1101 static int
1102 nfs_lookup(struct vop_old_lookup_args *ap)
1103 {
1104 	struct componentname *cnp = ap->a_cnp;
1105 	struct vnode *dvp = ap->a_dvp;
1106 	struct vnode **vpp = ap->a_vpp;
1107 	int flags = cnp->cn_flags;
1108 	struct vnode *newvp;
1109 	struct vnode *notvp;
1110 	struct nfsmount *nmp;
1111 	long len;
1112 	nfsfh_t *fhp;
1113 	struct nfsnode *np;
1114 	int lockparent, wantparent, attrflag, fhsize;
1115 	int error;
1116 	int tmp_error;
1117 	struct nfsm_info info;
1118 
1119 	info.mrep = NULL;
1120 	info.v3 = NFS_ISV3(dvp);
1121 	error = 0;
1122 
1123 	notvp = (cnp->cn_flags & CNP_NOTVP) ? cnp->cn_notvp : NULL;
1124 
1125 	/*
1126 	 * Read-only mount check and directory check.
1127 	 */
1128 	*vpp = NULLVP;
1129 	if ((dvp->v_mount->mnt_flag & MNT_RDONLY) &&
1130 	    (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME))
1131 		return (EROFS);
1132 
1133 	if (dvp->v_type != VDIR)
1134 		return (ENOTDIR);
1135 
1136 	/*
1137 	 * Look it up in the cache.  Note that ENOENT is only returned if we
1138 	 * previously entered a negative hit (see later on).  The additional
1139 	 * nfsneg_cache_timeout check causes previously cached results to
1140 	 * be instantly ignored if the negative caching is turned off.
1141 	 */
1142 	lockparent = flags & CNP_LOCKPARENT;
1143 	wantparent = flags & (CNP_LOCKPARENT|CNP_WANTPARENT);
1144 	nmp = VFSTONFS(dvp->v_mount);
1145 	np = VTONFS(dvp);
1146 
1147 	lwkt_gettoken(&nmp->nm_token);
1148 
1149 	/*
1150 	 * Go to the wire.
1151 	 */
1152 	error = 0;
1153 	newvp = NULLVP;
1154 	nfsstats.lookupcache_misses++;
1155 	nfsstats.rpccnt[NFSPROC_LOOKUP]++;
1156 	len = cnp->cn_namelen;
1157 	nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP,
1158 		     NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len));
1159 	ERROROUT(nfsm_fhtom(&info, dvp));
1160 	ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, len, NFS_MAXNAMLEN));
1161 	NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, cnp->cn_td,
1162 				cnp->cn_cred, &error));
1163 	if (error) {
1164 		tmp_error = nfsm_postop_attr(&info, dvp, &attrflag,
1165 					     NFS_LATTR_NOSHRINK);
1166 		if (tmp_error) {
1167 			error = tmp_error;
1168 			goto nfsmout;
1169 		}
1170 
1171 		m_freem(info.mrep);
1172 		info.mrep = NULL;
1173 		goto nfsmout;
1174 	}
1175 	NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp));
1176 
1177 	/*
1178 	 * Handle RENAME case...
1179 	 */
1180 	if (cnp->cn_nameiop == NAMEI_RENAME && wantparent) {
1181 		if (NFS_CMPFH(np, fhp, fhsize)) {
1182 			m_freem(info.mrep);
1183 			info.mrep = NULL;
1184 			lwkt_reltoken(&nmp->nm_token);
1185 			return (EISDIR);
1186 		}
1187 		error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, notvp);
1188 		if (error) {
1189 			m_freem(info.mrep);
1190 			info.mrep = NULL;
1191 			lwkt_reltoken(&nmp->nm_token);
1192 			return (error);
1193 		}
1194 		newvp = NFSTOV(np);
1195 		if (info.v3) {
1196 			ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
1197 						  NFS_LATTR_NOSHRINK));
1198 			ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag,
1199 						  NFS_LATTR_NOSHRINK));
1200 		} else {
1201 			ERROROUT(nfsm_loadattr(&info, newvp, NULL));
1202 		}
1203 		*vpp = newvp;
1204 		m_freem(info.mrep);
1205 		info.mrep = NULL;
1206 		if (!lockparent) {
1207 			vn_unlock(dvp);
1208 			cnp->cn_flags |= CNP_PDIRUNLOCK;
1209 		}
1210 		lwkt_reltoken(&nmp->nm_token);
1211 		return (0);
1212 	}
1213 
1214 	if (flags & CNP_ISDOTDOT) {
1215 		vn_unlock(dvp);
1216 		cnp->cn_flags |= CNP_PDIRUNLOCK;
1217 		error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, notvp);
1218 		if (error) {
1219 			vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1220 			cnp->cn_flags &= ~CNP_PDIRUNLOCK;
1221 			lwkt_reltoken(&nmp->nm_token);
1222 			return (error); /* NOTE: return error from nget */
1223 		}
1224 		newvp = NFSTOV(np);
1225 		if (lockparent) {
1226 			error = vn_lock(dvp, LK_EXCLUSIVE | LK_FAILRECLAIM);
1227 			if (error) {
1228 				vput(newvp);
1229 				lwkt_reltoken(&nmp->nm_token);
1230 				return (error);
1231 			}
1232 			cnp->cn_flags |= CNP_PDIRUNLOCK;
1233 		}
1234 	} else if (NFS_CMPFH(np, fhp, fhsize)) {
1235 		vref(dvp);
1236 		newvp = dvp;
1237 	} else {
1238 		error = nfs_nget(dvp->v_mount, fhp, fhsize, &np, notvp);
1239 		if (error) {
1240 			m_freem(info.mrep);
1241 			info.mrep = NULL;
1242 			lwkt_reltoken(&nmp->nm_token);
1243 			return (error);
1244 		}
1245 		if (!lockparent) {
1246 			vn_unlock(dvp);
1247 			cnp->cn_flags |= CNP_PDIRUNLOCK;
1248 		}
1249 		newvp = NFSTOV(np);
1250 	}
1251 	if (info.v3) {
1252 		ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
1253 					  NFS_LATTR_NOSHRINK));
1254 		ERROROUT(nfsm_postop_attr(&info, dvp, &attrflag,
1255 					  NFS_LATTR_NOSHRINK));
1256 	} else {
1257 		ERROROUT(nfsm_loadattr(&info, newvp, NULL));
1258 	}
1259 #if 0
1260 	/* XXX MOVE TO nfs_nremove() */
1261 	if ((cnp->cn_flags & CNP_MAKEENTRY) &&
1262 	    cnp->cn_nameiop != NAMEI_DELETE) {
1263 		np->n_ctime = np->n_vattr.va_ctime.tv_sec; /* XXX */
1264 	}
1265 #endif
1266 	*vpp = newvp;
1267 	m_freem(info.mrep);
1268 	info.mrep = NULL;
1269 nfsmout:
1270 	if (error) {
1271 		if (newvp != NULLVP) {
1272 			vrele(newvp);
1273 			*vpp = NULLVP;
1274 		}
1275 		if ((cnp->cn_nameiop == NAMEI_CREATE ||
1276 		     cnp->cn_nameiop == NAMEI_RENAME) &&
1277 		    error == ENOENT) {
1278 			if (!lockparent) {
1279 				vn_unlock(dvp);
1280 				cnp->cn_flags |= CNP_PDIRUNLOCK;
1281 			}
1282 			if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1283 				error = EROFS;
1284 			else
1285 				error = EJUSTRETURN;
1286 		}
1287 	}
1288 	lwkt_reltoken(&nmp->nm_token);
1289 	return (error);
1290 }
1291 
1292 /*
1293  * nfs read call.
1294  * Just call nfs_bioread() to do the work.
1295  *
1296  * nfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1297  *	    struct ucred *a_cred)
1298  */
1299 static int
1300 nfs_read(struct vop_read_args *ap)
1301 {
1302 	struct vnode *vp = ap->a_vp;
1303 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1304 	int error;
1305 
1306 	lwkt_gettoken(&nmp->nm_token);
1307 	error = nfs_bioread(vp, ap->a_uio, ap->a_ioflag);
1308 	lwkt_reltoken(&nmp->nm_token);
1309 
1310 	return error;
1311 }
1312 
1313 /*
1314  * nfs readlink call
1315  *
1316  * nfs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1317  */
1318 static int
1319 nfs_readlink(struct vop_readlink_args *ap)
1320 {
1321 	struct vnode *vp = ap->a_vp;
1322 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1323 	int error;
1324 
1325 	if (vp->v_type != VLNK)
1326 		return (EINVAL);
1327 
1328 	lwkt_gettoken(&nmp->nm_token);
1329 	error = nfs_bioread(vp, ap->a_uio, 0);
1330 	lwkt_reltoken(&nmp->nm_token);
1331 
1332 	return error;
1333 }
1334 
1335 /*
1336  * Do a readlink rpc.
1337  * Called by nfs_doio() from below the buffer cache.
1338  */
1339 int
1340 nfs_readlinkrpc_uio(struct vnode *vp, struct uio *uiop)
1341 {
1342 	int error = 0, len, attrflag;
1343 	struct nfsm_info info;
1344 
1345 	info.mrep = NULL;
1346 	info.v3 = NFS_ISV3(vp);
1347 
1348 	nfsstats.rpccnt[NFSPROC_READLINK]++;
1349 	nfsm_reqhead(&info, vp, NFSPROC_READLINK, NFSX_FH(info.v3));
1350 	ERROROUT(nfsm_fhtom(&info, vp));
1351 	NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READLINK, uiop->uio_td,
1352 				nfs_vpcred(vp, ND_CHECK), &error));
1353 	if (info.v3) {
1354 		ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
1355 					  NFS_LATTR_NOSHRINK));
1356 	}
1357 	if (!error) {
1358 		NEGATIVEOUT(len = nfsm_strsiz(&info, NFS_MAXPATHLEN));
1359 		if (len == NFS_MAXPATHLEN) {
1360 			struct nfsnode *np = VTONFS(vp);
1361 			if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1362 				len = np->n_size;
1363 		}
1364 		ERROROUT(nfsm_mtouio(&info, uiop, len));
1365 	}
1366 	m_freem(info.mrep);
1367 	info.mrep = NULL;
1368 nfsmout:
1369 	return (error);
1370 }
1371 
1372 /*
1373  * nfs synchronous read rpc using UIO
1374  */
1375 int
1376 nfs_readrpc_uio(struct vnode *vp, struct uio *uiop)
1377 {
1378 	u_int32_t *tl;
1379 	struct nfsmount *nmp;
1380 	int error = 0, len, retlen, tsiz, eof, attrflag;
1381 	struct nfsm_info info;
1382 	off_t tmp_off;
1383 
1384 	info.mrep = NULL;
1385 	info.v3 = NFS_ISV3(vp);
1386 
1387 #ifndef nolint
1388 	eof = 0;
1389 #endif
1390 	nmp = VFSTONFS(vp->v_mount);
1391 
1392 	tsiz = uiop->uio_resid;
1393 	tmp_off = uiop->uio_offset + tsiz;
1394 	if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset)
1395 		return (EFBIG);
1396 	tmp_off = uiop->uio_offset;
1397 	while (tsiz > 0) {
1398 		nfsstats.rpccnt[NFSPROC_READ]++;
1399 		len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1400 		nfsm_reqhead(&info, vp, NFSPROC_READ,
1401 			     NFSX_FH(info.v3) + NFSX_UNSIGNED * 3);
1402 		ERROROUT(nfsm_fhtom(&info, vp));
1403 		tl = nfsm_build(&info, NFSX_UNSIGNED * 3);
1404 		if (info.v3) {
1405 			txdr_hyper(uiop->uio_offset, tl);
1406 			*(tl + 2) = txdr_unsigned(len);
1407 		} else {
1408 			*tl++ = txdr_unsigned(uiop->uio_offset);
1409 			*tl++ = txdr_unsigned(len);
1410 			*tl = 0;
1411 		}
1412 		NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READ, uiop->uio_td,
1413 					nfs_vpcred(vp, ND_READ), &error));
1414 		if (info.v3) {
1415 			ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
1416 						 NFS_LATTR_NOSHRINK));
1417 			NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
1418 			eof = fxdr_unsigned(int, *(tl + 1));
1419 		} else {
1420 			ERROROUT(nfsm_loadattr(&info, vp, NULL));
1421 		}
1422 		NEGATIVEOUT(retlen = nfsm_strsiz(&info, len));
1423 		ERROROUT(nfsm_mtouio(&info, uiop, retlen));
1424 		m_freem(info.mrep);
1425 		info.mrep = NULL;
1426 
1427 		/*
1428 		 * Handle short-read from server (NFSv3).  If EOF is not
1429 		 * flagged (and no error occurred), but retlen is less
1430 		 * then the request size, we must zero-fill the remainder.
1431 		 */
1432 		if (retlen < len && info.v3 && eof == 0) {
1433 			ERROROUT(uiomovez(len - retlen, uiop));
1434 			retlen = len;
1435 		}
1436 		tsiz -= retlen;
1437 
1438 		/*
1439 		 * Terminate loop on EOF or zero-length read.
1440 		 *
1441 		 * For NFSv2 a short-read indicates EOF, not zero-fill,
1442 		 * and also terminates the loop.
1443 		 */
1444 		if (info.v3) {
1445 			if (eof || retlen == 0)
1446 				tsiz = 0;
1447 		} else if (retlen < len) {
1448 			tsiz = 0;
1449 		}
1450 	}
1451 nfsmout:
1452 	return (error);
1453 }
1454 
1455 /*
1456  * nfs write call
1457  */
1458 int
1459 nfs_writerpc_uio(struct vnode *vp, struct uio *uiop,
1460 		 int *iomode, int *must_commit)
1461 {
1462 	u_int32_t *tl;
1463 	int32_t backup;
1464 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1465 	int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1466 	int  committed = NFSV3WRITE_FILESYNC;
1467 	struct nfsm_info info;
1468 
1469 	info.mrep = NULL;
1470 	info.v3 = NFS_ISV3(vp);
1471 
1472 #ifndef DIAGNOSTIC
1473 	if (uiop->uio_iovcnt != 1)
1474 		panic("nfs: writerpc iovcnt > 1");
1475 #endif
1476 	*must_commit = 0;
1477 	tsiz = uiop->uio_resid;
1478 	if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1479 		return (EFBIG);
1480 	while (tsiz > 0) {
1481 		nfsstats.rpccnt[NFSPROC_WRITE]++;
1482 		len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1483 		nfsm_reqhead(&info, vp, NFSPROC_WRITE,
1484 			     NFSX_FH(info.v3) + 5 * NFSX_UNSIGNED +
1485 			     nfsm_rndup(len));
1486 		ERROROUT(nfsm_fhtom(&info, vp));
1487 		if (info.v3) {
1488 			tl = nfsm_build(&info, 5 * NFSX_UNSIGNED);
1489 			txdr_hyper(uiop->uio_offset, tl);
1490 			tl += 2;
1491 			*tl++ = txdr_unsigned(len);
1492 			*tl++ = txdr_unsigned(*iomode);
1493 			*tl = txdr_unsigned(len);
1494 		} else {
1495 			u_int32_t x;
1496 
1497 			tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
1498 			/* Set both "begin" and "current" to non-garbage. */
1499 			x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1500 			*tl++ = x;	/* "begin offset" */
1501 			*tl++ = x;	/* "current offset" */
1502 			x = txdr_unsigned(len);
1503 			*tl++ = x;	/* total to this offset */
1504 			*tl = x;	/* size of this write */
1505 		}
1506 		ERROROUT(nfsm_uiotom(&info, uiop, len));
1507 		NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_WRITE, uiop->uio_td,
1508 					nfs_vpcred(vp, ND_WRITE), &error));
1509 		if (info.v3) {
1510 			/*
1511 			 * The write RPC returns a before and after mtime.  The
1512 			 * nfsm_wcc_data() macro checks the before n_mtime
1513 			 * against the before time and stores the after time
1514 			 * in the nfsnode's cached vattr and n_mtime field.
1515 			 * The NRMODIFIED bit will be set if the before
1516 			 * time did not match the original mtime.
1517 			 */
1518 			wccflag = NFSV3_WCCCHK;
1519 			ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
1520 			if (error == 0) {
1521 				NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED + NFSX_V3WRITEVERF));
1522 				rlen = fxdr_unsigned(int, *tl++);
1523 				if (rlen == 0) {
1524 					error = NFSERR_IO;
1525 					m_freem(info.mrep);
1526 					info.mrep = NULL;
1527 					break;
1528 				} else if (rlen < len) {
1529 					backup = len - rlen;
1530 					uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base - backup;
1531 					uiop->uio_iov->iov_len += backup;
1532 					uiop->uio_offset -= backup;
1533 					uiop->uio_resid += backup;
1534 					len = rlen;
1535 				}
1536 				commit = fxdr_unsigned(int, *tl++);
1537 
1538 				/*
1539 				 * Return the lowest committment level
1540 				 * obtained by any of the RPCs.
1541 				 */
1542 				if (committed == NFSV3WRITE_FILESYNC)
1543 					committed = commit;
1544 				else if (committed == NFSV3WRITE_DATASYNC &&
1545 					commit == NFSV3WRITE_UNSTABLE)
1546 					committed = commit;
1547 				if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1548 				    bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1549 					NFSX_V3WRITEVERF);
1550 				    nmp->nm_state |= NFSSTA_HASWRITEVERF;
1551 				} else if (bcmp((caddr_t)tl,
1552 				    (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1553 				    *must_commit = 1;
1554 				    bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1555 					NFSX_V3WRITEVERF);
1556 				}
1557 			}
1558 		} else {
1559 			ERROROUT(nfsm_loadattr(&info, vp, NULL));
1560 		}
1561 		m_freem(info.mrep);
1562 		info.mrep = NULL;
1563 		if (error)
1564 			break;
1565 		tsiz -= len;
1566 	}
1567 nfsmout:
1568 	if (vp->v_mount->mnt_flag & MNT_ASYNC)
1569 		committed = NFSV3WRITE_FILESYNC;
1570 	*iomode = committed;
1571 	if (error)
1572 		uiop->uio_resid = tsiz;
1573 	return (error);
1574 }
1575 
1576 /*
1577  * nfs mknod rpc
1578  * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1579  * mode set to specify the file type and the size field for rdev.
1580  */
1581 static int
1582 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1583 	     struct vattr *vap)
1584 {
1585 	struct nfsv2_sattr *sp;
1586 	u_int32_t *tl;
1587 	struct vnode *newvp = NULL;
1588 	struct nfsnode *np = NULL;
1589 	struct vattr vattr;
1590 	int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1591 	int rmajor, rminor;
1592 	struct nfsm_info info;
1593 
1594 	info.mrep = NULL;
1595 	info.v3 = NFS_ISV3(dvp);
1596 
1597 	if (vap->va_type == VCHR || vap->va_type == VBLK) {
1598 		rmajor = txdr_unsigned(vap->va_rmajor);
1599 		rminor = txdr_unsigned(vap->va_rminor);
1600 	} else if (vap->va_type == VFIFO || vap->va_type == VSOCK) {
1601 		rmajor = nfs_xdrneg1;
1602 		rminor = nfs_xdrneg1;
1603 	} else {
1604 		return (EOPNOTSUPP);
1605 	}
1606 	if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
1607 		return (error);
1608 	}
1609 	nfsstats.rpccnt[NFSPROC_MKNOD]++;
1610 	nfsm_reqhead(&info, dvp, NFSPROC_MKNOD,
1611 		     NFSX_FH(info.v3) + 4 * NFSX_UNSIGNED +
1612 		     nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(info.v3));
1613 	ERROROUT(nfsm_fhtom(&info, dvp));
1614 	ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
1615 			     NFS_MAXNAMLEN));
1616 	if (info.v3) {
1617 		tl = nfsm_build(&info, NFSX_UNSIGNED);
1618 		*tl++ = vtonfsv3_type(vap->va_type);
1619 		nfsm_v3attrbuild(&info, vap, FALSE);
1620 		if (vap->va_type == VCHR || vap->va_type == VBLK) {
1621 			tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
1622 			*tl++ = txdr_unsigned(vap->va_rmajor);
1623 			*tl = txdr_unsigned(vap->va_rminor);
1624 		}
1625 	} else {
1626 		sp = nfsm_build(&info, NFSX_V2SATTR);
1627 		sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1628 		sp->sa_uid = nfs_xdrneg1;
1629 		sp->sa_gid = nfs_xdrneg1;
1630 		sp->sa_size = makeudev(rmajor, rminor);
1631 		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1632 		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1633 	}
1634 	NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_MKNOD, cnp->cn_td,
1635 				cnp->cn_cred, &error));
1636 	if (!error) {
1637 		ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
1638 		if (!gotvp) {
1639 			if (newvp) {
1640 				vput(newvp);
1641 				newvp = NULL;
1642 			}
1643 			error = nfs_lookitup(dvp, cnp->cn_nameptr,
1644 			    cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1645 			if (!error)
1646 				newvp = NFSTOV(np);
1647 		}
1648 	}
1649 	if (info.v3) {
1650 		ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
1651 	}
1652 	m_freem(info.mrep);
1653 	info.mrep = NULL;
1654 nfsmout:
1655 	if (error) {
1656 		if (newvp)
1657 			vput(newvp);
1658 	} else {
1659 		*vpp = newvp;
1660 	}
1661 	VTONFS(dvp)->n_flag |= NLMODIFIED;
1662 	if (!wccflag)
1663 		VTONFS(dvp)->n_attrstamp = 0;
1664 	return (error);
1665 }
1666 
1667 /*
1668  * nfs mknod vop
1669  * just call nfs_mknodrpc() to do the work.
1670  *
1671  * nfs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
1672  *	     struct componentname *a_cnp, struct vattr *a_vap)
1673  */
1674 /* ARGSUSED */
1675 static int
1676 nfs_mknod(struct vop_old_mknod_args *ap)
1677 {
1678 	struct nfsmount *nmp = VFSTONFS(ap->a_dvp->v_mount);
1679 	int error;
1680 
1681 	lwkt_gettoken(&nmp->nm_token);
1682 	error = nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1683 	lwkt_reltoken(&nmp->nm_token);
1684 	if (error == 0)
1685 		nfs_knote(ap->a_dvp, NOTE_WRITE);
1686 
1687 	return error;
1688 }
1689 
1690 static u_long create_verf;
1691 /*
1692  * nfs file create call
1693  *
1694  * nfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
1695  *	      struct componentname *a_cnp, struct vattr *a_vap)
1696  */
1697 static int
1698 nfs_create(struct vop_old_create_args *ap)
1699 {
1700 	struct vnode *dvp = ap->a_dvp;
1701 	struct vattr *vap = ap->a_vap;
1702 	struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
1703 	struct componentname *cnp = ap->a_cnp;
1704 	struct nfsv2_sattr *sp;
1705 	u_int32_t *tl;
1706 	struct nfsnode *np = NULL;
1707 	struct vnode *newvp = NULL;
1708 	int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1709 	struct vattr vattr;
1710 	struct nfsm_info info;
1711 
1712 	info.mrep = NULL;
1713 	info.v3 = NFS_ISV3(dvp);
1714 	lwkt_gettoken(&nmp->nm_token);
1715 
1716 	/*
1717 	 * Oops, not for me..
1718 	 */
1719 	if (vap->va_type == VSOCK) {
1720 		error = nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap);
1721 		lwkt_reltoken(&nmp->nm_token);
1722 		return error;
1723 	}
1724 
1725 	if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
1726 		lwkt_reltoken(&nmp->nm_token);
1727 		return (error);
1728 	}
1729 	if (vap->va_vaflags & VA_EXCLUSIVE)
1730 		fmode |= O_EXCL;
1731 again:
1732 	nfsstats.rpccnt[NFSPROC_CREATE]++;
1733 	nfsm_reqhead(&info, dvp, NFSPROC_CREATE,
1734 		     NFSX_FH(info.v3) + 2 * NFSX_UNSIGNED +
1735 		     nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(info.v3));
1736 	ERROROUT(nfsm_fhtom(&info, dvp));
1737 	ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
1738 			     NFS_MAXNAMLEN));
1739 	if (info.v3) {
1740 		tl = nfsm_build(&info, NFSX_UNSIGNED);
1741 		if (fmode & O_EXCL) {
1742 			*tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1743 			tl = nfsm_build(&info, NFSX_V3CREATEVERF);
1744 #ifdef INET
1745 			if (!TAILQ_EMPTY(&in_ifaddrheads[mycpuid]))
1746 				*tl++ = IA_SIN(TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia)->sin_addr.s_addr;
1747 			else
1748 #endif
1749 				*tl++ = create_verf;
1750 			*tl = ++create_verf;
1751 		} else {
1752 			*tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1753 			nfsm_v3attrbuild(&info, vap, FALSE);
1754 		}
1755 	} else {
1756 		sp = nfsm_build(&info, NFSX_V2SATTR);
1757 		sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1758 		sp->sa_uid = nfs_xdrneg1;
1759 		sp->sa_gid = nfs_xdrneg1;
1760 		sp->sa_size = 0;
1761 		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1762 		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1763 	}
1764 	NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_CREATE, cnp->cn_td,
1765 				cnp->cn_cred, &error));
1766 	if (error == 0) {
1767 		ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
1768 		if (!gotvp) {
1769 			if (newvp) {
1770 				vput(newvp);
1771 				newvp = NULL;
1772 			}
1773 			error = nfs_lookitup(dvp, cnp->cn_nameptr,
1774 			    cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1775 			if (!error)
1776 				newvp = NFSTOV(np);
1777 		}
1778 	}
1779 	if (info.v3) {
1780 		if (error == 0)
1781 			error = nfsm_wcc_data(&info, dvp, &wccflag);
1782 		else
1783 			(void)nfsm_wcc_data(&info, dvp, &wccflag);
1784 	}
1785 	m_freem(info.mrep);
1786 	info.mrep = NULL;
1787 nfsmout:
1788 	if (error) {
1789 		if (info.v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1790 			KKASSERT(newvp == NULL);
1791 			fmode &= ~O_EXCL;
1792 			goto again;
1793 		}
1794 	} else if (info.v3 && (fmode & O_EXCL)) {
1795 		/*
1796 		 * We are normally called with only a partially initialized
1797 		 * VAP.  Since the NFSv3 spec says that server may use the
1798 		 * file attributes to store the verifier, the spec requires
1799 		 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1800 		 * in atime, but we can't really assume that all servers will
1801 		 * so we ensure that our SETATTR sets both atime and mtime.
1802 		 */
1803 		if (vap->va_mtime.tv_sec == VNOVAL)
1804 			vfs_timestamp(&vap->va_mtime);
1805 		if (vap->va_atime.tv_sec == VNOVAL)
1806 			vap->va_atime = vap->va_mtime;
1807 		error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_td);
1808 	}
1809 	if (error == 0) {
1810 		/*
1811 		 * The new np may have enough info for access
1812 		 * checks, make sure rucred and wucred are
1813 		 * initialized for read and write rpc's.
1814 		 */
1815 		np = VTONFS(newvp);
1816 		if (np->n_rucred == NULL)
1817 			np->n_rucred = nfs_crhold(cnp->cn_cred);
1818 		if (np->n_wucred == NULL)
1819 			np->n_wucred = nfs_crhold(cnp->cn_cred);
1820 		*ap->a_vpp = newvp;
1821 		nfs_knote(dvp, NOTE_WRITE);
1822 	} else if (newvp) {
1823 		vput(newvp);
1824 	}
1825 	VTONFS(dvp)->n_flag |= NLMODIFIED;
1826 	if (!wccflag)
1827 		VTONFS(dvp)->n_attrstamp = 0;
1828 	lwkt_reltoken(&nmp->nm_token);
1829 	return (error);
1830 }
1831 
1832 /*
1833  * nfs file remove call
1834  * To try and make nfs semantics closer to ufs semantics, a file that has
1835  * other processes using the vnode is renamed instead of removed and then
1836  * removed later on the last close.
1837  * - If v_refcnt > 1
1838  *	  If a rename is not already in the works
1839  *	     call nfs_sillyrename() to set it up
1840  *     else
1841  *	  do the remove rpc
1842  *
1843  * nfs_remove(struct vnode *a_dvp, struct vnode *a_vp,
1844  *	      struct componentname *a_cnp)
1845  */
1846 static int
1847 nfs_remove(struct vop_old_remove_args *ap)
1848 {
1849 	struct vnode *vp = ap->a_vp;
1850 	struct vnode *dvp = ap->a_dvp;
1851 	struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
1852 	struct componentname *cnp = ap->a_cnp;
1853 	struct nfsnode *np = VTONFS(vp);
1854 	int error = 0;
1855 	struct vattr vattr;
1856 
1857 	lwkt_gettoken(&nmp->nm_token);
1858 #ifndef DIAGNOSTIC
1859 	if (VREFCNT(vp) < 1)
1860 		panic("nfs_remove: bad v_refcnt");
1861 #endif
1862 	if (vp->v_type == VDIR) {
1863 		error = EPERM;
1864 	} else if (VREFCNT(vp) == 1 || (np->n_sillyrename &&
1865 		   VOP_GETATTR(vp, &vattr) == 0 && vattr.va_nlink > 1)) {
1866 		/*
1867 		 * Force finalization so the VOP_INACTIVE() call is not delayed.
1868 		 * This prevents cred structures from building up in nfsnodes
1869 		 * for deleted files.
1870 		 */
1871 		atomic_set_int(&vp->v_refcnt, VREF_FINALIZE);
1872 		np->n_flag |= NREMOVED;
1873 
1874 		/*
1875 		 * Throw away biocache buffers, mainly to avoid
1876 		 * unnecessary delayed writes later.
1877 		 */
1878 		error = nfs_vinvalbuf(vp, 0, 1);
1879 		/* Do the rpc */
1880 		if (error != EINTR) {
1881 			error = nfs_removerpc(dvp, cnp->cn_nameptr,
1882 					      cnp->cn_namelen,
1883 					      cnp->cn_cred, cnp->cn_td);
1884 		}
1885 
1886 		/*
1887 		 * Kludge City: If the first reply to the remove rpc is lost..
1888 		 *   the reply to the retransmitted request will be ENOENT
1889 		 *   since the file was in fact removed
1890 		 *   Therefore, we cheat and return success.
1891 		 */
1892 		if (error == ENOENT)
1893 			error = 0;
1894 	} else if (!np->n_sillyrename) {
1895 		error = nfs_sillyrename(dvp, vp, cnp);
1896 	}
1897 	np->n_attrstamp = 0;
1898 	lwkt_reltoken(&nmp->nm_token);
1899 	if (error == 0) {
1900 		nfs_knote(vp, NOTE_DELETE);
1901 		nfs_knote(dvp, NOTE_WRITE);
1902 	}
1903 
1904 	return (error);
1905 }
1906 
1907 /*
1908  * nfs file remove rpc called from nfs_inactive
1909  *
1910  * NOTE: s_dvp can be VBAD during a forced unmount.
1911  */
1912 int
1913 nfs_removeit(struct sillyrename *sp)
1914 {
1915 	if (sp->s_dvp->v_type == VBAD)
1916 		return(0);
1917 	return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen,
1918 		sp->s_cred, NULL));
1919 }
1920 
1921 /*
1922  * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1923  */
1924 static int
1925 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1926 	      struct ucred *cred, struct thread *td)
1927 {
1928 	int error = 0, wccflag = NFSV3_WCCRATTR;
1929 	struct nfsm_info info;
1930 
1931 	info.mrep = NULL;
1932 	info.v3 = NFS_ISV3(dvp);
1933 
1934 	nfsstats.rpccnt[NFSPROC_REMOVE]++;
1935 	nfsm_reqhead(&info, dvp, NFSPROC_REMOVE,
1936 		     NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1937 	ERROROUT(nfsm_fhtom(&info, dvp));
1938 	ERROROUT(nfsm_strtom(&info, name, namelen, NFS_MAXNAMLEN));
1939 	NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_REMOVE, td, cred, &error));
1940 	if (info.v3) {
1941 		ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
1942 	}
1943 	m_freem(info.mrep);
1944 	info.mrep = NULL;
1945 nfsmout:
1946 	VTONFS(dvp)->n_flag |= NLMODIFIED;
1947 	if (!wccflag)
1948 		VTONFS(dvp)->n_attrstamp = 0;
1949 	return (error);
1950 }
1951 
1952 /*
1953  * nfs file rename call
1954  *
1955  * nfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
1956  *	      struct componentname *a_fcnp, struct vnode *a_tdvp,
1957  *	      struct vnode *a_tvp, struct componentname *a_tcnp)
1958  */
1959 static int
1960 nfs_rename(struct vop_old_rename_args *ap)
1961 {
1962 	struct vnode *fvp = ap->a_fvp;
1963 	struct vnode *tvp = ap->a_tvp;
1964 	struct vnode *fdvp = ap->a_fdvp;
1965 	struct vnode *tdvp = ap->a_tdvp;
1966 	struct componentname *tcnp = ap->a_tcnp;
1967 	struct componentname *fcnp = ap->a_fcnp;
1968 	struct nfsmount *nmp = VFSTONFS(fdvp->v_mount);
1969 	int error;
1970 
1971 	lwkt_gettoken(&nmp->nm_token);
1972 
1973 	/*
1974 	 * Force finalization so the VOP_INACTIVE() call is not delayed.
1975 	 * This prevents cred structures from building up in nfsnodes
1976 	 * for deleted files.
1977 	 */
1978 	if (tvp) {
1979 		atomic_set_int(&tvp->v_refcnt, VREF_FINALIZE);
1980 		if (VTONFS(tvp))
1981 			VTONFS(tvp)->n_flag |= NREMOVED;
1982 	}
1983 
1984 	/* Check for cross-device rename */
1985 	if ((fvp->v_mount != tdvp->v_mount) ||
1986 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
1987 		error = EXDEV;
1988 		goto out;
1989 	}
1990 
1991 	/*
1992 	 * We shouldn't have to flush fvp on rename for most server-side
1993 	 * filesystems as the file handle should not change.  Unfortunately
1994 	 * the inode for some filesystems (msdosfs) might be tied to the
1995 	 * file name or directory position so to be completely safe
1996 	 * vfs.nfs.flush_on_rename is set by default.  Clear to improve
1997 	 * performance.
1998 	 *
1999 	 * We must flush tvp on rename because it might become stale on the
2000 	 * server after the rename.
2001 	 */
2002 	if (nfs_flush_on_rename)
2003 	    VOP_FSYNC(fvp, MNT_WAIT, 0);
2004 	if (tvp)
2005 	    VOP_FSYNC(tvp, MNT_WAIT, 0);
2006 
2007 	/*
2008 	 * If the tvp exists and is in use, sillyrename it before doing the
2009 	 * rename of the new file over it.
2010 	 *
2011 	 * XXX Can't sillyrename a directory.
2012 	 *
2013 	 * We do not attempt to do any namecache purges in this old API
2014 	 * routine.  The new API compat functions have access to the actual
2015 	 * namecache structures and will do it for us.
2016 	 */
2017 	if (tvp && VREFCNT(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
2018 		tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
2019 		nfs_knote(tvp, NOTE_DELETE);
2020 		vput(tvp);
2021 		tvp = NULL;
2022 	} else if (tvp) {
2023 		nfs_knote(tvp, NOTE_DELETE);
2024 	}
2025 
2026 	error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
2027 		tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
2028 		tcnp->cn_td);
2029 
2030 out:
2031 	if (error == 0) {
2032 		nfs_knote(fdvp, NOTE_WRITE);
2033 		nfs_knote(tdvp, NOTE_WRITE);
2034 		nfs_knote(fvp, NOTE_RENAME);
2035 	}
2036 	lwkt_reltoken(&nmp->nm_token);
2037 	if (tdvp == tvp)
2038 		vrele(tdvp);
2039 	else
2040 		vput(tdvp);
2041 	if (tvp)
2042 		vput(tvp);
2043 	vrele(fdvp);
2044 	vrele(fvp);
2045 	/*
2046 	 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
2047 	 */
2048 	if (error == ENOENT)
2049 		error = 0;
2050 	return (error);
2051 }
2052 
2053 /*
2054  * nfs file rename rpc called from nfs_remove() above
2055  */
2056 static int
2057 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
2058 	     struct sillyrename *sp)
2059 {
2060 	return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
2061 		sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_td));
2062 }
2063 
2064 /*
2065  * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
2066  */
2067 static int
2068 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
2069 	      struct vnode *tdvp, const char *tnameptr, int tnamelen,
2070 	      struct ucred *cred, struct thread *td)
2071 {
2072 	int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
2073 	struct nfsm_info info;
2074 
2075 	info.mrep = NULL;
2076 	info.v3 = NFS_ISV3(fdvp);
2077 
2078 	nfsstats.rpccnt[NFSPROC_RENAME]++;
2079 	nfsm_reqhead(&info, fdvp, NFSPROC_RENAME,
2080 		    (NFSX_FH(info.v3) + NFSX_UNSIGNED)*2 +
2081 		    nfsm_rndup(fnamelen) + nfsm_rndup(tnamelen));
2082 	ERROROUT(nfsm_fhtom(&info, fdvp));
2083 	ERROROUT(nfsm_strtom(&info, fnameptr, fnamelen, NFS_MAXNAMLEN));
2084 	ERROROUT(nfsm_fhtom(&info, tdvp));
2085 	ERROROUT(nfsm_strtom(&info, tnameptr, tnamelen, NFS_MAXNAMLEN));
2086 	NEGKEEPOUT(nfsm_request(&info, fdvp, NFSPROC_RENAME, td, cred, &error));
2087 	if (info.v3) {
2088 		ERROROUT(nfsm_wcc_data(&info, fdvp, &fwccflag));
2089 		ERROROUT(nfsm_wcc_data(&info, tdvp, &twccflag));
2090 	}
2091 	m_freem(info.mrep);
2092 	info.mrep = NULL;
2093 nfsmout:
2094 	VTONFS(fdvp)->n_flag |= NLMODIFIED;
2095 	VTONFS(tdvp)->n_flag |= NLMODIFIED;
2096 	if (!fwccflag)
2097 		VTONFS(fdvp)->n_attrstamp = 0;
2098 	if (!twccflag)
2099 		VTONFS(tdvp)->n_attrstamp = 0;
2100 	return (error);
2101 }
2102 
2103 /*
2104  * nfs hard link create call
2105  *
2106  * nfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
2107  *	    struct componentname *a_cnp)
2108  */
2109 static int
2110 nfs_link(struct vop_old_link_args *ap)
2111 {
2112 	struct vnode *vp = ap->a_vp;
2113 	struct vnode *tdvp = ap->a_tdvp;
2114 	struct nfsmount *nmp = VFSTONFS(tdvp->v_mount);
2115 	struct componentname *cnp = ap->a_cnp;
2116 	int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
2117 	struct nfsm_info info;
2118 
2119 	if (vp->v_mount != tdvp->v_mount) {
2120 		return (EXDEV);
2121 	}
2122 	lwkt_gettoken(&nmp->nm_token);
2123 
2124 	/*
2125 	 * The attribute cache may get out of sync with the server on link.
2126 	 * Pushing writes to the server before handle was inherited from
2127 	 * long long ago and it is unclear if we still need to do this.
2128 	 * Defaults to off.
2129 	 */
2130 	if (nfs_flush_on_hlink)
2131 		VOP_FSYNC(vp, MNT_WAIT, 0);
2132 
2133 	info.mrep = NULL;
2134 	info.v3 = NFS_ISV3(vp);
2135 
2136 	nfsstats.rpccnt[NFSPROC_LINK]++;
2137 	nfsm_reqhead(&info, vp, NFSPROC_LINK,
2138 		     NFSX_FH(info.v3) * 2 + NFSX_UNSIGNED +
2139 		     nfsm_rndup(cnp->cn_namelen));
2140 	ERROROUT(nfsm_fhtom(&info, vp));
2141 	ERROROUT(nfsm_fhtom(&info, tdvp));
2142 	ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2143 			     NFS_MAXNAMLEN));
2144 	NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_LINK, cnp->cn_td,
2145 				cnp->cn_cred, &error));
2146 	if (info.v3) {
2147 		ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2148 					 NFS_LATTR_NOSHRINK));
2149 		ERROROUT(nfsm_wcc_data(&info, tdvp, &wccflag));
2150 	}
2151 	m_freem(info.mrep);
2152 	info.mrep = NULL;
2153 nfsmout:
2154 	VTONFS(tdvp)->n_flag |= NLMODIFIED;
2155 	if (!attrflag)
2156 		VTONFS(vp)->n_attrstamp = 0;
2157 	if (!wccflag)
2158 		VTONFS(tdvp)->n_attrstamp = 0;
2159 	/*
2160 	 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2161 	 */
2162 	if (error == EEXIST)
2163 		error = 0;
2164 	lwkt_reltoken(&nmp->nm_token);
2165 	if (error == 0) {
2166 		nfs_knote(vp, NOTE_LINK);
2167 		nfs_knote(tdvp, NOTE_WRITE);
2168 	}
2169 
2170 	return (error);
2171 }
2172 
2173 /*
2174  * nfs symbolic link create call
2175  *
2176  * nfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
2177  *		struct componentname *a_cnp, struct vattr *a_vap,
2178  *		char *a_target)
2179  */
2180 static int
2181 nfs_symlink(struct vop_old_symlink_args *ap)
2182 {
2183 	struct vnode *dvp = ap->a_dvp;
2184 	struct vattr *vap = ap->a_vap;
2185 	struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2186 	struct componentname *cnp = ap->a_cnp;
2187 	struct nfsv2_sattr *sp;
2188 	int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
2189 	struct vnode *newvp = NULL;
2190 	struct nfsm_info info;
2191 
2192 	info.mrep = NULL;
2193 	info.v3 = NFS_ISV3(dvp);
2194 	lwkt_gettoken(&nmp->nm_token);
2195 
2196 	nfsstats.rpccnt[NFSPROC_SYMLINK]++;
2197 	slen = strlen(ap->a_target);
2198 	nfsm_reqhead(&info, dvp, NFSPROC_SYMLINK,
2199 		     NFSX_FH(info.v3) + 2*NFSX_UNSIGNED +
2200 		     nfsm_rndup(cnp->cn_namelen) +
2201 		     nfsm_rndup(slen) + NFSX_SATTR(info.v3));
2202 	ERROROUT(nfsm_fhtom(&info, dvp));
2203 	ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2204 			     NFS_MAXNAMLEN));
2205 	if (info.v3) {
2206 		nfsm_v3attrbuild(&info, vap, FALSE);
2207 	}
2208 	ERROROUT(nfsm_strtom(&info, ap->a_target, slen, NFS_MAXPATHLEN));
2209 	if (info.v3 == 0) {
2210 		sp = nfsm_build(&info, NFSX_V2SATTR);
2211 		sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
2212 		sp->sa_uid = nfs_xdrneg1;
2213 		sp->sa_gid = nfs_xdrneg1;
2214 		sp->sa_size = nfs_xdrneg1;
2215 		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2216 		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2217 	}
2218 
2219 	/*
2220 	 * Issue the NFS request and get the rpc response.
2221 	 *
2222 	 * Only NFSv3 responses returning an error of 0 actually return
2223 	 * a file handle that can be converted into newvp without having
2224 	 * to do an extra lookup rpc.
2225 	 */
2226 	NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_SYMLINK, cnp->cn_td,
2227 				cnp->cn_cred, &error));
2228 	if (info.v3) {
2229 		if (error == 0) {
2230 		       ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
2231 		}
2232 		ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2233 	}
2234 
2235 	/*
2236 	 * out code jumps -> here, mrep is also freed.
2237 	 */
2238 
2239 	m_freem(info.mrep);
2240 	info.mrep = NULL;
2241 nfsmout:
2242 
2243 	/*
2244 	 * If we get an EEXIST error, silently convert it to no-error
2245 	 * in case of an NFS retry.
2246 	 */
2247 	if (error == EEXIST)
2248 		error = 0;
2249 
2250 	/*
2251 	 * If we do not have (or no longer have) an error, and we could
2252 	 * not extract the newvp from the response due to the request being
2253 	 * NFSv2 or the error being EEXIST.  We have to do a lookup in order
2254 	 * to obtain a newvp to return.
2255 	 */
2256 	if (error == 0 && newvp == NULL) {
2257 		struct nfsnode *np = NULL;
2258 
2259 		error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2260 				     cnp->cn_cred, cnp->cn_td, &np);
2261 		if (!error)
2262 			newvp = NFSTOV(np);
2263 	}
2264 	if (error) {
2265 		if (newvp)
2266 			vput(newvp);
2267 	} else {
2268 		*ap->a_vpp = newvp;
2269 	}
2270 	VTONFS(dvp)->n_flag |= NLMODIFIED;
2271 	if (!wccflag)
2272 		VTONFS(dvp)->n_attrstamp = 0;
2273 	if (error == 0 && *ap->a_vpp)
2274 		nfs_knote(*ap->a_vpp, NOTE_WRITE);
2275 	lwkt_reltoken(&nmp->nm_token);
2276 
2277 	return (error);
2278 }
2279 
2280 /*
2281  * nfs make dir call
2282  *
2283  * nfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
2284  *	     struct componentname *a_cnp, struct vattr *a_vap)
2285  */
2286 static int
2287 nfs_mkdir(struct vop_old_mkdir_args *ap)
2288 {
2289 	struct vnode *dvp = ap->a_dvp;
2290 	struct vattr *vap = ap->a_vap;
2291 	struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2292 	struct componentname *cnp = ap->a_cnp;
2293 	struct nfsv2_sattr *sp;
2294 	struct nfsnode *np = NULL;
2295 	struct vnode *newvp = NULL;
2296 	struct vattr vattr;
2297 	int error = 0, wccflag = NFSV3_WCCRATTR;
2298 	int gotvp = 0;
2299 	int len;
2300 	struct nfsm_info info;
2301 
2302 	info.mrep = NULL;
2303 	info.v3 = NFS_ISV3(dvp);
2304 	lwkt_gettoken(&nmp->nm_token);
2305 
2306 	if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
2307 		lwkt_reltoken(&nmp->nm_token);
2308 		return (error);
2309 	}
2310 	len = cnp->cn_namelen;
2311 	nfsstats.rpccnt[NFSPROC_MKDIR]++;
2312 	nfsm_reqhead(&info, dvp, NFSPROC_MKDIR,
2313 		     NFSX_FH(info.v3) + NFSX_UNSIGNED +
2314 		     nfsm_rndup(len) + NFSX_SATTR(info.v3));
2315 	ERROROUT(nfsm_fhtom(&info, dvp));
2316 	ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, len, NFS_MAXNAMLEN));
2317 	if (info.v3) {
2318 		nfsm_v3attrbuild(&info, vap, FALSE);
2319 	} else {
2320 		sp = nfsm_build(&info, NFSX_V2SATTR);
2321 		sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
2322 		sp->sa_uid = nfs_xdrneg1;
2323 		sp->sa_gid = nfs_xdrneg1;
2324 		sp->sa_size = nfs_xdrneg1;
2325 		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2326 		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2327 	}
2328 	NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_MKDIR, cnp->cn_td,
2329 		    cnp->cn_cred, &error));
2330 	if (error == 0) {
2331 		ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
2332 	}
2333 	if (info.v3) {
2334 		ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2335 	}
2336 	m_freem(info.mrep);
2337 	info.mrep = NULL;
2338 nfsmout:
2339 	VTONFS(dvp)->n_flag |= NLMODIFIED;
2340 	if (!wccflag)
2341 		VTONFS(dvp)->n_attrstamp = 0;
2342 	/*
2343 	 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2344 	 * if we can succeed in looking up the directory.
2345 	 */
2346 	if (error == EEXIST || (!error && !gotvp)) {
2347 		if (newvp) {
2348 			vrele(newvp);
2349 			newvp = NULL;
2350 		}
2351 		error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
2352 			cnp->cn_td, &np);
2353 		if (!error) {
2354 			newvp = NFSTOV(np);
2355 			if (newvp->v_type != VDIR)
2356 				error = EEXIST;
2357 		}
2358 	}
2359 	if (error) {
2360 		if (newvp)
2361 			vrele(newvp);
2362 	} else {
2363 		nfs_knote(dvp, NOTE_WRITE | NOTE_LINK);
2364 		*ap->a_vpp = newvp;
2365 	}
2366 	lwkt_reltoken(&nmp->nm_token);
2367 	return (error);
2368 }
2369 
2370 /*
2371  * nfs remove directory call
2372  *
2373  * nfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
2374  *	     struct componentname *a_cnp)
2375  */
2376 static int
2377 nfs_rmdir(struct vop_old_rmdir_args *ap)
2378 {
2379 	struct vnode *vp = ap->a_vp;
2380 	struct vnode *dvp = ap->a_dvp;
2381 	struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2382 	struct componentname *cnp = ap->a_cnp;
2383 	int error = 0, wccflag = NFSV3_WCCRATTR;
2384 	struct nfsm_info info;
2385 
2386 	info.mrep = NULL;
2387 	info.v3 = NFS_ISV3(dvp);
2388 
2389 	if (dvp == vp)
2390 		return (EINVAL);
2391 
2392 	lwkt_gettoken(&nmp->nm_token);
2393 
2394 	nfsstats.rpccnt[NFSPROC_RMDIR]++;
2395 	nfsm_reqhead(&info, dvp, NFSPROC_RMDIR,
2396 		     NFSX_FH(info.v3) + NFSX_UNSIGNED +
2397 		     nfsm_rndup(cnp->cn_namelen));
2398 	ERROROUT(nfsm_fhtom(&info, dvp));
2399 	ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2400 		 NFS_MAXNAMLEN));
2401 	NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_RMDIR, cnp->cn_td,
2402 				cnp->cn_cred, &error));
2403 	if (info.v3) {
2404 		ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2405 	}
2406 	m_freem(info.mrep);
2407 	info.mrep = NULL;
2408 nfsmout:
2409 	VTONFS(dvp)->n_flag |= NLMODIFIED;
2410 	if (!wccflag)
2411 		VTONFS(dvp)->n_attrstamp = 0;
2412 	/*
2413 	 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2414 	 */
2415 	if (error == ENOENT)
2416 		error = 0;
2417 	else
2418 		nfs_knote(dvp, NOTE_WRITE | NOTE_LINK);
2419 	lwkt_reltoken(&nmp->nm_token);
2420 
2421 	return (error);
2422 }
2423 
2424 /*
2425  * nfs readdir call
2426  *
2427  * nfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
2428  */
2429 static int
2430 nfs_readdir(struct vop_readdir_args *ap)
2431 {
2432 	struct vnode *vp = ap->a_vp;
2433 	struct nfsnode *np = VTONFS(vp);
2434 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2435 	struct uio *uio = ap->a_uio;
2436 	int tresid, error;
2437 	struct vattr vattr;
2438 
2439 	if (vp->v_type != VDIR)
2440 		return (EPERM);
2441 
2442 	error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
2443 	if (error)
2444 		return (error);
2445 
2446 	lwkt_gettoken(&nmp->nm_token);
2447 
2448 	/*
2449 	 * If we have a valid EOF offset cache we must call VOP_GETATTR()
2450 	 * and then check that is still valid, or if this is an NQNFS mount
2451 	 * we call NQNFS_CKCACHEABLE() instead of VOP_GETATTR().  Note that
2452 	 * VOP_GETATTR() does not necessarily go to the wire.
2453 	 */
2454 	if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2455 	    (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0) {
2456 		if (VOP_GETATTR(vp, &vattr) == 0 &&
2457 		    (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0
2458 		) {
2459 			nfsstats.direofcache_hits++;
2460 			goto done;
2461 		}
2462 	}
2463 
2464 	/*
2465 	 * Call nfs_bioread() to do the real work.  nfs_bioread() does its
2466 	 * own cache coherency checks so we do not have to.
2467 	 */
2468 	tresid = uio->uio_resid;
2469 	error = nfs_bioread(vp, uio, 0);
2470 
2471 	if (!error && uio->uio_resid == tresid)
2472 		nfsstats.direofcache_misses++;
2473 done:
2474 	lwkt_reltoken(&nmp->nm_token);
2475 	vn_unlock(vp);
2476 
2477 	return (error);
2478 }
2479 
2480 /*
2481  * Readdir rpc call.  nfs_bioread->nfs_doio->nfs_readdirrpc.
2482  *
2483  * Note that for directories, nfs_bioread maintains the underlying nfs-centric
2484  * offset/block and converts the nfs formatted directory entries for userland
2485  * consumption as well as deals with offsets into the middle of blocks.
2486  * nfs_doio only deals with logical blocks.  In particular, uio_offset will
2487  * be block-bounded.  It must convert to cookies for the actual RPC.
2488  */
2489 int
2490 nfs_readdirrpc_uio(struct vnode *vp, struct uio *uiop)
2491 {
2492 	int len, left;
2493 	struct nfs_dirent *dp = NULL;
2494 	u_int32_t *tl;
2495 	nfsuint64 *cookiep;
2496 	caddr_t cp;
2497 	nfsuint64 cookie;
2498 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2499 	struct nfsnode *dnp = VTONFS(vp);
2500 	u_quad_t fileno;
2501 	int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2502 	int attrflag;
2503 	struct nfsm_info info;
2504 
2505 	info.mrep = NULL;
2506 	info.v3 = NFS_ISV3(vp);
2507 
2508 #ifndef DIAGNOSTIC
2509 	if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2510 		(uiop->uio_resid & (DIRBLKSIZ - 1)))
2511 		panic("nfs readdirrpc bad uio");
2512 #endif
2513 
2514 	/*
2515 	 * If there is no cookie, assume directory was stale.
2516 	 */
2517 	cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2518 	if (cookiep)
2519 		cookie = *cookiep;
2520 	else
2521 		return (NFSERR_BAD_COOKIE);
2522 	/*
2523 	 * Loop around doing readdir rpc's of size nm_readdirsize
2524 	 * truncated to a multiple of DIRBLKSIZ.
2525 	 * The stopping criteria is EOF or buffer full.
2526 	 */
2527 	while (more_dirs && bigenough) {
2528 		nfsstats.rpccnt[NFSPROC_READDIR]++;
2529 		nfsm_reqhead(&info, vp, NFSPROC_READDIR,
2530 			     NFSX_FH(info.v3) + NFSX_READDIR(info.v3));
2531 		ERROROUT(nfsm_fhtom(&info, vp));
2532 		if (info.v3) {
2533 			tl = nfsm_build(&info, 5 * NFSX_UNSIGNED);
2534 			*tl++ = cookie.nfsuquad[0];
2535 			*tl++ = cookie.nfsuquad[1];
2536 			*tl++ = dnp->n_cookieverf.nfsuquad[0];
2537 			*tl++ = dnp->n_cookieverf.nfsuquad[1];
2538 		} else {
2539 			/*
2540 			 * WARNING!  HAMMER DIRECTORIES WILL NOT WORK WELL
2541 			 * WITH NFSv2!!!  There's nothing I can really do
2542 			 * about it other than to hope the server supports
2543 			 * rdirplus w/NFSv2.
2544 			 */
2545 			tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
2546 			*tl++ = cookie.nfsuquad[0];
2547 		}
2548 		*tl = txdr_unsigned(nmp->nm_readdirsize);
2549 		NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READDIR,
2550 					uiop->uio_td,
2551 					nfs_vpcred(vp, ND_READ), &error));
2552 		if (info.v3) {
2553 			ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2554 						  NFS_LATTR_NOSHRINK));
2555 			NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2556 			dnp->n_cookieverf.nfsuquad[0] = *tl++;
2557 			dnp->n_cookieverf.nfsuquad[1] = *tl;
2558 		}
2559 		NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2560 		more_dirs = fxdr_unsigned(int, *tl);
2561 
2562 		/* loop thru the dir entries, converting them to std form */
2563 		while (more_dirs && bigenough) {
2564 			if (info.v3) {
2565 				NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2566 				fileno = fxdr_hyper(tl);
2567 				len = fxdr_unsigned(int, *(tl + 2));
2568 			} else {
2569 				NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2570 				fileno = fxdr_unsigned(u_quad_t, *tl++);
2571 				len = fxdr_unsigned(int, *tl);
2572 			}
2573 			if (len <= 0 || len > NFS_MAXNAMLEN) {
2574 				error = EBADRPC;
2575 				m_freem(info.mrep);
2576 				info.mrep = NULL;
2577 				goto nfsmout;
2578 			}
2579 
2580 			/*
2581 			 * len is the number of bytes in the path element
2582 			 * name, not including the \0 termination.
2583 			 *
2584 			 * tlen is the number of bytes w have to reserve for
2585 			 * the path element name.
2586 			 */
2587 			tlen = nfsm_rndup(len);
2588 			if (tlen == len)
2589 				tlen += 4;	/* To ensure null termination */
2590 
2591 			/*
2592 			 * If the entry would cross a DIRBLKSIZ boundary,
2593 			 * extend the previous nfs_dirent to cover the
2594 			 * remaining space.
2595 			 */
2596 			left = DIRBLKSIZ - blksiz;
2597 			if ((tlen + sizeof(struct nfs_dirent)) > left) {
2598 				dp->nfs_reclen += left;
2599 				uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2600 				uiop->uio_iov->iov_len -= left;
2601 				uiop->uio_offset += left;
2602 				uiop->uio_resid -= left;
2603 				blksiz = 0;
2604 			}
2605 			if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2606 				bigenough = 0;
2607 			if (bigenough) {
2608 				dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2609 				dp->nfs_ino = fileno;
2610 				dp->nfs_namlen = len;
2611 				dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2612 				dp->nfs_type = DT_UNKNOWN;
2613 				blksiz += dp->nfs_reclen;
2614 				if (blksiz == DIRBLKSIZ)
2615 					blksiz = 0;
2616 				uiop->uio_offset += sizeof(struct nfs_dirent);
2617 				uiop->uio_resid -= sizeof(struct nfs_dirent);
2618 				uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + sizeof(struct nfs_dirent);
2619 				uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2620 				ERROROUT(nfsm_mtouio(&info, uiop, len));
2621 
2622 				/*
2623 				 * The uiop has advanced by nfs_dirent + len
2624 				 * but really needs to advance by
2625 				 * nfs_dirent + tlen
2626 				 */
2627 				cp = uiop->uio_iov->iov_base;
2628 				tlen -= len;
2629 				*cp = '\0';	/* null terminate */
2630 				uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + tlen;
2631 				uiop->uio_iov->iov_len -= tlen;
2632 				uiop->uio_offset += tlen;
2633 				uiop->uio_resid -= tlen;
2634 			} else {
2635 				/*
2636 				 * NFS strings must be rounded up (nfsm_myouio
2637 				 * handled that in the bigenough case).
2638 				 */
2639 				ERROROUT(nfsm_adv(&info, nfsm_rndup(len)));
2640 			}
2641 			if (info.v3) {
2642 				NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2643 			} else {
2644 				NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2645 			}
2646 
2647 			/*
2648 			 * If we were able to accomodate the last entry,
2649 			 * get the cookie for the next one.  Otherwise
2650 			 * hold-over the cookie for the one we were not
2651 			 * able to accomodate.
2652 			 */
2653 			if (bigenough) {
2654 				cookie.nfsuquad[0] = *tl++;
2655 				if (info.v3)
2656 					cookie.nfsuquad[1] = *tl++;
2657 			} else if (info.v3) {
2658 				tl += 2;
2659 			} else {
2660 				tl++;
2661 			}
2662 			more_dirs = fxdr_unsigned(int, *tl);
2663 		}
2664 		/*
2665 		 * If at end of rpc data, get the eof boolean
2666 		 */
2667 		if (!more_dirs) {
2668 			NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2669 			more_dirs = (fxdr_unsigned(int, *tl) == 0);
2670 		}
2671 		m_freem(info.mrep);
2672 		info.mrep = NULL;
2673 	}
2674 	/*
2675 	 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2676 	 * by increasing d_reclen for the last record.
2677 	 */
2678 	if (blksiz > 0) {
2679 		left = DIRBLKSIZ - blksiz;
2680 		dp->nfs_reclen += left;
2681 		uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2682 		uiop->uio_iov->iov_len -= left;
2683 		uiop->uio_offset += left;
2684 		uiop->uio_resid -= left;
2685 	}
2686 
2687 	if (bigenough) {
2688 		/*
2689 		 * We hit the end of the directory, update direofoffset.
2690 		 */
2691 		dnp->n_direofoffset = uiop->uio_offset;
2692 	} else {
2693 		/*
2694 		 * There is more to go, insert the link cookie so the
2695 		 * next block can be read.
2696 		 */
2697 		if (uiop->uio_resid > 0)
2698 			kprintf("EEK! readdirrpc resid > 0\n");
2699 		cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2700 		*cookiep = cookie;
2701 	}
2702 nfsmout:
2703 	return (error);
2704 }
2705 
2706 /*
2707  * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2708  */
2709 int
2710 nfs_readdirplusrpc_uio(struct vnode *vp, struct uio *uiop)
2711 {
2712 	int len, left;
2713 	struct nfs_dirent *dp;
2714 	u_int32_t *tl;
2715 	struct vnode *newvp;
2716 	nfsuint64 *cookiep;
2717 	caddr_t dpossav1, dpossav2;
2718 	caddr_t cp;
2719 	struct mbuf *mdsav1, *mdsav2;
2720 	nfsuint64 cookie;
2721 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2722 	struct nfsnode *dnp = VTONFS(vp), *np;
2723 	nfsfh_t *fhp;
2724 	u_quad_t fileno;
2725 	int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2726 	int attrflag, fhsize;
2727 	struct nchandle nch;
2728 	struct nchandle dnch;
2729 	struct nlcomponent nlc;
2730 	struct nfsm_info info;
2731 
2732 	info.mrep = NULL;
2733 	info.v3 = 1;
2734 
2735 #ifndef nolint
2736 	dp = NULL;
2737 #endif
2738 #ifndef DIAGNOSTIC
2739 	if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2740 		(uiop->uio_resid & (DIRBLKSIZ - 1)))
2741 		panic("nfs readdirplusrpc bad uio");
2742 #endif
2743 	/*
2744 	 * Obtain the namecache record for the directory so we have something
2745 	 * to use as a basis for creating the entries.  This function will
2746 	 * return a held (but not locked) ncp.  The ncp may be disconnected
2747 	 * from the tree and cannot be used for upward traversals, and the
2748 	 * ncp may be unnamed.  Note that other unrelated operations may
2749 	 * cause the ncp to be named at any time.
2750 	 *
2751 	 * We have to lock the ncp to prevent a lock order reversal when
2752 	 * rdirplus does nlookups of the children, because the vnode is
2753 	 * locked and has to stay that way.
2754 	 */
2755 	cache_fromdvp(vp, NULL, 0, &dnch);
2756 	bzero(&nlc, sizeof(nlc));
2757 	newvp = NULLVP;
2758 
2759 	/*
2760 	 * If there is no cookie, assume directory was stale.
2761 	 */
2762 	cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2763 	if (cookiep) {
2764 		cookie = *cookiep;
2765 	} else {
2766 		if (dnch.ncp)
2767 			cache_drop(&dnch);
2768 		return (NFSERR_BAD_COOKIE);
2769 	}
2770 
2771 	/*
2772 	 * Loop around doing readdir rpc's of size nm_readdirsize
2773 	 * truncated to a multiple of DIRBLKSIZ.
2774 	 * The stopping criteria is EOF or buffer full.
2775 	 */
2776 	while (more_dirs && bigenough) {
2777 		nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2778 		nfsm_reqhead(&info, vp, NFSPROC_READDIRPLUS,
2779 			     NFSX_FH(info.v3) + 6 * NFSX_UNSIGNED);
2780 		ERROROUT(nfsm_fhtom(&info, vp));
2781 		tl = nfsm_build(&info, 6 * NFSX_UNSIGNED);
2782 		*tl++ = cookie.nfsuquad[0];
2783 		*tl++ = cookie.nfsuquad[1];
2784 		*tl++ = dnp->n_cookieverf.nfsuquad[0];
2785 		*tl++ = dnp->n_cookieverf.nfsuquad[1];
2786 		*tl++ = txdr_unsigned(nmp->nm_readdirsize);
2787 		*tl = txdr_unsigned(nmp->nm_rsize);
2788 		NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READDIRPLUS,
2789 					uiop->uio_td,
2790 					nfs_vpcred(vp, ND_READ), &error));
2791 		ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2792 					  NFS_LATTR_NOSHRINK));
2793 		NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2794 		dnp->n_cookieverf.nfsuquad[0] = *tl++;
2795 		dnp->n_cookieverf.nfsuquad[1] = *tl++;
2796 		more_dirs = fxdr_unsigned(int, *tl);
2797 
2798 		/* loop thru the dir entries, doctoring them to 4bsd form */
2799 		while (more_dirs && bigenough) {
2800 			NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2801 			fileno = fxdr_hyper(tl);
2802 			len = fxdr_unsigned(int, *(tl + 2));
2803 			if (len <= 0 || len > NFS_MAXNAMLEN) {
2804 				error = EBADRPC;
2805 				m_freem(info.mrep);
2806 				info.mrep = NULL;
2807 				goto nfsmout;
2808 			}
2809 			tlen = nfsm_rndup(len);
2810 			if (tlen == len)
2811 				tlen += 4;	/* To ensure null termination*/
2812 			left = DIRBLKSIZ - blksiz;
2813 			if ((tlen + sizeof(struct nfs_dirent)) > left) {
2814 				dp->nfs_reclen += left;
2815 				uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2816 				uiop->uio_iov->iov_len -= left;
2817 				uiop->uio_offset += left;
2818 				uiop->uio_resid -= left;
2819 				blksiz = 0;
2820 			}
2821 			if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2822 				bigenough = 0;
2823 			if (bigenough) {
2824 				dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2825 				dp->nfs_ino = fileno;
2826 				dp->nfs_namlen = len;
2827 				dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2828 				dp->nfs_type = DT_UNKNOWN;
2829 				blksiz += dp->nfs_reclen;
2830 				if (blksiz == DIRBLKSIZ)
2831 					blksiz = 0;
2832 				uiop->uio_offset += sizeof(struct nfs_dirent);
2833 				uiop->uio_resid -= sizeof(struct nfs_dirent);
2834 				uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + sizeof(struct nfs_dirent);
2835 				uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2836 				nlc.nlc_nameptr = uiop->uio_iov->iov_base;
2837 				nlc.nlc_namelen = len;
2838 				ERROROUT(nfsm_mtouio(&info, uiop, len));
2839 				cp = uiop->uio_iov->iov_base;
2840 				tlen -= len;
2841 				*cp = '\0';
2842 				uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + tlen;
2843 				uiop->uio_iov->iov_len -= tlen;
2844 				uiop->uio_offset += tlen;
2845 				uiop->uio_resid -= tlen;
2846 			} else {
2847 				ERROROUT(nfsm_adv(&info, nfsm_rndup(len)));
2848 			}
2849 			NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2850 			if (bigenough) {
2851 				cookie.nfsuquad[0] = *tl++;
2852 				cookie.nfsuquad[1] = *tl++;
2853 			} else {
2854 				tl += 2;
2855 			}
2856 
2857 			/*
2858 			 * Since the attributes are before the file handle
2859 			 * (sigh), we must skip over the attributes and then
2860 			 * come back and get them.
2861 			 */
2862 			attrflag = fxdr_unsigned(int, *tl);
2863 			if (attrflag) {
2864 			    dpossav1 = info.dpos;
2865 			    mdsav1 = info.md;
2866 			    ERROROUT(nfsm_adv(&info, NFSX_V3FATTR));
2867 			    NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2868 			    doit = fxdr_unsigned(int, *tl);
2869 			    if (doit) {
2870 				NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp));
2871 			    }
2872 			    if (doit && bigenough && !nlcdegenerate(&nlc) &&
2873 				!NFS_CMPFH(dnp, fhp, fhsize)
2874 			    ) {
2875 				if (dnch.ncp) {
2876 #if 0
2877 				    kprintf("NFS/READDIRPLUS, ENTER %*.*s\n",
2878 					nlc.nlc_namelen, nlc.nlc_namelen,
2879 					nlc.nlc_nameptr);
2880 #endif
2881 				    /*
2882 				     * This is a bit hokey but there isn't
2883 				     * much we can do about it.  We can't
2884 				     * hold the directory vp locked while
2885 				     * doing lookups and gets.
2886 				     */
2887 				    nch = cache_nlookup_nonblock(&dnch, &nlc);
2888 				    if (nch.ncp == NULL)
2889 					goto rdfail;
2890 				    cache_setunresolved(&nch);
2891 				    error = nfs_nget_nonblock(vp->v_mount, fhp,
2892 							      fhsize, &np,
2893 							      NULL);
2894 				    if (error) {
2895 					cache_put(&nch);
2896 					goto rdfail;
2897 				    }
2898 				    newvp = NFSTOV(np);
2899 				    dpossav2 = info.dpos;
2900 				    info.dpos = dpossav1;
2901 				    mdsav2 = info.md;
2902 				    info.md = mdsav1;
2903 				    ERROROUT(nfsm_loadattr(&info, newvp, NULL));
2904 				    info.dpos = dpossav2;
2905 				    info.md = mdsav2;
2906 				    dp->nfs_type =
2907 					    IFTODT(VTTOIF(np->n_vattr.va_type));
2908 				    nfs_cache_setvp(&nch, newvp,
2909 						    nfspos_cache_timeout);
2910 				    vput(newvp);
2911 				    newvp = NULLVP;
2912 				    cache_put(&nch);
2913 				} else {
2914 rdfail:
2915 				    ;
2916 #if 0
2917 				    kprintf("Warning: NFS/rddirplus, "
2918 					    "UNABLE TO ENTER %*.*s\n",
2919 					nlc.nlc_namelen, nlc.nlc_namelen,
2920 					nlc.nlc_nameptr);
2921 #endif
2922 				}
2923 			    }
2924 			} else {
2925 			    /* Just skip over the file handle */
2926 			    NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2927 			    i = fxdr_unsigned(int, *tl);
2928 			    ERROROUT(nfsm_adv(&info, nfsm_rndup(i)));
2929 			}
2930 			NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2931 			more_dirs = fxdr_unsigned(int, *tl);
2932 		}
2933 		/*
2934 		 * If at end of rpc data, get the eof boolean
2935 		 */
2936 		if (!more_dirs) {
2937 			NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2938 			more_dirs = (fxdr_unsigned(int, *tl) == 0);
2939 		}
2940 		m_freem(info.mrep);
2941 		info.mrep = NULL;
2942 	}
2943 	/*
2944 	 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2945 	 * by increasing d_reclen for the last record.
2946 	 */
2947 	if (blksiz > 0) {
2948 		left = DIRBLKSIZ - blksiz;
2949 		dp->nfs_reclen += left;
2950 		uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2951 		uiop->uio_iov->iov_len -= left;
2952 		uiop->uio_offset += left;
2953 		uiop->uio_resid -= left;
2954 	}
2955 
2956 	/*
2957 	 * We are now either at the end of the directory or have filled the
2958 	 * block.
2959 	 */
2960 	if (bigenough) {
2961 		dnp->n_direofoffset = uiop->uio_offset;
2962 	} else {
2963 		if (uiop->uio_resid > 0)
2964 			kprintf("EEK! readdirplusrpc resid > 0\n");
2965 		cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2966 		*cookiep = cookie;
2967 	}
2968 nfsmout:
2969 	if (newvp != NULLVP) {
2970 	        if (newvp == vp)
2971 			vrele(newvp);
2972 		else
2973 			vput(newvp);
2974 		newvp = NULLVP;
2975 	}
2976 	if (dnch.ncp)
2977 		cache_drop(&dnch);
2978 	return (error);
2979 }
2980 
2981 /*
2982  * Silly rename. To make the NFS filesystem that is stateless look a little
2983  * more like the "ufs" a remove of an active vnode is translated to a rename
2984  * to a funny looking filename that is removed by nfs_inactive on the
2985  * nfsnode. There is the potential for another process on a different client
2986  * to create the same funny name between the nfs_lookitup() fails and the
2987  * nfs_rename() completes, but...
2988  */
2989 static int
2990 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2991 {
2992 	struct sillyrename *sp;
2993 	struct nfsnode *np;
2994 	int error;
2995 
2996 	/*
2997 	 * Force finalization so the VOP_INACTIVE() call is not delayed.
2998 	 * This prevents cred structures from building up in nfsnodes
2999 	 * for deleted files.
3000 	 */
3001 	atomic_set_int(&vp->v_refcnt, VREF_FINALIZE);
3002 	np = VTONFS(vp);
3003 	np->n_flag |= NREMOVED;
3004 
3005 	/*
3006 	 * We previously purged dvp instead of vp.  I don't know why, it
3007 	 * completely destroys performance.  We can't do it anyway with the
3008 	 * new VFS API since we would be breaking the namecache topology.
3009 	 */
3010 	cache_purge(vp);	/* XXX */
3011 #ifndef DIAGNOSTIC
3012 	if (vp->v_type == VDIR)
3013 		panic("nfs: sillyrename dir");
3014 #endif
3015 	sp = kmalloc(sizeof(struct sillyrename), M_NFSREQ, M_WAITOK);
3016 	sp->s_cred = crdup(cnp->cn_cred);
3017 	sp->s_dvp = dvp;
3018 	vref(dvp);
3019 
3020 	/* Fudge together a funny name */
3021 	sp->s_namlen = ksprintf(sp->s_name, ".nfsA%08x4.4",
3022 				(int)(intptr_t)cnp->cn_td);
3023 
3024 	/* Try lookitups until we get one that isn't there */
3025 	while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
3026 		cnp->cn_td, NULL) == 0) {
3027 		sp->s_name[4]++;
3028 		if (sp->s_name[4] > 'z') {
3029 			error = EINVAL;
3030 			goto bad;
3031 		}
3032 	}
3033 	error = nfs_renameit(dvp, cnp, sp);
3034 	if (error)
3035 		goto bad;
3036 	error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
3037 		cnp->cn_td, &np);
3038 	np->n_sillyrename = sp;
3039 	return (0);
3040 bad:
3041 	vrele(sp->s_dvp);
3042 	crfree(sp->s_cred);
3043 	kfree((caddr_t)sp, M_NFSREQ);
3044 
3045 	return (error);
3046 }
3047 
3048 /*
3049  * Look up a file name and optionally either update the file handle or
3050  * allocate an nfsnode, depending on the value of npp.
3051  * npp == NULL	--> just do the lookup
3052  * *npp == NULL --> allocate a new nfsnode and make sure attributes are
3053  *			handled too
3054  * *npp != NULL --> update the file handle in the vnode
3055  */
3056 static int
3057 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
3058 	     struct thread *td, struct nfsnode **npp)
3059 {
3060 	struct vnode *newvp = NULL;
3061 	struct nfsnode *np, *dnp = VTONFS(dvp);
3062 	int error = 0, fhlen, attrflag;
3063 	nfsfh_t *nfhp;
3064 	struct nfsm_info info;
3065 
3066 	info.mrep = NULL;
3067 	info.v3 = NFS_ISV3(dvp);
3068 
3069 	nfsstats.rpccnt[NFSPROC_LOOKUP]++;
3070 	nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP,
3071 		     NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len));
3072 	ERROROUT(nfsm_fhtom(&info, dvp));
3073 	ERROROUT(nfsm_strtom(&info, name, len, NFS_MAXNAMLEN));
3074 	NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, td, cred, &error));
3075 	if (npp && !error) {
3076 		NEGATIVEOUT(fhlen = nfsm_getfh(&info, &nfhp));
3077 		if (*npp) {
3078 		    np = *npp;
3079 		    if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
3080 			kfree((caddr_t)np->n_fhp, M_NFSBIGFH);
3081 			np->n_fhp = &np->n_fh;
3082 		    } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
3083 			np->n_fhp =(nfsfh_t *)kmalloc(fhlen,M_NFSBIGFH,M_WAITOK);
3084 		    bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
3085 		    np->n_fhsize = fhlen;
3086 		    newvp = NFSTOV(np);
3087 		} else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
3088 		    vref(dvp);
3089 		    newvp = dvp;
3090 		} else {
3091 		    error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np, NULL);
3092 		    if (error) {
3093 			m_freem(info.mrep);
3094 			info.mrep = NULL;
3095 			return (error);
3096 		    }
3097 		    newvp = NFSTOV(np);
3098 		}
3099 		if (info.v3) {
3100 			ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
3101 						  NFS_LATTR_NOSHRINK));
3102 			if (!attrflag && *npp == NULL) {
3103 				m_freem(info.mrep);
3104 				info.mrep = NULL;
3105 				if (newvp == dvp)
3106 					vrele(newvp);
3107 				else
3108 					vput(newvp);
3109 				return (ENOENT);
3110 			}
3111 		} else {
3112 			ERROROUT(nfsm_loadattr(&info, newvp, NULL));
3113 		}
3114 	}
3115 	m_freem(info.mrep);
3116 	info.mrep = NULL;
3117 nfsmout:
3118 	if (npp && *npp == NULL) {
3119 		if (error) {
3120 			if (newvp) {
3121 				if (newvp == dvp)
3122 					vrele(newvp);
3123 				else
3124 					vput(newvp);
3125 			}
3126 		} else
3127 			*npp = np;
3128 	}
3129 	return (error);
3130 }
3131 
3132 /*
3133  * Nfs Version 3 commit rpc
3134  *
3135  * We call it 'uio' to distinguish it from 'bio' but there is no real uio
3136  * involved.
3137  */
3138 int
3139 nfs_commitrpc_uio(struct vnode *vp, u_quad_t offset, int cnt, struct thread *td)
3140 {
3141 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3142 	int error = 0, wccflag = NFSV3_WCCRATTR;
3143 	struct nfsm_info info;
3144 	u_int32_t *tl;
3145 
3146 	info.mrep = NULL;
3147 	info.v3 = 1;
3148 
3149 	if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
3150 		return (0);
3151 	nfsstats.rpccnt[NFSPROC_COMMIT]++;
3152 	nfsm_reqhead(&info, vp, NFSPROC_COMMIT, NFSX_FH(1));
3153 	ERROROUT(nfsm_fhtom(&info, vp));
3154 	tl = nfsm_build(&info, 3 * NFSX_UNSIGNED);
3155 	txdr_hyper(offset, tl);
3156 	tl += 2;
3157 	*tl = txdr_unsigned(cnt);
3158 	NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_COMMIT, td,
3159 				nfs_vpcred(vp, ND_WRITE), &error));
3160 	ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
3161 	if (!error) {
3162 		NULLOUT(tl = nfsm_dissect(&info, NFSX_V3WRITEVERF));
3163 		if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
3164 			NFSX_V3WRITEVERF)) {
3165 			bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
3166 				NFSX_V3WRITEVERF);
3167 			error = NFSERR_STALEWRITEVERF;
3168 		}
3169 	}
3170 	m_freem(info.mrep);
3171 	info.mrep = NULL;
3172 nfsmout:
3173 	return (error);
3174 }
3175 
3176 /*
3177  * Kludge City..
3178  * - make nfs_bmap() essentially a no-op that does no translation
3179  * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
3180  *   (Maybe I could use the process's page mapping, but I was concerned that
3181  *    Kernel Write might not be enabled and also figured copyout() would do
3182  *    a lot more work than bcopy() and also it currently happens in the
3183  *    context of the swapper process (2).
3184  *
3185  * nfs_bmap(struct vnode *a_vp, off_t a_loffset,
3186  *	    off_t *a_doffsetp, int *a_runp, int *a_runb)
3187  */
3188 static int
3189 nfs_bmap(struct vop_bmap_args *ap)
3190 {
3191 	/* no token lock required */
3192 	if (ap->a_doffsetp != NULL)
3193 		*ap->a_doffsetp = ap->a_loffset;
3194 	if (ap->a_runp != NULL)
3195 		*ap->a_runp = 0;
3196 	if (ap->a_runb != NULL)
3197 		*ap->a_runb = 0;
3198 	return (0);
3199 }
3200 
3201 /*
3202  * Strategy routine.
3203  */
3204 static int
3205 nfs_strategy(struct vop_strategy_args *ap)
3206 {
3207 	struct bio *bio = ap->a_bio;
3208 	struct bio *nbio;
3209 	struct buf *bp __debugvar = bio->bio_buf;
3210 	struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
3211 	struct thread *td;
3212 	int error;
3213 
3214 	KASSERT(bp->b_cmd != BUF_CMD_DONE,
3215 		("nfs_strategy: buffer %p unexpectedly marked done", bp));
3216 	KASSERT(BUF_LOCKINUSE(bp),
3217 		("nfs_strategy: buffer %p not locked", bp));
3218 
3219 	if (bio->bio_flags & BIO_SYNC)
3220 		td = curthread;	/* XXX */
3221 	else
3222 		td = NULL;
3223 
3224 	lwkt_gettoken(&nmp->nm_token);
3225 
3226         /*
3227 	 * We probably don't need to push an nbio any more since no
3228 	 * block conversion is required due to the use of 64 bit byte
3229 	 * offsets, but do it anyway.
3230 	 *
3231 	 * NOTE: When NFS callers itself via this strategy routines and
3232 	 *	 sets up a synchronous I/O, it expects the I/O to run
3233 	 *	 synchronously (its bio_done routine just assumes it),
3234 	 *	 so for now we have to honor the bit.
3235          */
3236 	nbio = push_bio(bio);
3237 	nbio->bio_offset = bio->bio_offset;
3238 	nbio->bio_flags = bio->bio_flags & BIO_SYNC;
3239 
3240 	/*
3241 	 * If the op is asynchronous and an i/o daemon is waiting
3242 	 * queue the request, wake it up and wait for completion
3243 	 * otherwise just do it ourselves.
3244 	 */
3245 	if (bio->bio_flags & BIO_SYNC) {
3246 		error = nfs_doio(ap->a_vp, nbio, td);
3247 	} else {
3248 		nfs_asyncio(ap->a_vp, nbio);
3249 		error = 0;
3250 	}
3251 	lwkt_reltoken(&nmp->nm_token);
3252 
3253 	return (error);
3254 }
3255 
3256 /*
3257  * fsync vnode op. Just call nfs_flush() with commit == 1.
3258  *
3259  * nfs_fsync(struct vnode *a_vp, int a_waitfor)
3260  */
3261 /* ARGSUSED */
3262 static int
3263 nfs_fsync(struct vop_fsync_args *ap)
3264 {
3265 	struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
3266 	int error;
3267 
3268 	lwkt_gettoken(&nmp->nm_token);
3269 
3270 	/*
3271 	 * NOTE: Because attributes are set synchronously we currently
3272 	 *	 do not have to implement vsetisdirty()/vclrisdirty().
3273 	 */
3274 	error = nfs_flush(ap->a_vp, ap->a_waitfor, curthread, 1);
3275 
3276 	lwkt_reltoken(&nmp->nm_token);
3277 
3278 	return error;
3279 }
3280 
3281 /*
3282  * Flush all the blocks associated with a vnode.   Dirty NFS buffers may be
3283  * in one of two states:  If B_NEEDCOMMIT is clear then the buffer contains
3284  * new NFS data which needs to be written to the server.  If B_NEEDCOMMIT is
3285  * set the buffer contains data that has already been written to the server
3286  * and which now needs a commit RPC.
3287  *
3288  * If commit is 0 we only take one pass and only flush buffers containing new
3289  * dirty data.
3290  *
3291  * If commit is 1 we take two passes, issuing a commit RPC in the second
3292  * pass.
3293  *
3294  * If waitfor is MNT_WAIT and commit is 1, we loop as many times as required
3295  * to completely flush all pending data.
3296  *
3297  * Note that the RB_SCAN code properly handles the case where the
3298  * callback might block and directly or indirectly (another thread) cause
3299  * the RB tree to change.
3300  */
3301 
3302 #ifndef NFS_COMMITBVECSIZ
3303 #define NFS_COMMITBVECSIZ	16
3304 #endif
3305 
3306 struct nfs_flush_info {
3307 	enum { NFI_FLUSHNEW, NFI_COMMIT } mode;
3308 	struct thread *td;
3309 	struct vnode *vp;
3310 	int waitfor;
3311 	int slpflag;
3312 	int slptimeo;
3313 	int loops;
3314 	struct buf *bvary[NFS_COMMITBVECSIZ];
3315 	int bvsize;
3316 	off_t beg_off;
3317 	off_t end_off;
3318 };
3319 
3320 static int nfs_flush_bp(struct buf *bp, void *data);
3321 static int nfs_flush_docommit(struct nfs_flush_info *info, int error);
3322 
3323 int
3324 nfs_flush(struct vnode *vp, int waitfor, struct thread *td, int commit)
3325 {
3326 	struct nfsnode *np = VTONFS(vp);
3327 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3328 	struct nfs_flush_info info;
3329 	int error;
3330 
3331 	bzero(&info, sizeof(info));
3332 	info.td = td;
3333 	info.vp = vp;
3334 	info.waitfor = waitfor;
3335 	info.slpflag = (nmp->nm_flag & NFSMNT_INT) ? PCATCH : 0;
3336 	info.loops = 0;
3337 	lwkt_gettoken(&vp->v_token);
3338 
3339 	do {
3340 		/*
3341 		 * Flush mode
3342 		 */
3343 		info.mode = NFI_FLUSHNEW;
3344 		error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
3345 				nfs_flush_bp, &info);
3346 
3347 		/*
3348 		 * Take a second pass if committing and no error occured.
3349 		 * Clean up any left over collection (whether an error
3350 		 * occurs or not).
3351 		 */
3352 		if (commit && error == 0) {
3353 			info.mode = NFI_COMMIT;
3354 			error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
3355 					nfs_flush_bp, &info);
3356 			if (info.bvsize)
3357 				error = nfs_flush_docommit(&info, error);
3358 		}
3359 
3360 		/*
3361 		 * Wait for pending I/O to complete before checking whether
3362 		 * any further dirty buffers exist.
3363 		 */
3364 		while (waitfor == MNT_WAIT &&
3365 		       bio_track_active(&vp->v_track_write)) {
3366 			error = bio_track_wait(&vp->v_track_write,
3367 					       info.slpflag, info.slptimeo);
3368 			if (error) {
3369 				/*
3370 				 * We have to be able to break out if this
3371 				 * is an 'intr' mount.
3372 				 */
3373 				if (nfs_sigintr(nmp, NULL, td)) {
3374 					error = -EINTR;
3375 					break;
3376 				}
3377 
3378 				/*
3379 				 * Since we do not process pending signals,
3380 				 * once we get a PCATCH our tsleep() will no
3381 				 * longer sleep, switch to a fixed timeout
3382 				 * instead.
3383 				 */
3384 				if (info.slpflag == PCATCH) {
3385 					info.slpflag = 0;
3386 					info.slptimeo = 2 * hz;
3387 				}
3388 				error = 0;
3389 			}
3390 		}
3391 		++info.loops;
3392 		/*
3393 		 * Loop if we are flushing synchronous as well as committing,
3394 		 * and dirty buffers are still present.  Otherwise we might livelock.
3395 		 */
3396 	} while (waitfor == MNT_WAIT && commit &&
3397 		 error == 0 && !RB_EMPTY(&vp->v_rbdirty_tree));
3398 
3399 	/*
3400 	 * The callbacks have to return a negative error to terminate the
3401 	 * RB scan.
3402 	 */
3403 	if (error < 0)
3404 		error = -error;
3405 
3406 	/*
3407 	 * Deal with any error collection
3408 	 */
3409 	if (np->n_flag & NWRITEERR) {
3410 		error = np->n_error;
3411 		np->n_flag &= ~NWRITEERR;
3412 	}
3413 	lwkt_reltoken(&vp->v_token);
3414 	return (error);
3415 }
3416 
3417 static
3418 int
3419 nfs_flush_bp(struct buf *bp, void *data)
3420 {
3421 	struct nfs_flush_info *info = data;
3422 	int lkflags;
3423 	int error;
3424 	off_t toff;
3425 
3426 	error = 0;
3427 	switch(info->mode) {
3428 	case NFI_FLUSHNEW:
3429 		error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3430 		if (error && info->loops && info->waitfor == MNT_WAIT) {
3431 			error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3432 			if (error) {
3433 				lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
3434 				if (info->slpflag & PCATCH)
3435 					lkflags |= LK_PCATCH;
3436 				error = BUF_TIMELOCK(bp, lkflags, "nfsfsync",
3437 						     info->slptimeo);
3438 			}
3439 		}
3440 
3441 		/*
3442 		 * Ignore locking errors
3443 		 */
3444 		if (error) {
3445 			error = 0;
3446 			break;
3447 		}
3448 
3449 		/*
3450 		 * The buffer may have changed out from under us, even if
3451 		 * we did not block (MPSAFE).  Check again now that it is
3452 		 * locked.
3453 		 */
3454 		if (bp->b_vp == info->vp &&
3455 		    (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) == B_DELWRI) {
3456 			bremfree(bp);
3457 			bawrite(bp);
3458 		} else {
3459 			BUF_UNLOCK(bp);
3460 		}
3461 		break;
3462 	case NFI_COMMIT:
3463 		/*
3464 		 * Only process buffers in need of a commit which we can
3465 		 * immediately lock.  This may prevent a buffer from being
3466 		 * committed, but the normal flush loop will block on the
3467 		 * same buffer so we shouldn't get into an endless loop.
3468 		 */
3469 		if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3470 		    (B_DELWRI | B_NEEDCOMMIT)) {
3471 			break;
3472 		}
3473 		if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
3474 			break;
3475 
3476 		/*
3477 		 * We must recheck after successfully locking the buffer.
3478 		 */
3479 		if (bp->b_vp != info->vp ||
3480 		    (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3481 		    (B_DELWRI | B_NEEDCOMMIT)) {
3482 			BUF_UNLOCK(bp);
3483 			break;
3484 		}
3485 
3486 		/*
3487 		 * NOTE: storing the bp in the bvary[] basically sets
3488 		 * it up for a commit operation.
3489 		 *
3490 		 * We must call vfs_busy_pages() now so the commit operation
3491 		 * is interlocked with user modifications to memory mapped
3492 		 * pages.  The b_dirtyoff/b_dirtyend range is not correct
3493 		 * until after the pages have been busied.
3494 		 *
3495 		 * Note: to avoid loopback deadlocks, we do not
3496 		 * assign b_runningbufspace.
3497 		 */
3498 		bremfree(bp);
3499 		bp->b_cmd = BUF_CMD_WRITE;
3500 		vfs_busy_pages(bp->b_vp, bp);
3501 		info->bvary[info->bvsize] = bp;
3502 		toff = bp->b_bio2.bio_offset + bp->b_dirtyoff;
3503 		if (info->bvsize == 0 || toff < info->beg_off)
3504 			info->beg_off = toff;
3505 		toff += (off_t)(bp->b_dirtyend - bp->b_dirtyoff);
3506 		if (info->bvsize == 0 || toff > info->end_off)
3507 			info->end_off = toff;
3508 		++info->bvsize;
3509 		if (info->bvsize == NFS_COMMITBVECSIZ) {
3510 			error = nfs_flush_docommit(info, 0);
3511 			KKASSERT(info->bvsize == 0);
3512 		}
3513 	}
3514 	return (error);
3515 }
3516 
3517 static
3518 int
3519 nfs_flush_docommit(struct nfs_flush_info *info, int error)
3520 {
3521 	struct vnode *vp;
3522 	struct buf *bp;
3523 	off_t bytes;
3524 	int retv;
3525 	int i;
3526 
3527 	vp = info->vp;
3528 
3529 	if (info->bvsize > 0) {
3530 		/*
3531 		 * Commit data on the server, as required.  Note that
3532 		 * nfs_commit will use the vnode's cred for the commit.
3533 		 * The NFSv3 commit RPC is limited to a 32 bit byte count.
3534 		 */
3535 		bytes = info->end_off - info->beg_off;
3536 		if (bytes > 0x40000000)
3537 			bytes = 0x40000000;
3538 		if (error) {
3539 			retv = -error;
3540 		} else {
3541 			retv = nfs_commitrpc_uio(vp, info->beg_off,
3542 						 (int)bytes, info->td);
3543 			if (retv == NFSERR_STALEWRITEVERF)
3544 				nfs_clearcommit(vp->v_mount);
3545 		}
3546 
3547 		/*
3548 		 * Now, either mark the blocks I/O done or mark the
3549 		 * blocks dirty, depending on whether the commit
3550 		 * succeeded.
3551 		 */
3552 		for (i = 0; i < info->bvsize; ++i) {
3553 			bp = info->bvary[i];
3554 			if (retv || (bp->b_flags & B_NEEDCOMMIT) == 0) {
3555 				/*
3556 				 * Either an error or the original
3557 				 * vfs_busy_pages() cleared B_NEEDCOMMIT
3558 				 * due to finding new dirty VM pages in
3559 				 * the buffer.
3560 				 *
3561 				 * Leave B_DELWRI intact.
3562 				 */
3563 				bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3564 				vfs_unbusy_pages(bp);
3565 				bp->b_cmd = BUF_CMD_DONE;
3566 				bqrelse(bp);
3567 			} else {
3568 				/*
3569 				 * Success, remove B_DELWRI ( bundirty() ).
3570 				 *
3571 				 * b_dirtyoff/b_dirtyend seem to be NFS
3572 				 * specific.  We should probably move that
3573 				 * into bundirty(). XXX
3574 				 *
3575 				 * We are faking an I/O write, we have to
3576 				 * start the transaction in order to
3577 				 * immediately biodone() it.
3578 				 */
3579 				bundirty(bp);
3580 				bp->b_flags &= ~B_ERROR;
3581 				bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3582 				bp->b_dirtyoff = bp->b_dirtyend = 0;
3583 				biodone(&bp->b_bio1);
3584 			}
3585 		}
3586 		info->bvsize = 0;
3587 	}
3588 	return (error);
3589 }
3590 
3591 /*
3592  * NFS advisory byte-level locks.
3593  * Currently unsupported.
3594  *
3595  * nfs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
3596  *		int a_flags)
3597  */
3598 static int
3599 nfs_advlock(struct vop_advlock_args *ap)
3600 {
3601 	struct nfsnode *np = VTONFS(ap->a_vp);
3602 
3603 	/* no token lock currently required */
3604 	/*
3605 	 * The following kludge is to allow diskless support to work
3606 	 * until a real NFS lockd is implemented. Basically, just pretend
3607 	 * that this is a local lock.
3608 	 */
3609 	return (lf_advlock(ap, &(np->n_lockf), np->n_size));
3610 }
3611 
3612 /*
3613  * Print out the contents of an nfsnode.
3614  *
3615  * nfs_print(struct vnode *a_vp)
3616  */
3617 static int
3618 nfs_print(struct vop_print_args *ap)
3619 {
3620 	struct vnode *vp = ap->a_vp;
3621 	struct nfsnode *np = VTONFS(vp);
3622 
3623 	kprintf("tag VT_NFS, fileid %lld fsid 0x%x",
3624 		(long long)np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3625 	if (vp->v_type == VFIFO)
3626 		fifo_printinfo(vp);
3627 	kprintf("\n");
3628 	return (0);
3629 }
3630 
3631 /*
3632  * nfs special file access vnode op.
3633  *
3634  * nfs_laccess(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
3635  */
3636 static int
3637 nfs_laccess(struct vop_access_args *ap)
3638 {
3639 	struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
3640 	struct vattr vattr;
3641 	int error;
3642 
3643 	lwkt_gettoken(&nmp->nm_token);
3644 	error = VOP_GETATTR(ap->a_vp, &vattr);
3645 	if (error == 0) {
3646 		error = vop_helper_access(ap, vattr.va_uid, vattr.va_gid,
3647 					  vattr.va_mode, 0);
3648 	}
3649 	lwkt_reltoken(&nmp->nm_token);
3650 
3651 	return (error);
3652 }
3653 
3654 /*
3655  * Read wrapper for fifos.
3656  *
3657  * nfsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3658  *		struct ucred *a_cred)
3659  */
3660 static int
3661 nfsfifo_read(struct vop_read_args *ap)
3662 {
3663 	struct nfsnode *np = VTONFS(ap->a_vp);
3664 
3665 	/* no token access required */
3666 	/*
3667 	 * Set access flag.
3668 	 */
3669 	np->n_flag |= NACC;
3670 	getnanotime(&np->n_atim);
3671 	return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3672 }
3673 
3674 /*
3675  * Write wrapper for fifos.
3676  *
3677  * nfsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3678  *		 struct ucred *a_cred)
3679  */
3680 static int
3681 nfsfifo_write(struct vop_write_args *ap)
3682 {
3683 	struct nfsnode *np = VTONFS(ap->a_vp);
3684 
3685 	/* no token access required */
3686 	/*
3687 	 * Set update flag.
3688 	 */
3689 	np->n_flag |= NUPD;
3690 	getnanotime(&np->n_mtim);
3691 	return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3692 }
3693 
3694 /*
3695  * Close wrapper for fifos.
3696  *
3697  * Update the times on the nfsnode then do fifo close.
3698  *
3699  * nfsfifo_close(struct vnode *a_vp, int a_fflag)
3700  */
3701 static int
3702 nfsfifo_close(struct vop_close_args *ap)
3703 {
3704 	struct vnode *vp = ap->a_vp;
3705 	struct nfsnode *np = VTONFS(vp);
3706 	struct vattr vattr;
3707 	struct timespec ts;
3708 
3709 	/* no token access required */
3710 
3711 	vn_lock(vp, LK_UPGRADE | LK_RETRY); /* XXX */
3712 	if (np->n_flag & (NACC | NUPD)) {
3713 		getnanotime(&ts);
3714 		if (np->n_flag & NACC)
3715 			np->n_atim = ts;
3716 		if (np->n_flag & NUPD)
3717 			np->n_mtim = ts;
3718 		np->n_flag |= NCHG;
3719 		if (VREFCNT(vp) == 1 &&
3720 		    (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3721 			VATTR_NULL(&vattr);
3722 			if (np->n_flag & NACC)
3723 				vattr.va_atime = np->n_atim;
3724 			if (np->n_flag & NUPD)
3725 				vattr.va_mtime = np->n_mtim;
3726 			(void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE));
3727 		}
3728 	}
3729 	return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3730 }
3731 
3732 /************************************************************************
3733  *                          KQFILTER OPS                                *
3734  ************************************************************************/
3735 
3736 static void filt_nfsdetach(struct knote *kn);
3737 static int filt_nfsread(struct knote *kn, long hint);
3738 static int filt_nfswrite(struct knote *kn, long hint);
3739 static int filt_nfsvnode(struct knote *kn, long hint);
3740 
3741 static struct filterops nfsread_filtops =
3742 	{ FILTEROP_ISFD | FILTEROP_MPSAFE,
3743 	  NULL, filt_nfsdetach, filt_nfsread };
3744 static struct filterops nfswrite_filtops =
3745 	{ FILTEROP_ISFD | FILTEROP_MPSAFE,
3746 	  NULL, filt_nfsdetach, filt_nfswrite };
3747 static struct filterops nfsvnode_filtops =
3748 	{ FILTEROP_ISFD | FILTEROP_MPSAFE,
3749 	  NULL, filt_nfsdetach, filt_nfsvnode };
3750 
3751 static int
3752 nfs_kqfilter (struct vop_kqfilter_args *ap)
3753 {
3754 	struct vnode *vp = ap->a_vp;
3755 	struct knote *kn = ap->a_kn;
3756 
3757 	switch (kn->kn_filter) {
3758 	case EVFILT_READ:
3759 		kn->kn_fop = &nfsread_filtops;
3760 		break;
3761 	case EVFILT_WRITE:
3762 		kn->kn_fop = &nfswrite_filtops;
3763 		break;
3764 	case EVFILT_VNODE:
3765 		kn->kn_fop = &nfsvnode_filtops;
3766 		break;
3767 	default:
3768 		return (EOPNOTSUPP);
3769 	}
3770 
3771 	kn->kn_hook = (caddr_t)vp;
3772 
3773 	knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
3774 
3775 	return(0);
3776 }
3777 
3778 static void
3779 filt_nfsdetach(struct knote *kn)
3780 {
3781 	struct vnode *vp = (void *)kn->kn_hook;
3782 
3783 	knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
3784 }
3785 
3786 static int
3787 filt_nfsread(struct knote *kn, long hint)
3788 {
3789 	struct vnode *vp = (void *)kn->kn_hook;
3790 	struct nfsnode *node = VTONFS(vp);
3791 	off_t off;
3792 
3793 	if (hint == NOTE_REVOKE) {
3794 		kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
3795 		return(1);
3796 	}
3797 
3798 	/*
3799 	 * Interlock against MP races when performing this function. XXX
3800 	 */
3801 	/* TMPFS_NODE_LOCK_SH(node); */
3802 	off = node->n_size - kn->kn_fp->f_offset;
3803 	kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
3804 	if (kn->kn_sfflags & NOTE_OLDAPI) {
3805 		/* TMPFS_NODE_UNLOCK(node); */
3806 		return(1);
3807 	}
3808 	if (kn->kn_data == 0) {
3809 		kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
3810 	}
3811 	/* TMPFS_NODE_UNLOCK(node); */
3812 	return (kn->kn_data != 0);
3813 }
3814 
3815 static int
3816 filt_nfswrite(struct knote *kn, long hint)
3817 {
3818 	if (hint == NOTE_REVOKE)
3819 		kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
3820 	kn->kn_data = 0;
3821 	return (1);
3822 }
3823 
3824 static int
3825 filt_nfsvnode(struct knote *kn, long hint)
3826 {
3827 	if (kn->kn_sfflags & hint)
3828 		kn->kn_fflags |= hint;
3829 	if (hint == NOTE_REVOKE) {
3830 		kn->kn_flags |= (EV_EOF | EV_NODATA);
3831 		return (1);
3832 	}
3833 	return (kn->kn_fflags != 0);
3834 }
3835