xref: /freebsd/sys/fs/nfsclient/nfs_clport.c (revision a3cf0ef5a295c885c895fabfd56470c0d1db322d)
1 /*-
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Rick Macklem at The University of Guelph.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 /*
38  * generally, I don't like #includes inside .h files, but it seems to
39  * be the easiest way to handle the port.
40  */
41 #include <fs/nfs/nfsport.h>
42 #include <netinet/if_ether.h>
43 #include <net/if_types.h>
44 
45 extern u_int32_t newnfs_true, newnfs_false, newnfs_xdrneg1;
46 extern struct vop_vector newnfs_vnodeops;
47 extern struct vop_vector newnfs_fifoops;
48 extern uma_zone_t newnfsnode_zone;
49 extern struct buf_ops buf_ops_newnfs;
50 extern int ncl_pbuf_freecnt;
51 extern short nfsv4_cbport;
52 extern int nfscl_enablecallb;
53 extern int nfs_numnfscbd;
54 extern int nfscl_inited;
55 struct mtx nfs_clstate_mutex;
56 struct mtx ncl_iod_mutex;
57 NFSDLOCKMUTEX;
58 
59 extern void (*ncl_call_invalcaches)(struct vnode *);
60 
61 /*
62  * Comparison function for vfs_hash functions.
63  */
64 int
65 newnfs_vncmpf(struct vnode *vp, void *arg)
66 {
67 	struct nfsfh *nfhp = (struct nfsfh *)arg;
68 	struct nfsnode *np = VTONFS(vp);
69 
70 	if (np->n_fhp->nfh_len != nfhp->nfh_len ||
71 	    NFSBCMP(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len))
72 		return (1);
73 	return (0);
74 }
75 
76 /*
77  * Look up a vnode/nfsnode by file handle.
78  * Callers must check for mount points!!
79  * In all cases, a pointer to a
80  * nfsnode structure is returned.
81  * This variant takes a "struct nfsfh *" as second argument and uses
82  * that structure up, either by hanging off the nfsnode or FREEing it.
83  */
84 int
85 nfscl_nget(struct mount *mntp, struct vnode *dvp, struct nfsfh *nfhp,
86     struct componentname *cnp, struct thread *td, struct nfsnode **npp,
87     void *stuff)
88 {
89 	struct nfsnode *np, *dnp;
90 	struct vnode *vp, *nvp;
91 	struct nfsv4node *newd, *oldd;
92 	int error;
93 	u_int hash;
94 	struct nfsmount *nmp;
95 
96 	nmp = VFSTONFS(mntp);
97 	dnp = VTONFS(dvp);
98 	*npp = NULL;
99 
100 	hash = fnv_32_buf(nfhp->nfh_fh, nfhp->nfh_len, FNV1_32_INIT);
101 
102 	error = vfs_hash_get(mntp, hash, LK_EXCLUSIVE,
103 	    td, &nvp, newnfs_vncmpf, nfhp);
104 	if (error == 0 && nvp != NULL) {
105 		/*
106 		 * I believe there is a slight chance that vgonel() could
107 		 * get called on this vnode between when vn_lock() drops
108 		 * the VI_LOCK() and vget() acquires it again, so that it
109 		 * hasn't yet had v_usecount incremented. If this were to
110 		 * happen, the VI_DOOMED flag would be set, so check for
111 		 * that here. Since we now have the v_usecount incremented,
112 		 * we should be ok until we vrele() it, if the VI_DOOMED
113 		 * flag isn't set now.
114 		 */
115 		VI_LOCK(nvp);
116 		if ((nvp->v_iflag & VI_DOOMED)) {
117 			VI_UNLOCK(nvp);
118 			vrele(nvp);
119 			error = ENOENT;
120 		} else {
121 			VI_UNLOCK(nvp);
122 		}
123 	}
124 	if (error) {
125 		FREE((caddr_t)nfhp, M_NFSFH);
126 		return (error);
127 	}
128 	if (nvp != NULL) {
129 		np = VTONFS(nvp);
130 		/*
131 		 * For NFSv4, check to see if it is the same name and
132 		 * replace the name, if it is different.
133 		 */
134 		oldd = newd = NULL;
135 		if ((nmp->nm_flag & NFSMNT_NFSV4) && np->n_v4 != NULL &&
136 		    nvp->v_type == VREG &&
137 		    (np->n_v4->n4_namelen != cnp->cn_namelen ||
138 		     NFSBCMP(cnp->cn_nameptr, NFS4NODENAME(np->n_v4),
139 		     cnp->cn_namelen) ||
140 		     dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen ||
141 		     NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
142 		     dnp->n_fhp->nfh_len))) {
143 		    MALLOC(newd, struct nfsv4node *,
144 			sizeof (struct nfsv4node) + dnp->n_fhp->nfh_len +
145 			+ cnp->cn_namelen - 1, M_NFSV4NODE, M_WAITOK);
146 		    NFSLOCKNODE(np);
147 		    if (newd != NULL && np->n_v4 != NULL && nvp->v_type == VREG
148 			&& (np->n_v4->n4_namelen != cnp->cn_namelen ||
149 			 NFSBCMP(cnp->cn_nameptr, NFS4NODENAME(np->n_v4),
150 			 cnp->cn_namelen) ||
151 			 dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen ||
152 			 NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
153 			 dnp->n_fhp->nfh_len))) {
154 			oldd = np->n_v4;
155 			np->n_v4 = newd;
156 			newd = NULL;
157 			np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
158 			np->n_v4->n4_namelen = cnp->cn_namelen;
159 			NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
160 			    dnp->n_fhp->nfh_len);
161 			NFSBCOPY(cnp->cn_nameptr, NFS4NODENAME(np->n_v4),
162 			    cnp->cn_namelen);
163 		    }
164 		    NFSUNLOCKNODE(np);
165 		}
166 		if (newd != NULL)
167 			FREE((caddr_t)newd, M_NFSV4NODE);
168 		if (oldd != NULL)
169 			FREE((caddr_t)oldd, M_NFSV4NODE);
170 		*npp = np;
171 		FREE((caddr_t)nfhp, M_NFSFH);
172 		return (0);
173 	}
174 
175 	/*
176 	 * Allocate before getnewvnode since doing so afterward
177 	 * might cause a bogus v_data pointer to get dereferenced
178 	 * elsewhere if zalloc should block.
179 	 */
180 	np = uma_zalloc(newnfsnode_zone, M_WAITOK | M_ZERO);
181 
182 	error = getnewvnode("newnfs", mntp, &newnfs_vnodeops, &nvp);
183 	if (error) {
184 		uma_zfree(newnfsnode_zone, np);
185 		FREE((caddr_t)nfhp, M_NFSFH);
186 		return (error);
187 	}
188 	vp = nvp;
189 	vp->v_bufobj.bo_ops = &buf_ops_newnfs;
190 	vp->v_data = np;
191 	np->n_vnode = vp;
192 	/*
193 	 * Initialize the mutex even if the vnode is going to be a loser.
194 	 * This simplifies the logic in reclaim, which can then unconditionally
195 	 * destroy the mutex (in the case of the loser, or if hash_insert
196 	 * happened to return an error no special casing is needed).
197 	 */
198 	mtx_init(&np->n_mtx, "NEWNFSnode lock", NULL, MTX_DEF | MTX_DUPOK);
199 
200 	/*
201 	 * Are we getting the root? If so, make sure the vnode flags
202 	 * are correct
203 	 */
204 	if ((nfhp->nfh_len == nmp->nm_fhsize) &&
205 	    !bcmp(nfhp->nfh_fh, nmp->nm_fh, nfhp->nfh_len)) {
206 		if (vp->v_type == VNON)
207 			vp->v_type = VDIR;
208 		vp->v_vflag |= VV_ROOT;
209 	}
210 
211 	np->n_fhp = nfhp;
212 	/*
213 	 * For NFSv4, we have to attach the directory file handle and
214 	 * file name, so that Open Ops can be done later.
215 	 */
216 	if (nmp->nm_flag & NFSMNT_NFSV4) {
217 		MALLOC(np->n_v4, struct nfsv4node *, sizeof (struct nfsv4node)
218 		    + dnp->n_fhp->nfh_len + cnp->cn_namelen - 1, M_NFSV4NODE,
219 		    M_WAITOK);
220 		np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
221 		np->n_v4->n4_namelen = cnp->cn_namelen;
222 		NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
223 		    dnp->n_fhp->nfh_len);
224 		NFSBCOPY(cnp->cn_nameptr, NFS4NODENAME(np->n_v4),
225 		    cnp->cn_namelen);
226 	} else {
227 		np->n_v4 = NULL;
228 	}
229 
230 	/*
231 	 * NFS supports recursive and shared locking.
232 	 */
233 	lockmgr(vp->v_vnlock, LK_EXCLUSIVE | LK_NOWITNESS, NULL);
234 	VN_LOCK_AREC(vp);
235 	VN_LOCK_ASHARE(vp);
236 	error = insmntque(vp, mntp);
237 	if (error != 0) {
238 		*npp = NULL;
239 		mtx_destroy(&np->n_mtx);
240 		FREE((caddr_t)nfhp, M_NFSFH);
241 		if (np->n_v4 != NULL)
242 			FREE((caddr_t)np->n_v4, M_NFSV4NODE);
243 		uma_zfree(newnfsnode_zone, np);
244 		return (error);
245 	}
246 	error = vfs_hash_insert(vp, hash, LK_EXCLUSIVE,
247 	    td, &nvp, newnfs_vncmpf, nfhp);
248 	if (error)
249 		return (error);
250 	if (nvp != NULL) {
251 		*npp = VTONFS(nvp);
252 		/* vfs_hash_insert() vput()'s the losing vnode */
253 		return (0);
254 	}
255 	*npp = np;
256 
257 	return (0);
258 }
259 
260 /*
261  * Anothe variant of nfs_nget(). This one is only used by reopen. It
262  * takes almost the same args as nfs_nget(), but only succeeds if an entry
263  * exists in the cache. (Since files should already be "open" with a
264  * vnode ref cnt on the node when reopen calls this, it should always
265  * succeed.)
266  * Also, don't get a vnode lock, since it may already be locked by some
267  * other process that is handling it. This is ok, since all other threads
268  * on the client are blocked by the nfsc_lock being exclusively held by the
269  * caller of this function.
270  */
271 int
272 nfscl_ngetreopen(struct mount *mntp, u_int8_t *fhp, int fhsize,
273     struct thread *td, struct nfsnode **npp)
274 {
275 	struct vnode *nvp;
276 	u_int hash;
277 	struct nfsfh *nfhp;
278 	int error;
279 
280 	*npp = NULL;
281 	/* For forced dismounts, just return error. */
282 	if ((mntp->mnt_kern_flag & MNTK_UNMOUNTF))
283 		return (EINTR);
284 	MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) + fhsize,
285 	    M_NFSFH, M_WAITOK);
286 	bcopy(fhp, &nfhp->nfh_fh[0], fhsize);
287 	nfhp->nfh_len = fhsize;
288 
289 	hash = fnv_32_buf(fhp, fhsize, FNV1_32_INIT);
290 
291 	/*
292 	 * First, try to get the vnode locked, but don't block for the lock.
293 	 */
294 	error = vfs_hash_get(mntp, hash, (LK_EXCLUSIVE | LK_NOWAIT), td, &nvp,
295 	    newnfs_vncmpf, nfhp);
296 	if (error == 0 && nvp != NULL) {
297 		VOP_UNLOCK(nvp, 0);
298 	} else if (error == EBUSY) {
299 		/*
300 		 * The LK_EXCLOTHER lock type tells nfs_lock1() to not try
301 		 * and lock the vnode, but just get a v_usecount on it.
302 		 * LK_NOWAIT is set so that when vget() returns ENOENT,
303 		 * vfs_hash_get() fails instead of looping.
304 		 * If this succeeds, it is safe so long as a vflush() with
305 		 * FORCECLOSE has not been done. Since the Renew thread is
306 		 * stopped and the MNTK_UNMOUNTF flag is set before doing
307 		 * a vflush() with FORCECLOSE, we should be ok here.
308 		 */
309 		if ((mntp->mnt_kern_flag & MNTK_UNMOUNTF))
310 			error = EINTR;
311 		else
312 			error = vfs_hash_get(mntp, hash,
313 			    (LK_EXCLOTHER | LK_NOWAIT), td, &nvp,
314 			    newnfs_vncmpf, nfhp);
315 	}
316 	FREE(nfhp, M_NFSFH);
317 	if (error)
318 		return (error);
319 	if (nvp != NULL) {
320 		*npp = VTONFS(nvp);
321 		return (0);
322 	}
323 	return (EINVAL);
324 }
325 
326 /*
327  * Load the attribute cache (that lives in the nfsnode entry) with
328  * the attributes of the second argument and
329  * Iff vaper not NULL
330  *    copy the attributes to *vaper
331  * Similar to nfs_loadattrcache(), except the attributes are passed in
332  * instead of being parsed out of the mbuf list.
333  */
334 int
335 nfscl_loadattrcache(struct vnode **vpp, struct nfsvattr *nap, void *nvaper,
336     void *stuff, int writeattr, int dontshrink)
337 {
338 	struct vnode *vp = *vpp;
339 	struct vattr *vap, *nvap = &nap->na_vattr, *vaper = nvaper;
340 	struct nfsnode *np;
341 	struct nfsmount *nmp;
342 	struct timespec mtime_save;
343 
344 	/*
345 	 * If v_type == VNON it is a new node, so fill in the v_type,
346 	 * n_mtime fields. Check to see if it represents a special
347 	 * device, and if so, check for a possible alias. Once the
348 	 * correct vnode has been obtained, fill in the rest of the
349 	 * information.
350 	 */
351 	np = VTONFS(vp);
352 	NFSLOCKNODE(np);
353 	if (vp->v_type != nvap->va_type) {
354 		vp->v_type = nvap->va_type;
355 		if (vp->v_type == VFIFO)
356 			vp->v_op = &newnfs_fifoops;
357 		np->n_mtime = nvap->va_mtime;
358 	}
359 	nmp = VFSTONFS(vp->v_mount);
360 	vap = &np->n_vattr.na_vattr;
361 	mtime_save = vap->va_mtime;
362 	if (writeattr) {
363 		np->n_vattr.na_filerev = nap->na_filerev;
364 		np->n_vattr.na_size = nap->na_size;
365 		np->n_vattr.na_mtime = nap->na_mtime;
366 		np->n_vattr.na_ctime = nap->na_ctime;
367 		np->n_vattr.na_fsid = nap->na_fsid;
368 	} else {
369 		NFSBCOPY((caddr_t)nap, (caddr_t)&np->n_vattr,
370 		    sizeof (struct nfsvattr));
371 	}
372 
373 	/*
374 	 * For NFSv4, if the node's fsid is not equal to the mount point's
375 	 * fsid, return the low order 32bits of the node's fsid. This
376 	 * allows getcwd(3) to work. There is a chance that the fsid might
377 	 * be the same as a local fs, but since this is in an NFS mount
378 	 * point, I don't think that will cause any problems?
379 	 */
380 	if ((nmp->nm_flag & (NFSMNT_NFSV4 | NFSMNT_HASSETFSID)) ==
381 	    (NFSMNT_NFSV4 | NFSMNT_HASSETFSID) &&
382 	    (nmp->nm_fsid[0] != np->n_vattr.na_filesid[0] ||
383 	     nmp->nm_fsid[1] != np->n_vattr.na_filesid[1]))
384 		vap->va_fsid = np->n_vattr.na_filesid[0];
385 	else
386 		vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
387 	np->n_attrstamp = time_second;
388 	if (vap->va_size != np->n_size) {
389 		if (vap->va_type == VREG) {
390 			if (dontshrink && vap->va_size < np->n_size) {
391 				/*
392 				 * We've been told not to shrink the file;
393 				 * zero np->n_attrstamp to indicate that
394 				 * the attributes are stale.
395 				 */
396 				vap->va_size = np->n_size;
397 				np->n_attrstamp = 0;
398 			} else if (np->n_flag & NMODIFIED) {
399 				/*
400 				 * We've modified the file: Use the larger
401 				 * of our size, and the server's size.
402 				 */
403 				if (vap->va_size < np->n_size) {
404 					vap->va_size = np->n_size;
405 				} else {
406 					np->n_size = vap->va_size;
407 					np->n_flag |= NSIZECHANGED;
408 				}
409 			} else {
410 				np->n_size = vap->va_size;
411 				np->n_flag |= NSIZECHANGED;
412 			}
413 			vnode_pager_setsize(vp, np->n_size);
414 		} else {
415 			np->n_size = vap->va_size;
416 		}
417 	}
418 	/*
419 	 * The following checks are added to prevent a race between (say)
420 	 * a READDIR+ and a WRITE.
421 	 * READDIR+, WRITE requests sent out.
422 	 * READDIR+ resp, WRITE resp received on client.
423 	 * However, the WRITE resp was handled before the READDIR+ resp
424 	 * causing the post op attrs from the write to be loaded first
425 	 * and the attrs from the READDIR+ to be loaded later. If this
426 	 * happens, we have stale attrs loaded into the attrcache.
427 	 * We detect this by for the mtime moving back. We invalidate the
428 	 * attrcache when this happens.
429 	 */
430 	if (timespeccmp(&mtime_save, &vap->va_mtime, >))
431 		/* Size changed or mtime went backwards */
432 		np->n_attrstamp = 0;
433 	if (vaper != NULL) {
434 		NFSBCOPY((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
435 		if (np->n_flag & NCHG) {
436 			if (np->n_flag & NACC)
437 				vaper->va_atime = np->n_atim;
438 			if (np->n_flag & NUPD)
439 				vaper->va_mtime = np->n_mtim;
440 		}
441 	}
442 	NFSUNLOCKNODE(np);
443 	return (0);
444 }
445 
446 /*
447  * Fill in the client id name. For these bytes:
448  * 1 - they must be unique
449  * 2 - they should be persistent across client reboots
450  * 1 is more critical than 2
451  * Use the mount point's unique id plus either the uuid or, if that
452  * isn't set, random junk.
453  */
454 void
455 nfscl_fillclid(u_int64_t clval, char *uuid, u_int8_t *cp, u_int16_t idlen)
456 {
457 	int uuidlen;
458 
459 	/*
460 	 * First, put in the 64bit mount point identifier.
461 	 */
462 	if (idlen >= sizeof (u_int64_t)) {
463 		NFSBCOPY((caddr_t)&clval, cp, sizeof (u_int64_t));
464 		cp += sizeof (u_int64_t);
465 		idlen -= sizeof (u_int64_t);
466 	}
467 
468 	/*
469 	 * If uuid is non-zero length, use it.
470 	 */
471 	uuidlen = strlen(uuid);
472 	if (uuidlen > 0 && idlen >= uuidlen) {
473 		NFSBCOPY(uuid, cp, uuidlen);
474 		cp += uuidlen;
475 		idlen -= uuidlen;
476 	}
477 
478 	/*
479 	 * This only normally happens if the uuid isn't set.
480 	 */
481 	while (idlen > 0) {
482 		*cp++ = (u_int8_t)(arc4random() % 256);
483 		idlen--;
484 	}
485 }
486 
487 /*
488  * Fill in a lock owner name. For now, pid + the process's creation time.
489  */
490 void
491 nfscl_filllockowner(struct thread *td, u_int8_t *cp)
492 {
493 	union {
494 		u_int32_t	lval;
495 		u_int8_t	cval[4];
496 	} tl;
497 	struct proc *p;
498 
499 if (td == NULL) {
500 	printf("NULL td\n");
501 	bzero(cp, 12);
502 	return;
503 }
504 	p = td->td_proc;
505 if (p == NULL) {
506 	printf("NULL pid\n");
507 	bzero(cp, 12);
508 	return;
509 }
510 	tl.lval = p->p_pid;
511 	*cp++ = tl.cval[0];
512 	*cp++ = tl.cval[1];
513 	*cp++ = tl.cval[2];
514 	*cp++ = tl.cval[3];
515 if (p->p_stats == NULL) {
516 	printf("pstats null\n");
517 	bzero(cp, 8);
518 	return;
519 }
520 	tl.lval = p->p_stats->p_start.tv_sec;
521 	*cp++ = tl.cval[0];
522 	*cp++ = tl.cval[1];
523 	*cp++ = tl.cval[2];
524 	*cp++ = tl.cval[3];
525 	tl.lval = p->p_stats->p_start.tv_usec;
526 	*cp++ = tl.cval[0];
527 	*cp++ = tl.cval[1];
528 	*cp++ = tl.cval[2];
529 	*cp = tl.cval[3];
530 }
531 
532 /*
533  * Find the parent process for the thread passed in as an argument.
534  * If none exists, return NULL, otherwise return a thread for the parent.
535  * (Can be any of the threads, since it is only used for td->td_proc.)
536  */
537 NFSPROC_T *
538 nfscl_getparent(struct thread *td)
539 {
540 	struct proc *p;
541 	struct thread *ptd;
542 
543 	if (td == NULL)
544 		return (NULL);
545 	p = td->td_proc;
546 	if (p->p_pid == 0)
547 		return (NULL);
548 	p = p->p_pptr;
549 	if (p == NULL)
550 		return (NULL);
551 	ptd = TAILQ_FIRST(&p->p_threads);
552 	return (ptd);
553 }
554 
555 /*
556  * Start up the renew kernel thread.
557  */
558 static void
559 start_nfscl(void *arg)
560 {
561 	struct nfsclclient *clp;
562 	struct thread *td;
563 
564 	clp = (struct nfsclclient *)arg;
565 	td = TAILQ_FIRST(&clp->nfsc_renewthread->p_threads);
566 	nfscl_renewthread(clp, td);
567 	kproc_exit(0);
568 }
569 
570 void
571 nfscl_start_renewthread(struct nfsclclient *clp)
572 {
573 
574 	kproc_create(start_nfscl, (void *)clp, &clp->nfsc_renewthread, 0, 0,
575 	    "nfscl");
576 }
577 
578 /*
579  * Handle wcc_data.
580  * For NFSv4, it assumes that nfsv4_wccattr() was used to set up the getattr
581  * as the first Op after PutFH.
582  * (For NFSv4, the postop attributes are after the Op, so they can't be
583  *  parsed here. A separate call to nfscl_postop_attr() is required.)
584  */
585 int
586 nfscl_wcc_data(struct nfsrv_descript *nd, struct vnode *vp,
587     struct nfsvattr *nap, int *flagp, int *wccflagp, void *stuff)
588 {
589 	u_int32_t *tl;
590 	struct nfsnode *np = VTONFS(vp);
591 	struct nfsvattr nfsva;
592 	int error = 0;
593 
594 	if (wccflagp != NULL)
595 		*wccflagp = 0;
596 	if (nd->nd_flag & ND_NFSV3) {
597 		*flagp = 0;
598 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
599 		if (*tl == newnfs_true) {
600 			NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
601 			if (wccflagp != NULL) {
602 				mtx_lock(&np->n_mtx);
603 				*wccflagp = (np->n_mtime.tv_sec ==
604 				    fxdr_unsigned(u_int32_t, *(tl + 2)) &&
605 				    np->n_mtime.tv_nsec ==
606 				    fxdr_unsigned(u_int32_t, *(tl + 3)));
607 				mtx_unlock(&np->n_mtx);
608 			}
609 		}
610 		error = nfscl_postop_attr(nd, nap, flagp, stuff);
611 	} else if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR))
612 	    == (ND_NFSV4 | ND_V4WCCATTR)) {
613 		error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
614 		    NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
615 		    NULL, NULL, NULL, NULL, NULL);
616 		if (error)
617 			return (error);
618 		/*
619 		 * Get rid of Op# and status for next op.
620 		 */
621 		NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
622 		if (*++tl)
623 			nd->nd_flag |= ND_NOMOREDATA;
624 		if (wccflagp != NULL &&
625 		    nfsva.na_vattr.va_mtime.tv_sec != 0) {
626 			mtx_lock(&np->n_mtx);
627 			*wccflagp = (np->n_mtime.tv_sec ==
628 			    nfsva.na_vattr.va_mtime.tv_sec &&
629 			    np->n_mtime.tv_nsec ==
630 			    nfsva.na_vattr.va_mtime.tv_sec);
631 			mtx_unlock(&np->n_mtx);
632 		}
633 	}
634 nfsmout:
635 	return (error);
636 }
637 
638 /*
639  * Get postop attributes.
640  */
641 int
642 nfscl_postop_attr(struct nfsrv_descript *nd, struct nfsvattr *nap, int *retp,
643     void *stuff)
644 {
645 	u_int32_t *tl;
646 	int error = 0;
647 
648 	*retp = 0;
649 	if (nd->nd_flag & ND_NOMOREDATA)
650 		return (error);
651 	if (nd->nd_flag & ND_NFSV3) {
652 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
653 		*retp = fxdr_unsigned(int, *tl);
654 	} else if (nd->nd_flag & ND_NFSV4) {
655 		/*
656 		 * For NFSv4, the postop attr are at the end, so no point
657 		 * in looking if nd_repstat != 0.
658 		 */
659 		if (!nd->nd_repstat) {
660 			NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
661 			if (*(tl + 1))
662 				/* should never happen since nd_repstat != 0 */
663 				nd->nd_flag |= ND_NOMOREDATA;
664 			else
665 				*retp = 1;
666 		}
667 	} else if (!nd->nd_repstat) {
668 		/* For NFSv2, the attributes are here iff nd_repstat == 0 */
669 		*retp = 1;
670 	}
671 	if (*retp) {
672 		error = nfsm_loadattr(nd, nap);
673 		if (error)
674 			*retp = 0;
675 	}
676 nfsmout:
677 	return (error);
678 }
679 
680 /*
681  * Fill in the setable attributes. The full argument indicates whether
682  * to fill in them all or just mode and time.
683  */
684 void
685 nfscl_fillsattr(struct nfsrv_descript *nd, struct vattr *vap,
686     struct vnode *vp, int flags, u_int32_t rdev)
687 {
688 	u_int32_t *tl;
689 	struct nfsv2_sattr *sp;
690 	nfsattrbit_t attrbits;
691 	struct timeval curtime;
692 
693 	switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
694 	case ND_NFSV2:
695 		NFSM_BUILD(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
696 		if (vap->va_mode == (mode_t)VNOVAL)
697 			sp->sa_mode = newnfs_xdrneg1;
698 		else
699 			sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
700 		if (vap->va_uid == (uid_t)VNOVAL)
701 			sp->sa_uid = newnfs_xdrneg1;
702 		else
703 			sp->sa_uid = txdr_unsigned(vap->va_uid);
704 		if (vap->va_gid == (gid_t)VNOVAL)
705 			sp->sa_gid = newnfs_xdrneg1;
706 		else
707 			sp->sa_gid = txdr_unsigned(vap->va_gid);
708 		if (flags & NFSSATTR_SIZE0)
709 			sp->sa_size = 0;
710 		else if (flags & NFSSATTR_SIZENEG1)
711 			sp->sa_size = newnfs_xdrneg1;
712 		else if (flags & NFSSATTR_SIZERDEV)
713 			sp->sa_size = txdr_unsigned(rdev);
714 		else
715 			sp->sa_size = txdr_unsigned(vap->va_size);
716 		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
717 		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
718 		break;
719 	case ND_NFSV3:
720 		getmicrotime(&curtime);
721 		if (vap->va_mode != (mode_t)VNOVAL) {
722 			NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
723 			*tl++ = newnfs_true;
724 			*tl = txdr_unsigned(vap->va_mode);
725 		} else {
726 			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
727 			*tl = newnfs_false;
728 		}
729 		if ((flags & NFSSATTR_FULL) && vap->va_uid != (uid_t)VNOVAL) {
730 			NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
731 			*tl++ = newnfs_true;
732 			*tl = txdr_unsigned(vap->va_uid);
733 		} else {
734 			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
735 			*tl = newnfs_false;
736 		}
737 		if ((flags & NFSSATTR_FULL) && vap->va_gid != (gid_t)VNOVAL) {
738 			NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
739 			*tl++ = newnfs_true;
740 			*tl = txdr_unsigned(vap->va_gid);
741 		} else {
742 			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
743 			*tl = newnfs_false;
744 		}
745 		if ((flags & NFSSATTR_FULL) && vap->va_size != VNOVAL) {
746 			NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
747 			*tl++ = newnfs_true;
748 			txdr_hyper(vap->va_size, tl);
749 		} else {
750 			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
751 			*tl = newnfs_false;
752 		}
753 		if (vap->va_atime.tv_sec != VNOVAL) {
754 			if (vap->va_atime.tv_sec != curtime.tv_sec) {
755 				NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
756 				*tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
757 				txdr_nfsv3time(&vap->va_atime, tl);
758 			} else {
759 				NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
760 				*tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
761 			}
762 		} else {
763 			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
764 			*tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
765 		}
766 		if (vap->va_mtime.tv_sec != VNOVAL) {
767 			if (vap->va_mtime.tv_sec != curtime.tv_sec) {
768 				NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
769 				*tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
770 				txdr_nfsv3time(&vap->va_mtime, tl);
771 			} else {
772 				NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
773 				*tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
774 			}
775 		} else {
776 			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
777 			*tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
778 		}
779 		break;
780 	case ND_NFSV4:
781 		NFSZERO_ATTRBIT(&attrbits);
782 		if (vap->va_mode != (mode_t)VNOVAL)
783 			NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_MODE);
784 		if ((flags & NFSSATTR_FULL) && vap->va_uid != (uid_t)VNOVAL)
785 			NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_OWNER);
786 		if ((flags & NFSSATTR_FULL) && vap->va_gid != (gid_t)VNOVAL)
787 			NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_OWNERGROUP);
788 		if ((flags & NFSSATTR_FULL) && vap->va_size != VNOVAL)
789 			NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
790 		if (vap->va_atime.tv_sec != VNOVAL)
791 			NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESSSET);
792 		if (vap->va_mtime.tv_sec != VNOVAL)
793 			NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFYSET);
794 		(void) nfsv4_fillattr(nd, vp, NULL, vap, NULL, 0, &attrbits,
795 		    NULL, NULL, 0, 0);
796 		break;
797 	};
798 }
799 
800 /*
801  * nfscl_request() - mostly a wrapper for newnfs_request().
802  */
803 int
804 nfscl_request(struct nfsrv_descript *nd, struct vnode *vp, NFSPROC_T *p,
805     struct ucred *cred, void *stuff)
806 {
807 	int ret, vers;
808 	struct nfsmount *nmp;
809 
810 	nmp = VFSTONFS(vp->v_mount);
811 	if (nd->nd_flag & ND_NFSV4)
812 		vers = NFS_VER4;
813 	else if (nd->nd_flag & ND_NFSV3)
814 		vers = NFS_VER3;
815 	else
816 		vers = NFS_VER2;
817 	ret = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
818 		NFS_PROG, vers, NULL, 1, NULL);
819 	return (ret);
820 }
821 
822 /*
823  * fill in this bsden's variant of statfs using nfsstatfs.
824  */
825 void
826 nfscl_loadsbinfo(struct nfsmount *nmp, struct nfsstatfs *sfp, void *statfs)
827 {
828 	struct statfs *sbp = (struct statfs *)statfs;
829 	nfsquad_t tquad;
830 
831 	if (nmp->nm_flag & (NFSMNT_NFSV3 | NFSMNT_NFSV4)) {
832 		sbp->f_bsize = NFS_FABLKSIZE;
833 		tquad.qval = sfp->sf_tbytes;
834 		sbp->f_blocks = (long)(tquad.qval / ((u_quad_t)NFS_FABLKSIZE));
835 		tquad.qval = sfp->sf_fbytes;
836 		sbp->f_bfree = (long)(tquad.qval / ((u_quad_t)NFS_FABLKSIZE));
837 		tquad.qval = sfp->sf_abytes;
838 		sbp->f_bavail = (long)(tquad.qval / ((u_quad_t)NFS_FABLKSIZE));
839 		tquad.qval = sfp->sf_tfiles;
840 		sbp->f_files = (tquad.lval[0] & 0x7fffffff);
841 		tquad.qval = sfp->sf_ffiles;
842 		sbp->f_ffree = (tquad.lval[0] & 0x7fffffff);
843 	} else if ((nmp->nm_flag & NFSMNT_NFSV4) == 0) {
844 		sbp->f_bsize = (int32_t)sfp->sf_bsize;
845 		sbp->f_blocks = (int32_t)sfp->sf_blocks;
846 		sbp->f_bfree = (int32_t)sfp->sf_bfree;
847 		sbp->f_bavail = (int32_t)sfp->sf_bavail;
848 		sbp->f_files = 0;
849 		sbp->f_ffree = 0;
850 	}
851 }
852 
853 /*
854  * Use the fsinfo stuff to update the mount point.
855  */
856 void
857 nfscl_loadfsinfo(struct nfsmount *nmp, struct nfsfsinfo *fsp)
858 {
859 
860 	if ((nmp->nm_wsize == 0 || fsp->fs_wtpref < nmp->nm_wsize) &&
861 	    fsp->fs_wtpref >= NFS_FABLKSIZE)
862 		nmp->nm_wsize = (fsp->fs_wtpref + NFS_FABLKSIZE - 1) &
863 		    ~(NFS_FABLKSIZE - 1);
864 	if (fsp->fs_wtmax < nmp->nm_wsize && fsp->fs_wtmax > 0) {
865 		nmp->nm_wsize = fsp->fs_wtmax & ~(NFS_FABLKSIZE - 1);
866 		if (nmp->nm_wsize == 0)
867 			nmp->nm_wsize = fsp->fs_wtmax;
868 	}
869 	if (nmp->nm_wsize < NFS_FABLKSIZE)
870 		nmp->nm_wsize = NFS_FABLKSIZE;
871 	if ((nmp->nm_rsize == 0 || fsp->fs_rtpref < nmp->nm_rsize) &&
872 	    fsp->fs_rtpref >= NFS_FABLKSIZE)
873 		nmp->nm_rsize = (fsp->fs_rtpref + NFS_FABLKSIZE - 1) &
874 		    ~(NFS_FABLKSIZE - 1);
875 	if (fsp->fs_rtmax < nmp->nm_rsize && fsp->fs_rtmax > 0) {
876 		nmp->nm_rsize = fsp->fs_rtmax & ~(NFS_FABLKSIZE - 1);
877 		if (nmp->nm_rsize == 0)
878 			nmp->nm_rsize = fsp->fs_rtmax;
879 	}
880 	if (nmp->nm_rsize < NFS_FABLKSIZE)
881 		nmp->nm_rsize = NFS_FABLKSIZE;
882 	if ((nmp->nm_readdirsize == 0 || fsp->fs_dtpref < nmp->nm_readdirsize)
883 	    && fsp->fs_dtpref >= NFS_DIRBLKSIZ)
884 		nmp->nm_readdirsize = (fsp->fs_dtpref + NFS_DIRBLKSIZ - 1) &
885 		    ~(NFS_DIRBLKSIZ - 1);
886 	if (fsp->fs_rtmax < nmp->nm_readdirsize && fsp->fs_rtmax > 0) {
887 		nmp->nm_readdirsize = fsp->fs_rtmax & ~(NFS_DIRBLKSIZ - 1);
888 		if (nmp->nm_readdirsize == 0)
889 			nmp->nm_readdirsize = fsp->fs_rtmax;
890 	}
891 	if (nmp->nm_readdirsize < NFS_DIRBLKSIZ)
892 		nmp->nm_readdirsize = NFS_DIRBLKSIZ;
893 	if (fsp->fs_maxfilesize > 0 &&
894 	    fsp->fs_maxfilesize < nmp->nm_maxfilesize)
895 		nmp->nm_maxfilesize = fsp->fs_maxfilesize;
896 	nmp->nm_mountp->mnt_stat.f_iosize = newnfs_iosize(nmp);
897 	nmp->nm_state |= NFSSTA_GOTFSINFO;
898 }
899 
900 /*
901  * Get a pointer to my IP addrress and return it.
902  * Return NULL if you can't find one.
903  */
904 u_int8_t *
905 nfscl_getmyip(struct nfsmount *nmp, int *isinet6p)
906 {
907 	struct sockaddr_in sad, *sin;
908 	struct rtentry *rt;
909 	u_int8_t *retp = NULL;
910 	static struct in_addr laddr;
911 
912 	*isinet6p = 0;
913 	/*
914 	 * Loop up a route for the destination address.
915 	 */
916 	if (nmp->nm_nam->sa_family == AF_INET) {
917 		bzero(&sad, sizeof (sad));
918 		sin = (struct sockaddr_in *)nmp->nm_nam;
919 		sad.sin_family = AF_INET;
920 		sad.sin_len = sizeof (struct sockaddr_in);
921 		sad.sin_addr.s_addr = sin->sin_addr.s_addr;
922 		rt = rtalloc1((struct sockaddr *)&sad, 0, 0UL);
923 		if (rt != NULL) {
924 			if (rt->rt_ifp != NULL &&
925 			    rt->rt_ifa != NULL &&
926 			    ((rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) &&
927 			    rt->rt_ifa->ifa_addr->sa_family == AF_INET) {
928 				sin = (struct sockaddr_in *)
929 				    rt->rt_ifa->ifa_addr;
930 				laddr.s_addr = sin->sin_addr.s_addr;
931 				retp = (u_int8_t *)&laddr;
932 			}
933 			RTFREE_LOCKED(rt);
934 		}
935 #ifdef INET6
936 	} else if (nmp->nm_nam->sa_family == AF_INET6) {
937 		struct sockaddr_in6 sad6, *sin6;
938 		static struct in6_addr laddr6;
939 
940 		bzero(&sad6, sizeof (sad6));
941 		sin6 = (struct sockaddr_in6 *)nmp->nm_nam;
942 		sad6.sin6_family = AF_INET6;
943 		sad6.sin6_len = sizeof (struct sockaddr_in6);
944 		sad6.sin6_addr = sin6->sin6_addr;
945 		rt = rtalloc1((struct sockaddr *)&sad6, 0, 0UL);
946 		if (rt != NULL) {
947 			if (rt->rt_ifp != NULL &&
948 			    rt->rt_ifa != NULL &&
949 			    ((rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) &&
950 			    rt->rt_ifa->ifa_addr->sa_family == AF_INET6) {
951 				sin6 = (struct sockaddr_in6 *)
952 				    rt->rt_ifa->ifa_addr;
953 				laddr6 = sin6->sin6_addr;
954 				retp = (u_int8_t *)&laddr6;
955 				*isinet6p = 1;
956 			}
957 			RTFREE_LOCKED(rt);
958 		}
959 #endif
960 	}
961 	return (retp);
962 }
963 
964 /*
965  * Copy NFS uid, gids from the cred structure.
966  */
967 void
968 newnfs_copyincred(struct ucred *cr, struct nfscred *nfscr)
969 {
970 	int i;
971 
972 	KASSERT(cr->cr_ngroups >= 0,
973 	    ("newnfs_copyincred: negative cr_ngroups"));
974 	nfscr->nfsc_uid = cr->cr_uid;
975 	nfscr->nfsc_ngroups = MIN(cr->cr_ngroups, NFS_MAXGRPS + 1);
976 	for (i = 0; i < nfscr->nfsc_ngroups; i++)
977 		nfscr->nfsc_groups[i] = cr->cr_groups[i];
978 }
979 
980 
981 /*
982  * Do any client specific initialization.
983  */
984 void
985 nfscl_init(void)
986 {
987 	static int inited = 0;
988 
989 	if (inited)
990 		return;
991 	inited = 1;
992 	nfscl_inited = 1;
993 	ncl_pbuf_freecnt = nswbuf / 2 + 1;
994 }
995 
996 /*
997  * Check each of the attributes to be set, to ensure they aren't already
998  * the correct value. Disable setting ones already correct.
999  */
1000 int
1001 nfscl_checksattr(struct vattr *vap, struct nfsvattr *nvap)
1002 {
1003 
1004 	if (vap->va_mode != (mode_t)VNOVAL) {
1005 		if (vap->va_mode == nvap->na_mode)
1006 			vap->va_mode = (mode_t)VNOVAL;
1007 	}
1008 	if (vap->va_uid != (uid_t)VNOVAL) {
1009 		if (vap->va_uid == nvap->na_uid)
1010 			vap->va_uid = (uid_t)VNOVAL;
1011 	}
1012 	if (vap->va_gid != (gid_t)VNOVAL) {
1013 		if (vap->va_gid == nvap->na_gid)
1014 			vap->va_gid = (gid_t)VNOVAL;
1015 	}
1016 	if (vap->va_size != VNOVAL) {
1017 		if (vap->va_size == nvap->na_size)
1018 			vap->va_size = VNOVAL;
1019 	}
1020 
1021 	/*
1022 	 * We are normally called with only a partially initialized
1023 	 * VAP.  Since the NFSv3 spec says that server may use the
1024 	 * file attributes to store the verifier, the spec requires
1025 	 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1026 	 * in atime, but we can't really assume that all servers will
1027 	 * so we ensure that our SETATTR sets both atime and mtime.
1028 	 */
1029 	if (vap->va_mtime.tv_sec == VNOVAL)
1030 		vfs_timestamp(&vap->va_mtime);
1031 	if (vap->va_atime.tv_sec == VNOVAL)
1032 		vap->va_atime = vap->va_mtime;
1033 	return (1);
1034 }
1035 
1036 /*
1037  * Map nfsv4 errors to errno.h errors.
1038  * The uid and gid arguments are only used for NFSERR_BADOWNER and that
1039  * error should only be returned for the Open, Create and Setattr Ops.
1040  * As such, most calls can just pass in 0 for those arguments.
1041  */
1042 APPLESTATIC int
1043 nfscl_maperr(struct thread *td, int error, uid_t uid, gid_t gid)
1044 {
1045 	struct proc *p;
1046 
1047 	if (error < 10000)
1048 		return (error);
1049 	if (td != NULL)
1050 		p = td->td_proc;
1051 	else
1052 		p = NULL;
1053 	switch (error) {
1054 	case NFSERR_BADOWNER:
1055 		tprintf(p, LOG_INFO,
1056 		    "No name and/or group mapping for uid,gid:(%d,%d)\n",
1057 		    uid, gid);
1058 		return (EPERM);
1059 	case NFSERR_STALECLIENTID:
1060 	case NFSERR_STALESTATEID:
1061 	case NFSERR_EXPIRED:
1062 	case NFSERR_BADSTATEID:
1063 		printf("nfsv4 recover err returned %d\n", error);
1064 		return (EIO);
1065 	case NFSERR_BADHANDLE:
1066 	case NFSERR_SERVERFAULT:
1067 	case NFSERR_BADTYPE:
1068 	case NFSERR_FHEXPIRED:
1069 	case NFSERR_RESOURCE:
1070 	case NFSERR_MOVED:
1071 	case NFSERR_NOFILEHANDLE:
1072 	case NFSERR_MINORVERMISMATCH:
1073 	case NFSERR_OLDSTATEID:
1074 	case NFSERR_BADSEQID:
1075 	case NFSERR_LEASEMOVED:
1076 	case NFSERR_RECLAIMBAD:
1077 	case NFSERR_BADXDR:
1078 	case NFSERR_BADCHAR:
1079 	case NFSERR_BADNAME:
1080 	case NFSERR_OPILLEGAL:
1081 		printf("nfsv4 client/server protocol prob err=%d\n",
1082 		    error);
1083 		return (EIO);
1084 	default:
1085 		tprintf(p, LOG_INFO, "nfsv4 err=%d\n", error);
1086 		return (EIO);
1087 	};
1088 }
1089 
1090 /*
1091  * Locate a process by number; return only "live" processes -- i.e., neither
1092  * zombies nor newly born but incompletely initialized processes.  By not
1093  * returning processes in the PRS_NEW state, we allow callers to avoid
1094  * testing for that condition to avoid dereferencing p_ucred, et al.
1095  * Identical to pfind() in kern_proc.c, except it assume the list is
1096  * already locked.
1097  */
1098 static struct proc *
1099 pfind_locked(pid_t pid)
1100 {
1101 	struct proc *p;
1102 
1103 	LIST_FOREACH(p, PIDHASH(pid), p_hash)
1104 		if (p->p_pid == pid) {
1105 			if (p->p_state == PRS_NEW) {
1106 				p = NULL;
1107 				break;
1108 			}
1109 			PROC_LOCK(p);
1110 			break;
1111 		}
1112 	return (p);
1113 }
1114 
1115 /*
1116  * Check to see if the process for this owner exists. Return 1 if it doesn't
1117  * and 0 otherwise.
1118  */
1119 int
1120 nfscl_procdoesntexist(u_int8_t *own)
1121 {
1122 	union {
1123 		u_int32_t	lval;
1124 		u_int8_t	cval[4];
1125 	} tl;
1126 	struct proc *p;
1127 	pid_t pid;
1128 	int ret = 0;
1129 
1130 	tl.cval[0] = *own++;
1131 	tl.cval[1] = *own++;
1132 	tl.cval[2] = *own++;
1133 	tl.cval[3] = *own++;
1134 	pid = tl.lval;
1135 	p = pfind_locked(pid);
1136 	if (p == NULL)
1137 		return (1);
1138 	if (p->p_stats == NULL) {
1139 		PROC_UNLOCK(p);
1140 		return (0);
1141 	}
1142 	tl.cval[0] = *own++;
1143 	tl.cval[1] = *own++;
1144 	tl.cval[2] = *own++;
1145 	tl.cval[3] = *own++;
1146 	if (tl.lval != p->p_stats->p_start.tv_sec) {
1147 		ret = 1;
1148 	} else {
1149 		tl.cval[0] = *own++;
1150 		tl.cval[1] = *own++;
1151 		tl.cval[2] = *own++;
1152 		tl.cval[3] = *own;
1153 		if (tl.lval != p->p_stats->p_start.tv_usec)
1154 			ret = 1;
1155 	}
1156 	PROC_UNLOCK(p);
1157 	return (ret);
1158 }
1159 
1160 /*
1161  * - nfs pseudo system call for the client
1162  */
1163 /*
1164  * MPSAFE
1165  */
1166 static int
1167 nfssvc_nfscl(struct thread *td, struct nfssvc_args *uap)
1168 {
1169 	struct file *fp;
1170 	struct nfscbd_args nfscbdarg;
1171 	struct nfsd_nfscbd_args nfscbdarg2;
1172 	int error;
1173 
1174 	if (uap->flag & NFSSVC_CBADDSOCK) {
1175 		error = copyin(uap->argp, (caddr_t)&nfscbdarg, sizeof(nfscbdarg));
1176 		if (error)
1177 			return (error);
1178 		if ((error = fget(td, nfscbdarg.sock, &fp)) != 0) {
1179 			return (error);
1180 		}
1181 		if (fp->f_type != DTYPE_SOCKET) {
1182 			fdrop(fp, td);
1183 			return (EPERM);
1184 		}
1185 		error = nfscbd_addsock(fp);
1186 		fdrop(fp, td);
1187 		if (!error && nfscl_enablecallb == 0) {
1188 			nfsv4_cbport = nfscbdarg.port;
1189 			nfscl_enablecallb = 1;
1190 		}
1191 	} else if (uap->flag & NFSSVC_NFSCBD) {
1192 		if (uap->argp == NULL)
1193 			return (EINVAL);
1194 		error = copyin(uap->argp, (caddr_t)&nfscbdarg2,
1195 		    sizeof(nfscbdarg2));
1196 		if (error)
1197 			return (error);
1198 		error = nfscbd_nfsd(td, &nfscbdarg2);
1199 	} else {
1200 		error = EINVAL;
1201 	}
1202 	return (error);
1203 }
1204 
1205 extern int (*nfsd_call_nfscl)(struct thread *, struct nfssvc_args *);
1206 
1207 /*
1208  * Called once to initialize data structures...
1209  */
1210 static int
1211 nfscl_modevent(module_t mod, int type, void *data)
1212 {
1213 	int error = 0;
1214 	static int loaded = 0;
1215 
1216 	switch (type) {
1217 	case MOD_LOAD:
1218 		if (loaded)
1219 			return (0);
1220 		newnfs_portinit();
1221 		mtx_init(&nfs_clstate_mutex, "nfs_clstate_mutex", NULL,
1222 		    MTX_DEF);
1223 		mtx_init(&ncl_iod_mutex, "ncl_iod_mutex", NULL, MTX_DEF);
1224 		nfscl_init();
1225 		NFSD_LOCK();
1226 		nfsrvd_cbinit(0);
1227 		NFSD_UNLOCK();
1228 		ncl_call_invalcaches = ncl_invalcaches;
1229 		nfsd_call_nfscl = nfssvc_nfscl;
1230 		loaded = 1;
1231 		break;
1232 
1233 	case MOD_UNLOAD:
1234 		if (nfs_numnfscbd != 0) {
1235 			error = EBUSY;
1236 			break;
1237 		}
1238 
1239 		/*
1240 		 * XXX: Unloading of nfscl module is unsupported.
1241 		 */
1242 #if 0
1243 		ncl_call_invalcaches = NULL;
1244 		nfsd_call_nfscl = NULL;
1245 		/* and get rid of the mutexes */
1246 		mtx_destroy(&nfs_clstate_mutex);
1247 		mtx_destroy(&ncl_iod_mutex);
1248 		loaded = 0;
1249 		break;
1250 #else
1251 		/* FALLTHROUGH */
1252 #endif
1253 	default:
1254 		error = EOPNOTSUPP;
1255 		break;
1256 	}
1257 	return error;
1258 }
1259 static moduledata_t nfscl_mod = {
1260 	"nfscl",
1261 	nfscl_modevent,
1262 	NULL,
1263 };
1264 DECLARE_MODULE(nfscl, nfscl_mod, SI_SUB_VFS, SI_ORDER_FIRST);
1265 
1266 /* So that loader and kldload(2) can find us, wherever we are.. */
1267 MODULE_VERSION(nfscl, 1);
1268 MODULE_DEPEND(nfscl, nfscommon, 1, 1, 1);
1269 MODULE_DEPEND(nfscl, krpc, 1, 1, 1);
1270 MODULE_DEPEND(nfscl, nfssvc, 1, 1, 1);
1271 MODULE_DEPEND(nfscl, nfslock, 1, 1, 1);
1272 
1273