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.
41 #include <fs/nfs/nfsport.h>
42 #include <netinet/if_ether.h>
43 #include <net/if_types.h>
45 extern u_int32_t newnfs_true, newnfs_false, newnfs_xdrneg1;
46 extern struct vop_vector newnfs_vnodeops;
47 extern struct vop_vector newnfs_fifoops;
48 extern uma_zone_t newnfsnode_zone;
49 extern struct buf_ops buf_ops_newnfs;
50 extern int ncl_pbuf_freecnt;
51 extern short nfsv4_cbport;
52 extern int nfscl_enablecallb;
53 extern int nfs_numnfscbd;
54 extern int nfscl_inited;
55 struct mtx nfs_clstate_mutex;
56 struct mtx ncl_iod_mutex;
59 extern void (*ncl_call_invalcaches)(struct vnode *);
62 * Comparison function for vfs_hash functions.
65 newnfs_vncmpf(struct vnode *vp, void *arg)
67 struct nfsfh *nfhp = (struct nfsfh *)arg;
68 struct nfsnode *np = VTONFS(vp);
70 if (np->n_fhp->nfh_len != nfhp->nfh_len ||
71 NFSBCMP(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len))
77 * Look up a vnode/nfsnode by file handle.
78 * Callers must check for mount points!!
79 * In all cases, a pointer to a
80 * nfsnode structure is returned.
81 * This variant takes a "struct nfsfh *" as second argument and uses
82 * that structure up, either by hanging off the nfsnode or FREEing it.
85 nfscl_nget(struct mount *mntp, struct vnode *dvp, struct nfsfh *nfhp,
86 struct componentname *cnp, struct thread *td, struct nfsnode **npp,
89 struct nfsnode *np, *dnp;
90 struct vnode *vp, *nvp;
91 struct nfsv4node *newd, *oldd;
100 hash = fnv_32_buf(nfhp->nfh_fh, nfhp->nfh_len, FNV1_32_INIT);
102 error = vfs_hash_get(mntp, hash, LK_EXCLUSIVE,
103 td, &nvp, newnfs_vncmpf, nfhp);
104 if (error == 0 && nvp != NULL) {
106 * I believe there is a slight chance that vgonel() could
107 * get called on this vnode between when vn_lock() drops
108 * the VI_LOCK() and vget() acquires it again, so that it
109 * hasn't yet had v_usecount incremented. If this were to
110 * happen, the VI_DOOMED flag would be set, so check for
111 * that here. Since we now have the v_usecount incremented,
112 * we should be ok until we vrele() it, if the VI_DOOMED
113 * flag isn't set now.
116 if ((nvp->v_iflag & VI_DOOMED)) {
125 FREE((caddr_t)nfhp, M_NFSFH);
131 * For NFSv4, check to see if it is the same name and
132 * replace the name, if it is different.
135 if ((nmp->nm_flag & NFSMNT_NFSV4) && np->n_v4 != NULL &&
136 nvp->v_type == VREG &&
137 (np->n_v4->n4_namelen != cnp->cn_namelen ||
138 NFSBCMP(cnp->cn_nameptr, NFS4NODENAME(np->n_v4),
140 dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen ||
141 NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
142 dnp->n_fhp->nfh_len))) {
143 MALLOC(newd, struct nfsv4node *,
144 sizeof (struct nfsv4node) + dnp->n_fhp->nfh_len +
145 + cnp->cn_namelen - 1, M_NFSV4NODE, M_WAITOK);
147 if (newd != NULL && np->n_v4 != NULL && nvp->v_type == VREG
148 && (np->n_v4->n4_namelen != cnp->cn_namelen ||
149 NFSBCMP(cnp->cn_nameptr, NFS4NODENAME(np->n_v4),
151 dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen ||
152 NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
153 dnp->n_fhp->nfh_len))) {
157 np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
158 np->n_v4->n4_namelen = cnp->cn_namelen;
159 NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
160 dnp->n_fhp->nfh_len);
161 NFSBCOPY(cnp->cn_nameptr, NFS4NODENAME(np->n_v4),
167 FREE((caddr_t)newd, M_NFSV4NODE);
169 FREE((caddr_t)oldd, M_NFSV4NODE);
171 FREE((caddr_t)nfhp, M_NFSFH);
176 * Allocate before getnewvnode since doing so afterward
177 * might cause a bogus v_data pointer to get dereferenced
178 * elsewhere if zalloc should block.
180 np = uma_zalloc(newnfsnode_zone, M_WAITOK | M_ZERO);
182 error = getnewvnode("newnfs", mntp, &newnfs_vnodeops, &nvp);
184 uma_zfree(newnfsnode_zone, np);
185 FREE((caddr_t)nfhp, M_NFSFH);
189 vp->v_bufobj.bo_ops = &buf_ops_newnfs;
193 * Initialize the mutex even if the vnode is going to be a loser.
194 * This simplifies the logic in reclaim, which can then unconditionally
195 * destroy the mutex (in the case of the loser, or if hash_insert
196 * happened to return an error no special casing is needed).
198 mtx_init(&np->n_mtx, "NEWNFSnode lock", NULL, MTX_DEF | MTX_DUPOK);
201 * Are we getting the root? If so, make sure the vnode flags
204 if ((nfhp->nfh_len == nmp->nm_fhsize) &&
205 !bcmp(nfhp->nfh_fh, nmp->nm_fh, nfhp->nfh_len)) {
206 if (vp->v_type == VNON)
208 vp->v_vflag |= VV_ROOT;
213 * For NFSv4, we have to attach the directory file handle and
214 * file name, so that Open Ops can be done later.
216 if (nmp->nm_flag & NFSMNT_NFSV4) {
217 MALLOC(np->n_v4, struct nfsv4node *, sizeof (struct nfsv4node)
218 + dnp->n_fhp->nfh_len + cnp->cn_namelen - 1, M_NFSV4NODE,
220 np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len;
221 np->n_v4->n4_namelen = cnp->cn_namelen;
222 NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data,
223 dnp->n_fhp->nfh_len);
224 NFSBCOPY(cnp->cn_nameptr, NFS4NODENAME(np->n_v4),
231 * NFS supports recursive and shared locking.
235 lockmgr(vp->v_vnlock, LK_EXCLUSIVE | LK_NOWITNESS, NULL);
236 error = insmntque(vp, mntp);
239 mtx_destroy(&np->n_mtx);
240 FREE((caddr_t)nfhp, M_NFSFH);
241 if (np->n_v4 != NULL)
242 FREE((caddr_t)np->n_v4, M_NFSV4NODE);
243 uma_zfree(newnfsnode_zone, np);
246 error = vfs_hash_insert(vp, hash, LK_EXCLUSIVE,
247 td, &nvp, newnfs_vncmpf, nfhp);
252 /* vfs_hash_insert() vput()'s the losing vnode */
261 * Anothe variant of nfs_nget(). This one is only used by reopen. It
262 * takes almost the same args as nfs_nget(), but only succeeds if an entry
263 * exists in the cache. (Since files should already be "open" with a
264 * vnode ref cnt on the node when reopen calls this, it should always
266 * Also, don't get a vnode lock, since it may already be locked by some
267 * other process that is handling it. This is ok, since all other threads
268 * on the client are blocked by the nfsc_lock being exclusively held by the
269 * caller of this function.
272 nfscl_ngetreopen(struct mount *mntp, u_int8_t *fhp, int fhsize,
273 struct thread *td, struct nfsnode **npp)
281 /* For forced dismounts, just return error. */
282 if ((mntp->mnt_kern_flag & MNTK_UNMOUNTF))
284 MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) + fhsize,
286 bcopy(fhp, &nfhp->nfh_fh[0], fhsize);
287 nfhp->nfh_len = fhsize;
289 hash = fnv_32_buf(fhp, fhsize, FNV1_32_INIT);
292 * First, try to get the vnode locked, but don't block for the lock.
294 error = vfs_hash_get(mntp, hash, (LK_EXCLUSIVE | LK_NOWAIT), td, &nvp,
295 newnfs_vncmpf, nfhp);
296 if (error == 0 && nvp != NULL) {
298 } else if (error == EBUSY) {
300 * The LK_EXCLOTHER lock type tells nfs_lock1() to not try
301 * and lock the vnode, but just get a v_usecount on it.
302 * LK_NOWAIT is set so that when vget() returns ENOENT,
303 * vfs_hash_get() fails instead of looping.
304 * If this succeeds, it is safe so long as a vflush() with
305 * FORCECLOSE has not been done. Since the Renew thread is
306 * stopped and the MNTK_UNMOUNTF flag is set before doing
307 * a vflush() with FORCECLOSE, we should be ok here.
309 if ((mntp->mnt_kern_flag & MNTK_UNMOUNTF))
312 error = vfs_hash_get(mntp, hash,
313 (LK_EXCLOTHER | LK_NOWAIT), td, &nvp,
314 newnfs_vncmpf, nfhp);
327 * Load the attribute cache (that lives in the nfsnode entry) with
328 * the attributes of the second argument and
330 * copy the attributes to *vaper
331 * Similar to nfs_loadattrcache(), except the attributes are passed in
332 * instead of being parsed out of the mbuf list.
335 nfscl_loadattrcache(struct vnode **vpp, struct nfsvattr *nap, void *nvaper,
336 void *stuff, int writeattr, int dontshrink)
338 struct vnode *vp = *vpp;
339 struct vattr *vap, *nvap = &nap->na_vattr, *vaper = nvaper;
341 struct nfsmount *nmp;
342 struct timespec mtime_save;
345 * If v_type == VNON it is a new node, so fill in the v_type,
346 * n_mtime fields. Check to see if it represents a special
347 * device, and if so, check for a possible alias. Once the
348 * correct vnode has been obtained, fill in the rest of the
353 if (vp->v_type != nvap->va_type) {
354 vp->v_type = nvap->va_type;
355 if (vp->v_type == VFIFO)
356 vp->v_op = &newnfs_fifoops;
357 np->n_mtime = nvap->va_mtime;
359 nmp = VFSTONFS(vp->v_mount);
360 vap = &np->n_vattr.na_vattr;
361 mtime_save = vap->va_mtime;
363 np->n_vattr.na_filerev = nap->na_filerev;
364 np->n_vattr.na_size = nap->na_size;
365 np->n_vattr.na_mtime = nap->na_mtime;
366 np->n_vattr.na_ctime = nap->na_ctime;
367 np->n_vattr.na_fsid = nap->na_fsid;
369 NFSBCOPY((caddr_t)nap, (caddr_t)&np->n_vattr,
370 sizeof (struct nfsvattr));
374 * For NFSv4, if the node's fsid is not equal to the mount point's
375 * fsid, return the low order 32bits of the node's fsid. This
376 * allows getcwd(3) to work. There is a chance that the fsid might
377 * be the same as a local fs, but since this is in an NFS mount
378 * point, I don't think that will cause any problems?
380 if ((nmp->nm_flag & (NFSMNT_NFSV4 | NFSMNT_HASSETFSID)) ==
381 (NFSMNT_NFSV4 | NFSMNT_HASSETFSID) &&
382 (nmp->nm_fsid[0] != np->n_vattr.na_filesid[0] ||
383 nmp->nm_fsid[1] != np->n_vattr.na_filesid[1]))
384 vap->va_fsid = np->n_vattr.na_filesid[0];
386 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
387 np->n_attrstamp = time_second;
388 if (vap->va_size != np->n_size) {
389 if (vap->va_type == VREG) {
390 if (dontshrink && vap->va_size < np->n_size) {
392 * We've been told not to shrink the file;
393 * zero np->n_attrstamp to indicate that
394 * the attributes are stale.
396 vap->va_size = np->n_size;
398 } else if (np->n_flag & NMODIFIED) {
400 * We've modified the file: Use the larger
401 * of our size, and the server's size.
403 if (vap->va_size < np->n_size) {
404 vap->va_size = np->n_size;
406 np->n_size = vap->va_size;
407 np->n_flag |= NSIZECHANGED;
410 np->n_size = vap->va_size;
411 np->n_flag |= NSIZECHANGED;
413 vnode_pager_setsize(vp, np->n_size);
415 np->n_size = vap->va_size;
419 * The following checks are added to prevent a race between (say)
420 * a READDIR+ and a WRITE.
421 * READDIR+, WRITE requests sent out.
422 * READDIR+ resp, WRITE resp received on client.
423 * However, the WRITE resp was handled before the READDIR+ resp
424 * causing the post op attrs from the write to be loaded first
425 * and the attrs from the READDIR+ to be loaded later. If this
426 * happens, we have stale attrs loaded into the attrcache.
427 * We detect this by for the mtime moving back. We invalidate the
428 * attrcache when this happens.
430 if (timespeccmp(&mtime_save, &vap->va_mtime, >))
431 /* Size changed or mtime went backwards */
434 NFSBCOPY((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
435 if (np->n_flag & NCHG) {
436 if (np->n_flag & NACC)
437 vaper->va_atime = np->n_atim;
438 if (np->n_flag & NUPD)
439 vaper->va_mtime = np->n_mtim;
447 * Fill in the client id name. For these bytes:
448 * 1 - they must be unique
449 * 2 - they should be persistent across client reboots
450 * 1 is more critical than 2
451 * Use the mount point's unique id plus either the uuid or, if that
452 * isn't set, random junk.
455 nfscl_fillclid(u_int64_t clval, char *uuid, u_int8_t *cp, u_int16_t idlen)
460 * First, put in the 64bit mount point identifier.
462 if (idlen >= sizeof (u_int64_t)) {
463 NFSBCOPY((caddr_t)&clval, cp, sizeof (u_int64_t));
464 cp += sizeof (u_int64_t);
465 idlen -= sizeof (u_int64_t);
469 * If uuid is non-zero length, use it.
471 uuidlen = strlen(uuid);
472 if (uuidlen > 0 && idlen >= uuidlen) {
473 NFSBCOPY(uuid, cp, uuidlen);
479 * This only normally happens if the uuid isn't set.
482 *cp++ = (u_int8_t)(arc4random() % 256);
488 * Fill in a lock owner name. For now, pid + the process's creation time.
491 nfscl_filllockowner(struct thread *td, u_int8_t *cp)
506 printf("NULL pid\n");
515 if (p->p_stats == NULL) {
516 printf("pstats null\n");
520 tl.lval = p->p_stats->p_start.tv_sec;
525 tl.lval = p->p_stats->p_start.tv_usec;
533 * Find the parent process for the thread passed in as an argument.
534 * If none exists, return NULL, otherwise return a thread for the parent.
535 * (Can be any of the threads, since it is only used for td->td_proc.)
538 nfscl_getparent(struct thread *td)
551 ptd = TAILQ_FIRST(&p->p_threads);
556 * Start up the renew kernel thread.
559 start_nfscl(void *arg)
561 struct nfsclclient *clp;
564 clp = (struct nfsclclient *)arg;
565 td = TAILQ_FIRST(&clp->nfsc_renewthread->p_threads);
566 nfscl_renewthread(clp, td);
571 nfscl_start_renewthread(struct nfsclclient *clp)
574 kproc_create(start_nfscl, (void *)clp, &clp->nfsc_renewthread, 0, 0,
580 * For NFSv4, it assumes that nfsv4_wccattr() was used to set up the getattr
581 * as the first Op after PutFH.
582 * (For NFSv4, the postop attributes are after the Op, so they can't be
583 * parsed here. A separate call to nfscl_postop_attr() is required.)
586 nfscl_wcc_data(struct nfsrv_descript *nd, struct vnode *vp,
587 struct nfsvattr *nap, int *flagp, int *wccflagp, void *stuff)
590 struct nfsnode *np = VTONFS(vp);
591 struct nfsvattr nfsva;
594 if (wccflagp != NULL)
596 if (nd->nd_flag & ND_NFSV3) {
598 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
599 if (*tl == newnfs_true) {
600 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
601 if (wccflagp != NULL) {
602 mtx_lock(&np->n_mtx);
603 *wccflagp = (np->n_mtime.tv_sec ==
604 fxdr_unsigned(u_int32_t, *(tl + 2)) &&
605 np->n_mtime.tv_nsec ==
606 fxdr_unsigned(u_int32_t, *(tl + 3)));
607 mtx_unlock(&np->n_mtx);
610 error = nfscl_postop_attr(nd, nap, flagp, stuff);
611 } else if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR))
612 == (ND_NFSV4 | ND_V4WCCATTR)) {
613 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
614 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
615 NULL, NULL, NULL, NULL, NULL);
619 * Get rid of Op# and status for next op.
621 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
623 nd->nd_flag |= ND_NOMOREDATA;
624 if (wccflagp != NULL &&
625 nfsva.na_vattr.va_mtime.tv_sec != 0) {
626 mtx_lock(&np->n_mtx);
627 *wccflagp = (np->n_mtime.tv_sec ==
628 nfsva.na_vattr.va_mtime.tv_sec &&
629 np->n_mtime.tv_nsec ==
630 nfsva.na_vattr.va_mtime.tv_sec);
631 mtx_unlock(&np->n_mtx);
639 * Get postop attributes.
642 nfscl_postop_attr(struct nfsrv_descript *nd, struct nfsvattr *nap, int *retp,
649 if (nd->nd_flag & ND_NOMOREDATA)
651 if (nd->nd_flag & ND_NFSV3) {
652 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
653 *retp = fxdr_unsigned(int, *tl);
654 } else if (nd->nd_flag & ND_NFSV4) {
656 * For NFSv4, the postop attr are at the end, so no point
657 * in looking if nd_repstat != 0.
659 if (!nd->nd_repstat) {
660 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
662 /* should never happen since nd_repstat != 0 */
663 nd->nd_flag |= ND_NOMOREDATA;
667 } else if (!nd->nd_repstat) {
668 /* For NFSv2, the attributes are here iff nd_repstat == 0 */
672 error = nfsm_loadattr(nd, nap);
681 * Fill in the setable attributes. The full argument indicates whether
682 * to fill in them all or just mode and time.
685 nfscl_fillsattr(struct nfsrv_descript *nd, struct vattr *vap,
686 struct vnode *vp, int flags, u_int32_t rdev)
689 struct nfsv2_sattr *sp;
690 nfsattrbit_t attrbits;
691 struct timeval curtime;
693 switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
695 NFSM_BUILD(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
696 if (vap->va_mode == (mode_t)VNOVAL)
697 sp->sa_mode = newnfs_xdrneg1;
699 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
700 if (vap->va_uid == (uid_t)VNOVAL)
701 sp->sa_uid = newnfs_xdrneg1;
703 sp->sa_uid = txdr_unsigned(vap->va_uid);
704 if (vap->va_gid == (gid_t)VNOVAL)
705 sp->sa_gid = newnfs_xdrneg1;
707 sp->sa_gid = txdr_unsigned(vap->va_gid);
708 if (flags & NFSSATTR_SIZE0)
710 else if (flags & NFSSATTR_SIZENEG1)
711 sp->sa_size = newnfs_xdrneg1;
712 else if (flags & NFSSATTR_SIZERDEV)
713 sp->sa_size = txdr_unsigned(rdev);
715 sp->sa_size = txdr_unsigned(vap->va_size);
716 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
717 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
720 getmicrotime(&curtime);
721 if (vap->va_mode != (mode_t)VNOVAL) {
722 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
724 *tl = txdr_unsigned(vap->va_mode);
726 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
729 if ((flags & NFSSATTR_FULL) && vap->va_uid != (uid_t)VNOVAL) {
730 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
732 *tl = txdr_unsigned(vap->va_uid);
734 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
737 if ((flags & NFSSATTR_FULL) && vap->va_gid != (gid_t)VNOVAL) {
738 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
740 *tl = txdr_unsigned(vap->va_gid);
742 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
745 if ((flags & NFSSATTR_FULL) && vap->va_size != VNOVAL) {
746 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
748 txdr_hyper(vap->va_size, tl);
750 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
753 if (vap->va_atime.tv_sec != VNOVAL) {
754 if (vap->va_atime.tv_sec != curtime.tv_sec) {
755 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
756 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
757 txdr_nfsv3time(&vap->va_atime, tl);
759 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
760 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
763 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
764 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
766 if (vap->va_mtime.tv_sec != VNOVAL) {
767 if (vap->va_mtime.tv_sec != curtime.tv_sec) {
768 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
769 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
770 txdr_nfsv3time(&vap->va_mtime, tl);
772 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
773 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
776 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
777 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
781 NFSZERO_ATTRBIT(&attrbits);
782 if (vap->va_mode != (mode_t)VNOVAL)
783 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_MODE);
784 if ((flags & NFSSATTR_FULL) && vap->va_uid != (uid_t)VNOVAL)
785 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_OWNER);
786 if ((flags & NFSSATTR_FULL) && vap->va_gid != (gid_t)VNOVAL)
787 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_OWNERGROUP);
788 if ((flags & NFSSATTR_FULL) && vap->va_size != VNOVAL)
789 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
790 if (vap->va_atime.tv_sec != VNOVAL)
791 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESSSET);
792 if (vap->va_mtime.tv_sec != VNOVAL)
793 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFYSET);
794 (void) nfsv4_fillattr(nd, vp, NULL, vap, NULL, 0, &attrbits,
801 * nfscl_request() - mostly a wrapper for newnfs_request().
804 nfscl_request(struct nfsrv_descript *nd, struct vnode *vp, NFSPROC_T *p,
805 struct ucred *cred, void *stuff)
808 struct nfsmount *nmp;
810 nmp = VFSTONFS(vp->v_mount);
811 if (nd->nd_flag & ND_NFSV4)
813 else if (nd->nd_flag & ND_NFSV3)
817 ret = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
818 NFS_PROG, vers, NULL, 1, NULL);
823 * fill in this bsden's variant of statfs using nfsstatfs.
826 nfscl_loadsbinfo(struct nfsmount *nmp, struct nfsstatfs *sfp, void *statfs)
828 struct statfs *sbp = (struct statfs *)statfs;
831 if (nmp->nm_flag & (NFSMNT_NFSV3 | NFSMNT_NFSV4)) {
832 sbp->f_bsize = NFS_FABLKSIZE;
833 tquad.qval = sfp->sf_tbytes;
834 sbp->f_blocks = (long)(tquad.qval / ((u_quad_t)NFS_FABLKSIZE));
835 tquad.qval = sfp->sf_fbytes;
836 sbp->f_bfree = (long)(tquad.qval / ((u_quad_t)NFS_FABLKSIZE));
837 tquad.qval = sfp->sf_abytes;
838 sbp->f_bavail = (long)(tquad.qval / ((u_quad_t)NFS_FABLKSIZE));
839 tquad.qval = sfp->sf_tfiles;
840 sbp->f_files = (tquad.lval[0] & 0x7fffffff);
841 tquad.qval = sfp->sf_ffiles;
842 sbp->f_ffree = (tquad.lval[0] & 0x7fffffff);
843 } else if ((nmp->nm_flag & NFSMNT_NFSV4) == 0) {
844 sbp->f_bsize = (int32_t)sfp->sf_bsize;
845 sbp->f_blocks = (int32_t)sfp->sf_blocks;
846 sbp->f_bfree = (int32_t)sfp->sf_bfree;
847 sbp->f_bavail = (int32_t)sfp->sf_bavail;
854 * Use the fsinfo stuff to update the mount point.
857 nfscl_loadfsinfo(struct nfsmount *nmp, struct nfsfsinfo *fsp)
860 if ((nmp->nm_wsize == 0 || fsp->fs_wtpref < nmp->nm_wsize) &&
861 fsp->fs_wtpref >= NFS_FABLKSIZE)
862 nmp->nm_wsize = (fsp->fs_wtpref + NFS_FABLKSIZE - 1) &
863 ~(NFS_FABLKSIZE - 1);
864 if (fsp->fs_wtmax < nmp->nm_wsize && fsp->fs_wtmax > 0) {
865 nmp->nm_wsize = fsp->fs_wtmax & ~(NFS_FABLKSIZE - 1);
866 if (nmp->nm_wsize == 0)
867 nmp->nm_wsize = fsp->fs_wtmax;
869 if (nmp->nm_wsize < NFS_FABLKSIZE)
870 nmp->nm_wsize = NFS_FABLKSIZE;
871 if ((nmp->nm_rsize == 0 || fsp->fs_rtpref < nmp->nm_rsize) &&
872 fsp->fs_rtpref >= NFS_FABLKSIZE)
873 nmp->nm_rsize = (fsp->fs_rtpref + NFS_FABLKSIZE - 1) &
874 ~(NFS_FABLKSIZE - 1);
875 if (fsp->fs_rtmax < nmp->nm_rsize && fsp->fs_rtmax > 0) {
876 nmp->nm_rsize = fsp->fs_rtmax & ~(NFS_FABLKSIZE - 1);
877 if (nmp->nm_rsize == 0)
878 nmp->nm_rsize = fsp->fs_rtmax;
880 if (nmp->nm_rsize < NFS_FABLKSIZE)
881 nmp->nm_rsize = NFS_FABLKSIZE;
882 if ((nmp->nm_readdirsize == 0 || fsp->fs_dtpref < nmp->nm_readdirsize)
883 && fsp->fs_dtpref >= NFS_DIRBLKSIZ)
884 nmp->nm_readdirsize = (fsp->fs_dtpref + NFS_DIRBLKSIZ - 1) &
885 ~(NFS_DIRBLKSIZ - 1);
886 if (fsp->fs_rtmax < nmp->nm_readdirsize && fsp->fs_rtmax > 0) {
887 nmp->nm_readdirsize = fsp->fs_rtmax & ~(NFS_DIRBLKSIZ - 1);
888 if (nmp->nm_readdirsize == 0)
889 nmp->nm_readdirsize = fsp->fs_rtmax;
891 if (nmp->nm_readdirsize < NFS_DIRBLKSIZ)
892 nmp->nm_readdirsize = NFS_DIRBLKSIZ;
893 if (fsp->fs_maxfilesize > 0 &&
894 fsp->fs_maxfilesize < nmp->nm_maxfilesize)
895 nmp->nm_maxfilesize = fsp->fs_maxfilesize;
896 nmp->nm_mountp->mnt_stat.f_iosize = newnfs_iosize(nmp);
897 nmp->nm_state |= NFSSTA_GOTFSINFO;
901 * Get a pointer to my IP addrress and return it.
902 * Return NULL if you can't find one.
905 nfscl_getmyip(struct nfsmount *nmp, int *isinet6p)
907 struct sockaddr_in sad, *sin;
909 u_int8_t *retp = NULL;
910 static struct in_addr laddr;
914 * Loop up a route for the destination address.
916 if (nmp->nm_nam->sa_family == AF_INET) {
917 bzero(&sad, sizeof (sad));
918 sin = (struct sockaddr_in *)nmp->nm_nam;
919 sad.sin_family = AF_INET;
920 sad.sin_len = sizeof (struct sockaddr_in);
921 sad.sin_addr.s_addr = sin->sin_addr.s_addr;
922 rt = rtalloc1((struct sockaddr *)&sad, 0, 0UL);
924 if (rt->rt_ifp != NULL &&
925 rt->rt_ifa != NULL &&
926 ((rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) &&
927 rt->rt_ifa->ifa_addr->sa_family == AF_INET) {
928 sin = (struct sockaddr_in *)
929 rt->rt_ifa->ifa_addr;
930 laddr.s_addr = sin->sin_addr.s_addr;
931 retp = (u_int8_t *)&laddr;
936 } else if (nmp->nm_nam->sa_family == AF_INET6) {
937 struct sockaddr_in6 sad6, *sin6;
938 static struct in6_addr laddr6;
940 bzero(&sad6, sizeof (sad6));
941 sin6 = (struct sockaddr_in6 *)nmp->nm_nam;
942 sad6.sin6_family = AF_INET6;
943 sad6.sin6_len = sizeof (struct sockaddr_in6);
944 sad6.sin6_addr = sin6->sin6_addr;
945 rt = rtalloc1((struct sockaddr *)&sad6, 0, 0UL);
947 if (rt->rt_ifp != NULL &&
948 rt->rt_ifa != NULL &&
949 ((rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) &&
950 rt->rt_ifa->ifa_addr->sa_family == AF_INET6) {
951 sin6 = (struct sockaddr_in6 *)
952 rt->rt_ifa->ifa_addr;
953 laddr6 = sin6->sin6_addr;
954 retp = (u_int8_t *)&laddr6;
965 * Copy NFS uid, gids from the cred structure.
968 newnfs_copyincred(struct ucred *cr, struct nfscred *nfscr)
972 KASSERT(cr->cr_ngroups >= 0,
973 ("newnfs_copyincred: negative cr_ngroups"));
974 nfscr->nfsc_uid = cr->cr_uid;
975 nfscr->nfsc_ngroups = MIN(cr->cr_ngroups, NFS_MAXGRPS + 1);
976 for (i = 0; i < nfscr->nfsc_ngroups; i++)
977 nfscr->nfsc_groups[i] = cr->cr_groups[i];
982 * Do any client specific initialization.
987 static int inited = 0;
993 ncl_pbuf_freecnt = nswbuf / 2 + 1;
997 * Check each of the attributes to be set, to ensure they aren't already
998 * the correct value. Disable setting ones already correct.
1001 nfscl_checksattr(struct vattr *vap, struct nfsvattr *nvap)
1004 if (vap->va_mode != (mode_t)VNOVAL) {
1005 if (vap->va_mode == nvap->na_mode)
1006 vap->va_mode = (mode_t)VNOVAL;
1008 if (vap->va_uid != (uid_t)VNOVAL) {
1009 if (vap->va_uid == nvap->na_uid)
1010 vap->va_uid = (uid_t)VNOVAL;
1012 if (vap->va_gid != (gid_t)VNOVAL) {
1013 if (vap->va_gid == nvap->na_gid)
1014 vap->va_gid = (gid_t)VNOVAL;
1016 if (vap->va_size != VNOVAL) {
1017 if (vap->va_size == nvap->na_size)
1018 vap->va_size = VNOVAL;
1022 * We are normally called with only a partially initialized
1023 * VAP. Since the NFSv3 spec says that server may use the
1024 * file attributes to store the verifier, the spec requires
1025 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1026 * in atime, but we can't really assume that all servers will
1027 * so we ensure that our SETATTR sets both atime and mtime.
1029 if (vap->va_mtime.tv_sec == VNOVAL)
1030 vfs_timestamp(&vap->va_mtime);
1031 if (vap->va_atime.tv_sec == VNOVAL)
1032 vap->va_atime = vap->va_mtime;
1037 * Map nfsv4 errors to errno.h errors.
1038 * The uid and gid arguments are only used for NFSERR_BADOWNER and that
1039 * error should only be returned for the Open, Create and Setattr Ops.
1040 * As such, most calls can just pass in 0 for those arguments.
1043 nfscl_maperr(struct thread *td, int error, uid_t uid, gid_t gid)
1054 case NFSERR_BADOWNER:
1055 tprintf(p, LOG_INFO,
1056 "No name and/or group mapping for uid,gid:(%d,%d)\n",
1059 case NFSERR_STALECLIENTID:
1060 case NFSERR_STALESTATEID:
1061 case NFSERR_EXPIRED:
1062 case NFSERR_BADSTATEID:
1063 printf("nfsv4 recover err returned %d\n", error);
1065 case NFSERR_BADHANDLE:
1066 case NFSERR_SERVERFAULT:
1067 case NFSERR_BADTYPE:
1068 case NFSERR_FHEXPIRED:
1069 case NFSERR_RESOURCE:
1071 case NFSERR_NOFILEHANDLE:
1072 case NFSERR_MINORVERMISMATCH:
1073 case NFSERR_OLDSTATEID:
1074 case NFSERR_BADSEQID:
1075 case NFSERR_LEASEMOVED:
1076 case NFSERR_RECLAIMBAD:
1078 case NFSERR_BADCHAR:
1079 case NFSERR_BADNAME:
1080 case NFSERR_OPILLEGAL:
1081 printf("nfsv4 client/server protocol prob err=%d\n",
1085 tprintf(p, LOG_INFO, "nfsv4 err=%d\n", error);
1091 * Locate a process by number; return only "live" processes -- i.e., neither
1092 * zombies nor newly born but incompletely initialized processes. By not
1093 * returning processes in the PRS_NEW state, we allow callers to avoid
1094 * testing for that condition to avoid dereferencing p_ucred, et al.
1095 * Identical to pfind() in kern_proc.c, except it assume the list is
1098 static struct proc *
1099 pfind_locked(pid_t pid)
1103 LIST_FOREACH(p, PIDHASH(pid), p_hash)
1104 if (p->p_pid == pid) {
1105 if (p->p_state == PRS_NEW) {
1116 * Check to see if the process for this owner exists. Return 1 if it doesn't
1120 nfscl_procdoesntexist(u_int8_t *own)
1130 tl.cval[0] = *own++;
1131 tl.cval[1] = *own++;
1132 tl.cval[2] = *own++;
1133 tl.cval[3] = *own++;
1135 p = pfind_locked(pid);
1138 if (p->p_stats == NULL) {
1142 tl.cval[0] = *own++;
1143 tl.cval[1] = *own++;
1144 tl.cval[2] = *own++;
1145 tl.cval[3] = *own++;
1146 if (tl.lval != p->p_stats->p_start.tv_sec) {
1149 tl.cval[0] = *own++;
1150 tl.cval[1] = *own++;
1151 tl.cval[2] = *own++;
1153 if (tl.lval != p->p_stats->p_start.tv_usec)
1161 * - nfs pseudo system call for the client
1167 nfssvc_nfscl(struct thread *td, struct nfssvc_args *uap)
1170 struct nfscbd_args nfscbdarg;
1171 struct nfsd_nfscbd_args nfscbdarg2;
1174 if (uap->flag & NFSSVC_CBADDSOCK) {
1175 error = copyin(uap->argp, (caddr_t)&nfscbdarg, sizeof(nfscbdarg));
1178 if ((error = fget(td, nfscbdarg.sock, &fp)) != 0) {
1181 if (fp->f_type != DTYPE_SOCKET) {
1185 error = nfscbd_addsock(fp);
1187 if (!error && nfscl_enablecallb == 0) {
1188 nfsv4_cbport = nfscbdarg.port;
1189 nfscl_enablecallb = 1;
1191 } else if (uap->flag & NFSSVC_NFSCBD) {
1192 if (uap->argp == NULL)
1194 error = copyin(uap->argp, (caddr_t)&nfscbdarg2,
1195 sizeof(nfscbdarg2));
1198 error = nfscbd_nfsd(td, &nfscbdarg2);
1205 extern int (*nfsd_call_nfscl)(struct thread *, struct nfssvc_args *);
1208 * Called once to initialize data structures...
1211 nfscl_modevent(module_t mod, int type, void *data)
1214 static int loaded = 0;
1221 mtx_init(&nfs_clstate_mutex, "nfs_clstate_mutex", NULL,
1223 mtx_init(&ncl_iod_mutex, "ncl_iod_mutex", NULL, MTX_DEF);
1228 ncl_call_invalcaches = ncl_invalcaches;
1229 nfsd_call_nfscl = nfssvc_nfscl;
1234 if (nfs_numnfscbd != 0) {
1240 * XXX: Unloading of nfscl module is unsupported.
1243 ncl_call_invalcaches = NULL;
1244 nfsd_call_nfscl = NULL;
1245 /* and get rid of the mutexes */
1246 mtx_destroy(&nfs_clstate_mutex);
1247 mtx_destroy(&ncl_iod_mutex);
1259 static moduledata_t nfscl_mod = {
1264 DECLARE_MODULE(nfscl, nfscl_mod, SI_SUB_VFS, SI_ORDER_FIRST);
1266 /* So that loader and kldload(2) can find us, wherever we are.. */
1267 MODULE_VERSION(nfscl, 1);
1268 MODULE_DEPEND(nfscl, nfscommon, 1, 1, 1);
1269 MODULE_DEPEND(nfscl, krpc, 1, 1, 1);
1270 MODULE_DEPEND(nfscl, nfssvc, 1, 1, 1);