xref: /dragonfly/sys/vfs/nfs/nfs_vnops.c (revision 5f39c7e7)
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 int
1843 nfs_removeit(struct sillyrename *sp)
1844 {
1845 	return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen,
1846 		sp->s_cred, NULL));
1847 }
1848 
1849 /*
1850  * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1851  */
1852 static int
1853 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1854 	      struct ucred *cred, struct thread *td)
1855 {
1856 	int error = 0, wccflag = NFSV3_WCCRATTR;
1857 	struct nfsm_info info;
1858 
1859 	info.mrep = NULL;
1860 	info.v3 = NFS_ISV3(dvp);
1861 
1862 	nfsstats.rpccnt[NFSPROC_REMOVE]++;
1863 	nfsm_reqhead(&info, dvp, NFSPROC_REMOVE,
1864 		     NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1865 	ERROROUT(nfsm_fhtom(&info, dvp));
1866 	ERROROUT(nfsm_strtom(&info, name, namelen, NFS_MAXNAMLEN));
1867 	NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_REMOVE, td, cred, &error));
1868 	if (info.v3) {
1869 		ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
1870 	}
1871 	m_freem(info.mrep);
1872 	info.mrep = NULL;
1873 nfsmout:
1874 	VTONFS(dvp)->n_flag |= NLMODIFIED;
1875 	if (!wccflag)
1876 		VTONFS(dvp)->n_attrstamp = 0;
1877 	return (error);
1878 }
1879 
1880 /*
1881  * nfs file rename call
1882  *
1883  * nfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
1884  *	      struct componentname *a_fcnp, struct vnode *a_tdvp,
1885  *	      struct vnode *a_tvp, struct componentname *a_tcnp)
1886  */
1887 static int
1888 nfs_rename(struct vop_old_rename_args *ap)
1889 {
1890 	struct vnode *fvp = ap->a_fvp;
1891 	struct vnode *tvp = ap->a_tvp;
1892 	struct vnode *fdvp = ap->a_fdvp;
1893 	struct vnode *tdvp = ap->a_tdvp;
1894 	struct componentname *tcnp = ap->a_tcnp;
1895 	struct componentname *fcnp = ap->a_fcnp;
1896 	struct nfsmount *nmp = VFSTONFS(fdvp->v_mount);
1897 	int error;
1898 
1899 	lwkt_gettoken(&nmp->nm_token);
1900 
1901 	/* Check for cross-device rename */
1902 	if ((fvp->v_mount != tdvp->v_mount) ||
1903 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
1904 		error = EXDEV;
1905 		goto out;
1906 	}
1907 
1908 	/*
1909 	 * We shouldn't have to flush fvp on rename for most server-side
1910 	 * filesystems as the file handle should not change.  Unfortunately
1911 	 * the inode for some filesystems (msdosfs) might be tied to the
1912 	 * file name or directory position so to be completely safe
1913 	 * vfs.nfs.flush_on_rename is set by default.  Clear to improve
1914 	 * performance.
1915 	 *
1916 	 * We must flush tvp on rename because it might become stale on the
1917 	 * server after the rename.
1918 	 */
1919 	if (nfs_flush_on_rename)
1920 	    VOP_FSYNC(fvp, MNT_WAIT, 0);
1921 	if (tvp)
1922 	    VOP_FSYNC(tvp, MNT_WAIT, 0);
1923 
1924 	/*
1925 	 * If the tvp exists and is in use, sillyrename it before doing the
1926 	 * rename of the new file over it.
1927 	 *
1928 	 * XXX Can't sillyrename a directory.
1929 	 *
1930 	 * We do not attempt to do any namecache purges in this old API
1931 	 * routine.  The new API compat functions have access to the actual
1932 	 * namecache structures and will do it for us.
1933 	 */
1934 	if (tvp && VREFCNT(tvp) > 1 && !VTONFS(tvp)->n_sillyrename &&
1935 		tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1936 		vput(tvp);
1937 		tvp = NULL;
1938 	} else if (tvp) {
1939 		;
1940 	}
1941 
1942 	error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1943 		tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1944 		tcnp->cn_td);
1945 
1946 out:
1947 	lwkt_reltoken(&nmp->nm_token);
1948 	if (tdvp == tvp)
1949 		vrele(tdvp);
1950 	else
1951 		vput(tdvp);
1952 	if (tvp)
1953 		vput(tvp);
1954 	vrele(fdvp);
1955 	vrele(fvp);
1956 	/*
1957 	 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1958 	 */
1959 	if (error == ENOENT)
1960 		error = 0;
1961 	return (error);
1962 }
1963 
1964 /*
1965  * nfs file rename rpc called from nfs_remove() above
1966  */
1967 static int
1968 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
1969 	     struct sillyrename *sp)
1970 {
1971 	return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
1972 		sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_td));
1973 }
1974 
1975 /*
1976  * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1977  */
1978 static int
1979 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1980 	      struct vnode *tdvp, const char *tnameptr, int tnamelen,
1981 	      struct ucred *cred, struct thread *td)
1982 {
1983 	int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1984 	struct nfsm_info info;
1985 
1986 	info.mrep = NULL;
1987 	info.v3 = NFS_ISV3(fdvp);
1988 
1989 	nfsstats.rpccnt[NFSPROC_RENAME]++;
1990 	nfsm_reqhead(&info, fdvp, NFSPROC_RENAME,
1991 		    (NFSX_FH(info.v3) + NFSX_UNSIGNED)*2 +
1992 		    nfsm_rndup(fnamelen) + nfsm_rndup(tnamelen));
1993 	ERROROUT(nfsm_fhtom(&info, fdvp));
1994 	ERROROUT(nfsm_strtom(&info, fnameptr, fnamelen, NFS_MAXNAMLEN));
1995 	ERROROUT(nfsm_fhtom(&info, tdvp));
1996 	ERROROUT(nfsm_strtom(&info, tnameptr, tnamelen, NFS_MAXNAMLEN));
1997 	NEGKEEPOUT(nfsm_request(&info, fdvp, NFSPROC_RENAME, td, cred, &error));
1998 	if (info.v3) {
1999 		ERROROUT(nfsm_wcc_data(&info, fdvp, &fwccflag));
2000 		ERROROUT(nfsm_wcc_data(&info, tdvp, &twccflag));
2001 	}
2002 	m_freem(info.mrep);
2003 	info.mrep = NULL;
2004 nfsmout:
2005 	VTONFS(fdvp)->n_flag |= NLMODIFIED;
2006 	VTONFS(tdvp)->n_flag |= NLMODIFIED;
2007 	if (!fwccflag)
2008 		VTONFS(fdvp)->n_attrstamp = 0;
2009 	if (!twccflag)
2010 		VTONFS(tdvp)->n_attrstamp = 0;
2011 	return (error);
2012 }
2013 
2014 /*
2015  * nfs hard link create call
2016  *
2017  * nfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
2018  *	    struct componentname *a_cnp)
2019  */
2020 static int
2021 nfs_link(struct vop_old_link_args *ap)
2022 {
2023 	struct vnode *vp = ap->a_vp;
2024 	struct vnode *tdvp = ap->a_tdvp;
2025 	struct nfsmount *nmp = VFSTONFS(tdvp->v_mount);
2026 	struct componentname *cnp = ap->a_cnp;
2027 	int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
2028 	struct nfsm_info info;
2029 
2030 	if (vp->v_mount != tdvp->v_mount) {
2031 		return (EXDEV);
2032 	}
2033 	lwkt_gettoken(&nmp->nm_token);
2034 
2035 	/*
2036 	 * The attribute cache may get out of sync with the server on link.
2037 	 * Pushing writes to the server before handle was inherited from
2038 	 * long long ago and it is unclear if we still need to do this.
2039 	 * Defaults to off.
2040 	 */
2041 	if (nfs_flush_on_hlink)
2042 		VOP_FSYNC(vp, MNT_WAIT, 0);
2043 
2044 	info.mrep = NULL;
2045 	info.v3 = NFS_ISV3(vp);
2046 
2047 	nfsstats.rpccnt[NFSPROC_LINK]++;
2048 	nfsm_reqhead(&info, vp, NFSPROC_LINK,
2049 		     NFSX_FH(info.v3) * 2 + NFSX_UNSIGNED +
2050 		     nfsm_rndup(cnp->cn_namelen));
2051 	ERROROUT(nfsm_fhtom(&info, vp));
2052 	ERROROUT(nfsm_fhtom(&info, tdvp));
2053 	ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2054 			     NFS_MAXNAMLEN));
2055 	NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_LINK, cnp->cn_td,
2056 				cnp->cn_cred, &error));
2057 	if (info.v3) {
2058 		ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2059 					 NFS_LATTR_NOSHRINK));
2060 		ERROROUT(nfsm_wcc_data(&info, tdvp, &wccflag));
2061 	}
2062 	m_freem(info.mrep);
2063 	info.mrep = NULL;
2064 nfsmout:
2065 	VTONFS(tdvp)->n_flag |= NLMODIFIED;
2066 	if (!attrflag)
2067 		VTONFS(vp)->n_attrstamp = 0;
2068 	if (!wccflag)
2069 		VTONFS(tdvp)->n_attrstamp = 0;
2070 	/*
2071 	 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
2072 	 */
2073 	if (error == EEXIST)
2074 		error = 0;
2075 	lwkt_reltoken(&nmp->nm_token);
2076 	return (error);
2077 }
2078 
2079 /*
2080  * nfs symbolic link create call
2081  *
2082  * nfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
2083  *		struct componentname *a_cnp, struct vattr *a_vap,
2084  *		char *a_target)
2085  */
2086 static int
2087 nfs_symlink(struct vop_old_symlink_args *ap)
2088 {
2089 	struct vnode *dvp = ap->a_dvp;
2090 	struct vattr *vap = ap->a_vap;
2091 	struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2092 	struct componentname *cnp = ap->a_cnp;
2093 	struct nfsv2_sattr *sp;
2094 	int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
2095 	struct vnode *newvp = NULL;
2096 	struct nfsm_info info;
2097 
2098 	info.mrep = NULL;
2099 	info.v3 = NFS_ISV3(dvp);
2100 	lwkt_gettoken(&nmp->nm_token);
2101 
2102 	nfsstats.rpccnt[NFSPROC_SYMLINK]++;
2103 	slen = strlen(ap->a_target);
2104 	nfsm_reqhead(&info, dvp, NFSPROC_SYMLINK,
2105 		     NFSX_FH(info.v3) + 2*NFSX_UNSIGNED +
2106 		     nfsm_rndup(cnp->cn_namelen) +
2107 		     nfsm_rndup(slen) + NFSX_SATTR(info.v3));
2108 	ERROROUT(nfsm_fhtom(&info, dvp));
2109 	ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2110 			     NFS_MAXNAMLEN));
2111 	if (info.v3) {
2112 		nfsm_v3attrbuild(&info, vap, FALSE);
2113 	}
2114 	ERROROUT(nfsm_strtom(&info, ap->a_target, slen, NFS_MAXPATHLEN));
2115 	if (info.v3 == 0) {
2116 		sp = nfsm_build(&info, NFSX_V2SATTR);
2117 		sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
2118 		sp->sa_uid = nfs_xdrneg1;
2119 		sp->sa_gid = nfs_xdrneg1;
2120 		sp->sa_size = nfs_xdrneg1;
2121 		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2122 		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2123 	}
2124 
2125 	/*
2126 	 * Issue the NFS request and get the rpc response.
2127 	 *
2128 	 * Only NFSv3 responses returning an error of 0 actually return
2129 	 * a file handle that can be converted into newvp without having
2130 	 * to do an extra lookup rpc.
2131 	 */
2132 	NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_SYMLINK, cnp->cn_td,
2133 				cnp->cn_cred, &error));
2134 	if (info.v3) {
2135 		if (error == 0) {
2136 		       ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
2137 		}
2138 		ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2139 	}
2140 
2141 	/*
2142 	 * out code jumps -> here, mrep is also freed.
2143 	 */
2144 
2145 	m_freem(info.mrep);
2146 	info.mrep = NULL;
2147 nfsmout:
2148 
2149 	/*
2150 	 * If we get an EEXIST error, silently convert it to no-error
2151 	 * in case of an NFS retry.
2152 	 */
2153 	if (error == EEXIST)
2154 		error = 0;
2155 
2156 	/*
2157 	 * If we do not have (or no longer have) an error, and we could
2158 	 * not extract the newvp from the response due to the request being
2159 	 * NFSv2 or the error being EEXIST.  We have to do a lookup in order
2160 	 * to obtain a newvp to return.
2161 	 */
2162 	if (error == 0 && newvp == NULL) {
2163 		struct nfsnode *np = NULL;
2164 
2165 		error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
2166 				     cnp->cn_cred, cnp->cn_td, &np);
2167 		if (!error)
2168 			newvp = NFSTOV(np);
2169 	}
2170 	if (error) {
2171 		if (newvp)
2172 			vput(newvp);
2173 	} else {
2174 		*ap->a_vpp = newvp;
2175 	}
2176 	VTONFS(dvp)->n_flag |= NLMODIFIED;
2177 	if (!wccflag)
2178 		VTONFS(dvp)->n_attrstamp = 0;
2179 	lwkt_reltoken(&nmp->nm_token);
2180 
2181 	return (error);
2182 }
2183 
2184 /*
2185  * nfs make dir call
2186  *
2187  * nfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
2188  *	     struct componentname *a_cnp, struct vattr *a_vap)
2189  */
2190 static int
2191 nfs_mkdir(struct vop_old_mkdir_args *ap)
2192 {
2193 	struct vnode *dvp = ap->a_dvp;
2194 	struct vattr *vap = ap->a_vap;
2195 	struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2196 	struct componentname *cnp = ap->a_cnp;
2197 	struct nfsv2_sattr *sp;
2198 	struct nfsnode *np = NULL;
2199 	struct vnode *newvp = NULL;
2200 	struct vattr vattr;
2201 	int error = 0, wccflag = NFSV3_WCCRATTR;
2202 	int gotvp = 0;
2203 	int len;
2204 	struct nfsm_info info;
2205 
2206 	info.mrep = NULL;
2207 	info.v3 = NFS_ISV3(dvp);
2208 	lwkt_gettoken(&nmp->nm_token);
2209 
2210 	if ((error = VOP_GETATTR(dvp, &vattr)) != 0) {
2211 		lwkt_reltoken(&nmp->nm_token);
2212 		return (error);
2213 	}
2214 	len = cnp->cn_namelen;
2215 	nfsstats.rpccnt[NFSPROC_MKDIR]++;
2216 	nfsm_reqhead(&info, dvp, NFSPROC_MKDIR,
2217 		     NFSX_FH(info.v3) + NFSX_UNSIGNED +
2218 		     nfsm_rndup(len) + NFSX_SATTR(info.v3));
2219 	ERROROUT(nfsm_fhtom(&info, dvp));
2220 	ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, len, NFS_MAXNAMLEN));
2221 	if (info.v3) {
2222 		nfsm_v3attrbuild(&info, vap, FALSE);
2223 	} else {
2224 		sp = nfsm_build(&info, NFSX_V2SATTR);
2225 		sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
2226 		sp->sa_uid = nfs_xdrneg1;
2227 		sp->sa_gid = nfs_xdrneg1;
2228 		sp->sa_size = nfs_xdrneg1;
2229 		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2230 		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2231 	}
2232 	NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_MKDIR, cnp->cn_td,
2233 		    cnp->cn_cred, &error));
2234 	if (error == 0) {
2235 		ERROROUT(nfsm_mtofh(&info, dvp, &newvp, &gotvp));
2236 	}
2237 	if (info.v3) {
2238 		ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2239 	}
2240 	m_freem(info.mrep);
2241 	info.mrep = NULL;
2242 nfsmout:
2243 	VTONFS(dvp)->n_flag |= NLMODIFIED;
2244 	if (!wccflag)
2245 		VTONFS(dvp)->n_attrstamp = 0;
2246 	/*
2247 	 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2248 	 * if we can succeed in looking up the directory.
2249 	 */
2250 	if (error == EEXIST || (!error && !gotvp)) {
2251 		if (newvp) {
2252 			vrele(newvp);
2253 			newvp = NULL;
2254 		}
2255 		error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
2256 			cnp->cn_td, &np);
2257 		if (!error) {
2258 			newvp = NFSTOV(np);
2259 			if (newvp->v_type != VDIR)
2260 				error = EEXIST;
2261 		}
2262 	}
2263 	if (error) {
2264 		if (newvp)
2265 			vrele(newvp);
2266 	} else {
2267 		*ap->a_vpp = newvp;
2268 	}
2269 	lwkt_reltoken(&nmp->nm_token);
2270 	return (error);
2271 }
2272 
2273 /*
2274  * nfs remove directory call
2275  *
2276  * nfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
2277  *	     struct componentname *a_cnp)
2278  */
2279 static int
2280 nfs_rmdir(struct vop_old_rmdir_args *ap)
2281 {
2282 	struct vnode *vp = ap->a_vp;
2283 	struct vnode *dvp = ap->a_dvp;
2284 	struct nfsmount *nmp = VFSTONFS(dvp->v_mount);
2285 	struct componentname *cnp = ap->a_cnp;
2286 	int error = 0, wccflag = NFSV3_WCCRATTR;
2287 	struct nfsm_info info;
2288 
2289 	info.mrep = NULL;
2290 	info.v3 = NFS_ISV3(dvp);
2291 
2292 	if (dvp == vp)
2293 		return (EINVAL);
2294 
2295 	lwkt_gettoken(&nmp->nm_token);
2296 
2297 	nfsstats.rpccnt[NFSPROC_RMDIR]++;
2298 	nfsm_reqhead(&info, dvp, NFSPROC_RMDIR,
2299 		     NFSX_FH(info.v3) + NFSX_UNSIGNED +
2300 		     nfsm_rndup(cnp->cn_namelen));
2301 	ERROROUT(nfsm_fhtom(&info, dvp));
2302 	ERROROUT(nfsm_strtom(&info, cnp->cn_nameptr, cnp->cn_namelen,
2303 		 NFS_MAXNAMLEN));
2304 	NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_RMDIR, cnp->cn_td,
2305 				cnp->cn_cred, &error));
2306 	if (info.v3) {
2307 		ERROROUT(nfsm_wcc_data(&info, dvp, &wccflag));
2308 	}
2309 	m_freem(info.mrep);
2310 	info.mrep = NULL;
2311 nfsmout:
2312 	VTONFS(dvp)->n_flag |= NLMODIFIED;
2313 	if (!wccflag)
2314 		VTONFS(dvp)->n_attrstamp = 0;
2315 	/*
2316 	 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2317 	 */
2318 	if (error == ENOENT)
2319 		error = 0;
2320 	lwkt_reltoken(&nmp->nm_token);
2321 
2322 	return (error);
2323 }
2324 
2325 /*
2326  * nfs readdir call
2327  *
2328  * nfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
2329  */
2330 static int
2331 nfs_readdir(struct vop_readdir_args *ap)
2332 {
2333 	struct vnode *vp = ap->a_vp;
2334 	struct nfsnode *np = VTONFS(vp);
2335 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2336 	struct uio *uio = ap->a_uio;
2337 	int tresid, error;
2338 	struct vattr vattr;
2339 
2340 	if (vp->v_type != VDIR)
2341 		return (EPERM);
2342 
2343 	error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
2344 	if (error)
2345 		return (error);
2346 
2347 	lwkt_gettoken(&nmp->nm_token);
2348 
2349 	/*
2350 	 * If we have a valid EOF offset cache we must call VOP_GETATTR()
2351 	 * and then check that is still valid, or if this is an NQNFS mount
2352 	 * we call NQNFS_CKCACHEABLE() instead of VOP_GETATTR().  Note that
2353 	 * VOP_GETATTR() does not necessarily go to the wire.
2354 	 */
2355 	if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2356 	    (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0) {
2357 		if (VOP_GETATTR(vp, &vattr) == 0 &&
2358 		    (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0
2359 		) {
2360 			nfsstats.direofcache_hits++;
2361 			goto done;
2362 		}
2363 	}
2364 
2365 	/*
2366 	 * Call nfs_bioread() to do the real work.  nfs_bioread() does its
2367 	 * own cache coherency checks so we do not have to.
2368 	 */
2369 	tresid = uio->uio_resid;
2370 	error = nfs_bioread(vp, uio, 0);
2371 
2372 	if (!error && uio->uio_resid == tresid)
2373 		nfsstats.direofcache_misses++;
2374 done:
2375 	lwkt_reltoken(&nmp->nm_token);
2376 	vn_unlock(vp);
2377 
2378 	return (error);
2379 }
2380 
2381 /*
2382  * Readdir rpc call.  nfs_bioread->nfs_doio->nfs_readdirrpc.
2383  *
2384  * Note that for directories, nfs_bioread maintains the underlying nfs-centric
2385  * offset/block and converts the nfs formatted directory entries for userland
2386  * consumption as well as deals with offsets into the middle of blocks.
2387  * nfs_doio only deals with logical blocks.  In particular, uio_offset will
2388  * be block-bounded.  It must convert to cookies for the actual RPC.
2389  */
2390 int
2391 nfs_readdirrpc_uio(struct vnode *vp, struct uio *uiop)
2392 {
2393 	int len, left;
2394 	struct nfs_dirent *dp = NULL;
2395 	u_int32_t *tl;
2396 	nfsuint64 *cookiep;
2397 	caddr_t cp;
2398 	nfsuint64 cookie;
2399 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2400 	struct nfsnode *dnp = VTONFS(vp);
2401 	u_quad_t fileno;
2402 	int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2403 	int attrflag;
2404 	struct nfsm_info info;
2405 
2406 	info.mrep = NULL;
2407 	info.v3 = NFS_ISV3(vp);
2408 
2409 #ifndef DIAGNOSTIC
2410 	if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2411 		(uiop->uio_resid & (DIRBLKSIZ - 1)))
2412 		panic("nfs readdirrpc bad uio");
2413 #endif
2414 
2415 	/*
2416 	 * If there is no cookie, assume directory was stale.
2417 	 */
2418 	cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2419 	if (cookiep)
2420 		cookie = *cookiep;
2421 	else
2422 		return (NFSERR_BAD_COOKIE);
2423 	/*
2424 	 * Loop around doing readdir rpc's of size nm_readdirsize
2425 	 * truncated to a multiple of DIRBLKSIZ.
2426 	 * The stopping criteria is EOF or buffer full.
2427 	 */
2428 	while (more_dirs && bigenough) {
2429 		nfsstats.rpccnt[NFSPROC_READDIR]++;
2430 		nfsm_reqhead(&info, vp, NFSPROC_READDIR,
2431 			     NFSX_FH(info.v3) + NFSX_READDIR(info.v3));
2432 		ERROROUT(nfsm_fhtom(&info, vp));
2433 		if (info.v3) {
2434 			tl = nfsm_build(&info, 5 * NFSX_UNSIGNED);
2435 			*tl++ = cookie.nfsuquad[0];
2436 			*tl++ = cookie.nfsuquad[1];
2437 			*tl++ = dnp->n_cookieverf.nfsuquad[0];
2438 			*tl++ = dnp->n_cookieverf.nfsuquad[1];
2439 		} else {
2440 			/*
2441 			 * WARNING!  HAMMER DIRECTORIES WILL NOT WORK WELL
2442 			 * WITH NFSv2!!!  There's nothing I can really do
2443 			 * about it other than to hope the server supports
2444 			 * rdirplus w/NFSv2.
2445 			 */
2446 			tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
2447 			*tl++ = cookie.nfsuquad[0];
2448 		}
2449 		*tl = txdr_unsigned(nmp->nm_readdirsize);
2450 		NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READDIR,
2451 					uiop->uio_td,
2452 					nfs_vpcred(vp, ND_READ), &error));
2453 		if (info.v3) {
2454 			ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2455 						  NFS_LATTR_NOSHRINK));
2456 			NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2457 			dnp->n_cookieverf.nfsuquad[0] = *tl++;
2458 			dnp->n_cookieverf.nfsuquad[1] = *tl;
2459 		}
2460 		NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2461 		more_dirs = fxdr_unsigned(int, *tl);
2462 
2463 		/* loop thru the dir entries, converting them to std form */
2464 		while (more_dirs && bigenough) {
2465 			if (info.v3) {
2466 				NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2467 				fileno = fxdr_hyper(tl);
2468 				len = fxdr_unsigned(int, *(tl + 2));
2469 			} else {
2470 				NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2471 				fileno = fxdr_unsigned(u_quad_t, *tl++);
2472 				len = fxdr_unsigned(int, *tl);
2473 			}
2474 			if (len <= 0 || len > NFS_MAXNAMLEN) {
2475 				error = EBADRPC;
2476 				m_freem(info.mrep);
2477 				info.mrep = NULL;
2478 				goto nfsmout;
2479 			}
2480 
2481 			/*
2482 			 * len is the number of bytes in the path element
2483 			 * name, not including the \0 termination.
2484 			 *
2485 			 * tlen is the number of bytes w have to reserve for
2486 			 * the path element name.
2487 			 */
2488 			tlen = nfsm_rndup(len);
2489 			if (tlen == len)
2490 				tlen += 4;	/* To ensure null termination */
2491 
2492 			/*
2493 			 * If the entry would cross a DIRBLKSIZ boundary,
2494 			 * extend the previous nfs_dirent to cover the
2495 			 * remaining space.
2496 			 */
2497 			left = DIRBLKSIZ - blksiz;
2498 			if ((tlen + sizeof(struct nfs_dirent)) > left) {
2499 				dp->nfs_reclen += left;
2500 				uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2501 				uiop->uio_iov->iov_len -= left;
2502 				uiop->uio_offset += left;
2503 				uiop->uio_resid -= left;
2504 				blksiz = 0;
2505 			}
2506 			if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2507 				bigenough = 0;
2508 			if (bigenough) {
2509 				dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2510 				dp->nfs_ino = fileno;
2511 				dp->nfs_namlen = len;
2512 				dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2513 				dp->nfs_type = DT_UNKNOWN;
2514 				blksiz += dp->nfs_reclen;
2515 				if (blksiz == DIRBLKSIZ)
2516 					blksiz = 0;
2517 				uiop->uio_offset += sizeof(struct nfs_dirent);
2518 				uiop->uio_resid -= sizeof(struct nfs_dirent);
2519 				uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + sizeof(struct nfs_dirent);
2520 				uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2521 				ERROROUT(nfsm_mtouio(&info, uiop, len));
2522 
2523 				/*
2524 				 * The uiop has advanced by nfs_dirent + len
2525 				 * but really needs to advance by
2526 				 * nfs_dirent + tlen
2527 				 */
2528 				cp = uiop->uio_iov->iov_base;
2529 				tlen -= len;
2530 				*cp = '\0';	/* null terminate */
2531 				uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + tlen;
2532 				uiop->uio_iov->iov_len -= tlen;
2533 				uiop->uio_offset += tlen;
2534 				uiop->uio_resid -= tlen;
2535 			} else {
2536 				/*
2537 				 * NFS strings must be rounded up (nfsm_myouio
2538 				 * handled that in the bigenough case).
2539 				 */
2540 				ERROROUT(nfsm_adv(&info, nfsm_rndup(len)));
2541 			}
2542 			if (info.v3) {
2543 				NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2544 			} else {
2545 				NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2546 			}
2547 
2548 			/*
2549 			 * If we were able to accomodate the last entry,
2550 			 * get the cookie for the next one.  Otherwise
2551 			 * hold-over the cookie for the one we were not
2552 			 * able to accomodate.
2553 			 */
2554 			if (bigenough) {
2555 				cookie.nfsuquad[0] = *tl++;
2556 				if (info.v3)
2557 					cookie.nfsuquad[1] = *tl++;
2558 			} else if (info.v3) {
2559 				tl += 2;
2560 			} else {
2561 				tl++;
2562 			}
2563 			more_dirs = fxdr_unsigned(int, *tl);
2564 		}
2565 		/*
2566 		 * If at end of rpc data, get the eof boolean
2567 		 */
2568 		if (!more_dirs) {
2569 			NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2570 			more_dirs = (fxdr_unsigned(int, *tl) == 0);
2571 		}
2572 		m_freem(info.mrep);
2573 		info.mrep = NULL;
2574 	}
2575 	/*
2576 	 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2577 	 * by increasing d_reclen for the last record.
2578 	 */
2579 	if (blksiz > 0) {
2580 		left = DIRBLKSIZ - blksiz;
2581 		dp->nfs_reclen += left;
2582 		uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2583 		uiop->uio_iov->iov_len -= left;
2584 		uiop->uio_offset += left;
2585 		uiop->uio_resid -= left;
2586 	}
2587 
2588 	if (bigenough) {
2589 		/*
2590 		 * We hit the end of the directory, update direofoffset.
2591 		 */
2592 		dnp->n_direofoffset = uiop->uio_offset;
2593 	} else {
2594 		/*
2595 		 * There is more to go, insert the link cookie so the
2596 		 * next block can be read.
2597 		 */
2598 		if (uiop->uio_resid > 0)
2599 			kprintf("EEK! readdirrpc resid > 0\n");
2600 		cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2601 		*cookiep = cookie;
2602 	}
2603 nfsmout:
2604 	return (error);
2605 }
2606 
2607 /*
2608  * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2609  */
2610 int
2611 nfs_readdirplusrpc_uio(struct vnode *vp, struct uio *uiop)
2612 {
2613 	int len, left;
2614 	struct nfs_dirent *dp;
2615 	u_int32_t *tl;
2616 	struct vnode *newvp;
2617 	nfsuint64 *cookiep;
2618 	caddr_t dpossav1, dpossav2;
2619 	caddr_t cp;
2620 	struct mbuf *mdsav1, *mdsav2;
2621 	nfsuint64 cookie;
2622 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2623 	struct nfsnode *dnp = VTONFS(vp), *np;
2624 	nfsfh_t *fhp;
2625 	u_quad_t fileno;
2626 	int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2627 	int attrflag, fhsize;
2628 	struct nchandle nch;
2629 	struct nchandle dnch;
2630 	struct nlcomponent nlc;
2631 	struct nfsm_info info;
2632 
2633 	info.mrep = NULL;
2634 	info.v3 = 1;
2635 
2636 #ifndef nolint
2637 	dp = NULL;
2638 #endif
2639 #ifndef DIAGNOSTIC
2640 	if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2641 		(uiop->uio_resid & (DIRBLKSIZ - 1)))
2642 		panic("nfs readdirplusrpc bad uio");
2643 #endif
2644 	/*
2645 	 * Obtain the namecache record for the directory so we have something
2646 	 * to use as a basis for creating the entries.  This function will
2647 	 * return a held (but not locked) ncp.  The ncp may be disconnected
2648 	 * from the tree and cannot be used for upward traversals, and the
2649 	 * ncp may be unnamed.  Note that other unrelated operations may
2650 	 * cause the ncp to be named at any time.
2651 	 *
2652 	 * We have to lock the ncp to prevent a lock order reversal when
2653 	 * rdirplus does nlookups of the children, because the vnode is
2654 	 * locked and has to stay that way.
2655 	 */
2656 	cache_fromdvp(vp, NULL, 0, &dnch);
2657 	bzero(&nlc, sizeof(nlc));
2658 	newvp = NULLVP;
2659 
2660 	/*
2661 	 * If there is no cookie, assume directory was stale.
2662 	 */
2663 	cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2664 	if (cookiep) {
2665 		cookie = *cookiep;
2666 	} else {
2667 		if (dnch.ncp)
2668 			cache_drop(&dnch);
2669 		return (NFSERR_BAD_COOKIE);
2670 	}
2671 
2672 	/*
2673 	 * Loop around doing readdir rpc's of size nm_readdirsize
2674 	 * truncated to a multiple of DIRBLKSIZ.
2675 	 * The stopping criteria is EOF or buffer full.
2676 	 */
2677 	while (more_dirs && bigenough) {
2678 		nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2679 		nfsm_reqhead(&info, vp, NFSPROC_READDIRPLUS,
2680 			     NFSX_FH(info.v3) + 6 * NFSX_UNSIGNED);
2681 		ERROROUT(nfsm_fhtom(&info, vp));
2682 		tl = nfsm_build(&info, 6 * NFSX_UNSIGNED);
2683 		*tl++ = cookie.nfsuquad[0];
2684 		*tl++ = cookie.nfsuquad[1];
2685 		*tl++ = dnp->n_cookieverf.nfsuquad[0];
2686 		*tl++ = dnp->n_cookieverf.nfsuquad[1];
2687 		*tl++ = txdr_unsigned(nmp->nm_readdirsize);
2688 		*tl = txdr_unsigned(nmp->nm_rsize);
2689 		NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_READDIRPLUS,
2690 					uiop->uio_td,
2691 					nfs_vpcred(vp, ND_READ), &error));
2692 		ERROROUT(nfsm_postop_attr(&info, vp, &attrflag,
2693 					  NFS_LATTR_NOSHRINK));
2694 		NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2695 		dnp->n_cookieverf.nfsuquad[0] = *tl++;
2696 		dnp->n_cookieverf.nfsuquad[1] = *tl++;
2697 		more_dirs = fxdr_unsigned(int, *tl);
2698 
2699 		/* loop thru the dir entries, doctoring them to 4bsd form */
2700 		while (more_dirs && bigenough) {
2701 			NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2702 			fileno = fxdr_hyper(tl);
2703 			len = fxdr_unsigned(int, *(tl + 2));
2704 			if (len <= 0 || len > NFS_MAXNAMLEN) {
2705 				error = EBADRPC;
2706 				m_freem(info.mrep);
2707 				info.mrep = NULL;
2708 				goto nfsmout;
2709 			}
2710 			tlen = nfsm_rndup(len);
2711 			if (tlen == len)
2712 				tlen += 4;	/* To ensure null termination*/
2713 			left = DIRBLKSIZ - blksiz;
2714 			if ((tlen + sizeof(struct nfs_dirent)) > left) {
2715 				dp->nfs_reclen += left;
2716 				uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2717 				uiop->uio_iov->iov_len -= left;
2718 				uiop->uio_offset += left;
2719 				uiop->uio_resid -= left;
2720 				blksiz = 0;
2721 			}
2722 			if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2723 				bigenough = 0;
2724 			if (bigenough) {
2725 				dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2726 				dp->nfs_ino = fileno;
2727 				dp->nfs_namlen = len;
2728 				dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2729 				dp->nfs_type = DT_UNKNOWN;
2730 				blksiz += dp->nfs_reclen;
2731 				if (blksiz == DIRBLKSIZ)
2732 					blksiz = 0;
2733 				uiop->uio_offset += sizeof(struct nfs_dirent);
2734 				uiop->uio_resid -= sizeof(struct nfs_dirent);
2735 				uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + sizeof(struct nfs_dirent);
2736 				uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2737 				nlc.nlc_nameptr = uiop->uio_iov->iov_base;
2738 				nlc.nlc_namelen = len;
2739 				ERROROUT(nfsm_mtouio(&info, uiop, len));
2740 				cp = uiop->uio_iov->iov_base;
2741 				tlen -= len;
2742 				*cp = '\0';
2743 				uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + tlen;
2744 				uiop->uio_iov->iov_len -= tlen;
2745 				uiop->uio_offset += tlen;
2746 				uiop->uio_resid -= tlen;
2747 			} else {
2748 				ERROROUT(nfsm_adv(&info, nfsm_rndup(len)));
2749 			}
2750 			NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2751 			if (bigenough) {
2752 				cookie.nfsuquad[0] = *tl++;
2753 				cookie.nfsuquad[1] = *tl++;
2754 			} else {
2755 				tl += 2;
2756 			}
2757 
2758 			/*
2759 			 * Since the attributes are before the file handle
2760 			 * (sigh), we must skip over the attributes and then
2761 			 * come back and get them.
2762 			 */
2763 			attrflag = fxdr_unsigned(int, *tl);
2764 			if (attrflag) {
2765 			    dpossav1 = info.dpos;
2766 			    mdsav1 = info.md;
2767 			    ERROROUT(nfsm_adv(&info, NFSX_V3FATTR));
2768 			    NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2769 			    doit = fxdr_unsigned(int, *tl);
2770 			    if (doit) {
2771 				NEGATIVEOUT(fhsize = nfsm_getfh(&info, &fhp));
2772 			    }
2773 			    if (doit && bigenough && !nlcdegenerate(&nlc) &&
2774 				!NFS_CMPFH(dnp, fhp, fhsize)
2775 			    ) {
2776 				if (dnch.ncp) {
2777 #if 0
2778 				    kprintf("NFS/READDIRPLUS, ENTER %*.*s\n",
2779 					nlc.nlc_namelen, nlc.nlc_namelen,
2780 					nlc.nlc_nameptr);
2781 #endif
2782 				    /*
2783 				     * This is a bit hokey but there isn't
2784 				     * much we can do about it.  We can't
2785 				     * hold the directory vp locked while
2786 				     * doing lookups and gets.
2787 				     */
2788 				    nch = cache_nlookup_nonblock(&dnch, &nlc);
2789 				    if (nch.ncp == NULL)
2790 					goto rdfail;
2791 				    cache_setunresolved(&nch);
2792 				    error = nfs_nget_nonblock(vp->v_mount, fhp,
2793 							      fhsize, &np,
2794 							      NULL);
2795 				    if (error) {
2796 					cache_put(&nch);
2797 					goto rdfail;
2798 				    }
2799 				    newvp = NFSTOV(np);
2800 				    dpossav2 = info.dpos;
2801 				    info.dpos = dpossav1;
2802 				    mdsav2 = info.md;
2803 				    info.md = mdsav1;
2804 				    ERROROUT(nfsm_loadattr(&info, newvp, NULL));
2805 				    info.dpos = dpossav2;
2806 				    info.md = mdsav2;
2807 				    dp->nfs_type =
2808 					    IFTODT(VTTOIF(np->n_vattr.va_type));
2809 				    nfs_cache_setvp(&nch, newvp,
2810 						    nfspos_cache_timeout);
2811 				    vput(newvp);
2812 				    newvp = NULLVP;
2813 				    cache_put(&nch);
2814 				} else {
2815 rdfail:
2816 				    ;
2817 #if 0
2818 				    kprintf("Warning: NFS/rddirplus, "
2819 					    "UNABLE TO ENTER %*.*s\n",
2820 					nlc.nlc_namelen, nlc.nlc_namelen,
2821 					nlc.nlc_nameptr);
2822 #endif
2823 				}
2824 			    }
2825 			} else {
2826 			    /* Just skip over the file handle */
2827 			    NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2828 			    i = fxdr_unsigned(int, *tl);
2829 			    ERROROUT(nfsm_adv(&info, nfsm_rndup(i)));
2830 			}
2831 			NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2832 			more_dirs = fxdr_unsigned(int, *tl);
2833 		}
2834 		/*
2835 		 * If at end of rpc data, get the eof boolean
2836 		 */
2837 		if (!more_dirs) {
2838 			NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2839 			more_dirs = (fxdr_unsigned(int, *tl) == 0);
2840 		}
2841 		m_freem(info.mrep);
2842 		info.mrep = NULL;
2843 	}
2844 	/*
2845 	 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2846 	 * by increasing d_reclen for the last record.
2847 	 */
2848 	if (blksiz > 0) {
2849 		left = DIRBLKSIZ - blksiz;
2850 		dp->nfs_reclen += left;
2851 		uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + left;
2852 		uiop->uio_iov->iov_len -= left;
2853 		uiop->uio_offset += left;
2854 		uiop->uio_resid -= left;
2855 	}
2856 
2857 	/*
2858 	 * We are now either at the end of the directory or have filled the
2859 	 * block.
2860 	 */
2861 	if (bigenough) {
2862 		dnp->n_direofoffset = uiop->uio_offset;
2863 	} else {
2864 		if (uiop->uio_resid > 0)
2865 			kprintf("EEK! readdirplusrpc resid > 0\n");
2866 		cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2867 		*cookiep = cookie;
2868 	}
2869 nfsmout:
2870 	if (newvp != NULLVP) {
2871 	        if (newvp == vp)
2872 			vrele(newvp);
2873 		else
2874 			vput(newvp);
2875 		newvp = NULLVP;
2876 	}
2877 	if (dnch.ncp)
2878 		cache_drop(&dnch);
2879 	return (error);
2880 }
2881 
2882 /*
2883  * Silly rename. To make the NFS filesystem that is stateless look a little
2884  * more like the "ufs" a remove of an active vnode is translated to a rename
2885  * to a funny looking filename that is removed by nfs_inactive on the
2886  * nfsnode. There is the potential for another process on a different client
2887  * to create the same funny name between the nfs_lookitup() fails and the
2888  * nfs_rename() completes, but...
2889  */
2890 static int
2891 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2892 {
2893 	struct sillyrename *sp;
2894 	struct nfsnode *np;
2895 	int error;
2896 
2897 	/*
2898 	 * We previously purged dvp instead of vp.  I don't know why, it
2899 	 * completely destroys performance.  We can't do it anyway with the
2900 	 * new VFS API since we would be breaking the namecache topology.
2901 	 */
2902 	cache_purge(vp);	/* XXX */
2903 	np = VTONFS(vp);
2904 #ifndef DIAGNOSTIC
2905 	if (vp->v_type == VDIR)
2906 		panic("nfs: sillyrename dir");
2907 #endif
2908 	sp = kmalloc(sizeof(struct sillyrename), M_NFSREQ, M_WAITOK);
2909 	sp->s_cred = crdup(cnp->cn_cred);
2910 	sp->s_dvp = dvp;
2911 	vref(dvp);
2912 
2913 	/* Fudge together a funny name */
2914 	sp->s_namlen = ksprintf(sp->s_name, ".nfsA%08x4.4",
2915 				(int)(intptr_t)cnp->cn_td);
2916 
2917 	/* Try lookitups until we get one that isn't there */
2918 	while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2919 		cnp->cn_td, NULL) == 0) {
2920 		sp->s_name[4]++;
2921 		if (sp->s_name[4] > 'z') {
2922 			error = EINVAL;
2923 			goto bad;
2924 		}
2925 	}
2926 	error = nfs_renameit(dvp, cnp, sp);
2927 	if (error)
2928 		goto bad;
2929 	error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2930 		cnp->cn_td, &np);
2931 	np->n_sillyrename = sp;
2932 	return (0);
2933 bad:
2934 	vrele(sp->s_dvp);
2935 	crfree(sp->s_cred);
2936 	kfree((caddr_t)sp, M_NFSREQ);
2937 	return (error);
2938 }
2939 
2940 /*
2941  * Look up a file name and optionally either update the file handle or
2942  * allocate an nfsnode, depending on the value of npp.
2943  * npp == NULL	--> just do the lookup
2944  * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2945  *			handled too
2946  * *npp != NULL --> update the file handle in the vnode
2947  */
2948 static int
2949 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2950 	     struct thread *td, struct nfsnode **npp)
2951 {
2952 	struct vnode *newvp = NULL;
2953 	struct nfsnode *np, *dnp = VTONFS(dvp);
2954 	int error = 0, fhlen, attrflag;
2955 	nfsfh_t *nfhp;
2956 	struct nfsm_info info;
2957 
2958 	info.mrep = NULL;
2959 	info.v3 = NFS_ISV3(dvp);
2960 
2961 	nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2962 	nfsm_reqhead(&info, dvp, NFSPROC_LOOKUP,
2963 		     NFSX_FH(info.v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2964 	ERROROUT(nfsm_fhtom(&info, dvp));
2965 	ERROROUT(nfsm_strtom(&info, name, len, NFS_MAXNAMLEN));
2966 	NEGKEEPOUT(nfsm_request(&info, dvp, NFSPROC_LOOKUP, td, cred, &error));
2967 	if (npp && !error) {
2968 		NEGATIVEOUT(fhlen = nfsm_getfh(&info, &nfhp));
2969 		if (*npp) {
2970 		    np = *npp;
2971 		    if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2972 			kfree((caddr_t)np->n_fhp, M_NFSBIGFH);
2973 			np->n_fhp = &np->n_fh;
2974 		    } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2975 			np->n_fhp =(nfsfh_t *)kmalloc(fhlen,M_NFSBIGFH,M_WAITOK);
2976 		    bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2977 		    np->n_fhsize = fhlen;
2978 		    newvp = NFSTOV(np);
2979 		} else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2980 		    vref(dvp);
2981 		    newvp = dvp;
2982 		} else {
2983 		    error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np, NULL);
2984 		    if (error) {
2985 			m_freem(info.mrep);
2986 			info.mrep = NULL;
2987 			return (error);
2988 		    }
2989 		    newvp = NFSTOV(np);
2990 		}
2991 		if (info.v3) {
2992 			ERROROUT(nfsm_postop_attr(&info, newvp, &attrflag,
2993 						  NFS_LATTR_NOSHRINK));
2994 			if (!attrflag && *npp == NULL) {
2995 				m_freem(info.mrep);
2996 				info.mrep = NULL;
2997 				if (newvp == dvp)
2998 					vrele(newvp);
2999 				else
3000 					vput(newvp);
3001 				return (ENOENT);
3002 			}
3003 		} else {
3004 			ERROROUT(nfsm_loadattr(&info, newvp, NULL));
3005 		}
3006 	}
3007 	m_freem(info.mrep);
3008 	info.mrep = NULL;
3009 nfsmout:
3010 	if (npp && *npp == NULL) {
3011 		if (error) {
3012 			if (newvp) {
3013 				if (newvp == dvp)
3014 					vrele(newvp);
3015 				else
3016 					vput(newvp);
3017 			}
3018 		} else
3019 			*npp = np;
3020 	}
3021 	return (error);
3022 }
3023 
3024 /*
3025  * Nfs Version 3 commit rpc
3026  *
3027  * We call it 'uio' to distinguish it from 'bio' but there is no real uio
3028  * involved.
3029  */
3030 int
3031 nfs_commitrpc_uio(struct vnode *vp, u_quad_t offset, int cnt, struct thread *td)
3032 {
3033 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3034 	int error = 0, wccflag = NFSV3_WCCRATTR;
3035 	struct nfsm_info info;
3036 	u_int32_t *tl;
3037 
3038 	info.mrep = NULL;
3039 	info.v3 = 1;
3040 
3041 	if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
3042 		return (0);
3043 	nfsstats.rpccnt[NFSPROC_COMMIT]++;
3044 	nfsm_reqhead(&info, vp, NFSPROC_COMMIT, NFSX_FH(1));
3045 	ERROROUT(nfsm_fhtom(&info, vp));
3046 	tl = nfsm_build(&info, 3 * NFSX_UNSIGNED);
3047 	txdr_hyper(offset, tl);
3048 	tl += 2;
3049 	*tl = txdr_unsigned(cnt);
3050 	NEGKEEPOUT(nfsm_request(&info, vp, NFSPROC_COMMIT, td,
3051 				nfs_vpcred(vp, ND_WRITE), &error));
3052 	ERROROUT(nfsm_wcc_data(&info, vp, &wccflag));
3053 	if (!error) {
3054 		NULLOUT(tl = nfsm_dissect(&info, NFSX_V3WRITEVERF));
3055 		if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
3056 			NFSX_V3WRITEVERF)) {
3057 			bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
3058 				NFSX_V3WRITEVERF);
3059 			error = NFSERR_STALEWRITEVERF;
3060 		}
3061 	}
3062 	m_freem(info.mrep);
3063 	info.mrep = NULL;
3064 nfsmout:
3065 	return (error);
3066 }
3067 
3068 /*
3069  * Kludge City..
3070  * - make nfs_bmap() essentially a no-op that does no translation
3071  * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
3072  *   (Maybe I could use the process's page mapping, but I was concerned that
3073  *    Kernel Write might not be enabled and also figured copyout() would do
3074  *    a lot more work than bcopy() and also it currently happens in the
3075  *    context of the swapper process (2).
3076  *
3077  * nfs_bmap(struct vnode *a_vp, off_t a_loffset,
3078  *	    off_t *a_doffsetp, int *a_runp, int *a_runb)
3079  */
3080 static int
3081 nfs_bmap(struct vop_bmap_args *ap)
3082 {
3083 	/* no token lock required */
3084 	if (ap->a_doffsetp != NULL)
3085 		*ap->a_doffsetp = ap->a_loffset;
3086 	if (ap->a_runp != NULL)
3087 		*ap->a_runp = 0;
3088 	if (ap->a_runb != NULL)
3089 		*ap->a_runb = 0;
3090 	return (0);
3091 }
3092 
3093 /*
3094  * Strategy routine.
3095  */
3096 static int
3097 nfs_strategy(struct vop_strategy_args *ap)
3098 {
3099 	struct bio *bio = ap->a_bio;
3100 	struct bio *nbio;
3101 	struct buf *bp __debugvar = bio->bio_buf;
3102 	struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
3103 	struct thread *td;
3104 	int error;
3105 
3106 	KASSERT(bp->b_cmd != BUF_CMD_DONE,
3107 		("nfs_strategy: buffer %p unexpectedly marked done", bp));
3108 	KASSERT(BUF_REFCNT(bp) > 0,
3109 		("nfs_strategy: buffer %p not locked", bp));
3110 
3111 	if (bio->bio_flags & BIO_SYNC)
3112 		td = curthread;	/* XXX */
3113 	else
3114 		td = NULL;
3115 
3116 	lwkt_gettoken(&nmp->nm_token);
3117 
3118         /*
3119 	 * We probably don't need to push an nbio any more since no
3120 	 * block conversion is required due to the use of 64 bit byte
3121 	 * offsets, but do it anyway.
3122 	 *
3123 	 * NOTE: When NFS callers itself via this strategy routines and
3124 	 *	 sets up a synchronous I/O, it expects the I/O to run
3125 	 *	 synchronously (its bio_done routine just assumes it),
3126 	 *	 so for now we have to honor the bit.
3127          */
3128 	nbio = push_bio(bio);
3129 	nbio->bio_offset = bio->bio_offset;
3130 	nbio->bio_flags = bio->bio_flags & BIO_SYNC;
3131 
3132 	/*
3133 	 * If the op is asynchronous and an i/o daemon is waiting
3134 	 * queue the request, wake it up and wait for completion
3135 	 * otherwise just do it ourselves.
3136 	 */
3137 	if (bio->bio_flags & BIO_SYNC) {
3138 		error = nfs_doio(ap->a_vp, nbio, td);
3139 	} else {
3140 		nfs_asyncio(ap->a_vp, nbio);
3141 		error = 0;
3142 	}
3143 	lwkt_reltoken(&nmp->nm_token);
3144 
3145 	return (error);
3146 }
3147 
3148 /*
3149  * Mmap a file
3150  *
3151  * NB Currently unsupported.
3152  *
3153  * nfs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred)
3154  */
3155 /* ARGSUSED */
3156 static int
3157 nfs_mmap(struct vop_mmap_args *ap)
3158 {
3159 	/* no token lock required */
3160 	return (EINVAL);
3161 }
3162 
3163 /*
3164  * fsync vnode op. Just call nfs_flush() with commit == 1.
3165  *
3166  * nfs_fsync(struct vnode *a_vp, int a_waitfor)
3167  */
3168 /* ARGSUSED */
3169 static int
3170 nfs_fsync(struct vop_fsync_args *ap)
3171 {
3172 	struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
3173 	int error;
3174 
3175 	lwkt_gettoken(&nmp->nm_token);
3176 
3177 	/*
3178 	 * NOTE: Because attributes are set synchronously we currently
3179 	 *	 do not have to implement vsetisdirty()/vclrisdirty().
3180 	 */
3181 	error = nfs_flush(ap->a_vp, ap->a_waitfor, curthread, 1);
3182 
3183 	lwkt_reltoken(&nmp->nm_token);
3184 
3185 	return error;
3186 }
3187 
3188 /*
3189  * Flush all the blocks associated with a vnode.   Dirty NFS buffers may be
3190  * in one of two states:  If B_NEEDCOMMIT is clear then the buffer contains
3191  * new NFS data which needs to be written to the server.  If B_NEEDCOMMIT is
3192  * set the buffer contains data that has already been written to the server
3193  * and which now needs a commit RPC.
3194  *
3195  * If commit is 0 we only take one pass and only flush buffers containing new
3196  * dirty data.
3197  *
3198  * If commit is 1 we take two passes, issuing a commit RPC in the second
3199  * pass.
3200  *
3201  * If waitfor is MNT_WAIT and commit is 1, we loop as many times as required
3202  * to completely flush all pending data.
3203  *
3204  * Note that the RB_SCAN code properly handles the case where the
3205  * callback might block and directly or indirectly (another thread) cause
3206  * the RB tree to change.
3207  */
3208 
3209 #ifndef NFS_COMMITBVECSIZ
3210 #define NFS_COMMITBVECSIZ	16
3211 #endif
3212 
3213 struct nfs_flush_info {
3214 	enum { NFI_FLUSHNEW, NFI_COMMIT } mode;
3215 	struct thread *td;
3216 	struct vnode *vp;
3217 	int waitfor;
3218 	int slpflag;
3219 	int slptimeo;
3220 	int loops;
3221 	struct buf *bvary[NFS_COMMITBVECSIZ];
3222 	int bvsize;
3223 	off_t beg_off;
3224 	off_t end_off;
3225 };
3226 
3227 static int nfs_flush_bp(struct buf *bp, void *data);
3228 static int nfs_flush_docommit(struct nfs_flush_info *info, int error);
3229 
3230 int
3231 nfs_flush(struct vnode *vp, int waitfor, struct thread *td, int commit)
3232 {
3233 	struct nfsnode *np = VTONFS(vp);
3234 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
3235 	struct nfs_flush_info info;
3236 	int error;
3237 
3238 	bzero(&info, sizeof(info));
3239 	info.td = td;
3240 	info.vp = vp;
3241 	info.waitfor = waitfor;
3242 	info.slpflag = (nmp->nm_flag & NFSMNT_INT) ? PCATCH : 0;
3243 	info.loops = 0;
3244 	lwkt_gettoken(&vp->v_token);
3245 
3246 	do {
3247 		/*
3248 		 * Flush mode
3249 		 */
3250 		info.mode = NFI_FLUSHNEW;
3251 		error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
3252 				nfs_flush_bp, &info);
3253 
3254 		/*
3255 		 * Take a second pass if committing and no error occured.
3256 		 * Clean up any left over collection (whether an error
3257 		 * occurs or not).
3258 		 */
3259 		if (commit && error == 0) {
3260 			info.mode = NFI_COMMIT;
3261 			error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
3262 					nfs_flush_bp, &info);
3263 			if (info.bvsize)
3264 				error = nfs_flush_docommit(&info, error);
3265 		}
3266 
3267 		/*
3268 		 * Wait for pending I/O to complete before checking whether
3269 		 * any further dirty buffers exist.
3270 		 */
3271 		while (waitfor == MNT_WAIT &&
3272 		       bio_track_active(&vp->v_track_write)) {
3273 			error = bio_track_wait(&vp->v_track_write,
3274 					       info.slpflag, info.slptimeo);
3275 			if (error) {
3276 				/*
3277 				 * We have to be able to break out if this
3278 				 * is an 'intr' mount.
3279 				 */
3280 				if (nfs_sigintr(nmp, NULL, td)) {
3281 					error = -EINTR;
3282 					break;
3283 				}
3284 
3285 				/*
3286 				 * Since we do not process pending signals,
3287 				 * once we get a PCATCH our tsleep() will no
3288 				 * longer sleep, switch to a fixed timeout
3289 				 * instead.
3290 				 */
3291 				if (info.slpflag == PCATCH) {
3292 					info.slpflag = 0;
3293 					info.slptimeo = 2 * hz;
3294 				}
3295 				error = 0;
3296 			}
3297 		}
3298 		++info.loops;
3299 		/*
3300 		 * Loop if we are flushing synchronous as well as committing,
3301 		 * and dirty buffers are still present.  Otherwise we might livelock.
3302 		 */
3303 	} while (waitfor == MNT_WAIT && commit &&
3304 		 error == 0 && !RB_EMPTY(&vp->v_rbdirty_tree));
3305 
3306 	/*
3307 	 * The callbacks have to return a negative error to terminate the
3308 	 * RB scan.
3309 	 */
3310 	if (error < 0)
3311 		error = -error;
3312 
3313 	/*
3314 	 * Deal with any error collection
3315 	 */
3316 	if (np->n_flag & NWRITEERR) {
3317 		error = np->n_error;
3318 		np->n_flag &= ~NWRITEERR;
3319 	}
3320 	lwkt_reltoken(&vp->v_token);
3321 	return (error);
3322 }
3323 
3324 static
3325 int
3326 nfs_flush_bp(struct buf *bp, void *data)
3327 {
3328 	struct nfs_flush_info *info = data;
3329 	int lkflags;
3330 	int error;
3331 	off_t toff;
3332 
3333 	error = 0;
3334 	switch(info->mode) {
3335 	case NFI_FLUSHNEW:
3336 		error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3337 		if (error && info->loops && info->waitfor == MNT_WAIT) {
3338 			error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3339 			if (error) {
3340 				lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
3341 				if (info->slpflag & PCATCH)
3342 					lkflags |= LK_PCATCH;
3343 				error = BUF_TIMELOCK(bp, lkflags, "nfsfsync",
3344 						     info->slptimeo);
3345 			}
3346 		}
3347 
3348 		/*
3349 		 * Ignore locking errors
3350 		 */
3351 		if (error) {
3352 			error = 0;
3353 			break;
3354 		}
3355 
3356 		/*
3357 		 * The buffer may have changed out from under us, even if
3358 		 * we did not block (MPSAFE).  Check again now that it is
3359 		 * locked.
3360 		 */
3361 		if (bp->b_vp == info->vp &&
3362 		    (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) == B_DELWRI) {
3363 			bremfree(bp);
3364 			bawrite(bp);
3365 		} else {
3366 			BUF_UNLOCK(bp);
3367 		}
3368 		break;
3369 	case NFI_COMMIT:
3370 		/*
3371 		 * Only process buffers in need of a commit which we can
3372 		 * immediately lock.  This may prevent a buffer from being
3373 		 * committed, but the normal flush loop will block on the
3374 		 * same buffer so we shouldn't get into an endless loop.
3375 		 */
3376 		if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3377 		    (B_DELWRI | B_NEEDCOMMIT)) {
3378 			break;
3379 		}
3380 		if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
3381 			break;
3382 
3383 		/*
3384 		 * We must recheck after successfully locking the buffer.
3385 		 */
3386 		if (bp->b_vp != info->vp ||
3387 		    (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3388 		    (B_DELWRI | B_NEEDCOMMIT)) {
3389 			BUF_UNLOCK(bp);
3390 			break;
3391 		}
3392 
3393 		/*
3394 		 * NOTE: storing the bp in the bvary[] basically sets
3395 		 * it up for a commit operation.
3396 		 *
3397 		 * We must call vfs_busy_pages() now so the commit operation
3398 		 * is interlocked with user modifications to memory mapped
3399 		 * pages.  The b_dirtyoff/b_dirtyend range is not correct
3400 		 * until after the pages have been busied.
3401 		 *
3402 		 * Note: to avoid loopback deadlocks, we do not
3403 		 * assign b_runningbufspace.
3404 		 */
3405 		bremfree(bp);
3406 		bp->b_cmd = BUF_CMD_WRITE;
3407 		vfs_busy_pages(bp->b_vp, bp);
3408 		info->bvary[info->bvsize] = bp;
3409 		toff = bp->b_bio2.bio_offset + bp->b_dirtyoff;
3410 		if (info->bvsize == 0 || toff < info->beg_off)
3411 			info->beg_off = toff;
3412 		toff += (off_t)(bp->b_dirtyend - bp->b_dirtyoff);
3413 		if (info->bvsize == 0 || toff > info->end_off)
3414 			info->end_off = toff;
3415 		++info->bvsize;
3416 		if (info->bvsize == NFS_COMMITBVECSIZ) {
3417 			error = nfs_flush_docommit(info, 0);
3418 			KKASSERT(info->bvsize == 0);
3419 		}
3420 	}
3421 	return (error);
3422 }
3423 
3424 static
3425 int
3426 nfs_flush_docommit(struct nfs_flush_info *info, int error)
3427 {
3428 	struct vnode *vp;
3429 	struct buf *bp;
3430 	off_t bytes;
3431 	int retv;
3432 	int i;
3433 
3434 	vp = info->vp;
3435 
3436 	if (info->bvsize > 0) {
3437 		/*
3438 		 * Commit data on the server, as required.  Note that
3439 		 * nfs_commit will use the vnode's cred for the commit.
3440 		 * The NFSv3 commit RPC is limited to a 32 bit byte count.
3441 		 */
3442 		bytes = info->end_off - info->beg_off;
3443 		if (bytes > 0x40000000)
3444 			bytes = 0x40000000;
3445 		if (error) {
3446 			retv = -error;
3447 		} else {
3448 			retv = nfs_commitrpc_uio(vp, info->beg_off,
3449 						 (int)bytes, info->td);
3450 			if (retv == NFSERR_STALEWRITEVERF)
3451 				nfs_clearcommit(vp->v_mount);
3452 		}
3453 
3454 		/*
3455 		 * Now, either mark the blocks I/O done or mark the
3456 		 * blocks dirty, depending on whether the commit
3457 		 * succeeded.
3458 		 */
3459 		for (i = 0; i < info->bvsize; ++i) {
3460 			bp = info->bvary[i];
3461 			if (retv || (bp->b_flags & B_NEEDCOMMIT) == 0) {
3462 				/*
3463 				 * Either an error or the original
3464 				 * vfs_busy_pages() cleared B_NEEDCOMMIT
3465 				 * due to finding new dirty VM pages in
3466 				 * the buffer.
3467 				 *
3468 				 * Leave B_DELWRI intact.
3469 				 */
3470 				bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3471 				vfs_unbusy_pages(bp);
3472 				bp->b_cmd = BUF_CMD_DONE;
3473 				bqrelse(bp);
3474 			} else {
3475 				/*
3476 				 * Success, remove B_DELWRI ( bundirty() ).
3477 				 *
3478 				 * b_dirtyoff/b_dirtyend seem to be NFS
3479 				 * specific.  We should probably move that
3480 				 * into bundirty(). XXX
3481 				 *
3482 				 * We are faking an I/O write, we have to
3483 				 * start the transaction in order to
3484 				 * immediately biodone() it.
3485 				 */
3486 				bundirty(bp);
3487 				bp->b_flags &= ~B_ERROR;
3488 				bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3489 				bp->b_dirtyoff = bp->b_dirtyend = 0;
3490 				biodone(&bp->b_bio1);
3491 			}
3492 		}
3493 		info->bvsize = 0;
3494 	}
3495 	return (error);
3496 }
3497 
3498 /*
3499  * NFS advisory byte-level locks.
3500  * Currently unsupported.
3501  *
3502  * nfs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
3503  *		int a_flags)
3504  */
3505 static int
3506 nfs_advlock(struct vop_advlock_args *ap)
3507 {
3508 	struct nfsnode *np = VTONFS(ap->a_vp);
3509 
3510 	/* no token lock currently required */
3511 	/*
3512 	 * The following kludge is to allow diskless support to work
3513 	 * until a real NFS lockd is implemented. Basically, just pretend
3514 	 * that this is a local lock.
3515 	 */
3516 	return (lf_advlock(ap, &(np->n_lockf), np->n_size));
3517 }
3518 
3519 /*
3520  * Print out the contents of an nfsnode.
3521  *
3522  * nfs_print(struct vnode *a_vp)
3523  */
3524 static int
3525 nfs_print(struct vop_print_args *ap)
3526 {
3527 	struct vnode *vp = ap->a_vp;
3528 	struct nfsnode *np = VTONFS(vp);
3529 
3530 	kprintf("tag VT_NFS, fileid %lld fsid 0x%x",
3531 		(long long)np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3532 	if (vp->v_type == VFIFO)
3533 		fifo_printinfo(vp);
3534 	kprintf("\n");
3535 	return (0);
3536 }
3537 
3538 /*
3539  * nfs special file access vnode op.
3540  *
3541  * nfs_laccess(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
3542  */
3543 static int
3544 nfs_laccess(struct vop_access_args *ap)
3545 {
3546 	struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
3547 	struct vattr vattr;
3548 	int error;
3549 
3550 	lwkt_gettoken(&nmp->nm_token);
3551 	error = VOP_GETATTR(ap->a_vp, &vattr);
3552 	if (error == 0) {
3553 		error = vop_helper_access(ap, vattr.va_uid, vattr.va_gid,
3554 					  vattr.va_mode, 0);
3555 	}
3556 	lwkt_reltoken(&nmp->nm_token);
3557 
3558 	return (error);
3559 }
3560 
3561 /*
3562  * Read wrapper for fifos.
3563  *
3564  * nfsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3565  *		struct ucred *a_cred)
3566  */
3567 static int
3568 nfsfifo_read(struct vop_read_args *ap)
3569 {
3570 	struct nfsnode *np = VTONFS(ap->a_vp);
3571 
3572 	/* no token access required */
3573 	/*
3574 	 * Set access flag.
3575 	 */
3576 	np->n_flag |= NACC;
3577 	getnanotime(&np->n_atim);
3578 	return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3579 }
3580 
3581 /*
3582  * Write wrapper for fifos.
3583  *
3584  * nfsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3585  *		 struct ucred *a_cred)
3586  */
3587 static int
3588 nfsfifo_write(struct vop_write_args *ap)
3589 {
3590 	struct nfsnode *np = VTONFS(ap->a_vp);
3591 
3592 	/* no token access required */
3593 	/*
3594 	 * Set update flag.
3595 	 */
3596 	np->n_flag |= NUPD;
3597 	getnanotime(&np->n_mtim);
3598 	return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3599 }
3600 
3601 /*
3602  * Close wrapper for fifos.
3603  *
3604  * Update the times on the nfsnode then do fifo close.
3605  *
3606  * nfsfifo_close(struct vnode *a_vp, int a_fflag)
3607  */
3608 static int
3609 nfsfifo_close(struct vop_close_args *ap)
3610 {
3611 	struct vnode *vp = ap->a_vp;
3612 	struct nfsnode *np = VTONFS(vp);
3613 	struct vattr vattr;
3614 	struct timespec ts;
3615 
3616 	/* no token access required */
3617 
3618 	vn_lock(vp, LK_UPGRADE | LK_RETRY); /* XXX */
3619 	if (np->n_flag & (NACC | NUPD)) {
3620 		getnanotime(&ts);
3621 		if (np->n_flag & NACC)
3622 			np->n_atim = ts;
3623 		if (np->n_flag & NUPD)
3624 			np->n_mtim = ts;
3625 		np->n_flag |= NCHG;
3626 		if (VREFCNT(vp) == 1 &&
3627 		    (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3628 			VATTR_NULL(&vattr);
3629 			if (np->n_flag & NACC)
3630 				vattr.va_atime = np->n_atim;
3631 			if (np->n_flag & NUPD)
3632 				vattr.va_mtime = np->n_mtim;
3633 			(void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE));
3634 		}
3635 	}
3636 	return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3637 }
3638 
3639