2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include "opt_inet6.h"
39 #include <sys/capability.h>
42 * generally, I don't like #includes inside .h files, but it seems to
43 * be the easiest way to handle the port.
46 #include <fs/nfs/nfsport.h>
47 #include <netinet/if_ether.h>
48 #include <net/if_types.h>
50 #include <fs/nfsclient/nfs_kdtrace.h>
53 dtrace_nfsclient_attrcache_flush_probe_func_t
54 dtrace_nfscl_attrcache_flush_done_probe;
55 uint32_t nfscl_attrcache_flush_done_id;
57 dtrace_nfsclient_attrcache_get_hit_probe_func_t
58 dtrace_nfscl_attrcache_get_hit_probe;
59 uint32_t nfscl_attrcache_get_hit_id;
61 dtrace_nfsclient_attrcache_get_miss_probe_func_t
62 dtrace_nfscl_attrcache_get_miss_probe;
63 uint32_t nfscl_attrcache_get_miss_id;
65 dtrace_nfsclient_attrcache_load_probe_func_t
66 dtrace_nfscl_attrcache_load_done_probe;
67 uint32_t nfscl_attrcache_load_done_id;
68 #endif /* !KDTRACE_HOOKS */
70 extern u_int32_t newnfs_true, newnfs_false, newnfs_xdrneg1;
71 extern struct vop_vector newnfs_vnodeops;
72 extern struct vop_vector newnfs_fifoops;
73 extern uma_zone_t newnfsnode_zone;
74 extern struct buf_ops buf_ops_newnfs;
75 extern int ncl_pbuf_freecnt;
76 extern short nfsv4_cbport;
77 extern int nfscl_enablecallb;
78 extern int nfs_numnfscbd;
79 extern int nfscl_inited;
80 struct mtx nfs_clstate_mutex;
81 struct mtx ncl_iod_mutex;
84 extern void (*ncl_call_invalcaches)(struct vnode *);
87 * Comparison function for vfs_hash functions.
90 newnfs_vncmpf(struct vnode *vp, void *arg)
92 struct nfsfh *nfhp = (struct nfsfh *)arg;
93 struct nfsnode *np = VTONFS(vp);
95 if (np->n_fhp->nfh_len != nfhp->nfh_len ||
96 NFSBCMP(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len))
102 * Look up a vnode/nfsnode by file handle.
103 * Callers must check for mount points!!
104 * In all cases, a pointer to a
105 * nfsnode structure is returned.
106 * This variant takes a "struct nfsfh *" as second argument and uses
107 * that structure up, either by hanging off the nfsnode or FREEing it.
110 nfscl_nget(struct mount *mntp, struct vnode *dvp, struct nfsfh *nfhp,
111 struct componentname *cnp, struct thread *td, struct nfsnode **npp,
112 void *stuff, int lkflags)
114 struct nfsnode *np, *dnp;
115 struct vnode *vp, *nvp;
116 struct nfsv4node *newd, *oldd;
119 struct nfsmount *nmp;
121 nmp = VFSTONFS(mntp);
125 hash = fnv_32_buf(nfhp->nfh_fh, nfhp->nfh_len, FNV1_32_INIT);
127 error = vfs_hash_get(mntp, hash, lkflags,
128 td, &nvp, newnfs_vncmpf, nfhp);
129 if (error == 0 && nvp != NULL) {
131 * I believe there is a slight chance that vgonel() could
132 * get called on this vnode between when NFSVOPLOCK() drops
133 * the VI_LOCK() and vget() acquires it again, so that it
134 * hasn't yet had v_usecount incremented. If this were to
135 * happen, the VI_DOOMED flag would be set, so check for
136 * that here. Since we now have the v_usecount incremented,
137 * we should be ok until we vrele() it, if the VI_DOOMED
138 * flag isn't set now.
141 if ((nvp->v_iflag & VI_DOOMED)) {
150 FREE((caddr_t)nfhp, M_NFSFH);
156 * For NFSv4, check to see if it is the same name and
157 * replace the name, if it is different.
160 if ((nmp->nm_flag & NFSMNT_NFSV4) && np->n_v4 != NULL &&
161 nvp->v_type == VREG &&
162 (np->n_v4->n4_namelen != cnp->cn_namelen ||
163 NFSBCMP(cnp->cn_nameptr, NFS4NODENAME(np->n_v4),
165 dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen ||
166 NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
167 dnp->n_fhp->nfh_len))) {
168 MALLOC(newd, struct nfsv4node *,
169 sizeof (struct nfsv4node) + dnp->n_fhp->nfh_len +
170 + cnp->cn_namelen - 1, M_NFSV4NODE, M_WAITOK);
172 if (newd != NULL && np->n_v4 != NULL && nvp->v_type == VREG
173 && (np->n_v4->n4_namelen != cnp->cn_namelen ||
174 NFSBCMP(cnp->cn_nameptr, NFS4NODENAME(np->n_v4),
176 dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen ||
177 NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
178 dnp->n_fhp->nfh_len))) {
182 np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
183 np->n_v4->n4_namelen = cnp->cn_namelen;
184 NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
185 dnp->n_fhp->nfh_len);
186 NFSBCOPY(cnp->cn_nameptr, NFS4NODENAME(np->n_v4),
192 FREE((caddr_t)newd, M_NFSV4NODE);
194 FREE((caddr_t)oldd, M_NFSV4NODE);
196 FREE((caddr_t)nfhp, M_NFSFH);
199 np = uma_zalloc(newnfsnode_zone, M_WAITOK | M_ZERO);
201 error = getnewvnode("newnfs", mntp, &newnfs_vnodeops, &nvp);
203 uma_zfree(newnfsnode_zone, np);
204 FREE((caddr_t)nfhp, M_NFSFH);
208 KASSERT(vp->v_bufobj.bo_bsize != 0, ("nfscl_nget: bo_bsize == 0"));
209 vp->v_bufobj.bo_ops = &buf_ops_newnfs;
213 * Initialize the mutex even if the vnode is going to be a loser.
214 * This simplifies the logic in reclaim, which can then unconditionally
215 * destroy the mutex (in the case of the loser, or if hash_insert
216 * happened to return an error no special casing is needed).
218 mtx_init(&np->n_mtx, "NEWNFSnode lock", NULL, MTX_DEF | MTX_DUPOK);
221 * Are we getting the root? If so, make sure the vnode flags
224 if ((nfhp->nfh_len == nmp->nm_fhsize) &&
225 !bcmp(nfhp->nfh_fh, nmp->nm_fh, nfhp->nfh_len)) {
226 if (vp->v_type == VNON)
228 vp->v_vflag |= VV_ROOT;
233 * For NFSv4, we have to attach the directory file handle and
234 * file name, so that Open Ops can be done later.
236 if (nmp->nm_flag & NFSMNT_NFSV4) {
237 MALLOC(np->n_v4, struct nfsv4node *, sizeof (struct nfsv4node)
238 + dnp->n_fhp->nfh_len + cnp->cn_namelen - 1, M_NFSV4NODE,
240 np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
241 np->n_v4->n4_namelen = cnp->cn_namelen;
242 NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
243 dnp->n_fhp->nfh_len);
244 NFSBCOPY(cnp->cn_nameptr, NFS4NODENAME(np->n_v4),
251 * NFS supports recursive and shared locking.
253 lockmgr(vp->v_vnlock, LK_EXCLUSIVE | LK_NOWITNESS, NULL);
256 error = insmntque(vp, mntp);
259 mtx_destroy(&np->n_mtx);
260 FREE((caddr_t)nfhp, M_NFSFH);
261 if (np->n_v4 != NULL)
262 FREE((caddr_t)np->n_v4, M_NFSV4NODE);
263 uma_zfree(newnfsnode_zone, np);
266 error = vfs_hash_insert(vp, hash, lkflags,
267 td, &nvp, newnfs_vncmpf, nfhp);
272 /* vfs_hash_insert() vput()'s the losing vnode */
281 * Anothe variant of nfs_nget(). This one is only used by reopen. It
282 * takes almost the same args as nfs_nget(), but only succeeds if an entry
283 * exists in the cache. (Since files should already be "open" with a
284 * vnode ref cnt on the node when reopen calls this, it should always
286 * Also, don't get a vnode lock, since it may already be locked by some
287 * other process that is handling it. This is ok, since all other threads
288 * on the client are blocked by the nfsc_lock being exclusively held by the
289 * caller of this function.
292 nfscl_ngetreopen(struct mount *mntp, u_int8_t *fhp, int fhsize,
293 struct thread *td, struct nfsnode **npp)
301 /* For forced dismounts, just return error. */
302 if ((mntp->mnt_kern_flag & MNTK_UNMOUNTF))
304 MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) + fhsize,
306 bcopy(fhp, &nfhp->nfh_fh[0], fhsize);
307 nfhp->nfh_len = fhsize;
309 hash = fnv_32_buf(fhp, fhsize, FNV1_32_INIT);
312 * First, try to get the vnode locked, but don't block for the lock.
314 error = vfs_hash_get(mntp, hash, (LK_EXCLUSIVE | LK_NOWAIT), td, &nvp,
315 newnfs_vncmpf, nfhp);
316 if (error == 0 && nvp != NULL) {
317 NFSVOPUNLOCK(nvp, 0);
318 } else if (error == EBUSY) {
320 * The LK_EXCLOTHER lock type tells nfs_lock1() to not try
321 * and lock the vnode, but just get a v_usecount on it.
322 * LK_NOWAIT is set so that when vget() returns ENOENT,
323 * vfs_hash_get() fails instead of looping.
324 * If this succeeds, it is safe so long as a vflush() with
325 * FORCECLOSE has not been done. Since the Renew thread is
326 * stopped and the MNTK_UNMOUNTF flag is set before doing
327 * a vflush() with FORCECLOSE, we should be ok here.
329 if ((mntp->mnt_kern_flag & MNTK_UNMOUNTF))
332 error = vfs_hash_get(mntp, hash,
333 (LK_EXCLOTHER | LK_NOWAIT), td, &nvp,
334 newnfs_vncmpf, nfhp);
347 * Load the attribute cache (that lives in the nfsnode entry) with
348 * the attributes of the second argument and
350 * copy the attributes to *vaper
351 * Similar to nfs_loadattrcache(), except the attributes are passed in
352 * instead of being parsed out of the mbuf list.
355 nfscl_loadattrcache(struct vnode **vpp, struct nfsvattr *nap, void *nvaper,
356 void *stuff, int writeattr, int dontshrink)
358 struct vnode *vp = *vpp;
359 struct vattr *vap, *nvap = &nap->na_vattr, *vaper = nvaper;
361 struct nfsmount *nmp;
362 struct timespec mtime_save;
367 * If v_type == VNON it is a new node, so fill in the v_type,
368 * n_mtime fields. Check to see if it represents a special
369 * device, and if so, check for a possible alias. Once the
370 * correct vnode has been obtained, fill in the rest of the
375 if (vp->v_type != nvap->va_type) {
376 vp->v_type = nvap->va_type;
377 if (vp->v_type == VFIFO)
378 vp->v_op = &newnfs_fifoops;
379 np->n_mtime = nvap->va_mtime;
381 nmp = VFSTONFS(vp->v_mount);
382 vap = &np->n_vattr.na_vattr;
383 mtime_save = vap->va_mtime;
385 np->n_vattr.na_filerev = nap->na_filerev;
386 np->n_vattr.na_size = nap->na_size;
387 np->n_vattr.na_mtime = nap->na_mtime;
388 np->n_vattr.na_ctime = nap->na_ctime;
389 np->n_vattr.na_fsid = nap->na_fsid;
390 np->n_vattr.na_mode = nap->na_mode;
392 NFSBCOPY((caddr_t)nap, (caddr_t)&np->n_vattr,
393 sizeof (struct nfsvattr));
397 * For NFSv4, if the node's fsid is not equal to the mount point's
398 * fsid, return the low order 32bits of the node's fsid. This
399 * allows getcwd(3) to work. There is a chance that the fsid might
400 * be the same as a local fs, but since this is in an NFS mount
401 * point, I don't think that will cause any problems?
403 if (NFSHASNFSV4(nmp) && NFSHASHASSETFSID(nmp) &&
404 (nmp->nm_fsid[0] != np->n_vattr.na_filesid[0] ||
405 nmp->nm_fsid[1] != np->n_vattr.na_filesid[1])) {
407 * va_fsid needs to be set to some value derived from
408 * np->n_vattr.na_filesid that is not equal
409 * vp->v_mount->mnt_stat.f_fsid[0], so that it changes
410 * from the value used for the top level server volume
411 * in the mounted subtree.
413 if (vp->v_mount->mnt_stat.f_fsid.val[0] !=
414 (uint32_t)np->n_vattr.na_filesid[0])
415 vap->va_fsid = (uint32_t)np->n_vattr.na_filesid[0];
417 vap->va_fsid = (uint32_t)hash32_buf(
418 np->n_vattr.na_filesid, 2 * sizeof(uint64_t), 0);
420 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
421 np->n_attrstamp = time_second;
424 if (vap->va_size != np->n_size) {
425 if (vap->va_type == VREG) {
426 if (dontshrink && vap->va_size < np->n_size) {
428 * We've been told not to shrink the file;
429 * zero np->n_attrstamp to indicate that
430 * the attributes are stale.
432 vap->va_size = np->n_size;
434 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
435 vnode_pager_setsize(vp, np->n_size);
436 } else if (np->n_flag & NMODIFIED) {
438 * We've modified the file: Use the larger
439 * of our size, and the server's size.
441 if (vap->va_size < np->n_size) {
442 vap->va_size = np->n_size;
444 np->n_size = vap->va_size;
445 np->n_flag |= NSIZECHANGED;
447 vnode_pager_setsize(vp, np->n_size);
448 } else if (vap->va_size < np->n_size) {
450 * When shrinking the size, the call to
451 * vnode_pager_setsize() cannot be done
452 * with the mutex held, so delay it until
453 * after the mtx_unlock call.
455 nsize = np->n_size = vap->va_size;
456 np->n_flag |= NSIZECHANGED;
459 np->n_size = vap->va_size;
460 np->n_flag |= NSIZECHANGED;
461 vnode_pager_setsize(vp, np->n_size);
464 np->n_size = vap->va_size;
468 * The following checks are added to prevent a race between (say)
469 * a READDIR+ and a WRITE.
470 * READDIR+, WRITE requests sent out.
471 * READDIR+ resp, WRITE resp received on client.
472 * However, the WRITE resp was handled before the READDIR+ resp
473 * causing the post op attrs from the write to be loaded first
474 * and the attrs from the READDIR+ to be loaded later. If this
475 * happens, we have stale attrs loaded into the attrcache.
476 * We detect this by for the mtime moving back. We invalidate the
477 * attrcache when this happens.
479 if (timespeccmp(&mtime_save, &vap->va_mtime, >)) {
480 /* Size changed or mtime went backwards */
482 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
485 NFSBCOPY((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
486 if (np->n_flag & NCHG) {
487 if (np->n_flag & NACC)
488 vaper->va_atime = np->n_atim;
489 if (np->n_flag & NUPD)
490 vaper->va_mtime = np->n_mtim;
494 if (np->n_attrstamp != 0)
495 KDTRACE_NFS_ATTRCACHE_LOAD_DONE(vp, vap, 0);
499 vnode_pager_setsize(vp, nsize);
504 * Fill in the client id name. For these bytes:
505 * 1 - they must be unique
506 * 2 - they should be persistent across client reboots
507 * 1 is more critical than 2
508 * Use the mount point's unique id plus either the uuid or, if that
509 * isn't set, random junk.
512 nfscl_fillclid(u_int64_t clval, char *uuid, u_int8_t *cp, u_int16_t idlen)
517 * First, put in the 64bit mount point identifier.
519 if (idlen >= sizeof (u_int64_t)) {
520 NFSBCOPY((caddr_t)&clval, cp, sizeof (u_int64_t));
521 cp += sizeof (u_int64_t);
522 idlen -= sizeof (u_int64_t);
526 * If uuid is non-zero length, use it.
528 uuidlen = strlen(uuid);
529 if (uuidlen > 0 && idlen >= uuidlen) {
530 NFSBCOPY(uuid, cp, uuidlen);
536 * This only normally happens if the uuid isn't set.
539 *cp++ = (u_int8_t)(arc4random() % 256);
545 * Fill in a lock owner name. For now, pid + the process's creation time.
548 nfscl_filllockowner(void *id, u_int8_t *cp, int flags)
558 bzero(cp, NFSV4CL_LOCKNAMELEN);
561 if ((flags & F_POSIX) != 0) {
562 p = (struct proc *)id;
568 tl.lval = p->p_stats->p_start.tv_sec;
573 tl.lval = p->p_stats->p_start.tv_usec;
578 } else if ((flags & F_FLOCK) != 0) {
579 bcopy(&id, cp, sizeof(id));
580 bzero(&cp[sizeof(id)], NFSV4CL_LOCKNAMELEN - sizeof(id));
582 printf("nfscl_filllockowner: not F_POSIX or F_FLOCK\n");
583 bzero(cp, NFSV4CL_LOCKNAMELEN);
588 * Find the parent process for the thread passed in as an argument.
589 * If none exists, return NULL, otherwise return a thread for the parent.
590 * (Can be any of the threads, since it is only used for td->td_proc.)
593 nfscl_getparent(struct thread *td)
606 ptd = TAILQ_FIRST(&p->p_threads);
611 * Start up the renew kernel thread.
614 start_nfscl(void *arg)
616 struct nfsclclient *clp;
619 clp = (struct nfsclclient *)arg;
620 td = TAILQ_FIRST(&clp->nfsc_renewthread->p_threads);
621 nfscl_renewthread(clp, td);
626 nfscl_start_renewthread(struct nfsclclient *clp)
629 kproc_create(start_nfscl, (void *)clp, &clp->nfsc_renewthread, 0, 0,
635 * For NFSv4, it assumes that nfsv4_wccattr() was used to set up the getattr
636 * as the first Op after PutFH.
637 * (For NFSv4, the postop attributes are after the Op, so they can't be
638 * parsed here. A separate call to nfscl_postop_attr() is required.)
641 nfscl_wcc_data(struct nfsrv_descript *nd, struct vnode *vp,
642 struct nfsvattr *nap, int *flagp, int *wccflagp, void *stuff)
645 struct nfsnode *np = VTONFS(vp);
646 struct nfsvattr nfsva;
649 if (wccflagp != NULL)
651 if (nd->nd_flag & ND_NFSV3) {
653 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
654 if (*tl == newnfs_true) {
655 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
656 if (wccflagp != NULL) {
657 mtx_lock(&np->n_mtx);
658 *wccflagp = (np->n_mtime.tv_sec ==
659 fxdr_unsigned(u_int32_t, *(tl + 2)) &&
660 np->n_mtime.tv_nsec ==
661 fxdr_unsigned(u_int32_t, *(tl + 3)));
662 mtx_unlock(&np->n_mtx);
665 error = nfscl_postop_attr(nd, nap, flagp, stuff);
666 } else if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR))
667 == (ND_NFSV4 | ND_V4WCCATTR)) {
668 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
669 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
670 NULL, NULL, NULL, NULL, NULL);
674 * Get rid of Op# and status for next op.
676 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
678 nd->nd_flag |= ND_NOMOREDATA;
679 if (wccflagp != NULL &&
680 nfsva.na_vattr.va_mtime.tv_sec != 0) {
681 mtx_lock(&np->n_mtx);
682 *wccflagp = (np->n_mtime.tv_sec ==
683 nfsva.na_vattr.va_mtime.tv_sec &&
684 np->n_mtime.tv_nsec ==
685 nfsva.na_vattr.va_mtime.tv_sec);
686 mtx_unlock(&np->n_mtx);
694 * Get postop attributes.
697 nfscl_postop_attr(struct nfsrv_descript *nd, struct nfsvattr *nap, int *retp,
704 if (nd->nd_flag & ND_NOMOREDATA)
706 if (nd->nd_flag & ND_NFSV3) {
707 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
708 *retp = fxdr_unsigned(int, *tl);
709 } else if (nd->nd_flag & ND_NFSV4) {
711 * For NFSv4, the postop attr are at the end, so no point
712 * in looking if nd_repstat != 0.
714 if (!nd->nd_repstat) {
715 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
717 /* should never happen since nd_repstat != 0 */
718 nd->nd_flag |= ND_NOMOREDATA;
722 } else if (!nd->nd_repstat) {
723 /* For NFSv2, the attributes are here iff nd_repstat == 0 */
727 error = nfsm_loadattr(nd, nap);
736 * Fill in the setable attributes. The full argument indicates whether
737 * to fill in them all or just mode and time.
740 nfscl_fillsattr(struct nfsrv_descript *nd, struct vattr *vap,
741 struct vnode *vp, int flags, u_int32_t rdev)
744 struct nfsv2_sattr *sp;
745 nfsattrbit_t attrbits;
747 switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
749 NFSM_BUILD(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
750 if (vap->va_mode == (mode_t)VNOVAL)
751 sp->sa_mode = newnfs_xdrneg1;
753 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
754 if (vap->va_uid == (uid_t)VNOVAL)
755 sp->sa_uid = newnfs_xdrneg1;
757 sp->sa_uid = txdr_unsigned(vap->va_uid);
758 if (vap->va_gid == (gid_t)VNOVAL)
759 sp->sa_gid = newnfs_xdrneg1;
761 sp->sa_gid = txdr_unsigned(vap->va_gid);
762 if (flags & NFSSATTR_SIZE0)
764 else if (flags & NFSSATTR_SIZENEG1)
765 sp->sa_size = newnfs_xdrneg1;
766 else if (flags & NFSSATTR_SIZERDEV)
767 sp->sa_size = txdr_unsigned(rdev);
769 sp->sa_size = txdr_unsigned(vap->va_size);
770 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
771 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
774 if (vap->va_mode != (mode_t)VNOVAL) {
775 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
777 *tl = txdr_unsigned(vap->va_mode);
779 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
782 if ((flags & NFSSATTR_FULL) && vap->va_uid != (uid_t)VNOVAL) {
783 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
785 *tl = txdr_unsigned(vap->va_uid);
787 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
790 if ((flags & NFSSATTR_FULL) && vap->va_gid != (gid_t)VNOVAL) {
791 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
793 *tl = txdr_unsigned(vap->va_gid);
795 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
798 if ((flags & NFSSATTR_FULL) && vap->va_size != VNOVAL) {
799 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
801 txdr_hyper(vap->va_size, tl);
803 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
806 if (vap->va_atime.tv_sec != VNOVAL) {
807 if ((vap->va_vaflags & VA_UTIMES_NULL) == 0) {
808 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
809 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
810 txdr_nfsv3time(&vap->va_atime, tl);
812 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
813 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
816 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
817 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
819 if (vap->va_mtime.tv_sec != VNOVAL) {
820 if ((vap->va_vaflags & VA_UTIMES_NULL) == 0) {
821 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
822 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
823 txdr_nfsv3time(&vap->va_mtime, tl);
825 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
826 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
829 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
830 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
834 NFSZERO_ATTRBIT(&attrbits);
835 if (vap->va_mode != (mode_t)VNOVAL)
836 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_MODE);
837 if ((flags & NFSSATTR_FULL) && vap->va_uid != (uid_t)VNOVAL)
838 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_OWNER);
839 if ((flags & NFSSATTR_FULL) && vap->va_gid != (gid_t)VNOVAL)
840 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_OWNERGROUP);
841 if ((flags & NFSSATTR_FULL) && vap->va_size != VNOVAL)
842 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
843 if (vap->va_atime.tv_sec != VNOVAL)
844 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESSSET);
845 if (vap->va_mtime.tv_sec != VNOVAL)
846 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFYSET);
847 (void) nfsv4_fillattr(nd, vp->v_mount, vp, NULL, vap, NULL, 0,
848 &attrbits, NULL, NULL, 0, 0, 0, 0, (uint64_t)0);
854 * nfscl_request() - mostly a wrapper for newnfs_request().
857 nfscl_request(struct nfsrv_descript *nd, struct vnode *vp, NFSPROC_T *p,
858 struct ucred *cred, void *stuff)
861 struct nfsmount *nmp;
863 nmp = VFSTONFS(vp->v_mount);
864 if (nd->nd_flag & ND_NFSV4)
866 else if (nd->nd_flag & ND_NFSV3)
870 ret = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
871 NFS_PROG, vers, NULL, 1, NULL, NULL);
876 * fill in this bsden's variant of statfs using nfsstatfs.
879 nfscl_loadsbinfo(struct nfsmount *nmp, struct nfsstatfs *sfp, void *statfs)
881 struct statfs *sbp = (struct statfs *)statfs;
883 if (nmp->nm_flag & (NFSMNT_NFSV3 | NFSMNT_NFSV4)) {
884 sbp->f_bsize = NFS_FABLKSIZE;
885 sbp->f_blocks = sfp->sf_tbytes / NFS_FABLKSIZE;
886 sbp->f_bfree = sfp->sf_fbytes / NFS_FABLKSIZE;
888 * Although sf_abytes is uint64_t and f_bavail is int64_t,
889 * the value after dividing by NFS_FABLKSIZE is small
890 * enough that it will fit in 63bits, so it is ok to
891 * assign it to f_bavail without fear that it will become
894 sbp->f_bavail = sfp->sf_abytes / NFS_FABLKSIZE;
895 sbp->f_files = sfp->sf_tfiles;
896 /* Since f_ffree is int64_t, clip it to 63bits. */
897 if (sfp->sf_ffiles > INT64_MAX)
898 sbp->f_ffree = INT64_MAX;
900 sbp->f_ffree = sfp->sf_ffiles;
901 } else if ((nmp->nm_flag & NFSMNT_NFSV4) == 0) {
903 * The type casts to (int32_t) ensure that this code is
904 * compatible with the old NFS client, in that it will
905 * propagate bit31 to the high order bits. This may or may
906 * not be correct for NFSv2, but since it is a legacy
907 * environment, I'd rather retain backwards compatibility.
909 sbp->f_bsize = (int32_t)sfp->sf_bsize;
910 sbp->f_blocks = (int32_t)sfp->sf_blocks;
911 sbp->f_bfree = (int32_t)sfp->sf_bfree;
912 sbp->f_bavail = (int32_t)sfp->sf_bavail;
919 * Use the fsinfo stuff to update the mount point.
922 nfscl_loadfsinfo(struct nfsmount *nmp, struct nfsfsinfo *fsp)
925 if ((nmp->nm_wsize == 0 || fsp->fs_wtpref < nmp->nm_wsize) &&
926 fsp->fs_wtpref >= NFS_FABLKSIZE)
927 nmp->nm_wsize = (fsp->fs_wtpref + NFS_FABLKSIZE - 1) &
928 ~(NFS_FABLKSIZE - 1);
929 if (fsp->fs_wtmax < nmp->nm_wsize && fsp->fs_wtmax > 0) {
930 nmp->nm_wsize = fsp->fs_wtmax & ~(NFS_FABLKSIZE - 1);
931 if (nmp->nm_wsize == 0)
932 nmp->nm_wsize = fsp->fs_wtmax;
934 if (nmp->nm_wsize < NFS_FABLKSIZE)
935 nmp->nm_wsize = NFS_FABLKSIZE;
936 if ((nmp->nm_rsize == 0 || fsp->fs_rtpref < nmp->nm_rsize) &&
937 fsp->fs_rtpref >= NFS_FABLKSIZE)
938 nmp->nm_rsize = (fsp->fs_rtpref + NFS_FABLKSIZE - 1) &
939 ~(NFS_FABLKSIZE - 1);
940 if (fsp->fs_rtmax < nmp->nm_rsize && fsp->fs_rtmax > 0) {
941 nmp->nm_rsize = fsp->fs_rtmax & ~(NFS_FABLKSIZE - 1);
942 if (nmp->nm_rsize == 0)
943 nmp->nm_rsize = fsp->fs_rtmax;
945 if (nmp->nm_rsize < NFS_FABLKSIZE)
946 nmp->nm_rsize = NFS_FABLKSIZE;
947 if ((nmp->nm_readdirsize == 0 || fsp->fs_dtpref < nmp->nm_readdirsize)
948 && fsp->fs_dtpref >= NFS_DIRBLKSIZ)
949 nmp->nm_readdirsize = (fsp->fs_dtpref + NFS_DIRBLKSIZ - 1) &
950 ~(NFS_DIRBLKSIZ - 1);
951 if (fsp->fs_rtmax < nmp->nm_readdirsize && fsp->fs_rtmax > 0) {
952 nmp->nm_readdirsize = fsp->fs_rtmax & ~(NFS_DIRBLKSIZ - 1);
953 if (nmp->nm_readdirsize == 0)
954 nmp->nm_readdirsize = fsp->fs_rtmax;
956 if (nmp->nm_readdirsize < NFS_DIRBLKSIZ)
957 nmp->nm_readdirsize = NFS_DIRBLKSIZ;
958 if (fsp->fs_maxfilesize > 0 &&
959 fsp->fs_maxfilesize < nmp->nm_maxfilesize)
960 nmp->nm_maxfilesize = fsp->fs_maxfilesize;
961 nmp->nm_mountp->mnt_stat.f_iosize = newnfs_iosize(nmp);
962 nmp->nm_state |= NFSSTA_GOTFSINFO;
966 * Get a pointer to my IP addrress and return it.
967 * Return NULL if you can't find one.
970 nfscl_getmyip(struct nfsmount *nmp, int *isinet6p)
972 struct sockaddr_in sad, *sin;
974 u_int8_t *retp = NULL;
975 static struct in_addr laddr;
979 * Loop up a route for the destination address.
981 if (nmp->nm_nam->sa_family == AF_INET) {
982 bzero(&sad, sizeof (sad));
983 sin = (struct sockaddr_in *)nmp->nm_nam;
984 sad.sin_family = AF_INET;
985 sad.sin_len = sizeof (struct sockaddr_in);
986 sad.sin_addr.s_addr = sin->sin_addr.s_addr;
987 CURVNET_SET(CRED_TO_VNET(nmp->nm_sockreq.nr_cred));
988 rt = rtalloc1_fib((struct sockaddr *)&sad, 0, 0UL,
989 curthread->td_proc->p_fibnum);
991 if (rt->rt_ifp != NULL &&
992 rt->rt_ifa != NULL &&
993 ((rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) &&
994 rt->rt_ifa->ifa_addr->sa_family == AF_INET) {
995 sin = (struct sockaddr_in *)
996 rt->rt_ifa->ifa_addr;
997 laddr.s_addr = sin->sin_addr.s_addr;
998 retp = (u_int8_t *)&laddr;
1004 } else if (nmp->nm_nam->sa_family == AF_INET6) {
1005 struct sockaddr_in6 sad6, *sin6;
1006 static struct in6_addr laddr6;
1008 bzero(&sad6, sizeof (sad6));
1009 sin6 = (struct sockaddr_in6 *)nmp->nm_nam;
1010 sad6.sin6_family = AF_INET6;
1011 sad6.sin6_len = sizeof (struct sockaddr_in6);
1012 sad6.sin6_addr = sin6->sin6_addr;
1013 CURVNET_SET(CRED_TO_VNET(nmp->nm_sockreq.nr_cred));
1014 rt = rtalloc1_fib((struct sockaddr *)&sad6, 0, 0UL,
1015 curthread->td_proc->p_fibnum);
1017 if (rt->rt_ifp != NULL &&
1018 rt->rt_ifa != NULL &&
1019 ((rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) &&
1020 rt->rt_ifa->ifa_addr->sa_family == AF_INET6) {
1021 sin6 = (struct sockaddr_in6 *)
1022 rt->rt_ifa->ifa_addr;
1023 laddr6 = sin6->sin6_addr;
1024 retp = (u_int8_t *)&laddr6;
1036 * Copy NFS uid, gids from the cred structure.
1039 newnfs_copyincred(struct ucred *cr, struct nfscred *nfscr)
1043 KASSERT(cr->cr_ngroups >= 0,
1044 ("newnfs_copyincred: negative cr_ngroups"));
1045 nfscr->nfsc_uid = cr->cr_uid;
1046 nfscr->nfsc_ngroups = MIN(cr->cr_ngroups, NFS_MAXGRPS + 1);
1047 for (i = 0; i < nfscr->nfsc_ngroups; i++)
1048 nfscr->nfsc_groups[i] = cr->cr_groups[i];
1053 * Do any client specific initialization.
1058 static int inited = 0;
1064 ncl_pbuf_freecnt = nswbuf / 2 + 1;
1068 * Check each of the attributes to be set, to ensure they aren't already
1069 * the correct value. Disable setting ones already correct.
1072 nfscl_checksattr(struct vattr *vap, struct nfsvattr *nvap)
1075 if (vap->va_mode != (mode_t)VNOVAL) {
1076 if (vap->va_mode == nvap->na_mode)
1077 vap->va_mode = (mode_t)VNOVAL;
1079 if (vap->va_uid != (uid_t)VNOVAL) {
1080 if (vap->va_uid == nvap->na_uid)
1081 vap->va_uid = (uid_t)VNOVAL;
1083 if (vap->va_gid != (gid_t)VNOVAL) {
1084 if (vap->va_gid == nvap->na_gid)
1085 vap->va_gid = (gid_t)VNOVAL;
1087 if (vap->va_size != VNOVAL) {
1088 if (vap->va_size == nvap->na_size)
1089 vap->va_size = VNOVAL;
1093 * We are normally called with only a partially initialized
1094 * VAP. Since the NFSv3 spec says that server may use the
1095 * file attributes to store the verifier, the spec requires
1096 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1097 * in atime, but we can't really assume that all servers will
1098 * so we ensure that our SETATTR sets both atime and mtime.
1100 if (vap->va_mtime.tv_sec == VNOVAL)
1101 vfs_timestamp(&vap->va_mtime);
1102 if (vap->va_atime.tv_sec == VNOVAL)
1103 vap->va_atime = vap->va_mtime;
1108 * Map nfsv4 errors to errno.h errors.
1109 * The uid and gid arguments are only used for NFSERR_BADOWNER and that
1110 * error should only be returned for the Open, Create and Setattr Ops.
1111 * As such, most calls can just pass in 0 for those arguments.
1114 nfscl_maperr(struct thread *td, int error, uid_t uid, gid_t gid)
1125 case NFSERR_BADOWNER:
1126 tprintf(p, LOG_INFO,
1127 "No name and/or group mapping for uid,gid:(%d,%d)\n",
1130 case NFSERR_BADNAME:
1131 case NFSERR_BADCHAR:
1132 printf("nfsv4 char/name not handled by server\n");
1134 case NFSERR_STALECLIENTID:
1135 case NFSERR_STALESTATEID:
1136 case NFSERR_EXPIRED:
1137 case NFSERR_BADSTATEID:
1138 case NFSERR_BADSESSION:
1139 printf("nfsv4 recover err returned %d\n", error);
1141 case NFSERR_BADHANDLE:
1142 case NFSERR_SERVERFAULT:
1143 case NFSERR_BADTYPE:
1144 case NFSERR_FHEXPIRED:
1145 case NFSERR_RESOURCE:
1147 case NFSERR_NOFILEHANDLE:
1148 case NFSERR_MINORVERMISMATCH:
1149 case NFSERR_OLDSTATEID:
1150 case NFSERR_BADSEQID:
1151 case NFSERR_LEASEMOVED:
1152 case NFSERR_RECLAIMBAD:
1154 case NFSERR_OPILLEGAL:
1155 printf("nfsv4 client/server protocol prob err=%d\n",
1159 tprintf(p, LOG_INFO, "nfsv4 err=%d\n", error);
1165 * Check to see if the process for this owner exists. Return 1 if it doesn't
1169 nfscl_procdoesntexist(u_int8_t *own)
1179 tl.cval[0] = *own++;
1180 tl.cval[1] = *own++;
1181 tl.cval[2] = *own++;
1182 tl.cval[3] = *own++;
1184 p = pfind_locked(pid);
1187 if (p->p_stats == NULL) {
1191 tl.cval[0] = *own++;
1192 tl.cval[1] = *own++;
1193 tl.cval[2] = *own++;
1194 tl.cval[3] = *own++;
1195 if (tl.lval != p->p_stats->p_start.tv_sec) {
1198 tl.cval[0] = *own++;
1199 tl.cval[1] = *own++;
1200 tl.cval[2] = *own++;
1202 if (tl.lval != p->p_stats->p_start.tv_usec)
1210 * - nfs pseudo system call for the client
1216 nfssvc_nfscl(struct thread *td, struct nfssvc_args *uap)
1219 struct nfscbd_args nfscbdarg;
1220 struct nfsd_nfscbd_args nfscbdarg2;
1221 struct nameidata nd;
1222 struct nfscl_dumpmntopts dumpmntopts;
1223 cap_rights_t rights;
1227 if (uap->flag & NFSSVC_CBADDSOCK) {
1228 error = copyin(uap->argp, (caddr_t)&nfscbdarg, sizeof(nfscbdarg));
1232 * Since we don't know what rights might be required,
1233 * pretend that we need them all. It is better to be too
1234 * careful than too reckless.
1236 error = fget(td, nfscbdarg.sock,
1237 cap_rights_init(&rights, CAP_SOCK_CLIENT), &fp);
1240 if (fp->f_type != DTYPE_SOCKET) {
1244 error = nfscbd_addsock(fp);
1246 if (!error && nfscl_enablecallb == 0) {
1247 nfsv4_cbport = nfscbdarg.port;
1248 nfscl_enablecallb = 1;
1250 } else if (uap->flag & NFSSVC_NFSCBD) {
1251 if (uap->argp == NULL)
1253 error = copyin(uap->argp, (caddr_t)&nfscbdarg2,
1254 sizeof(nfscbdarg2));
1257 error = nfscbd_nfsd(td, &nfscbdarg2);
1258 } else if (uap->flag & NFSSVC_DUMPMNTOPTS) {
1259 error = copyin(uap->argp, &dumpmntopts, sizeof(dumpmntopts));
1260 if (error == 0 && (dumpmntopts.ndmnt_blen < 256 ||
1261 dumpmntopts.ndmnt_blen > 1024))
1264 error = nfsrv_lookupfilename(&nd,
1265 dumpmntopts.ndmnt_fname, td);
1266 if (error == 0 && strcmp(nd.ni_vp->v_mount->mnt_vfc->vfc_name,
1272 buf = malloc(dumpmntopts.ndmnt_blen, M_TEMP, M_WAITOK);
1273 nfscl_retopts(VFSTONFS(nd.ni_vp->v_mount), buf,
1274 dumpmntopts.ndmnt_blen);
1276 error = copyout(buf, dumpmntopts.ndmnt_buf,
1277 dumpmntopts.ndmnt_blen);
1286 extern int (*nfsd_call_nfscl)(struct thread *, struct nfssvc_args *);
1289 * Called once to initialize data structures...
1292 nfscl_modevent(module_t mod, int type, void *data)
1295 static int loaded = 0;
1302 mtx_init(&nfs_clstate_mutex, "nfs_clstate_mutex", NULL,
1304 mtx_init(&ncl_iod_mutex, "ncl_iod_mutex", NULL, MTX_DEF);
1309 ncl_call_invalcaches = ncl_invalcaches;
1310 nfsd_call_nfscl = nfssvc_nfscl;
1315 if (nfs_numnfscbd != 0) {
1321 * XXX: Unloading of nfscl module is unsupported.
1324 ncl_call_invalcaches = NULL;
1325 nfsd_call_nfscl = NULL;
1326 /* and get rid of the mutexes */
1327 mtx_destroy(&nfs_clstate_mutex);
1328 mtx_destroy(&ncl_iod_mutex);
1340 static moduledata_t nfscl_mod = {
1345 DECLARE_MODULE(nfscl, nfscl_mod, SI_SUB_VFS, SI_ORDER_FIRST);
1347 /* So that loader and kldload(2) can find us, wherever we are.. */
1348 MODULE_VERSION(nfscl, 1);
1349 MODULE_DEPEND(nfscl, nfscommon, 1, 1, 1);
1350 MODULE_DEPEND(nfscl, krpc, 1, 1, 1);
1351 MODULE_DEPEND(nfscl, nfssvc, 1, 1, 1);
1352 MODULE_DEPEND(nfscl, nfslock, 1, 1, 1);