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