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