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
32 * @(#)nfs_subs.c 8.8 (Berkeley) 5/22/95
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 * These functions support the macros and help fiddle mbuf chains for
40 * the nfs op functions. They do things like create the rpc header and
41 * copy data between mbuf chains and uio lists.
44 #include "opt_kdtrace.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
52 #include <sys/mount.h>
53 #include <sys/vnode.h>
54 #include <sys/namei.h>
56 #include <sys/socket.h>
58 #include <sys/malloc.h>
59 #include <sys/sysent.h>
60 #include <sys/syscall.h>
61 #include <sys/sysproto.h>
64 #include <vm/vm_object.h>
65 #include <vm/vm_extern.h>
68 #include <nfs/nfsproto.h>
69 #include <nfsclient/nfs.h>
70 #include <nfsclient/nfsnode.h>
71 #include <nfsclient/nfs_kdtrace.h>
72 #include <nfs/xdr_subs.h>
73 #include <nfsclient/nfsm_subs.h>
74 #include <nfsclient/nfsmount.h>
76 #include <netinet/in.h>
79 * Note that stdarg.h and the ANSI style va_start macro is used for both
80 * ANSI and traditional C compilers.
82 #include <machine/stdarg.h>
85 dtrace_nfsclient_attrcache_flush_probe_func_t
86 dtrace_nfsclient_attrcache_flush_done_probe;
87 uint32_t nfsclient_attrcache_flush_done_id;
89 dtrace_nfsclient_attrcache_get_hit_probe_func_t
90 dtrace_nfsclient_attrcache_get_hit_probe;
91 uint32_t nfsclient_attrcache_get_hit_id;
93 dtrace_nfsclient_attrcache_get_miss_probe_func_t
94 dtrace_nfsclient_attrcache_get_miss_probe;
95 uint32_t nfsclient_attrcache_get_miss_id;
97 dtrace_nfsclient_attrcache_load_probe_func_t
98 dtrace_nfsclient_attrcache_load_done_probe;
99 uint32_t nfsclient_attrcache_load_done_id;
100 #endif /* !KDTRACE_HOOKS */
103 * Data items converted to xdr at startup, since they are constant
104 * This is kinda hokey, but may save a little time doing byte swaps
106 u_int32_t nfs_xdrneg1;
107 u_int32_t nfs_true, nfs_false;
109 /* And other global data */
110 static u_int32_t nfs_xid = 0;
111 static enum vtype nv2tov_type[8]= {
112 VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON, VNON
116 int nfs_pbuf_freecnt = -1; /* start out unlimited */
118 struct nfs_bufq nfs_bufq;
119 static struct mtx nfs_xid_mtx;
122 * and the reverse mapping from generic to Version 2 procedure numbers
124 int nfsv2_procid[NFS_NPROCS] = {
150 LIST_HEAD(nfsnodehashhead, nfsnode);
157 mtx_lock(&nfs_xid_mtx);
159 /* Get a pretty random xid to start with */
163 * Skip zero xid if it should ever happen.
168 mtx_unlock(&nfs_xid_mtx);
173 * Create the header for an rpc request packet
174 * The hsiz is the size of the rest of the nfs request header.
175 * (just used to decide if a cluster is a good idea)
178 nfsm_reqhead(struct vnode *vp, u_long procid, int hsiz)
182 MGET(mb, M_WAIT, MT_DATA);
183 if (hsiz >= MINCLSIZE)
190 * copies a uio scatter/gather list to an mbuf chain.
191 * NOTE: can ony handle iovcnt == 1
194 nfsm_uiotombuf(struct uio *uiop, struct mbuf **mq, int siz, caddr_t *bpos)
197 struct mbuf *mp, *mp2;
198 int xfer, left, mlen;
199 int uiosiz, clflg, rem;
203 if (uiop->uio_iovcnt != 1)
204 panic("nfsm_uiotombuf: iovcnt != 1");
207 if (siz > MLEN) /* or should it >= MCLBYTES ?? */
211 rem = nfsm_rndup(siz)-siz;
214 left = uiop->uio_iov->iov_len;
215 uiocp = uiop->uio_iov->iov_base;
220 mlen = M_TRAILINGSPACE(mp);
222 MGET(mp, M_WAIT, MT_DATA);
228 mlen = M_TRAILINGSPACE(mp);
230 xfer = (left > mlen) ? mlen : left;
233 if (uiop->uio_iov->iov_op != NULL)
234 (*(uiop->uio_iov->iov_op))
235 (uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
238 if (uiop->uio_segflg == UIO_SYSSPACE)
239 bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
241 copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
245 uiop->uio_offset += xfer;
246 uiop->uio_resid -= xfer;
248 uiop->uio_iov->iov_base =
249 (char *)uiop->uio_iov->iov_base + uiosiz;
250 uiop->uio_iov->iov_len -= uiosiz;
254 if (rem > M_TRAILINGSPACE(mp)) {
255 MGET(mp, M_WAIT, MT_DATA);
259 cp = mtod(mp, caddr_t)+mp->m_len;
260 for (left = 0; left < rem; left++)
265 *bpos = mtod(mp, caddr_t)+mp->m_len;
271 * Copy a string into mbufs for the hard cases...
274 nfsm_strtmbuf(struct mbuf **mb, char **bpos, const char *cp, long siz)
276 struct mbuf *m1 = NULL, *m2;
277 long left, xfer, len, tlen;
283 left = M_TRAILINGSPACE(m2);
285 tl = ((u_int32_t *)(*bpos));
286 *tl++ = txdr_unsigned(siz);
288 left -= NFSX_UNSIGNED;
289 m2->m_len += NFSX_UNSIGNED;
291 bcopy(cp, (caddr_t) tl, left);
298 /* Loop around adding mbufs */
300 MGET(m1, M_WAIT, MT_DATA);
303 m1->m_len = NFSMSIZ(m1);
306 tl = mtod(m1, u_int32_t *);
309 *tl++ = txdr_unsigned(siz);
310 m1->m_len -= NFSX_UNSIGNED;
311 tlen = NFSX_UNSIGNED;
314 if (siz < m1->m_len) {
315 len = nfsm_rndup(siz);
320 xfer = len = m1->m_len;
322 bcopy(cp, (caddr_t) tl, xfer);
323 m1->m_len = len+tlen;
328 *bpos = mtod(m1, caddr_t)+m1->m_len;
333 * Called once to initialize data structures...
336 nfs_init(struct vfsconf *vfsp)
340 nfsmount_zone = uma_zcreate("NFSMOUNT", sizeof(struct nfsmount),
341 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
342 nfs_true = txdr_unsigned(TRUE);
343 nfs_false = txdr_unsigned(FALSE);
344 nfs_xdrneg1 = txdr_unsigned(-1);
345 nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
348 /* Ensure async daemons disabled */
349 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
350 nfs_iodwant[i] = NFSIOD_NOT_AVAILABLE;
351 nfs_iodmount[i] = NULL;
353 nfs_nhinit(); /* Init the nfsnode table */
356 * Initialize reply list and start timer
358 mtx_init(&nfs_iod_mtx, "NFS iod lock", NULL, MTX_DEF);
359 mtx_init(&nfs_xid_mtx, "NFS xid lock", NULL, MTX_DEF);
361 nfs_pbuf_freecnt = nswbuf / 2 + 1;
367 nfs_uninit(struct vfsconf *vfsp)
372 * Tell all nfsiod processes to exit. Clear nfs_iodmax, and wakeup
373 * any sleeping nfsiods so they check nfs_iodmax and exit.
375 mtx_lock(&nfs_iod_mtx);
377 for (i = 0; i < nfs_numasync; i++)
378 if (nfs_iodwant[i] == NFSIOD_AVAILABLE)
379 wakeup(&nfs_iodwant[i]);
380 /* The last nfsiod to exit will wake us up when nfs_numasync hits 0 */
382 msleep(&nfs_numasync, &nfs_iod_mtx, PWAIT, "ioddie", 0);
383 mtx_unlock(&nfs_iod_mtx);
385 uma_zdestroy(nfsmount_zone);
390 nfs_dircookie_lock(struct nfsnode *np)
392 mtx_lock(&np->n_mtx);
393 while (np->n_flag & NDIRCOOKIELK)
394 (void) msleep(&np->n_flag, &np->n_mtx, PZERO, "nfsdirlk", 0);
395 np->n_flag |= NDIRCOOKIELK;
396 mtx_unlock(&np->n_mtx);
400 nfs_dircookie_unlock(struct nfsnode *np)
402 mtx_lock(&np->n_mtx);
403 np->n_flag &= ~NDIRCOOKIELK;
405 mtx_unlock(&np->n_mtx);
409 nfs_upgrade_vnlock(struct vnode *vp)
413 ASSERT_VOP_LOCKED(vp, "nfs_upgrade_vnlock");
414 old_lock = VOP_ISLOCKED(vp);
415 if (old_lock != LK_EXCLUSIVE) {
416 KASSERT(old_lock == LK_SHARED,
417 ("nfs_upgrade_vnlock: wrong old_lock %d", old_lock));
418 /* Upgrade to exclusive lock, this might block */
419 vn_lock(vp, LK_UPGRADE | LK_RETRY);
425 nfs_downgrade_vnlock(struct vnode *vp, int old_lock)
427 if (old_lock != LK_EXCLUSIVE) {
428 KASSERT(old_lock == LK_SHARED, ("wrong old_lock %d", old_lock));
429 /* Downgrade from exclusive lock. */
430 vn_lock(vp, LK_DOWNGRADE | LK_RETRY);
435 nfs_printf(const char *fmt, ...)
447 * Attribute cache routines.
448 * nfs_loadattrcache() - loads or updates the cache contents from attributes
449 * that are on the mbuf list
450 * nfs_getattrcache() - returns valid attributes if found in cache, returns
455 * Load the attribute cache (that lives in the nfsnode entry) with
456 * the values on the mbuf list and
458 * copy the attributes to *vaper
461 nfs_loadattrcache(struct vnode **vpp, struct mbuf **mdp, caddr_t *dposp,
462 struct vattr *vaper, int dontshrink)
464 struct vnode *vp = *vpp;
466 struct nfs_fattr *fp;
467 struct nfsnode *np = NULL;
474 struct timespec mtime, mtime_save;
475 int v3 = NFS_ISV3(vp);
476 struct thread *td = curthread;
480 t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
481 cp2 = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, M_WAIT);
486 fp = (struct nfs_fattr *)cp2;
488 vtyp = nfsv3tov_type(fp->fa_type);
489 vmode = fxdr_unsigned(u_short, fp->fa_mode);
490 rdev = makedev(fxdr_unsigned(int, fp->fa3_rdev.specdata1),
491 fxdr_unsigned(int, fp->fa3_rdev.specdata2));
492 fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
494 vtyp = nfsv2tov_type(fp->fa_type);
495 vmode = fxdr_unsigned(u_short, fp->fa_mode);
499 * The duplicate information returned in fa_type and fa_mode
500 * is an ambiguity in the NFS version 2 protocol.
502 * VREG should be taken literally as a regular file. If a
503 * server intents to return some type information differently
504 * in the upper bits of the mode field (e.g. for sockets, or
505 * FIFOs), NFSv2 mandates fa_type to be VNON. Anyway, we
506 * leave the examination of the mode bits even in the VREG
507 * case to avoid breakage for bogus servers, but we make sure
508 * that there are actually type bits set in the upper part of
509 * fa_mode (and failing that, trust the va_type field).
511 * NFSv3 cleared the issue, and requires fa_mode to not
512 * contain any type information (while also introduing sockets
513 * and FIFOs for fa_type).
515 if (vtyp == VNON || (vtyp == VREG && (vmode & S_IFMT) != 0))
516 vtyp = IFTOVT(vmode);
517 rdev = fxdr_unsigned(int32_t, fp->fa2_rdev);
518 fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
521 * Really ugly NFSv2 kludge.
523 if (vtyp == VCHR && rdev == 0xffffffff)
528 * If v_type == VNON it is a new node, so fill in the v_type,
529 * n_mtime fields. Check to see if it represents a special
530 * device, and if so, check for a possible alias. Once the
531 * correct vnode has been obtained, fill in the rest of the
535 mtx_lock(&np->n_mtx);
536 if (vp->v_type != vtyp) {
538 if (vp->v_type == VFIFO)
539 vp->v_op = &nfs_fifoops;
544 vap->va_mode = (vmode & 07777);
546 mtime_save = vap->va_mtime;
547 vap->va_mtime = mtime;
548 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
550 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
551 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
552 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
553 vap->va_size = fxdr_hyper(&fp->fa3_size);
554 vap->va_blocksize = NFS_FABLKSIZE;
555 vap->va_bytes = fxdr_hyper(&fp->fa3_used);
556 vap->va_fileid = fxdr_unsigned(int32_t,
557 fp->fa3_fileid.nfsuquad[1]);
558 fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
559 fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
563 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
564 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
565 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
566 vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
567 vap->va_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize);
568 vap->va_bytes = (u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks)
570 vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
571 fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
573 vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t,
574 fp->fa2_ctime.nfsv2_sec);
575 vap->va_ctime.tv_nsec = 0;
576 vap->va_gen = fxdr_unsigned(u_int32_t, fp->fa2_ctime.nfsv2_usec);
579 np->n_attrstamp = time_second;
580 /* Timestamp the NFS otw getattr fetch */
582 np->n_ac_ts_tid = td->td_tid;
583 np->n_ac_ts_pid = td->td_proc->p_pid;
584 np->n_ac_ts_syscalls = td->td_syscalls;
586 bzero(&np->n_ac_ts, sizeof(struct nfs_attrcache_timestamp));
588 if (vap->va_size != np->n_size) {
589 if (vap->va_type == VREG) {
590 if (dontshrink && vap->va_size < np->n_size) {
592 * We've been told not to shrink the file;
593 * zero np->n_attrstamp to indicate that
594 * the attributes are stale.
596 vap->va_size = np->n_size;
598 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
599 } else if (np->n_flag & NMODIFIED) {
601 * We've modified the file: Use the larger
602 * of our size, and the server's size.
604 if (vap->va_size < np->n_size) {
605 vap->va_size = np->n_size;
607 np->n_size = vap->va_size;
608 np->n_flag |= NSIZECHANGED;
611 np->n_size = vap->va_size;
612 np->n_flag |= NSIZECHANGED;
614 vnode_pager_setsize(vp, np->n_size);
616 np->n_size = vap->va_size;
620 * The following checks are added to prevent a race between (say)
621 * a READDIR+ and a WRITE.
622 * READDIR+, WRITE requests sent out.
623 * READDIR+ resp, WRITE resp received on client.
624 * However, the WRITE resp was handled before the READDIR+ resp
625 * causing the post op attrs from the write to be loaded first
626 * and the attrs from the READDIR+ to be loaded later. If this
627 * happens, we have stale attrs loaded into the attrcache.
628 * We detect this by for the mtime moving back. We invalidate the
629 * attrcache when this happens.
631 if (timespeccmp(&mtime_save, &vap->va_mtime, >)) {
632 /* Size changed or mtime went backwards */
634 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
637 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
638 if (np->n_flag & NCHG) {
639 if (np->n_flag & NACC)
640 vaper->va_atime = np->n_atim;
641 if (np->n_flag & NUPD)
642 vaper->va_mtime = np->n_mtim;
647 if (np->n_attrstamp != 0)
648 KDTRACE_NFS_ATTRCACHE_LOAD_DONE(vp, &np->n_vattr, 0);
650 mtx_unlock(&np->n_mtx);
654 KDTRACE_NFS_ATTRCACHE_LOAD_DONE(vp, NULL, error);
660 #include <sys/sysctl.h>
661 SYSCTL_DECL(_vfs_nfs);
662 static int nfs_acdebug;
663 SYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0,
664 "Toggle acdebug (access cache debug) flag");
668 * Check the time stamp
669 * If the cache is valid, copy contents to *vap and return 0
670 * otherwise return an error
673 nfs_getattrcache(struct vnode *vp, struct vattr *vaper)
677 struct nfsmount *nmp;
682 nmp = VFSTONFS(vp->v_mount);
684 mtx_lock(&Giant); /* nfs_printf() */
686 mtx_lock(&np->n_mtx);
687 /* XXX n_mtime doesn't seem to be updated on a miss-and-reload */
688 timeo = (time_second - np->n_mtime.tv_sec) / 10;
692 nfs_printf("nfs_getattrcache: initial timeo = %d\n", timeo);
695 if (vap->va_type == VDIR) {
696 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin)
697 timeo = nmp->nm_acdirmin;
698 else if (timeo > nmp->nm_acdirmax)
699 timeo = nmp->nm_acdirmax;
701 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin)
702 timeo = nmp->nm_acregmin;
703 else if (timeo > nmp->nm_acregmax)
704 timeo = nmp->nm_acregmax;
709 nfs_printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n",
710 nmp->nm_acregmin, nmp->nm_acregmax,
711 nmp->nm_acdirmin, nmp->nm_acdirmax);
714 nfs_printf("nfs_getattrcache: age = %d; final timeo = %d\n",
715 (time_second - np->n_attrstamp), timeo);
718 if ((time_second - np->n_attrstamp) >= timeo) {
719 nfsstats.attrcache_misses++;
720 mtx_unlock(&np->n_mtx);
721 KDTRACE_NFS_ATTRCACHE_GET_MISS(vp);
724 nfsstats.attrcache_hits++;
725 if (vap->va_size != np->n_size) {
726 if (vap->va_type == VREG) {
727 if (np->n_flag & NMODIFIED) {
728 if (vap->va_size < np->n_size)
729 vap->va_size = np->n_size;
731 np->n_size = vap->va_size;
733 np->n_size = vap->va_size;
735 vnode_pager_setsize(vp, np->n_size);
737 np->n_size = vap->va_size;
740 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
741 if (np->n_flag & NCHG) {
742 if (np->n_flag & NACC)
743 vaper->va_atime = np->n_atim;
744 if (np->n_flag & NUPD)
745 vaper->va_mtime = np->n_mtim;
747 mtx_unlock(&np->n_mtx);
749 mtx_unlock(&Giant); /* nfs_printf() */
751 KDTRACE_NFS_ATTRCACHE_GET_HIT(vp, vap);
756 * Purge all cached information about an NFS vnode including name
757 * cache entries, the attribute cache, and the access cache. This is
758 * called when an NFS request for a node fails with a stale
762 nfs_purgecache(struct vnode *vp)
769 mtx_lock(&np->n_mtx);
771 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
772 for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
773 np->n_accesscache[i].stamp = 0;
774 KDTRACE_NFS_ACCESSCACHE_FLUSH_DONE(vp);
775 mtx_unlock(&np->n_mtx);
778 static nfsuint64 nfs_nullcookie = { { 0, 0 } };
780 * This function finds the directory cookie that corresponds to the
781 * logical byte offset given.
784 nfs_getcookie(struct nfsnode *np, off_t off, int add)
786 struct nfsdmap *dp, *dp2;
788 nfsuint64 *retval = NULL;
790 pos = (uoff_t)off / NFS_DIRBLKSIZ;
791 if (pos == 0 || off < 0) {
794 panic("nfs getcookie add at <= 0");
796 return (&nfs_nullcookie);
799 dp = LIST_FIRST(&np->n_cookies);
802 dp = malloc(sizeof (struct nfsdmap),
803 M_NFSDIROFF, M_WAITOK);
804 dp->ndm_eocookie = 0;
805 LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
809 while (pos >= NFSNUMCOOKIES) {
810 pos -= NFSNUMCOOKIES;
811 if (LIST_NEXT(dp, ndm_list)) {
812 if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
813 pos >= dp->ndm_eocookie)
815 dp = LIST_NEXT(dp, ndm_list);
817 dp2 = malloc(sizeof (struct nfsdmap),
818 M_NFSDIROFF, M_WAITOK);
819 dp2->ndm_eocookie = 0;
820 LIST_INSERT_AFTER(dp, dp2, ndm_list);
825 if (pos >= dp->ndm_eocookie) {
827 dp->ndm_eocookie = pos + 1;
831 retval = &dp->ndm_cookies[pos];
837 * Invalidate cached directory information, except for the actual directory
838 * blocks (which are invalidated separately).
839 * Done mainly to avoid the use of stale offset cookies.
842 nfs_invaldir(struct vnode *vp)
844 struct nfsnode *np = VTONFS(vp);
847 if (vp->v_type != VDIR)
848 panic("nfs: invaldir not dir");
850 nfs_dircookie_lock(np);
851 np->n_direofoffset = 0;
852 np->n_cookieverf.nfsuquad[0] = 0;
853 np->n_cookieverf.nfsuquad[1] = 0;
854 if (LIST_FIRST(&np->n_cookies))
855 LIST_FIRST(&np->n_cookies)->ndm_eocookie = 0;
856 nfs_dircookie_unlock(np);
860 * The write verifier has changed (probably due to a server reboot), so all
861 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
862 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
863 * and B_CLUSTEROK flags. Once done the new write verifier can be set for the
866 * B_CLUSTEROK must be cleared along with B_NEEDCOMMIT because stage 1 data
867 * writes are not clusterable.
870 nfs_clearcommit(struct mount *mp)
872 struct vnode *vp, *nvp;
873 struct buf *bp, *nbp;
877 MNT_VNODE_FOREACH(vp, mp, nvp) {
880 if (vp->v_iflag & VI_DOOMED) {
888 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
889 if (!BUF_ISLOCKED(bp) &&
890 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
891 == (B_DELWRI | B_NEEDCOMMIT))
892 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
902 * Helper functions for former macros. Some of these should be
903 * moved to their callers.
907 nfsm_mtofh_xx(struct vnode *d, struct vnode **v, int v3, int *f,
908 struct mbuf **md, caddr_t *dpos)
910 struct nfsnode *ttnp;
918 tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
921 *f = fxdr_unsigned(int, *tl);
925 t1 = nfsm_getfh_xx(&ttfhp, &ttfhsize, (v3), md, dpos);
928 t1 = nfs_nget(d->v_mount, ttfhp, ttfhsize, &ttnp, LK_EXCLUSIVE);
934 tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
938 *f = fxdr_unsigned(int, *tl);
939 else if (fxdr_unsigned(int, *tl))
940 nfsm_adv_xx(NFSX_V3FATTR, md, dpos);
944 t1 = nfs_loadattrcache(&ttvp, md, dpos, NULL, 0);
953 nfsm_getfh_xx(nfsfh_t **f, int *s, int v3, struct mbuf **md, caddr_t *dpos)
958 tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
961 *s = fxdr_unsigned(int, *tl);
962 if (*s <= 0 || *s > NFSX_V3FHMAX)
966 *f = nfsm_dissect_xx(nfsm_rndup(*s), md, dpos);
975 nfsm_loadattr_xx(struct vnode **v, struct vattr *va, struct mbuf **md,
980 struct vnode *ttvp = *v;
981 t1 = nfs_loadattrcache(&ttvp, md, dpos, va, 0);
989 nfsm_postop_attr_xx(struct vnode **v, int *f, struct mbuf **md,
995 struct vnode *ttvp = *v;
996 tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
999 *f = fxdr_unsigned(int, *tl);
1001 t1 = nfs_loadattrcache(&ttvp, md, dpos, NULL, 1);
1012 nfsm_wcc_data_xx(struct vnode **v, int *f, struct mbuf **md, caddr_t *dpos)
1015 int ttattrf, ttretf = 0;
1018 tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
1021 if (*tl == nfs_true) {
1022 tl = nfsm_dissect_xx(6 * NFSX_UNSIGNED, md, dpos);
1025 mtx_lock(&(VTONFS(*v))->n_mtx);
1027 ttretf = (VTONFS(*v)->n_mtime.tv_sec == fxdr_unsigned(u_int32_t, *(tl + 2)) &&
1028 VTONFS(*v)->n_mtime.tv_nsec == fxdr_unsigned(u_int32_t, *(tl + 3)));
1029 mtx_unlock(&(VTONFS(*v))->n_mtx);
1031 t1 = nfsm_postop_attr_xx(v, &ttattrf, md, dpos);
1042 nfsm_strtom_xx(const char *a, int s, int m, struct mbuf **mb, caddr_t *bpos)
1048 return ENAMETOOLONG;
1049 t1 = nfsm_rndup(s) + NFSX_UNSIGNED;
1050 if (t1 <= M_TRAILINGSPACE(*mb)) {
1051 tl = nfsm_build_xx(t1, mb, bpos);
1052 *tl++ = txdr_unsigned(s);
1053 *(tl + ((t1 >> 2) - 2)) = 0;
1056 t1 = nfsm_strtmbuf(mb, bpos, a, s);
1064 nfsm_fhtom_xx(struct vnode *v, int v3, struct mbuf **mb, caddr_t *bpos)
1071 t1 = nfsm_rndup(VTONFS(v)->n_fhsize) + NFSX_UNSIGNED;
1072 if (t1 < M_TRAILINGSPACE(*mb)) {
1073 tl = nfsm_build_xx(t1, mb, bpos);
1074 *tl++ = txdr_unsigned(VTONFS(v)->n_fhsize);
1075 *(tl + ((t1 >> 2) - 2)) = 0;
1076 bcopy(VTONFS(v)->n_fhp, tl, VTONFS(v)->n_fhsize);
1078 t1 = nfsm_strtmbuf(mb, bpos,
1079 (const char *)VTONFS(v)->n_fhp,
1080 VTONFS(v)->n_fhsize);
1085 cp = nfsm_build_xx(NFSX_V2FH, mb, bpos);
1086 bcopy(VTONFS(v)->n_fhp, cp, NFSX_V2FH);
1092 nfsm_v3attrbuild_xx(struct vattr *va, int full, struct mbuf **mb,
1097 if (va->va_mode != (mode_t)VNOVAL) {
1098 tl = nfsm_build_xx(2 * NFSX_UNSIGNED, mb, bpos);
1100 *tl = txdr_unsigned(va->va_mode);
1102 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1105 if (full && va->va_uid != (uid_t)VNOVAL) {
1106 tl = nfsm_build_xx(2 * NFSX_UNSIGNED, mb, bpos);
1108 *tl = txdr_unsigned(va->va_uid);
1110 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1113 if (full && va->va_gid != (gid_t)VNOVAL) {
1114 tl = nfsm_build_xx(2 * NFSX_UNSIGNED, mb, bpos);
1116 *tl = txdr_unsigned(va->va_gid);
1118 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1121 if (full && va->va_size != VNOVAL) {
1122 tl = nfsm_build_xx(3 * NFSX_UNSIGNED, mb, bpos);
1124 txdr_hyper(va->va_size, tl);
1126 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1129 if (va->va_atime.tv_sec != VNOVAL) {
1130 if (va->va_atime.tv_sec != time_second) {
1131 tl = nfsm_build_xx(3 * NFSX_UNSIGNED, mb, bpos);
1132 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
1133 txdr_nfsv3time(&va->va_atime, tl);
1135 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1136 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
1139 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1140 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
1142 if (va->va_mtime.tv_sec != VNOVAL) {
1143 if (va->va_mtime.tv_sec != time_second) {
1144 tl = nfsm_build_xx(3 * NFSX_UNSIGNED, mb, bpos);
1145 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
1146 txdr_nfsv3time(&va->va_mtime, tl);
1148 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1149 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
1152 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1153 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);