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