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