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