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