xref: /dragonfly/sys/vfs/nfs/nfs_vnops.c (revision 4caa7869)
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. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)nfs_vnops.c	8.16 (Berkeley) 5/27/95
37  * $FreeBSD: src/sys/nfs/nfs_vnops.c,v 1.150.2.5 2001/12/20 19:56:28 dillon Exp $
38  * $DragonFly: src/sys/vfs/nfs/nfs_vnops.c,v 1.15 2003/11/15 21:05:44 dillon Exp $
39  */
40 
41 
42 /*
43  * vnode op calls for Sun NFS version 2 and 3
44  */
45 
46 #include "opt_inet.h"
47 
48 #include <sys/param.h>
49 #include <sys/kernel.h>
50 #include <sys/systm.h>
51 #include <sys/resourcevar.h>
52 #include <sys/proc.h>
53 #include <sys/mount.h>
54 #include <sys/buf.h>
55 #include <sys/malloc.h>
56 #include <sys/mbuf.h>
57 #include <sys/namei.h>
58 #include <sys/socket.h>
59 #include <sys/vnode.h>
60 #include <sys/dirent.h>
61 #include <sys/fcntl.h>
62 #include <sys/lockf.h>
63 #include <sys/stat.h>
64 #include <sys/sysctl.h>
65 #include <sys/conf.h>
66 
67 #include <vm/vm.h>
68 #include <vm/vm_extern.h>
69 #include <vm/vm_zone.h>
70 
71 #include <sys/buf2.h>
72 
73 #include <vfs/fifofs/fifo.h>
74 
75 #include "rpcv2.h"
76 #include "nfsproto.h"
77 #include "nfs.h"
78 #include "nfsmount.h"
79 #include "nfsnode.h"
80 #include "xdr_subs.h"
81 #include "nfsm_subs.h"
82 #include "nqnfs.h"
83 
84 #include <net/if.h>
85 #include <netinet/in.h>
86 #include <netinet/in_var.h>
87 
88 /* Defs */
89 #define	TRUE	1
90 #define	FALSE	0
91 
92 /*
93  * Ifdef for FreeBSD-current merged buffer cache. It is unfortunate that these
94  * calls are not in getblk() and brelse() so that they would not be necessary
95  * here.
96  */
97 #ifndef B_VMIO
98 #define vfs_busy_pages(bp, f)
99 #endif
100 
101 static int	nfsspec_read (struct vop_read_args *);
102 static int	nfsspec_write (struct vop_write_args *);
103 static int	nfsfifo_read (struct vop_read_args *);
104 static int	nfsfifo_write (struct vop_write_args *);
105 static int	nfsspec_close (struct vop_close_args *);
106 static int	nfsfifo_close (struct vop_close_args *);
107 #define nfs_poll vop_nopoll
108 static int	nfs_flush (struct vnode *,int,struct thread *,int);
109 static int	nfs_setattrrpc (struct vnode *,struct vattr *,struct ucred *,struct thread *);
110 static	int	nfs_lookup (struct vop_lookup_args *);
111 static	int	nfs_create (struct vop_create_args *);
112 static	int	nfs_mknod (struct vop_mknod_args *);
113 static	int	nfs_open (struct vop_open_args *);
114 static	int	nfs_close (struct vop_close_args *);
115 static	int	nfs_access (struct vop_access_args *);
116 static	int	nfs_getattr (struct vop_getattr_args *);
117 static	int	nfs_setattr (struct vop_setattr_args *);
118 static	int	nfs_read (struct vop_read_args *);
119 static	int	nfs_mmap (struct vop_mmap_args *);
120 static	int	nfs_fsync (struct vop_fsync_args *);
121 static	int	nfs_remove (struct vop_remove_args *);
122 static	int	nfs_link (struct vop_link_args *);
123 static	int	nfs_rename (struct vop_rename_args *);
124 static	int	nfs_mkdir (struct vop_mkdir_args *);
125 static	int	nfs_rmdir (struct vop_rmdir_args *);
126 static	int	nfs_symlink (struct vop_symlink_args *);
127 static	int	nfs_readdir (struct vop_readdir_args *);
128 static	int	nfs_bmap (struct vop_bmap_args *);
129 static	int	nfs_strategy (struct vop_strategy_args *);
130 static	int	nfs_lookitup (struct vnode *, const char *, int,
131 			struct ucred *, struct thread *, struct nfsnode **);
132 static	int	nfs_sillyrename (struct vnode *,struct vnode *,struct componentname *);
133 static int	nfsspec_access (struct vop_access_args *);
134 static int	nfs_readlink (struct vop_readlink_args *);
135 static int	nfs_print (struct vop_print_args *);
136 static int	nfs_advlock (struct vop_advlock_args *);
137 static int	nfs_bwrite (struct vop_bwrite_args *);
138 /*
139  * Global vfs data structures for nfs
140  */
141 vop_t **nfsv2_vnodeop_p;
142 static struct vnodeopv_entry_desc nfsv2_vnodeop_entries[] = {
143 	{ &vop_default_desc,		(vop_t *) vop_defaultop },
144 	{ &vop_access_desc,		(vop_t *) nfs_access },
145 	{ &vop_advlock_desc,		(vop_t *) nfs_advlock },
146 	{ &vop_bmap_desc,		(vop_t *) nfs_bmap },
147 	{ &vop_bwrite_desc,		(vop_t *) nfs_bwrite },
148 	{ &vop_close_desc,		(vop_t *) nfs_close },
149 	{ &vop_create_desc,		(vop_t *) nfs_create },
150 	{ &vop_fsync_desc,		(vop_t *) nfs_fsync },
151 	{ &vop_getattr_desc,		(vop_t *) nfs_getattr },
152 	{ &vop_getpages_desc,		(vop_t *) nfs_getpages },
153 	{ &vop_putpages_desc,		(vop_t *) nfs_putpages },
154 	{ &vop_inactive_desc,		(vop_t *) nfs_inactive },
155 	{ &vop_islocked_desc,		(vop_t *) vop_stdislocked },
156 	{ &vop_lease_desc,		(vop_t *) vop_null },
157 	{ &vop_link_desc,		(vop_t *) nfs_link },
158 	{ &vop_lock_desc,		(vop_t *) vop_sharedlock },
159 	{ &vop_lookup_desc,		(vop_t *) nfs_lookup },
160 	{ &vop_mkdir_desc,		(vop_t *) nfs_mkdir },
161 	{ &vop_mknod_desc,		(vop_t *) nfs_mknod },
162 	{ &vop_mmap_desc,		(vop_t *) nfs_mmap },
163 	{ &vop_open_desc,		(vop_t *) nfs_open },
164 	{ &vop_poll_desc,		(vop_t *) nfs_poll },
165 	{ &vop_print_desc,		(vop_t *) nfs_print },
166 	{ &vop_read_desc,		(vop_t *) nfs_read },
167 	{ &vop_readdir_desc,		(vop_t *) nfs_readdir },
168 	{ &vop_readlink_desc,		(vop_t *) nfs_readlink },
169 	{ &vop_reclaim_desc,		(vop_t *) nfs_reclaim },
170 	{ &vop_remove_desc,		(vop_t *) nfs_remove },
171 	{ &vop_rename_desc,		(vop_t *) nfs_rename },
172 	{ &vop_rmdir_desc,		(vop_t *) nfs_rmdir },
173 	{ &vop_setattr_desc,		(vop_t *) nfs_setattr },
174 	{ &vop_strategy_desc,		(vop_t *) nfs_strategy },
175 	{ &vop_symlink_desc,		(vop_t *) nfs_symlink },
176 	{ &vop_unlock_desc,		(vop_t *) vop_stdunlock },
177 	{ &vop_write_desc,		(vop_t *) nfs_write },
178 	{ NULL, NULL }
179 };
180 static struct vnodeopv_desc nfsv2_vnodeop_opv_desc =
181 	{ &nfsv2_vnodeop_p, nfsv2_vnodeop_entries };
182 VNODEOP_SET(nfsv2_vnodeop_opv_desc);
183 
184 /*
185  * Special device vnode ops
186  */
187 vop_t **spec_nfsv2nodeop_p;
188 static struct vnodeopv_entry_desc nfsv2_specop_entries[] = {
189 	{ &vop_default_desc,		(vop_t *) spec_vnoperate },
190 	{ &vop_access_desc,		(vop_t *) nfsspec_access },
191 	{ &vop_close_desc,		(vop_t *) nfsspec_close },
192 	{ &vop_fsync_desc,		(vop_t *) nfs_fsync },
193 	{ &vop_getattr_desc,		(vop_t *) nfs_getattr },
194 	{ &vop_inactive_desc,		(vop_t *) nfs_inactive },
195 	{ &vop_islocked_desc,		(vop_t *) vop_stdislocked },
196 	{ &vop_lock_desc,		(vop_t *) vop_sharedlock },
197 	{ &vop_print_desc,		(vop_t *) nfs_print },
198 	{ &vop_read_desc,		(vop_t *) nfsspec_read },
199 	{ &vop_reclaim_desc,		(vop_t *) nfs_reclaim },
200 	{ &vop_setattr_desc,		(vop_t *) nfs_setattr },
201 	{ &vop_unlock_desc,		(vop_t *) vop_stdunlock },
202 	{ &vop_write_desc,		(vop_t *) nfsspec_write },
203 	{ NULL, NULL }
204 };
205 static struct vnodeopv_desc spec_nfsv2nodeop_opv_desc =
206 	{ &spec_nfsv2nodeop_p, nfsv2_specop_entries };
207 VNODEOP_SET(spec_nfsv2nodeop_opv_desc);
208 
209 vop_t **fifo_nfsv2nodeop_p;
210 static struct vnodeopv_entry_desc nfsv2_fifoop_entries[] = {
211 	{ &vop_default_desc,		(vop_t *) fifo_vnoperate },
212 	{ &vop_access_desc,		(vop_t *) nfsspec_access },
213 	{ &vop_close_desc,		(vop_t *) nfsfifo_close },
214 	{ &vop_fsync_desc,		(vop_t *) nfs_fsync },
215 	{ &vop_getattr_desc,		(vop_t *) nfs_getattr },
216 	{ &vop_inactive_desc,		(vop_t *) nfs_inactive },
217 	{ &vop_islocked_desc,		(vop_t *) vop_stdislocked },
218 	{ &vop_lock_desc,		(vop_t *) vop_sharedlock },
219 	{ &vop_print_desc,		(vop_t *) nfs_print },
220 	{ &vop_read_desc,		(vop_t *) nfsfifo_read },
221 	{ &vop_reclaim_desc,		(vop_t *) nfs_reclaim },
222 	{ &vop_setattr_desc,		(vop_t *) nfs_setattr },
223 	{ &vop_unlock_desc,		(vop_t *) vop_stdunlock },
224 	{ &vop_write_desc,		(vop_t *) nfsfifo_write },
225 	{ NULL, NULL }
226 };
227 static struct vnodeopv_desc fifo_nfsv2nodeop_opv_desc =
228 	{ &fifo_nfsv2nodeop_p, nfsv2_fifoop_entries };
229 VNODEOP_SET(fifo_nfsv2nodeop_opv_desc);
230 
231 static int	nfs_mknodrpc (struct vnode *dvp, struct vnode **vpp,
232 				  struct componentname *cnp,
233 				  struct vattr *vap);
234 static int	nfs_removerpc (struct vnode *dvp, const char *name,
235 				   int namelen,
236 				   struct ucred *cred, struct thread *td);
237 static int	nfs_renamerpc (struct vnode *fdvp, const char *fnameptr,
238 				   int fnamelen, struct vnode *tdvp,
239 				   const char *tnameptr, int tnamelen,
240 				   struct ucred *cred, struct thread *td);
241 static int	nfs_renameit (struct vnode *sdvp,
242 				  struct componentname *scnp,
243 				  struct sillyrename *sp);
244 
245 /*
246  * Global variables
247  */
248 extern u_int32_t nfs_true, nfs_false;
249 extern u_int32_t nfs_xdrneg1;
250 extern struct nfsstats nfsstats;
251 extern nfstype nfsv3_type[9];
252 struct thread *nfs_iodwant[NFS_MAXASYNCDAEMON];
253 struct nfsmount *nfs_iodmount[NFS_MAXASYNCDAEMON];
254 int nfs_numasync = 0;
255 #define	DIRHDSIZ	(sizeof (struct dirent) - (MAXNAMLEN + 1))
256 
257 SYSCTL_DECL(_vfs_nfs);
258 
259 static int	nfsaccess_cache_timeout = NFS_MAXATTRTIMO;
260 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
261 	   &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
262 
263 static int	nfsv3_commit_on_close = 0;
264 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfsv3_commit_on_close, CTLFLAG_RW,
265 	   &nfsv3_commit_on_close, 0, "write+commit on close, else only write");
266 #if 0
267 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_hits, CTLFLAG_RD,
268 	   &nfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
269 
270 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_misses, CTLFLAG_RD,
271 	   &nfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
272 #endif
273 
274 #define	NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY		\
275 			 | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE	\
276 			 | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
277 static int
278 nfs3_access_otw(struct vnode *vp, int wmode,
279 	struct thread *td, struct ucred *cred)
280 {
281 	const int v3 = 1;
282 	u_int32_t *tl;
283 	int error = 0, attrflag;
284 
285 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
286 	caddr_t bpos, dpos, cp2;
287 	int32_t t1, t2;
288 	caddr_t cp;
289 	u_int32_t rmode;
290 	struct nfsnode *np = VTONFS(vp);
291 
292 	nfsstats.rpccnt[NFSPROC_ACCESS]++;
293 	nfsm_reqhead(vp, NFSPROC_ACCESS, NFSX_FH(v3) + NFSX_UNSIGNED);
294 	nfsm_fhtom(vp, v3);
295 	nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
296 	*tl = txdr_unsigned(wmode);
297 	nfsm_request(vp, NFSPROC_ACCESS, td, cred);
298 	nfsm_postop_attr(vp, attrflag);
299 	if (!error) {
300 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
301 		rmode = fxdr_unsigned(u_int32_t, *tl);
302 		np->n_mode = rmode;
303 		np->n_modeuid = cred->cr_uid;
304 		np->n_modestamp = time_second;
305 	}
306 	m_freem(mrep);
307 nfsmout:
308 	return error;
309 }
310 
311 /*
312  * nfs access vnode op.
313  * For nfs version 2, just return ok. File accesses may fail later.
314  * For nfs version 3, use the access rpc to check accessibility. If file modes
315  * are changed on the server, accesses might still fail later.
316  */
317 static int
318 nfs_access(ap)
319 	struct vop_access_args /* {
320 		struct vnode *a_vp;
321 		int  a_mode;
322 		struct ucred *a_cred;
323 		struct thread *a_td;
324 	} */ *ap;
325 {
326 	struct vnode *vp = ap->a_vp;
327 	int error = 0;
328 	u_int32_t mode, wmode;
329 	int v3 = NFS_ISV3(vp);
330 	struct nfsnode *np = VTONFS(vp);
331 
332 	/*
333 	 * Disallow write attempts on filesystems mounted read-only;
334 	 * unless the file is a socket, fifo, or a block or character
335 	 * device resident on the filesystem.
336 	 */
337 	if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
338 		switch (vp->v_type) {
339 		case VREG:
340 		case VDIR:
341 		case VLNK:
342 			return (EROFS);
343 		default:
344 			break;
345 		}
346 	}
347 	/*
348 	 * For nfs v3, check to see if we have done this recently, and if
349 	 * so return our cached result instead of making an ACCESS call.
350 	 * If not, do an access rpc, otherwise you are stuck emulating
351 	 * ufs_access() locally using the vattr. This may not be correct,
352 	 * since the server may apply other access criteria such as
353 	 * client uid-->server uid mapping that we do not know about.
354 	 */
355 	if (v3) {
356 		if (ap->a_mode & VREAD)
357 			mode = NFSV3ACCESS_READ;
358 		else
359 			mode = 0;
360 		if (vp->v_type != VDIR) {
361 			if (ap->a_mode & VWRITE)
362 				mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
363 			if (ap->a_mode & VEXEC)
364 				mode |= NFSV3ACCESS_EXECUTE;
365 		} else {
366 			if (ap->a_mode & VWRITE)
367 				mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
368 					 NFSV3ACCESS_DELETE);
369 			if (ap->a_mode & VEXEC)
370 				mode |= NFSV3ACCESS_LOOKUP;
371 		}
372 		/* XXX safety belt, only make blanket request if caching */
373 		if (nfsaccess_cache_timeout > 0) {
374 			wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY |
375 				NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE |
376 				NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
377 		} else {
378 			wmode = mode;
379 		}
380 
381 		/*
382 		 * Does our cached result allow us to give a definite yes to
383 		 * this request?
384 		 */
385 		if ((time_second < (np->n_modestamp + nfsaccess_cache_timeout)) &&
386 		    (ap->a_cred->cr_uid == np->n_modeuid) &&
387 		    ((np->n_mode & mode) == mode)) {
388 			nfsstats.accesscache_hits++;
389 		} else {
390 			/*
391 			 * Either a no, or a don't know.  Go to the wire.
392 			 */
393 			nfsstats.accesscache_misses++;
394 		        error = nfs3_access_otw(vp, wmode, ap->a_td,ap->a_cred);
395 			if (!error) {
396 				if ((np->n_mode & mode) != mode) {
397 					error = EACCES;
398 				}
399 			}
400 		}
401 	} else {
402 		if ((error = nfsspec_access(ap)) != 0)
403 			return (error);
404 
405 		/*
406 		 * Attempt to prevent a mapped root from accessing a file
407 		 * which it shouldn't.  We try to read a byte from the file
408 		 * if the user is root and the file is not zero length.
409 		 * After calling nfsspec_access, we should have the correct
410 		 * file size cached.
411 		 */
412 		if (ap->a_cred->cr_uid == 0 && (ap->a_mode & VREAD)
413 		    && VTONFS(vp)->n_size > 0) {
414 			struct iovec aiov;
415 			struct uio auio;
416 			char buf[1];
417 
418 			aiov.iov_base = buf;
419 			aiov.iov_len = 1;
420 			auio.uio_iov = &aiov;
421 			auio.uio_iovcnt = 1;
422 			auio.uio_offset = 0;
423 			auio.uio_resid = 1;
424 			auio.uio_segflg = UIO_SYSSPACE;
425 			auio.uio_rw = UIO_READ;
426 			auio.uio_td = ap->a_td;
427 
428 			if (vp->v_type == VREG) {
429 				error = nfs_readrpc(vp, &auio);
430 			} else if (vp->v_type == VDIR) {
431 				char* bp;
432 				bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
433 				aiov.iov_base = bp;
434 				aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
435 				error = nfs_readdirrpc(vp, &auio);
436 				free(bp, M_TEMP);
437 			} else if (vp->v_type == VLNK) {
438 				error = nfs_readlinkrpc(vp, &auio);
439 			} else {
440 				error = EACCES;
441 			}
442 		}
443 	}
444 	/*
445 	 * [re]record creds for reading and/or writing if access
446 	 * was granted.
447 	 */
448 	if (error == 0) {
449 		if ((ap->a_mode & VREAD) && ap->a_cred != np->n_rucred) {
450 			crhold(ap->a_cred);
451 			if (np->n_rucred)
452 				crfree(np->n_rucred);
453 			np->n_rucred = ap->a_cred;
454 		}
455 		if ((ap->a_mode & VWRITE) && ap->a_cred != np->n_wucred) {
456 			crhold(ap->a_cred);
457 			if (np->n_wucred)
458 				crfree(np->n_wucred);
459 			np->n_wucred = ap->a_cred;
460 		}
461 	}
462 	return(error);
463 }
464 
465 /*
466  * nfs open vnode op
467  * Check to see if the type is ok
468  * and that deletion is not in progress.
469  * For paged in text files, you will need to flush the page cache
470  * if consistency is lost.
471  */
472 /* ARGSUSED */
473 static int
474 nfs_open(ap)
475 	struct vop_open_args /* {
476 		struct vnode *a_vp;
477 		int  a_mode;
478 		struct ucred *a_cred;
479 		struct thread *a_td;
480 	} */ *ap;
481 {
482 	struct vnode *vp = ap->a_vp;
483 	struct nfsnode *np = VTONFS(vp);
484 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
485 	struct vattr vattr;
486 	int error;
487 
488 	if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) {
489 #ifdef DIAGNOSTIC
490 		printf("open eacces vtyp=%d\n",vp->v_type);
491 #endif
492 		return (EACCES);
493 	}
494 	/*
495 	 * Get a valid lease. If cached data is stale, flush it.
496 	 */
497 	if (nmp->nm_flag & NFSMNT_NQNFS) {
498 		if (NQNFS_CKINVALID(vp, np, ND_READ)) {
499 		    do {
500 			error = nqnfs_getlease(vp, ND_READ, ap->a_td);
501 		    } while (error == NQNFS_EXPIRED);
502 		    if (error)
503 			return (error);
504 		    if (np->n_lrev != np->n_brev ||
505 			(np->n_flag & NQNFSNONCACHE)) {
506 			if ((error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1))
507 			    == EINTR) {
508 				return (error);
509 			}
510 			np->n_brev = np->n_lrev;
511 		    }
512 		}
513 	} else {
514 		if (np->n_flag & NMODIFIED) {
515 			if ((error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1))
516 			    == EINTR) {
517 				return (error);
518 			}
519 			np->n_attrstamp = 0;
520 			if (vp->v_type == VDIR)
521 				np->n_direofoffset = 0;
522 			error = VOP_GETATTR(vp, &vattr, ap->a_td);
523 			if (error)
524 				return (error);
525 			np->n_mtime = vattr.va_mtime.tv_sec;
526 		} else {
527 			error = VOP_GETATTR(vp, &vattr, ap->a_td);
528 			if (error)
529 				return (error);
530 			if (np->n_mtime != vattr.va_mtime.tv_sec) {
531 				if (vp->v_type == VDIR)
532 					np->n_direofoffset = 0;
533 				if ((error = nfs_vinvalbuf(vp, V_SAVE,
534 				    ap->a_td, 1)) == EINTR) {
535 					return (error);
536 				}
537 				np->n_mtime = vattr.va_mtime.tv_sec;
538 			}
539 		}
540 	}
541 	if ((nmp->nm_flag & NFSMNT_NQNFS) == 0)
542 		np->n_attrstamp = 0; /* For Open/Close consistency */
543 	return (0);
544 }
545 
546 /*
547  * nfs close vnode op
548  * What an NFS client should do upon close after writing is a debatable issue.
549  * Most NFS clients push delayed writes to the server upon close, basically for
550  * two reasons:
551  * 1 - So that any write errors may be reported back to the client process
552  *     doing the close system call. By far the two most likely errors are
553  *     NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
554  * 2 - To put a worst case upper bound on cache inconsistency between
555  *     multiple clients for the file.
556  * There is also a consistency problem for Version 2 of the protocol w.r.t.
557  * not being able to tell if other clients are writing a file concurrently,
558  * since there is no way of knowing if the changed modify time in the reply
559  * is only due to the write for this client.
560  * (NFS Version 3 provides weak cache consistency data in the reply that
561  *  should be sufficient to detect and handle this case.)
562  *
563  * The current code does the following:
564  * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
565  * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
566  *                     or commit them (this satisfies 1 and 2 except for the
567  *                     case where the server crashes after this close but
568  *                     before the commit RPC, which is felt to be "good
569  *                     enough". Changing the last argument to nfs_flush() to
570  *                     a 1 would force a commit operation, if it is felt a
571  *                     commit is necessary now.
572  * for NQNFS         - do nothing now, since 2 is dealt with via leases and
573  *                     1 should be dealt with via an fsync() system call for
574  *                     cases where write errors are important.
575  */
576 /* ARGSUSED */
577 static int
578 nfs_close(ap)
579 	struct vop_close_args /* {
580 		struct vnodeop_desc *a_desc;
581 		struct vnode *a_vp;
582 		int  a_fflag;
583 		struct ucred *a_cred;
584 		struct thread *a_td;
585 	} */ *ap;
586 {
587 	struct vnode *vp = ap->a_vp;
588 	struct nfsnode *np = VTONFS(vp);
589 	int error = 0;
590 
591 	if (vp->v_type == VREG) {
592 	    if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) == 0 &&
593 		(np->n_flag & NMODIFIED)) {
594 		if (NFS_ISV3(vp)) {
595 		    /*
596 		     * Under NFSv3 we have dirty buffers to dispose of.  We
597 		     * must flush them to the NFS server.  We have the option
598 		     * of waiting all the way through the commit rpc or just
599 		     * waiting for the initial write.  The default is to only
600 		     * wait through the initial write so the data is in the
601 		     * server's cache, which is roughly similar to the state
602 		     * a standard disk subsystem leaves the file in on close().
603 		     *
604 		     * We cannot clear the NMODIFIED bit in np->n_flag due to
605 		     * potential races with other processes, and certainly
606 		     * cannot clear it if we don't commit.
607 		     */
608 		    int cm = nfsv3_commit_on_close ? 1 : 0;
609 		    error = nfs_flush(vp, MNT_WAIT, ap->a_td, cm);
610 		    /* np->n_flag &= ~NMODIFIED; */
611 		} else {
612 		    error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
613 		}
614 		np->n_attrstamp = 0;
615 	    }
616 	    if (np->n_flag & NWRITEERR) {
617 		np->n_flag &= ~NWRITEERR;
618 		error = np->n_error;
619 	    }
620 	}
621 	return (error);
622 }
623 
624 /*
625  * nfs getattr call from vfs.
626  */
627 static int
628 nfs_getattr(ap)
629 	struct vop_getattr_args /* {
630 		struct vnode *a_vp;
631 		struct vattr *a_vap;
632 		struct ucred *a_cred;
633 		struct thread *a_td;
634 	} */ *ap;
635 {
636 	struct vnode *vp = ap->a_vp;
637 	struct nfsnode *np = VTONFS(vp);
638 	caddr_t cp;
639 	u_int32_t *tl;
640 	int32_t t1, t2;
641 	caddr_t bpos, dpos;
642 	int error = 0;
643 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
644 	int v3 = NFS_ISV3(vp);
645 
646 	/*
647 	 * Update local times for special files.
648 	 */
649 	if (np->n_flag & (NACC | NUPD))
650 		np->n_flag |= NCHG;
651 	/*
652 	 * First look in the cache.
653 	 */
654 	if (nfs_getattrcache(vp, ap->a_vap) == 0)
655 		return (0);
656 
657 	if (v3 && nfsaccess_cache_timeout > 0) {
658 		nfsstats.accesscache_misses++;
659 		nfs3_access_otw(vp, NFSV3ACCESS_ALL, ap->a_td, nfs_vpcred(vp, ND_CHECK));
660 		if (nfs_getattrcache(vp, ap->a_vap) == 0)
661 			return (0);
662 	}
663 
664 	nfsstats.rpccnt[NFSPROC_GETATTR]++;
665 	nfsm_reqhead(vp, NFSPROC_GETATTR, NFSX_FH(v3));
666 	nfsm_fhtom(vp, v3);
667 	nfsm_request(vp, NFSPROC_GETATTR, ap->a_td, nfs_vpcred(vp, ND_CHECK));
668 	if (!error) {
669 		nfsm_loadattr(vp, ap->a_vap);
670 	}
671 	m_freem(mrep);
672 nfsmout:
673 	return (error);
674 }
675 
676 /*
677  * nfs setattr call.
678  */
679 static int
680 nfs_setattr(ap)
681 	struct vop_setattr_args /* {
682 		struct vnodeop_desc *a_desc;
683 		struct vnode *a_vp;
684 		struct vattr *a_vap;
685 		struct ucred *a_cred;
686 		struct thread *a_td;
687 	} */ *ap;
688 {
689 	struct vnode *vp = ap->a_vp;
690 	struct nfsnode *np = VTONFS(vp);
691 	struct vattr *vap = ap->a_vap;
692 	int error = 0;
693 	u_quad_t tsize;
694 
695 #ifndef nolint
696 	tsize = (u_quad_t)0;
697 #endif
698 
699 	/*
700 	 * Setting of flags is not supported.
701 	 */
702 	if (vap->va_flags != VNOVAL)
703 		return (EOPNOTSUPP);
704 
705 	/*
706 	 * Disallow write attempts if the filesystem is mounted read-only.
707 	 */
708   	if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
709 	    vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
710 	    vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
711 	    (vp->v_mount->mnt_flag & MNT_RDONLY))
712 		return (EROFS);
713 	if (vap->va_size != VNOVAL) {
714  		switch (vp->v_type) {
715  		case VDIR:
716  			return (EISDIR);
717  		case VCHR:
718  		case VBLK:
719  		case VSOCK:
720  		case VFIFO:
721 			if (vap->va_mtime.tv_sec == VNOVAL &&
722 			    vap->va_atime.tv_sec == VNOVAL &&
723 			    vap->va_mode == (mode_t)VNOVAL &&
724 			    vap->va_uid == (uid_t)VNOVAL &&
725 			    vap->va_gid == (gid_t)VNOVAL)
726 				return (0);
727  			vap->va_size = VNOVAL;
728  			break;
729  		default:
730 			/*
731 			 * Disallow write attempts if the filesystem is
732 			 * mounted read-only.
733 			 */
734 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
735 				return (EROFS);
736 
737 			/*
738 			 * We run vnode_pager_setsize() early (why?),
739 			 * we must set np->n_size now to avoid vinvalbuf
740 			 * V_SAVE races that might setsize a lower
741 			 * value.
742 			 */
743 
744 			tsize = np->n_size;
745 			error = nfs_meta_setsize(vp, ap->a_td, vap->va_size);
746 
747  			if (np->n_flag & NMODIFIED) {
748  			    if (vap->va_size == 0)
749  				error = nfs_vinvalbuf(vp, 0, ap->a_td, 1);
750  			    else
751  				error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
752  			    if (error) {
753 				np->n_size = tsize;
754 				vnode_pager_setsize(vp, np->n_size);
755  				return (error);
756 			    }
757  			}
758 			/* np->n_size has already been set to vap->va_size
759 			 * in nfs_meta_setsize(). We must set it again since
760 			 * nfs_loadattrcache() could be called through
761 			 * nfs_meta_setsize() and could modify np->n_size.
762 			 */
763 			np->n_vattr.va_size = np->n_size = vap->va_size;
764 		};
765   	} else if ((vap->va_mtime.tv_sec != VNOVAL ||
766 		vap->va_atime.tv_sec != VNOVAL) && (np->n_flag & NMODIFIED) &&
767 		vp->v_type == VREG &&
768   		(error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1)) == EINTR)
769 		return (error);
770 	error = nfs_setattrrpc(vp, vap, ap->a_cred, ap->a_td);
771 	if (error && vap->va_size != VNOVAL) {
772 		np->n_size = np->n_vattr.va_size = tsize;
773 		vnode_pager_setsize(vp, np->n_size);
774 	}
775 	return (error);
776 }
777 
778 /*
779  * Do an nfs setattr rpc.
780  */
781 static int
782 nfs_setattrrpc(struct vnode *vp, struct vattr *vap,
783 	struct ucred *cred, struct thread *td)
784 {
785 	struct nfsv2_sattr *sp;
786 	caddr_t cp;
787 	int32_t t1, t2;
788 	caddr_t bpos, dpos, cp2;
789 	u_int32_t *tl;
790 	int error = 0, wccflag = NFSV3_WCCRATTR;
791 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
792 	int v3 = NFS_ISV3(vp);
793 
794 	nfsstats.rpccnt[NFSPROC_SETATTR]++;
795 	nfsm_reqhead(vp, NFSPROC_SETATTR, NFSX_FH(v3) + NFSX_SATTR(v3));
796 	nfsm_fhtom(vp, v3);
797 	if (v3) {
798 		nfsm_v3attrbuild(vap, TRUE);
799 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
800 		*tl = nfs_false;
801 	} else {
802 		nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
803 		if (vap->va_mode == (mode_t)VNOVAL)
804 			sp->sa_mode = nfs_xdrneg1;
805 		else
806 			sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
807 		if (vap->va_uid == (uid_t)VNOVAL)
808 			sp->sa_uid = nfs_xdrneg1;
809 		else
810 			sp->sa_uid = txdr_unsigned(vap->va_uid);
811 		if (vap->va_gid == (gid_t)VNOVAL)
812 			sp->sa_gid = nfs_xdrneg1;
813 		else
814 			sp->sa_gid = txdr_unsigned(vap->va_gid);
815 		sp->sa_size = txdr_unsigned(vap->va_size);
816 		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
817 		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
818 	}
819 	nfsm_request(vp, NFSPROC_SETATTR, td, cred);
820 	if (v3) {
821 		nfsm_wcc_data(vp, wccflag);
822 	} else
823 		nfsm_loadattr(vp, (struct vattr *)0);
824 	m_freem(mrep);
825 nfsmout:
826 	return (error);
827 }
828 
829 /*
830  * nfs lookup call, one step at a time...
831  * First look in cache
832  * If not found, unlock the directory nfsnode and do the rpc
833  */
834 static int
835 nfs_lookup(ap)
836 	struct vop_lookup_args /* {
837 		struct vnodeop_desc *a_desc;
838 		struct vnode *a_dvp;
839 		struct vnode **a_vpp;
840 		struct componentname *a_cnp;
841 	} */ *ap;
842 {
843 	struct componentname *cnp = ap->a_cnp;
844 	struct vnode *dvp = ap->a_dvp;
845 	struct vnode **vpp = ap->a_vpp;
846 	int flags = cnp->cn_flags;
847 	struct vnode *newvp;
848 	u_int32_t *tl;
849 	caddr_t cp;
850 	int32_t t1, t2;
851 	struct nfsmount *nmp;
852 	caddr_t bpos, dpos, cp2;
853 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
854 	long len;
855 	nfsfh_t *fhp;
856 	struct nfsnode *np;
857 	int lockparent, wantparent, error = 0, attrflag, fhsize;
858 	int v3 = NFS_ISV3(dvp);
859 	struct thread *td = cnp->cn_td;
860 
861 	*vpp = NULLVP;
862 	if ((flags & CNP_ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
863 	    (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME))
864 		return (EROFS);
865 	if (dvp->v_type != VDIR)
866 		return (ENOTDIR);
867 	lockparent = flags & CNP_LOCKPARENT;
868 	wantparent = flags & (CNP_LOCKPARENT|CNP_WANTPARENT);
869 	nmp = VFSTONFS(dvp->v_mount);
870 	np = VTONFS(dvp);
871 	if ((error = cache_lookup(dvp, NCPNULL, vpp, NCPPNULL, cnp)) && error != ENOENT) {
872 		struct vattr vattr;
873 		int vpid;
874 
875 		if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, td)) != 0) {
876 			*vpp = NULLVP;
877 			return (error);
878 		}
879 
880 		newvp = *vpp;
881 		vpid = newvp->v_id;
882 		/*
883 		 * See the comment starting `Step through' in ufs/ufs_lookup.c
884 		 * for an explanation of the locking protocol
885 		 */
886 		if (dvp == newvp) {
887 			VREF(newvp);
888 			error = 0;
889 		} else if (flags & CNP_ISDOTDOT) {
890 			VOP_UNLOCK(dvp, 0, td);
891 			error = vget(newvp, LK_EXCLUSIVE, td);
892 			if (!error && lockparent && (flags & CNP_ISLASTCN))
893 				error = vn_lock(dvp, LK_EXCLUSIVE, td);
894 		} else {
895 			error = vget(newvp, LK_EXCLUSIVE, td);
896 			if (!lockparent || error || !(flags & CNP_ISLASTCN))
897 				VOP_UNLOCK(dvp, 0, td);
898 		}
899 		if (!error) {
900 			if (vpid == newvp->v_id) {
901 			   if (!VOP_GETATTR(newvp, &vattr, td)
902 			    && vattr.va_ctime.tv_sec == VTONFS(newvp)->n_ctime) {
903 				nfsstats.lookupcache_hits++;
904 				if (cnp->cn_nameiop != NAMEI_LOOKUP &&
905 				    (flags & CNP_ISLASTCN))
906 					cnp->cn_flags |= CNP_SAVENAME;
907 				return (0);
908 			   }
909 			   cache_purge(newvp);
910 			}
911 			vput(newvp);
912 			if (lockparent && dvp != newvp && (flags & CNP_ISLASTCN))
913 				VOP_UNLOCK(dvp, 0, td);
914 		}
915 		error = vn_lock(dvp, LK_EXCLUSIVE, td);
916 		*vpp = NULLVP;
917 		if (error)
918 			return (error);
919 	}
920 	error = 0;
921 	newvp = NULLVP;
922 	nfsstats.lookupcache_misses++;
923 	nfsstats.rpccnt[NFSPROC_LOOKUP]++;
924 	len = cnp->cn_namelen;
925 	nfsm_reqhead(dvp, NFSPROC_LOOKUP,
926 		NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
927 	nfsm_fhtom(dvp, v3);
928 	nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
929 	nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_td, cnp->cn_cred);
930 	if (error) {
931 		nfsm_postop_attr(dvp, attrflag);
932 		m_freem(mrep);
933 		goto nfsmout;
934 	}
935 	nfsm_getfh(fhp, fhsize, v3);
936 
937 	/*
938 	 * Handle RENAME case...
939 	 */
940 	if (cnp->cn_nameiop == NAMEI_RENAME && wantparent && (flags & CNP_ISLASTCN)) {
941 		if (NFS_CMPFH(np, fhp, fhsize)) {
942 			m_freem(mrep);
943 			return (EISDIR);
944 		}
945 		error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
946 		if (error) {
947 			m_freem(mrep);
948 			return (error);
949 		}
950 		newvp = NFSTOV(np);
951 		if (v3) {
952 			nfsm_postop_attr(newvp, attrflag);
953 			nfsm_postop_attr(dvp, attrflag);
954 		} else
955 			nfsm_loadattr(newvp, (struct vattr *)0);
956 		*vpp = newvp;
957 		m_freem(mrep);
958 		cnp->cn_flags |= CNP_SAVENAME;
959 		if (!lockparent)
960 			VOP_UNLOCK(dvp, 0, td);
961 		return (0);
962 	}
963 
964 	if (flags & CNP_ISDOTDOT) {
965 		VOP_UNLOCK(dvp, 0, td);
966 		error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
967 		if (error) {
968 			vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
969 			return (error);
970 		}
971 		newvp = NFSTOV(np);
972 		if (lockparent && (flags & CNP_ISLASTCN) &&
973 		    (error = vn_lock(dvp, LK_EXCLUSIVE, td))) {
974 		    	vput(newvp);
975 			return (error);
976 		}
977 	} else if (NFS_CMPFH(np, fhp, fhsize)) {
978 		VREF(dvp);
979 		newvp = dvp;
980 	} else {
981 		error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
982 		if (error) {
983 			m_freem(mrep);
984 			return (error);
985 		}
986 		if (!lockparent || !(flags & CNP_ISLASTCN))
987 			VOP_UNLOCK(dvp, 0, td);
988 		newvp = NFSTOV(np);
989 	}
990 	if (v3) {
991 		nfsm_postop_attr(newvp, attrflag);
992 		nfsm_postop_attr(dvp, attrflag);
993 	} else
994 		nfsm_loadattr(newvp, (struct vattr *)0);
995 	if (cnp->cn_nameiop != NAMEI_LOOKUP && (flags & CNP_ISLASTCN))
996 		cnp->cn_flags |= CNP_SAVENAME;
997 	if ((cnp->cn_flags & CNP_MAKEENTRY) &&
998 	    (cnp->cn_nameiop != NAMEI_DELETE || !(flags & CNP_ISLASTCN))) {
999 		np->n_ctime = np->n_vattr.va_ctime.tv_sec;
1000 		cache_enter(dvp, NCPNULL, newvp, cnp);
1001 	}
1002 	*vpp = newvp;
1003 	m_freem(mrep);
1004 nfsmout:
1005 	if (error) {
1006 		if (newvp != NULLVP) {
1007 			vrele(newvp);
1008 			*vpp = NULLVP;
1009 		}
1010 		if ((cnp->cn_nameiop == NAMEI_CREATE || cnp->cn_nameiop == NAMEI_RENAME) &&
1011 		    (flags & CNP_ISLASTCN) && error == ENOENT) {
1012 			if (!lockparent)
1013 				VOP_UNLOCK(dvp, 0, td);
1014 			if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1015 				error = EROFS;
1016 			else
1017 				error = EJUSTRETURN;
1018 		}
1019 		if (cnp->cn_nameiop != NAMEI_LOOKUP && (flags & CNP_ISLASTCN))
1020 			cnp->cn_flags |= CNP_SAVENAME;
1021 	}
1022 	return (error);
1023 }
1024 
1025 /*
1026  * nfs read call.
1027  * Just call nfs_bioread() to do the work.
1028  */
1029 static int
1030 nfs_read(ap)
1031 	struct vop_read_args /* {
1032 		struct vnode *a_vp;
1033 		struct uio *a_uio;
1034 		int  a_ioflag;
1035 		struct ucred *a_cred;
1036 	} */ *ap;
1037 {
1038 	struct vnode *vp = ap->a_vp;
1039 
1040 	if (vp->v_type != VREG)
1041 		return (EPERM);
1042 	return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag));
1043 }
1044 
1045 /*
1046  * nfs readlink call
1047  */
1048 static int
1049 nfs_readlink(ap)
1050 	struct vop_readlink_args /* {
1051 		struct vnode *a_vp;
1052 		struct uio *a_uio;
1053 		struct ucred *a_cred;
1054 	} */ *ap;
1055 {
1056 	struct vnode *vp = ap->a_vp;
1057 
1058 	if (vp->v_type != VLNK)
1059 		return (EINVAL);
1060 	return (nfs_bioread(vp, ap->a_uio, 0));
1061 }
1062 
1063 /*
1064  * Do a readlink rpc.
1065  * Called by nfs_doio() from below the buffer cache.
1066  */
1067 int
1068 nfs_readlinkrpc(struct vnode *vp, struct uio *uiop)
1069 {
1070 	u_int32_t *tl;
1071 	caddr_t cp;
1072 	int32_t t1, t2;
1073 	caddr_t bpos, dpos, cp2;
1074 	int error = 0, len, attrflag;
1075 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1076 	int v3 = NFS_ISV3(vp);
1077 
1078 	nfsstats.rpccnt[NFSPROC_READLINK]++;
1079 	nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3));
1080 	nfsm_fhtom(vp, v3);
1081 	nfsm_request(vp, NFSPROC_READLINK, uiop->uio_td, nfs_vpcred(vp, ND_CHECK));
1082 	if (v3)
1083 		nfsm_postop_attr(vp, attrflag);
1084 	if (!error) {
1085 		nfsm_strsiz(len, NFS_MAXPATHLEN);
1086 		if (len == NFS_MAXPATHLEN) {
1087 			struct nfsnode *np = VTONFS(vp);
1088 			if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1089 				len = np->n_size;
1090 		}
1091 		nfsm_mtouio(uiop, len);
1092 	}
1093 	m_freem(mrep);
1094 nfsmout:
1095 	return (error);
1096 }
1097 
1098 /*
1099  * nfs read rpc call
1100  * Ditto above
1101  */
1102 int
1103 nfs_readrpc(struct vnode *vp, struct uio *uiop)
1104 {
1105 	u_int32_t *tl;
1106 	caddr_t cp;
1107 	int32_t t1, t2;
1108 	caddr_t bpos, dpos, cp2;
1109 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1110 	struct nfsmount *nmp;
1111 	int error = 0, len, retlen, tsiz, eof, attrflag;
1112 	int v3 = NFS_ISV3(vp);
1113 
1114 #ifndef nolint
1115 	eof = 0;
1116 #endif
1117 	nmp = VFSTONFS(vp->v_mount);
1118 	tsiz = uiop->uio_resid;
1119 	if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1120 		return (EFBIG);
1121 	while (tsiz > 0) {
1122 		nfsstats.rpccnt[NFSPROC_READ]++;
1123 		len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1124 		nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1125 		nfsm_fhtom(vp, v3);
1126 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1127 		if (v3) {
1128 			txdr_hyper(uiop->uio_offset, tl);
1129 			*(tl + 2) = txdr_unsigned(len);
1130 		} else {
1131 			*tl++ = txdr_unsigned(uiop->uio_offset);
1132 			*tl++ = txdr_unsigned(len);
1133 			*tl = 0;
1134 		}
1135 		nfsm_request(vp, NFSPROC_READ, uiop->uio_td, nfs_vpcred(vp, ND_READ));
1136 		if (v3) {
1137 			nfsm_postop_attr(vp, attrflag);
1138 			if (error) {
1139 				m_freem(mrep);
1140 				goto nfsmout;
1141 			}
1142 			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1143 			eof = fxdr_unsigned(int, *(tl + 1));
1144 		} else
1145 			nfsm_loadattr(vp, (struct vattr *)0);
1146 		nfsm_strsiz(retlen, nmp->nm_rsize);
1147 		nfsm_mtouio(uiop, retlen);
1148 		m_freem(mrep);
1149 		tsiz -= retlen;
1150 		if (v3) {
1151 			if (eof || retlen == 0) {
1152 				tsiz = 0;
1153 			}
1154 		} else if (retlen < len) {
1155 			tsiz = 0;
1156 		}
1157 	}
1158 nfsmout:
1159 	return (error);
1160 }
1161 
1162 /*
1163  * nfs write call
1164  */
1165 int
1166 nfs_writerpc(vp, uiop, iomode, must_commit)
1167 	struct vnode *vp;
1168 	struct uio *uiop;
1169 	int *iomode, *must_commit;
1170 {
1171 	u_int32_t *tl;
1172 	caddr_t cp;
1173 	int32_t t1, t2, backup;
1174 	caddr_t bpos, dpos, cp2;
1175 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1176 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1177 	int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1178 	int v3 = NFS_ISV3(vp), committed = NFSV3WRITE_FILESYNC;
1179 
1180 #ifndef DIAGNOSTIC
1181 	if (uiop->uio_iovcnt != 1)
1182 		panic("nfs: writerpc iovcnt > 1");
1183 #endif
1184 	*must_commit = 0;
1185 	tsiz = uiop->uio_resid;
1186 	if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1187 		return (EFBIG);
1188 	while (tsiz > 0) {
1189 		nfsstats.rpccnt[NFSPROC_WRITE]++;
1190 		len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1191 		nfsm_reqhead(vp, NFSPROC_WRITE,
1192 			NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1193 		nfsm_fhtom(vp, v3);
1194 		if (v3) {
1195 			nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1196 			txdr_hyper(uiop->uio_offset, tl);
1197 			tl += 2;
1198 			*tl++ = txdr_unsigned(len);
1199 			*tl++ = txdr_unsigned(*iomode);
1200 			*tl = txdr_unsigned(len);
1201 		} else {
1202 			u_int32_t x;
1203 
1204 			nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1205 			/* Set both "begin" and "current" to non-garbage. */
1206 			x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1207 			*tl++ = x;	/* "begin offset" */
1208 			*tl++ = x;	/* "current offset" */
1209 			x = txdr_unsigned(len);
1210 			*tl++ = x;	/* total to this offset */
1211 			*tl = x;	/* size of this write */
1212 		}
1213 		nfsm_uiotom(uiop, len);
1214 		nfsm_request(vp, NFSPROC_WRITE, uiop->uio_td, nfs_vpcred(vp, ND_WRITE));
1215 		if (v3) {
1216 			wccflag = NFSV3_WCCCHK;
1217 			nfsm_wcc_data(vp, wccflag);
1218 			if (!error) {
1219 				nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1220 					+ NFSX_V3WRITEVERF);
1221 				rlen = fxdr_unsigned(int, *tl++);
1222 				if (rlen == 0) {
1223 					error = NFSERR_IO;
1224 					m_freem(mrep);
1225 					break;
1226 				} else if (rlen < len) {
1227 					backup = len - rlen;
1228 					uiop->uio_iov->iov_base -= backup;
1229 					uiop->uio_iov->iov_len += backup;
1230 					uiop->uio_offset -= backup;
1231 					uiop->uio_resid += backup;
1232 					len = rlen;
1233 				}
1234 				commit = fxdr_unsigned(int, *tl++);
1235 
1236 				/*
1237 				 * Return the lowest committment level
1238 				 * obtained by any of the RPCs.
1239 				 */
1240 				if (committed == NFSV3WRITE_FILESYNC)
1241 					committed = commit;
1242 				else if (committed == NFSV3WRITE_DATASYNC &&
1243 					commit == NFSV3WRITE_UNSTABLE)
1244 					committed = commit;
1245 				if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1246 				    bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1247 					NFSX_V3WRITEVERF);
1248 				    nmp->nm_state |= NFSSTA_HASWRITEVERF;
1249 				} else if (bcmp((caddr_t)tl,
1250 				    (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1251 				    *must_commit = 1;
1252 				    bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1253 					NFSX_V3WRITEVERF);
1254 				}
1255 			}
1256 		} else
1257 		    nfsm_loadattr(vp, (struct vattr *)0);
1258 		if (wccflag)
1259 		    VTONFS(vp)->n_mtime = VTONFS(vp)->n_vattr.va_mtime.tv_sec;
1260 		m_freem(mrep);
1261 		if (error)
1262 			break;
1263 		tsiz -= len;
1264 	}
1265 nfsmout:
1266 	if (vp->v_mount->mnt_flag & MNT_ASYNC)
1267 		committed = NFSV3WRITE_FILESYNC;
1268 	*iomode = committed;
1269 	if (error)
1270 		uiop->uio_resid = tsiz;
1271 	return (error);
1272 }
1273 
1274 /*
1275  * nfs mknod rpc
1276  * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1277  * mode set to specify the file type and the size field for rdev.
1278  */
1279 static int
1280 nfs_mknodrpc(dvp, vpp, cnp, vap)
1281 	struct vnode *dvp;
1282 	struct vnode **vpp;
1283 	struct componentname *cnp;
1284 	struct vattr *vap;
1285 {
1286 	struct nfsv2_sattr *sp;
1287 	u_int32_t *tl;
1288 	caddr_t cp;
1289 	int32_t t1, t2;
1290 	struct vnode *newvp = (struct vnode *)0;
1291 	struct nfsnode *np = (struct nfsnode *)0;
1292 	struct vattr vattr;
1293 	char *cp2;
1294 	caddr_t bpos, dpos;
1295 	int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1296 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1297 	u_int32_t rdev;
1298 	int v3 = NFS_ISV3(dvp);
1299 
1300 	if (vap->va_type == VCHR || vap->va_type == VBLK)
1301 		rdev = txdr_unsigned(vap->va_rdev);
1302 	else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1303 		rdev = nfs_xdrneg1;
1304 	else {
1305 		return (EOPNOTSUPP);
1306 	}
1307 	if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
1308 		return (error);
1309 	}
1310 	nfsstats.rpccnt[NFSPROC_MKNOD]++;
1311 	nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1312 		+ nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1313 	nfsm_fhtom(dvp, v3);
1314 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1315 	if (v3) {
1316 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1317 		*tl++ = vtonfsv3_type(vap->va_type);
1318 		nfsm_v3attrbuild(vap, FALSE);
1319 		if (vap->va_type == VCHR || vap->va_type == VBLK) {
1320 			nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1321 			*tl++ = txdr_unsigned(umajor(vap->va_rdev));
1322 			*tl = txdr_unsigned(uminor(vap->va_rdev));
1323 		}
1324 	} else {
1325 		nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1326 		sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1327 		sp->sa_uid = nfs_xdrneg1;
1328 		sp->sa_gid = nfs_xdrneg1;
1329 		sp->sa_size = rdev;
1330 		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1331 		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1332 	}
1333 	nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_td, cnp->cn_cred);
1334 	if (!error) {
1335 		nfsm_mtofh(dvp, newvp, v3, gotvp);
1336 		if (!gotvp) {
1337 			if (newvp) {
1338 				vput(newvp);
1339 				newvp = (struct vnode *)0;
1340 			}
1341 			error = nfs_lookitup(dvp, cnp->cn_nameptr,
1342 			    cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1343 			if (!error)
1344 				newvp = NFSTOV(np);
1345 		}
1346 	}
1347 	if (v3)
1348 		nfsm_wcc_data(dvp, wccflag);
1349 	m_freem(mrep);
1350 nfsmout:
1351 	if (error) {
1352 		if (newvp)
1353 			vput(newvp);
1354 	} else {
1355 		if (cnp->cn_flags & CNP_MAKEENTRY)
1356 			cache_enter(dvp, NCPNULL, newvp, cnp);
1357 		*vpp = newvp;
1358 	}
1359 	VTONFS(dvp)->n_flag |= NMODIFIED;
1360 	if (!wccflag)
1361 		VTONFS(dvp)->n_attrstamp = 0;
1362 	return (error);
1363 }
1364 
1365 /*
1366  * nfs mknod vop
1367  * just call nfs_mknodrpc() to do the work.
1368  */
1369 /* ARGSUSED */
1370 static int
1371 nfs_mknod(ap)
1372 	struct vop_mknod_args /* {
1373 		struct vnode *a_dvp;
1374 		struct vnode **a_vpp;
1375 		struct componentname *a_cnp;
1376 		struct vattr *a_vap;
1377 	} */ *ap;
1378 {
1379 	return nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1380 }
1381 
1382 static u_long create_verf;
1383 /*
1384  * nfs file create call
1385  */
1386 static int
1387 nfs_create(ap)
1388 	struct vop_create_args /* {
1389 		struct vnode *a_dvp;
1390 		struct vnode **a_vpp;
1391 		struct componentname *a_cnp;
1392 		struct vattr *a_vap;
1393 	} */ *ap;
1394 {
1395 	struct vnode *dvp = ap->a_dvp;
1396 	struct vattr *vap = ap->a_vap;
1397 	struct componentname *cnp = ap->a_cnp;
1398 	struct nfsv2_sattr *sp;
1399 	u_int32_t *tl;
1400 	caddr_t cp;
1401 	int32_t t1, t2;
1402 	struct nfsnode *np = (struct nfsnode *)0;
1403 	struct vnode *newvp = (struct vnode *)0;
1404 	caddr_t bpos, dpos, cp2;
1405 	int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1406 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1407 	struct vattr vattr;
1408 	int v3 = NFS_ISV3(dvp);
1409 
1410 	/*
1411 	 * Oops, not for me..
1412 	 */
1413 	if (vap->va_type == VSOCK)
1414 		return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1415 
1416 	if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
1417 		return (error);
1418 	}
1419 	if (vap->va_vaflags & VA_EXCLUSIVE)
1420 		fmode |= O_EXCL;
1421 again:
1422 	nfsstats.rpccnt[NFSPROC_CREATE]++;
1423 	nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1424 		nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1425 	nfsm_fhtom(dvp, v3);
1426 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1427 	if (v3) {
1428 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1429 		if (fmode & O_EXCL) {
1430 			*tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1431 			nfsm_build(tl, u_int32_t *, NFSX_V3CREATEVERF);
1432 #ifdef INET
1433 			if (!TAILQ_EMPTY(&in_ifaddrhead))
1434 				*tl++ = IA_SIN(in_ifaddrhead.tqh_first)->sin_addr.s_addr;
1435 			else
1436 #endif
1437 				*tl++ = create_verf;
1438 			*tl = ++create_verf;
1439 		} else {
1440 			*tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1441 			nfsm_v3attrbuild(vap, FALSE);
1442 		}
1443 	} else {
1444 		nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1445 		sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1446 		sp->sa_uid = nfs_xdrneg1;
1447 		sp->sa_gid = nfs_xdrneg1;
1448 		sp->sa_size = 0;
1449 		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1450 		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1451 	}
1452 	nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_td, cnp->cn_cred);
1453 	if (!error) {
1454 		nfsm_mtofh(dvp, newvp, v3, gotvp);
1455 		if (!gotvp) {
1456 			if (newvp) {
1457 				vput(newvp);
1458 				newvp = (struct vnode *)0;
1459 			}
1460 			error = nfs_lookitup(dvp, cnp->cn_nameptr,
1461 			    cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1462 			if (!error)
1463 				newvp = NFSTOV(np);
1464 		}
1465 	}
1466 	if (v3)
1467 		nfsm_wcc_data(dvp, wccflag);
1468 	m_freem(mrep);
1469 nfsmout:
1470 	if (error) {
1471 		if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1472 			fmode &= ~O_EXCL;
1473 			goto again;
1474 		}
1475 		if (newvp)
1476 			vput(newvp);
1477 	} else if (v3 && (fmode & O_EXCL)) {
1478 		/*
1479 		 * We are normally called with only a partially initialized
1480 		 * VAP.  Since the NFSv3 spec says that server may use the
1481 		 * file attributes to store the verifier, the spec requires
1482 		 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1483 		 * in atime, but we can't really assume that all servers will
1484 		 * so we ensure that our SETATTR sets both atime and mtime.
1485 		 */
1486 		if (vap->va_mtime.tv_sec == VNOVAL)
1487 			vfs_timestamp(&vap->va_mtime);
1488 		if (vap->va_atime.tv_sec == VNOVAL)
1489 			vap->va_atime = vap->va_mtime;
1490 		error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_td);
1491 	}
1492 	if (!error) {
1493 		if (cnp->cn_flags & CNP_MAKEENTRY)
1494 			cache_enter(dvp, NCPNULL, newvp, cnp);
1495 		/*
1496 		 * The new np may have enough info for access
1497 		 * checks, make sure rucred and wucred are
1498 		 * initialized for read and write rpc's.
1499 		 */
1500 		np = VTONFS(newvp);
1501 		if (np->n_rucred == NULL)
1502 			np->n_rucred = crhold(cnp->cn_cred);
1503 		if (np->n_wucred == NULL)
1504 			np->n_wucred = crhold(cnp->cn_cred);
1505 		*ap->a_vpp = newvp;
1506 	}
1507 	VTONFS(dvp)->n_flag |= NMODIFIED;
1508 	if (!wccflag)
1509 		VTONFS(dvp)->n_attrstamp = 0;
1510 	return (error);
1511 }
1512 
1513 /*
1514  * nfs file remove call
1515  * To try and make nfs semantics closer to ufs semantics, a file that has
1516  * other processes using the vnode is renamed instead of removed and then
1517  * removed later on the last close.
1518  * - If v_usecount > 1
1519  *	  If a rename is not already in the works
1520  *	     call nfs_sillyrename() to set it up
1521  *     else
1522  *	  do the remove rpc
1523  */
1524 static int
1525 nfs_remove(ap)
1526 	struct vop_remove_args /* {
1527 		struct vnodeop_desc *a_desc;
1528 		struct vnode * a_dvp;
1529 		struct vnode * a_vp;
1530 		struct componentname * a_cnp;
1531 	} */ *ap;
1532 {
1533 	struct vnode *vp = ap->a_vp;
1534 	struct vnode *dvp = ap->a_dvp;
1535 	struct componentname *cnp = ap->a_cnp;
1536 	struct nfsnode *np = VTONFS(vp);
1537 	int error = 0;
1538 	struct vattr vattr;
1539 
1540 #ifndef DIAGNOSTIC
1541 	if ((cnp->cn_flags & CNP_HASBUF) == 0)
1542 		panic("nfs_remove: no name");
1543 	if (vp->v_usecount < 1)
1544 		panic("nfs_remove: bad v_usecount");
1545 #endif
1546 	if (vp->v_type == VDIR)
1547 		error = EPERM;
1548 	else if (vp->v_usecount == 1 || (np->n_sillyrename &&
1549 	    VOP_GETATTR(vp, &vattr, cnp->cn_td) == 0 &&
1550 	    vattr.va_nlink > 1)) {
1551 		/*
1552 		 * Purge the name cache so that the chance of a lookup for
1553 		 * the name succeeding while the remove is in progress is
1554 		 * minimized. Without node locking it can still happen, such
1555 		 * that an I/O op returns ESTALE, but since you get this if
1556 		 * another host removes the file..
1557 		 */
1558 		cache_purge(vp);
1559 		/*
1560 		 * throw away biocache buffers, mainly to avoid
1561 		 * unnecessary delayed writes later.
1562 		 */
1563 		error = nfs_vinvalbuf(vp, 0, cnp->cn_td, 1);
1564 		/* Do the rpc */
1565 		if (error != EINTR)
1566 			error = nfs_removerpc(dvp, cnp->cn_nameptr,
1567 				cnp->cn_namelen, cnp->cn_cred, cnp->cn_td);
1568 		/*
1569 		 * Kludge City: If the first reply to the remove rpc is lost..
1570 		 *   the reply to the retransmitted request will be ENOENT
1571 		 *   since the file was in fact removed
1572 		 *   Therefore, we cheat and return success.
1573 		 */
1574 		if (error == ENOENT)
1575 			error = 0;
1576 	} else if (!np->n_sillyrename)
1577 		error = nfs_sillyrename(dvp, vp, cnp);
1578 	np->n_attrstamp = 0;
1579 	return (error);
1580 }
1581 
1582 /*
1583  * nfs file remove rpc called from nfs_inactive
1584  */
1585 int
1586 nfs_removeit(struct sillyrename *sp)
1587 {
1588 
1589 	return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen,
1590 		sp->s_cred, NULL));
1591 }
1592 
1593 /*
1594  * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1595  */
1596 static int
1597 nfs_removerpc(dvp, name, namelen, cred, td)
1598 	struct vnode *dvp;
1599 	const char *name;
1600 	int namelen;
1601 	struct ucred *cred;
1602 	struct thread *td;
1603 {
1604 	u_int32_t *tl;
1605 	caddr_t cp;
1606 	int32_t t1, t2;
1607 	caddr_t bpos, dpos, cp2;
1608 	int error = 0, wccflag = NFSV3_WCCRATTR;
1609 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1610 	int v3 = NFS_ISV3(dvp);
1611 
1612 	nfsstats.rpccnt[NFSPROC_REMOVE]++;
1613 	nfsm_reqhead(dvp, NFSPROC_REMOVE,
1614 		NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1615 	nfsm_fhtom(dvp, v3);
1616 	nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1617 	nfsm_request(dvp, NFSPROC_REMOVE, td, cred);
1618 	if (v3)
1619 		nfsm_wcc_data(dvp, wccflag);
1620 	m_freem(mrep);
1621 nfsmout:
1622 	VTONFS(dvp)->n_flag |= NMODIFIED;
1623 	if (!wccflag)
1624 		VTONFS(dvp)->n_attrstamp = 0;
1625 	return (error);
1626 }
1627 
1628 /*
1629  * nfs file rename call
1630  */
1631 static int
1632 nfs_rename(ap)
1633 	struct vop_rename_args  /* {
1634 		struct vnode *a_fdvp;
1635 		struct vnode *a_fvp;
1636 		struct componentname *a_fcnp;
1637 		struct vnode *a_tdvp;
1638 		struct vnode *a_tvp;
1639 		struct componentname *a_tcnp;
1640 	} */ *ap;
1641 {
1642 	struct vnode *fvp = ap->a_fvp;
1643 	struct vnode *tvp = ap->a_tvp;
1644 	struct vnode *fdvp = ap->a_fdvp;
1645 	struct vnode *tdvp = ap->a_tdvp;
1646 	struct componentname *tcnp = ap->a_tcnp;
1647 	struct componentname *fcnp = ap->a_fcnp;
1648 	int error;
1649 
1650 #ifndef DIAGNOSTIC
1651 	if ((tcnp->cn_flags & CNP_HASBUF) == 0 ||
1652 	    (fcnp->cn_flags & CNP_HASBUF) == 0)
1653 		panic("nfs_rename: no name");
1654 #endif
1655 	/* Check for cross-device rename */
1656 	if ((fvp->v_mount != tdvp->v_mount) ||
1657 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
1658 		error = EXDEV;
1659 		goto out;
1660 	}
1661 
1662 	/*
1663 	 * We have to flush B_DELWRI data prior to renaming
1664 	 * the file.  If we don't, the delayed-write buffers
1665 	 * can be flushed out later after the file has gone stale
1666 	 * under NFSV3.  NFSV2 does not have this problem because
1667 	 * ( as far as I can tell ) it flushes dirty buffers more
1668 	 * often.
1669 	 */
1670 
1671 	VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_td);
1672 	if (tvp)
1673 	    VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_td);
1674 
1675 	/*
1676 	 * If the tvp exists and is in use, sillyrename it before doing the
1677 	 * rename of the new file over it.
1678 	 * XXX Can't sillyrename a directory.
1679 	 */
1680 	if (tvp && tvp->v_usecount > 1 && !VTONFS(tvp)->n_sillyrename &&
1681 		tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1682 		vput(tvp);
1683 		tvp = NULL;
1684 	}
1685 
1686 	error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1687 		tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1688 		tcnp->cn_td);
1689 
1690 	if (fvp->v_type == VDIR) {
1691 		if (tvp != NULL && tvp->v_type == VDIR)
1692 			cache_purge(tdvp);
1693 		cache_purge(fdvp);
1694 	}
1695 
1696 out:
1697 	if (tdvp == tvp)
1698 		vrele(tdvp);
1699 	else
1700 		vput(tdvp);
1701 	if (tvp)
1702 		vput(tvp);
1703 	vrele(fdvp);
1704 	vrele(fvp);
1705 	/*
1706 	 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1707 	 */
1708 	if (error == ENOENT)
1709 		error = 0;
1710 	return (error);
1711 }
1712 
1713 /*
1714  * nfs file rename rpc called from nfs_remove() above
1715  */
1716 static int
1717 nfs_renameit(sdvp, scnp, sp)
1718 	struct vnode *sdvp;
1719 	struct componentname *scnp;
1720 	struct sillyrename *sp;
1721 {
1722 	return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
1723 		sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_td));
1724 }
1725 
1726 /*
1727  * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1728  */
1729 static int
1730 nfs_renamerpc(fdvp, fnameptr, fnamelen, tdvp, tnameptr, tnamelen, cred, td)
1731 	struct vnode *fdvp;
1732 	const char *fnameptr;
1733 	int fnamelen;
1734 	struct vnode *tdvp;
1735 	const char *tnameptr;
1736 	int tnamelen;
1737 	struct ucred *cred;
1738 	struct thread *td;
1739 {
1740 	u_int32_t *tl;
1741 	caddr_t cp;
1742 	int32_t t1, t2;
1743 	caddr_t bpos, dpos, cp2;
1744 	int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1745 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1746 	int v3 = NFS_ISV3(fdvp);
1747 
1748 	nfsstats.rpccnt[NFSPROC_RENAME]++;
1749 	nfsm_reqhead(fdvp, NFSPROC_RENAME,
1750 		(NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
1751 		nfsm_rndup(tnamelen));
1752 	nfsm_fhtom(fdvp, v3);
1753 	nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
1754 	nfsm_fhtom(tdvp, v3);
1755 	nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
1756 	nfsm_request(fdvp, NFSPROC_RENAME, td, cred);
1757 	if (v3) {
1758 		nfsm_wcc_data(fdvp, fwccflag);
1759 		nfsm_wcc_data(tdvp, twccflag);
1760 	}
1761 	m_freem(mrep);
1762 nfsmout:
1763 	VTONFS(fdvp)->n_flag |= NMODIFIED;
1764 	VTONFS(tdvp)->n_flag |= NMODIFIED;
1765 	if (!fwccflag)
1766 		VTONFS(fdvp)->n_attrstamp = 0;
1767 	if (!twccflag)
1768 		VTONFS(tdvp)->n_attrstamp = 0;
1769 	return (error);
1770 }
1771 
1772 /*
1773  * nfs hard link create call
1774  */
1775 static int
1776 nfs_link(ap)
1777 	struct vop_link_args /* {
1778 		struct vnode *a_tdvp;
1779 		struct vnode *a_vp;
1780 		struct componentname *a_cnp;
1781 	} */ *ap;
1782 {
1783 	struct vnode *vp = ap->a_vp;
1784 	struct vnode *tdvp = ap->a_tdvp;
1785 	struct componentname *cnp = ap->a_cnp;
1786 	u_int32_t *tl;
1787 	caddr_t cp;
1788 	int32_t t1, t2;
1789 	caddr_t bpos, dpos, cp2;
1790 	int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
1791 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1792 	int v3;
1793 
1794 	if (vp->v_mount != tdvp->v_mount) {
1795 		return (EXDEV);
1796 	}
1797 
1798 	/*
1799 	 * Push all writes to the server, so that the attribute cache
1800 	 * doesn't get "out of sync" with the server.
1801 	 * XXX There should be a better way!
1802 	 */
1803 	VOP_FSYNC(vp, MNT_WAIT, cnp->cn_td);
1804 
1805 	v3 = NFS_ISV3(vp);
1806 	nfsstats.rpccnt[NFSPROC_LINK]++;
1807 	nfsm_reqhead(vp, NFSPROC_LINK,
1808 		NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1809 	nfsm_fhtom(vp, v3);
1810 	nfsm_fhtom(tdvp, v3);
1811 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1812 	nfsm_request(vp, NFSPROC_LINK, cnp->cn_td, cnp->cn_cred);
1813 	if (v3) {
1814 		nfsm_postop_attr(vp, attrflag);
1815 		nfsm_wcc_data(tdvp, wccflag);
1816 	}
1817 	m_freem(mrep);
1818 nfsmout:
1819 	VTONFS(tdvp)->n_flag |= NMODIFIED;
1820 	if (!attrflag)
1821 		VTONFS(vp)->n_attrstamp = 0;
1822 	if (!wccflag)
1823 		VTONFS(tdvp)->n_attrstamp = 0;
1824 	/*
1825 	 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1826 	 */
1827 	if (error == EEXIST)
1828 		error = 0;
1829 	return (error);
1830 }
1831 
1832 /*
1833  * nfs symbolic link create call
1834  */
1835 static int
1836 nfs_symlink(ap)
1837 	struct vop_symlink_args /* {
1838 		struct vnode *a_dvp;
1839 		struct vnode **a_vpp;
1840 		struct componentname *a_cnp;
1841 		struct vattr *a_vap;
1842 		char *a_target;
1843 	} */ *ap;
1844 {
1845 	struct vnode *dvp = ap->a_dvp;
1846 	struct vattr *vap = ap->a_vap;
1847 	struct componentname *cnp = ap->a_cnp;
1848 	struct nfsv2_sattr *sp;
1849 	u_int32_t *tl;
1850 	caddr_t cp;
1851 	int32_t t1, t2;
1852 	caddr_t bpos, dpos, cp2;
1853 	int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
1854 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1855 	struct vnode *newvp = (struct vnode *)0;
1856 	int v3 = NFS_ISV3(dvp);
1857 
1858 	nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1859 	slen = strlen(ap->a_target);
1860 	nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
1861 	    nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
1862 	nfsm_fhtom(dvp, v3);
1863 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1864 	if (v3) {
1865 		nfsm_v3attrbuild(vap, FALSE);
1866 	}
1867 	nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
1868 	if (!v3) {
1869 		nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1870 		sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
1871 		sp->sa_uid = nfs_xdrneg1;
1872 		sp->sa_gid = nfs_xdrneg1;
1873 		sp->sa_size = nfs_xdrneg1;
1874 		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1875 		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1876 	}
1877 
1878 	/*
1879 	 * Issue the NFS request and get the rpc response.
1880 	 *
1881 	 * Only NFSv3 responses returning an error of 0 actually return
1882 	 * a file handle that can be converted into newvp without having
1883 	 * to do an extra lookup rpc.
1884 	 */
1885 	nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_td, cnp->cn_cred);
1886 	if (v3) {
1887 		if (error == 0)
1888 			nfsm_mtofh(dvp, newvp, v3, gotvp);
1889 		nfsm_wcc_data(dvp, wccflag);
1890 	}
1891 
1892 	/*
1893 	 * out code jumps -> here, mrep is also freed.
1894 	 */
1895 
1896 	m_freem(mrep);
1897 nfsmout:
1898 
1899 	/*
1900 	 * If we get an EEXIST error, silently convert it to no-error
1901 	 * in case of an NFS retry.
1902 	 */
1903 	if (error == EEXIST)
1904 		error = 0;
1905 
1906 	/*
1907 	 * If we do not have (or no longer have) an error, and we could
1908 	 * not extract the newvp from the response due to the request being
1909 	 * NFSv2 or the error being EEXIST.  We have to do a lookup in order
1910 	 * to obtain a newvp to return.
1911 	 */
1912 	if (error == 0 && newvp == NULL) {
1913 		struct nfsnode *np = NULL;
1914 
1915 		error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1916 		    cnp->cn_cred, cnp->cn_td, &np);
1917 		if (!error)
1918 			newvp = NFSTOV(np);
1919 	}
1920 	if (error) {
1921 		if (newvp)
1922 			vput(newvp);
1923 	} else {
1924 		*ap->a_vpp = newvp;
1925 	}
1926 	VTONFS(dvp)->n_flag |= NMODIFIED;
1927 	if (!wccflag)
1928 		VTONFS(dvp)->n_attrstamp = 0;
1929 	return (error);
1930 }
1931 
1932 /*
1933  * nfs make dir call
1934  */
1935 static int
1936 nfs_mkdir(ap)
1937 	struct vop_mkdir_args /* {
1938 		struct vnode *a_dvp;
1939 		struct vnode **a_vpp;
1940 		struct componentname *a_cnp;
1941 		struct vattr *a_vap;
1942 	} */ *ap;
1943 {
1944 	struct vnode *dvp = ap->a_dvp;
1945 	struct vattr *vap = ap->a_vap;
1946 	struct componentname *cnp = ap->a_cnp;
1947 	struct nfsv2_sattr *sp;
1948 	u_int32_t *tl;
1949 	caddr_t cp;
1950 	int32_t t1, t2;
1951 	int len;
1952 	struct nfsnode *np = (struct nfsnode *)0;
1953 	struct vnode *newvp = (struct vnode *)0;
1954 	caddr_t bpos, dpos, cp2;
1955 	int error = 0, wccflag = NFSV3_WCCRATTR;
1956 	int gotvp = 0;
1957 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1958 	struct vattr vattr;
1959 	int v3 = NFS_ISV3(dvp);
1960 
1961 	if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
1962 		return (error);
1963 	}
1964 	len = cnp->cn_namelen;
1965 	nfsstats.rpccnt[NFSPROC_MKDIR]++;
1966 	nfsm_reqhead(dvp, NFSPROC_MKDIR,
1967 	  NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
1968 	nfsm_fhtom(dvp, v3);
1969 	nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
1970 	if (v3) {
1971 		nfsm_v3attrbuild(vap, FALSE);
1972 	} else {
1973 		nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1974 		sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
1975 		sp->sa_uid = nfs_xdrneg1;
1976 		sp->sa_gid = nfs_xdrneg1;
1977 		sp->sa_size = nfs_xdrneg1;
1978 		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1979 		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1980 	}
1981 	nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_td, cnp->cn_cred);
1982 	if (!error)
1983 		nfsm_mtofh(dvp, newvp, v3, gotvp);
1984 	if (v3)
1985 		nfsm_wcc_data(dvp, wccflag);
1986 	m_freem(mrep);
1987 nfsmout:
1988 	VTONFS(dvp)->n_flag |= NMODIFIED;
1989 	if (!wccflag)
1990 		VTONFS(dvp)->n_attrstamp = 0;
1991 	/*
1992 	 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
1993 	 * if we can succeed in looking up the directory.
1994 	 */
1995 	if (error == EEXIST || (!error && !gotvp)) {
1996 		if (newvp) {
1997 			vrele(newvp);
1998 			newvp = (struct vnode *)0;
1999 		}
2000 		error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
2001 			cnp->cn_td, &np);
2002 		if (!error) {
2003 			newvp = NFSTOV(np);
2004 			if (newvp->v_type != VDIR)
2005 				error = EEXIST;
2006 		}
2007 	}
2008 	if (error) {
2009 		if (newvp)
2010 			vrele(newvp);
2011 	} else
2012 		*ap->a_vpp = newvp;
2013 	return (error);
2014 }
2015 
2016 /*
2017  * nfs remove directory call
2018  */
2019 static int
2020 nfs_rmdir(ap)
2021 	struct vop_rmdir_args /* {
2022 		struct vnode *a_dvp;
2023 		struct vnode *a_vp;
2024 		struct componentname *a_cnp;
2025 	} */ *ap;
2026 {
2027 	struct vnode *vp = ap->a_vp;
2028 	struct vnode *dvp = ap->a_dvp;
2029 	struct componentname *cnp = ap->a_cnp;
2030 	u_int32_t *tl;
2031 	caddr_t cp;
2032 	int32_t t1, t2;
2033 	caddr_t bpos, dpos, cp2;
2034 	int error = 0, wccflag = NFSV3_WCCRATTR;
2035 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2036 	int v3 = NFS_ISV3(dvp);
2037 
2038 	if (dvp == vp)
2039 		return (EINVAL);
2040 	nfsstats.rpccnt[NFSPROC_RMDIR]++;
2041 	nfsm_reqhead(dvp, NFSPROC_RMDIR,
2042 		NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2043 	nfsm_fhtom(dvp, v3);
2044 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2045 	nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_td, cnp->cn_cred);
2046 	if (v3)
2047 		nfsm_wcc_data(dvp, wccflag);
2048 	m_freem(mrep);
2049 nfsmout:
2050 	VTONFS(dvp)->n_flag |= NMODIFIED;
2051 	if (!wccflag)
2052 		VTONFS(dvp)->n_attrstamp = 0;
2053 	cache_purge(dvp);
2054 	cache_purge(vp);
2055 	/*
2056 	 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2057 	 */
2058 	if (error == ENOENT)
2059 		error = 0;
2060 	return (error);
2061 }
2062 
2063 /*
2064  * nfs readdir call
2065  */
2066 static int
2067 nfs_readdir(ap)
2068 	struct vop_readdir_args /* {
2069 		struct vnode *a_vp;
2070 		struct uio *a_uio;
2071 		struct ucred *a_cred;
2072 	} */ *ap;
2073 {
2074 	struct vnode *vp = ap->a_vp;
2075 	struct nfsnode *np = VTONFS(vp);
2076 	struct uio *uio = ap->a_uio;
2077 	int tresid, error;
2078 	struct vattr vattr;
2079 
2080 	if (vp->v_type != VDIR)
2081 		return (EPERM);
2082 	/*
2083 	 * First, check for hit on the EOF offset cache
2084 	 */
2085 	if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2086 	    (np->n_flag & NMODIFIED) == 0) {
2087 		if (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) {
2088 			if (NQNFS_CKCACHABLE(vp, ND_READ)) {
2089 				nfsstats.direofcache_hits++;
2090 				return (0);
2091 			}
2092 		} else if (VOP_GETATTR(vp, &vattr, uio->uio_td) == 0 &&
2093 			np->n_mtime == vattr.va_mtime.tv_sec) {
2094 			nfsstats.direofcache_hits++;
2095 			return (0);
2096 		}
2097 	}
2098 
2099 	/*
2100 	 * Call nfs_bioread() to do the real work.
2101 	 */
2102 	tresid = uio->uio_resid;
2103 	error = nfs_bioread(vp, uio, 0);
2104 
2105 	if (!error && uio->uio_resid == tresid)
2106 		nfsstats.direofcache_misses++;
2107 	return (error);
2108 }
2109 
2110 /*
2111  * Readdir rpc call.
2112  * Called from below the buffer cache by nfs_doio().
2113  */
2114 int
2115 nfs_readdirrpc(struct vnode *vp, struct uio *uiop)
2116 {
2117 	int len, left;
2118 	struct dirent *dp = NULL;
2119 	u_int32_t *tl;
2120 	caddr_t cp;
2121 	int32_t t1, t2;
2122 	nfsuint64 *cookiep;
2123 	caddr_t bpos, dpos, cp2;
2124 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2125 	nfsuint64 cookie;
2126 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2127 	struct nfsnode *dnp = VTONFS(vp);
2128 	u_quad_t fileno;
2129 	int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2130 	int attrflag;
2131 	int v3 = NFS_ISV3(vp);
2132 
2133 #ifndef DIAGNOSTIC
2134 	if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2135 		(uiop->uio_resid & (DIRBLKSIZ - 1)))
2136 		panic("nfs readdirrpc bad uio");
2137 #endif
2138 
2139 	/*
2140 	 * If there is no cookie, assume directory was stale.
2141 	 */
2142 	cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2143 	if (cookiep)
2144 		cookie = *cookiep;
2145 	else
2146 		return (NFSERR_BAD_COOKIE);
2147 	/*
2148 	 * Loop around doing readdir rpc's of size nm_readdirsize
2149 	 * truncated to a multiple of DIRBLKSIZ.
2150 	 * The stopping criteria is EOF or buffer full.
2151 	 */
2152 	while (more_dirs && bigenough) {
2153 		nfsstats.rpccnt[NFSPROC_READDIR]++;
2154 		nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) +
2155 			NFSX_READDIR(v3));
2156 		nfsm_fhtom(vp, v3);
2157 		if (v3) {
2158 			nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2159 			*tl++ = cookie.nfsuquad[0];
2160 			*tl++ = cookie.nfsuquad[1];
2161 			*tl++ = dnp->n_cookieverf.nfsuquad[0];
2162 			*tl++ = dnp->n_cookieverf.nfsuquad[1];
2163 		} else {
2164 			nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2165 			*tl++ = cookie.nfsuquad[0];
2166 		}
2167 		*tl = txdr_unsigned(nmp->nm_readdirsize);
2168 		nfsm_request(vp, NFSPROC_READDIR, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2169 		if (v3) {
2170 			nfsm_postop_attr(vp, attrflag);
2171 			if (!error) {
2172 				nfsm_dissect(tl, u_int32_t *,
2173 				    2 * NFSX_UNSIGNED);
2174 				dnp->n_cookieverf.nfsuquad[0] = *tl++;
2175 				dnp->n_cookieverf.nfsuquad[1] = *tl;
2176 			} else {
2177 				m_freem(mrep);
2178 				goto nfsmout;
2179 			}
2180 		}
2181 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2182 		more_dirs = fxdr_unsigned(int, *tl);
2183 
2184 		/* loop thru the dir entries, doctoring them to 4bsd form */
2185 		while (more_dirs && bigenough) {
2186 			if (v3) {
2187 				nfsm_dissect(tl, u_int32_t *,
2188 				    3 * NFSX_UNSIGNED);
2189 				fileno = fxdr_hyper(tl);
2190 				len = fxdr_unsigned(int, *(tl + 2));
2191 			} else {
2192 				nfsm_dissect(tl, u_int32_t *,
2193 				    2 * NFSX_UNSIGNED);
2194 				fileno = fxdr_unsigned(u_quad_t, *tl++);
2195 				len = fxdr_unsigned(int, *tl);
2196 			}
2197 			if (len <= 0 || len > NFS_MAXNAMLEN) {
2198 				error = EBADRPC;
2199 				m_freem(mrep);
2200 				goto nfsmout;
2201 			}
2202 			tlen = nfsm_rndup(len);
2203 			if (tlen == len)
2204 				tlen += 4;	/* To ensure null termination */
2205 			left = DIRBLKSIZ - blksiz;
2206 			if ((tlen + DIRHDSIZ) > left) {
2207 				dp->d_reclen += left;
2208 				uiop->uio_iov->iov_base += left;
2209 				uiop->uio_iov->iov_len -= left;
2210 				uiop->uio_offset += left;
2211 				uiop->uio_resid -= left;
2212 				blksiz = 0;
2213 			}
2214 			if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2215 				bigenough = 0;
2216 			if (bigenough) {
2217 				dp = (struct dirent *)uiop->uio_iov->iov_base;
2218 				dp->d_fileno = (int)fileno;
2219 				dp->d_namlen = len;
2220 				dp->d_reclen = tlen + DIRHDSIZ;
2221 				dp->d_type = DT_UNKNOWN;
2222 				blksiz += dp->d_reclen;
2223 				if (blksiz == DIRBLKSIZ)
2224 					blksiz = 0;
2225 				uiop->uio_offset += DIRHDSIZ;
2226 				uiop->uio_resid -= DIRHDSIZ;
2227 				uiop->uio_iov->iov_base += DIRHDSIZ;
2228 				uiop->uio_iov->iov_len -= DIRHDSIZ;
2229 				nfsm_mtouio(uiop, len);
2230 				cp = uiop->uio_iov->iov_base;
2231 				tlen -= len;
2232 				*cp = '\0';	/* null terminate */
2233 				uiop->uio_iov->iov_base += tlen;
2234 				uiop->uio_iov->iov_len -= tlen;
2235 				uiop->uio_offset += tlen;
2236 				uiop->uio_resid -= tlen;
2237 			} else
2238 				nfsm_adv(nfsm_rndup(len));
2239 			if (v3) {
2240 				nfsm_dissect(tl, u_int32_t *,
2241 				    3 * NFSX_UNSIGNED);
2242 			} else {
2243 				nfsm_dissect(tl, u_int32_t *,
2244 				    2 * NFSX_UNSIGNED);
2245 			}
2246 			if (bigenough) {
2247 				cookie.nfsuquad[0] = *tl++;
2248 				if (v3)
2249 					cookie.nfsuquad[1] = *tl++;
2250 			} else if (v3)
2251 				tl += 2;
2252 			else
2253 				tl++;
2254 			more_dirs = fxdr_unsigned(int, *tl);
2255 		}
2256 		/*
2257 		 * If at end of rpc data, get the eof boolean
2258 		 */
2259 		if (!more_dirs) {
2260 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2261 			more_dirs = (fxdr_unsigned(int, *tl) == 0);
2262 		}
2263 		m_freem(mrep);
2264 	}
2265 	/*
2266 	 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2267 	 * by increasing d_reclen for the last record.
2268 	 */
2269 	if (blksiz > 0) {
2270 		left = DIRBLKSIZ - blksiz;
2271 		dp->d_reclen += left;
2272 		uiop->uio_iov->iov_base += left;
2273 		uiop->uio_iov->iov_len -= left;
2274 		uiop->uio_offset += left;
2275 		uiop->uio_resid -= left;
2276 	}
2277 
2278 	/*
2279 	 * We are now either at the end of the directory or have filled the
2280 	 * block.
2281 	 */
2282 	if (bigenough)
2283 		dnp->n_direofoffset = uiop->uio_offset;
2284 	else {
2285 		if (uiop->uio_resid > 0)
2286 			printf("EEK! readdirrpc resid > 0\n");
2287 		cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2288 		*cookiep = cookie;
2289 	}
2290 nfsmout:
2291 	return (error);
2292 }
2293 
2294 /*
2295  * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2296  */
2297 int
2298 nfs_readdirplusrpc(struct vnode *vp, struct uio *uiop)
2299 {
2300 	int len, left;
2301 	struct dirent *dp;
2302 	u_int32_t *tl;
2303 	caddr_t cp;
2304 	int32_t t1, t2;
2305 	struct vnode *newvp;
2306 	nfsuint64 *cookiep;
2307 	caddr_t bpos, dpos, cp2, dpossav1, dpossav2;
2308 	struct mbuf *mreq, *mrep, *md, *mb, *mb2, *mdsav1, *mdsav2;
2309 	struct nameidata nami, *ndp = &nami;
2310 	struct componentname *cnp = &ndp->ni_cnd;
2311 	nfsuint64 cookie;
2312 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2313 	struct nfsnode *dnp = VTONFS(vp), *np;
2314 	nfsfh_t *fhp;
2315 	u_quad_t fileno;
2316 	int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2317 	int attrflag, fhsize;
2318 
2319 #ifndef nolint
2320 	dp = (struct dirent *)0;
2321 #endif
2322 #ifndef DIAGNOSTIC
2323 	if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2324 		(uiop->uio_resid & (DIRBLKSIZ - 1)))
2325 		panic("nfs readdirplusrpc bad uio");
2326 #endif
2327 	ndp->ni_dvp = vp;
2328 	newvp = NULLVP;
2329 
2330 	/*
2331 	 * If there is no cookie, assume directory was stale.
2332 	 */
2333 	cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2334 	if (cookiep)
2335 		cookie = *cookiep;
2336 	else
2337 		return (NFSERR_BAD_COOKIE);
2338 	/*
2339 	 * Loop around doing readdir rpc's of size nm_readdirsize
2340 	 * truncated to a multiple of DIRBLKSIZ.
2341 	 * The stopping criteria is EOF or buffer full.
2342 	 */
2343 	while (more_dirs && bigenough) {
2344 		nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2345 		nfsm_reqhead(vp, NFSPROC_READDIRPLUS,
2346 			NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2347 		nfsm_fhtom(vp, 1);
2348  		nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2349 		*tl++ = cookie.nfsuquad[0];
2350 		*tl++ = cookie.nfsuquad[1];
2351 		*tl++ = dnp->n_cookieverf.nfsuquad[0];
2352 		*tl++ = dnp->n_cookieverf.nfsuquad[1];
2353 		*tl++ = txdr_unsigned(nmp->nm_readdirsize);
2354 		*tl = txdr_unsigned(nmp->nm_rsize);
2355 		nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2356 		nfsm_postop_attr(vp, attrflag);
2357 		if (error) {
2358 			m_freem(mrep);
2359 			goto nfsmout;
2360 		}
2361 		nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2362 		dnp->n_cookieverf.nfsuquad[0] = *tl++;
2363 		dnp->n_cookieverf.nfsuquad[1] = *tl++;
2364 		more_dirs = fxdr_unsigned(int, *tl);
2365 
2366 		/* loop thru the dir entries, doctoring them to 4bsd form */
2367 		while (more_dirs && bigenough) {
2368 			nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2369 			fileno = fxdr_hyper(tl);
2370 			len = fxdr_unsigned(int, *(tl + 2));
2371 			if (len <= 0 || len > NFS_MAXNAMLEN) {
2372 				error = EBADRPC;
2373 				m_freem(mrep);
2374 				goto nfsmout;
2375 			}
2376 			tlen = nfsm_rndup(len);
2377 			if (tlen == len)
2378 				tlen += 4;	/* To ensure null termination*/
2379 			left = DIRBLKSIZ - blksiz;
2380 			if ((tlen + DIRHDSIZ) > left) {
2381 				dp->d_reclen += left;
2382 				uiop->uio_iov->iov_base += left;
2383 				uiop->uio_iov->iov_len -= left;
2384 				uiop->uio_offset += left;
2385 				uiop->uio_resid -= left;
2386 				blksiz = 0;
2387 			}
2388 			if ((tlen + DIRHDSIZ) > uiop->uio_resid)
2389 				bigenough = 0;
2390 			if (bigenough) {
2391 				dp = (struct dirent *)uiop->uio_iov->iov_base;
2392 				dp->d_fileno = (int)fileno;
2393 				dp->d_namlen = len;
2394 				dp->d_reclen = tlen + DIRHDSIZ;
2395 				dp->d_type = DT_UNKNOWN;
2396 				blksiz += dp->d_reclen;
2397 				if (blksiz == DIRBLKSIZ)
2398 					blksiz = 0;
2399 				uiop->uio_offset += DIRHDSIZ;
2400 				uiop->uio_resid -= DIRHDSIZ;
2401 				uiop->uio_iov->iov_base += DIRHDSIZ;
2402 				uiop->uio_iov->iov_len -= DIRHDSIZ;
2403 				cnp->cn_nameptr = uiop->uio_iov->iov_base;
2404 				cnp->cn_namelen = len;
2405 				nfsm_mtouio(uiop, len);
2406 				cp = uiop->uio_iov->iov_base;
2407 				tlen -= len;
2408 				*cp = '\0';
2409 				uiop->uio_iov->iov_base += tlen;
2410 				uiop->uio_iov->iov_len -= tlen;
2411 				uiop->uio_offset += tlen;
2412 				uiop->uio_resid -= tlen;
2413 			} else
2414 				nfsm_adv(nfsm_rndup(len));
2415 			nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2416 			if (bigenough) {
2417 				cookie.nfsuquad[0] = *tl++;
2418 				cookie.nfsuquad[1] = *tl++;
2419 			} else
2420 				tl += 2;
2421 
2422 			/*
2423 			 * Since the attributes are before the file handle
2424 			 * (sigh), we must skip over the attributes and then
2425 			 * come back and get them.
2426 			 */
2427 			attrflag = fxdr_unsigned(int, *tl);
2428 			if (attrflag) {
2429 			    dpossav1 = dpos;
2430 			    mdsav1 = md;
2431 			    nfsm_adv(NFSX_V3FATTR);
2432 			    nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2433 			    doit = fxdr_unsigned(int, *tl);
2434 			    if (doit) {
2435 				nfsm_getfh(fhp, fhsize, 1);
2436 				if (NFS_CMPFH(dnp, fhp, fhsize)) {
2437 				    VREF(vp);
2438 				    newvp = vp;
2439 				    np = dnp;
2440 				} else {
2441 				    error = nfs_nget(vp->v_mount, fhp,
2442 					fhsize, &np);
2443 				    if (error)
2444 					doit = 0;
2445 				    else
2446 					newvp = NFSTOV(np);
2447 				}
2448 			    }
2449 			    if (doit && bigenough) {
2450 				dpossav2 = dpos;
2451 				dpos = dpossav1;
2452 				mdsav2 = md;
2453 				md = mdsav1;
2454 				nfsm_loadattr(newvp, (struct vattr *)0);
2455 				dpos = dpossav2;
2456 				md = mdsav2;
2457 				dp->d_type =
2458 				    IFTODT(VTTOIF(np->n_vattr.va_type));
2459 				ndp->ni_vp = newvp;
2460 			        cache_enter(ndp->ni_dvp, NCPNULL, ndp->ni_vp, cnp);
2461 			    }
2462 			} else {
2463 			    /* Just skip over the file handle */
2464 			    nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2465 			    i = fxdr_unsigned(int, *tl);
2466 			    nfsm_adv(nfsm_rndup(i));
2467 			}
2468 			if (newvp != NULLVP) {
2469 			    if (newvp == vp)
2470 				vrele(newvp);
2471 			    else
2472 				vput(newvp);
2473 			    newvp = NULLVP;
2474 			}
2475 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2476 			more_dirs = fxdr_unsigned(int, *tl);
2477 		}
2478 		/*
2479 		 * If at end of rpc data, get the eof boolean
2480 		 */
2481 		if (!more_dirs) {
2482 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2483 			more_dirs = (fxdr_unsigned(int, *tl) == 0);
2484 		}
2485 		m_freem(mrep);
2486 	}
2487 	/*
2488 	 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2489 	 * by increasing d_reclen for the last record.
2490 	 */
2491 	if (blksiz > 0) {
2492 		left = DIRBLKSIZ - blksiz;
2493 		dp->d_reclen += left;
2494 		uiop->uio_iov->iov_base += left;
2495 		uiop->uio_iov->iov_len -= left;
2496 		uiop->uio_offset += left;
2497 		uiop->uio_resid -= left;
2498 	}
2499 
2500 	/*
2501 	 * We are now either at the end of the directory or have filled the
2502 	 * block.
2503 	 */
2504 	if (bigenough)
2505 		dnp->n_direofoffset = uiop->uio_offset;
2506 	else {
2507 		if (uiop->uio_resid > 0)
2508 			printf("EEK! readdirplusrpc resid > 0\n");
2509 		cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2510 		*cookiep = cookie;
2511 	}
2512 nfsmout:
2513 	if (newvp != NULLVP) {
2514 	        if (newvp == vp)
2515 			vrele(newvp);
2516 		else
2517 			vput(newvp);
2518 		newvp = NULLVP;
2519 	}
2520 	return (error);
2521 }
2522 
2523 /*
2524  * Silly rename. To make the NFS filesystem that is stateless look a little
2525  * more like the "ufs" a remove of an active vnode is translated to a rename
2526  * to a funny looking filename that is removed by nfs_inactive on the
2527  * nfsnode. There is the potential for another process on a different client
2528  * to create the same funny name between the nfs_lookitup() fails and the
2529  * nfs_rename() completes, but...
2530  */
2531 static int
2532 nfs_sillyrename(dvp, vp, cnp)
2533 	struct vnode *dvp, *vp;
2534 	struct componentname *cnp;
2535 {
2536 	struct sillyrename *sp;
2537 	struct nfsnode *np;
2538 	int error;
2539 
2540 	cache_purge(dvp);
2541 	np = VTONFS(vp);
2542 #ifndef DIAGNOSTIC
2543 	if (vp->v_type == VDIR)
2544 		panic("nfs: sillyrename dir");
2545 #endif
2546 	MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2547 		M_NFSREQ, M_WAITOK);
2548 	sp->s_cred = crdup(cnp->cn_cred);
2549 	sp->s_dvp = dvp;
2550 	VREF(dvp);
2551 
2552 	/* Fudge together a funny name */
2553 	sp->s_namlen = sprintf(sp->s_name, ".nfsA%08x4.4", (int)cnp->cn_td);
2554 
2555 	/* Try lookitups until we get one that isn't there */
2556 	while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2557 		cnp->cn_td, (struct nfsnode **)0) == 0) {
2558 		sp->s_name[4]++;
2559 		if (sp->s_name[4] > 'z') {
2560 			error = EINVAL;
2561 			goto bad;
2562 		}
2563 	}
2564 	error = nfs_renameit(dvp, cnp, sp);
2565 	if (error)
2566 		goto bad;
2567 	error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2568 		cnp->cn_td, &np);
2569 	np->n_sillyrename = sp;
2570 	return (0);
2571 bad:
2572 	vrele(sp->s_dvp);
2573 	crfree(sp->s_cred);
2574 	free((caddr_t)sp, M_NFSREQ);
2575 	return (error);
2576 }
2577 
2578 /*
2579  * Look up a file name and optionally either update the file handle or
2580  * allocate an nfsnode, depending on the value of npp.
2581  * npp == NULL	--> just do the lookup
2582  * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2583  *			handled too
2584  * *npp != NULL --> update the file handle in the vnode
2585  */
2586 static int
2587 nfs_lookitup(dvp, name, len, cred, td, npp)
2588 	struct vnode *dvp;
2589 	const char *name;
2590 	int len;
2591 	struct ucred *cred;
2592 	struct thread *td;
2593 	struct nfsnode **npp;
2594 {
2595 	u_int32_t *tl;
2596 	caddr_t cp;
2597 	int32_t t1, t2;
2598 	struct vnode *newvp = (struct vnode *)0;
2599 	struct nfsnode *np, *dnp = VTONFS(dvp);
2600 	caddr_t bpos, dpos, cp2;
2601 	int error = 0, fhlen, attrflag;
2602 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2603 	nfsfh_t *nfhp;
2604 	int v3 = NFS_ISV3(dvp);
2605 
2606 	nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2607 	nfsm_reqhead(dvp, NFSPROC_LOOKUP,
2608 		NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2609 	nfsm_fhtom(dvp, v3);
2610 	nfsm_strtom(name, len, NFS_MAXNAMLEN);
2611 	nfsm_request(dvp, NFSPROC_LOOKUP, td, cred);
2612 	if (npp && !error) {
2613 		nfsm_getfh(nfhp, fhlen, v3);
2614 		if (*npp) {
2615 		    np = *npp;
2616 		    if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2617 			free((caddr_t)np->n_fhp, M_NFSBIGFH);
2618 			np->n_fhp = &np->n_fh;
2619 		    } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2620 			np->n_fhp =(nfsfh_t *)malloc(fhlen,M_NFSBIGFH,M_WAITOK);
2621 		    bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2622 		    np->n_fhsize = fhlen;
2623 		    newvp = NFSTOV(np);
2624 		} else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2625 		    VREF(dvp);
2626 		    newvp = dvp;
2627 		} else {
2628 		    error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
2629 		    if (error) {
2630 			m_freem(mrep);
2631 			return (error);
2632 		    }
2633 		    newvp = NFSTOV(np);
2634 		}
2635 		if (v3) {
2636 			nfsm_postop_attr(newvp, attrflag);
2637 			if (!attrflag && *npp == NULL) {
2638 				m_freem(mrep);
2639 				if (newvp == dvp)
2640 					vrele(newvp);
2641 				else
2642 					vput(newvp);
2643 				return (ENOENT);
2644 			}
2645 		} else
2646 			nfsm_loadattr(newvp, (struct vattr *)0);
2647 	}
2648 	m_freem(mrep);
2649 nfsmout:
2650 	if (npp && *npp == NULL) {
2651 		if (error) {
2652 			if (newvp) {
2653 				if (newvp == dvp)
2654 					vrele(newvp);
2655 				else
2656 					vput(newvp);
2657 			}
2658 		} else
2659 			*npp = np;
2660 	}
2661 	return (error);
2662 }
2663 
2664 /*
2665  * Nfs Version 3 commit rpc
2666  */
2667 int
2668 nfs_commit(struct vnode *vp, u_quad_t offset, int cnt, struct thread *td)
2669 {
2670 	caddr_t cp;
2671 	u_int32_t *tl;
2672 	int32_t t1, t2;
2673 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2674 	caddr_t bpos, dpos, cp2;
2675 	int error = 0, wccflag = NFSV3_WCCRATTR;
2676 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2677 
2678 	if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
2679 		return (0);
2680 	nfsstats.rpccnt[NFSPROC_COMMIT]++;
2681 	nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1));
2682 	nfsm_fhtom(vp, 1);
2683 	nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2684 	txdr_hyper(offset, tl);
2685 	tl += 2;
2686 	*tl = txdr_unsigned(cnt);
2687 	nfsm_request(vp, NFSPROC_COMMIT, td, nfs_vpcred(vp, ND_WRITE));
2688 	nfsm_wcc_data(vp, wccflag);
2689 	if (!error) {
2690 		nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF);
2691 		if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
2692 			NFSX_V3WRITEVERF)) {
2693 			bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
2694 				NFSX_V3WRITEVERF);
2695 			error = NFSERR_STALEWRITEVERF;
2696 		}
2697 	}
2698 	m_freem(mrep);
2699 nfsmout:
2700 	return (error);
2701 }
2702 
2703 /*
2704  * Kludge City..
2705  * - make nfs_bmap() essentially a no-op that does no translation
2706  * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
2707  *   (Maybe I could use the process's page mapping, but I was concerned that
2708  *    Kernel Write might not be enabled and also figured copyout() would do
2709  *    a lot more work than bcopy() and also it currently happens in the
2710  *    context of the swapper process (2).
2711  */
2712 static int
2713 nfs_bmap(ap)
2714 	struct vop_bmap_args /* {
2715 		struct vnode *a_vp;
2716 		daddr_t  a_bn;
2717 		struct vnode **a_vpp;
2718 		daddr_t *a_bnp;
2719 		int *a_runp;
2720 		int *a_runb;
2721 	} */ *ap;
2722 {
2723 	struct vnode *vp = ap->a_vp;
2724 
2725 	if (ap->a_vpp != NULL)
2726 		*ap->a_vpp = vp;
2727 	if (ap->a_bnp != NULL)
2728 		*ap->a_bnp = ap->a_bn * btodb(vp->v_mount->mnt_stat.f_iosize);
2729 	if (ap->a_runp != NULL)
2730 		*ap->a_runp = 0;
2731 	if (ap->a_runb != NULL)
2732 		*ap->a_runb = 0;
2733 	return (0);
2734 }
2735 
2736 /*
2737  * Strategy routine.
2738  * For async requests when nfsiod(s) are running, queue the request by
2739  * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2740  * request.
2741  */
2742 static int
2743 nfs_strategy(ap)
2744 	struct vop_strategy_args *ap;
2745 {
2746 	struct buf *bp = ap->a_bp;
2747 	struct thread *td;
2748 	int error = 0;
2749 
2750 	KASSERT(!(bp->b_flags & B_DONE), ("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2751 	KASSERT(BUF_REFCNT(bp) > 0, ("nfs_strategy: buffer %p not locked", bp));
2752 
2753 	if (bp->b_flags & B_PHYS)
2754 		panic("nfs physio");
2755 
2756 	if (bp->b_flags & B_ASYNC)
2757 		td = NULL;
2758 	else
2759 		td = curthread;	/* XXX */
2760 
2761 	/*
2762 	 * If the op is asynchronous and an i/o daemon is waiting
2763 	 * queue the request, wake it up and wait for completion
2764 	 * otherwise just do it ourselves.
2765 	 */
2766 	if ((bp->b_flags & B_ASYNC) == 0 ||
2767 		nfs_asyncio(bp, td))
2768 		error = nfs_doio(bp, td);
2769 	return (error);
2770 }
2771 
2772 /*
2773  * Mmap a file
2774  *
2775  * NB Currently unsupported.
2776  */
2777 /* ARGSUSED */
2778 static int
2779 nfs_mmap(ap)
2780 	struct vop_mmap_args /* {
2781 		struct vnode *a_vp;
2782 		int  a_fflags;
2783 		struct ucred *a_cred;
2784 		struct thread *a_td;
2785 	} */ *ap;
2786 {
2787 
2788 	return (EINVAL);
2789 }
2790 
2791 /*
2792  * fsync vnode op. Just call nfs_flush() with commit == 1.
2793  */
2794 /* ARGSUSED */
2795 static int
2796 nfs_fsync(ap)
2797 	struct vop_fsync_args /* {
2798 		struct vnodeop_desc *a_desc;
2799 		struct vnode * a_vp;
2800 		struct ucred * a_cred;
2801 		int  a_waitfor;
2802 		struct thread * a_td;
2803 	} */ *ap;
2804 {
2805 
2806 	return (nfs_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1));
2807 }
2808 
2809 /*
2810  * Flush all the blocks associated with a vnode.
2811  * 	Walk through the buffer pool and push any dirty pages
2812  *	associated with the vnode.
2813  */
2814 static int
2815 nfs_flush(vp, waitfor, td, commit)
2816 	struct vnode *vp;
2817 	int waitfor;
2818 	struct thread *td;
2819 	int commit;
2820 {
2821 	struct nfsnode *np = VTONFS(vp);
2822 	struct buf *bp;
2823 	int i;
2824 	struct buf *nbp;
2825 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2826 	int s, error = 0, slptimeo = 0, slpflag = 0, retv, bvecpos;
2827 	int passone = 1;
2828 	u_quad_t off, endoff, toff;
2829 	struct buf **bvec = NULL;
2830 #ifndef NFS_COMMITBVECSIZ
2831 #define NFS_COMMITBVECSIZ	20
2832 #endif
2833 	struct buf *bvec_on_stack[NFS_COMMITBVECSIZ];
2834 	int bvecsize = 0, bveccount;
2835 
2836 	if (nmp->nm_flag & NFSMNT_INT)
2837 		slpflag = PCATCH;
2838 	if (!commit)
2839 		passone = 0;
2840 	/*
2841 	 * A b_flags == (B_DELWRI | B_NEEDCOMMIT) block has been written to the
2842 	 * server, but nas not been committed to stable storage on the server
2843 	 * yet. On the first pass, the byte range is worked out and the commit
2844 	 * rpc is done. On the second pass, nfs_writebp() is called to do the
2845 	 * job.
2846 	 */
2847 again:
2848 	off = (u_quad_t)-1;
2849 	endoff = 0;
2850 	bvecpos = 0;
2851 	if (NFS_ISV3(vp) && commit) {
2852 		s = splbio();
2853 		/*
2854 		 * Count up how many buffers waiting for a commit.
2855 		 */
2856 		bveccount = 0;
2857 		for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2858 			nbp = TAILQ_NEXT(bp, b_vnbufs);
2859 			if (BUF_REFCNT(bp) == 0 &&
2860 			    (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2861 				== (B_DELWRI | B_NEEDCOMMIT))
2862 				bveccount++;
2863 		}
2864 		/*
2865 		 * Allocate space to remember the list of bufs to commit.  It is
2866 		 * important to use M_NOWAIT here to avoid a race with nfs_write.
2867 		 * If we can't get memory (for whatever reason), we will end up
2868 		 * committing the buffers one-by-one in the loop below.
2869 		 */
2870 		if (bvec != NULL && bvec != bvec_on_stack)
2871 			free(bvec, M_TEMP);
2872 		if (bveccount > NFS_COMMITBVECSIZ) {
2873 			bvec = (struct buf **)
2874 				malloc(bveccount * sizeof(struct buf *),
2875 				       M_TEMP, M_NOWAIT);
2876 			if (bvec == NULL) {
2877 				bvec = bvec_on_stack;
2878 				bvecsize = NFS_COMMITBVECSIZ;
2879 			} else
2880 				bvecsize = bveccount;
2881 		} else {
2882 			bvec = bvec_on_stack;
2883 			bvecsize = NFS_COMMITBVECSIZ;
2884 		}
2885 		for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2886 			nbp = TAILQ_NEXT(bp, b_vnbufs);
2887 			if (bvecpos >= bvecsize)
2888 				break;
2889 			if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
2890 			    (B_DELWRI | B_NEEDCOMMIT) ||
2891 			    BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
2892 				continue;
2893 			bremfree(bp);
2894 			/*
2895 			 * NOTE: we are not clearing B_DONE here, so we have
2896 			 * to do it later on in this routine if we intend to
2897 			 * initiate I/O on the bp.
2898 			 *
2899 			 * Note: to avoid loopback deadlocks, we do not
2900 			 * assign b_runningbufspace.
2901 			 */
2902 			bp->b_flags |= B_WRITEINPROG;
2903 			vfs_busy_pages(bp, 1);
2904 
2905 			/*
2906 			 * bp is protected by being locked, but nbp is not
2907 			 * and vfs_busy_pages() may sleep.  We have to
2908 			 * recalculate nbp.
2909 			 */
2910 			nbp = TAILQ_NEXT(bp, b_vnbufs);
2911 
2912 			/*
2913 			 * A list of these buffers is kept so that the
2914 			 * second loop knows which buffers have actually
2915 			 * been committed. This is necessary, since there
2916 			 * may be a race between the commit rpc and new
2917 			 * uncommitted writes on the file.
2918 			 */
2919 			bvec[bvecpos++] = bp;
2920 			toff = ((u_quad_t)bp->b_blkno) * DEV_BSIZE +
2921 				bp->b_dirtyoff;
2922 			if (toff < off)
2923 				off = toff;
2924 			toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
2925 			if (toff > endoff)
2926 				endoff = toff;
2927 		}
2928 		splx(s);
2929 	}
2930 	if (bvecpos > 0) {
2931 		/*
2932 		 * Commit data on the server, as required.  Note that
2933 		 * nfs_commit will use the vnode's cred for the commit.
2934 		 */
2935 		retv = nfs_commit(vp, off, (int)(endoff - off), td);
2936 
2937 		if (retv == NFSERR_STALEWRITEVERF)
2938 			nfs_clearcommit(vp->v_mount);
2939 
2940 		/*
2941 		 * Now, either mark the blocks I/O done or mark the
2942 		 * blocks dirty, depending on whether the commit
2943 		 * succeeded.
2944 		 */
2945 		for (i = 0; i < bvecpos; i++) {
2946 			bp = bvec[i];
2947 			bp->b_flags &= ~(B_NEEDCOMMIT | B_WRITEINPROG | B_CLUSTEROK);
2948 			if (retv) {
2949 				/*
2950 				 * Error, leave B_DELWRI intact
2951 				 */
2952 				vfs_unbusy_pages(bp);
2953 				brelse(bp);
2954 			} else {
2955 				/*
2956 				 * Success, remove B_DELWRI ( bundirty() ).
2957 				 *
2958 				 * b_dirtyoff/b_dirtyend seem to be NFS
2959 				 * specific.  We should probably move that
2960 				 * into bundirty(). XXX
2961 				 */
2962 				s = splbio();
2963 				vp->v_numoutput++;
2964 				bp->b_flags |= B_ASYNC;
2965 				bundirty(bp);
2966 				bp->b_flags &= ~(B_READ|B_DONE|B_ERROR);
2967 				bp->b_dirtyoff = bp->b_dirtyend = 0;
2968 				splx(s);
2969 				biodone(bp);
2970 			}
2971 		}
2972 	}
2973 
2974 	/*
2975 	 * Start/do any write(s) that are required.
2976 	 */
2977 loop:
2978 	s = splbio();
2979 	for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2980 		nbp = TAILQ_NEXT(bp, b_vnbufs);
2981 		if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
2982 			if (waitfor != MNT_WAIT || passone)
2983 				continue;
2984 			error = BUF_TIMELOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL,
2985 			    "nfsfsync", slpflag, slptimeo);
2986 			splx(s);
2987 			if (error == 0)
2988 				panic("nfs_fsync: inconsistent lock");
2989 			if (error == ENOLCK)
2990 				goto loop;
2991 			if (nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
2992 				error = EINTR;
2993 				goto done;
2994 			}
2995 			if (slpflag == PCATCH) {
2996 				slpflag = 0;
2997 				slptimeo = 2 * hz;
2998 			}
2999 			goto loop;
3000 		}
3001 		if ((bp->b_flags & B_DELWRI) == 0)
3002 			panic("nfs_fsync: not dirty");
3003 		if ((passone || !commit) && (bp->b_flags & B_NEEDCOMMIT)) {
3004 			BUF_UNLOCK(bp);
3005 			continue;
3006 		}
3007 		bremfree(bp);
3008 		if (passone || !commit)
3009 		    bp->b_flags |= B_ASYNC;
3010 		else
3011 		    bp->b_flags |= B_ASYNC | B_WRITEINPROG;
3012 		splx(s);
3013 		VOP_BWRITE(bp->b_vp, bp);
3014 		goto loop;
3015 	}
3016 	splx(s);
3017 	if (passone) {
3018 		passone = 0;
3019 		goto again;
3020 	}
3021 	if (waitfor == MNT_WAIT) {
3022 		while (vp->v_numoutput) {
3023 			vp->v_flag |= VBWAIT;
3024 			error = tsleep((caddr_t)&vp->v_numoutput,
3025 				slpflag, "nfsfsync", slptimeo);
3026 			if (error) {
3027 			    if (nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
3028 				error = EINTR;
3029 				goto done;
3030 			    }
3031 			    if (slpflag == PCATCH) {
3032 				slpflag = 0;
3033 				slptimeo = 2 * hz;
3034 			    }
3035 			}
3036 		}
3037 		if (!TAILQ_EMPTY(&vp->v_dirtyblkhd) && commit) {
3038 			goto loop;
3039 		}
3040 	}
3041 	if (np->n_flag & NWRITEERR) {
3042 		error = np->n_error;
3043 		np->n_flag &= ~NWRITEERR;
3044 	}
3045 done:
3046 	if (bvec != NULL && bvec != bvec_on_stack)
3047 		free(bvec, M_TEMP);
3048 	return (error);
3049 }
3050 
3051 /*
3052  * NFS advisory byte-level locks.
3053  * Currently unsupported.
3054  */
3055 static int
3056 nfs_advlock(ap)
3057 	struct vop_advlock_args /* {
3058 		struct vnode *a_vp;
3059 		caddr_t  a_id;
3060 		int  a_op;
3061 		struct flock *a_fl;
3062 		int  a_flags;
3063 	} */ *ap;
3064 {
3065 	struct nfsnode *np = VTONFS(ap->a_vp);
3066 
3067 	/*
3068 	 * The following kludge is to allow diskless support to work
3069 	 * until a real NFS lockd is implemented. Basically, just pretend
3070 	 * that this is a local lock.
3071 	 */
3072 	return (lf_advlock(ap, &(np->n_lockf), np->n_size));
3073 }
3074 
3075 /*
3076  * Print out the contents of an nfsnode.
3077  */
3078 static int
3079 nfs_print(ap)
3080 	struct vop_print_args /* {
3081 		struct vnode *a_vp;
3082 	} */ *ap;
3083 {
3084 	struct vnode *vp = ap->a_vp;
3085 	struct nfsnode *np = VTONFS(vp);
3086 
3087 	printf("tag VT_NFS, fileid %ld fsid 0x%x",
3088 		np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3089 	if (vp->v_type == VFIFO)
3090 		fifo_printinfo(vp);
3091 	printf("\n");
3092 	return (0);
3093 }
3094 
3095 /*
3096  * Just call nfs_writebp() with the force argument set to 1.
3097  *
3098  * NOTE: B_DONE may or may not be set in a_bp on call.
3099  */
3100 static int
3101 nfs_bwrite(ap)
3102 	struct vop_bwrite_args /* {
3103 		struct vnode *a_bp;
3104 	} */ *ap;
3105 {
3106 	return (nfs_writebp(ap->a_bp, 1, curthread));
3107 }
3108 
3109 /*
3110  * This is a clone of vn_bwrite(), except that B_WRITEINPROG isn't set unless
3111  * the force flag is one and it also handles the B_NEEDCOMMIT flag.  We set
3112  * B_CACHE if this is a VMIO buffer.
3113  */
3114 int
3115 nfs_writebp(bp, force, td)
3116 	struct buf *bp;
3117 	int force;
3118 	struct thread *td;
3119 {
3120 	int s;
3121 	int oldflags = bp->b_flags;
3122 #if 0
3123 	int retv = 1;
3124 	off_t off;
3125 #endif
3126 
3127 	if (BUF_REFCNT(bp) == 0)
3128 		panic("bwrite: buffer is not locked???");
3129 
3130 	if (bp->b_flags & B_INVAL) {
3131 		brelse(bp);
3132 		return(0);
3133 	}
3134 
3135 	bp->b_flags |= B_CACHE;
3136 
3137 	/*
3138 	 * Undirty the bp.  We will redirty it later if the I/O fails.
3139 	 */
3140 
3141 	s = splbio();
3142 	bundirty(bp);
3143 	bp->b_flags &= ~(B_READ|B_DONE|B_ERROR);
3144 
3145 	bp->b_vp->v_numoutput++;
3146 	splx(s);
3147 
3148 	/*
3149 	 * Note: to avoid loopback deadlocks, we do not
3150 	 * assign b_runningbufspace.
3151 	 */
3152 	vfs_busy_pages(bp, 1);
3153 
3154 	if (force)
3155 		bp->b_flags |= B_WRITEINPROG;
3156 	BUF_KERNPROC(bp);
3157 	VOP_STRATEGY(bp->b_vp, bp);
3158 
3159 	if( (oldflags & B_ASYNC) == 0) {
3160 		int rtval = biowait(bp);
3161 
3162 		if (oldflags & B_DELWRI) {
3163 			s = splbio();
3164 			reassignbuf(bp, bp->b_vp);
3165 			splx(s);
3166 		}
3167 
3168 		brelse(bp);
3169 		return (rtval);
3170 	}
3171 
3172 	return (0);
3173 }
3174 
3175 /*
3176  * nfs special file access vnode op.
3177  * Essentially just get vattr and then imitate iaccess() since the device is
3178  * local to the client.
3179  */
3180 static int
3181 nfsspec_access(ap)
3182 	struct vop_access_args /* {
3183 		struct vnode *a_vp;
3184 		int  a_mode;
3185 		struct ucred *a_cred;
3186 		struct thread *a_td;
3187 	} */ *ap;
3188 {
3189 	struct vattr *vap;
3190 	gid_t *gp;
3191 	struct ucred *cred = ap->a_cred;
3192 	struct vnode *vp = ap->a_vp;
3193 	mode_t mode = ap->a_mode;
3194 	struct vattr vattr;
3195 	int i;
3196 	int error;
3197 
3198 	/*
3199 	 * Disallow write attempts on filesystems mounted read-only;
3200 	 * unless the file is a socket, fifo, or a block or character
3201 	 * device resident on the filesystem.
3202 	 */
3203 	if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3204 		switch (vp->v_type) {
3205 		case VREG:
3206 		case VDIR:
3207 		case VLNK:
3208 			return (EROFS);
3209 		default:
3210 			break;
3211 		}
3212 	}
3213 	/*
3214 	 * If you're the super-user,
3215 	 * you always get access.
3216 	 */
3217 	if (cred->cr_uid == 0)
3218 		return (0);
3219 	vap = &vattr;
3220 	error = VOP_GETATTR(vp, vap, ap->a_td);
3221 	if (error)
3222 		return (error);
3223 	/*
3224 	 * Access check is based on only one of owner, group, public.
3225 	 * If not owner, then check group. If not a member of the
3226 	 * group, then check public access.
3227 	 */
3228 	if (cred->cr_uid != vap->va_uid) {
3229 		mode >>= 3;
3230 		gp = cred->cr_groups;
3231 		for (i = 0; i < cred->cr_ngroups; i++, gp++)
3232 			if (vap->va_gid == *gp)
3233 				goto found;
3234 		mode >>= 3;
3235 found:
3236 		;
3237 	}
3238 	error = (vap->va_mode & mode) == mode ? 0 : EACCES;
3239 	return (error);
3240 }
3241 
3242 /*
3243  * Read wrapper for special devices.
3244  */
3245 static int
3246 nfsspec_read(ap)
3247 	struct vop_read_args /* {
3248 		struct vnode *a_vp;
3249 		struct uio *a_uio;
3250 		int  a_ioflag;
3251 		struct ucred *a_cred;
3252 	} */ *ap;
3253 {
3254 	struct nfsnode *np = VTONFS(ap->a_vp);
3255 
3256 	/*
3257 	 * Set access flag.
3258 	 */
3259 	np->n_flag |= NACC;
3260 	getnanotime(&np->n_atim);
3261 	return (VOCALL(spec_vnodeop_p, VOFFSET(vop_read), ap));
3262 }
3263 
3264 /*
3265  * Write wrapper for special devices.
3266  */
3267 static int
3268 nfsspec_write(ap)
3269 	struct vop_write_args /* {
3270 		struct vnode *a_vp;
3271 		struct uio *a_uio;
3272 		int  a_ioflag;
3273 		struct ucred *a_cred;
3274 	} */ *ap;
3275 {
3276 	struct nfsnode *np = VTONFS(ap->a_vp);
3277 
3278 	/*
3279 	 * Set update flag.
3280 	 */
3281 	np->n_flag |= NUPD;
3282 	getnanotime(&np->n_mtim);
3283 	return (VOCALL(spec_vnodeop_p, VOFFSET(vop_write), ap));
3284 }
3285 
3286 /*
3287  * Close wrapper for special devices.
3288  *
3289  * Update the times on the nfsnode then do device close.
3290  */
3291 static int
3292 nfsspec_close(ap)
3293 	struct vop_close_args /* {
3294 		struct vnode *a_vp;
3295 		int  a_fflag;
3296 		struct ucred *a_cred;
3297 		struct thread *a_td;
3298 	} */ *ap;
3299 {
3300 	struct vnode *vp = ap->a_vp;
3301 	struct nfsnode *np = VTONFS(vp);
3302 	struct vattr vattr;
3303 
3304 	if (np->n_flag & (NACC | NUPD)) {
3305 		np->n_flag |= NCHG;
3306 		if (vp->v_usecount == 1 &&
3307 		    (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3308 			VATTR_NULL(&vattr);
3309 			if (np->n_flag & NACC)
3310 				vattr.va_atime = np->n_atim;
3311 			if (np->n_flag & NUPD)
3312 				vattr.va_mtime = np->n_mtim;
3313 			(void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE), ap->a_td);
3314 		}
3315 	}
3316 	return (VOCALL(spec_vnodeop_p, VOFFSET(vop_close), ap));
3317 }
3318 
3319 /*
3320  * Read wrapper for fifos.
3321  */
3322 static int
3323 nfsfifo_read(ap)
3324 	struct vop_read_args /* {
3325 		struct vnode *a_vp;
3326 		struct uio *a_uio;
3327 		int  a_ioflag;
3328 		struct ucred *a_cred;
3329 	} */ *ap;
3330 {
3331 	struct nfsnode *np = VTONFS(ap->a_vp);
3332 
3333 	/*
3334 	 * Set access flag.
3335 	 */
3336 	np->n_flag |= NACC;
3337 	getnanotime(&np->n_atim);
3338 	return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_read), ap));
3339 }
3340 
3341 /*
3342  * Write wrapper for fifos.
3343  */
3344 static int
3345 nfsfifo_write(ap)
3346 	struct vop_write_args /* {
3347 		struct vnode *a_vp;
3348 		struct uio *a_uio;
3349 		int  a_ioflag;
3350 		struct ucred *a_cred;
3351 	} */ *ap;
3352 {
3353 	struct nfsnode *np = VTONFS(ap->a_vp);
3354 
3355 	/*
3356 	 * Set update flag.
3357 	 */
3358 	np->n_flag |= NUPD;
3359 	getnanotime(&np->n_mtim);
3360 	return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_write), ap));
3361 }
3362 
3363 /*
3364  * Close wrapper for fifos.
3365  *
3366  * Update the times on the nfsnode then do fifo close.
3367  */
3368 static int
3369 nfsfifo_close(ap)
3370 	struct vop_close_args /* {
3371 		struct vnode *a_vp;
3372 		int  a_fflag;
3373 		struct thread *a_td;
3374 	} */ *ap;
3375 {
3376 	struct vnode *vp = ap->a_vp;
3377 	struct nfsnode *np = VTONFS(vp);
3378 	struct vattr vattr;
3379 	struct timespec ts;
3380 
3381 	if (np->n_flag & (NACC | NUPD)) {
3382 		getnanotime(&ts);
3383 		if (np->n_flag & NACC)
3384 			np->n_atim = ts;
3385 		if (np->n_flag & NUPD)
3386 			np->n_mtim = ts;
3387 		np->n_flag |= NCHG;
3388 		if (vp->v_usecount == 1 &&
3389 		    (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3390 			VATTR_NULL(&vattr);
3391 			if (np->n_flag & NACC)
3392 				vattr.va_atime = np->n_atim;
3393 			if (np->n_flag & NUPD)
3394 				vattr.va_mtime = np->n_mtim;
3395 			(void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE), ap->a_td);
3396 		}
3397 	}
3398 	return (VOCALL(fifo_vnodeop_p, VOFFSET(vop_close), ap));
3399 }
3400 
3401