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;
343 struct thread *td = curthread;
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;
370 NFSBCOPY((caddr_t)nap, (caddr_t)&np->n_vattr,
371 sizeof (struct nfsvattr));
375 * For NFSv4, if the node's fsid is not equal to the mount point's
376 * fsid, return the low order 32bits of the node's fsid. This
377 * allows getcwd(3) to work. There is a chance that the fsid might
378 * be the same as a local fs, but since this is in an NFS mount
379 * point, I don't think that will cause any problems?
381 if ((nmp->nm_flag & (NFSMNT_NFSV4 | NFSMNT_HASSETFSID)) ==
382 (NFSMNT_NFSV4 | NFSMNT_HASSETFSID) &&
383 (nmp->nm_fsid[0] != np->n_vattr.na_filesid[0] ||
384 nmp->nm_fsid[1] != np->n_vattr.na_filesid[1]))
385 vap->va_fsid = np->n_vattr.na_filesid[0];
387 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
388 np->n_attrstamp = time_second;
389 /* Timestamp the NFS otw getattr fetch */
391 np->n_ac_ts_tid = td->td_tid;
392 np->n_ac_ts_pid = td->td_proc->p_pid;
393 np->n_ac_ts_syscalls = td->td_syscalls;
395 bzero(&np->n_ac_ts, sizeof(struct nfs_attrcache_timestamp));
397 if (vap->va_size != np->n_size) {
398 if (vap->va_type == VREG) {
399 if (dontshrink && vap->va_size < np->n_size) {
401 * We've been told not to shrink the file;
402 * zero np->n_attrstamp to indicate that
403 * the attributes are stale.
405 vap->va_size = np->n_size;
407 } else if (np->n_flag & NMODIFIED) {
409 * We've modified the file: Use the larger
410 * of our size, and the server's size.
412 if (vap->va_size < np->n_size) {
413 vap->va_size = np->n_size;
415 np->n_size = vap->va_size;
416 np->n_flag |= NSIZECHANGED;
419 np->n_size = vap->va_size;
420 np->n_flag |= NSIZECHANGED;
422 vnode_pager_setsize(vp, np->n_size);
424 np->n_size = vap->va_size;
428 * The following checks are added to prevent a race between (say)
429 * a READDIR+ and a WRITE.
430 * READDIR+, WRITE requests sent out.
431 * READDIR+ resp, WRITE resp received on client.
432 * However, the WRITE resp was handled before the READDIR+ resp
433 * causing the post op attrs from the write to be loaded first
434 * and the attrs from the READDIR+ to be loaded later. If this
435 * happens, we have stale attrs loaded into the attrcache.
436 * We detect this by for the mtime moving back. We invalidate the
437 * attrcache when this happens.
439 if (timespeccmp(&mtime_save, &vap->va_mtime, >))
440 /* Size changed or mtime went backwards */
443 NFSBCOPY((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
444 if (np->n_flag & NCHG) {
445 if (np->n_flag & NACC)
446 vaper->va_atime = np->n_atim;
447 if (np->n_flag & NUPD)
448 vaper->va_mtime = np->n_mtim;
456 * Fill in the client id name. For these bytes:
457 * 1 - they must be unique
458 * 2 - they should be persistent across client reboots
459 * 1 is more critical than 2
460 * Use the mount point's unique id plus either the uuid or, if that
461 * isn't set, random junk.
464 nfscl_fillclid(u_int64_t clval, char *uuid, u_int8_t *cp, u_int16_t idlen)
469 * First, put in the 64bit mount point identifier.
471 if (idlen >= sizeof (u_int64_t)) {
472 NFSBCOPY((caddr_t)&clval, cp, sizeof (u_int64_t));
473 cp += sizeof (u_int64_t);
474 idlen -= sizeof (u_int64_t);
478 * If uuid is non-zero length, use it.
480 uuidlen = strlen(uuid);
481 if (uuidlen > 0 && idlen >= uuidlen) {
482 NFSBCOPY(uuid, cp, uuidlen);
488 * This only normally happens if the uuid isn't set.
491 *cp++ = (u_int8_t)(arc4random() % 256);
497 * Fill in a lock owner name. For now, pid + the process's creation time.
500 nfscl_filllockowner(struct thread *td, u_int8_t *cp)
515 printf("NULL pid\n");
524 if (p->p_stats == NULL) {
525 printf("pstats null\n");
529 tl.lval = p->p_stats->p_start.tv_sec;
534 tl.lval = p->p_stats->p_start.tv_usec;
542 * Find the parent process for the thread passed in as an argument.
543 * If none exists, return NULL, otherwise return a thread for the parent.
544 * (Can be any of the threads, since it is only used for td->td_proc.)
547 nfscl_getparent(struct thread *td)
560 ptd = TAILQ_FIRST(&p->p_threads);
565 * Start up the renew kernel thread.
568 start_nfscl(void *arg)
570 struct nfsclclient *clp;
573 clp = (struct nfsclclient *)arg;
574 td = TAILQ_FIRST(&clp->nfsc_renewthread->p_threads);
575 nfscl_renewthread(clp, td);
580 nfscl_start_renewthread(struct nfsclclient *clp)
583 kproc_create(start_nfscl, (void *)clp, &clp->nfsc_renewthread, 0, 0,
589 * For NFSv4, it assumes that nfsv4_wccattr() was used to set up the getattr
590 * as the first Op after PutFH.
591 * (For NFSv4, the postop attributes are after the Op, so they can't be
592 * parsed here. A separate call to nfscl_postop_attr() is required.)
595 nfscl_wcc_data(struct nfsrv_descript *nd, struct vnode *vp,
596 struct nfsvattr *nap, int *flagp, int *wccflagp, void *stuff)
599 struct nfsnode *np = VTONFS(vp);
600 struct nfsvattr nfsva;
603 if (wccflagp != NULL)
605 if (nd->nd_flag & ND_NFSV3) {
607 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
608 if (*tl == newnfs_true) {
609 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
610 if (wccflagp != NULL) {
611 mtx_lock(&np->n_mtx);
612 *wccflagp = (np->n_mtime.tv_sec ==
613 fxdr_unsigned(u_int32_t, *(tl + 2)) &&
614 np->n_mtime.tv_nsec ==
615 fxdr_unsigned(u_int32_t, *(tl + 3)));
616 mtx_unlock(&np->n_mtx);
619 error = nfscl_postop_attr(nd, nap, flagp, stuff);
620 } else if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR))
621 == (ND_NFSV4 | ND_V4WCCATTR)) {
622 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL,
623 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0,
624 NULL, NULL, NULL, NULL, NULL);
628 * Get rid of Op# and status for next op.
630 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
632 nd->nd_flag |= ND_NOMOREDATA;
633 if (wccflagp != NULL &&
634 nfsva.na_vattr.va_mtime.tv_sec != 0) {
635 mtx_lock(&np->n_mtx);
636 *wccflagp = (np->n_mtime.tv_sec ==
637 nfsva.na_vattr.va_mtime.tv_sec &&
638 np->n_mtime.tv_nsec ==
639 nfsva.na_vattr.va_mtime.tv_sec);
640 mtx_unlock(&np->n_mtx);
648 * Get postop attributes.
651 nfscl_postop_attr(struct nfsrv_descript *nd, struct nfsvattr *nap, int *retp,
658 if (nd->nd_flag & ND_NOMOREDATA)
660 if (nd->nd_flag & ND_NFSV3) {
661 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
662 *retp = fxdr_unsigned(int, *tl);
663 } else if (nd->nd_flag & ND_NFSV4) {
665 * For NFSv4, the postop attr are at the end, so no point
666 * in looking if nd_repstat != 0.
668 if (!nd->nd_repstat) {
669 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
671 /* should never happen since nd_repstat != 0 */
672 nd->nd_flag |= ND_NOMOREDATA;
676 } else if (!nd->nd_repstat) {
677 /* For NFSv2, the attributes are here iff nd_repstat == 0 */
681 error = nfsm_loadattr(nd, nap);
690 * Fill in the setable attributes. The full argument indicates whether
691 * to fill in them all or just mode and time.
694 nfscl_fillsattr(struct nfsrv_descript *nd, struct vattr *vap,
695 struct vnode *vp, int flags, u_int32_t rdev)
698 struct nfsv2_sattr *sp;
699 nfsattrbit_t attrbits;
700 struct timeval curtime;
702 switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
704 NFSM_BUILD(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
705 if (vap->va_mode == (mode_t)VNOVAL)
706 sp->sa_mode = newnfs_xdrneg1;
708 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
709 if (vap->va_uid == (uid_t)VNOVAL)
710 sp->sa_uid = newnfs_xdrneg1;
712 sp->sa_uid = txdr_unsigned(vap->va_uid);
713 if (vap->va_gid == (gid_t)VNOVAL)
714 sp->sa_gid = newnfs_xdrneg1;
716 sp->sa_gid = txdr_unsigned(vap->va_gid);
717 if (flags & NFSSATTR_SIZE0)
719 else if (flags & NFSSATTR_SIZENEG1)
720 sp->sa_size = newnfs_xdrneg1;
721 else if (flags & NFSSATTR_SIZERDEV)
722 sp->sa_size = txdr_unsigned(rdev);
724 sp->sa_size = txdr_unsigned(vap->va_size);
725 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
726 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
729 getmicrotime(&curtime);
730 if (vap->va_mode != (mode_t)VNOVAL) {
731 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
733 *tl = txdr_unsigned(vap->va_mode);
735 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
738 if ((flags & NFSSATTR_FULL) && vap->va_uid != (uid_t)VNOVAL) {
739 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
741 *tl = txdr_unsigned(vap->va_uid);
743 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
746 if ((flags & NFSSATTR_FULL) && vap->va_gid != (gid_t)VNOVAL) {
747 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
749 *tl = txdr_unsigned(vap->va_gid);
751 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
754 if ((flags & NFSSATTR_FULL) && vap->va_size != VNOVAL) {
755 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
757 txdr_hyper(vap->va_size, tl);
759 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
762 if (vap->va_atime.tv_sec != VNOVAL) {
763 if (vap->va_atime.tv_sec != curtime.tv_sec) {
764 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
765 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
766 txdr_nfsv3time(&vap->va_atime, tl);
768 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
769 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
772 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
773 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
775 if (vap->va_mtime.tv_sec != VNOVAL) {
776 if (vap->va_mtime.tv_sec != curtime.tv_sec) {
777 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
778 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
779 txdr_nfsv3time(&vap->va_mtime, tl);
781 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
782 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
785 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
786 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
790 NFSZERO_ATTRBIT(&attrbits);
791 if (vap->va_mode != (mode_t)VNOVAL)
792 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_MODE);
793 if ((flags & NFSSATTR_FULL) && vap->va_uid != (uid_t)VNOVAL)
794 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_OWNER);
795 if ((flags & NFSSATTR_FULL) && vap->va_gid != (gid_t)VNOVAL)
796 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_OWNERGROUP);
797 if ((flags & NFSSATTR_FULL) && vap->va_size != VNOVAL)
798 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
799 if (vap->va_atime.tv_sec != VNOVAL)
800 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESSSET);
801 if (vap->va_mtime.tv_sec != VNOVAL)
802 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFYSET);
803 (void) nfsv4_fillattr(nd, vp, NULL, vap, NULL, 0, &attrbits,
810 * nfscl_request() - mostly a wrapper for newnfs_request().
813 nfscl_request(struct nfsrv_descript *nd, struct vnode *vp, NFSPROC_T *p,
814 struct ucred *cred, void *stuff)
817 struct nfsmount *nmp;
819 nmp = VFSTONFS(vp->v_mount);
820 if (nd->nd_flag & ND_NFSV4)
822 else if (nd->nd_flag & ND_NFSV3)
826 ret = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred,
827 NFS_PROG, vers, NULL, 1, NULL);
832 * fill in this bsden's variant of statfs using nfsstatfs.
835 nfscl_loadsbinfo(struct nfsmount *nmp, struct nfsstatfs *sfp, void *statfs)
837 struct statfs *sbp = (struct statfs *)statfs;
840 if (nmp->nm_flag & (NFSMNT_NFSV3 | NFSMNT_NFSV4)) {
841 sbp->f_bsize = NFS_FABLKSIZE;
842 tquad.qval = sfp->sf_tbytes;
843 sbp->f_blocks = (long)(tquad.qval / ((u_quad_t)NFS_FABLKSIZE));
844 tquad.qval = sfp->sf_fbytes;
845 sbp->f_bfree = (long)(tquad.qval / ((u_quad_t)NFS_FABLKSIZE));
846 tquad.qval = sfp->sf_abytes;
847 sbp->f_bavail = (long)(tquad.qval / ((u_quad_t)NFS_FABLKSIZE));
848 tquad.qval = sfp->sf_tfiles;
849 sbp->f_files = (tquad.lval[0] & 0x7fffffff);
850 tquad.qval = sfp->sf_ffiles;
851 sbp->f_ffree = (tquad.lval[0] & 0x7fffffff);
852 } else if ((nmp->nm_flag & NFSMNT_NFSV4) == 0) {
853 sbp->f_bsize = (int32_t)sfp->sf_bsize;
854 sbp->f_blocks = (int32_t)sfp->sf_blocks;
855 sbp->f_bfree = (int32_t)sfp->sf_bfree;
856 sbp->f_bavail = (int32_t)sfp->sf_bavail;
863 * Use the fsinfo stuff to update the mount point.
866 nfscl_loadfsinfo(struct nfsmount *nmp, struct nfsfsinfo *fsp)
869 if ((nmp->nm_wsize == 0 || fsp->fs_wtpref < nmp->nm_wsize) &&
870 fsp->fs_wtpref >= NFS_FABLKSIZE)
871 nmp->nm_wsize = (fsp->fs_wtpref + NFS_FABLKSIZE - 1) &
872 ~(NFS_FABLKSIZE - 1);
873 if (fsp->fs_wtmax < nmp->nm_wsize && fsp->fs_wtmax > 0) {
874 nmp->nm_wsize = fsp->fs_wtmax & ~(NFS_FABLKSIZE - 1);
875 if (nmp->nm_wsize == 0)
876 nmp->nm_wsize = fsp->fs_wtmax;
878 if (nmp->nm_wsize < NFS_FABLKSIZE)
879 nmp->nm_wsize = NFS_FABLKSIZE;
880 if ((nmp->nm_rsize == 0 || fsp->fs_rtpref < nmp->nm_rsize) &&
881 fsp->fs_rtpref >= NFS_FABLKSIZE)
882 nmp->nm_rsize = (fsp->fs_rtpref + NFS_FABLKSIZE - 1) &
883 ~(NFS_FABLKSIZE - 1);
884 if (fsp->fs_rtmax < nmp->nm_rsize && fsp->fs_rtmax > 0) {
885 nmp->nm_rsize = fsp->fs_rtmax & ~(NFS_FABLKSIZE - 1);
886 if (nmp->nm_rsize == 0)
887 nmp->nm_rsize = fsp->fs_rtmax;
889 if (nmp->nm_rsize < NFS_FABLKSIZE)
890 nmp->nm_rsize = NFS_FABLKSIZE;
891 if ((nmp->nm_readdirsize == 0 || fsp->fs_dtpref < nmp->nm_readdirsize)
892 && fsp->fs_dtpref >= NFS_DIRBLKSIZ)
893 nmp->nm_readdirsize = (fsp->fs_dtpref + NFS_DIRBLKSIZ - 1) &
894 ~(NFS_DIRBLKSIZ - 1);
895 if (fsp->fs_rtmax < nmp->nm_readdirsize && fsp->fs_rtmax > 0) {
896 nmp->nm_readdirsize = fsp->fs_rtmax & ~(NFS_DIRBLKSIZ - 1);
897 if (nmp->nm_readdirsize == 0)
898 nmp->nm_readdirsize = fsp->fs_rtmax;
900 if (nmp->nm_readdirsize < NFS_DIRBLKSIZ)
901 nmp->nm_readdirsize = NFS_DIRBLKSIZ;
902 if (fsp->fs_maxfilesize > 0 &&
903 fsp->fs_maxfilesize < nmp->nm_maxfilesize)
904 nmp->nm_maxfilesize = fsp->fs_maxfilesize;
905 nmp->nm_mountp->mnt_stat.f_iosize = newnfs_iosize(nmp);
906 nmp->nm_state |= NFSSTA_GOTFSINFO;
910 * Get a pointer to my IP addrress and return it.
911 * Return NULL if you can't find one.
914 nfscl_getmyip(struct nfsmount *nmp, int *isinet6p)
916 struct sockaddr_in sad, *sin;
918 u_int8_t *retp = NULL;
919 static struct in_addr laddr;
923 * Loop up a route for the destination address.
925 if (nmp->nm_nam->sa_family == AF_INET) {
926 bzero(&sad, sizeof (sad));
927 sin = (struct sockaddr_in *)nmp->nm_nam;
928 sad.sin_family = AF_INET;
929 sad.sin_len = sizeof (struct sockaddr_in);
930 sad.sin_addr.s_addr = sin->sin_addr.s_addr;
931 rt = rtalloc1((struct sockaddr *)&sad, 0, 0UL);
933 if (rt->rt_ifp != NULL &&
934 rt->rt_ifa != NULL &&
935 ((rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) &&
936 rt->rt_ifa->ifa_addr->sa_family == AF_INET) {
937 sin = (struct sockaddr_in *)
938 rt->rt_ifa->ifa_addr;
939 laddr.s_addr = sin->sin_addr.s_addr;
940 retp = (u_int8_t *)&laddr;
945 } else if (nmp->nm_nam->sa_family == AF_INET6) {
946 struct sockaddr_in6 sad6, *sin6;
947 static struct in6_addr laddr6;
949 bzero(&sad6, sizeof (sad6));
950 sin6 = (struct sockaddr_in6 *)nmp->nm_nam;
951 sad6.sin6_family = AF_INET6;
952 sad6.sin6_len = sizeof (struct sockaddr_in6);
953 sad6.sin6_addr = sin6->sin6_addr;
954 rt = rtalloc1((struct sockaddr *)&sad6, 0, 0UL);
956 if (rt->rt_ifp != NULL &&
957 rt->rt_ifa != NULL &&
958 ((rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) &&
959 rt->rt_ifa->ifa_addr->sa_family == AF_INET6) {
960 sin6 = (struct sockaddr_in6 *)
961 rt->rt_ifa->ifa_addr;
962 laddr6 = sin6->sin6_addr;
963 retp = (u_int8_t *)&laddr6;
974 * Copy NFS uid, gids from the cred structure.
977 newnfs_copyincred(struct ucred *cr, struct nfscred *nfscr)
981 KASSERT(cr->cr_ngroups >= 0,
982 ("newnfs_copyincred: negative cr_ngroups"));
983 nfscr->nfsc_uid = cr->cr_uid;
984 nfscr->nfsc_ngroups = MIN(cr->cr_ngroups, NFS_MAXGRPS + 1);
985 for (i = 0; i < nfscr->nfsc_ngroups; i++)
986 nfscr->nfsc_groups[i] = cr->cr_groups[i];
991 * Do any client specific initialization.
996 static int inited = 0;
1002 ncl_pbuf_freecnt = nswbuf / 2 + 1;
1006 * Check each of the attributes to be set, to ensure they aren't already
1007 * the correct value. Disable setting ones already correct.
1010 nfscl_checksattr(struct vattr *vap, struct nfsvattr *nvap)
1013 if (vap->va_mode != (mode_t)VNOVAL) {
1014 if (vap->va_mode == nvap->na_mode)
1015 vap->va_mode = (mode_t)VNOVAL;
1017 if (vap->va_uid != (uid_t)VNOVAL) {
1018 if (vap->va_uid == nvap->na_uid)
1019 vap->va_uid = (uid_t)VNOVAL;
1021 if (vap->va_gid != (gid_t)VNOVAL) {
1022 if (vap->va_gid == nvap->na_gid)
1023 vap->va_gid = (gid_t)VNOVAL;
1025 if (vap->va_size != VNOVAL) {
1026 if (vap->va_size == nvap->na_size)
1027 vap->va_size = VNOVAL;
1031 * We are normally called with only a partially initialized
1032 * VAP. Since the NFSv3 spec says that server may use the
1033 * file attributes to store the verifier, the spec requires
1034 * us to do a SETATTR RPC. FreeBSD servers store the verifier
1035 * in atime, but we can't really assume that all servers will
1036 * so we ensure that our SETATTR sets both atime and mtime.
1038 if (vap->va_mtime.tv_sec == VNOVAL)
1039 vfs_timestamp(&vap->va_mtime);
1040 if (vap->va_atime.tv_sec == VNOVAL)
1041 vap->va_atime = vap->va_mtime;
1046 * Map nfsv4 errors to errno.h errors.
1047 * The uid and gid arguments are only used for NFSERR_BADOWNER and that
1048 * error should only be returned for the Open, Create and Setattr Ops.
1049 * As such, most calls can just pass in 0 for those arguments.
1052 nfscl_maperr(struct thread *td, int error, uid_t uid, gid_t gid)
1063 case NFSERR_BADOWNER:
1064 tprintf(p, LOG_INFO,
1065 "No name and/or group mapping for uid,gid:(%d,%d)\n",
1068 case NFSERR_STALECLIENTID:
1069 case NFSERR_STALESTATEID:
1070 case NFSERR_EXPIRED:
1071 case NFSERR_BADSTATEID:
1072 printf("nfsv4 recover err returned %d\n", error);
1074 case NFSERR_BADHANDLE:
1075 case NFSERR_SERVERFAULT:
1076 case NFSERR_BADTYPE:
1077 case NFSERR_FHEXPIRED:
1078 case NFSERR_RESOURCE:
1080 case NFSERR_NOFILEHANDLE:
1081 case NFSERR_MINORVERMISMATCH:
1082 case NFSERR_OLDSTATEID:
1083 case NFSERR_BADSEQID:
1084 case NFSERR_LEASEMOVED:
1085 case NFSERR_RECLAIMBAD:
1087 case NFSERR_BADCHAR:
1088 case NFSERR_BADNAME:
1089 case NFSERR_OPILLEGAL:
1090 printf("nfsv4 client/server protocol prob err=%d\n",
1094 tprintf(p, LOG_INFO, "nfsv4 err=%d\n", error);
1100 * Locate a process by number; return only "live" processes -- i.e., neither
1101 * zombies nor newly born but incompletely initialized processes. By not
1102 * returning processes in the PRS_NEW state, we allow callers to avoid
1103 * testing for that condition to avoid dereferencing p_ucred, et al.
1104 * Identical to pfind() in kern_proc.c, except it assume the list is
1107 static struct proc *
1108 pfind_locked(pid_t pid)
1112 LIST_FOREACH(p, PIDHASH(pid), p_hash)
1113 if (p->p_pid == pid) {
1114 if (p->p_state == PRS_NEW) {
1125 * Check to see if the process for this owner exists. Return 1 if it doesn't
1129 nfscl_procdoesntexist(u_int8_t *own)
1139 tl.cval[0] = *own++;
1140 tl.cval[1] = *own++;
1141 tl.cval[2] = *own++;
1142 tl.cval[3] = *own++;
1144 p = pfind_locked(pid);
1147 if (p->p_stats == NULL) {
1151 tl.cval[0] = *own++;
1152 tl.cval[1] = *own++;
1153 tl.cval[2] = *own++;
1154 tl.cval[3] = *own++;
1155 if (tl.lval != p->p_stats->p_start.tv_sec) {
1158 tl.cval[0] = *own++;
1159 tl.cval[1] = *own++;
1160 tl.cval[2] = *own++;
1162 if (tl.lval != p->p_stats->p_start.tv_usec)
1170 * - nfs pseudo system call for the client
1176 nfssvc_nfscl(struct thread *td, struct nfssvc_args *uap)
1179 struct nfscbd_args nfscbdarg;
1180 struct nfsd_nfscbd_args nfscbdarg2;
1183 if (uap->flag & NFSSVC_CBADDSOCK) {
1184 error = copyin(uap->argp, (caddr_t)&nfscbdarg, sizeof(nfscbdarg));
1187 if ((error = fget(td, nfscbdarg.sock, &fp)) != 0) {
1190 if (fp->f_type != DTYPE_SOCKET) {
1194 error = nfscbd_addsock(fp);
1196 if (!error && nfscl_enablecallb == 0) {
1197 nfsv4_cbport = nfscbdarg.port;
1198 nfscl_enablecallb = 1;
1200 } else if (uap->flag & NFSSVC_NFSCBD) {
1201 if (uap->argp == NULL)
1203 error = copyin(uap->argp, (caddr_t)&nfscbdarg2,
1204 sizeof(nfscbdarg2));
1207 error = nfscbd_nfsd(td, &nfscbdarg2);
1214 extern int (*nfsd_call_nfscl)(struct thread *, struct nfssvc_args *);
1217 * Called once to initialize data structures...
1220 nfscl_modevent(module_t mod, int type, void *data)
1223 static int loaded = 0;
1230 mtx_init(&nfs_clstate_mutex, "nfs_clstate_mutex", NULL,
1232 mtx_init(&ncl_iod_mutex, "ncl_iod_mutex", NULL, MTX_DEF);
1237 ncl_call_invalcaches = ncl_invalcaches;
1238 nfsd_call_nfscl = nfssvc_nfscl;
1243 if (nfs_numnfscbd != 0) {
1249 * XXX: Unloading of nfscl module is unsupported.
1252 ncl_call_invalcaches = NULL;
1253 nfsd_call_nfscl = NULL;
1254 /* and get rid of the mutexes */
1255 mtx_destroy(&nfs_clstate_mutex);
1256 mtx_destroy(&ncl_iod_mutex);
1268 static moduledata_t nfscl_mod = {
1273 DECLARE_MODULE(nfscl, nfscl_mod, SI_SUB_VFS, SI_ORDER_FIRST);
1275 /* So that loader and kldload(2) can find us, wherever we are.. */
1276 MODULE_VERSION(nfscl, 1);
1277 MODULE_DEPEND(nfscl, nfscommon, 1, 1, 1);