xref: /freebsd/sys/fs/nfsclient/nfs_clport.c (revision 734e82fe33aa764367791a7d603b383996c6b40b)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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  */
35 
36 #include <sys/cdefs.h>
37 #include "opt_inet.h"
38 #include "opt_inet6.h"
39 
40 #include <sys/capsicum.h>
41 
42 /*
43  * generally, I don't like #includes inside .h files, but it seems to
44  * be the easiest way to handle the port.
45  */
46 #include <sys/fail.h>
47 #include <sys/hash.h>
48 #include <sys/sysctl.h>
49 #include <fs/nfs/nfsport.h>
50 #include <netinet/in_fib.h>
51 #include <netinet/if_ether.h>
52 #include <netinet6/ip6_var.h>
53 #include <net/if_types.h>
54 #include <net/route/nhop.h>
55 
56 #include <fs/nfsclient/nfs_kdtrace.h>
57 
58 #ifdef KDTRACE_HOOKS
59 dtrace_nfsclient_attrcache_flush_probe_func_t
60 		dtrace_nfscl_attrcache_flush_done_probe;
61 uint32_t	nfscl_attrcache_flush_done_id;
62 
63 dtrace_nfsclient_attrcache_get_hit_probe_func_t
64 		dtrace_nfscl_attrcache_get_hit_probe;
65 uint32_t	nfscl_attrcache_get_hit_id;
66 
67 dtrace_nfsclient_attrcache_get_miss_probe_func_t
68 		dtrace_nfscl_attrcache_get_miss_probe;
69 uint32_t	nfscl_attrcache_get_miss_id;
70 
71 dtrace_nfsclient_attrcache_load_probe_func_t
72 		dtrace_nfscl_attrcache_load_done_probe;
73 uint32_t	nfscl_attrcache_load_done_id;
74 #endif /* !KDTRACE_HOOKS */
75 
76 extern u_int32_t newnfs_true, newnfs_false, newnfs_xdrneg1;
77 extern struct vop_vector newnfs_vnodeops;
78 extern struct vop_vector newnfs_fifoops;
79 extern uma_zone_t newnfsnode_zone;
80 extern struct buf_ops buf_ops_newnfs;
81 extern uma_zone_t ncl_pbuf_zone;
82 extern short nfsv4_cbport;
83 extern int nfscl_enablecallb;
84 extern int nfs_numnfscbd;
85 extern int nfscl_inited;
86 struct mtx ncl_iod_mutex;
87 NFSDLOCKMUTEX;
88 extern struct mtx nfsrv_dslock_mtx;
89 
90 extern void (*ncl_call_invalcaches)(struct vnode *);
91 
92 SYSCTL_DECL(_vfs_nfs);
93 static int ncl_fileid_maxwarnings = 10;
94 SYSCTL_INT(_vfs_nfs, OID_AUTO, fileid_maxwarnings, CTLFLAG_RWTUN,
95     &ncl_fileid_maxwarnings, 0,
96     "Limit fileid corruption warnings; 0 is off; -1 is unlimited");
97 static volatile int ncl_fileid_nwarnings;
98 
99 static void nfscl_warn_fileid(struct nfsmount *, struct nfsvattr *,
100     struct nfsvattr *);
101 
102 /*
103  * Comparison function for vfs_hash functions.
104  */
105 int
106 newnfs_vncmpf(struct vnode *vp, void *arg)
107 {
108 	struct nfsfh *nfhp = (struct nfsfh *)arg;
109 	struct nfsnode *np = VTONFS(vp);
110 
111 	if (np->n_fhp->nfh_len != nfhp->nfh_len ||
112 	    NFSBCMP(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len))
113 		return (1);
114 	return (0);
115 }
116 
117 /*
118  * Look up a vnode/nfsnode by file handle.
119  * Callers must check for mount points!!
120  * In all cases, a pointer to a
121  * nfsnode structure is returned.
122  * This variant takes a "struct nfsfh *" as second argument and uses
123  * that structure up, either by hanging off the nfsnode or FREEing it.
124  */
125 int
126 nfscl_nget(struct mount *mntp, struct vnode *dvp, struct nfsfh *nfhp,
127     struct componentname *cnp, struct thread *td, struct nfsnode **npp,
128     int lkflags)
129 {
130 	struct nfsnode *np, *dnp;
131 	struct vnode *vp, *nvp;
132 	struct nfsv4node *newd, *oldd;
133 	int error;
134 	u_int hash;
135 	struct nfsmount *nmp;
136 
137 	nmp = VFSTONFS(mntp);
138 	dnp = VTONFS(dvp);
139 	*npp = NULL;
140 
141 	/*
142 	 * If this is the mount point fh and NFSMNTP_FAKEROOT is set, replace
143 	 * it with the fake fh.
144 	 */
145 	if ((nmp->nm_privflag & NFSMNTP_FAKEROOTFH) != 0 &&
146 	    nmp->nm_fhsize > 0 && nmp->nm_fhsize == nfhp->nfh_len &&
147 	    !NFSBCMP(nmp->nm_fh, nfhp->nfh_fh, nmp->nm_fhsize)) {
148 		free(nfhp, M_NFSFH);
149 		nfhp = malloc(sizeof(struct nfsfh) + NFSX_FHMAX + 1,
150 		    M_NFSFH, M_WAITOK | M_ZERO);
151 		nfhp->nfh_len = NFSX_FHMAX + 1;
152 	}
153 
154 	hash = fnv_32_buf(nfhp->nfh_fh, nfhp->nfh_len, FNV1_32_INIT);
155 
156 	error = vfs_hash_get(mntp, hash, lkflags,
157 	    td, &nvp, newnfs_vncmpf, nfhp);
158 	if (error == 0 && nvp != NULL) {
159 		/*
160 		 * I believe there is a slight chance that vgonel() could
161 		 * get called on this vnode between when NFSVOPLOCK() drops
162 		 * the VI_LOCK() and vget() acquires it again, so that it
163 		 * hasn't yet had v_usecount incremented. If this were to
164 		 * happen, the VIRF_DOOMED flag would be set, so check for
165 		 * that here. Since we now have the v_usecount incremented,
166 		 * we should be ok until we vrele() it, if the VIRF_DOOMED
167 		 * flag isn't set now.
168 		 */
169 		VI_LOCK(nvp);
170 		if (VN_IS_DOOMED(nvp)) {
171 			VI_UNLOCK(nvp);
172 			vrele(nvp);
173 			error = ENOENT;
174 		} else {
175 			VI_UNLOCK(nvp);
176 		}
177 	}
178 	if (error) {
179 		free(nfhp, M_NFSFH);
180 		return (error);
181 	}
182 	if (nvp != NULL) {
183 		np = VTONFS(nvp);
184 		/*
185 		 * For NFSv4, check to see if it is the same name and
186 		 * replace the name, if it is different.
187 		 */
188 		oldd = newd = NULL;
189 		if ((nmp->nm_flag & NFSMNT_NFSV4) && np->n_v4 != NULL &&
190 		    nvp->v_type == VREG &&
191 		    (np->n_v4->n4_namelen != cnp->cn_namelen ||
192 		     NFSBCMP(cnp->cn_nameptr, NFS4NODENAME(np->n_v4),
193 		     cnp->cn_namelen) ||
194 		     dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen ||
195 		     NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
196 		     dnp->n_fhp->nfh_len))) {
197 		    newd = malloc(
198 			sizeof (struct nfsv4node) + dnp->n_fhp->nfh_len +
199 			+ cnp->cn_namelen - 1, M_NFSV4NODE, M_WAITOK);
200 		    NFSLOCKNODE(np);
201 		    if (newd != NULL && np->n_v4 != NULL && nvp->v_type == VREG
202 			&& (np->n_v4->n4_namelen != cnp->cn_namelen ||
203 			 NFSBCMP(cnp->cn_nameptr, NFS4NODENAME(np->n_v4),
204 			 cnp->cn_namelen) ||
205 			 dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen ||
206 			 NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
207 			 dnp->n_fhp->nfh_len))) {
208 			oldd = np->n_v4;
209 			np->n_v4 = newd;
210 			newd = NULL;
211 			np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
212 			np->n_v4->n4_namelen = cnp->cn_namelen;
213 			NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
214 			    dnp->n_fhp->nfh_len);
215 			NFSBCOPY(cnp->cn_nameptr, NFS4NODENAME(np->n_v4),
216 			    cnp->cn_namelen);
217 		    }
218 		    NFSUNLOCKNODE(np);
219 		}
220 		if (newd != NULL)
221 			free(newd, M_NFSV4NODE);
222 		if (oldd != NULL)
223 			free(oldd, M_NFSV4NODE);
224 		*npp = np;
225 		free(nfhp, M_NFSFH);
226 		return (0);
227 	}
228 	np = uma_zalloc(newnfsnode_zone, M_WAITOK | M_ZERO);
229 
230 	error = getnewvnode(nfs_vnode_tag, mntp, &newnfs_vnodeops, &nvp);
231 	if (error) {
232 		uma_zfree(newnfsnode_zone, np);
233 		free(nfhp, M_NFSFH);
234 		return (error);
235 	}
236 	vp = nvp;
237 	KASSERT(vp->v_bufobj.bo_bsize != 0, ("nfscl_nget: bo_bsize == 0"));
238 	vp->v_bufobj.bo_ops = &buf_ops_newnfs;
239 	vp->v_data = np;
240 	np->n_vnode = vp;
241 	/*
242 	 * Initialize the mutex even if the vnode is going to be a loser.
243 	 * This simplifies the logic in reclaim, which can then unconditionally
244 	 * destroy the mutex (in the case of the loser, or if hash_insert
245 	 * happened to return an error no special casing is needed).
246 	 */
247 	mtx_init(&np->n_mtx, "NEWNFSnode lock", NULL, MTX_DEF | MTX_DUPOK);
248 	lockinit(&np->n_excl, PVFS, "nfsupg", VLKTIMEOUT, LK_NOSHARE |
249 	    LK_CANRECURSE);
250 
251 	/*
252 	 * Are we getting the root? If so, make sure the vnode flags
253 	 * are correct
254 	 */
255 	if (nfhp->nfh_len == NFSX_FHMAX + 1 ||
256 	    (nfhp->nfh_len == nmp->nm_fhsize &&
257 	     !bcmp(nfhp->nfh_fh, nmp->nm_fh, nfhp->nfh_len))) {
258 		if (vp->v_type == VNON)
259 			vp->v_type = VDIR;
260 		vp->v_vflag |= VV_ROOT;
261 	}
262 
263 	vp->v_vflag |= VV_VMSIZEVNLOCK;
264 
265 	np->n_fhp = nfhp;
266 	/*
267 	 * For NFSv4, we have to attach the directory file handle and
268 	 * file name, so that Open Ops can be done later.
269 	 */
270 	if (nmp->nm_flag & NFSMNT_NFSV4) {
271 		np->n_v4 = malloc(sizeof (struct nfsv4node)
272 		    + dnp->n_fhp->nfh_len + cnp->cn_namelen - 1, M_NFSV4NODE,
273 		    M_WAITOK);
274 		np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
275 		np->n_v4->n4_namelen = cnp->cn_namelen;
276 		NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
277 		    dnp->n_fhp->nfh_len);
278 		NFSBCOPY(cnp->cn_nameptr, NFS4NODENAME(np->n_v4),
279 		    cnp->cn_namelen);
280 	} else {
281 		np->n_v4 = NULL;
282 	}
283 
284 	/*
285 	 * NFS supports recursive and shared locking.
286 	 */
287 	lockmgr(vp->v_vnlock, LK_EXCLUSIVE | LK_NOWITNESS, NULL);
288 	VN_LOCK_AREC(vp);
289 	VN_LOCK_ASHARE(vp);
290 	error = insmntque(vp, mntp);
291 	if (error != 0) {
292 		*npp = NULL;
293 		mtx_destroy(&np->n_mtx);
294 		lockdestroy(&np->n_excl);
295 		free(nfhp, M_NFSFH);
296 		if (np->n_v4 != NULL)
297 			free(np->n_v4, M_NFSV4NODE);
298 		uma_zfree(newnfsnode_zone, np);
299 		return (error);
300 	}
301 	vn_set_state(vp, VSTATE_CONSTRUCTED);
302 	error = vfs_hash_insert(vp, hash, lkflags,
303 	    td, &nvp, newnfs_vncmpf, nfhp);
304 	if (error)
305 		return (error);
306 	if (nvp != NULL) {
307 		*npp = VTONFS(nvp);
308 		/* vfs_hash_insert() vput()'s the losing vnode */
309 		return (0);
310 	}
311 	*npp = np;
312 
313 	return (0);
314 }
315 
316 /*
317  * Another variant of nfs_nget(). This one is only used by reopen. It
318  * takes almost the same args as nfs_nget(), but only succeeds if an entry
319  * exists in the cache. (Since files should already be "open" with a
320  * vnode ref cnt on the node when reopen calls this, it should always
321  * succeed.)
322  * Also, don't get a vnode lock, since it may already be locked by some
323  * other process that is handling it. This is ok, since all other threads
324  * on the client are blocked by the nfsc_lock being exclusively held by the
325  * caller of this function.
326  */
327 int
328 nfscl_ngetreopen(struct mount *mntp, u_int8_t *fhp, int fhsize,
329     struct thread *td, struct nfsnode **npp)
330 {
331 	struct vnode *nvp;
332 	u_int hash;
333 	struct nfsfh *nfhp;
334 	int error;
335 
336 	*npp = NULL;
337 	/* For forced dismounts, just return error. */
338 	if (NFSCL_FORCEDISM(mntp))
339 		return (EINTR);
340 	nfhp = malloc(sizeof (struct nfsfh) + fhsize,
341 	    M_NFSFH, M_WAITOK);
342 	bcopy(fhp, &nfhp->nfh_fh[0], fhsize);
343 	nfhp->nfh_len = fhsize;
344 
345 	hash = fnv_32_buf(fhp, fhsize, FNV1_32_INIT);
346 
347 	/*
348 	 * First, try to get the vnode locked, but don't block for the lock.
349 	 */
350 	error = vfs_hash_get(mntp, hash, (LK_EXCLUSIVE | LK_NOWAIT), td, &nvp,
351 	    newnfs_vncmpf, nfhp);
352 	if (error == 0 && nvp != NULL) {
353 		NFSVOPUNLOCK(nvp);
354 	} else if (error == EBUSY) {
355 		/*
356 		 * It is safe so long as a vflush() with
357 		 * FORCECLOSE has not been done. Since the Renew thread is
358 		 * stopped and the MNTK_UNMOUNTF flag is set before doing
359 		 * a vflush() with FORCECLOSE, we should be ok here.
360 		 */
361 		if (NFSCL_FORCEDISM(mntp))
362 			error = EINTR;
363 		else {
364 			vfs_hash_ref(mntp, hash, td, &nvp, newnfs_vncmpf, nfhp);
365 			if (nvp == NULL) {
366 				error = ENOENT;
367 			} else if (VN_IS_DOOMED(nvp)) {
368 				error = ENOENT;
369 				vrele(nvp);
370 			} else {
371 				error = 0;
372 			}
373 		}
374 	}
375 	free(nfhp, M_NFSFH);
376 	if (error)
377 		return (error);
378 	if (nvp != NULL) {
379 		*npp = VTONFS(nvp);
380 		return (0);
381 	}
382 	return (EINVAL);
383 }
384 
385 static void
386 nfscl_warn_fileid(struct nfsmount *nmp, struct nfsvattr *oldnap,
387     struct nfsvattr *newnap)
388 {
389 	int off;
390 
391 	if (ncl_fileid_maxwarnings >= 0 &&
392 	    ncl_fileid_nwarnings >= ncl_fileid_maxwarnings)
393 		return;
394 	off = 0;
395 	if (ncl_fileid_maxwarnings >= 0) {
396 		if (++ncl_fileid_nwarnings >= ncl_fileid_maxwarnings)
397 			off = 1;
398 	}
399 
400 	printf("newnfs: server '%s' error: fileid changed. "
401 	    "fsid %jx:%jx: expected fileid %#jx, got %#jx. "
402 	    "(BROKEN NFS SERVER OR MIDDLEWARE)\n",
403 	    nmp->nm_com.nmcom_hostname,
404 	    (uintmax_t)nmp->nm_fsid[0],
405 	    (uintmax_t)nmp->nm_fsid[1],
406 	    (uintmax_t)oldnap->na_fileid,
407 	    (uintmax_t)newnap->na_fileid);
408 
409 	if (off)
410 		printf("newnfs: Logged %d times about fileid corruption; "
411 		    "going quiet to avoid spamming logs excessively. (Limit "
412 		    "is: %d).\n", ncl_fileid_nwarnings,
413 		    ncl_fileid_maxwarnings);
414 }
415 
416 void
417 ncl_copy_vattr(struct vattr *dst, struct vattr *src)
418 {
419 	dst->va_type = src->va_type;
420 	dst->va_mode = src->va_mode;
421 	dst->va_nlink = src->va_nlink;
422 	dst->va_uid = src->va_uid;
423 	dst->va_gid = src->va_gid;
424 	dst->va_fsid = src->va_fsid;
425 	dst->va_fileid = src->va_fileid;
426 	dst->va_size = src->va_size;
427 	dst->va_blocksize = src->va_blocksize;
428 	dst->va_atime = src->va_atime;
429 	dst->va_mtime = src->va_mtime;
430 	dst->va_ctime = src->va_ctime;
431 	dst->va_birthtime = src->va_birthtime;
432 	dst->va_gen = src->va_gen;
433 	dst->va_flags = src->va_flags;
434 	dst->va_rdev = src->va_rdev;
435 	dst->va_bytes = src->va_bytes;
436 	dst->va_filerev = src->va_filerev;
437 }
438 
439 /*
440  * Load the attribute cache (that lives in the nfsnode entry) with
441  * the attributes of the second argument and
442  * Iff vaper not NULL
443  *    copy the attributes to *vaper
444  * Similar to nfs_loadattrcache(), except the attributes are passed in
445  * instead of being parsed out of the mbuf list.
446  */
447 int
448 nfscl_loadattrcache(struct vnode **vpp, struct nfsvattr *nap, void *nvaper,
449     int writeattr, int dontshrink)
450 {
451 	struct vnode *vp = *vpp;
452 	struct vattr *vap, *nvap = &nap->na_vattr, *vaper = nvaper;
453 	struct nfsnode *np;
454 	struct nfsmount *nmp;
455 	struct timespec mtime_save;
456 	int error, force_fid_err;
457 	dev_t topfsid;
458 
459 	error = 0;
460 
461 	/*
462 	 * If v_type == VNON it is a new node, so fill in the v_type,
463 	 * n_mtime fields. Check to see if it represents a special
464 	 * device, and if so, check for a possible alias. Once the
465 	 * correct vnode has been obtained, fill in the rest of the
466 	 * information.
467 	 */
468 	np = VTONFS(vp);
469 	NFSLOCKNODE(np);
470 	if (vp->v_type != nvap->va_type) {
471 		vp->v_type = nvap->va_type;
472 		if (vp->v_type == VFIFO)
473 			vp->v_op = &newnfs_fifoops;
474 		np->n_mtime = nvap->va_mtime;
475 	}
476 	nmp = VFSTONFS(vp->v_mount);
477 	vap = &np->n_vattr.na_vattr;
478 	mtime_save = vap->va_mtime;
479 	if (writeattr) {
480 		np->n_vattr.na_filerev = nap->na_filerev;
481 		np->n_vattr.na_size = nap->na_size;
482 		np->n_vattr.na_mtime = nap->na_mtime;
483 		np->n_vattr.na_ctime = nap->na_ctime;
484 		np->n_vattr.na_btime = nap->na_btime;
485 		np->n_vattr.na_fsid = nap->na_fsid;
486 		np->n_vattr.na_mode = nap->na_mode;
487 	} else {
488 		force_fid_err = 0;
489 		KFAIL_POINT_ERROR(DEBUG_FP, nfscl_force_fileid_warning,
490 		    force_fid_err);
491 		/*
492 		 * BROKEN NFS SERVER OR MIDDLEWARE
493 		 *
494 		 * Certain NFS servers (certain old proprietary filers ca.
495 		 * 2006) or broken middleboxes (e.g. WAN accelerator products)
496 		 * will respond to GETATTR requests with results for a
497 		 * different fileid.
498 		 *
499 		 * The WAN accelerator we've observed not only serves stale
500 		 * cache results for a given file, it also occasionally serves
501 		 * results for wholly different files.  This causes surprising
502 		 * problems; for example the cached size attribute of a file
503 		 * may truncate down and then back up, resulting in zero
504 		 * regions in file contents read by applications.  We observed
505 		 * this reliably with Clang and .c files during parallel build.
506 		 * A pcap revealed packet fragmentation and GETATTR RPC
507 		 * responses with wholly wrong fileids.
508 		 * For the case where the file handle is a fake one
509 		 * generated via the "syskrb5" mount option and
510 		 * the old fileid is 2, ignore the test, since this might
511 		 * be replacing the fake attributes with correct ones.
512 		 */
513 		if ((np->n_vattr.na_fileid != 0 &&
514 		     np->n_vattr.na_fileid != nap->na_fileid &&
515 		     (np->n_vattr.na_fileid != 2 || !NFSHASSYSKRB5(nmp) ||
516 		      np->n_fhp->nfh_len != NFSX_FHMAX + 1)) ||
517 		    force_fid_err) {
518 			nfscl_warn_fileid(nmp, &np->n_vattr, nap);
519 			error = EIDRM;
520 			goto out;
521 		}
522 		NFSBCOPY((caddr_t)nap, (caddr_t)&np->n_vattr,
523 		    sizeof (struct nfsvattr));
524 	}
525 
526 	/*
527 	 * For NFSv4, the server's export may be a tree of file systems
528 	 * where a fileno is a unique value within each file system.
529 	 * na_filesid[0,1] uniquely identify the server file system
530 	 * and nm_fsid[0,1] is the value for the root file system mounted.
531 	 * As such, the value of va_fsid generated by vn_fsid() represents
532 	 * the root file system on the server and a different value for
533 	 * va_fsid is needed for the other server file systems.  This
534 	 * va_fsid is ideally unique for all of the server file systems,
535 	 * so a 64bit hash on na_filesid[0,1] is calculated.
536 	 * Although highly unlikely that the fnv_64_hash() will be
537 	 * the same as the root, test for this case and recalculate the hash.
538 	 */
539 	vn_fsid(vp, vap);
540 	if (NFSHASNFSV4(nmp) && NFSHASHASSETFSID(nmp) &&
541 	    (nmp->nm_fsid[0] != np->n_vattr.na_filesid[0] ||
542 	     nmp->nm_fsid[1] != np->n_vattr.na_filesid[1])) {
543 		topfsid = vap->va_fsid;
544 		vap->va_fsid = FNV1_64_INIT;
545 		do {
546 			vap->va_fsid = fnv_64_buf(np->n_vattr.na_filesid,
547 			    sizeof(np->n_vattr.na_filesid), vap->va_fsid);
548 		} while (vap->va_fsid == topfsid);
549 	}
550 
551 	np->n_attrstamp = time_second;
552 	if (vap->va_size != np->n_size) {
553 		if (vap->va_type == VREG) {
554 			if (dontshrink && vap->va_size < np->n_size) {
555 				/*
556 				 * We've been told not to shrink the file;
557 				 * zero np->n_attrstamp to indicate that
558 				 * the attributes are stale.
559 				 */
560 				vap->va_size = np->n_size;
561 				np->n_attrstamp = 0;
562 				KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
563 			} else if (np->n_flag & NMODIFIED) {
564 				/*
565 				 * We've modified the file: Use the larger
566 				 * of our size, and the server's size.
567 				 */
568 				if (vap->va_size < np->n_size) {
569 					vap->va_size = np->n_size;
570 				} else {
571 					np->n_size = vap->va_size;
572 					np->n_flag |= NSIZECHANGED;
573 				}
574 			} else {
575 				np->n_size = vap->va_size;
576 				np->n_flag |= NSIZECHANGED;
577 			}
578 		} else {
579 			np->n_size = vap->va_size;
580 		}
581 	}
582 	/*
583 	 * The following checks are added to prevent a race between (say)
584 	 * a READDIR+ and a WRITE.
585 	 * READDIR+, WRITE requests sent out.
586 	 * READDIR+ resp, WRITE resp received on client.
587 	 * However, the WRITE resp was handled before the READDIR+ resp
588 	 * causing the post op attrs from the write to be loaded first
589 	 * and the attrs from the READDIR+ to be loaded later. If this
590 	 * happens, we have stale attrs loaded into the attrcache.
591 	 * We detect this by for the mtime moving back. We invalidate the
592 	 * attrcache when this happens.
593 	 */
594 	if (timespeccmp(&mtime_save, &vap->va_mtime, >)) {
595 		/* Size changed or mtime went backwards */
596 		np->n_attrstamp = 0;
597 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
598 	}
599 	if (vaper != NULL) {
600 		ncl_copy_vattr(vaper, vap);
601 		if (np->n_flag & NCHG) {
602 			if (np->n_flag & NACC)
603 				vaper->va_atime = np->n_atim;
604 			if (np->n_flag & NUPD)
605 				vaper->va_mtime = np->n_mtim;
606 		}
607 	}
608 
609 out:
610 #ifdef KDTRACE_HOOKS
611 	if (np->n_attrstamp != 0)
612 		KDTRACE_NFS_ATTRCACHE_LOAD_DONE(vp, vap, error);
613 #endif
614 	(void)ncl_pager_setsize(vp, NULL);
615 	return (error);
616 }
617 
618 /*
619  * Call vnode_pager_setsize() if the size of the node changed, as
620  * recorded in nfsnode vs. v_object, or delay the call if notifying
621  * the pager is not possible at the moment.
622  *
623  * If nsizep is non-NULL, the call is delayed and the new node size is
624  * provided.  Caller should itself call vnode_pager_setsize() if
625  * function returned true.  If nsizep is NULL, function tries to call
626  * vnode_pager_setsize() itself if needed and possible, and the nfs
627  * node is unlocked unconditionally, the return value is not useful.
628  */
629 bool
630 ncl_pager_setsize(struct vnode *vp, u_quad_t *nsizep)
631 {
632 	struct nfsnode *np;
633 	vm_object_t object;
634 	struct vattr *vap;
635 	u_quad_t nsize;
636 	bool setnsize;
637 
638 	np = VTONFS(vp);
639 	NFSASSERTNODE(np);
640 
641 	vap = &np->n_vattr.na_vattr;
642 	nsize = vap->va_size;
643 	object = vp->v_object;
644 	setnsize = false;
645 
646 	if (object != NULL && nsize != object->un_pager.vnp.vnp_size) {
647 		if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE &&
648 		    (curthread->td_pflags2 & TDP2_SBPAGES) == 0)
649 			setnsize = true;
650 		else
651 			np->n_flag |= NVNSETSZSKIP;
652 	}
653 	if (nsizep == NULL) {
654 		NFSUNLOCKNODE(np);
655 		if (setnsize)
656 			vnode_pager_setsize(vp, nsize);
657 		setnsize = false;
658 	} else {
659 		*nsizep = nsize;
660 	}
661 	return (setnsize);
662 }
663 
664 /*
665  * Fill in the client id name. For these bytes:
666  * 1 - they must be unique
667  * 2 - they should be persistent across client reboots
668  * 1 is more critical than 2
669  * Use the mount point's unique id plus either the uuid or, if that
670  * isn't set, random junk.
671  */
672 void
673 nfscl_fillclid(u_int64_t clval, char *uuid, u_int8_t *cp, u_int16_t idlen)
674 {
675 	int uuidlen;
676 
677 	/*
678 	 * First, put in the 64bit mount point identifier.
679 	 */
680 	if (idlen >= sizeof (u_int64_t)) {
681 		NFSBCOPY((caddr_t)&clval, cp, sizeof (u_int64_t));
682 		cp += sizeof (u_int64_t);
683 		idlen -= sizeof (u_int64_t);
684 	}
685 
686 	/*
687 	 * If uuid is non-zero length, use it.
688 	 */
689 	uuidlen = strlen(uuid);
690 	if (uuidlen > 0 && idlen >= uuidlen) {
691 		NFSBCOPY(uuid, cp, uuidlen);
692 		cp += uuidlen;
693 		idlen -= uuidlen;
694 	}
695 
696 	/*
697 	 * This only normally happens if the uuid isn't set.
698 	 */
699 	while (idlen > 0) {
700 		*cp++ = (u_int8_t)(arc4random() % 256);
701 		idlen--;
702 	}
703 }
704 
705 /*
706  * Fill in a lock owner name. For now, pid + the process's creation time.
707  */
708 void
709 nfscl_filllockowner(void *id, u_int8_t *cp, int flags)
710 {
711 	union {
712 		u_int32_t	lval;
713 		u_int8_t	cval[4];
714 	} tl;
715 	struct proc *p;
716 
717 	if (id == NULL) {
718 		/* Return the single open_owner of all 0 bytes. */
719 		bzero(cp, NFSV4CL_LOCKNAMELEN);
720 		return;
721 	}
722 	if ((flags & F_POSIX) != 0) {
723 		p = (struct proc *)id;
724 		tl.lval = p->p_pid;
725 		*cp++ = tl.cval[0];
726 		*cp++ = tl.cval[1];
727 		*cp++ = tl.cval[2];
728 		*cp++ = tl.cval[3];
729 		tl.lval = p->p_stats->p_start.tv_sec;
730 		*cp++ = tl.cval[0];
731 		*cp++ = tl.cval[1];
732 		*cp++ = tl.cval[2];
733 		*cp++ = tl.cval[3];
734 		tl.lval = p->p_stats->p_start.tv_usec;
735 		*cp++ = tl.cval[0];
736 		*cp++ = tl.cval[1];
737 		*cp++ = tl.cval[2];
738 		*cp = tl.cval[3];
739 	} else if ((flags & F_FLOCK) != 0) {
740 		bcopy(&id, cp, sizeof(id));
741 		bzero(&cp[sizeof(id)], NFSV4CL_LOCKNAMELEN - sizeof(id));
742 	} else {
743 		printf("nfscl_filllockowner: not F_POSIX or F_FLOCK\n");
744 		bzero(cp, NFSV4CL_LOCKNAMELEN);
745 	}
746 }
747 
748 /*
749  * Find the parent process for the thread passed in as an argument.
750  * If none exists, return NULL, otherwise return a thread for the parent.
751  * (Can be any of the threads, since it is only used for td->td_proc.)
752  */
753 NFSPROC_T *
754 nfscl_getparent(struct thread *td)
755 {
756 	struct proc *p;
757 	struct thread *ptd;
758 
759 	if (td == NULL)
760 		return (NULL);
761 	p = td->td_proc;
762 	if (p->p_pid == 0)
763 		return (NULL);
764 	p = p->p_pptr;
765 	if (p == NULL)
766 		return (NULL);
767 	ptd = TAILQ_FIRST(&p->p_threads);
768 	return (ptd);
769 }
770 
771 /*
772  * Start up the renew kernel thread.
773  */
774 static void
775 start_nfscl(void *arg)
776 {
777 	struct nfsclclient *clp;
778 	struct thread *td;
779 
780 	clp = (struct nfsclclient *)arg;
781 	td = TAILQ_FIRST(&clp->nfsc_renewthread->p_threads);
782 	nfscl_renewthread(clp, td);
783 	kproc_exit(0);
784 }
785 
786 void
787 nfscl_start_renewthread(struct nfsclclient *clp)
788 {
789 
790 	kproc_create(start_nfscl, (void *)clp, &clp->nfsc_renewthread, 0, 0,
791 	    "nfscl");
792 }
793 
794 /*
795  * Handle wcc_data.
796  * For NFSv4, it assumes that nfsv4_wccattr() was used to set up the getattr
797  * as the first Op after PutFH.
798  * (For NFSv4, the postop attributes are after the Op, so they can't be
799  *  parsed here. A separate call to nfscl_postop_attr() is required.)
800  */
801 int
802 nfscl_wcc_data(struct nfsrv_descript *nd, struct vnode *vp,
803     struct nfsvattr *nap, int *flagp, int *wccflagp, uint64_t *repsizep)
804 {
805 	u_int32_t *tl;
806 	struct nfsnode *np = VTONFS(vp);
807 	struct nfsvattr nfsva;
808 	int error = 0;
809 
810 	if (wccflagp != NULL)
811 		*wccflagp = 0;
812 	if (nd->nd_flag & ND_NFSV3) {
813 		*flagp = 0;
814 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
815 		if (*tl == newnfs_true) {
816 			NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
817 			if (wccflagp != NULL) {
818 				NFSLOCKNODE(np);
819 				*wccflagp = (np->n_mtime.tv_sec ==
820 				    fxdr_unsigned(u_int32_t, *(tl + 2)) &&
821 				    np->n_mtime.tv_nsec ==
822 				    fxdr_unsigned(u_int32_t, *(tl + 3)));
823 				NFSUNLOCKNODE(np);
824 			}
825 		}
826 		error = nfscl_postop_attr(nd, nap, flagp);
827 		if (wccflagp != NULL && *flagp == 0)
828 			*wccflagp = 0;
829 	} else if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR))
830 	    == (ND_NFSV4 | ND_V4WCCATTR)) {
831 		error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
832 		    NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
833 		    NULL, NULL, NULL, NULL, NULL);
834 		if (error)
835 			return (error);
836 		/*
837 		 * Get rid of Op# and status for next op.
838 		 */
839 		NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
840 		if (*++tl)
841 			nd->nd_flag |= ND_NOMOREDATA;
842 		if (repsizep != NULL)
843 			*repsizep = nfsva.na_size;
844 		if (wccflagp != NULL &&
845 		    nfsva.na_vattr.va_mtime.tv_sec != 0) {
846 			NFSLOCKNODE(np);
847 			*wccflagp = (np->n_mtime.tv_sec ==
848 			    nfsva.na_vattr.va_mtime.tv_sec &&
849 			    np->n_mtime.tv_nsec ==
850 			    nfsva.na_vattr.va_mtime.tv_sec);
851 			NFSUNLOCKNODE(np);
852 		}
853 	}
854 nfsmout:
855 	return (error);
856 }
857 
858 /*
859  * Get postop attributes.
860  */
861 int
862 nfscl_postop_attr(struct nfsrv_descript *nd, struct nfsvattr *nap, int *retp)
863 {
864 	u_int32_t *tl;
865 	int error = 0;
866 
867 	*retp = 0;
868 	if (nd->nd_flag & ND_NOMOREDATA)
869 		return (error);
870 	if (nd->nd_flag & ND_NFSV3) {
871 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
872 		*retp = fxdr_unsigned(int, *tl);
873 	} else if (nd->nd_flag & ND_NFSV4) {
874 		/*
875 		 * For NFSv4, the postop attr are at the end, so no point
876 		 * in looking if nd_repstat != 0.
877 		 */
878 		if (!nd->nd_repstat) {
879 			NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
880 			if (*(tl + 1))
881 				/* should never happen since nd_repstat != 0 */
882 				nd->nd_flag |= ND_NOMOREDATA;
883 			else
884 				*retp = 1;
885 		}
886 	} else if (!nd->nd_repstat) {
887 		/* For NFSv2, the attributes are here iff nd_repstat == 0 */
888 		*retp = 1;
889 	}
890 	if (*retp) {
891 		error = nfsm_loadattr(nd, nap);
892 		if (error)
893 			*retp = 0;
894 	}
895 nfsmout:
896 	return (error);
897 }
898 
899 /*
900  * nfscl_request() - mostly a wrapper for newnfs_request().
901  */
902 int
903 nfscl_request(struct nfsrv_descript *nd, struct vnode *vp, NFSPROC_T *p,
904     struct ucred *cred)
905 {
906 	int ret, vers;
907 	struct nfsmount *nmp;
908 
909 	nmp = VFSTONFS(vp->v_mount);
910 	if (nd->nd_flag & ND_NFSV4)
911 		vers = NFS_VER4;
912 	else if (nd->nd_flag & ND_NFSV3)
913 		vers = NFS_VER3;
914 	else
915 		vers = NFS_VER2;
916 	ret = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
917 		NFS_PROG, vers, NULL, 1, NULL, NULL);
918 	return (ret);
919 }
920 
921 /*
922  * fill in this bsden's variant of statfs using nfsstatfs.
923  */
924 void
925 nfscl_loadsbinfo(struct nfsmount *nmp, struct nfsstatfs *sfp, void *statfs)
926 {
927 	struct statfs *sbp = (struct statfs *)statfs;
928 
929 	if (nmp->nm_flag & (NFSMNT_NFSV3 | NFSMNT_NFSV4)) {
930 		sbp->f_bsize = NFS_FABLKSIZE;
931 		sbp->f_blocks = sfp->sf_tbytes / NFS_FABLKSIZE;
932 		sbp->f_bfree = sfp->sf_fbytes / NFS_FABLKSIZE;
933 		/*
934 		 * Although sf_abytes is uint64_t and f_bavail is int64_t,
935 		 * the value after dividing by NFS_FABLKSIZE is small
936 		 * enough that it will fit in 63bits, so it is ok to
937 		 * assign it to f_bavail without fear that it will become
938 		 * negative.
939 		 */
940 		sbp->f_bavail = sfp->sf_abytes / NFS_FABLKSIZE;
941 		sbp->f_files = sfp->sf_tfiles;
942 		/* Since f_ffree is int64_t, clip it to 63bits. */
943 		if (sfp->sf_ffiles > INT64_MAX)
944 			sbp->f_ffree = INT64_MAX;
945 		else
946 			sbp->f_ffree = sfp->sf_ffiles;
947 	} else if ((nmp->nm_flag & NFSMNT_NFSV4) == 0) {
948 		/*
949 		 * The type casts to (int32_t) ensure that this code is
950 		 * compatible with the old NFS client, in that it will
951 		 * propagate bit31 to the high order bits. This may or may
952 		 * not be correct for NFSv2, but since it is a legacy
953 		 * environment, I'd rather retain backwards compatibility.
954 		 */
955 		sbp->f_bsize = (int32_t)sfp->sf_bsize;
956 		sbp->f_blocks = (int32_t)sfp->sf_blocks;
957 		sbp->f_bfree = (int32_t)sfp->sf_bfree;
958 		sbp->f_bavail = (int32_t)sfp->sf_bavail;
959 		sbp->f_files = 0;
960 		sbp->f_ffree = 0;
961 	}
962 }
963 
964 /*
965  * Use the fsinfo stuff to update the mount point.
966  */
967 void
968 nfscl_loadfsinfo(struct nfsmount *nmp, struct nfsfsinfo *fsp)
969 {
970 
971 	if ((nmp->nm_wsize == 0 || fsp->fs_wtpref < nmp->nm_wsize) &&
972 	    fsp->fs_wtpref >= NFS_FABLKSIZE)
973 		nmp->nm_wsize = (fsp->fs_wtpref + NFS_FABLKSIZE - 1) &
974 		    ~(NFS_FABLKSIZE - 1);
975 	if (fsp->fs_wtmax < nmp->nm_wsize && fsp->fs_wtmax > 0) {
976 		nmp->nm_wsize = fsp->fs_wtmax & ~(NFS_FABLKSIZE - 1);
977 		if (nmp->nm_wsize == 0)
978 			nmp->nm_wsize = fsp->fs_wtmax;
979 	}
980 	if (nmp->nm_wsize < NFS_FABLKSIZE)
981 		nmp->nm_wsize = NFS_FABLKSIZE;
982 	if ((nmp->nm_rsize == 0 || fsp->fs_rtpref < nmp->nm_rsize) &&
983 	    fsp->fs_rtpref >= NFS_FABLKSIZE)
984 		nmp->nm_rsize = (fsp->fs_rtpref + NFS_FABLKSIZE - 1) &
985 		    ~(NFS_FABLKSIZE - 1);
986 	if (fsp->fs_rtmax < nmp->nm_rsize && fsp->fs_rtmax > 0) {
987 		nmp->nm_rsize = fsp->fs_rtmax & ~(NFS_FABLKSIZE - 1);
988 		if (nmp->nm_rsize == 0)
989 			nmp->nm_rsize = fsp->fs_rtmax;
990 	}
991 	if (nmp->nm_rsize < NFS_FABLKSIZE)
992 		nmp->nm_rsize = NFS_FABLKSIZE;
993 	if ((nmp->nm_readdirsize == 0 || fsp->fs_dtpref < nmp->nm_readdirsize)
994 	    && fsp->fs_dtpref >= NFS_DIRBLKSIZ)
995 		nmp->nm_readdirsize = (fsp->fs_dtpref + NFS_DIRBLKSIZ - 1) &
996 		    ~(NFS_DIRBLKSIZ - 1);
997 	if (fsp->fs_rtmax < nmp->nm_readdirsize && fsp->fs_rtmax > 0) {
998 		nmp->nm_readdirsize = fsp->fs_rtmax & ~(NFS_DIRBLKSIZ - 1);
999 		if (nmp->nm_readdirsize == 0)
1000 			nmp->nm_readdirsize = fsp->fs_rtmax;
1001 	}
1002 	if (nmp->nm_readdirsize < NFS_DIRBLKSIZ)
1003 		nmp->nm_readdirsize = NFS_DIRBLKSIZ;
1004 	if (fsp->fs_maxfilesize > 0 &&
1005 	    fsp->fs_maxfilesize < nmp->nm_maxfilesize)
1006 		nmp->nm_maxfilesize = fsp->fs_maxfilesize;
1007 	nmp->nm_mountp->mnt_stat.f_iosize = newnfs_iosize(nmp);
1008 	nmp->nm_state |= NFSSTA_GOTFSINFO;
1009 }
1010 
1011 /*
1012  * Lookups source address which should be used to communicate with
1013  * @nmp and stores it inside @pdst.
1014  *
1015  * Returns 0 on success.
1016  */
1017 u_int8_t *
1018 nfscl_getmyip(struct nfsmount *nmp, struct in6_addr *paddr, int *isinet6p)
1019 {
1020 #if defined(INET6) || defined(INET)
1021 	int fibnum;
1022 
1023 	fibnum = curthread->td_proc->p_fibnum;
1024 #endif
1025 #ifdef INET
1026 	if (nmp->nm_nam->sa_family == AF_INET) {
1027 		struct epoch_tracker et;
1028 		struct nhop_object *nh;
1029 		struct sockaddr_in *sin;
1030 		struct in_addr addr = {};
1031 
1032 		sin = (struct sockaddr_in *)nmp->nm_nam;
1033 		NET_EPOCH_ENTER(et);
1034 		CURVNET_SET(CRED_TO_VNET(nmp->nm_sockreq.nr_cred));
1035 		nh = fib4_lookup(fibnum, sin->sin_addr, 0, NHR_NONE, 0);
1036 		if (nh != NULL) {
1037 			addr = IA_SIN(ifatoia(nh->nh_ifa))->sin_addr;
1038 			if (IN_LOOPBACK(ntohl(addr.s_addr))) {
1039 				/* Ignore loopback addresses */
1040 				nh = NULL;
1041 			}
1042 		}
1043 		CURVNET_RESTORE();
1044 		NET_EPOCH_EXIT(et);
1045 
1046 		if (nh == NULL)
1047 			return (NULL);
1048 		*isinet6p = 0;
1049 		*((struct in_addr *)paddr) = addr;
1050 
1051 		return (u_int8_t *)paddr;
1052 	}
1053 #endif
1054 #ifdef INET6
1055 	if (nmp->nm_nam->sa_family == AF_INET6) {
1056 		struct epoch_tracker et;
1057 		struct sockaddr_in6 *sin6;
1058 		int error;
1059 
1060 		sin6 = (struct sockaddr_in6 *)nmp->nm_nam;
1061 
1062 		NET_EPOCH_ENTER(et);
1063 		CURVNET_SET(CRED_TO_VNET(nmp->nm_sockreq.nr_cred));
1064 		error = in6_selectsrc_addr(fibnum, &sin6->sin6_addr,
1065 		    sin6->sin6_scope_id, NULL, paddr, NULL);
1066 		CURVNET_RESTORE();
1067 		NET_EPOCH_EXIT(et);
1068 		if (error != 0)
1069 			return (NULL);
1070 
1071 		if (IN6_IS_ADDR_LOOPBACK(paddr))
1072 			return (NULL);
1073 
1074 		/* Scope is embedded in */
1075 		*isinet6p = 1;
1076 
1077 		return (u_int8_t *)paddr;
1078 	}
1079 #endif
1080 	return (NULL);
1081 }
1082 
1083 /*
1084  * Copy NFS uid, gids from the cred structure.
1085  */
1086 void
1087 newnfs_copyincred(struct ucred *cr, struct nfscred *nfscr)
1088 {
1089 	int i;
1090 
1091 	KASSERT(cr->cr_ngroups >= 0,
1092 	    ("newnfs_copyincred: negative cr_ngroups"));
1093 	nfscr->nfsc_uid = cr->cr_uid;
1094 	nfscr->nfsc_ngroups = MIN(cr->cr_ngroups, NFS_MAXGRPS + 1);
1095 	for (i = 0; i < nfscr->nfsc_ngroups; i++)
1096 		nfscr->nfsc_groups[i] = cr->cr_groups[i];
1097 }
1098 
1099 /*
1100  * Do any client specific initialization.
1101  */
1102 void
1103 nfscl_init(void)
1104 {
1105 	static int inited = 0;
1106 
1107 	if (inited)
1108 		return;
1109 	inited = 1;
1110 	nfscl_inited = 1;
1111 	ncl_pbuf_zone = pbuf_zsecond_create("nfspbuf", nswbuf / 2);
1112 }
1113 
1114 /*
1115  * Check each of the attributes to be set, to ensure they aren't already
1116  * the correct value. Disable setting ones already correct.
1117  */
1118 int
1119 nfscl_checksattr(struct vattr *vap, struct nfsvattr *nvap)
1120 {
1121 
1122 	if (vap->va_mode != (mode_t)VNOVAL) {
1123 		if (vap->va_mode == nvap->na_mode)
1124 			vap->va_mode = (mode_t)VNOVAL;
1125 	}
1126 	if (vap->va_uid != (uid_t)VNOVAL) {
1127 		if (vap->va_uid == nvap->na_uid)
1128 			vap->va_uid = (uid_t)VNOVAL;
1129 	}
1130 	if (vap->va_gid != (gid_t)VNOVAL) {
1131 		if (vap->va_gid == nvap->na_gid)
1132 			vap->va_gid = (gid_t)VNOVAL;
1133 	}
1134 	if (vap->va_size != VNOVAL) {
1135 		if (vap->va_size == nvap->na_size)
1136 			vap->va_size = VNOVAL;
1137 	}
1138 
1139 	/*
1140 	 * We are normally called with only a partially initialized
1141 	 * VAP.  Since the NFSv3 spec says that server may use the
1142 	 * file attributes to store the verifier, the spec requires
1143 	 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1144 	 * in atime, but we can't really assume that all servers will
1145 	 * so we ensure that our SETATTR sets both atime and mtime.
1146 	 * Set the VA_UTIMES_NULL flag for this case, so that
1147 	 * the server's time will be used.  This is needed to
1148 	 * work around a bug in some Solaris servers, where
1149 	 * setting the time TOCLIENT causes the Setattr RPC
1150 	 * to return NFS_OK, but not set va_mode.
1151 	 */
1152 	if (vap->va_mtime.tv_sec == VNOVAL) {
1153 		vfs_timestamp(&vap->va_mtime);
1154 		vap->va_vaflags |= VA_UTIMES_NULL;
1155 	}
1156 	if (vap->va_atime.tv_sec == VNOVAL)
1157 		vap->va_atime = vap->va_mtime;
1158 	return (1);
1159 }
1160 
1161 /*
1162  * Map nfsv4 errors to errno.h errors.
1163  * The uid and gid arguments are only used for NFSERR_BADOWNER and that
1164  * error should only be returned for the Open, Create and Setattr Ops.
1165  * As such, most calls can just pass in 0 for those arguments.
1166  */
1167 int
1168 nfscl_maperr(struct thread *td, int error, uid_t uid, gid_t gid)
1169 {
1170 	struct proc *p;
1171 
1172 	if (error < 10000 || error >= NFSERR_STALEWRITEVERF)
1173 		return (error);
1174 	if (td != NULL)
1175 		p = td->td_proc;
1176 	else
1177 		p = NULL;
1178 	switch (error) {
1179 	case NFSERR_BADOWNER:
1180 		tprintf(p, LOG_INFO,
1181 		    "No name and/or group mapping for uid,gid:(%d,%d)\n",
1182 		    uid, gid);
1183 		return (EPERM);
1184 	case NFSERR_BADNAME:
1185 	case NFSERR_BADCHAR:
1186 		printf("nfsv4 char/name not handled by server\n");
1187 		return (ENOENT);
1188 	case NFSERR_STALECLIENTID:
1189 	case NFSERR_STALESTATEID:
1190 	case NFSERR_EXPIRED:
1191 	case NFSERR_BADSTATEID:
1192 	case NFSERR_BADSESSION:
1193 		printf("nfsv4 recover err returned %d\n", error);
1194 		return (EIO);
1195 	case NFSERR_BADHANDLE:
1196 	case NFSERR_SERVERFAULT:
1197 	case NFSERR_BADTYPE:
1198 	case NFSERR_FHEXPIRED:
1199 	case NFSERR_RESOURCE:
1200 	case NFSERR_MOVED:
1201 	case NFSERR_MINORVERMISMATCH:
1202 	case NFSERR_OLDSTATEID:
1203 	case NFSERR_BADSEQID:
1204 	case NFSERR_LEASEMOVED:
1205 	case NFSERR_RECLAIMBAD:
1206 	case NFSERR_BADXDR:
1207 	case NFSERR_OPILLEGAL:
1208 		printf("nfsv4 client/server protocol prob err=%d\n",
1209 		    error);
1210 		return (EIO);
1211 	case NFSERR_NOFILEHANDLE:
1212 		printf("nfsv4 no file handle: usually means the file "
1213 		    "system is not exported on the NFSv4 server\n");
1214 		return (EIO);
1215 	case NFSERR_WRONGSEC:
1216 		tprintf(p, LOG_INFO, "NFSv4 error WrongSec: You probably need a"
1217 		    " Kerberos TGT\n");
1218 		return (EIO);
1219 	default:
1220 		tprintf(p, LOG_INFO, "nfsv4 err=%d\n", error);
1221 		return (EIO);
1222 	};
1223 }
1224 
1225 /*
1226  * Check to see if the process for this owner exists. Return 1 if it doesn't
1227  * and 0 otherwise.
1228  */
1229 int
1230 nfscl_procdoesntexist(u_int8_t *own)
1231 {
1232 	union {
1233 		u_int32_t	lval;
1234 		u_int8_t	cval[4];
1235 	} tl;
1236 	struct proc *p;
1237 	pid_t pid;
1238 	int i, ret = 0;
1239 
1240 	/* For the single open_owner of all 0 bytes, just return 0. */
1241 	for (i = 0; i < NFSV4CL_LOCKNAMELEN; i++)
1242 		if (own[i] != 0)
1243 			break;
1244 	if (i == NFSV4CL_LOCKNAMELEN)
1245 		return (0);
1246 
1247 	tl.cval[0] = *own++;
1248 	tl.cval[1] = *own++;
1249 	tl.cval[2] = *own++;
1250 	tl.cval[3] = *own++;
1251 	pid = tl.lval;
1252 	p = pfind_any_locked(pid);
1253 	if (p == NULL)
1254 		return (1);
1255 	if (p->p_stats == NULL) {
1256 		PROC_UNLOCK(p);
1257 		return (0);
1258 	}
1259 	tl.cval[0] = *own++;
1260 	tl.cval[1] = *own++;
1261 	tl.cval[2] = *own++;
1262 	tl.cval[3] = *own++;
1263 	if (tl.lval != p->p_stats->p_start.tv_sec) {
1264 		ret = 1;
1265 	} else {
1266 		tl.cval[0] = *own++;
1267 		tl.cval[1] = *own++;
1268 		tl.cval[2] = *own++;
1269 		tl.cval[3] = *own;
1270 		if (tl.lval != p->p_stats->p_start.tv_usec)
1271 			ret = 1;
1272 	}
1273 	PROC_UNLOCK(p);
1274 	return (ret);
1275 }
1276 
1277 /*
1278  * - nfs pseudo system call for the client
1279  */
1280 /*
1281  * MPSAFE
1282  */
1283 static int
1284 nfssvc_nfscl(struct thread *td, struct nfssvc_args *uap)
1285 {
1286 	struct file *fp;
1287 	struct nfscbd_args nfscbdarg;
1288 	struct nfsd_nfscbd_args nfscbdarg2;
1289 	struct nameidata nd;
1290 	struct nfscl_dumpmntopts dumpmntopts;
1291 	cap_rights_t rights;
1292 	char *buf;
1293 	int error;
1294 	struct mount *mp;
1295 	struct nfsmount *nmp;
1296 
1297 	NFSD_CURVNET_SET(NFSD_TD_TO_VNET(td));
1298 	if (uap->flag & NFSSVC_CBADDSOCK) {
1299 		error = copyin(uap->argp, (caddr_t)&nfscbdarg, sizeof(nfscbdarg));
1300 		if (error)
1301 			goto out;
1302 		/*
1303 		 * Since we don't know what rights might be required,
1304 		 * pretend that we need them all. It is better to be too
1305 		 * careful than too reckless.
1306 		 */
1307 		error = fget(td, nfscbdarg.sock,
1308 		    cap_rights_init_one(&rights, CAP_SOCK_CLIENT), &fp);
1309 		if (error)
1310 			goto out;
1311 		if (fp->f_type != DTYPE_SOCKET) {
1312 			fdrop(fp, td);
1313 			error = EPERM;
1314 			goto out;
1315 		}
1316 		error = nfscbd_addsock(fp);
1317 		fdrop(fp, td);
1318 		if (!error && nfscl_enablecallb == 0) {
1319 			nfsv4_cbport = nfscbdarg.port;
1320 			nfscl_enablecallb = 1;
1321 		}
1322 	} else if (uap->flag & NFSSVC_NFSCBD) {
1323 		if (uap->argp == NULL) {
1324 			error = EINVAL;
1325 			goto out;
1326 		}
1327 		error = copyin(uap->argp, (caddr_t)&nfscbdarg2,
1328 		    sizeof(nfscbdarg2));
1329 		if (error)
1330 			goto out;
1331 		error = nfscbd_nfsd(td, &nfscbdarg2);
1332 	} else if (uap->flag & NFSSVC_DUMPMNTOPTS) {
1333 		error = copyin(uap->argp, &dumpmntopts, sizeof(dumpmntopts));
1334 		if (error == 0 && (dumpmntopts.ndmnt_blen < 256 ||
1335 		    dumpmntopts.ndmnt_blen > 1024))
1336 			error = EINVAL;
1337 		if (error == 0)
1338 			error = nfsrv_lookupfilename(&nd,
1339 			    dumpmntopts.ndmnt_fname, td);
1340 		if (error == 0 && strcmp(nd.ni_vp->v_mount->mnt_vfc->vfc_name,
1341 		    "nfs") != 0) {
1342 			vput(nd.ni_vp);
1343 			error = EINVAL;
1344 		}
1345 		if (error == 0) {
1346 			buf = malloc(dumpmntopts.ndmnt_blen, M_TEMP, M_WAITOK |
1347 			    M_ZERO);
1348 			nfscl_retopts(VFSTONFS(nd.ni_vp->v_mount), buf,
1349 			    dumpmntopts.ndmnt_blen);
1350 			vput(nd.ni_vp);
1351 			error = copyout(buf, dumpmntopts.ndmnt_buf,
1352 			    dumpmntopts.ndmnt_blen);
1353 			free(buf, M_TEMP);
1354 		}
1355 	} else if (uap->flag & NFSSVC_FORCEDISM) {
1356 		buf = malloc(MNAMELEN + 1, M_TEMP, M_WAITOK);
1357 		error = copyinstr(uap->argp, buf, MNAMELEN + 1, NULL);
1358 		if (error == 0) {
1359 			nmp = NULL;
1360 			mtx_lock(&mountlist_mtx);
1361 			TAILQ_FOREACH(mp, &mountlist, mnt_list) {
1362 				if (strcmp(mp->mnt_stat.f_mntonname, buf) ==
1363 				    0 && strcmp(mp->mnt_stat.f_fstypename,
1364 				    "nfs") == 0 && mp->mnt_data != NULL) {
1365 					nmp = VFSTONFS(mp);
1366 					NFSDDSLOCK();
1367 					if (nfsv4_findmirror(nmp) != NULL) {
1368 						NFSDDSUNLOCK();
1369 						error = ENXIO;
1370 						nmp = NULL;
1371 						break;
1372 					}
1373 					mtx_lock(&nmp->nm_mtx);
1374 					if ((nmp->nm_privflag &
1375 					    NFSMNTP_FORCEDISM) == 0) {
1376 						nmp->nm_privflag |=
1377 						   (NFSMNTP_FORCEDISM |
1378 						    NFSMNTP_CANCELRPCS);
1379 						mtx_unlock(&nmp->nm_mtx);
1380 					} else {
1381 						mtx_unlock(&nmp->nm_mtx);
1382 						nmp = NULL;
1383 					}
1384 					NFSDDSUNLOCK();
1385 					break;
1386 				}
1387 			}
1388 			mtx_unlock(&mountlist_mtx);
1389 
1390 			if (nmp != NULL) {
1391 				/*
1392 				 * Call newnfs_nmcancelreqs() to cause
1393 				 * any RPCs in progress on the mount point to
1394 				 * fail.
1395 				 * This will cause any process waiting for an
1396 				 * RPC to complete while holding a vnode lock
1397 				 * on the mounted-on vnode (such as "df" or
1398 				 * a non-forced "umount") to fail.
1399 				 * This will unlock the mounted-on vnode so
1400 				 * a forced dismount can succeed.
1401 				 * Then clear NFSMNTP_CANCELRPCS and wakeup(),
1402 				 * so that nfs_unmount() can complete.
1403 				 */
1404 				newnfs_nmcancelreqs(nmp);
1405 				mtx_lock(&nmp->nm_mtx);
1406 				nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
1407 				wakeup(nmp);
1408 				mtx_unlock(&nmp->nm_mtx);
1409 			} else if (error == 0)
1410 				error = EINVAL;
1411 		}
1412 		free(buf, M_TEMP);
1413 	} else {
1414 		error = EINVAL;
1415 	}
1416 out:
1417 	NFSD_CURVNET_RESTORE();
1418 	return (error);
1419 }
1420 
1421 extern int (*nfsd_call_nfscl)(struct thread *, struct nfssvc_args *);
1422 
1423 /*
1424  * Called once to initialize data structures...
1425  */
1426 static int
1427 nfscl_modevent(module_t mod, int type, void *data)
1428 {
1429 	int error = 0;
1430 	static int loaded = 0;
1431 
1432 	switch (type) {
1433 	case MOD_LOAD:
1434 		if (loaded)
1435 			return (0);
1436 		newnfs_portinit();
1437 		mtx_init(&ncl_iod_mutex, "ncl_iod_mutex", NULL, MTX_DEF);
1438 		nfscl_init();
1439 		NFSD_LOCK();
1440 		nfsrvd_cbinit(0);
1441 		NFSD_UNLOCK();
1442 		ncl_call_invalcaches = ncl_invalcaches;
1443 		nfsd_call_nfscl = nfssvc_nfscl;
1444 		loaded = 1;
1445 		break;
1446 
1447 	case MOD_UNLOAD:
1448 		if (nfs_numnfscbd != 0) {
1449 			error = EBUSY;
1450 			break;
1451 		}
1452 
1453 		/*
1454 		 * XXX: Unloading of nfscl module is unsupported.
1455 		 */
1456 #if 0
1457 		ncl_call_invalcaches = NULL;
1458 		nfsd_call_nfscl = NULL;
1459 		uma_zdestroy(ncl_pbuf_zone);
1460 		/* and get rid of the mutexes */
1461 		mtx_destroy(&ncl_iod_mutex);
1462 		loaded = 0;
1463 		break;
1464 #else
1465 		/* FALLTHROUGH */
1466 #endif
1467 	default:
1468 		error = EOPNOTSUPP;
1469 		break;
1470 	}
1471 	return error;
1472 }
1473 static moduledata_t nfscl_mod = {
1474 	"nfscl",
1475 	nfscl_modevent,
1476 	NULL,
1477 };
1478 /*
1479  * This is the main module declaration for the NFS client.  The
1480  * nfscl_modevent() function is needed to ensure that the module
1481  * cannot be unloaded, among other things.
1482  * There is also a module declaration in sys/fs/nfsclient/nfs_clvfsops.c
1483  * for the name "nfs" within the VFS_SET() macro that defines the "nfs"
1484  * file system type.
1485  */
1486 DECLARE_MODULE(nfscl, nfscl_mod, SI_SUB_VFS, SI_ORDER_FIRST);
1487 
1488 /* So that loader and kldload(2) can find us, wherever we are.. */
1489 MODULE_VERSION(nfscl, 1);
1490 MODULE_DEPEND(nfscl, nfscommon, 1, 1, 1);
1491 MODULE_DEPEND(nfscl, krpc, 1, 1, 1);
1492 MODULE_DEPEND(nfscl, nfssvc, 1, 1, 1);
1493 MODULE_DEPEND(nfscl, xdr, 1, 1, 1);
1494