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