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