xref: /dragonfly/sys/vfs/nfs/nfs_vnops.c (revision 375d1659)
1 /*
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Rick Macklem at The University of Guelph.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)nfs_vnops.c	8.16 (Berkeley) 5/27/95
37  * $FreeBSD: src/sys/nfs/nfs_vnops.c,v 1.150.2.5 2001/12/20 19:56:28 dillon Exp $
38  * $DragonFly: src/sys/vfs/nfs/nfs_vnops.c,v 1.49 2006/03/05 18:38:37 dillon Exp $
39  */
40 
41 
42 /*
43  * vnode op calls for Sun NFS version 2 and 3
44  */
45 
46 #include "opt_inet.h"
47 
48 #include <sys/param.h>
49 #include <sys/kernel.h>
50 #include <sys/systm.h>
51 #include <sys/resourcevar.h>
52 #include <sys/proc.h>
53 #include <sys/mount.h>
54 #include <sys/buf.h>
55 #include <sys/malloc.h>
56 #include <sys/mbuf.h>
57 #include <sys/namei.h>
58 #include <sys/nlookup.h>
59 #include <sys/socket.h>
60 #include <sys/vnode.h>
61 #include <sys/dirent.h>
62 #include <sys/fcntl.h>
63 #include <sys/lockf.h>
64 #include <sys/stat.h>
65 #include <sys/sysctl.h>
66 #include <sys/conf.h>
67 
68 #include <vm/vm.h>
69 #include <vm/vm_extern.h>
70 #include <vm/vm_zone.h>
71 
72 #include <sys/buf2.h>
73 
74 #include <vfs/fifofs/fifo.h>
75 #include <vfs/ufs/dir.h>
76 
77 #undef DIRBLKSIZ
78 
79 #include "rpcv2.h"
80 #include "nfsproto.h"
81 #include "nfs.h"
82 #include "nfsmount.h"
83 #include "nfsnode.h"
84 #include "xdr_subs.h"
85 #include "nfsm_subs.h"
86 #include "nqnfs.h"
87 
88 #include <net/if.h>
89 #include <netinet/in.h>
90 #include <netinet/in_var.h>
91 
92 #include <sys/thread2.h>
93 
94 /* Defs */
95 #define	TRUE	1
96 #define	FALSE	0
97 
98 /*
99  * Ifdef for FreeBSD-current merged buffer cache. It is unfortunate that these
100  * calls are not in getblk() and brelse() so that they would not be necessary
101  * here.
102  */
103 #ifndef B_VMIO
104 #define vfs_busy_pages(bp, f)
105 #endif
106 
107 static int	nfsspec_read (struct vop_read_args *);
108 static int	nfsspec_write (struct vop_write_args *);
109 static int	nfsfifo_read (struct vop_read_args *);
110 static int	nfsfifo_write (struct vop_write_args *);
111 static int	nfsspec_close (struct vop_close_args *);
112 static int	nfsfifo_close (struct vop_close_args *);
113 #define nfs_poll vop_nopoll
114 static int	nfs_setattrrpc (struct vnode *,struct vattr *,struct ucred *,struct thread *);
115 static	int	nfs_lookup (struct vop_old_lookup_args *);
116 static	int	nfs_create (struct vop_old_create_args *);
117 static	int	nfs_mknod (struct vop_old_mknod_args *);
118 static	int	nfs_open (struct vop_open_args *);
119 static	int	nfs_close (struct vop_close_args *);
120 static	int	nfs_access (struct vop_access_args *);
121 static	int	nfs_getattr (struct vop_getattr_args *);
122 static	int	nfs_setattr (struct vop_setattr_args *);
123 static	int	nfs_read (struct vop_read_args *);
124 static	int	nfs_mmap (struct vop_mmap_args *);
125 static	int	nfs_fsync (struct vop_fsync_args *);
126 static	int	nfs_remove (struct vop_old_remove_args *);
127 static	int	nfs_link (struct vop_old_link_args *);
128 static	int	nfs_rename (struct vop_old_rename_args *);
129 static	int	nfs_mkdir (struct vop_old_mkdir_args *);
130 static	int	nfs_rmdir (struct vop_old_rmdir_args *);
131 static	int	nfs_symlink (struct vop_old_symlink_args *);
132 static	int	nfs_readdir (struct vop_readdir_args *);
133 static	int	nfs_bmap (struct vop_bmap_args *);
134 static	int	nfs_strategy (struct vop_strategy_args *);
135 static	int	nfs_lookitup (struct vnode *, const char *, int,
136 			struct ucred *, struct thread *, struct nfsnode **);
137 static	int	nfs_sillyrename (struct vnode *,struct vnode *,struct componentname *);
138 static int	nfsspec_access (struct vop_access_args *);
139 static int	nfs_readlink (struct vop_readlink_args *);
140 static int	nfs_print (struct vop_print_args *);
141 static int	nfs_advlock (struct vop_advlock_args *);
142 static int	nfs_bwrite (struct vop_bwrite_args *);
143 
144 static	int	nfs_nresolve (struct vop_nresolve_args *);
145 /*
146  * Global vfs data structures for nfs
147  */
148 struct vnodeopv_entry_desc nfsv2_vnodeop_entries[] = {
149 	{ &vop_default_desc,		vop_defaultop },
150 	{ &vop_access_desc,		(vnodeopv_entry_t) nfs_access },
151 	{ &vop_advlock_desc,		(vnodeopv_entry_t) nfs_advlock },
152 	{ &vop_bmap_desc,		(vnodeopv_entry_t) nfs_bmap },
153 	{ &vop_bwrite_desc,		(vnodeopv_entry_t) nfs_bwrite },
154 	{ &vop_close_desc,		(vnodeopv_entry_t) nfs_close },
155 	{ &vop_old_create_desc,		(vnodeopv_entry_t) nfs_create },
156 	{ &vop_fsync_desc,		(vnodeopv_entry_t) nfs_fsync },
157 	{ &vop_getattr_desc,		(vnodeopv_entry_t) nfs_getattr },
158 	{ &vop_getpages_desc,		(vnodeopv_entry_t) nfs_getpages },
159 	{ &vop_putpages_desc,		(vnodeopv_entry_t) nfs_putpages },
160 	{ &vop_inactive_desc,		(vnodeopv_entry_t) nfs_inactive },
161 	{ &vop_islocked_desc,		(vnodeopv_entry_t) vop_stdislocked },
162 	{ &vop_lease_desc,		vop_null },
163 	{ &vop_old_link_desc,		(vnodeopv_entry_t) nfs_link },
164 	{ &vop_lock_desc,		(vnodeopv_entry_t) vop_stdlock },
165 	{ &vop_old_lookup_desc,		(vnodeopv_entry_t) nfs_lookup },
166 	{ &vop_old_mkdir_desc,		(vnodeopv_entry_t) nfs_mkdir },
167 	{ &vop_old_mknod_desc,		(vnodeopv_entry_t) nfs_mknod },
168 	{ &vop_mmap_desc,		(vnodeopv_entry_t) nfs_mmap },
169 	{ &vop_open_desc,		(vnodeopv_entry_t) nfs_open },
170 	{ &vop_poll_desc,		(vnodeopv_entry_t) nfs_poll },
171 	{ &vop_print_desc,		(vnodeopv_entry_t) nfs_print },
172 	{ &vop_read_desc,		(vnodeopv_entry_t) nfs_read },
173 	{ &vop_readdir_desc,		(vnodeopv_entry_t) nfs_readdir },
174 	{ &vop_readlink_desc,		(vnodeopv_entry_t) nfs_readlink },
175 	{ &vop_reclaim_desc,		(vnodeopv_entry_t) nfs_reclaim },
176 	{ &vop_old_remove_desc,		(vnodeopv_entry_t) nfs_remove },
177 	{ &vop_old_rename_desc,		(vnodeopv_entry_t) nfs_rename },
178 	{ &vop_old_rmdir_desc,		(vnodeopv_entry_t) nfs_rmdir },
179 	{ &vop_setattr_desc,		(vnodeopv_entry_t) nfs_setattr },
180 	{ &vop_strategy_desc,		(vnodeopv_entry_t) nfs_strategy },
181 	{ &vop_old_symlink_desc,	(vnodeopv_entry_t) nfs_symlink },
182 	{ &vop_unlock_desc,		(vnodeopv_entry_t) vop_stdunlock },
183 	{ &vop_write_desc,		(vnodeopv_entry_t) nfs_write },
184 
185 	{ &vop_nresolve_desc,		(vnodeopv_entry_t) nfs_nresolve },
186 	{ NULL, NULL }
187 };
188 
189 /*
190  * Special device vnode ops
191  */
192 struct vnodeopv_entry_desc nfsv2_specop_entries[] = {
193 	{ &vop_default_desc,		(vnodeopv_entry_t) spec_vnoperate },
194 	{ &vop_access_desc,		(vnodeopv_entry_t) nfsspec_access },
195 	{ &vop_close_desc,		(vnodeopv_entry_t) nfsspec_close },
196 	{ &vop_fsync_desc,		(vnodeopv_entry_t) nfs_fsync },
197 	{ &vop_getattr_desc,		(vnodeopv_entry_t) nfs_getattr },
198 	{ &vop_inactive_desc,		(vnodeopv_entry_t) nfs_inactive },
199 	{ &vop_islocked_desc,		(vnodeopv_entry_t) vop_stdislocked },
200 	{ &vop_lock_desc,		(vnodeopv_entry_t) vop_stdlock },
201 	{ &vop_print_desc,		(vnodeopv_entry_t) nfs_print },
202 	{ &vop_read_desc,		(vnodeopv_entry_t) nfsspec_read },
203 	{ &vop_reclaim_desc,		(vnodeopv_entry_t) nfs_reclaim },
204 	{ &vop_setattr_desc,		(vnodeopv_entry_t) nfs_setattr },
205 	{ &vop_unlock_desc,		(vnodeopv_entry_t) vop_stdunlock },
206 	{ &vop_write_desc,		(vnodeopv_entry_t) nfsspec_write },
207 	{ NULL, NULL }
208 };
209 
210 struct vnodeopv_entry_desc nfsv2_fifoop_entries[] = {
211 	{ &vop_default_desc,		(vnodeopv_entry_t) fifo_vnoperate },
212 	{ &vop_access_desc,		(vnodeopv_entry_t) nfsspec_access },
213 	{ &vop_close_desc,		(vnodeopv_entry_t) nfsfifo_close },
214 	{ &vop_fsync_desc,		(vnodeopv_entry_t) nfs_fsync },
215 	{ &vop_getattr_desc,		(vnodeopv_entry_t) nfs_getattr },
216 	{ &vop_inactive_desc,		(vnodeopv_entry_t) nfs_inactive },
217 	{ &vop_islocked_desc,		(vnodeopv_entry_t) vop_stdislocked },
218 	{ &vop_lock_desc,		(vnodeopv_entry_t) vop_stdlock },
219 	{ &vop_print_desc,		(vnodeopv_entry_t) nfs_print },
220 	{ &vop_read_desc,		(vnodeopv_entry_t) nfsfifo_read },
221 	{ &vop_reclaim_desc,		(vnodeopv_entry_t) nfs_reclaim },
222 	{ &vop_setattr_desc,		(vnodeopv_entry_t) nfs_setattr },
223 	{ &vop_unlock_desc,		(vnodeopv_entry_t) vop_stdunlock },
224 	{ &vop_write_desc,		(vnodeopv_entry_t) nfsfifo_write },
225 	{ NULL, NULL }
226 };
227 
228 static int	nfs_mknodrpc (struct vnode *dvp, struct vnode **vpp,
229 				  struct componentname *cnp,
230 				  struct vattr *vap);
231 static int	nfs_removerpc (struct vnode *dvp, const char *name,
232 				   int namelen,
233 				   struct ucred *cred, struct thread *td);
234 static int	nfs_renamerpc (struct vnode *fdvp, const char *fnameptr,
235 				   int fnamelen, struct vnode *tdvp,
236 				   const char *tnameptr, int tnamelen,
237 				   struct ucred *cred, struct thread *td);
238 static int	nfs_renameit (struct vnode *sdvp,
239 				  struct componentname *scnp,
240 				  struct sillyrename *sp);
241 
242 /*
243  * Global variables
244  */
245 extern u_int32_t nfs_true, nfs_false;
246 extern u_int32_t nfs_xdrneg1;
247 extern struct nfsstats nfsstats;
248 extern nfstype nfsv3_type[9];
249 struct thread *nfs_iodwant[NFS_MAXASYNCDAEMON];
250 struct nfsmount *nfs_iodmount[NFS_MAXASYNCDAEMON];
251 int nfs_numasync = 0;
252 
253 SYSCTL_DECL(_vfs_nfs);
254 
255 static int	nfsaccess_cache_timeout = NFS_DEFATTRTIMO;
256 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_timeout, CTLFLAG_RW,
257 	   &nfsaccess_cache_timeout, 0, "NFS ACCESS cache timeout");
258 
259 static int	nfsneg_cache_timeout = NFS_MINATTRTIMO;
260 SYSCTL_INT(_vfs_nfs, OID_AUTO, neg_cache_timeout, CTLFLAG_RW,
261 	   &nfsneg_cache_timeout, 0, "NFS NEGATIVE ACCESS cache timeout");
262 
263 static int	nfsv3_commit_on_close = 0;
264 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfsv3_commit_on_close, CTLFLAG_RW,
265 	   &nfsv3_commit_on_close, 0, "write+commit on close, else only write");
266 #if 0
267 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_hits, CTLFLAG_RD,
268 	   &nfsstats.accesscache_hits, 0, "NFS ACCESS cache hit count");
269 
270 SYSCTL_INT(_vfs_nfs, OID_AUTO, access_cache_misses, CTLFLAG_RD,
271 	   &nfsstats.accesscache_misses, 0, "NFS ACCESS cache miss count");
272 #endif
273 
274 #define	NFSV3ACCESS_ALL (NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY		\
275 			 | NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE	\
276 			 | NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP)
277 static int
278 nfs3_access_otw(struct vnode *vp, int wmode,
279 		struct thread *td, struct ucred *cred)
280 {
281 	const int v3 = 1;
282 	u_int32_t *tl;
283 	int error = 0, attrflag;
284 
285 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
286 	caddr_t bpos, dpos, cp2;
287 	int32_t t1, t2;
288 	caddr_t cp;
289 	u_int32_t rmode;
290 	struct nfsnode *np = VTONFS(vp);
291 
292 	nfsstats.rpccnt[NFSPROC_ACCESS]++;
293 	nfsm_reqhead(vp, NFSPROC_ACCESS, NFSX_FH(v3) + NFSX_UNSIGNED);
294 	nfsm_fhtom(vp, v3);
295 	nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
296 	*tl = txdr_unsigned(wmode);
297 	nfsm_request(vp, NFSPROC_ACCESS, td, cred);
298 	nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
299 	if (!error) {
300 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
301 		rmode = fxdr_unsigned(u_int32_t, *tl);
302 		np->n_mode = rmode;
303 		np->n_modeuid = cred->cr_uid;
304 		np->n_modestamp = mycpu->gd_time_seconds;
305 	}
306 	m_freem(mrep);
307 nfsmout:
308 	return error;
309 }
310 
311 /*
312  * nfs access vnode op.
313  * For nfs version 2, just return ok. File accesses may fail later.
314  * For nfs version 3, use the access rpc to check accessibility. If file modes
315  * are changed on the server, accesses might still fail later.
316  *
317  * nfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
318  *	      struct thread *a_td)
319  */
320 static int
321 nfs_access(struct vop_access_args *ap)
322 {
323 	struct vnode *vp = ap->a_vp;
324 	int error = 0;
325 	u_int32_t mode, wmode;
326 	int v3 = NFS_ISV3(vp);
327 	struct nfsnode *np = VTONFS(vp);
328 
329 	/*
330 	 * Disallow write attempts on filesystems mounted read-only;
331 	 * unless the file is a socket, fifo, or a block or character
332 	 * device resident on the filesystem.
333 	 */
334 	if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
335 		switch (vp->v_type) {
336 		case VREG:
337 		case VDIR:
338 		case VLNK:
339 			return (EROFS);
340 		default:
341 			break;
342 		}
343 	}
344 	/*
345 	 * For nfs v3, check to see if we have done this recently, and if
346 	 * so return our cached result instead of making an ACCESS call.
347 	 * If not, do an access rpc, otherwise you are stuck emulating
348 	 * ufs_access() locally using the vattr. This may not be correct,
349 	 * since the server may apply other access criteria such as
350 	 * client uid-->server uid mapping that we do not know about.
351 	 */
352 	if (v3) {
353 		if (ap->a_mode & VREAD)
354 			mode = NFSV3ACCESS_READ;
355 		else
356 			mode = 0;
357 		if (vp->v_type != VDIR) {
358 			if (ap->a_mode & VWRITE)
359 				mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
360 			if (ap->a_mode & VEXEC)
361 				mode |= NFSV3ACCESS_EXECUTE;
362 		} else {
363 			if (ap->a_mode & VWRITE)
364 				mode |= (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
365 					 NFSV3ACCESS_DELETE);
366 			if (ap->a_mode & VEXEC)
367 				mode |= NFSV3ACCESS_LOOKUP;
368 		}
369 		/* XXX safety belt, only make blanket request if caching */
370 		if (nfsaccess_cache_timeout > 0) {
371 			wmode = NFSV3ACCESS_READ | NFSV3ACCESS_MODIFY |
372 				NFSV3ACCESS_EXTEND | NFSV3ACCESS_EXECUTE |
373 				NFSV3ACCESS_DELETE | NFSV3ACCESS_LOOKUP;
374 		} else {
375 			wmode = mode;
376 		}
377 
378 		/*
379 		 * Does our cached result allow us to give a definite yes to
380 		 * this request?
381 		 */
382 		if (np->n_modestamp &&
383 		   (mycpu->gd_time_seconds < (np->n_modestamp + nfsaccess_cache_timeout)) &&
384 		   (ap->a_cred->cr_uid == np->n_modeuid) &&
385 		   ((np->n_mode & mode) == mode)) {
386 			nfsstats.accesscache_hits++;
387 		} else {
388 			/*
389 			 * Either a no, or a don't know.  Go to the wire.
390 			 */
391 			nfsstats.accesscache_misses++;
392 		        error = nfs3_access_otw(vp, wmode, ap->a_td,ap->a_cred);
393 			if (!error) {
394 				if ((np->n_mode & mode) != mode) {
395 					error = EACCES;
396 				}
397 			}
398 		}
399 	} else {
400 		if ((error = nfsspec_access(ap)) != 0)
401 			return (error);
402 
403 		/*
404 		 * Attempt to prevent a mapped root from accessing a file
405 		 * which it shouldn't.  We try to read a byte from the file
406 		 * if the user is root and the file is not zero length.
407 		 * After calling nfsspec_access, we should have the correct
408 		 * file size cached.
409 		 */
410 		if (ap->a_cred->cr_uid == 0 && (ap->a_mode & VREAD)
411 		    && VTONFS(vp)->n_size > 0) {
412 			struct iovec aiov;
413 			struct uio auio;
414 			char buf[1];
415 
416 			aiov.iov_base = buf;
417 			aiov.iov_len = 1;
418 			auio.uio_iov = &aiov;
419 			auio.uio_iovcnt = 1;
420 			auio.uio_offset = 0;
421 			auio.uio_resid = 1;
422 			auio.uio_segflg = UIO_SYSSPACE;
423 			auio.uio_rw = UIO_READ;
424 			auio.uio_td = ap->a_td;
425 
426 			if (vp->v_type == VREG) {
427 				error = nfs_readrpc(vp, &auio);
428 			} else if (vp->v_type == VDIR) {
429 				char* bp;
430 				bp = malloc(NFS_DIRBLKSIZ, M_TEMP, M_WAITOK);
431 				aiov.iov_base = bp;
432 				aiov.iov_len = auio.uio_resid = NFS_DIRBLKSIZ;
433 				error = nfs_readdirrpc(vp, &auio);
434 				free(bp, M_TEMP);
435 			} else if (vp->v_type == VLNK) {
436 				error = nfs_readlinkrpc(vp, &auio);
437 			} else {
438 				error = EACCES;
439 			}
440 		}
441 	}
442 	/*
443 	 * [re]record creds for reading and/or writing if access
444 	 * was granted.  Assume the NFS server will grant read access
445 	 * for execute requests.
446 	 */
447 	if (error == 0) {
448 		if ((ap->a_mode & (VREAD|VEXEC)) && ap->a_cred != np->n_rucred) {
449 			crhold(ap->a_cred);
450 			if (np->n_rucred)
451 				crfree(np->n_rucred);
452 			np->n_rucred = ap->a_cred;
453 		}
454 		if ((ap->a_mode & VWRITE) && ap->a_cred != np->n_wucred) {
455 			crhold(ap->a_cred);
456 			if (np->n_wucred)
457 				crfree(np->n_wucred);
458 			np->n_wucred = ap->a_cred;
459 		}
460 	}
461 	return(error);
462 }
463 
464 /*
465  * nfs open vnode op
466  * Check to see if the type is ok
467  * and that deletion is not in progress.
468  * For paged in text files, you will need to flush the page cache
469  * if consistency is lost.
470  *
471  * nfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
472  *	    struct thread *a_td)
473  */
474 /* ARGSUSED */
475 static int
476 nfs_open(struct vop_open_args *ap)
477 {
478 	struct vnode *vp = ap->a_vp;
479 	struct nfsnode *np = VTONFS(vp);
480 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
481 	struct vattr vattr;
482 	int error;
483 
484 	if (vp->v_type != VREG && vp->v_type != VDIR && vp->v_type != VLNK) {
485 #ifdef DIAGNOSTIC
486 		printf("open eacces vtyp=%d\n",vp->v_type);
487 #endif
488 		return (EOPNOTSUPP);
489 	}
490 
491 	/*
492 	 * Clear the attribute cache only if opening with write access.  It
493 	 * is unclear if we should do this at all here, but we certainly
494 	 * should not clear the cache unconditionally simply because a file
495 	 * is being opened.
496 	 */
497 	if (ap->a_mode & FWRITE)
498 		np->n_attrstamp = 0;
499 
500 	if (nmp->nm_flag & NFSMNT_NQNFS) {
501 		/*
502 		 * If NQNFS is active, get a valid lease
503 		 */
504 		if (NQNFS_CKINVALID(vp, np, ND_READ)) {
505 		    do {
506 			error = nqnfs_getlease(vp, ND_READ, ap->a_td);
507 		    } while (error == NQNFS_EXPIRED);
508 		    if (error)
509 			return (error);
510 		    if (np->n_lrev != np->n_brev ||
511 			(np->n_flag & NQNFSNONCACHE)) {
512 			if ((error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1))
513 			    == EINTR) {
514 				return (error);
515 			}
516 			np->n_brev = np->n_lrev;
517 		    }
518 		}
519 	} else {
520 		/*
521 		 * For normal NFS, reconcile changes made locally verses
522 		 * changes made remotely.  Note that VOP_GETATTR only goes
523 		 * to the wire if the cached attribute has timed out or been
524 		 * cleared.
525 		 *
526 		 * If local modifications have been made clear the attribute
527 		 * cache to force an attribute and modified time check.  If
528 		 * GETATTR detects that the file has been changed by someone
529 		 * other then us it will set NRMODIFIED.
530 		 *
531 		 * If we are opening a directory and local changes have been
532 		 * made we have to invalidate the cache in order to ensure
533 		 * that we get the most up-to-date information from the
534 		 * server.  XXX
535 		 */
536 		if (np->n_flag & NLMODIFIED) {
537 			np->n_attrstamp = 0;
538 			if (vp->v_type == VDIR) {
539 				error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
540 				if (error == EINTR)
541 					return (error);
542 				nfs_invaldir(vp);
543 			}
544 		}
545 		error = VOP_GETATTR(vp, &vattr, ap->a_td);
546 		if (error)
547 			return (error);
548 		if (np->n_flag & NRMODIFIED) {
549 			if (vp->v_type == VDIR)
550 				nfs_invaldir(vp);
551 			error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
552 			if (error == EINTR)
553 				return (error);
554 			np->n_flag &= ~NRMODIFIED;
555 		}
556 	}
557 
558 	return (0);
559 }
560 
561 /*
562  * nfs close vnode op
563  * What an NFS client should do upon close after writing is a debatable issue.
564  * Most NFS clients push delayed writes to the server upon close, basically for
565  * two reasons:
566  * 1 - So that any write errors may be reported back to the client process
567  *     doing the close system call. By far the two most likely errors are
568  *     NFSERR_NOSPC and NFSERR_DQUOT to indicate space allocation failure.
569  * 2 - To put a worst case upper bound on cache inconsistency between
570  *     multiple clients for the file.
571  * There is also a consistency problem for Version 2 of the protocol w.r.t.
572  * not being able to tell if other clients are writing a file concurrently,
573  * since there is no way of knowing if the changed modify time in the reply
574  * is only due to the write for this client.
575  * (NFS Version 3 provides weak cache consistency data in the reply that
576  *  should be sufficient to detect and handle this case.)
577  *
578  * The current code does the following:
579  * for NFS Version 2 - play it safe and flush/invalidate all dirty buffers
580  * for NFS Version 3 - flush dirty buffers to the server but don't invalidate
581  *                     or commit them (this satisfies 1 and 2 except for the
582  *                     case where the server crashes after this close but
583  *                     before the commit RPC, which is felt to be "good
584  *                     enough". Changing the last argument to nfs_flush() to
585  *                     a 1 would force a commit operation, if it is felt a
586  *                     commit is necessary now.
587  * for NQNFS         - do nothing now, since 2 is dealt with via leases and
588  *                     1 should be dealt with via an fsync() system call for
589  *                     cases where write errors are important.
590  *
591  * nfs_close(struct vnodeop_desc *a_desc, struct vnode *a_vp, int a_fflag,
592  *	     struct ucred *a_cred, struct thread *a_td)
593  */
594 /* ARGSUSED */
595 static int
596 nfs_close(struct vop_close_args *ap)
597 {
598 	struct vnode *vp = ap->a_vp;
599 	struct nfsnode *np = VTONFS(vp);
600 	int error = 0;
601 
602 	if (vp->v_type == VREG) {
603 	    if ((VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) == 0 &&
604 		(np->n_flag & NLMODIFIED)) {
605 		if (NFS_ISV3(vp)) {
606 		    /*
607 		     * Under NFSv3 we have dirty buffers to dispose of.  We
608 		     * must flush them to the NFS server.  We have the option
609 		     * of waiting all the way through the commit rpc or just
610 		     * waiting for the initial write.  The default is to only
611 		     * wait through the initial write so the data is in the
612 		     * server's cache, which is roughly similar to the state
613 		     * a standard disk subsystem leaves the file in on close().
614 		     *
615 		     * We cannot clear the NLMODIFIED bit in np->n_flag due to
616 		     * potential races with other processes, and certainly
617 		     * cannot clear it if we don't commit.
618 		     */
619 		    int cm = nfsv3_commit_on_close ? 1 : 0;
620 		    error = nfs_flush(vp, MNT_WAIT, ap->a_td, cm);
621 		    /* np->n_flag &= ~NLMODIFIED; */
622 		} else {
623 		    error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
624 		}
625 		np->n_attrstamp = 0;
626 	    }
627 	    if (np->n_flag & NWRITEERR) {
628 		np->n_flag &= ~NWRITEERR;
629 		error = np->n_error;
630 	    }
631 	}
632 	return (error);
633 }
634 
635 /*
636  * nfs getattr call from vfs.
637  *
638  * nfs_getattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred,
639  *		struct thread *a_td)
640  */
641 static int
642 nfs_getattr(struct vop_getattr_args *ap)
643 {
644 	struct vnode *vp = ap->a_vp;
645 	struct nfsnode *np = VTONFS(vp);
646 	caddr_t cp;
647 	u_int32_t *tl;
648 	int32_t t1, t2;
649 	caddr_t bpos, dpos;
650 	int error = 0;
651 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
652 	int v3 = NFS_ISV3(vp);
653 
654 	/*
655 	 * Update local times for special files.
656 	 */
657 	if (np->n_flag & (NACC | NUPD))
658 		np->n_flag |= NCHG;
659 	/*
660 	 * First look in the cache.
661 	 */
662 	if (nfs_getattrcache(vp, ap->a_vap) == 0)
663 		return (0);
664 
665 	if (v3 && nfsaccess_cache_timeout > 0) {
666 		nfsstats.accesscache_misses++;
667 		nfs3_access_otw(vp, NFSV3ACCESS_ALL, ap->a_td, nfs_vpcred(vp, ND_CHECK));
668 		if (nfs_getattrcache(vp, ap->a_vap) == 0)
669 			return (0);
670 	}
671 
672 	nfsstats.rpccnt[NFSPROC_GETATTR]++;
673 	nfsm_reqhead(vp, NFSPROC_GETATTR, NFSX_FH(v3));
674 	nfsm_fhtom(vp, v3);
675 	nfsm_request(vp, NFSPROC_GETATTR, ap->a_td, nfs_vpcred(vp, ND_CHECK));
676 	if (!error) {
677 		nfsm_loadattr(vp, ap->a_vap);
678 	}
679 	m_freem(mrep);
680 nfsmout:
681 	return (error);
682 }
683 
684 /*
685  * nfs setattr call.
686  *
687  * nfs_setattr(struct vnodeop_desc *a_desc, struct vnode *a_vp,
688  *		struct vattr *a_vap, struct ucred *a_cred,
689  *		struct thread *a_td)
690  */
691 static int
692 nfs_setattr(struct vop_setattr_args *ap)
693 {
694 	struct vnode *vp = ap->a_vp;
695 	struct nfsnode *np = VTONFS(vp);
696 	struct vattr *vap = ap->a_vap;
697 	int error = 0;
698 	u_quad_t tsize;
699 
700 #ifndef nolint
701 	tsize = (u_quad_t)0;
702 #endif
703 
704 	/*
705 	 * Setting of flags is not supported.
706 	 */
707 	if (vap->va_flags != VNOVAL)
708 		return (EOPNOTSUPP);
709 
710 	/*
711 	 * Disallow write attempts if the filesystem is mounted read-only.
712 	 */
713   	if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
714 	    vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
715 	    vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
716 	    (vp->v_mount->mnt_flag & MNT_RDONLY))
717 		return (EROFS);
718 	if (vap->va_size != VNOVAL) {
719  		switch (vp->v_type) {
720  		case VDIR:
721  			return (EISDIR);
722  		case VCHR:
723  		case VBLK:
724  		case VSOCK:
725  		case VFIFO:
726 			if (vap->va_mtime.tv_sec == VNOVAL &&
727 			    vap->va_atime.tv_sec == VNOVAL &&
728 			    vap->va_mode == (mode_t)VNOVAL &&
729 			    vap->va_uid == (uid_t)VNOVAL &&
730 			    vap->va_gid == (gid_t)VNOVAL)
731 				return (0);
732  			vap->va_size = VNOVAL;
733  			break;
734  		default:
735 			/*
736 			 * Disallow write attempts if the filesystem is
737 			 * mounted read-only.
738 			 */
739 			if (vp->v_mount->mnt_flag & MNT_RDONLY)
740 				return (EROFS);
741 
742 			/*
743 			 * This is nasty.  The RPCs we send to flush pending
744 			 * data often return attribute information which is
745 			 * cached via a callback to nfs_loadattrcache(), which
746 			 * has the effect of changing our notion of the file
747 			 * size.  Due to flushed appends and other operations
748 			 * the file size can be set to virtually anything,
749 			 * including values that do not match either the old
750 			 * or intended file size.
751 			 *
752 			 * When this condition is detected we must loop to
753 			 * try the operation again.  Hopefully no more
754 			 * flushing is required on the loop so it works the
755 			 * second time around.  THIS CASE ALMOST ALWAYS
756 			 * HAPPENS!
757 			 */
758 			tsize = np->n_size;
759 again:
760 			error = nfs_meta_setsize(vp, ap->a_td, vap->va_size);
761 
762  			if (np->n_flag & NLMODIFIED) {
763  			    if (vap->va_size == 0)
764  				error = nfs_vinvalbuf(vp, 0, ap->a_td, 1);
765  			    else
766  				error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1);
767  			}
768 			/*
769 			 * note: this loop case almost always happens at
770 			 * least once per truncation.
771 			 */
772 			if (error == 0 && np->n_size != vap->va_size)
773 				goto again;
774 			np->n_vattr.va_size = vap->va_size;
775 			break;
776 		}
777   	} else if ((vap->va_mtime.tv_sec != VNOVAL ||
778 		vap->va_atime.tv_sec != VNOVAL) && (np->n_flag & NLMODIFIED) &&
779 		vp->v_type == VREG &&
780   		(error = nfs_vinvalbuf(vp, V_SAVE, ap->a_td, 1)) == EINTR
781 	) {
782 		return (error);
783 	}
784 	error = nfs_setattrrpc(vp, vap, ap->a_cred, ap->a_td);
785 
786 	/*
787 	 * Sanity check if a truncation was issued.  This should only occur
788 	 * if multiple processes are racing on the same file.
789 	 */
790 	if (error == 0 && vap->va_size != VNOVAL &&
791 	    np->n_size != vap->va_size) {
792 		printf("NFS ftruncate: server disagrees on the file size: %lld/%lld/%lld\n", tsize, vap->va_size, np->n_size);
793 		goto again;
794 	}
795 	if (error && vap->va_size != VNOVAL) {
796 		np->n_size = np->n_vattr.va_size = tsize;
797 		vnode_pager_setsize(vp, np->n_size);
798 	}
799 	return (error);
800 }
801 
802 /*
803  * Do an nfs setattr rpc.
804  */
805 static int
806 nfs_setattrrpc(struct vnode *vp, struct vattr *vap,
807 	       struct ucred *cred, struct thread *td)
808 {
809 	struct nfsv2_sattr *sp;
810 	struct nfsnode *np = VTONFS(vp);
811 	caddr_t cp;
812 	int32_t t1, t2;
813 	caddr_t bpos, dpos, cp2;
814 	u_int32_t *tl;
815 	int error = 0, wccflag = NFSV3_WCCRATTR;
816 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
817 	int v3 = NFS_ISV3(vp);
818 
819 	nfsstats.rpccnt[NFSPROC_SETATTR]++;
820 	nfsm_reqhead(vp, NFSPROC_SETATTR, NFSX_FH(v3) + NFSX_SATTR(v3));
821 	nfsm_fhtom(vp, v3);
822 	if (v3) {
823 		nfsm_v3attrbuild(vap, TRUE);
824 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
825 		*tl = nfs_false;
826 	} else {
827 		nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
828 		if (vap->va_mode == (mode_t)VNOVAL)
829 			sp->sa_mode = nfs_xdrneg1;
830 		else
831 			sp->sa_mode = vtonfsv2_mode(vp->v_type, vap->va_mode);
832 		if (vap->va_uid == (uid_t)VNOVAL)
833 			sp->sa_uid = nfs_xdrneg1;
834 		else
835 			sp->sa_uid = txdr_unsigned(vap->va_uid);
836 		if (vap->va_gid == (gid_t)VNOVAL)
837 			sp->sa_gid = nfs_xdrneg1;
838 		else
839 			sp->sa_gid = txdr_unsigned(vap->va_gid);
840 		sp->sa_size = txdr_unsigned(vap->va_size);
841 		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
842 		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
843 	}
844 	nfsm_request(vp, NFSPROC_SETATTR, td, cred);
845 	if (v3) {
846 		np->n_modestamp = 0;
847 		nfsm_wcc_data(vp, wccflag);
848 	} else
849 		nfsm_loadattr(vp, (struct vattr *)0);
850 	m_freem(mrep);
851 nfsmout:
852 	return (error);
853 }
854 
855 /*
856  * NEW API CALL - replaces nfs_lookup().  However, we cannot remove
857  * nfs_lookup() until all remaining new api calls are implemented.
858  *
859  * Resolve a namecache entry.  This function is passed a locked ncp and
860  * must call cache_setvp() on it as appropriate to resolve the entry.
861  */
862 static int
863 nfs_nresolve(struct vop_nresolve_args *ap)
864 {
865 	struct thread *td = curthread;
866 	struct namecache *ncp;
867 	struct ucred *cred;
868 	struct nfsnode *np;
869 	struct vnode *dvp;
870 	struct vnode *nvp;
871 	nfsfh_t *fhp;
872 	int attrflag;
873 	int fhsize;
874 	int error;
875 	int len;
876 	int v3;
877 	/******NFSM MACROS********/
878 	struct mbuf *mb, *mrep, *mreq, *mb2, *md;
879 	caddr_t bpos, dpos, cp, cp2;
880 	u_int32_t *tl;
881 	int32_t t1, t2;
882 
883 	cred = ap->a_cred;
884 	ncp = ap->a_ncp;
885 
886 	KKASSERT(ncp->nc_parent && ncp->nc_parent->nc_vp);
887 	dvp = ncp->nc_parent->nc_vp;
888 	if ((error = vget(dvp, LK_SHARED, td)) != 0)
889 		return (error);
890 
891 	nvp = NULL;
892 	v3 = NFS_ISV3(dvp);
893 	nfsstats.lookupcache_misses++;
894 	nfsstats.rpccnt[NFSPROC_LOOKUP]++;
895 	len = ncp->nc_nlen;
896 	nfsm_reqhead(dvp, NFSPROC_LOOKUP,
897 		NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
898 	nfsm_fhtom(dvp, v3);
899 	nfsm_strtom(ncp->nc_name, len, NFS_MAXNAMLEN);
900 	nfsm_request(dvp, NFSPROC_LOOKUP, td, ap->a_cred);
901 	if (error) {
902 		/*
903 		 * Cache negatve lookups to reduce NFS traffic, but use
904 		 * a fast timeout.  Otherwise use a timeout of 1 tick.
905 		 * XXX we should add a namecache flag for no-caching
906 		 * to uncache the negative hit as soon as possible, but
907 		 * we cannot simply destroy the entry because it is used
908 		 * as a placeholder by the caller.
909 		 */
910 		if (error == ENOENT) {
911 			int nticks;
912 
913 			if (nfsneg_cache_timeout)
914 				nticks = nfsneg_cache_timeout * hz;
915 			else
916 				nticks = 1;
917 			cache_setvp(ncp, NULL);
918 			cache_settimeout(ncp, nticks);
919 		}
920 		nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
921 		m_freem(mrep);
922 		goto nfsmout;
923 	}
924 
925 	/*
926 	 * Success, get the file handle, do various checks, and load
927 	 * post-operation data from the reply packet.  Theoretically
928 	 * we should never be looking up "." so, theoretically, we
929 	 * should never get the same file handle as our directory.  But
930 	 * we check anyway. XXX
931 	 *
932 	 * Note that no timeout is set for the positive cache hit.  We
933 	 * assume, theoretically, that ESTALE returns will be dealt with
934 	 * properly to handle NFS races and in anycase we cannot depend
935 	 * on a timeout to deal with NFS open/create/excl issues so instead
936 	 * of a bad hack here the rest of the NFS client code needs to do
937 	 * the right thing.
938 	 */
939 	nfsm_getfh(fhp, fhsize, v3);
940 
941 	np = VTONFS(dvp);
942 	if (NFS_CMPFH(np, fhp, fhsize)) {
943 		vref(dvp);
944 		nvp = dvp;
945 	} else {
946 		error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
947 		if (error) {
948 			m_freem(mrep);
949 			vput(dvp);
950 			return (error);
951 		}
952 		nvp = NFSTOV(np);
953 	}
954 	if (v3) {
955 		nfsm_postop_attr(nvp, attrflag, NFS_LATTR_NOSHRINK);
956 		nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
957 	} else {
958 		nfsm_loadattr(nvp, NULL);
959 	}
960 	cache_setvp(ncp, nvp);
961 	m_freem(mrep);
962 nfsmout:
963 	vput(dvp);
964 	if (nvp) {
965 		if (nvp == dvp)
966 			vrele(nvp);
967 		else
968 			vput(nvp);
969 	}
970 	return (error);
971 }
972 
973 /*
974  * 'cached' nfs directory lookup
975  *
976  * NOTE: cannot be removed until NFS implements all the new n*() API calls.
977  *
978  * nfs_lookup(struct vnodeop_desc *a_desc, struct vnode *a_dvp,
979  *	      struct vnode **a_vpp, struct componentname *a_cnp)
980  */
981 static int
982 nfs_lookup(struct vop_old_lookup_args *ap)
983 {
984 	struct componentname *cnp = ap->a_cnp;
985 	struct vnode *dvp = ap->a_dvp;
986 	struct vnode **vpp = ap->a_vpp;
987 	int flags = cnp->cn_flags;
988 	struct vnode *newvp;
989 	u_int32_t *tl;
990 	caddr_t cp;
991 	int32_t t1, t2;
992 	struct nfsmount *nmp;
993 	caddr_t bpos, dpos, cp2;
994 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
995 	long len;
996 	nfsfh_t *fhp;
997 	struct nfsnode *np;
998 	int lockparent, wantparent, error = 0, attrflag, fhsize;
999 	int v3 = NFS_ISV3(dvp);
1000 	struct thread *td = cnp->cn_td;
1001 
1002 	/*
1003 	 * Read-only mount check and directory check.
1004 	 */
1005 	*vpp = NULLVP;
1006 	if ((dvp->v_mount->mnt_flag & MNT_RDONLY) &&
1007 	    (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME))
1008 		return (EROFS);
1009 
1010 	if (dvp->v_type != VDIR)
1011 		return (ENOTDIR);
1012 
1013 	/*
1014 	 * Look it up in the cache.  Note that ENOENT is only returned if we
1015 	 * previously entered a negative hit (see later on).  The additional
1016 	 * nfsneg_cache_timeout check causes previously cached results to
1017 	 * be instantly ignored if the negative caching is turned off.
1018 	 */
1019 	lockparent = flags & CNP_LOCKPARENT;
1020 	wantparent = flags & (CNP_LOCKPARENT|CNP_WANTPARENT);
1021 	nmp = VFSTONFS(dvp->v_mount);
1022 	np = VTONFS(dvp);
1023 
1024 	/*
1025 	 * Go to the wire.
1026 	 */
1027 	error = 0;
1028 	newvp = NULLVP;
1029 	nfsstats.lookupcache_misses++;
1030 	nfsstats.rpccnt[NFSPROC_LOOKUP]++;
1031 	len = cnp->cn_namelen;
1032 	nfsm_reqhead(dvp, NFSPROC_LOOKUP,
1033 		NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
1034 	nfsm_fhtom(dvp, v3);
1035 	nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
1036 	nfsm_request(dvp, NFSPROC_LOOKUP, cnp->cn_td, cnp->cn_cred);
1037 	if (error) {
1038 		nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
1039 		m_freem(mrep);
1040 		goto nfsmout;
1041 	}
1042 	nfsm_getfh(fhp, fhsize, v3);
1043 
1044 	/*
1045 	 * Handle RENAME case...
1046 	 */
1047 	if (cnp->cn_nameiop == NAMEI_RENAME && wantparent) {
1048 		if (NFS_CMPFH(np, fhp, fhsize)) {
1049 			m_freem(mrep);
1050 			return (EISDIR);
1051 		}
1052 		error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1053 		if (error) {
1054 			m_freem(mrep);
1055 			return (error);
1056 		}
1057 		newvp = NFSTOV(np);
1058 		if (v3) {
1059 			nfsm_postop_attr(newvp, attrflag, NFS_LATTR_NOSHRINK);
1060 			nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
1061 		} else
1062 			nfsm_loadattr(newvp, (struct vattr *)0);
1063 		*vpp = newvp;
1064 		m_freem(mrep);
1065 		if (!lockparent) {
1066 			VOP_UNLOCK(dvp, 0, td);
1067 			cnp->cn_flags |= CNP_PDIRUNLOCK;
1068 		}
1069 		return (0);
1070 	}
1071 
1072 	if (flags & CNP_ISDOTDOT) {
1073 		VOP_UNLOCK(dvp, 0, td);
1074 		cnp->cn_flags |= CNP_PDIRUNLOCK;
1075 		error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1076 		if (error) {
1077 			vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
1078 			cnp->cn_flags &= ~CNP_PDIRUNLOCK;
1079 			return (error); /* NOTE: return error from nget */
1080 		}
1081 		newvp = NFSTOV(np);
1082 		if (lockparent) {
1083 			error = vn_lock(dvp, LK_EXCLUSIVE, td);
1084 			if (error) {
1085 				vput(newvp);
1086 				return (error);
1087 			}
1088 			cnp->cn_flags |= CNP_PDIRUNLOCK;
1089 		}
1090 	} else if (NFS_CMPFH(np, fhp, fhsize)) {
1091 		vref(dvp);
1092 		newvp = dvp;
1093 	} else {
1094 		error = nfs_nget(dvp->v_mount, fhp, fhsize, &np);
1095 		if (error) {
1096 			m_freem(mrep);
1097 			return (error);
1098 		}
1099 		if (!lockparent) {
1100 			VOP_UNLOCK(dvp, 0, td);
1101 			cnp->cn_flags |= CNP_PDIRUNLOCK;
1102 		}
1103 		newvp = NFSTOV(np);
1104 	}
1105 	if (v3) {
1106 		nfsm_postop_attr(newvp, attrflag, NFS_LATTR_NOSHRINK);
1107 		nfsm_postop_attr(dvp, attrflag, NFS_LATTR_NOSHRINK);
1108 	} else
1109 		nfsm_loadattr(newvp, (struct vattr *)0);
1110 #if 0
1111 	/* XXX MOVE TO nfs_nremove() */
1112 	if ((cnp->cn_flags & CNP_MAKEENTRY) &&
1113 	    cnp->cn_nameiop != NAMEI_DELETE) {
1114 		np->n_ctime = np->n_vattr.va_ctime.tv_sec; /* XXX */
1115 	}
1116 #endif
1117 	*vpp = newvp;
1118 	m_freem(mrep);
1119 nfsmout:
1120 	if (error) {
1121 		if (newvp != NULLVP) {
1122 			vrele(newvp);
1123 			*vpp = NULLVP;
1124 		}
1125 		if ((cnp->cn_nameiop == NAMEI_CREATE ||
1126 		     cnp->cn_nameiop == NAMEI_RENAME) &&
1127 		    error == ENOENT) {
1128 			if (!lockparent) {
1129 				VOP_UNLOCK(dvp, 0, td);
1130 				cnp->cn_flags |= CNP_PDIRUNLOCK;
1131 			}
1132 			if (dvp->v_mount->mnt_flag & MNT_RDONLY)
1133 				error = EROFS;
1134 			else
1135 				error = EJUSTRETURN;
1136 		}
1137 	}
1138 	return (error);
1139 }
1140 
1141 /*
1142  * nfs read call.
1143  * Just call nfs_bioread() to do the work.
1144  *
1145  * nfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1146  *	    struct ucred *a_cred)
1147  */
1148 static int
1149 nfs_read(struct vop_read_args *ap)
1150 {
1151 	struct vnode *vp = ap->a_vp;
1152 
1153 	return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag));
1154 	switch (vp->v_type) {
1155 	case VREG:
1156 		return (nfs_bioread(vp, ap->a_uio, ap->a_ioflag));
1157 	case VDIR:
1158 		return (EISDIR);
1159 	default:
1160 		return EOPNOTSUPP;
1161 	}
1162 }
1163 
1164 /*
1165  * nfs readlink call
1166  *
1167  * nfs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1168  */
1169 static int
1170 nfs_readlink(struct vop_readlink_args *ap)
1171 {
1172 	struct vnode *vp = ap->a_vp;
1173 
1174 	if (vp->v_type != VLNK)
1175 		return (EINVAL);
1176 	return (nfs_bioread(vp, ap->a_uio, 0));
1177 }
1178 
1179 /*
1180  * Do a readlink rpc.
1181  * Called by nfs_doio() from below the buffer cache.
1182  */
1183 int
1184 nfs_readlinkrpc(struct vnode *vp, struct uio *uiop)
1185 {
1186 	u_int32_t *tl;
1187 	caddr_t cp;
1188 	int32_t t1, t2;
1189 	caddr_t bpos, dpos, cp2;
1190 	int error = 0, len, attrflag;
1191 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1192 	int v3 = NFS_ISV3(vp);
1193 
1194 	nfsstats.rpccnt[NFSPROC_READLINK]++;
1195 	nfsm_reqhead(vp, NFSPROC_READLINK, NFSX_FH(v3));
1196 	nfsm_fhtom(vp, v3);
1197 	nfsm_request(vp, NFSPROC_READLINK, uiop->uio_td, nfs_vpcred(vp, ND_CHECK));
1198 	if (v3)
1199 		nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
1200 	if (!error) {
1201 		nfsm_strsiz(len, NFS_MAXPATHLEN);
1202 		if (len == NFS_MAXPATHLEN) {
1203 			struct nfsnode *np = VTONFS(vp);
1204 			if (np->n_size && np->n_size < NFS_MAXPATHLEN)
1205 				len = np->n_size;
1206 		}
1207 		nfsm_mtouio(uiop, len);
1208 	}
1209 	m_freem(mrep);
1210 nfsmout:
1211 	return (error);
1212 }
1213 
1214 /*
1215  * nfs read rpc call
1216  * Ditto above
1217  */
1218 int
1219 nfs_readrpc(struct vnode *vp, struct uio *uiop)
1220 {
1221 	u_int32_t *tl;
1222 	caddr_t cp;
1223 	int32_t t1, t2;
1224 	caddr_t bpos, dpos, cp2;
1225 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1226 	struct nfsmount *nmp;
1227 	int error = 0, len, retlen, tsiz, eof, attrflag;
1228 	int v3 = NFS_ISV3(vp);
1229 
1230 #ifndef nolint
1231 	eof = 0;
1232 #endif
1233 	nmp = VFSTONFS(vp->v_mount);
1234 	tsiz = uiop->uio_resid;
1235 	if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1236 		return (EFBIG);
1237 	while (tsiz > 0) {
1238 		nfsstats.rpccnt[NFSPROC_READ]++;
1239 		len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz;
1240 		nfsm_reqhead(vp, NFSPROC_READ, NFSX_FH(v3) + NFSX_UNSIGNED * 3);
1241 		nfsm_fhtom(vp, v3);
1242 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED * 3);
1243 		if (v3) {
1244 			txdr_hyper(uiop->uio_offset, tl);
1245 			*(tl + 2) = txdr_unsigned(len);
1246 		} else {
1247 			*tl++ = txdr_unsigned(uiop->uio_offset);
1248 			*tl++ = txdr_unsigned(len);
1249 			*tl = 0;
1250 		}
1251 		nfsm_request(vp, NFSPROC_READ, uiop->uio_td, nfs_vpcred(vp, ND_READ));
1252 		if (v3) {
1253 			nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
1254 			if (error) {
1255 				m_freem(mrep);
1256 				goto nfsmout;
1257 			}
1258 			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1259 			eof = fxdr_unsigned(int, *(tl + 1));
1260 		} else
1261 			nfsm_loadattr(vp, (struct vattr *)0);
1262 		nfsm_strsiz(retlen, nmp->nm_rsize);
1263 		nfsm_mtouio(uiop, retlen);
1264 		m_freem(mrep);
1265 		tsiz -= retlen;
1266 		if (v3) {
1267 			if (eof || retlen == 0) {
1268 				tsiz = 0;
1269 			}
1270 		} else if (retlen < len) {
1271 			tsiz = 0;
1272 		}
1273 	}
1274 nfsmout:
1275 	return (error);
1276 }
1277 
1278 /*
1279  * nfs write call
1280  */
1281 int
1282 nfs_writerpc(struct vnode *vp, struct uio *uiop, int *iomode, int *must_commit)
1283 {
1284 	u_int32_t *tl;
1285 	caddr_t cp;
1286 	int32_t t1, t2, backup;
1287 	caddr_t bpos, dpos, cp2;
1288 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1289 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1290 	int error = 0, len, tsiz, wccflag = NFSV3_WCCRATTR, rlen, commit;
1291 	int v3 = NFS_ISV3(vp), committed = NFSV3WRITE_FILESYNC;
1292 
1293 #ifndef DIAGNOSTIC
1294 	if (uiop->uio_iovcnt != 1)
1295 		panic("nfs: writerpc iovcnt > 1");
1296 #endif
1297 	*must_commit = 0;
1298 	tsiz = uiop->uio_resid;
1299 	if (uiop->uio_offset + tsiz > nmp->nm_maxfilesize)
1300 		return (EFBIG);
1301 	while (tsiz > 0) {
1302 		nfsstats.rpccnt[NFSPROC_WRITE]++;
1303 		len = (tsiz > nmp->nm_wsize) ? nmp->nm_wsize : tsiz;
1304 		nfsm_reqhead(vp, NFSPROC_WRITE,
1305 			NFSX_FH(v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1306 		nfsm_fhtom(vp, v3);
1307 		if (v3) {
1308 			nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1309 			txdr_hyper(uiop->uio_offset, tl);
1310 			tl += 2;
1311 			*tl++ = txdr_unsigned(len);
1312 			*tl++ = txdr_unsigned(*iomode);
1313 			*tl = txdr_unsigned(len);
1314 		} else {
1315 			u_int32_t x;
1316 
1317 			nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1318 			/* Set both "begin" and "current" to non-garbage. */
1319 			x = txdr_unsigned((u_int32_t)uiop->uio_offset);
1320 			*tl++ = x;	/* "begin offset" */
1321 			*tl++ = x;	/* "current offset" */
1322 			x = txdr_unsigned(len);
1323 			*tl++ = x;	/* total to this offset */
1324 			*tl = x;	/* size of this write */
1325 		}
1326 		nfsm_uiotom(uiop, len);
1327 		nfsm_request(vp, NFSPROC_WRITE, uiop->uio_td, nfs_vpcred(vp, ND_WRITE));
1328 		if (v3) {
1329 			/*
1330 			 * The write RPC returns a before and after mtime.  The
1331 			 * nfsm_wcc_data() macro checks the before n_mtime
1332 			 * against the before time and stores the after time
1333 			 * in the nfsnode's cached vattr and n_mtime field.
1334 			 * The NRMODIFIED bit will be set if the before
1335 			 * time did not match the original mtime.
1336 			 */
1337 			wccflag = NFSV3_WCCCHK;
1338 			nfsm_wcc_data(vp, wccflag);
1339 			if (!error) {
1340 				nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED
1341 					+ NFSX_V3WRITEVERF);
1342 				rlen = fxdr_unsigned(int, *tl++);
1343 				if (rlen == 0) {
1344 					error = NFSERR_IO;
1345 					m_freem(mrep);
1346 					break;
1347 				} else if (rlen < len) {
1348 					backup = len - rlen;
1349 					uiop->uio_iov->iov_base -= backup;
1350 					uiop->uio_iov->iov_len += backup;
1351 					uiop->uio_offset -= backup;
1352 					uiop->uio_resid += backup;
1353 					len = rlen;
1354 				}
1355 				commit = fxdr_unsigned(int, *tl++);
1356 
1357 				/*
1358 				 * Return the lowest committment level
1359 				 * obtained by any of the RPCs.
1360 				 */
1361 				if (committed == NFSV3WRITE_FILESYNC)
1362 					committed = commit;
1363 				else if (committed == NFSV3WRITE_DATASYNC &&
1364 					commit == NFSV3WRITE_UNSTABLE)
1365 					committed = commit;
1366 				if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1367 				    bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1368 					NFSX_V3WRITEVERF);
1369 				    nmp->nm_state |= NFSSTA_HASWRITEVERF;
1370 				} else if (bcmp((caddr_t)tl,
1371 				    (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF)) {
1372 				    *must_commit = 1;
1373 				    bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
1374 					NFSX_V3WRITEVERF);
1375 				}
1376 			}
1377 		} else {
1378 			nfsm_loadattr(vp, (struct vattr *)0);
1379 		}
1380 		m_freem(mrep);
1381 		if (error)
1382 			break;
1383 		tsiz -= len;
1384 	}
1385 nfsmout:
1386 	if (vp->v_mount->mnt_flag & MNT_ASYNC)
1387 		committed = NFSV3WRITE_FILESYNC;
1388 	*iomode = committed;
1389 	if (error)
1390 		uiop->uio_resid = tsiz;
1391 	return (error);
1392 }
1393 
1394 /*
1395  * nfs mknod rpc
1396  * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the
1397  * mode set to specify the file type and the size field for rdev.
1398  */
1399 static int
1400 nfs_mknodrpc(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1401 	     struct vattr *vap)
1402 {
1403 	struct nfsv2_sattr *sp;
1404 	u_int32_t *tl;
1405 	caddr_t cp;
1406 	int32_t t1, t2;
1407 	struct vnode *newvp = (struct vnode *)0;
1408 	struct nfsnode *np = (struct nfsnode *)0;
1409 	struct vattr vattr;
1410 	char *cp2;
1411 	caddr_t bpos, dpos;
1412 	int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0;
1413 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1414 	u_int32_t rdev;
1415 	int v3 = NFS_ISV3(dvp);
1416 
1417 	if (vap->va_type == VCHR || vap->va_type == VBLK)
1418 		rdev = txdr_unsigned(vap->va_rdev);
1419 	else if (vap->va_type == VFIFO || vap->va_type == VSOCK)
1420 		rdev = nfs_xdrneg1;
1421 	else {
1422 		return (EOPNOTSUPP);
1423 	}
1424 	if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
1425 		return (error);
1426 	}
1427 	nfsstats.rpccnt[NFSPROC_MKNOD]++;
1428 	nfsm_reqhead(dvp, NFSPROC_MKNOD, NFSX_FH(v3) + 4 * NFSX_UNSIGNED +
1429 		+ nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1430 	nfsm_fhtom(dvp, v3);
1431 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1432 	if (v3) {
1433 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1434 		*tl++ = vtonfsv3_type(vap->va_type);
1435 		nfsm_v3attrbuild(vap, FALSE);
1436 		if (vap->va_type == VCHR || vap->va_type == VBLK) {
1437 			nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1438 			*tl++ = txdr_unsigned(umajor(vap->va_rdev));
1439 			*tl = txdr_unsigned(uminor(vap->va_rdev));
1440 		}
1441 	} else {
1442 		nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1443 		sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1444 		sp->sa_uid = nfs_xdrneg1;
1445 		sp->sa_gid = nfs_xdrneg1;
1446 		sp->sa_size = rdev;
1447 		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1448 		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1449 	}
1450 	nfsm_request(dvp, NFSPROC_MKNOD, cnp->cn_td, cnp->cn_cred);
1451 	if (!error) {
1452 		nfsm_mtofh(dvp, newvp, v3, gotvp);
1453 		if (!gotvp) {
1454 			if (newvp) {
1455 				vput(newvp);
1456 				newvp = (struct vnode *)0;
1457 			}
1458 			error = nfs_lookitup(dvp, cnp->cn_nameptr,
1459 			    cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1460 			if (!error)
1461 				newvp = NFSTOV(np);
1462 		}
1463 	}
1464 	if (v3)
1465 		nfsm_wcc_data(dvp, wccflag);
1466 	m_freem(mrep);
1467 nfsmout:
1468 	if (error) {
1469 		if (newvp)
1470 			vput(newvp);
1471 	} else {
1472 		*vpp = newvp;
1473 	}
1474 	VTONFS(dvp)->n_flag |= NLMODIFIED;
1475 	if (!wccflag)
1476 		VTONFS(dvp)->n_attrstamp = 0;
1477 	return (error);
1478 }
1479 
1480 /*
1481  * nfs mknod vop
1482  * just call nfs_mknodrpc() to do the work.
1483  *
1484  * nfs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
1485  *	     struct componentname *a_cnp, struct vattr *a_vap)
1486  */
1487 /* ARGSUSED */
1488 static int
1489 nfs_mknod(struct vop_old_mknod_args *ap)
1490 {
1491 	return nfs_mknodrpc(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap);
1492 }
1493 
1494 static u_long create_verf;
1495 /*
1496  * nfs file create call
1497  *
1498  * nfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
1499  *	      struct componentname *a_cnp, struct vattr *a_vap)
1500  */
1501 static int
1502 nfs_create(struct vop_old_create_args *ap)
1503 {
1504 	struct vnode *dvp = ap->a_dvp;
1505 	struct vattr *vap = ap->a_vap;
1506 	struct componentname *cnp = ap->a_cnp;
1507 	struct nfsv2_sattr *sp;
1508 	u_int32_t *tl;
1509 	caddr_t cp;
1510 	int32_t t1, t2;
1511 	struct nfsnode *np = (struct nfsnode *)0;
1512 	struct vnode *newvp = (struct vnode *)0;
1513 	caddr_t bpos, dpos, cp2;
1514 	int error = 0, wccflag = NFSV3_WCCRATTR, gotvp = 0, fmode = 0;
1515 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1516 	struct vattr vattr;
1517 	int v3 = NFS_ISV3(dvp);
1518 
1519 	/*
1520 	 * Oops, not for me..
1521 	 */
1522 	if (vap->va_type == VSOCK)
1523 		return (nfs_mknodrpc(dvp, ap->a_vpp, cnp, vap));
1524 
1525 	if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
1526 		return (error);
1527 	}
1528 	if (vap->va_vaflags & VA_EXCLUSIVE)
1529 		fmode |= O_EXCL;
1530 again:
1531 	nfsstats.rpccnt[NFSPROC_CREATE]++;
1532 	nfsm_reqhead(dvp, NFSPROC_CREATE, NFSX_FH(v3) + 2 * NFSX_UNSIGNED +
1533 		nfsm_rndup(cnp->cn_namelen) + NFSX_SATTR(v3));
1534 	nfsm_fhtom(dvp, v3);
1535 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1536 	if (v3) {
1537 		nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1538 		if (fmode & O_EXCL) {
1539 			*tl = txdr_unsigned(NFSV3CREATE_EXCLUSIVE);
1540 			nfsm_build(tl, u_int32_t *, NFSX_V3CREATEVERF);
1541 #ifdef INET
1542 			if (!TAILQ_EMPTY(&in_ifaddrhead))
1543 				*tl++ = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr.s_addr;
1544 			else
1545 #endif
1546 				*tl++ = create_verf;
1547 			*tl = ++create_verf;
1548 		} else {
1549 			*tl = txdr_unsigned(NFSV3CREATE_UNCHECKED);
1550 			nfsm_v3attrbuild(vap, FALSE);
1551 		}
1552 	} else {
1553 		nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1554 		sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1555 		sp->sa_uid = nfs_xdrneg1;
1556 		sp->sa_gid = nfs_xdrneg1;
1557 		sp->sa_size = 0;
1558 		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1559 		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1560 	}
1561 	nfsm_request(dvp, NFSPROC_CREATE, cnp->cn_td, cnp->cn_cred);
1562 	if (!error) {
1563 		nfsm_mtofh(dvp, newvp, v3, gotvp);
1564 		if (!gotvp) {
1565 			if (newvp) {
1566 				vput(newvp);
1567 				newvp = (struct vnode *)0;
1568 			}
1569 			error = nfs_lookitup(dvp, cnp->cn_nameptr,
1570 			    cnp->cn_namelen, cnp->cn_cred, cnp->cn_td, &np);
1571 			if (!error)
1572 				newvp = NFSTOV(np);
1573 		}
1574 	}
1575 	if (v3)
1576 		nfsm_wcc_data(dvp, wccflag);
1577 	m_freem(mrep);
1578 nfsmout:
1579 	if (error) {
1580 		if (v3 && (fmode & O_EXCL) && error == NFSERR_NOTSUPP) {
1581 			fmode &= ~O_EXCL;
1582 			goto again;
1583 		}
1584 		if (newvp)
1585 			vput(newvp);
1586 	} else if (v3 && (fmode & O_EXCL)) {
1587 		/*
1588 		 * We are normally called with only a partially initialized
1589 		 * VAP.  Since the NFSv3 spec says that server may use the
1590 		 * file attributes to store the verifier, the spec requires
1591 		 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1592 		 * in atime, but we can't really assume that all servers will
1593 		 * so we ensure that our SETATTR sets both atime and mtime.
1594 		 */
1595 		if (vap->va_mtime.tv_sec == VNOVAL)
1596 			vfs_timestamp(&vap->va_mtime);
1597 		if (vap->va_atime.tv_sec == VNOVAL)
1598 			vap->va_atime = vap->va_mtime;
1599 		error = nfs_setattrrpc(newvp, vap, cnp->cn_cred, cnp->cn_td);
1600 	}
1601 	if (!error) {
1602 		/*
1603 		 * The new np may have enough info for access
1604 		 * checks, make sure rucred and wucred are
1605 		 * initialized for read and write rpc's.
1606 		 */
1607 		np = VTONFS(newvp);
1608 		if (np->n_rucred == NULL)
1609 			np->n_rucred = crhold(cnp->cn_cred);
1610 		if (np->n_wucred == NULL)
1611 			np->n_wucred = crhold(cnp->cn_cred);
1612 		*ap->a_vpp = newvp;
1613 	}
1614 	VTONFS(dvp)->n_flag |= NLMODIFIED;
1615 	if (!wccflag)
1616 		VTONFS(dvp)->n_attrstamp = 0;
1617 	return (error);
1618 }
1619 
1620 /*
1621  * nfs file remove call
1622  * To try and make nfs semantics closer to ufs semantics, a file that has
1623  * other processes using the vnode is renamed instead of removed and then
1624  * removed later on the last close.
1625  * - If v_usecount > 1
1626  *	  If a rename is not already in the works
1627  *	     call nfs_sillyrename() to set it up
1628  *     else
1629  *	  do the remove rpc
1630  *
1631  * nfs_remove(struct vnodeop_desc *a_desc, struct vnode *a_dvp,
1632  *	      struct vnode *a_vp, struct componentname *a_cnp)
1633  */
1634 static int
1635 nfs_remove(struct vop_old_remove_args *ap)
1636 {
1637 	struct vnode *vp = ap->a_vp;
1638 	struct vnode *dvp = ap->a_dvp;
1639 	struct componentname *cnp = ap->a_cnp;
1640 	struct nfsnode *np = VTONFS(vp);
1641 	int error = 0;
1642 	struct vattr vattr;
1643 
1644 #ifndef DIAGNOSTIC
1645 	if (vp->v_usecount < 1)
1646 		panic("nfs_remove: bad v_usecount");
1647 #endif
1648 	if (vp->v_type == VDIR)
1649 		error = EPERM;
1650 	else if (vp->v_usecount == 1 || (np->n_sillyrename &&
1651 	    VOP_GETATTR(vp, &vattr, cnp->cn_td) == 0 &&
1652 	    vattr.va_nlink > 1)) {
1653 		/*
1654 		 * throw away biocache buffers, mainly to avoid
1655 		 * unnecessary delayed writes later.
1656 		 */
1657 		error = nfs_vinvalbuf(vp, 0, cnp->cn_td, 1);
1658 		/* Do the rpc */
1659 		if (error != EINTR)
1660 			error = nfs_removerpc(dvp, cnp->cn_nameptr,
1661 				cnp->cn_namelen, cnp->cn_cred, cnp->cn_td);
1662 		/*
1663 		 * Kludge City: If the first reply to the remove rpc is lost..
1664 		 *   the reply to the retransmitted request will be ENOENT
1665 		 *   since the file was in fact removed
1666 		 *   Therefore, we cheat and return success.
1667 		 */
1668 		if (error == ENOENT)
1669 			error = 0;
1670 	} else if (!np->n_sillyrename) {
1671 		error = nfs_sillyrename(dvp, vp, cnp);
1672 	}
1673 	np->n_attrstamp = 0;
1674 	return (error);
1675 }
1676 
1677 /*
1678  * nfs file remove rpc called from nfs_inactive
1679  */
1680 int
1681 nfs_removeit(struct sillyrename *sp)
1682 {
1683 	return (nfs_removerpc(sp->s_dvp, sp->s_name, sp->s_namlen,
1684 		sp->s_cred, NULL));
1685 }
1686 
1687 /*
1688  * Nfs remove rpc, called from nfs_remove() and nfs_removeit().
1689  */
1690 static int
1691 nfs_removerpc(struct vnode *dvp, const char *name, int namelen,
1692 	      struct ucred *cred, struct thread *td)
1693 {
1694 	u_int32_t *tl;
1695 	caddr_t cp;
1696 	int32_t t1, t2;
1697 	caddr_t bpos, dpos, cp2;
1698 	int error = 0, wccflag = NFSV3_WCCRATTR;
1699 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1700 	int v3 = NFS_ISV3(dvp);
1701 
1702 	nfsstats.rpccnt[NFSPROC_REMOVE]++;
1703 	nfsm_reqhead(dvp, NFSPROC_REMOVE,
1704 		NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(namelen));
1705 	nfsm_fhtom(dvp, v3);
1706 	nfsm_strtom(name, namelen, NFS_MAXNAMLEN);
1707 	nfsm_request(dvp, NFSPROC_REMOVE, td, cred);
1708 	if (v3)
1709 		nfsm_wcc_data(dvp, wccflag);
1710 	m_freem(mrep);
1711 nfsmout:
1712 	VTONFS(dvp)->n_flag |= NLMODIFIED;
1713 	if (!wccflag)
1714 		VTONFS(dvp)->n_attrstamp = 0;
1715 	return (error);
1716 }
1717 
1718 /*
1719  * nfs file rename call
1720  *
1721  * nfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
1722  *	      struct componentname *a_fcnp, struct vnode *a_tdvp,
1723  *	      struct vnode *a_tvp, struct componentname *a_tcnp)
1724  */
1725 static int
1726 nfs_rename(struct vop_old_rename_args *ap)
1727 {
1728 	struct vnode *fvp = ap->a_fvp;
1729 	struct vnode *tvp = ap->a_tvp;
1730 	struct vnode *fdvp = ap->a_fdvp;
1731 	struct vnode *tdvp = ap->a_tdvp;
1732 	struct componentname *tcnp = ap->a_tcnp;
1733 	struct componentname *fcnp = ap->a_fcnp;
1734 	int error;
1735 
1736 	/* Check for cross-device rename */
1737 	if ((fvp->v_mount != tdvp->v_mount) ||
1738 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
1739 		error = EXDEV;
1740 		goto out;
1741 	}
1742 
1743 	/*
1744 	 * We have to flush B_DELWRI data prior to renaming
1745 	 * the file.  If we don't, the delayed-write buffers
1746 	 * can be flushed out later after the file has gone stale
1747 	 * under NFSV3.  NFSV2 does not have this problem because
1748 	 * ( as far as I can tell ) it flushes dirty buffers more
1749 	 * often.
1750 	 */
1751 
1752 	VOP_FSYNC(fvp, MNT_WAIT, fcnp->cn_td);
1753 	if (tvp)
1754 	    VOP_FSYNC(tvp, MNT_WAIT, tcnp->cn_td);
1755 
1756 	/*
1757 	 * If the tvp exists and is in use, sillyrename it before doing the
1758 	 * rename of the new file over it.
1759 	 *
1760 	 * XXX Can't sillyrename a directory.
1761 	 *
1762 	 * We do not attempt to do any namecache purges in this old API
1763 	 * routine.  The new API compat functions have access to the actual
1764 	 * namecache structures and will do it for us.
1765 	 */
1766 	if (tvp && tvp->v_usecount > 1 && !VTONFS(tvp)->n_sillyrename &&
1767 		tvp->v_type != VDIR && !nfs_sillyrename(tdvp, tvp, tcnp)) {
1768 		vput(tvp);
1769 		tvp = NULL;
1770 	} else if (tvp) {
1771 		;
1772 	}
1773 
1774 	error = nfs_renamerpc(fdvp, fcnp->cn_nameptr, fcnp->cn_namelen,
1775 		tdvp, tcnp->cn_nameptr, tcnp->cn_namelen, tcnp->cn_cred,
1776 		tcnp->cn_td);
1777 
1778 out:
1779 	if (tdvp == tvp)
1780 		vrele(tdvp);
1781 	else
1782 		vput(tdvp);
1783 	if (tvp)
1784 		vput(tvp);
1785 	vrele(fdvp);
1786 	vrele(fvp);
1787 	/*
1788 	 * Kludge: Map ENOENT => 0 assuming that it is a reply to a retry.
1789 	 */
1790 	if (error == ENOENT)
1791 		error = 0;
1792 	return (error);
1793 }
1794 
1795 /*
1796  * nfs file rename rpc called from nfs_remove() above
1797  */
1798 static int
1799 nfs_renameit(struct vnode *sdvp, struct componentname *scnp,
1800 	     struct sillyrename *sp)
1801 {
1802 	return (nfs_renamerpc(sdvp, scnp->cn_nameptr, scnp->cn_namelen,
1803 		sdvp, sp->s_name, sp->s_namlen, scnp->cn_cred, scnp->cn_td));
1804 }
1805 
1806 /*
1807  * Do an nfs rename rpc. Called from nfs_rename() and nfs_renameit().
1808  */
1809 static int
1810 nfs_renamerpc(struct vnode *fdvp, const char *fnameptr, int fnamelen,
1811 	      struct vnode *tdvp, const char *tnameptr, int tnamelen,
1812 	      struct ucred *cred, struct thread *td)
1813 {
1814 	u_int32_t *tl;
1815 	caddr_t cp;
1816 	int32_t t1, t2;
1817 	caddr_t bpos, dpos, cp2;
1818 	int error = 0, fwccflag = NFSV3_WCCRATTR, twccflag = NFSV3_WCCRATTR;
1819 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1820 	int v3 = NFS_ISV3(fdvp);
1821 
1822 	nfsstats.rpccnt[NFSPROC_RENAME]++;
1823 	nfsm_reqhead(fdvp, NFSPROC_RENAME,
1824 		(NFSX_FH(v3) + NFSX_UNSIGNED)*2 + nfsm_rndup(fnamelen) +
1825 		nfsm_rndup(tnamelen));
1826 	nfsm_fhtom(fdvp, v3);
1827 	nfsm_strtom(fnameptr, fnamelen, NFS_MAXNAMLEN);
1828 	nfsm_fhtom(tdvp, v3);
1829 	nfsm_strtom(tnameptr, tnamelen, NFS_MAXNAMLEN);
1830 	nfsm_request(fdvp, NFSPROC_RENAME, td, cred);
1831 	if (v3) {
1832 		nfsm_wcc_data(fdvp, fwccflag);
1833 		nfsm_wcc_data(tdvp, twccflag);
1834 	}
1835 	m_freem(mrep);
1836 nfsmout:
1837 	VTONFS(fdvp)->n_flag |= NLMODIFIED;
1838 	VTONFS(tdvp)->n_flag |= NLMODIFIED;
1839 	if (!fwccflag)
1840 		VTONFS(fdvp)->n_attrstamp = 0;
1841 	if (!twccflag)
1842 		VTONFS(tdvp)->n_attrstamp = 0;
1843 	return (error);
1844 }
1845 
1846 /*
1847  * nfs hard link create call
1848  *
1849  * nfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
1850  *	    struct componentname *a_cnp)
1851  */
1852 static int
1853 nfs_link(struct vop_old_link_args *ap)
1854 {
1855 	struct vnode *vp = ap->a_vp;
1856 	struct vnode *tdvp = ap->a_tdvp;
1857 	struct componentname *cnp = ap->a_cnp;
1858 	u_int32_t *tl;
1859 	caddr_t cp;
1860 	int32_t t1, t2;
1861 	caddr_t bpos, dpos, cp2;
1862 	int error = 0, wccflag = NFSV3_WCCRATTR, attrflag = 0;
1863 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1864 	int v3;
1865 
1866 	if (vp->v_mount != tdvp->v_mount) {
1867 		return (EXDEV);
1868 	}
1869 
1870 	/*
1871 	 * Push all writes to the server, so that the attribute cache
1872 	 * doesn't get "out of sync" with the server.
1873 	 * XXX There should be a better way!
1874 	 */
1875 	VOP_FSYNC(vp, MNT_WAIT, cnp->cn_td);
1876 
1877 	v3 = NFS_ISV3(vp);
1878 	nfsstats.rpccnt[NFSPROC_LINK]++;
1879 	nfsm_reqhead(vp, NFSPROC_LINK,
1880 		NFSX_FH(v3)*2 + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
1881 	nfsm_fhtom(vp, v3);
1882 	nfsm_fhtom(tdvp, v3);
1883 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1884 	nfsm_request(vp, NFSPROC_LINK, cnp->cn_td, cnp->cn_cred);
1885 	if (v3) {
1886 		nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
1887 		nfsm_wcc_data(tdvp, wccflag);
1888 	}
1889 	m_freem(mrep);
1890 nfsmout:
1891 	VTONFS(tdvp)->n_flag |= NLMODIFIED;
1892 	if (!attrflag)
1893 		VTONFS(vp)->n_attrstamp = 0;
1894 	if (!wccflag)
1895 		VTONFS(tdvp)->n_attrstamp = 0;
1896 	/*
1897 	 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry.
1898 	 */
1899 	if (error == EEXIST)
1900 		error = 0;
1901 	return (error);
1902 }
1903 
1904 /*
1905  * nfs symbolic link create call
1906  *
1907  * nfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1908  *		struct componentname *a_cnp, struct vattr *a_vap,
1909  *		char *a_target)
1910  */
1911 static int
1912 nfs_symlink(struct vop_old_symlink_args *ap)
1913 {
1914 	struct vnode *dvp = ap->a_dvp;
1915 	struct vattr *vap = ap->a_vap;
1916 	struct componentname *cnp = ap->a_cnp;
1917 	struct nfsv2_sattr *sp;
1918 	u_int32_t *tl;
1919 	caddr_t cp;
1920 	int32_t t1, t2;
1921 	caddr_t bpos, dpos, cp2;
1922 	int slen, error = 0, wccflag = NFSV3_WCCRATTR, gotvp;
1923 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
1924 	struct vnode *newvp = (struct vnode *)0;
1925 	int v3 = NFS_ISV3(dvp);
1926 
1927 	nfsstats.rpccnt[NFSPROC_SYMLINK]++;
1928 	slen = strlen(ap->a_target);
1929 	nfsm_reqhead(dvp, NFSPROC_SYMLINK, NFSX_FH(v3) + 2*NFSX_UNSIGNED +
1930 	    nfsm_rndup(cnp->cn_namelen) + nfsm_rndup(slen) + NFSX_SATTR(v3));
1931 	nfsm_fhtom(dvp, v3);
1932 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
1933 	if (v3) {
1934 		nfsm_v3attrbuild(vap, FALSE);
1935 	}
1936 	nfsm_strtom(ap->a_target, slen, NFS_MAXPATHLEN);
1937 	if (!v3) {
1938 		nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1939 		sp->sa_mode = vtonfsv2_mode(VLNK, vap->va_mode);
1940 		sp->sa_uid = nfs_xdrneg1;
1941 		sp->sa_gid = nfs_xdrneg1;
1942 		sp->sa_size = nfs_xdrneg1;
1943 		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
1944 		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
1945 	}
1946 
1947 	/*
1948 	 * Issue the NFS request and get the rpc response.
1949 	 *
1950 	 * Only NFSv3 responses returning an error of 0 actually return
1951 	 * a file handle that can be converted into newvp without having
1952 	 * to do an extra lookup rpc.
1953 	 */
1954 	nfsm_request(dvp, NFSPROC_SYMLINK, cnp->cn_td, cnp->cn_cred);
1955 	if (v3) {
1956 		if (error == 0)
1957 			nfsm_mtofh(dvp, newvp, v3, gotvp);
1958 		nfsm_wcc_data(dvp, wccflag);
1959 	}
1960 
1961 	/*
1962 	 * out code jumps -> here, mrep is also freed.
1963 	 */
1964 
1965 	m_freem(mrep);
1966 nfsmout:
1967 
1968 	/*
1969 	 * If we get an EEXIST error, silently convert it to no-error
1970 	 * in case of an NFS retry.
1971 	 */
1972 	if (error == EEXIST)
1973 		error = 0;
1974 
1975 	/*
1976 	 * If we do not have (or no longer have) an error, and we could
1977 	 * not extract the newvp from the response due to the request being
1978 	 * NFSv2 or the error being EEXIST.  We have to do a lookup in order
1979 	 * to obtain a newvp to return.
1980 	 */
1981 	if (error == 0 && newvp == NULL) {
1982 		struct nfsnode *np = NULL;
1983 
1984 		error = nfs_lookitup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
1985 		    cnp->cn_cred, cnp->cn_td, &np);
1986 		if (!error)
1987 			newvp = NFSTOV(np);
1988 	}
1989 	if (error) {
1990 		if (newvp)
1991 			vput(newvp);
1992 	} else {
1993 		*ap->a_vpp = newvp;
1994 	}
1995 	VTONFS(dvp)->n_flag |= NLMODIFIED;
1996 	if (!wccflag)
1997 		VTONFS(dvp)->n_attrstamp = 0;
1998 	return (error);
1999 }
2000 
2001 /*
2002  * nfs make dir call
2003  *
2004  * nfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
2005  *	     struct componentname *a_cnp, struct vattr *a_vap)
2006  */
2007 static int
2008 nfs_mkdir(struct vop_old_mkdir_args *ap)
2009 {
2010 	struct vnode *dvp = ap->a_dvp;
2011 	struct vattr *vap = ap->a_vap;
2012 	struct componentname *cnp = ap->a_cnp;
2013 	struct nfsv2_sattr *sp;
2014 	u_int32_t *tl;
2015 	caddr_t cp;
2016 	int32_t t1, t2;
2017 	int len;
2018 	struct nfsnode *np = (struct nfsnode *)0;
2019 	struct vnode *newvp = (struct vnode *)0;
2020 	caddr_t bpos, dpos, cp2;
2021 	int error = 0, wccflag = NFSV3_WCCRATTR;
2022 	int gotvp = 0;
2023 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2024 	struct vattr vattr;
2025 	int v3 = NFS_ISV3(dvp);
2026 
2027 	if ((error = VOP_GETATTR(dvp, &vattr, cnp->cn_td)) != 0) {
2028 		return (error);
2029 	}
2030 	len = cnp->cn_namelen;
2031 	nfsstats.rpccnt[NFSPROC_MKDIR]++;
2032 	nfsm_reqhead(dvp, NFSPROC_MKDIR,
2033 	  NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len) + NFSX_SATTR(v3));
2034 	nfsm_fhtom(dvp, v3);
2035 	nfsm_strtom(cnp->cn_nameptr, len, NFS_MAXNAMLEN);
2036 	if (v3) {
2037 		nfsm_v3attrbuild(vap, FALSE);
2038 	} else {
2039 		nfsm_build(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2040 		sp->sa_mode = vtonfsv2_mode(VDIR, vap->va_mode);
2041 		sp->sa_uid = nfs_xdrneg1;
2042 		sp->sa_gid = nfs_xdrneg1;
2043 		sp->sa_size = nfs_xdrneg1;
2044 		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
2045 		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
2046 	}
2047 	nfsm_request(dvp, NFSPROC_MKDIR, cnp->cn_td, cnp->cn_cred);
2048 	if (!error)
2049 		nfsm_mtofh(dvp, newvp, v3, gotvp);
2050 	if (v3)
2051 		nfsm_wcc_data(dvp, wccflag);
2052 	m_freem(mrep);
2053 nfsmout:
2054 	VTONFS(dvp)->n_flag |= NLMODIFIED;
2055 	if (!wccflag)
2056 		VTONFS(dvp)->n_attrstamp = 0;
2057 	/*
2058 	 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry
2059 	 * if we can succeed in looking up the directory.
2060 	 */
2061 	if (error == EEXIST || (!error && !gotvp)) {
2062 		if (newvp) {
2063 			vrele(newvp);
2064 			newvp = (struct vnode *)0;
2065 		}
2066 		error = nfs_lookitup(dvp, cnp->cn_nameptr, len, cnp->cn_cred,
2067 			cnp->cn_td, &np);
2068 		if (!error) {
2069 			newvp = NFSTOV(np);
2070 			if (newvp->v_type != VDIR)
2071 				error = EEXIST;
2072 		}
2073 	}
2074 	if (error) {
2075 		if (newvp)
2076 			vrele(newvp);
2077 	} else
2078 		*ap->a_vpp = newvp;
2079 	return (error);
2080 }
2081 
2082 /*
2083  * nfs remove directory call
2084  *
2085  * nfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
2086  *	     struct componentname *a_cnp)
2087  */
2088 static int
2089 nfs_rmdir(struct vop_old_rmdir_args *ap)
2090 {
2091 	struct vnode *vp = ap->a_vp;
2092 	struct vnode *dvp = ap->a_dvp;
2093 	struct componentname *cnp = ap->a_cnp;
2094 	u_int32_t *tl;
2095 	caddr_t cp;
2096 	int32_t t1, t2;
2097 	caddr_t bpos, dpos, cp2;
2098 	int error = 0, wccflag = NFSV3_WCCRATTR;
2099 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2100 	int v3 = NFS_ISV3(dvp);
2101 
2102 	if (dvp == vp)
2103 		return (EINVAL);
2104 	nfsstats.rpccnt[NFSPROC_RMDIR]++;
2105 	nfsm_reqhead(dvp, NFSPROC_RMDIR,
2106 		NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(cnp->cn_namelen));
2107 	nfsm_fhtom(dvp, v3);
2108 	nfsm_strtom(cnp->cn_nameptr, cnp->cn_namelen, NFS_MAXNAMLEN);
2109 	nfsm_request(dvp, NFSPROC_RMDIR, cnp->cn_td, cnp->cn_cred);
2110 	if (v3)
2111 		nfsm_wcc_data(dvp, wccflag);
2112 	m_freem(mrep);
2113 nfsmout:
2114 	VTONFS(dvp)->n_flag |= NLMODIFIED;
2115 	if (!wccflag)
2116 		VTONFS(dvp)->n_attrstamp = 0;
2117 	/*
2118 	 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry.
2119 	 */
2120 	if (error == ENOENT)
2121 		error = 0;
2122 	return (error);
2123 }
2124 
2125 /*
2126  * nfs readdir call
2127  *
2128  * nfs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
2129  */
2130 static int
2131 nfs_readdir(struct vop_readdir_args *ap)
2132 {
2133 	struct vnode *vp = ap->a_vp;
2134 	struct nfsnode *np = VTONFS(vp);
2135 	struct uio *uio = ap->a_uio;
2136 	int tresid, error;
2137 	struct vattr vattr;
2138 
2139 	if (vp->v_type != VDIR)
2140 		return (EPERM);
2141 
2142 	/*
2143 	 * If we have a valid EOF offset cache we must call VOP_GETATTR()
2144 	 * and then check that is still valid, or if this is an NQNFS mount
2145 	 * we call NQNFS_CKCACHEABLE() instead of VOP_GETATTR().  Note that
2146 	 * VOP_GETATTR() does not necessarily go to the wire.
2147 	 */
2148 	if (np->n_direofoffset > 0 && uio->uio_offset >= np->n_direofoffset &&
2149 	    (np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0) {
2150 		if (VFSTONFS(vp->v_mount)->nm_flag & NFSMNT_NQNFS) {
2151 			if (NQNFS_CKCACHABLE(vp, ND_READ)) {
2152 				nfsstats.direofcache_hits++;
2153 				return (0);
2154 			}
2155 		} else if (VOP_GETATTR(vp, &vattr, uio->uio_td) == 0 &&
2156 			(np->n_flag & (NLMODIFIED|NRMODIFIED)) == 0
2157 		) {
2158 			nfsstats.direofcache_hits++;
2159 			return (0);
2160 		}
2161 	}
2162 
2163 	/*
2164 	 * Call nfs_bioread() to do the real work.  nfs_bioread() does its
2165 	 * own cache coherency checks so we do not have to.
2166 	 */
2167 	tresid = uio->uio_resid;
2168 	error = nfs_bioread(vp, uio, 0);
2169 
2170 	if (!error && uio->uio_resid == tresid)
2171 		nfsstats.direofcache_misses++;
2172 	return (error);
2173 }
2174 
2175 /*
2176  * Readdir rpc call.  nfs_bioread->nfs_doio->nfs_readdirrpc.
2177  *
2178  * Note that for directories, nfs_bioread maintains the underlying nfs-centric
2179  * offset/block and converts the nfs formatted directory entries for userland
2180  * consumption as well as deals with offsets into the middle of blocks.
2181  * nfs_doio only deals with logical blocks.  In particular, uio_offset will
2182  * be block-bounded.  It must convert to cookies for the actual RPC.
2183  */
2184 int
2185 nfs_readdirrpc(struct vnode *vp, struct uio *uiop)
2186 {
2187 	int len, left;
2188 	struct nfs_dirent *dp = NULL;
2189 	u_int32_t *tl;
2190 	caddr_t cp;
2191 	int32_t t1, t2;
2192 	nfsuint64 *cookiep;
2193 	caddr_t bpos, dpos, cp2;
2194 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2195 	nfsuint64 cookie;
2196 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2197 	struct nfsnode *dnp = VTONFS(vp);
2198 	u_quad_t fileno;
2199 	int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1;
2200 	int attrflag;
2201 	int v3 = NFS_ISV3(vp);
2202 
2203 #ifndef DIAGNOSTIC
2204 	if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2205 		(uiop->uio_resid & (DIRBLKSIZ - 1)))
2206 		panic("nfs readdirrpc bad uio");
2207 #endif
2208 
2209 	/*
2210 	 * If there is no cookie, assume directory was stale.
2211 	 */
2212 	cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2213 	if (cookiep)
2214 		cookie = *cookiep;
2215 	else
2216 		return (NFSERR_BAD_COOKIE);
2217 	/*
2218 	 * Loop around doing readdir rpc's of size nm_readdirsize
2219 	 * truncated to a multiple of DIRBLKSIZ.
2220 	 * The stopping criteria is EOF or buffer full.
2221 	 */
2222 	while (more_dirs && bigenough) {
2223 		nfsstats.rpccnt[NFSPROC_READDIR]++;
2224 		nfsm_reqhead(vp, NFSPROC_READDIR, NFSX_FH(v3) +
2225 			NFSX_READDIR(v3));
2226 		nfsm_fhtom(vp, v3);
2227 		if (v3) {
2228 			nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2229 			*tl++ = cookie.nfsuquad[0];
2230 			*tl++ = cookie.nfsuquad[1];
2231 			*tl++ = dnp->n_cookieverf.nfsuquad[0];
2232 			*tl++ = dnp->n_cookieverf.nfsuquad[1];
2233 		} else {
2234 			nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2235 			*tl++ = cookie.nfsuquad[0];
2236 		}
2237 		*tl = txdr_unsigned(nmp->nm_readdirsize);
2238 		nfsm_request(vp, NFSPROC_READDIR, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2239 		if (v3) {
2240 			nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
2241 			if (!error) {
2242 				nfsm_dissect(tl, u_int32_t *,
2243 				    2 * NFSX_UNSIGNED);
2244 				dnp->n_cookieverf.nfsuquad[0] = *tl++;
2245 				dnp->n_cookieverf.nfsuquad[1] = *tl;
2246 			} else {
2247 				m_freem(mrep);
2248 				goto nfsmout;
2249 			}
2250 		}
2251 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2252 		more_dirs = fxdr_unsigned(int, *tl);
2253 
2254 		/* loop thru the dir entries, converting them to std form */
2255 		while (more_dirs && bigenough) {
2256 			if (v3) {
2257 				nfsm_dissect(tl, u_int32_t *,
2258 				    3 * NFSX_UNSIGNED);
2259 				fileno = fxdr_hyper(tl);
2260 				len = fxdr_unsigned(int, *(tl + 2));
2261 			} else {
2262 				nfsm_dissect(tl, u_int32_t *,
2263 				    2 * NFSX_UNSIGNED);
2264 				fileno = fxdr_unsigned(u_quad_t, *tl++);
2265 				len = fxdr_unsigned(int, *tl);
2266 			}
2267 			if (len <= 0 || len > NFS_MAXNAMLEN) {
2268 				error = EBADRPC;
2269 				m_freem(mrep);
2270 				goto nfsmout;
2271 			}
2272 
2273 			/*
2274 			 * len is the number of bytes in the path element
2275 			 * name, not including the \0 termination.
2276 			 *
2277 			 * tlen is the number of bytes w have to reserve for
2278 			 * the path element name.
2279 			 */
2280 			tlen = nfsm_rndup(len);
2281 			if (tlen == len)
2282 				tlen += 4;	/* To ensure null termination */
2283 
2284 			/*
2285 			 * If the entry would cross a DIRBLKSIZ boundary,
2286 			 * extend the previous nfs_dirent to cover the
2287 			 * remaining space.
2288 			 */
2289 			left = DIRBLKSIZ - blksiz;
2290 			if ((tlen + sizeof(struct nfs_dirent)) > left) {
2291 				dp->nfs_reclen += left;
2292 				uiop->uio_iov->iov_base += left;
2293 				uiop->uio_iov->iov_len -= left;
2294 				uiop->uio_offset += left;
2295 				uiop->uio_resid -= left;
2296 				blksiz = 0;
2297 			}
2298 			if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2299 				bigenough = 0;
2300 			if (bigenough) {
2301 				dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2302 				dp->nfs_ino = fileno;
2303 				dp->nfs_namlen = len;
2304 				dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2305 				dp->nfs_type = DT_UNKNOWN;
2306 				blksiz += dp->nfs_reclen;
2307 				if (blksiz == DIRBLKSIZ)
2308 					blksiz = 0;
2309 				uiop->uio_offset += sizeof(struct nfs_dirent);
2310 				uiop->uio_resid -= sizeof(struct nfs_dirent);
2311 				uiop->uio_iov->iov_base += sizeof(struct nfs_dirent);
2312 				uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2313 				nfsm_mtouio(uiop, len);
2314 
2315 				/*
2316 				 * The uiop has advanced by nfs_dirent + len
2317 				 * but really needs to advance by
2318 				 * nfs_dirent + tlen
2319 				 */
2320 				cp = uiop->uio_iov->iov_base;
2321 				tlen -= len;
2322 				*cp = '\0';	/* null terminate */
2323 				uiop->uio_iov->iov_base += tlen;
2324 				uiop->uio_iov->iov_len -= tlen;
2325 				uiop->uio_offset += tlen;
2326 				uiop->uio_resid -= tlen;
2327 			} else {
2328 				/*
2329 				 * NFS strings must be rounded up (nfsm_myouio
2330 				 * handled that in the bigenough case).
2331 				 */
2332 				nfsm_adv(nfsm_rndup(len));
2333 			}
2334 			if (v3) {
2335 				nfsm_dissect(tl, u_int32_t *,
2336 				    3 * NFSX_UNSIGNED);
2337 			} else {
2338 				nfsm_dissect(tl, u_int32_t *,
2339 				    2 * NFSX_UNSIGNED);
2340 			}
2341 
2342 			/*
2343 			 * If we were able to accomodate the last entry,
2344 			 * get the cookie for the next one.  Otherwise
2345 			 * hold-over the cookie for the one we were not
2346 			 * able to accomodate.
2347 			 */
2348 			if (bigenough) {
2349 				cookie.nfsuquad[0] = *tl++;
2350 				if (v3)
2351 					cookie.nfsuquad[1] = *tl++;
2352 			} else if (v3) {
2353 				tl += 2;
2354 			} else {
2355 				tl++;
2356 			}
2357 			more_dirs = fxdr_unsigned(int, *tl);
2358 		}
2359 		/*
2360 		 * If at end of rpc data, get the eof boolean
2361 		 */
2362 		if (!more_dirs) {
2363 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2364 			more_dirs = (fxdr_unsigned(int, *tl) == 0);
2365 		}
2366 		m_freem(mrep);
2367 	}
2368 	/*
2369 	 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2370 	 * by increasing d_reclen for the last record.
2371 	 */
2372 	if (blksiz > 0) {
2373 		left = DIRBLKSIZ - blksiz;
2374 		dp->nfs_reclen += left;
2375 		uiop->uio_iov->iov_base += left;
2376 		uiop->uio_iov->iov_len -= left;
2377 		uiop->uio_offset += left;
2378 		uiop->uio_resid -= left;
2379 	}
2380 
2381 	if (bigenough) {
2382 		/*
2383 		 * We hit the end of the directory, update direofoffset.
2384 		 */
2385 		dnp->n_direofoffset = uiop->uio_offset;
2386 	} else {
2387 		/*
2388 		 * There is more to go, insert the link cookie so the
2389 		 * next block can be read.
2390 		 */
2391 		if (uiop->uio_resid > 0)
2392 			printf("EEK! readdirrpc resid > 0\n");
2393 		cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2394 		*cookiep = cookie;
2395 	}
2396 nfsmout:
2397 	return (error);
2398 }
2399 
2400 /*
2401  * NFS V3 readdir plus RPC. Used in place of nfs_readdirrpc().
2402  */
2403 int
2404 nfs_readdirplusrpc(struct vnode *vp, struct uio *uiop)
2405 {
2406 	int len, left;
2407 	struct nfs_dirent *dp;
2408 	u_int32_t *tl;
2409 	caddr_t cp;
2410 	int32_t t1, t2;
2411 	struct vnode *newvp;
2412 	nfsuint64 *cookiep;
2413 	caddr_t bpos, dpos, cp2, dpossav1, dpossav2;
2414 	struct mbuf *mreq, *mrep, *md, *mb, *mb2, *mdsav1, *mdsav2;
2415 	nfsuint64 cookie;
2416 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2417 	struct nfsnode *dnp = VTONFS(vp), *np;
2418 	nfsfh_t *fhp;
2419 	u_quad_t fileno;
2420 	int error = 0, tlen, more_dirs = 1, blksiz = 0, doit, bigenough = 1, i;
2421 	int attrflag, fhsize;
2422 	struct namecache *ncp;
2423 	struct namecache *dncp;
2424 	struct nlcomponent nlc;
2425 
2426 #ifndef nolint
2427 	dp = NULL;
2428 #endif
2429 #ifndef DIAGNOSTIC
2430 	if (uiop->uio_iovcnt != 1 || (uiop->uio_offset & (DIRBLKSIZ - 1)) ||
2431 		(uiop->uio_resid & (DIRBLKSIZ - 1)))
2432 		panic("nfs readdirplusrpc bad uio");
2433 #endif
2434 	/*
2435 	 * Obtain the namecache record for the directory so we have something
2436 	 * to use as a basis for creating the entries.  This function will
2437 	 * return a held (but not locked) ncp.  The ncp may be disconnected
2438 	 * from the tree and cannot be used for upward traversals, and the
2439 	 * ncp may be unnamed.  Note that other unrelated operations may
2440 	 * cause the ncp to be named at any time.
2441 	 */
2442 	dncp = cache_fromdvp(vp, NULL, 0);
2443 	bzero(&nlc, sizeof(nlc));
2444 	newvp = NULLVP;
2445 
2446 	/*
2447 	 * If there is no cookie, assume directory was stale.
2448 	 */
2449 	cookiep = nfs_getcookie(dnp, uiop->uio_offset, 0);
2450 	if (cookiep)
2451 		cookie = *cookiep;
2452 	else
2453 		return (NFSERR_BAD_COOKIE);
2454 	/*
2455 	 * Loop around doing readdir rpc's of size nm_readdirsize
2456 	 * truncated to a multiple of DIRBLKSIZ.
2457 	 * The stopping criteria is EOF or buffer full.
2458 	 */
2459 	while (more_dirs && bigenough) {
2460 		nfsstats.rpccnt[NFSPROC_READDIRPLUS]++;
2461 		nfsm_reqhead(vp, NFSPROC_READDIRPLUS,
2462 			NFSX_FH(1) + 6 * NFSX_UNSIGNED);
2463 		nfsm_fhtom(vp, 1);
2464  		nfsm_build(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2465 		*tl++ = cookie.nfsuquad[0];
2466 		*tl++ = cookie.nfsuquad[1];
2467 		*tl++ = dnp->n_cookieverf.nfsuquad[0];
2468 		*tl++ = dnp->n_cookieverf.nfsuquad[1];
2469 		*tl++ = txdr_unsigned(nmp->nm_readdirsize);
2470 		*tl = txdr_unsigned(nmp->nm_rsize);
2471 		nfsm_request(vp, NFSPROC_READDIRPLUS, uiop->uio_td, nfs_vpcred(vp, ND_READ));
2472 		nfsm_postop_attr(vp, attrflag, NFS_LATTR_NOSHRINK);
2473 		if (error) {
2474 			m_freem(mrep);
2475 			goto nfsmout;
2476 		}
2477 		nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2478 		dnp->n_cookieverf.nfsuquad[0] = *tl++;
2479 		dnp->n_cookieverf.nfsuquad[1] = *tl++;
2480 		more_dirs = fxdr_unsigned(int, *tl);
2481 
2482 		/* loop thru the dir entries, doctoring them to 4bsd form */
2483 		while (more_dirs && bigenough) {
2484 			nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2485 			fileno = fxdr_hyper(tl);
2486 			len = fxdr_unsigned(int, *(tl + 2));
2487 			if (len <= 0 || len > NFS_MAXNAMLEN) {
2488 				error = EBADRPC;
2489 				m_freem(mrep);
2490 				goto nfsmout;
2491 			}
2492 			tlen = nfsm_rndup(len);
2493 			if (tlen == len)
2494 				tlen += 4;	/* To ensure null termination*/
2495 			left = DIRBLKSIZ - blksiz;
2496 			if ((tlen + sizeof(struct nfs_dirent)) > left) {
2497 				dp->nfs_reclen += left;
2498 				uiop->uio_iov->iov_base += left;
2499 				uiop->uio_iov->iov_len -= left;
2500 				uiop->uio_offset += left;
2501 				uiop->uio_resid -= left;
2502 				blksiz = 0;
2503 			}
2504 			if ((tlen + sizeof(struct nfs_dirent)) > uiop->uio_resid)
2505 				bigenough = 0;
2506 			if (bigenough) {
2507 				dp = (struct nfs_dirent *)uiop->uio_iov->iov_base;
2508 				dp->nfs_ino = fileno;
2509 				dp->nfs_namlen = len;
2510 				dp->nfs_reclen = tlen + sizeof(struct nfs_dirent);
2511 				dp->nfs_type = DT_UNKNOWN;
2512 				blksiz += dp->nfs_reclen;
2513 				if (blksiz == DIRBLKSIZ)
2514 					blksiz = 0;
2515 				uiop->uio_offset += sizeof(struct nfs_dirent);
2516 				uiop->uio_resid -= sizeof(struct nfs_dirent);
2517 				uiop->uio_iov->iov_base += sizeof(struct nfs_dirent);
2518 				uiop->uio_iov->iov_len -= sizeof(struct nfs_dirent);
2519 				nlc.nlc_nameptr = uiop->uio_iov->iov_base;
2520 				nlc.nlc_namelen = len;
2521 				nfsm_mtouio(uiop, len);
2522 				cp = uiop->uio_iov->iov_base;
2523 				tlen -= len;
2524 				*cp = '\0';
2525 				uiop->uio_iov->iov_base += tlen;
2526 				uiop->uio_iov->iov_len -= tlen;
2527 				uiop->uio_offset += tlen;
2528 				uiop->uio_resid -= tlen;
2529 			} else
2530 				nfsm_adv(nfsm_rndup(len));
2531 			nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2532 			if (bigenough) {
2533 				cookie.nfsuquad[0] = *tl++;
2534 				cookie.nfsuquad[1] = *tl++;
2535 			} else
2536 				tl += 2;
2537 
2538 			/*
2539 			 * Since the attributes are before the file handle
2540 			 * (sigh), we must skip over the attributes and then
2541 			 * come back and get them.
2542 			 */
2543 			attrflag = fxdr_unsigned(int, *tl);
2544 			if (attrflag) {
2545 			    dpossav1 = dpos;
2546 			    mdsav1 = md;
2547 			    nfsm_adv(NFSX_V3FATTR);
2548 			    nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2549 			    doit = fxdr_unsigned(int, *tl);
2550 			    if (doit) {
2551 				nfsm_getfh(fhp, fhsize, 1);
2552 				if (NFS_CMPFH(dnp, fhp, fhsize)) {
2553 				    vref(vp);
2554 				    newvp = vp;
2555 				    np = dnp;
2556 				} else {
2557 				    error = nfs_nget(vp->v_mount, fhp,
2558 					fhsize, &np);
2559 				    if (error)
2560 					doit = 0;
2561 				    else
2562 					newvp = NFSTOV(np);
2563 				}
2564 			    }
2565 			    if (doit && bigenough) {
2566 				dpossav2 = dpos;
2567 				dpos = dpossav1;
2568 				mdsav2 = md;
2569 				md = mdsav1;
2570 				nfsm_loadattr(newvp, (struct vattr *)0);
2571 				dpos = dpossav2;
2572 				md = mdsav2;
2573 				dp->nfs_type =
2574 				    IFTODT(VTTOIF(np->n_vattr.va_type));
2575 				if (dncp) {
2576 				    printf("NFS/READDIRPLUS, ENTER %*.*s\n",
2577 					nlc.nlc_namelen, nlc.nlc_namelen,
2578 					nlc.nlc_nameptr);
2579 				    ncp = cache_nlookup(dncp, &nlc);
2580 				    cache_setunresolved(ncp);
2581 				    cache_setvp(ncp, newvp);
2582 				    cache_put(ncp);
2583 				} else {
2584 				    printf("NFS/READDIRPLUS, UNABLE TO ENTER"
2585 					" %*.*s\n",
2586 					nlc.nlc_namelen, nlc.nlc_namelen,
2587 					nlc.nlc_nameptr);
2588 				}
2589 			    }
2590 			} else {
2591 			    /* Just skip over the file handle */
2592 			    nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2593 			    i = fxdr_unsigned(int, *tl);
2594 			    nfsm_adv(nfsm_rndup(i));
2595 			}
2596 			if (newvp != NULLVP) {
2597 			    if (newvp == vp)
2598 				vrele(newvp);
2599 			    else
2600 				vput(newvp);
2601 			    newvp = NULLVP;
2602 			}
2603 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2604 			more_dirs = fxdr_unsigned(int, *tl);
2605 		}
2606 		/*
2607 		 * If at end of rpc data, get the eof boolean
2608 		 */
2609 		if (!more_dirs) {
2610 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2611 			more_dirs = (fxdr_unsigned(int, *tl) == 0);
2612 		}
2613 		m_freem(mrep);
2614 	}
2615 	/*
2616 	 * Fill last record, iff any, out to a multiple of DIRBLKSIZ
2617 	 * by increasing d_reclen for the last record.
2618 	 */
2619 	if (blksiz > 0) {
2620 		left = DIRBLKSIZ - blksiz;
2621 		dp->nfs_reclen += left;
2622 		uiop->uio_iov->iov_base += left;
2623 		uiop->uio_iov->iov_len -= left;
2624 		uiop->uio_offset += left;
2625 		uiop->uio_resid -= left;
2626 	}
2627 
2628 	/*
2629 	 * We are now either at the end of the directory or have filled the
2630 	 * block.
2631 	 */
2632 	if (bigenough)
2633 		dnp->n_direofoffset = uiop->uio_offset;
2634 	else {
2635 		if (uiop->uio_resid > 0)
2636 			printf("EEK! readdirplusrpc resid > 0\n");
2637 		cookiep = nfs_getcookie(dnp, uiop->uio_offset, 1);
2638 		*cookiep = cookie;
2639 	}
2640 nfsmout:
2641 	if (newvp != NULLVP) {
2642 	        if (newvp == vp)
2643 			vrele(newvp);
2644 		else
2645 			vput(newvp);
2646 		newvp = NULLVP;
2647 	}
2648 	if (dncp)
2649 		cache_drop(dncp);
2650 	return (error);
2651 }
2652 
2653 /*
2654  * Silly rename. To make the NFS filesystem that is stateless look a little
2655  * more like the "ufs" a remove of an active vnode is translated to a rename
2656  * to a funny looking filename that is removed by nfs_inactive on the
2657  * nfsnode. There is the potential for another process on a different client
2658  * to create the same funny name between the nfs_lookitup() fails and the
2659  * nfs_rename() completes, but...
2660  */
2661 static int
2662 nfs_sillyrename(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
2663 {
2664 	struct sillyrename *sp;
2665 	struct nfsnode *np;
2666 	int error;
2667 
2668 	/*
2669 	 * We previously purged dvp instead of vp.  I don't know why, it
2670 	 * completely destroys performance.  We can't do it anyway with the
2671 	 * new VFS API since we would be breaking the namecache topology.
2672 	 */
2673 	cache_purge(vp);	/* XXX */
2674 	np = VTONFS(vp);
2675 #ifndef DIAGNOSTIC
2676 	if (vp->v_type == VDIR)
2677 		panic("nfs: sillyrename dir");
2678 #endif
2679 	MALLOC(sp, struct sillyrename *, sizeof (struct sillyrename),
2680 		M_NFSREQ, M_WAITOK);
2681 	sp->s_cred = crdup(cnp->cn_cred);
2682 	sp->s_dvp = dvp;
2683 	vref(dvp);
2684 
2685 	/* Fudge together a funny name */
2686 	sp->s_namlen = sprintf(sp->s_name, ".nfsA%08x4.4", (int)cnp->cn_td);
2687 
2688 	/* Try lookitups until we get one that isn't there */
2689 	while (nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2690 		cnp->cn_td, (struct nfsnode **)0) == 0) {
2691 		sp->s_name[4]++;
2692 		if (sp->s_name[4] > 'z') {
2693 			error = EINVAL;
2694 			goto bad;
2695 		}
2696 	}
2697 	error = nfs_renameit(dvp, cnp, sp);
2698 	if (error)
2699 		goto bad;
2700 	error = nfs_lookitup(dvp, sp->s_name, sp->s_namlen, sp->s_cred,
2701 		cnp->cn_td, &np);
2702 	np->n_sillyrename = sp;
2703 	return (0);
2704 bad:
2705 	vrele(sp->s_dvp);
2706 	crfree(sp->s_cred);
2707 	free((caddr_t)sp, M_NFSREQ);
2708 	return (error);
2709 }
2710 
2711 /*
2712  * Look up a file name and optionally either update the file handle or
2713  * allocate an nfsnode, depending on the value of npp.
2714  * npp == NULL	--> just do the lookup
2715  * *npp == NULL --> allocate a new nfsnode and make sure attributes are
2716  *			handled too
2717  * *npp != NULL --> update the file handle in the vnode
2718  */
2719 static int
2720 nfs_lookitup(struct vnode *dvp, const char *name, int len, struct ucred *cred,
2721 	     struct thread *td, struct nfsnode **npp)
2722 {
2723 	u_int32_t *tl;
2724 	caddr_t cp;
2725 	int32_t t1, t2;
2726 	struct vnode *newvp = (struct vnode *)0;
2727 	struct nfsnode *np, *dnp = VTONFS(dvp);
2728 	caddr_t bpos, dpos, cp2;
2729 	int error = 0, fhlen, attrflag;
2730 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2731 	nfsfh_t *nfhp;
2732 	int v3 = NFS_ISV3(dvp);
2733 
2734 	nfsstats.rpccnt[NFSPROC_LOOKUP]++;
2735 	nfsm_reqhead(dvp, NFSPROC_LOOKUP,
2736 		NFSX_FH(v3) + NFSX_UNSIGNED + nfsm_rndup(len));
2737 	nfsm_fhtom(dvp, v3);
2738 	nfsm_strtom(name, len, NFS_MAXNAMLEN);
2739 	nfsm_request(dvp, NFSPROC_LOOKUP, td, cred);
2740 	if (npp && !error) {
2741 		nfsm_getfh(nfhp, fhlen, v3);
2742 		if (*npp) {
2743 		    np = *npp;
2744 		    if (np->n_fhsize > NFS_SMALLFH && fhlen <= NFS_SMALLFH) {
2745 			free((caddr_t)np->n_fhp, M_NFSBIGFH);
2746 			np->n_fhp = &np->n_fh;
2747 		    } else if (np->n_fhsize <= NFS_SMALLFH && fhlen>NFS_SMALLFH)
2748 			np->n_fhp =(nfsfh_t *)malloc(fhlen,M_NFSBIGFH,M_WAITOK);
2749 		    bcopy((caddr_t)nfhp, (caddr_t)np->n_fhp, fhlen);
2750 		    np->n_fhsize = fhlen;
2751 		    newvp = NFSTOV(np);
2752 		} else if (NFS_CMPFH(dnp, nfhp, fhlen)) {
2753 		    vref(dvp);
2754 		    newvp = dvp;
2755 		} else {
2756 		    error = nfs_nget(dvp->v_mount, nfhp, fhlen, &np);
2757 		    if (error) {
2758 			m_freem(mrep);
2759 			return (error);
2760 		    }
2761 		    newvp = NFSTOV(np);
2762 		}
2763 		if (v3) {
2764 			nfsm_postop_attr(newvp, attrflag, NFS_LATTR_NOSHRINK);
2765 			if (!attrflag && *npp == NULL) {
2766 				m_freem(mrep);
2767 				if (newvp == dvp)
2768 					vrele(newvp);
2769 				else
2770 					vput(newvp);
2771 				return (ENOENT);
2772 			}
2773 		} else
2774 			nfsm_loadattr(newvp, (struct vattr *)0);
2775 	}
2776 	m_freem(mrep);
2777 nfsmout:
2778 	if (npp && *npp == NULL) {
2779 		if (error) {
2780 			if (newvp) {
2781 				if (newvp == dvp)
2782 					vrele(newvp);
2783 				else
2784 					vput(newvp);
2785 			}
2786 		} else
2787 			*npp = np;
2788 	}
2789 	return (error);
2790 }
2791 
2792 /*
2793  * Nfs Version 3 commit rpc
2794  */
2795 int
2796 nfs_commit(struct vnode *vp, u_quad_t offset, int cnt, struct thread *td)
2797 {
2798 	caddr_t cp;
2799 	u_int32_t *tl;
2800 	int32_t t1, t2;
2801 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2802 	caddr_t bpos, dpos, cp2;
2803 	int error = 0, wccflag = NFSV3_WCCRATTR;
2804 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
2805 
2806 	if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0)
2807 		return (0);
2808 	nfsstats.rpccnt[NFSPROC_COMMIT]++;
2809 	nfsm_reqhead(vp, NFSPROC_COMMIT, NFSX_FH(1));
2810 	nfsm_fhtom(vp, 1);
2811 	nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2812 	txdr_hyper(offset, tl);
2813 	tl += 2;
2814 	*tl = txdr_unsigned(cnt);
2815 	nfsm_request(vp, NFSPROC_COMMIT, td, nfs_vpcred(vp, ND_WRITE));
2816 	nfsm_wcc_data(vp, wccflag);
2817 	if (!error) {
2818 		nfsm_dissect(tl, u_int32_t *, NFSX_V3WRITEVERF);
2819 		if (bcmp((caddr_t)nmp->nm_verf, (caddr_t)tl,
2820 			NFSX_V3WRITEVERF)) {
2821 			bcopy((caddr_t)tl, (caddr_t)nmp->nm_verf,
2822 				NFSX_V3WRITEVERF);
2823 			error = NFSERR_STALEWRITEVERF;
2824 		}
2825 	}
2826 	m_freem(mrep);
2827 nfsmout:
2828 	return (error);
2829 }
2830 
2831 /*
2832  * Kludge City..
2833  * - make nfs_bmap() essentially a no-op that does no translation
2834  * - do nfs_strategy() by doing I/O with nfs_readrpc/nfs_writerpc
2835  *   (Maybe I could use the process's page mapping, but I was concerned that
2836  *    Kernel Write might not be enabled and also figured copyout() would do
2837  *    a lot more work than bcopy() and also it currently happens in the
2838  *    context of the swapper process (2).
2839  *
2840  * nfs_bmap(struct vnode *a_vp, daddr_t a_bn, struct vnode **a_vpp,
2841  *	    daddr_t *a_bnp, int *a_runp, int *a_runb)
2842  */
2843 static int
2844 nfs_bmap(struct vop_bmap_args *ap)
2845 {
2846 	struct vnode *vp = ap->a_vp;
2847 
2848 	if (ap->a_vpp != NULL)
2849 		*ap->a_vpp = vp;
2850 	if (ap->a_bnp != NULL)
2851 		*ap->a_bnp = ap->a_bn * btodb(vp->v_mount->mnt_stat.f_iosize);
2852 	if (ap->a_runp != NULL)
2853 		*ap->a_runp = 0;
2854 	if (ap->a_runb != NULL)
2855 		*ap->a_runb = 0;
2856 	return (0);
2857 }
2858 
2859 /*
2860  * Strategy routine.
2861  *
2862  * For async requests when nfsiod(s) are running, queue the request by
2863  * calling nfs_asyncio(), otherwise just all nfs_doio() to do the
2864  * request.
2865  *
2866  * bio_blkno is NFS block-sized, which depends whether the vnode is a
2867  * regular file or a directory.
2868  */
2869 static int
2870 nfs_strategy(struct vop_strategy_args *ap)
2871 {
2872 	struct bio *bio = ap->a_bio;
2873 	struct bio *nbio;
2874 	struct buf *bp = bio->bio_buf;
2875 	struct thread *td;
2876 	int error = 0;
2877 
2878 	KASSERT(!(bp->b_flags & B_DONE),
2879 		("nfs_strategy: buffer %p unexpectedly marked B_DONE", bp));
2880 	KASSERT(BUF_REFCNT(bp) > 0,
2881 		("nfs_strategy: buffer %p not locked", bp));
2882 
2883 	if (bp->b_flags & B_PHYS)
2884 		panic("nfs physio");
2885 
2886 	if (bp->b_flags & B_ASYNC)
2887 		td = NULL;
2888 	else
2889 		td = curthread;	/* XXX */
2890 
2891         /*
2892 	 * Convert to DEV_BSIZE'd blocks for nfs_doio/nfs_asyncio
2893          */
2894 	nbio = push_bio(bio);
2895 
2896         if (bp->b_vp->v_type == VREG) {
2897                 int biosize;
2898 
2899                 biosize = bp->b_vp->v_mount->mnt_stat.f_iosize;
2900                 nbio->bio_blkno = ((off_t)bio->bio_blkno * biosize) >>
2901 				  DEV_BSHIFT;
2902         } else {
2903                 nbio->bio_blkno = ((off_t)bio->bio_blkno * NFS_DIRBLKSIZ) >>
2904 				  DEV_BSHIFT;
2905         }
2906 
2907 
2908 	/*
2909 	 * If the op is asynchronous and an i/o daemon is waiting
2910 	 * queue the request, wake it up and wait for completion
2911 	 * otherwise just do it ourselves.
2912 	 */
2913 	if ((bp->b_flags & B_ASYNC) == 0 || nfs_asyncio(ap->a_vp, nbio, td))
2914 		error = nfs_doio(ap->a_vp, nbio, td);
2915 	return (error);
2916 }
2917 
2918 /*
2919  * Mmap a file
2920  *
2921  * NB Currently unsupported.
2922  *
2923  * nfs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred,
2924  *	    struct thread *a_td)
2925  */
2926 /* ARGSUSED */
2927 static int
2928 nfs_mmap(struct vop_mmap_args *ap)
2929 {
2930 	return (EINVAL);
2931 }
2932 
2933 /*
2934  * fsync vnode op. Just call nfs_flush() with commit == 1.
2935  *
2936  * nfs_fsync(struct vnodeop_desc *a_desc, struct vnode *a_vp,
2937  *	     struct ucred * a_cred, int a_waitfor, struct thread *a_td)
2938  */
2939 /* ARGSUSED */
2940 static int
2941 nfs_fsync(struct vop_fsync_args *ap)
2942 {
2943 	return (nfs_flush(ap->a_vp, ap->a_waitfor, ap->a_td, 1));
2944 }
2945 
2946 /*
2947  * Flush all the blocks associated with a vnode.   Dirty NFS buffers may be
2948  * in one of two states:  If B_NEEDCOMMIT is clear then the buffer contains
2949  * new NFS data which needs to be written to the server.  If B_NEEDCOMMIT is
2950  * set the buffer contains data that has already been written to the server
2951  * and which now needs a commit RPC.
2952  *
2953  * If commit is 0 we only take one pass and only flush buffers containing new
2954  * dirty data.
2955  *
2956  * If commit is 1 we take two passes, issuing a commit RPC in the second
2957  * pass.
2958  *
2959  * If waitfor is MNT_WAIT and commit is 1, we loop as many times as required
2960  * to completely flush all pending data.
2961  *
2962  * Note that the RB_SCAN code properly handles the case where the
2963  * callback might block and directly or indirectly (another thread) cause
2964  * the RB tree to change.
2965  */
2966 
2967 #ifndef NFS_COMMITBVECSIZ
2968 #define NFS_COMMITBVECSIZ	16
2969 #endif
2970 
2971 struct nfs_flush_info {
2972 	enum { NFI_FLUSHNEW, NFI_COMMIT } mode;
2973 	struct thread *td;
2974 	struct vnode *vp;
2975 	int waitfor;
2976 	int slpflag;
2977 	int slptimeo;
2978 	int loops;
2979 	struct buf *bvary[NFS_COMMITBVECSIZ];
2980 	int bvsize;
2981 	off_t beg_off;
2982 	off_t end_off;
2983 };
2984 
2985 static int nfs_flush_bp(struct buf *bp, void *data);
2986 static int nfs_flush_docommit(struct nfs_flush_info *info, int error);
2987 
2988 int
2989 nfs_flush(struct vnode *vp, int waitfor, struct thread *td, int commit)
2990 {
2991 	struct nfsnode *np = VTONFS(vp);
2992 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
2993 	struct nfs_flush_info info;
2994 	int error;
2995 
2996 	bzero(&info, sizeof(info));
2997 	info.td = td;
2998 	info.vp = vp;
2999 	info.waitfor = waitfor;
3000 	info.slpflag = (nmp->nm_flag & NFSMNT_INT) ? PCATCH : 0;
3001 	info.loops = 0;
3002 
3003 	do {
3004 		/*
3005 		 * Flush mode
3006 		 */
3007 		info.mode = NFI_FLUSHNEW;
3008 		error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
3009 				nfs_flush_bp, &info);
3010 
3011 		/*
3012 		 * Take a second pass if committing and no error occured.
3013 		 * Clean up any left over collection (whether an error
3014 		 * occurs or not).
3015 		 */
3016 		if (commit && error == 0) {
3017 			info.mode = NFI_COMMIT;
3018 			error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
3019 					nfs_flush_bp, &info);
3020 			if (info.bvsize)
3021 				error = nfs_flush_docommit(&info, error);
3022 		}
3023 
3024 		/*
3025 		 * Wait for pending I/O to complete before checking whether
3026 		 * any further dirty buffers exist.
3027 		 */
3028 		while (waitfor == MNT_WAIT && vp->v_track_write.bk_active) {
3029 			vp->v_track_write.bk_waitflag = 1;
3030 			error = tsleep(&vp->v_track_write,
3031 				info.slpflag, "nfsfsync", info.slptimeo);
3032 			if (error) {
3033 				/*
3034 				 * We have to be able to break out if this
3035 				 * is an 'intr' mount.
3036 				 */
3037 				if (nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
3038 					error = -EINTR;
3039 					break;
3040 				}
3041 
3042 				/*
3043 				 * Since we do not process pending signals,
3044 				 * once we get a PCATCH our tsleep() will no
3045 				 * longer sleep, switch to a fixed timeout
3046 				 * instead.
3047 				 */
3048 				if (info.slpflag == PCATCH) {
3049 					info.slpflag = 0;
3050 					info.slptimeo = 2 * hz;
3051 				}
3052 				error = 0;
3053 			}
3054 		}
3055 		++info.loops;
3056 		/*
3057 		 * Loop if we are flushing synchronous as well as committing,
3058 		 * and dirty buffers are still present.  Otherwise we might livelock.
3059 		 */
3060 	} while (waitfor == MNT_WAIT && commit &&
3061 		 error == 0 && !RB_EMPTY(&vp->v_rbdirty_tree));
3062 
3063 	/*
3064 	 * The callbacks have to return a negative error to terminate the
3065 	 * RB scan.
3066 	 */
3067 	if (error < 0)
3068 		error = -error;
3069 
3070 	/*
3071 	 * Deal with any error collection
3072 	 */
3073 	if (np->n_flag & NWRITEERR) {
3074 		error = np->n_error;
3075 		np->n_flag &= ~NWRITEERR;
3076 	}
3077 	return (error);
3078 }
3079 
3080 
3081 static
3082 int
3083 nfs_flush_bp(struct buf *bp, void *data)
3084 {
3085 	struct nfs_flush_info *info = data;
3086 	off_t toff;
3087 	int error;
3088 
3089 	error = 0;
3090 	switch(info->mode) {
3091 	case NFI_FLUSHNEW:
3092 		crit_enter();
3093 		if (info->loops && info->waitfor == MNT_WAIT) {
3094 			error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3095 			if (error) {
3096 				int lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
3097 				if (info->slpflag & PCATCH)
3098 					lkflags |= LK_PCATCH;
3099 				error = BUF_TIMELOCK(bp, lkflags, "nfsfsync",
3100 						     info->slptimeo);
3101 			}
3102 		} else {
3103 			error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT);
3104 		}
3105 		if (error == 0) {
3106 			KKASSERT(bp->b_vp == info->vp);
3107 
3108 			if ((bp->b_flags & B_DELWRI) == 0)
3109 				panic("nfs_fsync: not dirty");
3110 			if (bp->b_flags & B_NEEDCOMMIT) {
3111 				BUF_UNLOCK(bp);
3112 				crit_exit();
3113 				break;
3114 			}
3115 			bremfree(bp);
3116 
3117 			bp->b_flags |= B_ASYNC;
3118 			crit_exit();
3119 			VOP_BWRITE(bp->b_vp, bp);
3120 		} else {
3121 			crit_exit();
3122 			error = 0;
3123 		}
3124 		break;
3125 	case NFI_COMMIT:
3126 		/*
3127 		 * Only process buffers in need of a commit which we can
3128 		 * immediately lock.  This may prevent a buffer from being
3129 		 * committed, but the normal flush loop will block on the
3130 		 * same buffer so we shouldn't get into an endless loop.
3131 		 */
3132 		crit_enter();
3133 		if ((bp->b_flags & (B_DELWRI | B_NEEDCOMMIT)) !=
3134 		    (B_DELWRI | B_NEEDCOMMIT) ||
3135 		    BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
3136 			crit_exit();
3137 			break;
3138 		}
3139 
3140 		KKASSERT(bp->b_vp == info->vp);
3141 		bremfree(bp);
3142 
3143 		/*
3144 		 * NOTE: we are not clearing B_DONE here, so we have
3145 		 * to do it later on in this routine if we intend to
3146 		 * initiate I/O on the bp.
3147 		 *
3148 		 * Note: to avoid loopback deadlocks, we do not
3149 		 * assign b_runningbufspace.
3150 		 */
3151 		vfs_busy_pages(bp, 1);
3152 
3153 		info->bvary[info->bvsize] = bp;
3154 		toff = ((u_quad_t)bp->b_bio2.bio_blkno) * DEV_BSIZE +
3155 			bp->b_dirtyoff;
3156 		if (info->bvsize == 0 || toff < info->beg_off)
3157 			info->beg_off = toff;
3158 		toff += (u_quad_t)(bp->b_dirtyend - bp->b_dirtyoff);
3159 		if (info->bvsize == 0 || toff > info->end_off)
3160 			info->end_off = toff;
3161 		++info->bvsize;
3162 		if (info->bvsize == NFS_COMMITBVECSIZ) {
3163 			error = nfs_flush_docommit(info, 0);
3164 			KKASSERT(info->bvsize == 0);
3165 		}
3166 		crit_exit();
3167 	}
3168 	return (error);
3169 }
3170 
3171 static
3172 int
3173 nfs_flush_docommit(struct nfs_flush_info *info, int error)
3174 {
3175 	struct vnode *vp;
3176 	struct buf *bp;
3177 	off_t bytes;
3178 	int retv;
3179 	int i;
3180 
3181 	vp = info->vp;
3182 
3183 	if (info->bvsize > 0) {
3184 		/*
3185 		 * Commit data on the server, as required.  Note that
3186 		 * nfs_commit will use the vnode's cred for the commit.
3187 		 * The NFSv3 commit RPC is limited to a 32 bit byte count.
3188 		 */
3189 		bytes = info->end_off - info->beg_off;
3190 		if (bytes > 0x40000000)
3191 			bytes = 0x40000000;
3192 		if (error) {
3193 			retv = -error;
3194 		} else {
3195 			retv = nfs_commit(vp, info->beg_off,
3196 					    (int)bytes, info->td);
3197 			if (retv == NFSERR_STALEWRITEVERF)
3198 				nfs_clearcommit(vp->v_mount);
3199 		}
3200 
3201 		/*
3202 		 * Now, either mark the blocks I/O done or mark the
3203 		 * blocks dirty, depending on whether the commit
3204 		 * succeeded.
3205 		 */
3206 		for (i = 0; i < info->bvsize; ++i) {
3207 			bp = info->bvary[i];
3208 			bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
3209 			if (retv) {
3210 				/*
3211 				 * Error, leave B_DELWRI intact
3212 				 */
3213 				vfs_unbusy_pages(bp);
3214 				brelse(bp);
3215 			} else {
3216 				/*
3217 				 * Success, remove B_DELWRI ( bundirty() ).
3218 				 *
3219 				 * b_dirtyoff/b_dirtyend seem to be NFS
3220 				 * specific.  We should probably move that
3221 				 * into bundirty(). XXX
3222 				 *
3223 				 * We are faking an I/O write, we have to
3224 				 * start the transaction in order to
3225 				 * immediately biodone() it.
3226 				 */
3227 				crit_enter();
3228 				bp->b_flags |= B_ASYNC;
3229 				bundirty(bp);
3230 				bp->b_flags &= ~(B_READ|B_DONE|B_ERROR);
3231 				bp->b_dirtyoff = bp->b_dirtyend = 0;
3232 				crit_exit();
3233 				biodone(&bp->b_bio1);
3234 			}
3235 		}
3236 		info->bvsize = 0;
3237 	}
3238 	return (error);
3239 }
3240 
3241 /*
3242  * NFS advisory byte-level locks.
3243  * Currently unsupported.
3244  *
3245  * nfs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl,
3246  *		int a_flags)
3247  */
3248 static int
3249 nfs_advlock(struct vop_advlock_args *ap)
3250 {
3251 	struct nfsnode *np = VTONFS(ap->a_vp);
3252 
3253 	/*
3254 	 * The following kludge is to allow diskless support to work
3255 	 * until a real NFS lockd is implemented. Basically, just pretend
3256 	 * that this is a local lock.
3257 	 */
3258 	return (lf_advlock(ap, &(np->n_lockf), np->n_size));
3259 }
3260 
3261 /*
3262  * Print out the contents of an nfsnode.
3263  *
3264  * nfs_print(struct vnode *a_vp)
3265  */
3266 static int
3267 nfs_print(struct vop_print_args *ap)
3268 {
3269 	struct vnode *vp = ap->a_vp;
3270 	struct nfsnode *np = VTONFS(vp);
3271 
3272 	printf("tag VT_NFS, fileid %ld fsid 0x%x",
3273 		np->n_vattr.va_fileid, np->n_vattr.va_fsid);
3274 	if (vp->v_type == VFIFO)
3275 		fifo_printinfo(vp);
3276 	printf("\n");
3277 	return (0);
3278 }
3279 
3280 /*
3281  * Just call nfs_writebp() with the force argument set to 1.
3282  *
3283  * NOTE: B_DONE may or may not be set in a_bp on call.
3284  *
3285  * nfs_bwrite(struct vnode *a_bp)
3286  */
3287 static int
3288 nfs_bwrite(struct vop_bwrite_args *ap)
3289 {
3290 	return (nfs_writebp(ap->a_bp, 1, curthread));
3291 }
3292 
3293 /*
3294  * This is a clone of vn_bwrite(), except that it also handles the
3295  * B_NEEDCOMMIT flag.  We set B_CACHE if this is a VMIO buffer.
3296  */
3297 int
3298 nfs_writebp(struct buf *bp, int force, struct thread *td)
3299 {
3300 	int error;
3301 
3302 	if (BUF_REFCNT(bp) == 0)
3303 		panic("bwrite: buffer is not locked???");
3304 
3305 	if (bp->b_flags & B_INVAL) {
3306 		brelse(bp);
3307 		return(0);
3308 	}
3309 
3310 	bp->b_flags |= B_CACHE;
3311 
3312 	/*
3313 	 * Undirty the bp.  We will redirty it later if the I/O fails.
3314 	 */
3315 	crit_enter();
3316 	bundirty(bp);
3317 	bp->b_flags &= ~(B_READ|B_DONE|B_ERROR);
3318 	crit_exit();
3319 
3320 	/*
3321 	 * Note: to avoid loopback deadlocks, we do not
3322 	 * assign b_runningbufspace.
3323 	 */
3324 	vfs_busy_pages(bp, 1);
3325 	BUF_KERNPROC(bp);
3326 
3327 	if (bp->b_flags & B_ASYNC) {
3328 		vn_strategy(bp->b_vp, &bp->b_bio1);
3329 		error = 0;
3330 	} else {
3331 		vn_strategy(bp->b_vp, &bp->b_bio1);
3332 		error = biowait(bp);
3333 		brelse(bp);
3334 	}
3335 	return (error);
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  * nfsspec_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
3344  *		  struct thread *a_td)
3345  */
3346 static int
3347 nfsspec_access(struct vop_access_args *ap)
3348 {
3349 	struct vattr *vap;
3350 	gid_t *gp;
3351 	struct ucred *cred = ap->a_cred;
3352 	struct vnode *vp = ap->a_vp;
3353 	mode_t mode = ap->a_mode;
3354 	struct vattr vattr;
3355 	int i;
3356 	int error;
3357 
3358 	/*
3359 	 * Disallow write attempts on filesystems mounted read-only;
3360 	 * unless the file is a socket, fifo, or a block or character
3361 	 * device resident on the filesystem.
3362 	 */
3363 	if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3364 		switch (vp->v_type) {
3365 		case VREG:
3366 		case VDIR:
3367 		case VLNK:
3368 			return (EROFS);
3369 		default:
3370 			break;
3371 		}
3372 	}
3373 	/*
3374 	 * If you're the super-user,
3375 	 * you always get access.
3376 	 */
3377 	if (cred->cr_uid == 0)
3378 		return (0);
3379 	vap = &vattr;
3380 	error = VOP_GETATTR(vp, vap, ap->a_td);
3381 	if (error)
3382 		return (error);
3383 	/*
3384 	 * Access check is based on only one of owner, group, public.
3385 	 * If not owner, then check group. If not a member of the
3386 	 * group, then check public access.
3387 	 */
3388 	if (cred->cr_uid != vap->va_uid) {
3389 		mode >>= 3;
3390 		gp = cred->cr_groups;
3391 		for (i = 0; i < cred->cr_ngroups; i++, gp++)
3392 			if (vap->va_gid == *gp)
3393 				goto found;
3394 		mode >>= 3;
3395 found:
3396 		;
3397 	}
3398 	error = (vap->va_mode & mode) == mode ? 0 : EACCES;
3399 	return (error);
3400 }
3401 
3402 /*
3403  * Read wrapper for special devices.
3404  *
3405  * nfsspec_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3406  *		struct ucred *a_cred)
3407  */
3408 static int
3409 nfsspec_read(struct vop_read_args *ap)
3410 {
3411 	struct nfsnode *np = VTONFS(ap->a_vp);
3412 
3413 	/*
3414 	 * Set access flag.
3415 	 */
3416 	np->n_flag |= NACC;
3417 	getnanotime(&np->n_atim);
3418 	return (VOCALL(spec_vnode_vops, &ap->a_head));
3419 }
3420 
3421 /*
3422  * Write wrapper for special devices.
3423  *
3424  * nfsspec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3425  *		 struct ucred *a_cred)
3426  */
3427 static int
3428 nfsspec_write(struct vop_write_args *ap)
3429 {
3430 	struct nfsnode *np = VTONFS(ap->a_vp);
3431 
3432 	/*
3433 	 * Set update flag.
3434 	 */
3435 	np->n_flag |= NUPD;
3436 	getnanotime(&np->n_mtim);
3437 	return (VOCALL(spec_vnode_vops, &ap->a_head));
3438 }
3439 
3440 /*
3441  * Close wrapper for special devices.
3442  *
3443  * Update the times on the nfsnode then do device close.
3444  *
3445  * nfsspec_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
3446  *		 struct thread *a_td)
3447  */
3448 static int
3449 nfsspec_close(struct vop_close_args *ap)
3450 {
3451 	struct vnode *vp = ap->a_vp;
3452 	struct nfsnode *np = VTONFS(vp);
3453 	struct vattr vattr;
3454 
3455 	if (np->n_flag & (NACC | NUPD)) {
3456 		np->n_flag |= NCHG;
3457 		if (vp->v_usecount == 1 &&
3458 		    (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3459 			VATTR_NULL(&vattr);
3460 			if (np->n_flag & NACC)
3461 				vattr.va_atime = np->n_atim;
3462 			if (np->n_flag & NUPD)
3463 				vattr.va_mtime = np->n_mtim;
3464 			(void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE), ap->a_td);
3465 		}
3466 	}
3467 	return (VOCALL(spec_vnode_vops, &ap->a_head));
3468 }
3469 
3470 /*
3471  * Read wrapper for fifos.
3472  *
3473  * nfsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3474  *		struct ucred *a_cred)
3475  */
3476 static int
3477 nfsfifo_read(struct vop_read_args *ap)
3478 {
3479 	struct nfsnode *np = VTONFS(ap->a_vp);
3480 
3481 	/*
3482 	 * Set access flag.
3483 	 */
3484 	np->n_flag |= NACC;
3485 	getnanotime(&np->n_atim);
3486 	return (VOCALL(fifo_vnode_vops, &ap->a_head));
3487 }
3488 
3489 /*
3490  * Write wrapper for fifos.
3491  *
3492  * nfsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
3493  *		 struct ucred *a_cred)
3494  */
3495 static int
3496 nfsfifo_write(struct vop_write_args *ap)
3497 {
3498 	struct nfsnode *np = VTONFS(ap->a_vp);
3499 
3500 	/*
3501 	 * Set update flag.
3502 	 */
3503 	np->n_flag |= NUPD;
3504 	getnanotime(&np->n_mtim);
3505 	return (VOCALL(fifo_vnode_vops, &ap->a_head));
3506 }
3507 
3508 /*
3509  * Close wrapper for fifos.
3510  *
3511  * Update the times on the nfsnode then do fifo close.
3512  *
3513  * nfsfifo_close(struct vnode *a_vp, int a_fflag, struct thread *a_td)
3514  */
3515 static int
3516 nfsfifo_close(struct vop_close_args *ap)
3517 {
3518 	struct vnode *vp = ap->a_vp;
3519 	struct nfsnode *np = VTONFS(vp);
3520 	struct vattr vattr;
3521 	struct timespec ts;
3522 
3523 	if (np->n_flag & (NACC | NUPD)) {
3524 		getnanotime(&ts);
3525 		if (np->n_flag & NACC)
3526 			np->n_atim = ts;
3527 		if (np->n_flag & NUPD)
3528 			np->n_mtim = ts;
3529 		np->n_flag |= NCHG;
3530 		if (vp->v_usecount == 1 &&
3531 		    (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
3532 			VATTR_NULL(&vattr);
3533 			if (np->n_flag & NACC)
3534 				vattr.va_atime = np->n_atim;
3535 			if (np->n_flag & NUPD)
3536 				vattr.va_mtime = np->n_mtim;
3537 			(void)VOP_SETATTR(vp, &vattr, nfs_vpcred(vp, ND_WRITE), ap->a_td);
3538 		}
3539 	}
3540 	return (VOCALL(fifo_vnode_vops, &ap->a_head));
3541 }
3542 
3543