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