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/rwlock.h>
60 #include <sys/sysent.h>
61 #include <sys/syscall.h>
62 #include <sys/sysproto.h>
63 #include <sys/taskqueue.h>
66 #include <vm/vm_object.h>
67 #include <vm/vm_extern.h>
70 #include <nfs/nfsproto.h>
71 #include <nfsclient/nfs.h>
72 #include <nfsclient/nfsnode.h>
73 #include <nfs/nfs_kdtrace.h>
74 #include <nfs/xdr_subs.h>
75 #include <nfsclient/nfsm_subs.h>
76 #include <nfsclient/nfsmount.h>
78 #include <netinet/in.h>
81 * Note that stdarg.h and the ANSI style va_start macro is used for both
82 * ANSI and traditional C compilers.
84 #include <machine/stdarg.h>
87 dtrace_nfsclient_attrcache_flush_probe_func_t
88 dtrace_nfsclient_attrcache_flush_done_probe;
89 uint32_t nfsclient_attrcache_flush_done_id;
91 dtrace_nfsclient_attrcache_get_hit_probe_func_t
92 dtrace_nfsclient_attrcache_get_hit_probe;
93 uint32_t nfsclient_attrcache_get_hit_id;
95 dtrace_nfsclient_attrcache_get_miss_probe_func_t
96 dtrace_nfsclient_attrcache_get_miss_probe;
97 uint32_t nfsclient_attrcache_get_miss_id;
99 dtrace_nfsclient_attrcache_load_probe_func_t
100 dtrace_nfsclient_attrcache_load_done_probe;
101 uint32_t nfsclient_attrcache_load_done_id;
102 #endif /* !KDTRACE_HOOKS */
105 * Data items converted to xdr at startup, since they are constant
106 * This is kinda hokey, but may save a little time doing byte swaps
108 u_int32_t nfs_xdrneg1;
109 u_int32_t nfs_true, nfs_false;
111 /* And other global data */
112 static u_int32_t nfs_xid = 0;
113 static enum vtype nv2tov_type[8]= {
114 VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON, VNON
118 int nfs_pbuf_freecnt = -1; /* start out unlimited */
120 struct nfs_bufq nfs_bufq;
121 static struct mtx nfs_xid_mtx;
122 struct task nfs_nfsiodnew_task;
125 * and the reverse mapping from generic to Version 2 procedure numbers
127 int nfsv2_procid[NFS_NPROCS] = {
153 LIST_HEAD(nfsnodehashhead, nfsnode);
160 mtx_lock(&nfs_xid_mtx);
162 /* Get a pretty random xid to start with */
166 * Skip zero xid if it should ever happen.
171 mtx_unlock(&nfs_xid_mtx);
176 * copies a uio scatter/gather list to an mbuf chain.
177 * NOTE: can ony handle iovcnt == 1
180 nfsm_uiotombuf(struct uio *uiop, struct mbuf **mq, int siz, caddr_t *bpos)
183 struct mbuf *mp, *mp2;
184 int xfer, left, mlen;
185 int uiosiz, clflg, rem;
188 KASSERT(uiop->uio_iovcnt == 1, ("nfsm_uiotombuf: iovcnt != 1"));
190 if (siz > MLEN) /* or should it >= MCLBYTES ?? */
194 rem = nfsm_rndup(siz)-siz;
197 left = uiop->uio_iov->iov_len;
198 uiocp = uiop->uio_iov->iov_base;
203 mlen = M_TRAILINGSPACE(mp);
206 mp = m_getcl(M_WAITOK, MT_DATA, 0);
208 mp = m_get(M_WAITOK, MT_DATA);
211 mlen = M_TRAILINGSPACE(mp);
213 xfer = (left > mlen) ? mlen : left;
216 if (uiop->uio_iov->iov_op != NULL)
217 (*(uiop->uio_iov->iov_op))
218 (uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
221 if (uiop->uio_segflg == UIO_SYSSPACE)
222 bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
224 copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
228 uiop->uio_offset += xfer;
229 uiop->uio_resid -= xfer;
231 uiop->uio_iov->iov_base =
232 (char *)uiop->uio_iov->iov_base + uiosiz;
233 uiop->uio_iov->iov_len -= uiosiz;
237 if (rem > M_TRAILINGSPACE(mp)) {
238 mp = m_get(M_WAITOK, MT_DATA);
241 cp = mtod(mp, caddr_t)+mp->m_len;
242 for (left = 0; left < rem; left++)
247 *bpos = mtod(mp, caddr_t)+mp->m_len;
253 * Copy a string into mbufs for the hard cases...
256 nfsm_strtmbuf(struct mbuf **mb, char **bpos, const char *cp, long siz)
258 struct mbuf *m1 = NULL, *m2;
259 long left, xfer, len, tlen;
265 left = M_TRAILINGSPACE(m2);
267 tl = ((u_int32_t *)(*bpos));
268 *tl++ = txdr_unsigned(siz);
270 left -= NFSX_UNSIGNED;
271 m2->m_len += NFSX_UNSIGNED;
273 bcopy(cp, (caddr_t) tl, left);
280 /* Loop around adding mbufs */
283 m1 = m_getcl(M_WAITOK, MT_DATA, 0);
284 m1->m_len = MCLBYTES;
286 m1 = m_get(M_WAITOK, MT_DATA);
291 tl = mtod(m1, u_int32_t *);
294 *tl++ = txdr_unsigned(siz);
295 m1->m_len -= NFSX_UNSIGNED;
296 tlen = NFSX_UNSIGNED;
299 if (siz < m1->m_len) {
300 len = nfsm_rndup(siz);
305 xfer = len = m1->m_len;
307 bcopy(cp, (caddr_t) tl, xfer);
308 m1->m_len = len+tlen;
313 *bpos = mtod(m1, caddr_t)+m1->m_len;
318 * Called once to initialize data structures...
321 nfs_init(struct vfsconf *vfsp)
325 nfsmount_zone = uma_zcreate("NFSMOUNT", sizeof(struct nfsmount),
326 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
327 nfs_true = txdr_unsigned(TRUE);
328 nfs_false = txdr_unsigned(FALSE);
329 nfs_xdrneg1 = txdr_unsigned(-1);
330 nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
333 /* Ensure async daemons disabled */
334 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
335 nfs_iodwant[i] = NFSIOD_NOT_AVAILABLE;
336 nfs_iodmount[i] = NULL;
338 nfs_nhinit(); /* Init the nfsnode table */
341 * Initialize reply list and start timer
343 mtx_init(&nfs_iod_mtx, "NFS iod lock", NULL, MTX_DEF);
344 mtx_init(&nfs_xid_mtx, "NFS xid lock", NULL, MTX_DEF);
345 TASK_INIT(&nfs_nfsiodnew_task, 0, nfs_nfsiodnew_tq, NULL);
347 nfs_pbuf_freecnt = nswbuf / 2 + 1;
353 nfs_uninit(struct vfsconf *vfsp)
358 * Tell all nfsiod processes to exit. Clear nfs_iodmax, and wakeup
359 * any sleeping nfsiods so they check nfs_iodmax and exit.
360 * Drain nfsiodnew task before we wait for them to finish.
362 mtx_lock(&nfs_iod_mtx);
364 mtx_unlock(&nfs_iod_mtx);
365 taskqueue_drain(taskqueue_thread, &nfs_nfsiodnew_task);
366 mtx_lock(&nfs_iod_mtx);
367 for (i = 0; i < nfs_numasync; i++)
368 if (nfs_iodwant[i] == NFSIOD_AVAILABLE)
369 wakeup(&nfs_iodwant[i]);
370 /* The last nfsiod to exit will wake us up when nfs_numasync hits 0 */
372 msleep(&nfs_numasync, &nfs_iod_mtx, PWAIT, "ioddie", 0);
373 mtx_unlock(&nfs_iod_mtx);
375 uma_zdestroy(nfsmount_zone);
380 nfs_dircookie_lock(struct nfsnode *np)
382 mtx_lock(&np->n_mtx);
383 while (np->n_flag & NDIRCOOKIELK)
384 (void) msleep(&np->n_flag, &np->n_mtx, PZERO, "nfsdirlk", 0);
385 np->n_flag |= NDIRCOOKIELK;
386 mtx_unlock(&np->n_mtx);
390 nfs_dircookie_unlock(struct nfsnode *np)
392 mtx_lock(&np->n_mtx);
393 np->n_flag &= ~NDIRCOOKIELK;
395 mtx_unlock(&np->n_mtx);
399 nfs_upgrade_vnlock(struct vnode *vp)
403 ASSERT_VOP_LOCKED(vp, "nfs_upgrade_vnlock");
404 old_lock = VOP_ISLOCKED(vp);
405 if (old_lock != LK_EXCLUSIVE) {
406 KASSERT(old_lock == LK_SHARED,
407 ("nfs_upgrade_vnlock: wrong old_lock %d", old_lock));
408 /* Upgrade to exclusive lock, this might block */
409 vn_lock(vp, LK_UPGRADE | LK_RETRY);
415 nfs_downgrade_vnlock(struct vnode *vp, int old_lock)
417 if (old_lock != LK_EXCLUSIVE) {
418 KASSERT(old_lock == LK_SHARED, ("wrong old_lock %d", old_lock));
419 /* Downgrade from exclusive lock. */
420 vn_lock(vp, LK_DOWNGRADE | LK_RETRY);
425 nfs_printf(const char *fmt, ...)
437 * Attribute cache routines.
438 * nfs_loadattrcache() - loads or updates the cache contents from attributes
439 * that are on the mbuf list
440 * nfs_getattrcache() - returns valid attributes if found in cache, returns
445 * Load the attribute cache (that lives in the nfsnode entry) with
446 * the values on the mbuf list and
448 * copy the attributes to *vaper
451 nfs_loadattrcache(struct vnode **vpp, struct mbuf **mdp, caddr_t *dposp,
452 struct vattr *vaper, int dontshrink)
454 struct vnode *vp = *vpp;
456 struct nfs_fattr *fp;
457 struct nfsnode *np = NULL;
464 struct timespec mtime, mtime_save;
465 int v3 = NFS_ISV3(vp);
471 t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
472 cp2 = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, M_WAITOK);
477 fp = (struct nfs_fattr *)cp2;
479 vtyp = nfsv3tov_type(fp->fa_type);
480 vmode = fxdr_unsigned(u_short, fp->fa_mode);
481 rdev = makedev(fxdr_unsigned(int, fp->fa3_rdev.specdata1),
482 fxdr_unsigned(int, fp->fa3_rdev.specdata2));
483 fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
485 vtyp = nfsv2tov_type(fp->fa_type);
486 vmode = fxdr_unsigned(u_short, fp->fa_mode);
490 * The duplicate information returned in fa_type and fa_mode
491 * is an ambiguity in the NFS version 2 protocol.
493 * VREG should be taken literally as a regular file. If a
494 * server intents to return some type information differently
495 * in the upper bits of the mode field (e.g. for sockets, or
496 * FIFOs), NFSv2 mandates fa_type to be VNON. Anyway, we
497 * leave the examination of the mode bits even in the VREG
498 * case to avoid breakage for bogus servers, but we make sure
499 * that there are actually type bits set in the upper part of
500 * fa_mode (and failing that, trust the va_type field).
502 * NFSv3 cleared the issue, and requires fa_mode to not
503 * contain any type information (while also introduing sockets
504 * and FIFOs for fa_type).
506 if (vtyp == VNON || (vtyp == VREG && (vmode & S_IFMT) != 0))
507 vtyp = IFTOVT(vmode);
508 rdev = fxdr_unsigned(int32_t, fp->fa2_rdev);
509 fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
512 * Really ugly NFSv2 kludge.
514 if (vtyp == VCHR && rdev == 0xffffffff)
519 * If v_type == VNON it is a new node, so fill in the v_type,
520 * n_mtime fields. Check to see if it represents a special
521 * device, and if so, check for a possible alias. Once the
522 * correct vnode has been obtained, fill in the rest of the
526 mtx_lock(&np->n_mtx);
527 if (vp->v_type != vtyp) {
529 if (vp->v_type == VFIFO)
530 vp->v_op = &nfs_fifoops;
535 vap->va_mode = (vmode & 07777);
537 mtime_save = vap->va_mtime;
538 vap->va_mtime = mtime;
539 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
541 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
542 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
543 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
544 vap->va_size = fxdr_hyper(&fp->fa3_size);
545 vap->va_blocksize = NFS_FABLKSIZE;
546 vap->va_bytes = fxdr_hyper(&fp->fa3_used);
547 vap->va_fileid = fxdr_unsigned(int32_t,
548 fp->fa3_fileid.nfsuquad[1]);
549 fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
550 fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
554 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
555 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
556 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
557 vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
558 vap->va_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize);
559 vap->va_bytes = (u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks)
561 vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
562 fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
564 vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t,
565 fp->fa2_ctime.nfsv2_sec);
566 vap->va_ctime.tv_nsec = 0;
567 vap->va_gen = fxdr_unsigned(u_int32_t, fp->fa2_ctime.nfsv2_usec);
570 np->n_attrstamp = time_second;
573 if (vap->va_size != np->n_size) {
574 if (vap->va_type == VREG) {
575 if (dontshrink && vap->va_size < np->n_size) {
577 * We've been told not to shrink the file;
578 * zero np->n_attrstamp to indicate that
579 * the attributes are stale.
581 vap->va_size = np->n_size;
583 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
584 vnode_pager_setsize(vp, np->n_size);
585 } else if (np->n_flag & NMODIFIED) {
587 * We've modified the file: Use the larger
588 * of our size, and the server's size.
590 if (vap->va_size < np->n_size) {
591 vap->va_size = np->n_size;
593 np->n_size = vap->va_size;
594 np->n_flag |= NSIZECHANGED;
596 vnode_pager_setsize(vp, np->n_size);
597 } else if (vap->va_size < np->n_size) {
599 * When shrinking the size, the call to
600 * vnode_pager_setsize() cannot be done
601 * with the mutex held, so delay it until
602 * after the mtx_unlock call.
604 nsize = np->n_size = vap->va_size;
605 np->n_flag |= NSIZECHANGED;
608 np->n_size = vap->va_size;
609 np->n_flag |= NSIZECHANGED;
610 vnode_pager_setsize(vp, np->n_size);
613 np->n_size = vap->va_size;
617 * The following checks are added to prevent a race between (say)
618 * a READDIR+ and a WRITE.
619 * READDIR+, WRITE requests sent out.
620 * READDIR+ resp, WRITE resp received on client.
621 * However, the WRITE resp was handled before the READDIR+ resp
622 * causing the post op attrs from the write to be loaded first
623 * and the attrs from the READDIR+ to be loaded later. If this
624 * happens, we have stale attrs loaded into the attrcache.
625 * We detect this by for the mtime moving back. We invalidate the
626 * attrcache when this happens.
628 if (timespeccmp(&mtime_save, &vap->va_mtime, >)) {
629 /* Size changed or mtime went backwards */
631 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
634 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
635 if (np->n_flag & NCHG) {
636 if (np->n_flag & NACC)
637 vaper->va_atime = np->n_atim;
638 if (np->n_flag & NUPD)
639 vaper->va_mtime = np->n_mtim;
644 if (np->n_attrstamp != 0)
645 KDTRACE_NFS_ATTRCACHE_LOAD_DONE(vp, &np->n_vattr, 0);
647 mtx_unlock(&np->n_mtx);
649 vnode_pager_setsize(vp, nsize);
653 KDTRACE_NFS_ATTRCACHE_LOAD_DONE(vp, NULL, error);
659 #include <sys/sysctl.h>
660 SYSCTL_DECL(_vfs_oldnfs);
661 static int nfs_acdebug;
662 SYSCTL_INT(_vfs_oldnfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0,
663 "Toggle acdebug (attribute cache debug) flag");
667 * Check the time stamp
668 * If the cache is valid, copy contents to *vap and return 0
669 * otherwise return an error
672 nfs_getattrcache(struct vnode *vp, struct vattr *vaper)
676 struct nfsmount *nmp;
681 nmp = VFSTONFS(vp->v_mount);
683 mtx_lock(&Giant); /* nfs_printf() */
685 mtx_lock(&np->n_mtx);
686 /* XXX n_mtime doesn't seem to be updated on a miss-and-reload */
687 timeo = (time_second - np->n_mtime.tv_sec) / 10;
691 nfs_printf("nfs_getattrcache: initial timeo = %d\n", timeo);
694 if (vap->va_type == VDIR) {
695 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin)
696 timeo = nmp->nm_acdirmin;
697 else if (timeo > nmp->nm_acdirmax)
698 timeo = nmp->nm_acdirmax;
700 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin)
701 timeo = nmp->nm_acregmin;
702 else if (timeo > nmp->nm_acregmax)
703 timeo = nmp->nm_acregmax;
708 nfs_printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n",
709 nmp->nm_acregmin, nmp->nm_acregmax,
710 nmp->nm_acdirmin, nmp->nm_acdirmax);
713 nfs_printf("nfs_getattrcache: age = %d; final timeo = %d\n",
714 (time_second - np->n_attrstamp), timeo);
717 if ((time_second - np->n_attrstamp) >= timeo) {
718 nfsstats.attrcache_misses++;
719 mtx_unlock(&np->n_mtx);
721 mtx_unlock(&Giant); /* nfs_printf() */
723 KDTRACE_NFS_ATTRCACHE_GET_MISS(vp);
726 nfsstats.attrcache_hits++;
727 if (vap->va_size != np->n_size) {
728 if (vap->va_type == VREG) {
729 if (np->n_flag & NMODIFIED) {
730 if (vap->va_size < np->n_size)
731 vap->va_size = np->n_size;
733 np->n_size = vap->va_size;
735 np->n_size = vap->va_size;
737 vnode_pager_setsize(vp, np->n_size);
739 np->n_size = vap->va_size;
742 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
743 if (np->n_flag & NCHG) {
744 if (np->n_flag & NACC)
745 vaper->va_atime = np->n_atim;
746 if (np->n_flag & NUPD)
747 vaper->va_mtime = np->n_mtim;
749 mtx_unlock(&np->n_mtx);
751 mtx_unlock(&Giant); /* nfs_printf() */
753 KDTRACE_NFS_ATTRCACHE_GET_HIT(vp, vap);
758 * Purge all cached information about an NFS vnode including name
759 * cache entries, the attribute cache, and the access cache. This is
760 * called when an NFS request for a node fails with a stale
764 nfs_purgecache(struct vnode *vp)
771 mtx_lock(&np->n_mtx);
773 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
774 for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
775 np->n_accesscache[i].stamp = 0;
776 KDTRACE_NFS_ACCESSCACHE_FLUSH_DONE(vp);
777 mtx_unlock(&np->n_mtx);
780 static nfsuint64 nfs_nullcookie = { { 0, 0 } };
782 * This function finds the directory cookie that corresponds to the
783 * logical byte offset given.
786 nfs_getcookie(struct nfsnode *np, off_t off, int add)
788 struct nfsdmap *dp, *dp2;
790 nfsuint64 *retval = NULL;
792 pos = (uoff_t)off / NFS_DIRBLKSIZ;
793 if (pos == 0 || off < 0) {
794 KASSERT(!add, ("nfs getcookie add at <= 0"));
795 return (&nfs_nullcookie);
798 dp = LIST_FIRST(&np->n_cookies);
801 dp = malloc(sizeof (struct nfsdmap),
802 M_NFSDIROFF, M_WAITOK);
803 dp->ndm_eocookie = 0;
804 LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
808 while (pos >= NFSNUMCOOKIES) {
809 pos -= NFSNUMCOOKIES;
810 if (LIST_NEXT(dp, ndm_list)) {
811 if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
812 pos >= dp->ndm_eocookie)
814 dp = LIST_NEXT(dp, ndm_list);
816 dp2 = malloc(sizeof (struct nfsdmap),
817 M_NFSDIROFF, M_WAITOK);
818 dp2->ndm_eocookie = 0;
819 LIST_INSERT_AFTER(dp, dp2, ndm_list);
824 if (pos >= dp->ndm_eocookie) {
826 dp->ndm_eocookie = pos + 1;
830 retval = &dp->ndm_cookies[pos];
836 * Invalidate cached directory information, except for the actual directory
837 * blocks (which are invalidated separately).
838 * Done mainly to avoid the use of stale offset cookies.
841 nfs_invaldir(struct vnode *vp)
843 struct nfsnode *np = VTONFS(vp);
845 KASSERT(vp->v_type == VDIR, ("nfs: invaldir not dir"));
846 nfs_dircookie_lock(np);
847 np->n_direofoffset = 0;
848 np->n_cookieverf.nfsuquad[0] = 0;
849 np->n_cookieverf.nfsuquad[1] = 0;
850 if (LIST_FIRST(&np->n_cookies))
851 LIST_FIRST(&np->n_cookies)->ndm_eocookie = 0;
852 nfs_dircookie_unlock(np);
856 * The write verifier has changed (probably due to a server reboot), so all
857 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
858 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
859 * and B_CLUSTEROK flags. Once done the new write verifier can be set for the
862 * B_CLUSTEROK must be cleared along with B_NEEDCOMMIT because stage 1 data
863 * writes are not clusterable.
866 nfs_clearcommit(struct mount *mp)
868 struct vnode *vp, *nvp;
869 struct buf *bp, *nbp;
872 MNT_VNODE_FOREACH_ALL(vp, mp, nvp) {
877 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
878 if (!BUF_ISLOCKED(bp) &&
879 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
880 == (B_DELWRI | B_NEEDCOMMIT))
881 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
889 * Helper functions for former macros. Some of these should be
890 * moved to their callers.
894 nfsm_mtofh_xx(struct vnode *d, struct vnode **v, int v3, int *f,
895 struct mbuf **md, caddr_t *dpos)
897 struct nfsnode *ttnp;
905 tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
908 *f = fxdr_unsigned(int, *tl);
912 t1 = nfsm_getfh_xx(&ttfhp, &ttfhsize, (v3), md, dpos);
915 t1 = nfs_nget(d->v_mount, ttfhp, ttfhsize, &ttnp, LK_EXCLUSIVE);
921 tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
925 *f = fxdr_unsigned(int, *tl);
926 else if (fxdr_unsigned(int, *tl))
927 nfsm_adv_xx(NFSX_V3FATTR, md, dpos);
931 t1 = nfs_loadattrcache(&ttvp, md, dpos, NULL, 0);
940 nfsm_getfh_xx(nfsfh_t **f, int *s, int v3, struct mbuf **md, caddr_t *dpos)
945 tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
948 *s = fxdr_unsigned(int, *tl);
949 if (*s <= 0 || *s > NFSX_V3FHMAX)
953 *f = nfsm_dissect_xx(nfsm_rndup(*s), md, dpos);
962 nfsm_loadattr_xx(struct vnode **v, struct vattr *va, struct mbuf **md,
967 struct vnode *ttvp = *v;
968 t1 = nfs_loadattrcache(&ttvp, md, dpos, va, 0);
976 nfsm_postop_attr_xx(struct vnode **v, int *f, struct vattr *va,
977 struct mbuf **md, caddr_t *dpos)
982 struct vnode *ttvp = *v;
983 tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
986 *f = fxdr_unsigned(int, *tl);
988 t1 = nfs_loadattrcache(&ttvp, md, dpos, va, 1);
999 nfsm_wcc_data_xx(struct vnode **v, int *f, struct mbuf **md, caddr_t *dpos)
1002 int ttattrf, ttretf = 0;
1005 tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
1008 if (*tl == nfs_true) {
1009 tl = nfsm_dissect_xx(6 * NFSX_UNSIGNED, md, dpos);
1012 mtx_lock(&(VTONFS(*v))->n_mtx);
1014 ttretf = (VTONFS(*v)->n_mtime.tv_sec == fxdr_unsigned(u_int32_t, *(tl + 2)) &&
1015 VTONFS(*v)->n_mtime.tv_nsec == fxdr_unsigned(u_int32_t, *(tl + 3)));
1016 mtx_unlock(&(VTONFS(*v))->n_mtx);
1018 t1 = nfsm_postop_attr_xx(v, &ttattrf, NULL, md, dpos);
1029 nfsm_strtom_xx(const char *a, int s, int m, struct mbuf **mb, caddr_t *bpos)
1035 return ENAMETOOLONG;
1036 t1 = nfsm_rndup(s) + NFSX_UNSIGNED;
1037 if (t1 <= M_TRAILINGSPACE(*mb)) {
1038 tl = nfsm_build_xx(t1, mb, bpos);
1039 *tl++ = txdr_unsigned(s);
1040 *(tl + ((t1 >> 2) - 2)) = 0;
1043 t1 = nfsm_strtmbuf(mb, bpos, a, s);
1051 nfsm_fhtom_xx(struct vnode *v, int v3, struct mbuf **mb, caddr_t *bpos)
1058 t1 = nfsm_rndup(VTONFS(v)->n_fhsize) + NFSX_UNSIGNED;
1059 if (t1 < M_TRAILINGSPACE(*mb)) {
1060 tl = nfsm_build_xx(t1, mb, bpos);
1061 *tl++ = txdr_unsigned(VTONFS(v)->n_fhsize);
1062 *(tl + ((t1 >> 2) - 2)) = 0;
1063 bcopy(VTONFS(v)->n_fhp, tl, VTONFS(v)->n_fhsize);
1065 t1 = nfsm_strtmbuf(mb, bpos,
1066 (const char *)VTONFS(v)->n_fhp,
1067 VTONFS(v)->n_fhsize);
1072 cp = nfsm_build_xx(NFSX_V2FH, mb, bpos);
1073 bcopy(VTONFS(v)->n_fhp, cp, NFSX_V2FH);
1079 nfsm_v3attrbuild_xx(struct vattr *va, int full, struct mbuf **mb,
1084 if (va->va_mode != (mode_t)VNOVAL) {
1085 tl = nfsm_build_xx(2 * NFSX_UNSIGNED, mb, bpos);
1087 *tl = txdr_unsigned(va->va_mode);
1089 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1092 if (full && va->va_uid != (uid_t)VNOVAL) {
1093 tl = nfsm_build_xx(2 * NFSX_UNSIGNED, mb, bpos);
1095 *tl = txdr_unsigned(va->va_uid);
1097 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1100 if (full && va->va_gid != (gid_t)VNOVAL) {
1101 tl = nfsm_build_xx(2 * NFSX_UNSIGNED, mb, bpos);
1103 *tl = txdr_unsigned(va->va_gid);
1105 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1108 if (full && va->va_size != VNOVAL) {
1109 tl = nfsm_build_xx(3 * NFSX_UNSIGNED, mb, bpos);
1111 txdr_hyper(va->va_size, tl);
1113 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1116 if (va->va_atime.tv_sec != VNOVAL) {
1117 if ((va->va_vaflags & VA_UTIMES_NULL) == 0) {
1118 tl = nfsm_build_xx(3 * NFSX_UNSIGNED, mb, bpos);
1119 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
1120 txdr_nfsv3time(&va->va_atime, tl);
1122 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1123 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
1126 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1127 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
1129 if (va->va_mtime.tv_sec != VNOVAL) {
1130 if ((va->va_vaflags & VA_UTIMES_NULL) == 0) {
1131 tl = nfsm_build_xx(3 * NFSX_UNSIGNED, mb, bpos);
1132 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
1133 txdr_nfsv3time(&va->va_mtime, 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);