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