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"
40 * generally, I don't like #includes inside .h files, but it seems to
41 * be the easiest way to handle the port.
44 #include <fs/nfs/nfsport.h>
45 #include <netinet/if_ether.h>
46 #include <net/if_types.h>
48 extern u_int32_t newnfs_true, newnfs_false, newnfs_xdrneg1;
49 extern struct vop_vector newnfs_vnodeops;
50 extern struct vop_vector newnfs_fifoops;
51 extern uma_zone_t newnfsnode_zone;
52 extern struct buf_ops buf_ops_newnfs;
53 extern int ncl_pbuf_freecnt;
54 extern short nfsv4_cbport;
55 extern int nfscl_enablecallb;
56 extern int nfs_numnfscbd;
57 extern int nfscl_inited;
58 struct mtx nfs_clstate_mutex;
59 struct mtx ncl_iod_mutex;
62 extern void (*ncl_call_invalcaches)(struct vnode *);
65 * Comparison function for vfs_hash functions.
68 newnfs_vncmpf(struct vnode *vp, void *arg)
70 struct nfsfh *nfhp = (struct nfsfh *)arg;
71 struct nfsnode *np = VTONFS(vp);
73 if (np->n_fhp->nfh_len != nfhp->nfh_len ||
74 NFSBCMP(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len))
80 * Look up a vnode/nfsnode by file handle.
81 * Callers must check for mount points!!
82 * In all cases, a pointer to a
83 * nfsnode structure is returned.
84 * This variant takes a "struct nfsfh *" as second argument and uses
85 * that structure up, either by hanging off the nfsnode or FREEing it.
88 nfscl_nget(struct mount *mntp, struct vnode *dvp, struct nfsfh *nfhp,
89 struct componentname *cnp, struct thread *td, struct nfsnode **npp,
90 void *stuff, int lkflags)
92 struct nfsnode *np, *dnp;
93 struct vnode *vp, *nvp;
94 struct nfsv4node *newd, *oldd;
103 hash = fnv_32_buf(nfhp->nfh_fh, nfhp->nfh_len, FNV1_32_INIT);
105 error = vfs_hash_get(mntp, hash, lkflags,
106 td, &nvp, newnfs_vncmpf, nfhp);
107 if (error == 0 && nvp != NULL) {
109 * I believe there is a slight chance that vgonel() could
110 * get called on this vnode between when NFSVOPLOCK() drops
111 * the VI_LOCK() and vget() acquires it again, so that it
112 * hasn't yet had v_usecount incremented. If this were to
113 * happen, the VI_DOOMED flag would be set, so check for
114 * that here. Since we now have the v_usecount incremented,
115 * we should be ok until we vrele() it, if the VI_DOOMED
116 * flag isn't set now.
119 if ((nvp->v_iflag & VI_DOOMED)) {
128 FREE((caddr_t)nfhp, M_NFSFH);
134 * For NFSv4, check to see if it is the same name and
135 * replace the name, if it is different.
138 if ((nmp->nm_flag & NFSMNT_NFSV4) && np->n_v4 != NULL &&
139 nvp->v_type == VREG &&
140 (np->n_v4->n4_namelen != cnp->cn_namelen ||
141 NFSBCMP(cnp->cn_nameptr, NFS4NODENAME(np->n_v4),
143 dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen ||
144 NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
145 dnp->n_fhp->nfh_len))) {
146 MALLOC(newd, struct nfsv4node *,
147 sizeof (struct nfsv4node) + dnp->n_fhp->nfh_len +
148 + cnp->cn_namelen - 1, M_NFSV4NODE, M_WAITOK);
150 if (newd != NULL && np->n_v4 != NULL && nvp->v_type == VREG
151 && (np->n_v4->n4_namelen != cnp->cn_namelen ||
152 NFSBCMP(cnp->cn_nameptr, NFS4NODENAME(np->n_v4),
154 dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen ||
155 NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
156 dnp->n_fhp->nfh_len))) {
160 np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
161 np->n_v4->n4_namelen = cnp->cn_namelen;
162 NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
163 dnp->n_fhp->nfh_len);
164 NFSBCOPY(cnp->cn_nameptr, NFS4NODENAME(np->n_v4),
170 FREE((caddr_t)newd, M_NFSV4NODE);
172 FREE((caddr_t)oldd, M_NFSV4NODE);
174 FREE((caddr_t)nfhp, M_NFSFH);
179 * Allocate before getnewvnode since doing so afterward
180 * might cause a bogus v_data pointer to get dereferenced
181 * elsewhere if zalloc should block.
183 np = uma_zalloc(newnfsnode_zone, M_WAITOK | M_ZERO);
185 error = getnewvnode("newnfs", mntp, &newnfs_vnodeops, &nvp);
187 uma_zfree(newnfsnode_zone, np);
188 FREE((caddr_t)nfhp, M_NFSFH);
192 KASSERT(vp->v_bufobj.bo_bsize != 0, ("nfscl_nget: bo_bsize == 0"));
193 vp->v_bufobj.bo_ops = &buf_ops_newnfs;
197 * Initialize the mutex even if the vnode is going to be a loser.
198 * This simplifies the logic in reclaim, which can then unconditionally
199 * destroy the mutex (in the case of the loser, or if hash_insert
200 * happened to return an error no special casing is needed).
202 mtx_init(&np->n_mtx, "NEWNFSnode lock", NULL, MTX_DEF | MTX_DUPOK);
205 * Are we getting the root? If so, make sure the vnode flags
208 if ((nfhp->nfh_len == nmp->nm_fhsize) &&
209 !bcmp(nfhp->nfh_fh, nmp->nm_fh, nfhp->nfh_len)) {
210 if (vp->v_type == VNON)
212 vp->v_vflag |= VV_ROOT;
217 * For NFSv4, we have to attach the directory file handle and
218 * file name, so that Open Ops can be done later.
220 if (nmp->nm_flag & NFSMNT_NFSV4) {
221 MALLOC(np->n_v4, struct nfsv4node *, sizeof (struct nfsv4node)
222 + dnp->n_fhp->nfh_len + cnp->cn_namelen - 1, M_NFSV4NODE,
224 np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
225 np->n_v4->n4_namelen = cnp->cn_namelen;
226 NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
227 dnp->n_fhp->nfh_len);
228 NFSBCOPY(cnp->cn_nameptr, NFS4NODENAME(np->n_v4),
235 * NFS supports recursive and shared locking.
239 lockmgr(vp->v_vnlock, LK_EXCLUSIVE | LK_NOWITNESS, NULL);
240 error = insmntque(vp, mntp);
243 mtx_destroy(&np->n_mtx);
244 FREE((caddr_t)nfhp, M_NFSFH);
245 if (np->n_v4 != NULL)
246 FREE((caddr_t)np->n_v4, M_NFSV4NODE);
247 uma_zfree(newnfsnode_zone, np);
250 error = vfs_hash_insert(vp, hash, lkflags,
251 td, &nvp, newnfs_vncmpf, nfhp);
256 /* vfs_hash_insert() vput()'s the losing vnode */
265 * Anothe variant of nfs_nget(). This one is only used by reopen. It
266 * takes almost the same args as nfs_nget(), but only succeeds if an entry
267 * exists in the cache. (Since files should already be "open" with a
268 * vnode ref cnt on the node when reopen calls this, it should always
270 * Also, don't get a vnode lock, since it may already be locked by some
271 * other process that is handling it. This is ok, since all other threads
272 * on the client are blocked by the nfsc_lock being exclusively held by the
273 * caller of this function.
276 nfscl_ngetreopen(struct mount *mntp, u_int8_t *fhp, int fhsize,
277 struct thread *td, struct nfsnode **npp)
285 /* For forced dismounts, just return error. */
286 if ((mntp->mnt_kern_flag & MNTK_UNMOUNTF))
288 MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) + fhsize,
290 bcopy(fhp, &nfhp->nfh_fh[0], fhsize);
291 nfhp->nfh_len = fhsize;
293 hash = fnv_32_buf(fhp, fhsize, FNV1_32_INIT);
296 * First, try to get the vnode locked, but don't block for the lock.
298 error = vfs_hash_get(mntp, hash, (LK_EXCLUSIVE | LK_NOWAIT), td, &nvp,
299 newnfs_vncmpf, nfhp);
300 if (error == 0 && nvp != NULL) {
301 NFSVOPUNLOCK(nvp, 0);
302 } else if (error == EBUSY) {
304 * The LK_EXCLOTHER lock type tells nfs_lock1() to not try
305 * and lock the vnode, but just get a v_usecount on it.
306 * LK_NOWAIT is set so that when vget() returns ENOENT,
307 * vfs_hash_get() fails instead of looping.
308 * If this succeeds, it is safe so long as a vflush() with
309 * FORCECLOSE has not been done. Since the Renew thread is
310 * stopped and the MNTK_UNMOUNTF flag is set before doing
311 * a vflush() with FORCECLOSE, we should be ok here.
313 if ((mntp->mnt_kern_flag & MNTK_UNMOUNTF))
316 error = vfs_hash_get(mntp, hash,
317 (LK_EXCLOTHER | LK_NOWAIT), td, &nvp,
318 newnfs_vncmpf, nfhp);
331 * Load the attribute cache (that lives in the nfsnode entry) with
332 * the attributes of the second argument and
334 * copy the attributes to *vaper
335 * Similar to nfs_loadattrcache(), except the attributes are passed in
336 * instead of being parsed out of the mbuf list.
339 nfscl_loadattrcache(struct vnode **vpp, struct nfsvattr *nap, void *nvaper,
340 void *stuff, int writeattr, int dontshrink)
342 struct vnode *vp = *vpp;
343 struct vattr *vap, *nvap = &nap->na_vattr, *vaper = nvaper;
345 struct nfsmount *nmp;
346 struct timespec mtime_save;
349 * If v_type == VNON it is a new node, so fill in the v_type,
350 * n_mtime fields. Check to see if it represents a special
351 * device, and if so, check for a possible alias. Once the
352 * correct vnode has been obtained, fill in the rest of the
357 if (vp->v_type != nvap->va_type) {
358 vp->v_type = nvap->va_type;
359 if (vp->v_type == VFIFO)
360 vp->v_op = &newnfs_fifoops;
361 np->n_mtime = nvap->va_mtime;
363 nmp = VFSTONFS(vp->v_mount);
364 vap = &np->n_vattr.na_vattr;
365 mtime_save = vap->va_mtime;
367 np->n_vattr.na_filerev = nap->na_filerev;
368 np->n_vattr.na_size = nap->na_size;
369 np->n_vattr.na_mtime = nap->na_mtime;
370 np->n_vattr.na_ctime = nap->na_ctime;
371 np->n_vattr.na_fsid = nap->na_fsid;
372 np->n_vattr.na_mode = nap->na_mode;
374 NFSBCOPY((caddr_t)nap, (caddr_t)&np->n_vattr,
375 sizeof (struct nfsvattr));
379 * For NFSv4, if the node's fsid is not equal to the mount point's
380 * fsid, return the low order 32bits of the node's fsid. This
381 * allows getcwd(3) to work. There is a chance that the fsid might
382 * be the same as a local fs, but since this is in an NFS mount
383 * point, I don't think that will cause any problems?
385 if (NFSHASNFSV4(nmp) && NFSHASHASSETFSID(nmp) &&
386 (nmp->nm_fsid[0] != np->n_vattr.na_filesid[0] ||
387 nmp->nm_fsid[1] != np->n_vattr.na_filesid[1])) {
389 * va_fsid needs to be set to some value derived from
390 * np->n_vattr.na_filesid that is not equal
391 * vp->v_mount->mnt_stat.f_fsid[0], so that it changes
392 * from the value used for the top level server volume
393 * in the mounted subtree.
395 if (vp->v_mount->mnt_stat.f_fsid.val[0] !=
396 (uint32_t)np->n_vattr.na_filesid[0])
397 vap->va_fsid = (uint32_t)np->n_vattr.na_filesid[0];
399 vap->va_fsid = (uint32_t)hash32_buf(
400 np->n_vattr.na_filesid, 2 * sizeof(uint64_t), 0);
402 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
403 np->n_attrstamp = time_second;
404 if (vap->va_size != np->n_size) {
405 if (vap->va_type == VREG) {
406 if (dontshrink && vap->va_size < np->n_size) {
408 * We've been told not to shrink the file;
409 * zero np->n_attrstamp to indicate that
410 * the attributes are stale.
412 vap->va_size = np->n_size;
414 } else if (np->n_flag & NMODIFIED) {
416 * We've modified the file: Use the larger
417 * of our size, and the server's size.
419 if (vap->va_size < np->n_size) {
420 vap->va_size = np->n_size;
422 np->n_size = vap->va_size;
423 np->n_flag |= NSIZECHANGED;
426 np->n_size = vap->va_size;
427 np->n_flag |= NSIZECHANGED;
429 vnode_pager_setsize(vp, np->n_size);
431 np->n_size = vap->va_size;
435 * The following checks are added to prevent a race between (say)
436 * a READDIR+ and a WRITE.
437 * READDIR+, WRITE requests sent out.
438 * READDIR+ resp, WRITE resp received on client.
439 * However, the WRITE resp was handled before the READDIR+ resp
440 * causing the post op attrs from the write to be loaded first
441 * and the attrs from the READDIR+ to be loaded later. If this
442 * happens, we have stale attrs loaded into the attrcache.
443 * We detect this by for the mtime moving back. We invalidate the
444 * attrcache when this happens.
446 if (timespeccmp(&mtime_save, &vap->va_mtime, >))
447 /* Size changed or mtime went backwards */
450 NFSBCOPY((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
451 if (np->n_flag & NCHG) {
452 if (np->n_flag & NACC)
453 vaper->va_atime = np->n_atim;
454 if (np->n_flag & NUPD)
455 vaper->va_mtime = np->n_mtim;
463 * Fill in the client id name. For these bytes:
464 * 1 - they must be unique
465 * 2 - they should be persistent across client reboots
466 * 1 is more critical than 2
467 * Use the mount point's unique id plus either the uuid or, if that
468 * isn't set, random junk.
471 nfscl_fillclid(u_int64_t clval, char *uuid, u_int8_t *cp, u_int16_t idlen)
476 * First, put in the 64bit mount point identifier.
478 if (idlen >= sizeof (u_int64_t)) {
479 NFSBCOPY((caddr_t)&clval, cp, sizeof (u_int64_t));
480 cp += sizeof (u_int64_t);
481 idlen -= sizeof (u_int64_t);
485 * If uuid is non-zero length, use it.
487 uuidlen = strlen(uuid);
488 if (uuidlen > 0 && idlen >= uuidlen) {
489 NFSBCOPY(uuid, cp, uuidlen);
495 * This only normally happens if the uuid isn't set.
498 *cp++ = (u_int8_t)(arc4random() % 256);
504 * Fill in a lock owner name. For now, pid + the process's creation time.
507 nfscl_filllockowner(void *id, u_int8_t *cp, int flags)
517 bzero(cp, NFSV4CL_LOCKNAMELEN);
520 if ((flags & F_POSIX) != 0) {
521 p = (struct proc *)id;
527 tl.lval = p->p_stats->p_start.tv_sec;
532 tl.lval = p->p_stats->p_start.tv_usec;
537 } else if ((flags & F_FLOCK) != 0) {
538 bcopy(&id, cp, sizeof(id));
539 bzero(&cp[sizeof(id)], NFSV4CL_LOCKNAMELEN - sizeof(id));
541 printf("nfscl_filllockowner: not F_POSIX or F_FLOCK\n");
542 bzero(cp, NFSV4CL_LOCKNAMELEN);
547 * Find the parent process for the thread passed in as an argument.
548 * If none exists, return NULL, otherwise return a thread for the parent.
549 * (Can be any of the threads, since it is only used for td->td_proc.)
552 nfscl_getparent(struct thread *td)
565 ptd = TAILQ_FIRST(&p->p_threads);
570 * Start up the renew kernel thread.
573 start_nfscl(void *arg)
575 struct nfsclclient *clp;
578 clp = (struct nfsclclient *)arg;
579 td = TAILQ_FIRST(&clp->nfsc_renewthread->p_threads);
580 nfscl_renewthread(clp, td);
585 nfscl_start_renewthread(struct nfsclclient *clp)
588 kproc_create(start_nfscl, (void *)clp, &clp->nfsc_renewthread, 0, 0,
594 * For NFSv4, it assumes that nfsv4_wccattr() was used to set up the getattr
595 * as the first Op after PutFH.
596 * (For NFSv4, the postop attributes are after the Op, so they can't be
597 * parsed here. A separate call to nfscl_postop_attr() is required.)
600 nfscl_wcc_data(struct nfsrv_descript *nd, struct vnode *vp,
601 struct nfsvattr *nap, int *flagp, int *wccflagp, void *stuff)
604 struct nfsnode *np = VTONFS(vp);
605 struct nfsvattr nfsva;
608 if (wccflagp != NULL)
610 if (nd->nd_flag & ND_NFSV3) {
612 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
613 if (*tl == newnfs_true) {
614 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
615 if (wccflagp != NULL) {
616 mtx_lock(&np->n_mtx);
617 *wccflagp = (np->n_mtime.tv_sec ==
618 fxdr_unsigned(u_int32_t, *(tl + 2)) &&
619 np->n_mtime.tv_nsec ==
620 fxdr_unsigned(u_int32_t, *(tl + 3)));
621 mtx_unlock(&np->n_mtx);
624 error = nfscl_postop_attr(nd, nap, flagp, stuff);
625 } else if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR))
626 == (ND_NFSV4 | ND_V4WCCATTR)) {
627 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
628 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
629 NULL, NULL, NULL, NULL, NULL);
633 * Get rid of Op# and status for next op.
635 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
637 nd->nd_flag |= ND_NOMOREDATA;
638 if (wccflagp != NULL &&
639 nfsva.na_vattr.va_mtime.tv_sec != 0) {
640 mtx_lock(&np->n_mtx);
641 *wccflagp = (np->n_mtime.tv_sec ==
642 nfsva.na_vattr.va_mtime.tv_sec &&
643 np->n_mtime.tv_nsec ==
644 nfsva.na_vattr.va_mtime.tv_sec);
645 mtx_unlock(&np->n_mtx);
653 * Get postop attributes.
656 nfscl_postop_attr(struct nfsrv_descript *nd, struct nfsvattr *nap, int *retp,
663 if (nd->nd_flag & ND_NOMOREDATA)
665 if (nd->nd_flag & ND_NFSV3) {
666 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
667 *retp = fxdr_unsigned(int, *tl);
668 } else if (nd->nd_flag & ND_NFSV4) {
670 * For NFSv4, the postop attr are at the end, so no point
671 * in looking if nd_repstat != 0.
673 if (!nd->nd_repstat) {
674 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
676 /* should never happen since nd_repstat != 0 */
677 nd->nd_flag |= ND_NOMOREDATA;
681 } else if (!nd->nd_repstat) {
682 /* For NFSv2, the attributes are here iff nd_repstat == 0 */
686 error = nfsm_loadattr(nd, nap);
695 * Fill in the setable attributes. The full argument indicates whether
696 * to fill in them all or just mode and time.
699 nfscl_fillsattr(struct nfsrv_descript *nd, struct vattr *vap,
700 struct vnode *vp, int flags, u_int32_t rdev)
703 struct nfsv2_sattr *sp;
704 nfsattrbit_t attrbits;
705 struct timeval curtime;
707 switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
709 NFSM_BUILD(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
710 if (vap->va_mode == (mode_t)VNOVAL)
711 sp->sa_mode = newnfs_xdrneg1;
713 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
714 if (vap->va_uid == (uid_t)VNOVAL)
715 sp->sa_uid = newnfs_xdrneg1;
717 sp->sa_uid = txdr_unsigned(vap->va_uid);
718 if (vap->va_gid == (gid_t)VNOVAL)
719 sp->sa_gid = newnfs_xdrneg1;
721 sp->sa_gid = txdr_unsigned(vap->va_gid);
722 if (flags & NFSSATTR_SIZE0)
724 else if (flags & NFSSATTR_SIZENEG1)
725 sp->sa_size = newnfs_xdrneg1;
726 else if (flags & NFSSATTR_SIZERDEV)
727 sp->sa_size = txdr_unsigned(rdev);
729 sp->sa_size = txdr_unsigned(vap->va_size);
730 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
731 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
734 getmicrotime(&curtime);
735 if (vap->va_mode != (mode_t)VNOVAL) {
736 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
738 *tl = txdr_unsigned(vap->va_mode);
740 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
743 if ((flags & NFSSATTR_FULL) && vap->va_uid != (uid_t)VNOVAL) {
744 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
746 *tl = txdr_unsigned(vap->va_uid);
748 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
751 if ((flags & NFSSATTR_FULL) && vap->va_gid != (gid_t)VNOVAL) {
752 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
754 *tl = txdr_unsigned(vap->va_gid);
756 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
759 if ((flags & NFSSATTR_FULL) && vap->va_size != VNOVAL) {
760 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
762 txdr_hyper(vap->va_size, tl);
764 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
767 if (vap->va_atime.tv_sec != VNOVAL) {
768 if (vap->va_atime.tv_sec != curtime.tv_sec) {
769 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
770 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
771 txdr_nfsv3time(&vap->va_atime, tl);
773 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
774 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
777 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
778 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
780 if (vap->va_mtime.tv_sec != VNOVAL) {
781 if (vap->va_mtime.tv_sec != curtime.tv_sec) {
782 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
783 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
784 txdr_nfsv3time(&vap->va_mtime, tl);
786 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
787 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
790 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
791 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
795 NFSZERO_ATTRBIT(&attrbits);
796 if (vap->va_mode != (mode_t)VNOVAL)
797 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_MODE);
798 if ((flags & NFSSATTR_FULL) && vap->va_uid != (uid_t)VNOVAL)
799 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_OWNER);
800 if ((flags & NFSSATTR_FULL) && vap->va_gid != (gid_t)VNOVAL)
801 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_OWNERGROUP);
802 if ((flags & NFSSATTR_FULL) && vap->va_size != VNOVAL)
803 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
804 if (vap->va_atime.tv_sec != VNOVAL)
805 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESSSET);
806 if (vap->va_mtime.tv_sec != VNOVAL)
807 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFYSET);
808 (void) nfsv4_fillattr(nd, vp->v_mount, vp, NULL, vap, NULL, 0,
809 &attrbits, NULL, NULL, 0, 0, 0, 0, (uint64_t)0);
815 * nfscl_request() - mostly a wrapper for newnfs_request().
818 nfscl_request(struct nfsrv_descript *nd, struct vnode *vp, NFSPROC_T *p,
819 struct ucred *cred, void *stuff)
822 struct nfsmount *nmp;
824 nmp = VFSTONFS(vp->v_mount);
825 if (nd->nd_flag & ND_NFSV4)
827 else if (nd->nd_flag & ND_NFSV3)
831 ret = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
832 NFS_PROG, vers, NULL, 1, NULL);
837 * fill in this bsden's variant of statfs using nfsstatfs.
840 nfscl_loadsbinfo(struct nfsmount *nmp, struct nfsstatfs *sfp, void *statfs)
842 struct statfs *sbp = (struct statfs *)statfs;
844 if (nmp->nm_flag & (NFSMNT_NFSV3 | NFSMNT_NFSV4)) {
845 sbp->f_bsize = NFS_FABLKSIZE;
846 sbp->f_blocks = sfp->sf_tbytes / NFS_FABLKSIZE;
847 sbp->f_bfree = sfp->sf_fbytes / NFS_FABLKSIZE;
849 * Although sf_abytes is uint64_t and f_bavail is int64_t,
850 * the value after dividing by NFS_FABLKSIZE is small
851 * enough that it will fit in 63bits, so it is ok to
852 * assign it to f_bavail without fear that it will become
855 sbp->f_bavail = sfp->sf_abytes / NFS_FABLKSIZE;
856 sbp->f_files = sfp->sf_tfiles;
857 /* Since f_ffree is int64_t, clip it to 63bits. */
858 if (sfp->sf_ffiles > INT64_MAX)
859 sbp->f_ffree = INT64_MAX;
861 sbp->f_ffree = sfp->sf_ffiles;
862 } else if ((nmp->nm_flag & NFSMNT_NFSV4) == 0) {
864 * The type casts to (int32_t) ensure that this code is
865 * compatible with the old NFS client, in that it will
866 * propagate bit31 to the high order bits. This may or may
867 * not be correct for NFSv2, but since it is a legacy
868 * environment, I'd rather retain backwards compatibility.
870 sbp->f_bsize = (int32_t)sfp->sf_bsize;
871 sbp->f_blocks = (int32_t)sfp->sf_blocks;
872 sbp->f_bfree = (int32_t)sfp->sf_bfree;
873 sbp->f_bavail = (int32_t)sfp->sf_bavail;
880 * Use the fsinfo stuff to update the mount point.
883 nfscl_loadfsinfo(struct nfsmount *nmp, struct nfsfsinfo *fsp)
886 if ((nmp->nm_wsize == 0 || fsp->fs_wtpref < nmp->nm_wsize) &&
887 fsp->fs_wtpref >= NFS_FABLKSIZE)
888 nmp->nm_wsize = (fsp->fs_wtpref + NFS_FABLKSIZE - 1) &
889 ~(NFS_FABLKSIZE - 1);
890 if (fsp->fs_wtmax < nmp->nm_wsize && fsp->fs_wtmax > 0) {
891 nmp->nm_wsize = fsp->fs_wtmax & ~(NFS_FABLKSIZE - 1);
892 if (nmp->nm_wsize == 0)
893 nmp->nm_wsize = fsp->fs_wtmax;
895 if (nmp->nm_wsize < NFS_FABLKSIZE)
896 nmp->nm_wsize = NFS_FABLKSIZE;
897 if ((nmp->nm_rsize == 0 || fsp->fs_rtpref < nmp->nm_rsize) &&
898 fsp->fs_rtpref >= NFS_FABLKSIZE)
899 nmp->nm_rsize = (fsp->fs_rtpref + NFS_FABLKSIZE - 1) &
900 ~(NFS_FABLKSIZE - 1);
901 if (fsp->fs_rtmax < nmp->nm_rsize && fsp->fs_rtmax > 0) {
902 nmp->nm_rsize = fsp->fs_rtmax & ~(NFS_FABLKSIZE - 1);
903 if (nmp->nm_rsize == 0)
904 nmp->nm_rsize = fsp->fs_rtmax;
906 if (nmp->nm_rsize < NFS_FABLKSIZE)
907 nmp->nm_rsize = NFS_FABLKSIZE;
908 if ((nmp->nm_readdirsize == 0 || fsp->fs_dtpref < nmp->nm_readdirsize)
909 && fsp->fs_dtpref >= NFS_DIRBLKSIZ)
910 nmp->nm_readdirsize = (fsp->fs_dtpref + NFS_DIRBLKSIZ - 1) &
911 ~(NFS_DIRBLKSIZ - 1);
912 if (fsp->fs_rtmax < nmp->nm_readdirsize && fsp->fs_rtmax > 0) {
913 nmp->nm_readdirsize = fsp->fs_rtmax & ~(NFS_DIRBLKSIZ - 1);
914 if (nmp->nm_readdirsize == 0)
915 nmp->nm_readdirsize = fsp->fs_rtmax;
917 if (nmp->nm_readdirsize < NFS_DIRBLKSIZ)
918 nmp->nm_readdirsize = NFS_DIRBLKSIZ;
919 if (fsp->fs_maxfilesize > 0 &&
920 fsp->fs_maxfilesize < nmp->nm_maxfilesize)
921 nmp->nm_maxfilesize = fsp->fs_maxfilesize;
922 nmp->nm_mountp->mnt_stat.f_iosize = newnfs_iosize(nmp);
923 nmp->nm_state |= NFSSTA_GOTFSINFO;
927 * Get a pointer to my IP addrress and return it.
928 * Return NULL if you can't find one.
931 nfscl_getmyip(struct nfsmount *nmp, int *isinet6p)
933 struct sockaddr_in sad, *sin;
935 u_int8_t *retp = NULL;
936 static struct in_addr laddr;
940 * Loop up a route for the destination address.
942 if (nmp->nm_nam->sa_family == AF_INET) {
943 bzero(&sad, sizeof (sad));
944 sin = (struct sockaddr_in *)nmp->nm_nam;
945 sad.sin_family = AF_INET;
946 sad.sin_len = sizeof (struct sockaddr_in);
947 sad.sin_addr.s_addr = sin->sin_addr.s_addr;
948 CURVNET_SET(CRED_TO_VNET(nmp->nm_sockreq.nr_cred));
949 rt = rtalloc1_fib((struct sockaddr *)&sad, 0, 0UL,
950 curthread->td_proc->p_fibnum);
952 if (rt->rt_ifp != NULL &&
953 rt->rt_ifa != NULL &&
954 ((rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) &&
955 rt->rt_ifa->ifa_addr->sa_family == AF_INET) {
956 sin = (struct sockaddr_in *)
957 rt->rt_ifa->ifa_addr;
958 laddr.s_addr = sin->sin_addr.s_addr;
959 retp = (u_int8_t *)&laddr;
965 } else if (nmp->nm_nam->sa_family == AF_INET6) {
966 struct sockaddr_in6 sad6, *sin6;
967 static struct in6_addr laddr6;
969 bzero(&sad6, sizeof (sad6));
970 sin6 = (struct sockaddr_in6 *)nmp->nm_nam;
971 sad6.sin6_family = AF_INET6;
972 sad6.sin6_len = sizeof (struct sockaddr_in6);
973 sad6.sin6_addr = sin6->sin6_addr;
974 CURVNET_SET(CRED_TO_VNET(nmp->nm_sockreq.nr_cred));
975 rt = rtalloc1_fib((struct sockaddr *)&sad6, 0, 0UL,
976 curthread->td_proc->p_fibnum);
978 if (rt->rt_ifp != NULL &&
979 rt->rt_ifa != NULL &&
980 ((rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) &&
981 rt->rt_ifa->ifa_addr->sa_family == AF_INET6) {
982 sin6 = (struct sockaddr_in6 *)
983 rt->rt_ifa->ifa_addr;
984 laddr6 = sin6->sin6_addr;
985 retp = (u_int8_t *)&laddr6;
997 * Copy NFS uid, gids from the cred structure.
1000 newnfs_copyincred(struct ucred *cr, struct nfscred *nfscr)
1004 KASSERT(cr->cr_ngroups >= 0,
1005 ("newnfs_copyincred: negative cr_ngroups"));
1006 nfscr->nfsc_uid = cr->cr_uid;
1007 nfscr->nfsc_ngroups = MIN(cr->cr_ngroups, NFS_MAXGRPS + 1);
1008 for (i = 0; i < nfscr->nfsc_ngroups; i++)
1009 nfscr->nfsc_groups[i] = cr->cr_groups[i];
1014 * Do any client specific initialization.
1019 static int inited = 0;
1025 ncl_pbuf_freecnt = nswbuf / 2 + 1;
1029 * Check each of the attributes to be set, to ensure they aren't already
1030 * the correct value. Disable setting ones already correct.
1033 nfscl_checksattr(struct vattr *vap, struct nfsvattr *nvap)
1036 if (vap->va_mode != (mode_t)VNOVAL) {
1037 if (vap->va_mode == nvap->na_mode)
1038 vap->va_mode = (mode_t)VNOVAL;
1040 if (vap->va_uid != (uid_t)VNOVAL) {
1041 if (vap->va_uid == nvap->na_uid)
1042 vap->va_uid = (uid_t)VNOVAL;
1044 if (vap->va_gid != (gid_t)VNOVAL) {
1045 if (vap->va_gid == nvap->na_gid)
1046 vap->va_gid = (gid_t)VNOVAL;
1048 if (vap->va_size != VNOVAL) {
1049 if (vap->va_size == nvap->na_size)
1050 vap->va_size = VNOVAL;
1054 * We are normally called with only a partially initialized
1055 * VAP. Since the NFSv3 spec says that server may use the
1056 * file attributes to store the verifier, the spec requires
1057 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1058 * in atime, but we can't really assume that all servers will
1059 * so we ensure that our SETATTR sets both atime and mtime.
1061 if (vap->va_mtime.tv_sec == VNOVAL)
1062 vfs_timestamp(&vap->va_mtime);
1063 if (vap->va_atime.tv_sec == VNOVAL)
1064 vap->va_atime = vap->va_mtime;
1069 * Map nfsv4 errors to errno.h errors.
1070 * The uid and gid arguments are only used for NFSERR_BADOWNER and that
1071 * error should only be returned for the Open, Create and Setattr Ops.
1072 * As such, most calls can just pass in 0 for those arguments.
1075 nfscl_maperr(struct thread *td, int error, uid_t uid, gid_t gid)
1086 case NFSERR_BADOWNER:
1087 tprintf(p, LOG_INFO,
1088 "No name and/or group mapping for uid,gid:(%d,%d)\n",
1091 case NFSERR_STALECLIENTID:
1092 case NFSERR_STALESTATEID:
1093 case NFSERR_EXPIRED:
1094 case NFSERR_BADSTATEID:
1095 printf("nfsv4 recover err returned %d\n", error);
1097 case NFSERR_BADHANDLE:
1098 case NFSERR_SERVERFAULT:
1099 case NFSERR_BADTYPE:
1100 case NFSERR_FHEXPIRED:
1101 case NFSERR_RESOURCE:
1103 case NFSERR_NOFILEHANDLE:
1104 case NFSERR_MINORVERMISMATCH:
1105 case NFSERR_OLDSTATEID:
1106 case NFSERR_BADSEQID:
1107 case NFSERR_LEASEMOVED:
1108 case NFSERR_RECLAIMBAD:
1110 case NFSERR_BADCHAR:
1111 case NFSERR_BADNAME:
1112 case NFSERR_OPILLEGAL:
1113 printf("nfsv4 client/server protocol prob err=%d\n",
1117 tprintf(p, LOG_INFO, "nfsv4 err=%d\n", error);
1123 * Locate a process by number; return only "live" processes -- i.e., neither
1124 * zombies nor newly born but incompletely initialized processes. By not
1125 * returning processes in the PRS_NEW state, we allow callers to avoid
1126 * testing for that condition to avoid dereferencing p_ucred, et al.
1127 * Identical to pfind() in kern_proc.c, except it assume the list is
1130 static struct proc *
1131 pfind_locked(pid_t pid)
1135 LIST_FOREACH(p, PIDHASH(pid), p_hash)
1136 if (p->p_pid == pid) {
1138 if (p->p_state == PRS_NEW) {
1148 * Check to see if the process for this owner exists. Return 1 if it doesn't
1152 nfscl_procdoesntexist(u_int8_t *own)
1162 tl.cval[0] = *own++;
1163 tl.cval[1] = *own++;
1164 tl.cval[2] = *own++;
1165 tl.cval[3] = *own++;
1167 p = pfind_locked(pid);
1170 if (p->p_stats == NULL) {
1174 tl.cval[0] = *own++;
1175 tl.cval[1] = *own++;
1176 tl.cval[2] = *own++;
1177 tl.cval[3] = *own++;
1178 if (tl.lval != p->p_stats->p_start.tv_sec) {
1181 tl.cval[0] = *own++;
1182 tl.cval[1] = *own++;
1183 tl.cval[2] = *own++;
1185 if (tl.lval != p->p_stats->p_start.tv_usec)
1193 * - nfs pseudo system call for the client
1199 nfssvc_nfscl(struct thread *td, struct nfssvc_args *uap)
1202 struct nfscbd_args nfscbdarg;
1203 struct nfsd_nfscbd_args nfscbdarg2;
1206 if (uap->flag & NFSSVC_CBADDSOCK) {
1207 error = copyin(uap->argp, (caddr_t)&nfscbdarg, sizeof(nfscbdarg));
1210 if ((error = fget(td, nfscbdarg.sock, &fp)) != 0) {
1213 if (fp->f_type != DTYPE_SOCKET) {
1217 error = nfscbd_addsock(fp);
1219 if (!error && nfscl_enablecallb == 0) {
1220 nfsv4_cbport = nfscbdarg.port;
1221 nfscl_enablecallb = 1;
1223 } else if (uap->flag & NFSSVC_NFSCBD) {
1224 if (uap->argp == NULL)
1226 error = copyin(uap->argp, (caddr_t)&nfscbdarg2,
1227 sizeof(nfscbdarg2));
1230 error = nfscbd_nfsd(td, &nfscbdarg2);
1237 extern int (*nfsd_call_nfscl)(struct thread *, struct nfssvc_args *);
1240 * Called once to initialize data structures...
1243 nfscl_modevent(module_t mod, int type, void *data)
1246 static int loaded = 0;
1253 mtx_init(&nfs_clstate_mutex, "nfs_clstate_mutex", NULL,
1255 mtx_init(&ncl_iod_mutex, "ncl_iod_mutex", NULL, MTX_DEF);
1260 ncl_call_invalcaches = ncl_invalcaches;
1261 nfsd_call_nfscl = nfssvc_nfscl;
1266 if (nfs_numnfscbd != 0) {
1272 * XXX: Unloading of nfscl module is unsupported.
1275 ncl_call_invalcaches = NULL;
1276 nfsd_call_nfscl = NULL;
1277 /* and get rid of the mutexes */
1278 mtx_destroy(&nfs_clstate_mutex);
1279 mtx_destroy(&ncl_iod_mutex);
1291 static moduledata_t nfscl_mod = {
1296 DECLARE_MODULE(nfscl, nfscl_mod, SI_SUB_VFS, SI_ORDER_FIRST);
1298 /* So that loader and kldload(2) can find us, wherever we are.. */
1299 MODULE_VERSION(nfscl, 1);
1300 MODULE_DEPEND(nfscl, nfscommon, 1, 1, 1);
1301 MODULE_DEPEND(nfscl, krpc, 1, 1, 1);
1302 MODULE_DEPEND(nfscl, nfssvc, 1, 1, 1);
1303 MODULE_DEPEND(nfscl, nfslock, 1, 1, 1);