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$");
38 * generally, I don't like #includes inside .h files, but it seems to
39 * be the easiest way to handle the port.
42 #include <fs/nfs/nfsport.h>
43 #include <netinet/if_ether.h>
44 #include <net/if_types.h>
46 extern u_int32_t newnfs_true, newnfs_false, newnfs_xdrneg1;
47 extern struct vop_vector newnfs_vnodeops;
48 extern struct vop_vector newnfs_fifoops;
49 extern uma_zone_t newnfsnode_zone;
50 extern struct buf_ops buf_ops_newnfs;
51 extern int ncl_pbuf_freecnt;
52 extern short nfsv4_cbport;
53 extern int nfscl_enablecallb;
54 extern int nfs_numnfscbd;
55 extern int nfscl_inited;
56 struct mtx nfs_clstate_mutex;
57 struct mtx ncl_iod_mutex;
60 extern void (*ncl_call_invalcaches)(struct vnode *);
63 * Comparison function for vfs_hash functions.
66 newnfs_vncmpf(struct vnode *vp, void *arg)
68 struct nfsfh *nfhp = (struct nfsfh *)arg;
69 struct nfsnode *np = VTONFS(vp);
71 if (np->n_fhp->nfh_len != nfhp->nfh_len ||
72 NFSBCMP(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len))
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.
86 nfscl_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)
90 struct nfsnode *np, *dnp;
91 struct vnode *vp, *nvp;
92 struct nfsv4node *newd, *oldd;
101 hash = fnv_32_buf(nfhp->nfh_fh, nfhp->nfh_len, FNV1_32_INIT);
103 error = vfs_hash_get(mntp, hash, lkflags,
104 td, &nvp, newnfs_vncmpf, nfhp);
105 if (error == 0 && nvp != NULL) {
107 * I believe there is a slight chance that vgonel() could
108 * get called on this vnode between when NFSVOPLOCK() 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.
117 if ((nvp->v_iflag & VI_DOOMED)) {
126 FREE((caddr_t)nfhp, M_NFSFH);
132 * For NFSv4, check to see if it is the same name and
133 * replace the name, if it is different.
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),
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);
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),
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))) {
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),
168 FREE((caddr_t)newd, M_NFSV4NODE);
170 FREE((caddr_t)oldd, M_NFSV4NODE);
172 FREE((caddr_t)nfhp, M_NFSFH);
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.
181 np = uma_zalloc(newnfsnode_zone, M_WAITOK | M_ZERO);
183 error = getnewvnode("newnfs", mntp, &newnfs_vnodeops, &nvp);
185 uma_zfree(newnfsnode_zone, np);
186 FREE((caddr_t)nfhp, M_NFSFH);
190 vp->v_bufobj.bo_ops = &buf_ops_newnfs;
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).
199 mtx_init(&np->n_mtx, "NEWNFSnode lock", NULL, MTX_DEF | MTX_DUPOK);
202 * Are we getting the root? If so, make sure the vnode flags
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)
209 vp->v_vflag |= VV_ROOT;
214 * For NFSv4, we have to attach the directory file handle and
215 * file name, so that Open Ops can be done later.
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,
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),
232 * NFS supports recursive and shared locking.
236 lockmgr(vp->v_vnlock, LK_EXCLUSIVE | LK_NOWITNESS, NULL);
237 error = insmntque(vp, mntp);
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);
247 error = vfs_hash_insert(vp, hash, lkflags,
248 td, &nvp, newnfs_vncmpf, nfhp);
253 /* vfs_hash_insert() vput()'s the losing vnode */
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
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.
273 nfscl_ngetreopen(struct mount *mntp, u_int8_t *fhp, int fhsize,
274 struct thread *td, struct nfsnode **npp)
282 /* For forced dismounts, just return error. */
283 if ((mntp->mnt_kern_flag & MNTK_UNMOUNTF))
285 MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) + fhsize,
287 bcopy(fhp, &nfhp->nfh_fh[0], fhsize);
288 nfhp->nfh_len = fhsize;
290 hash = fnv_32_buf(fhp, fhsize, FNV1_32_INIT);
293 * First, try to get the vnode locked, but don't block for the lock.
295 error = vfs_hash_get(mntp, hash, (LK_EXCLUSIVE | LK_NOWAIT), td, &nvp,
296 newnfs_vncmpf, nfhp);
297 if (error == 0 && nvp != NULL) {
299 } else if (error == EBUSY) {
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.
310 if ((mntp->mnt_kern_flag & MNTK_UNMOUNTF))
313 error = vfs_hash_get(mntp, hash,
314 (LK_EXCLOTHER | LK_NOWAIT), td, &nvp,
315 newnfs_vncmpf, nfhp);
328 * Load the attribute cache (that lives in the nfsnode entry) with
329 * the attributes of the second argument and
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.
336 nfscl_loadattrcache(struct vnode **vpp, struct nfsvattr *nap, void *nvaper,
337 void *stuff, int writeattr, int dontshrink)
339 struct vnode *vp = *vpp;
340 struct vattr *vap, *nvap = &nap->na_vattr, *vaper = nvaper;
342 struct nfsmount *nmp;
343 struct timespec mtime_save;
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
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;
360 nmp = VFSTONFS(vp->v_mount);
361 vap = &np->n_vattr.na_vattr;
362 mtime_save = vap->va_mtime;
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 np->n_vattr.na_mode = nap->na_mode;
371 NFSBCOPY((caddr_t)nap, (caddr_t)&np->n_vattr,
372 sizeof (struct nfsvattr));
376 * For NFSv4, if the node's fsid is not equal to the mount point's
377 * fsid, return the low order 32bits of the node's fsid. This
378 * allows getcwd(3) to work. There is a chance that the fsid might
379 * be the same as a local fs, but since this is in an NFS mount
380 * point, I don't think that will cause any problems?
382 if (NFSHASNFSV4(nmp) && NFSHASHASSETFSID(nmp) &&
383 (nmp->nm_fsid[0] != np->n_vattr.na_filesid[0] ||
384 nmp->nm_fsid[1] != np->n_vattr.na_filesid[1])) {
386 * va_fsid needs to be set to some value derived from
387 * np->n_vattr.na_filesid that is not equal
388 * vp->v_mount->mnt_stat.f_fsid[0], so that it changes
389 * from the value used for the top level server volume
390 * in the mounted subtree.
392 if (vp->v_mount->mnt_stat.f_fsid.val[0] !=
393 (uint32_t)np->n_vattr.na_filesid[0])
394 vap->va_fsid = (uint32_t)np->n_vattr.na_filesid[0];
396 vap->va_fsid = (uint32_t)hash32_buf(
397 np->n_vattr.na_filesid, 2 * sizeof(uint64_t), 0);
399 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
400 np->n_attrstamp = time_second;
401 if (vap->va_size != np->n_size) {
402 if (vap->va_type == VREG) {
403 if (dontshrink && vap->va_size < np->n_size) {
405 * We've been told not to shrink the file;
406 * zero np->n_attrstamp to indicate that
407 * the attributes are stale.
409 vap->va_size = np->n_size;
411 } else if (np->n_flag & NMODIFIED) {
413 * We've modified the file: Use the larger
414 * of our size, and the server's size.
416 if (vap->va_size < np->n_size) {
417 vap->va_size = np->n_size;
419 np->n_size = vap->va_size;
420 np->n_flag |= NSIZECHANGED;
423 np->n_size = vap->va_size;
424 np->n_flag |= NSIZECHANGED;
426 vnode_pager_setsize(vp, np->n_size);
428 np->n_size = vap->va_size;
432 * The following checks are added to prevent a race between (say)
433 * a READDIR+ and a WRITE.
434 * READDIR+, WRITE requests sent out.
435 * READDIR+ resp, WRITE resp received on client.
436 * However, the WRITE resp was handled before the READDIR+ resp
437 * causing the post op attrs from the write to be loaded first
438 * and the attrs from the READDIR+ to be loaded later. If this
439 * happens, we have stale attrs loaded into the attrcache.
440 * We detect this by for the mtime moving back. We invalidate the
441 * attrcache when this happens.
443 if (timespeccmp(&mtime_save, &vap->va_mtime, >))
444 /* Size changed or mtime went backwards */
447 NFSBCOPY((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
448 if (np->n_flag & NCHG) {
449 if (np->n_flag & NACC)
450 vaper->va_atime = np->n_atim;
451 if (np->n_flag & NUPD)
452 vaper->va_mtime = np->n_mtim;
460 * Fill in the client id name. For these bytes:
461 * 1 - they must be unique
462 * 2 - they should be persistent across client reboots
463 * 1 is more critical than 2
464 * Use the mount point's unique id plus either the uuid or, if that
465 * isn't set, random junk.
468 nfscl_fillclid(u_int64_t clval, char *uuid, u_int8_t *cp, u_int16_t idlen)
473 * First, put in the 64bit mount point identifier.
475 if (idlen >= sizeof (u_int64_t)) {
476 NFSBCOPY((caddr_t)&clval, cp, sizeof (u_int64_t));
477 cp += sizeof (u_int64_t);
478 idlen -= sizeof (u_int64_t);
482 * If uuid is non-zero length, use it.
484 uuidlen = strlen(uuid);
485 if (uuidlen > 0 && idlen >= uuidlen) {
486 NFSBCOPY(uuid, cp, uuidlen);
492 * This only normally happens if the uuid isn't set.
495 *cp++ = (u_int8_t)(arc4random() % 256);
501 * Fill in a lock owner name. For now, pid + the process's creation time.
504 nfscl_filllockowner(void *id, u_int8_t *cp, int flags)
514 bzero(cp, NFSV4CL_LOCKNAMELEN);
517 if ((flags & F_POSIX) != 0) {
518 p = (struct proc *)id;
524 tl.lval = p->p_stats->p_start.tv_sec;
529 tl.lval = p->p_stats->p_start.tv_usec;
534 } else if ((flags & F_FLOCK) != 0) {
535 bcopy(&id, cp, sizeof(id));
536 bzero(&cp[sizeof(id)], NFSV4CL_LOCKNAMELEN - sizeof(id));
538 printf("nfscl_filllockowner: not F_POSIX or F_FLOCK\n");
539 bzero(cp, NFSV4CL_LOCKNAMELEN);
544 * Find the parent process for the thread passed in as an argument.
545 * If none exists, return NULL, otherwise return a thread for the parent.
546 * (Can be any of the threads, since it is only used for td->td_proc.)
549 nfscl_getparent(struct thread *td)
562 ptd = TAILQ_FIRST(&p->p_threads);
567 * Start up the renew kernel thread.
570 start_nfscl(void *arg)
572 struct nfsclclient *clp;
575 clp = (struct nfsclclient *)arg;
576 td = TAILQ_FIRST(&clp->nfsc_renewthread->p_threads);
577 nfscl_renewthread(clp, td);
582 nfscl_start_renewthread(struct nfsclclient *clp)
585 kproc_create(start_nfscl, (void *)clp, &clp->nfsc_renewthread, 0, 0,
591 * For NFSv4, it assumes that nfsv4_wccattr() was used to set up the getattr
592 * as the first Op after PutFH.
593 * (For NFSv4, the postop attributes are after the Op, so they can't be
594 * parsed here. A separate call to nfscl_postop_attr() is required.)
597 nfscl_wcc_data(struct nfsrv_descript *nd, struct vnode *vp,
598 struct nfsvattr *nap, int *flagp, int *wccflagp, void *stuff)
601 struct nfsnode *np = VTONFS(vp);
602 struct nfsvattr nfsva;
605 if (wccflagp != NULL)
607 if (nd->nd_flag & ND_NFSV3) {
609 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
610 if (*tl == newnfs_true) {
611 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
612 if (wccflagp != NULL) {
613 mtx_lock(&np->n_mtx);
614 *wccflagp = (np->n_mtime.tv_sec ==
615 fxdr_unsigned(u_int32_t, *(tl + 2)) &&
616 np->n_mtime.tv_nsec ==
617 fxdr_unsigned(u_int32_t, *(tl + 3)));
618 mtx_unlock(&np->n_mtx);
621 error = nfscl_postop_attr(nd, nap, flagp, stuff);
622 } else if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR))
623 == (ND_NFSV4 | ND_V4WCCATTR)) {
624 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
625 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
626 NULL, NULL, NULL, NULL, NULL);
630 * Get rid of Op# and status for next op.
632 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
634 nd->nd_flag |= ND_NOMOREDATA;
635 if (wccflagp != NULL &&
636 nfsva.na_vattr.va_mtime.tv_sec != 0) {
637 mtx_lock(&np->n_mtx);
638 *wccflagp = (np->n_mtime.tv_sec ==
639 nfsva.na_vattr.va_mtime.tv_sec &&
640 np->n_mtime.tv_nsec ==
641 nfsva.na_vattr.va_mtime.tv_sec);
642 mtx_unlock(&np->n_mtx);
650 * Get postop attributes.
653 nfscl_postop_attr(struct nfsrv_descript *nd, struct nfsvattr *nap, int *retp,
660 if (nd->nd_flag & ND_NOMOREDATA)
662 if (nd->nd_flag & ND_NFSV3) {
663 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
664 *retp = fxdr_unsigned(int, *tl);
665 } else if (nd->nd_flag & ND_NFSV4) {
667 * For NFSv4, the postop attr are at the end, so no point
668 * in looking if nd_repstat != 0.
670 if (!nd->nd_repstat) {
671 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
673 /* should never happen since nd_repstat != 0 */
674 nd->nd_flag |= ND_NOMOREDATA;
678 } else if (!nd->nd_repstat) {
679 /* For NFSv2, the attributes are here iff nd_repstat == 0 */
683 error = nfsm_loadattr(nd, nap);
692 * Fill in the setable attributes. The full argument indicates whether
693 * to fill in them all or just mode and time.
696 nfscl_fillsattr(struct nfsrv_descript *nd, struct vattr *vap,
697 struct vnode *vp, int flags, u_int32_t rdev)
700 struct nfsv2_sattr *sp;
701 nfsattrbit_t attrbits;
702 struct timeval curtime;
704 switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
706 NFSM_BUILD(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
707 if (vap->va_mode == (mode_t)VNOVAL)
708 sp->sa_mode = newnfs_xdrneg1;
710 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
711 if (vap->va_uid == (uid_t)VNOVAL)
712 sp->sa_uid = newnfs_xdrneg1;
714 sp->sa_uid = txdr_unsigned(vap->va_uid);
715 if (vap->va_gid == (gid_t)VNOVAL)
716 sp->sa_gid = newnfs_xdrneg1;
718 sp->sa_gid = txdr_unsigned(vap->va_gid);
719 if (flags & NFSSATTR_SIZE0)
721 else if (flags & NFSSATTR_SIZENEG1)
722 sp->sa_size = newnfs_xdrneg1;
723 else if (flags & NFSSATTR_SIZERDEV)
724 sp->sa_size = txdr_unsigned(rdev);
726 sp->sa_size = txdr_unsigned(vap->va_size);
727 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
728 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
731 getmicrotime(&curtime);
732 if (vap->va_mode != (mode_t)VNOVAL) {
733 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
735 *tl = txdr_unsigned(vap->va_mode);
737 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
740 if ((flags & NFSSATTR_FULL) && vap->va_uid != (uid_t)VNOVAL) {
741 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
743 *tl = txdr_unsigned(vap->va_uid);
745 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
748 if ((flags & NFSSATTR_FULL) && vap->va_gid != (gid_t)VNOVAL) {
749 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
751 *tl = txdr_unsigned(vap->va_gid);
753 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
756 if ((flags & NFSSATTR_FULL) && vap->va_size != VNOVAL) {
757 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
759 txdr_hyper(vap->va_size, tl);
761 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
764 if (vap->va_atime.tv_sec != VNOVAL) {
765 if (vap->va_atime.tv_sec != curtime.tv_sec) {
766 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
767 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
768 txdr_nfsv3time(&vap->va_atime, tl);
770 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
771 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
774 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
775 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
777 if (vap->va_mtime.tv_sec != VNOVAL) {
778 if (vap->va_mtime.tv_sec != curtime.tv_sec) {
779 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
780 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
781 txdr_nfsv3time(&vap->va_mtime, tl);
783 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
784 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
787 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
788 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
792 NFSZERO_ATTRBIT(&attrbits);
793 if (vap->va_mode != (mode_t)VNOVAL)
794 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_MODE);
795 if ((flags & NFSSATTR_FULL) && vap->va_uid != (uid_t)VNOVAL)
796 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_OWNER);
797 if ((flags & NFSSATTR_FULL) && vap->va_gid != (gid_t)VNOVAL)
798 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_OWNERGROUP);
799 if ((flags & NFSSATTR_FULL) && vap->va_size != VNOVAL)
800 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
801 if (vap->va_atime.tv_sec != VNOVAL)
802 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESSSET);
803 if (vap->va_mtime.tv_sec != VNOVAL)
804 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFYSET);
805 (void) nfsv4_fillattr(nd, vp->v_mount, vp, NULL, vap, NULL, 0,
806 &attrbits, NULL, NULL, 0, 0, 0, 0, (uint64_t)0);
812 * nfscl_request() - mostly a wrapper for newnfs_request().
815 nfscl_request(struct nfsrv_descript *nd, struct vnode *vp, NFSPROC_T *p,
816 struct ucred *cred, void *stuff)
819 struct nfsmount *nmp;
821 nmp = VFSTONFS(vp->v_mount);
822 if (nd->nd_flag & ND_NFSV4)
824 else if (nd->nd_flag & ND_NFSV3)
828 ret = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
829 NFS_PROG, vers, NULL, 1, NULL);
834 * fill in this bsden's variant of statfs using nfsstatfs.
837 nfscl_loadsbinfo(struct nfsmount *nmp, struct nfsstatfs *sfp, void *statfs)
839 struct statfs *sbp = (struct statfs *)statfs;
841 if (nmp->nm_flag & (NFSMNT_NFSV3 | NFSMNT_NFSV4)) {
842 sbp->f_bsize = NFS_FABLKSIZE;
843 sbp->f_blocks = sfp->sf_tbytes / NFS_FABLKSIZE;
844 sbp->f_bfree = sfp->sf_fbytes / NFS_FABLKSIZE;
846 * Although sf_abytes is uint64_t and f_bavail is int64_t,
847 * the value after dividing by NFS_FABLKSIZE is small
848 * enough that it will fit in 63bits, so it is ok to
849 * assign it to f_bavail without fear that it will become
852 sbp->f_bavail = sfp->sf_abytes / NFS_FABLKSIZE;
853 sbp->f_files = sfp->sf_tfiles;
854 /* Since f_ffree is int64_t, clip it to 63bits. */
855 if (sfp->sf_ffiles > INT64_MAX)
856 sbp->f_ffree = INT64_MAX;
858 sbp->f_ffree = sfp->sf_ffiles;
859 } else if ((nmp->nm_flag & NFSMNT_NFSV4) == 0) {
861 * The type casts to (int32_t) ensure that this code is
862 * compatible with the old NFS client, in that it will
863 * propagate bit31 to the high order bits. This may or may
864 * not be correct for NFSv2, but since it is a legacy
865 * environment, I'd rather retain backwards compatibility.
867 sbp->f_bsize = (int32_t)sfp->sf_bsize;
868 sbp->f_blocks = (int32_t)sfp->sf_blocks;
869 sbp->f_bfree = (int32_t)sfp->sf_bfree;
870 sbp->f_bavail = (int32_t)sfp->sf_bavail;
877 * Use the fsinfo stuff to update the mount point.
880 nfscl_loadfsinfo(struct nfsmount *nmp, struct nfsfsinfo *fsp)
883 if ((nmp->nm_wsize == 0 || fsp->fs_wtpref < nmp->nm_wsize) &&
884 fsp->fs_wtpref >= NFS_FABLKSIZE)
885 nmp->nm_wsize = (fsp->fs_wtpref + NFS_FABLKSIZE - 1) &
886 ~(NFS_FABLKSIZE - 1);
887 if (fsp->fs_wtmax < nmp->nm_wsize && fsp->fs_wtmax > 0) {
888 nmp->nm_wsize = fsp->fs_wtmax & ~(NFS_FABLKSIZE - 1);
889 if (nmp->nm_wsize == 0)
890 nmp->nm_wsize = fsp->fs_wtmax;
892 if (nmp->nm_wsize < NFS_FABLKSIZE)
893 nmp->nm_wsize = NFS_FABLKSIZE;
894 if ((nmp->nm_rsize == 0 || fsp->fs_rtpref < nmp->nm_rsize) &&
895 fsp->fs_rtpref >= NFS_FABLKSIZE)
896 nmp->nm_rsize = (fsp->fs_rtpref + NFS_FABLKSIZE - 1) &
897 ~(NFS_FABLKSIZE - 1);
898 if (fsp->fs_rtmax < nmp->nm_rsize && fsp->fs_rtmax > 0) {
899 nmp->nm_rsize = fsp->fs_rtmax & ~(NFS_FABLKSIZE - 1);
900 if (nmp->nm_rsize == 0)
901 nmp->nm_rsize = fsp->fs_rtmax;
903 if (nmp->nm_rsize < NFS_FABLKSIZE)
904 nmp->nm_rsize = NFS_FABLKSIZE;
905 if ((nmp->nm_readdirsize == 0 || fsp->fs_dtpref < nmp->nm_readdirsize)
906 && fsp->fs_dtpref >= NFS_DIRBLKSIZ)
907 nmp->nm_readdirsize = (fsp->fs_dtpref + NFS_DIRBLKSIZ - 1) &
908 ~(NFS_DIRBLKSIZ - 1);
909 if (fsp->fs_rtmax < nmp->nm_readdirsize && fsp->fs_rtmax > 0) {
910 nmp->nm_readdirsize = fsp->fs_rtmax & ~(NFS_DIRBLKSIZ - 1);
911 if (nmp->nm_readdirsize == 0)
912 nmp->nm_readdirsize = fsp->fs_rtmax;
914 if (nmp->nm_readdirsize < NFS_DIRBLKSIZ)
915 nmp->nm_readdirsize = NFS_DIRBLKSIZ;
916 if (fsp->fs_maxfilesize > 0 &&
917 fsp->fs_maxfilesize < nmp->nm_maxfilesize)
918 nmp->nm_maxfilesize = fsp->fs_maxfilesize;
919 nmp->nm_mountp->mnt_stat.f_iosize = newnfs_iosize(nmp);
920 nmp->nm_state |= NFSSTA_GOTFSINFO;
924 * Get a pointer to my IP addrress and return it.
925 * Return NULL if you can't find one.
928 nfscl_getmyip(struct nfsmount *nmp, int *isinet6p)
930 struct sockaddr_in sad, *sin;
932 u_int8_t *retp = NULL;
933 static struct in_addr laddr;
937 * Loop up a route for the destination address.
939 if (nmp->nm_nam->sa_family == AF_INET) {
940 bzero(&sad, sizeof (sad));
941 sin = (struct sockaddr_in *)nmp->nm_nam;
942 sad.sin_family = AF_INET;
943 sad.sin_len = sizeof (struct sockaddr_in);
944 sad.sin_addr.s_addr = sin->sin_addr.s_addr;
945 CURVNET_SET(CRED_TO_VNET(nmp->nm_sockreq.nr_cred));
946 rt = rtalloc1((struct sockaddr *)&sad, 0, 0UL);
948 if (rt->rt_ifp != NULL &&
949 rt->rt_ifa != NULL &&
950 ((rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) &&
951 rt->rt_ifa->ifa_addr->sa_family == AF_INET) {
952 sin = (struct sockaddr_in *)
953 rt->rt_ifa->ifa_addr;
954 laddr.s_addr = sin->sin_addr.s_addr;
955 retp = (u_int8_t *)&laddr;
961 } else if (nmp->nm_nam->sa_family == AF_INET6) {
962 struct sockaddr_in6 sad6, *sin6;
963 static struct in6_addr laddr6;
965 bzero(&sad6, sizeof (sad6));
966 sin6 = (struct sockaddr_in6 *)nmp->nm_nam;
967 sad6.sin6_family = AF_INET6;
968 sad6.sin6_len = sizeof (struct sockaddr_in6);
969 sad6.sin6_addr = sin6->sin6_addr;
970 CURVNET_SET(CRED_TO_VNET(nmp->nm_sockreq.nr_cred));
971 rt = rtalloc1((struct sockaddr *)&sad6, 0, 0UL);
973 if (rt->rt_ifp != NULL &&
974 rt->rt_ifa != NULL &&
975 ((rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) &&
976 rt->rt_ifa->ifa_addr->sa_family == AF_INET6) {
977 sin6 = (struct sockaddr_in6 *)
978 rt->rt_ifa->ifa_addr;
979 laddr6 = sin6->sin6_addr;
980 retp = (u_int8_t *)&laddr6;
992 * Copy NFS uid, gids from the cred structure.
995 newnfs_copyincred(struct ucred *cr, struct nfscred *nfscr)
999 KASSERT(cr->cr_ngroups >= 0,
1000 ("newnfs_copyincred: negative cr_ngroups"));
1001 nfscr->nfsc_uid = cr->cr_uid;
1002 nfscr->nfsc_ngroups = MIN(cr->cr_ngroups, NFS_MAXGRPS + 1);
1003 for (i = 0; i < nfscr->nfsc_ngroups; i++)
1004 nfscr->nfsc_groups[i] = cr->cr_groups[i];
1009 * Do any client specific initialization.
1014 static int inited = 0;
1020 ncl_pbuf_freecnt = nswbuf / 2 + 1;
1024 * Check each of the attributes to be set, to ensure they aren't already
1025 * the correct value. Disable setting ones already correct.
1028 nfscl_checksattr(struct vattr *vap, struct nfsvattr *nvap)
1031 if (vap->va_mode != (mode_t)VNOVAL) {
1032 if (vap->va_mode == nvap->na_mode)
1033 vap->va_mode = (mode_t)VNOVAL;
1035 if (vap->va_uid != (uid_t)VNOVAL) {
1036 if (vap->va_uid == nvap->na_uid)
1037 vap->va_uid = (uid_t)VNOVAL;
1039 if (vap->va_gid != (gid_t)VNOVAL) {
1040 if (vap->va_gid == nvap->na_gid)
1041 vap->va_gid = (gid_t)VNOVAL;
1043 if (vap->va_size != VNOVAL) {
1044 if (vap->va_size == nvap->na_size)
1045 vap->va_size = VNOVAL;
1049 * We are normally called with only a partially initialized
1050 * VAP. Since the NFSv3 spec says that server may use the
1051 * file attributes to store the verifier, the spec requires
1052 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1053 * in atime, but we can't really assume that all servers will
1054 * so we ensure that our SETATTR sets both atime and mtime.
1056 if (vap->va_mtime.tv_sec == VNOVAL)
1057 vfs_timestamp(&vap->va_mtime);
1058 if (vap->va_atime.tv_sec == VNOVAL)
1059 vap->va_atime = vap->va_mtime;
1064 * Map nfsv4 errors to errno.h errors.
1065 * The uid and gid arguments are only used for NFSERR_BADOWNER and that
1066 * error should only be returned for the Open, Create and Setattr Ops.
1067 * As such, most calls can just pass in 0 for those arguments.
1070 nfscl_maperr(struct thread *td, int error, uid_t uid, gid_t gid)
1081 case NFSERR_BADOWNER:
1082 tprintf(p, LOG_INFO,
1083 "No name and/or group mapping for uid,gid:(%d,%d)\n",
1086 case NFSERR_STALECLIENTID:
1087 case NFSERR_STALESTATEID:
1088 case NFSERR_EXPIRED:
1089 case NFSERR_BADSTATEID:
1090 printf("nfsv4 recover err returned %d\n", error);
1092 case NFSERR_BADHANDLE:
1093 case NFSERR_SERVERFAULT:
1094 case NFSERR_BADTYPE:
1095 case NFSERR_FHEXPIRED:
1096 case NFSERR_RESOURCE:
1098 case NFSERR_NOFILEHANDLE:
1099 case NFSERR_MINORVERMISMATCH:
1100 case NFSERR_OLDSTATEID:
1101 case NFSERR_BADSEQID:
1102 case NFSERR_LEASEMOVED:
1103 case NFSERR_RECLAIMBAD:
1105 case NFSERR_BADCHAR:
1106 case NFSERR_BADNAME:
1107 case NFSERR_OPILLEGAL:
1108 printf("nfsv4 client/server protocol prob err=%d\n",
1112 tprintf(p, LOG_INFO, "nfsv4 err=%d\n", error);
1118 * Locate a process by number; return only "live" processes -- i.e., neither
1119 * zombies nor newly born but incompletely initialized processes. By not
1120 * returning processes in the PRS_NEW state, we allow callers to avoid
1121 * testing for that condition to avoid dereferencing p_ucred, et al.
1122 * Identical to pfind() in kern_proc.c, except it assume the list is
1125 static struct proc *
1126 pfind_locked(pid_t pid)
1130 LIST_FOREACH(p, PIDHASH(pid), p_hash)
1131 if (p->p_pid == pid) {
1133 if (p->p_state == PRS_NEW) {
1143 * Check to see if the process for this owner exists. Return 1 if it doesn't
1147 nfscl_procdoesntexist(u_int8_t *own)
1157 tl.cval[0] = *own++;
1158 tl.cval[1] = *own++;
1159 tl.cval[2] = *own++;
1160 tl.cval[3] = *own++;
1162 p = pfind_locked(pid);
1165 if (p->p_stats == NULL) {
1169 tl.cval[0] = *own++;
1170 tl.cval[1] = *own++;
1171 tl.cval[2] = *own++;
1172 tl.cval[3] = *own++;
1173 if (tl.lval != p->p_stats->p_start.tv_sec) {
1176 tl.cval[0] = *own++;
1177 tl.cval[1] = *own++;
1178 tl.cval[2] = *own++;
1180 if (tl.lval != p->p_stats->p_start.tv_usec)
1188 * - nfs pseudo system call for the client
1194 nfssvc_nfscl(struct thread *td, struct nfssvc_args *uap)
1197 struct nfscbd_args nfscbdarg;
1198 struct nfsd_nfscbd_args nfscbdarg2;
1201 if (uap->flag & NFSSVC_CBADDSOCK) {
1202 error = copyin(uap->argp, (caddr_t)&nfscbdarg, sizeof(nfscbdarg));
1205 if ((error = fget(td, nfscbdarg.sock, &fp)) != 0) {
1208 if (fp->f_type != DTYPE_SOCKET) {
1212 error = nfscbd_addsock(fp);
1214 if (!error && nfscl_enablecallb == 0) {
1215 nfsv4_cbport = nfscbdarg.port;
1216 nfscl_enablecallb = 1;
1218 } else if (uap->flag & NFSSVC_NFSCBD) {
1219 if (uap->argp == NULL)
1221 error = copyin(uap->argp, (caddr_t)&nfscbdarg2,
1222 sizeof(nfscbdarg2));
1225 error = nfscbd_nfsd(td, &nfscbdarg2);
1232 extern int (*nfsd_call_nfscl)(struct thread *, struct nfssvc_args *);
1235 * Called once to initialize data structures...
1238 nfscl_modevent(module_t mod, int type, void *data)
1241 static int loaded = 0;
1248 mtx_init(&nfs_clstate_mutex, "nfs_clstate_mutex", NULL,
1250 mtx_init(&ncl_iod_mutex, "ncl_iod_mutex", NULL, MTX_DEF);
1255 ncl_call_invalcaches = ncl_invalcaches;
1256 nfsd_call_nfscl = nfssvc_nfscl;
1261 if (nfs_numnfscbd != 0) {
1267 * XXX: Unloading of nfscl module is unsupported.
1270 ncl_call_invalcaches = NULL;
1271 nfsd_call_nfscl = NULL;
1272 /* and get rid of the mutexes */
1273 mtx_destroy(&nfs_clstate_mutex);
1274 mtx_destroy(&ncl_iod_mutex);
1286 static moduledata_t nfscl_mod = {
1291 DECLARE_MODULE(nfscl, nfscl_mod, SI_SUB_VFS, SI_ORDER_FIRST);
1293 /* So that loader and kldload(2) can find us, wherever we are.. */
1294 MODULE_VERSION(nfscl, 1);
1295 MODULE_DEPEND(nfscl, nfscommon, 1, 1, 1);
1296 MODULE_DEPEND(nfscl, krpc, 1, 1, 1);
1297 MODULE_DEPEND(nfscl, nfssvc, 1, 1, 1);
1298 MODULE_DEPEND(nfscl, nfslock, 1, 1, 1);