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