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 <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
50 #include <sys/mount.h>
51 #include <sys/vnode.h>
52 #include <sys/namei.h>
54 #include <sys/socket.h>
56 #include <sys/malloc.h>
57 #include <sys/sysent.h>
58 #include <sys/syscall.h>
59 #include <sys/sysproto.h>
62 #include <vm/vm_object.h>
63 #include <vm/vm_extern.h>
66 #include <rpc/rpcclnt.h>
68 #include <nfs/rpcv2.h>
69 #include <nfs/nfsproto.h>
70 #include <nfsclient/nfs.h>
71 #include <nfsclient/nfsnode.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 * Data items converted to xdr at startup, since they are constant
86 * This is kinda hokey, but may save a little time doing byte swaps
88 u_int32_t nfs_xdrneg1;
89 u_int32_t rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr,
90 rpc_mismatch, rpc_auth_unix, rpc_msgaccepted;
91 u_int32_t nfs_true, nfs_false;
93 /* And other global data */
94 u_int32_t nfs_xid = 0;
95 static enum vtype nv2tov_type[8]= {
96 VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON, VNON
100 int nfs_pbuf_freecnt = -1; /* start out unlimited */
102 struct nfs_reqq nfs_reqq;
103 struct mtx nfs_reqq_mtx;
104 struct nfs_bufq nfs_bufq;
107 * and the reverse mapping from generic to Version 2 procedure numbers
109 int nfsv2_procid[NFS_NPROCS] = {
135 LIST_HEAD(nfsnodehashhead, nfsnode);
138 * Create the header for an rpc request packet
139 * The hsiz is the size of the rest of the nfs request header.
140 * (just used to decide if a cluster is a good idea)
143 nfsm_reqhead(struct vnode *vp, u_long procid, int hsiz)
147 MGET(mb, M_TRYWAIT, MT_DATA);
148 if (hsiz >= MINCLSIZE)
149 MCLGET(mb, M_TRYWAIT);
155 * Build the RPC header and fill in the authorization info.
156 * The authorization string argument is only used when the credentials
157 * come from outside of the kernel.
158 * Returns the head of the mbuf list.
161 nfsm_rpchead(struct ucred *cr, int nmflag, int procid, int auth_type,
162 int auth_len, struct mbuf *mrest, int mrest_len, struct mbuf **mbp,
172 authsiz = nfsm_rndup(auth_len);
173 MGETHDR(mb, M_TRYWAIT, MT_DATA);
174 if ((authsiz + 10 * NFSX_UNSIGNED) >= MINCLSIZE) {
175 MCLGET(mb, M_TRYWAIT);
176 } else if ((authsiz + 10 * NFSX_UNSIGNED) < MHLEN) {
177 MH_ALIGN(mb, authsiz + 10 * NFSX_UNSIGNED);
179 MH_ALIGN(mb, 8 * NFSX_UNSIGNED);
183 bpos = mtod(mb, caddr_t);
186 * First the RPC header.
188 tl = nfsm_build(u_int32_t *, 8 * NFSX_UNSIGNED);
190 mtx_lock(&nfs_reqq_mtx);
191 /* Get a pretty random xid to start with */
195 * Skip zero xid if it should ever happen.
201 *tl++ = txdr_unsigned(nfs_xid);
202 mtx_unlock(&nfs_reqq_mtx);
205 *tl++ = txdr_unsigned(NFS_PROG);
206 if (nmflag & NFSMNT_NFSV3) {
207 *tl++ = txdr_unsigned(NFS_VER3);
208 *tl++ = txdr_unsigned(procid);
210 *tl++ = txdr_unsigned(NFS_VER2);
211 *tl++ = txdr_unsigned(nfsv2_procid[procid]);
215 * And then the authorization cred.
217 *tl++ = txdr_unsigned(auth_type);
218 *tl = txdr_unsigned(authsiz);
221 tl = nfsm_build(u_int32_t *, auth_len);
222 *tl++ = 0; /* stamp ?? */
223 *tl++ = 0; /* NULL hostname */
224 *tl++ = txdr_unsigned(cr->cr_uid);
225 *tl++ = txdr_unsigned(cr->cr_groups[0]);
226 grpsiz = (auth_len >> 2) - 5;
227 *tl++ = txdr_unsigned(grpsiz);
228 for (i = 1; i <= grpsiz; i++)
229 *tl++ = txdr_unsigned(cr->cr_groups[i]);
234 * And the verifier...
236 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
237 *tl++ = txdr_unsigned(RPCAUTH_NULL);
240 mreq->m_pkthdr.len = authsiz + 10 * NFSX_UNSIGNED + mrest_len;
241 mreq->m_pkthdr.rcvif = NULL;
247 * copies a uio scatter/gather list to an mbuf chain.
248 * NOTE: can ony handle iovcnt == 1
251 nfsm_uiotombuf(struct uio *uiop, struct mbuf **mq, int siz, caddr_t *bpos)
254 struct mbuf *mp, *mp2;
255 int xfer, left, mlen;
256 int uiosiz, clflg, rem;
260 if (uiop->uio_iovcnt != 1)
261 panic("nfsm_uiotombuf: iovcnt != 1");
264 if (siz > MLEN) /* or should it >= MCLBYTES ?? */
268 rem = nfsm_rndup(siz)-siz;
271 left = uiop->uio_iov->iov_len;
272 uiocp = uiop->uio_iov->iov_base;
277 mlen = M_TRAILINGSPACE(mp);
279 MGET(mp, M_TRYWAIT, MT_DATA);
281 MCLGET(mp, M_TRYWAIT);
285 mlen = M_TRAILINGSPACE(mp);
287 xfer = (left > mlen) ? mlen : left;
290 if (uiop->uio_iov->iov_op != NULL)
291 (*(uiop->uio_iov->iov_op))
292 (uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
295 if (uiop->uio_segflg == UIO_SYSSPACE)
296 bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
298 copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
302 uiop->uio_offset += xfer;
303 uiop->uio_resid -= xfer;
305 uiop->uio_iov->iov_base =
306 (char *)uiop->uio_iov->iov_base + uiosiz;
307 uiop->uio_iov->iov_len -= uiosiz;
311 if (rem > M_TRAILINGSPACE(mp)) {
312 MGET(mp, M_TRYWAIT, MT_DATA);
316 cp = mtod(mp, caddr_t)+mp->m_len;
317 for (left = 0; left < rem; left++)
322 *bpos = mtod(mp, caddr_t)+mp->m_len;
328 * Copy a string into mbufs for the hard cases...
331 nfsm_strtmbuf(struct mbuf **mb, char **bpos, const char *cp, long siz)
333 struct mbuf *m1 = NULL, *m2;
334 long left, xfer, len, tlen;
340 left = M_TRAILINGSPACE(m2);
342 tl = ((u_int32_t *)(*bpos));
343 *tl++ = txdr_unsigned(siz);
345 left -= NFSX_UNSIGNED;
346 m2->m_len += NFSX_UNSIGNED;
348 bcopy(cp, (caddr_t) tl, left);
355 /* Loop around adding mbufs */
357 MGET(m1, M_TRYWAIT, MT_DATA);
359 MCLGET(m1, M_TRYWAIT);
360 m1->m_len = NFSMSIZ(m1);
363 tl = mtod(m1, u_int32_t *);
366 *tl++ = txdr_unsigned(siz);
367 m1->m_len -= NFSX_UNSIGNED;
368 tlen = NFSX_UNSIGNED;
371 if (siz < m1->m_len) {
372 len = nfsm_rndup(siz);
377 xfer = len = m1->m_len;
379 bcopy(cp, (caddr_t) tl, xfer);
380 m1->m_len = len+tlen;
385 *bpos = mtod(m1, caddr_t)+m1->m_len;
390 * Called once to initialize data structures...
393 nfs_init(struct vfsconf *vfsp)
397 nfsmount_zone = uma_zcreate("NFSMOUNT", sizeof(struct nfsmount),
398 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
399 rpc_vers = txdr_unsigned(RPC_VER2);
400 rpc_call = txdr_unsigned(RPC_CALL);
401 rpc_reply = txdr_unsigned(RPC_REPLY);
402 rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
403 rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
404 rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
405 rpc_autherr = txdr_unsigned(RPC_AUTHERR);
406 rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
407 nfs_true = txdr_unsigned(TRUE);
408 nfs_false = txdr_unsigned(FALSE);
409 nfs_xdrneg1 = txdr_unsigned(-1);
410 nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
413 /* Ensure async daemons disabled */
414 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
415 nfs_iodwant[i] = NULL;
416 nfs_iodmount[i] = NULL;
418 nfs_nhinit(); /* Init the nfsnode table */
421 * Initialize reply list and start timer
423 TAILQ_INIT(&nfs_reqq);
424 callout_init(&nfs_callout, CALLOUT_MPSAFE);
425 mtx_init(&nfs_reqq_mtx, "NFS reqq lock", NULL, MTX_DEF);
426 mtx_init(&nfs_iod_mtx, "NFS iod lock", NULL, MTX_DEF);
428 nfs_pbuf_freecnt = nswbuf / 2 + 1;
434 nfs_uninit(struct vfsconf *vfsp)
438 callout_stop(&nfs_callout);
440 KASSERT(TAILQ_EMPTY(&nfs_reqq),
441 ("nfs_uninit: request queue not empty"));
444 * Tell all nfsiod processes to exit. Clear nfs_iodmax, and wakeup
445 * any sleeping nfsiods so they check nfs_iodmax and exit.
447 mtx_lock(&nfs_iod_mtx);
449 for (i = 0; i < nfs_numasync; i++)
451 wakeup(&nfs_iodwant[i]);
452 /* The last nfsiod to exit will wake us up when nfs_numasync hits 0 */
454 msleep(&nfs_numasync, &nfs_iod_mtx, PWAIT, "ioddie", 0);
455 mtx_unlock(&nfs_iod_mtx);
457 uma_zdestroy(nfsmount_zone);
462 nfs_dircookie_lock(struct nfsnode *np)
464 mtx_lock(&np->n_mtx);
465 while (np->n_flag & NDIRCOOKIELK)
466 (void) msleep(&np->n_flag, &np->n_mtx, PZERO, "nfsdirlk", 0);
467 np->n_flag |= NDIRCOOKIELK;
468 mtx_unlock(&np->n_mtx);
472 nfs_dircookie_unlock(struct nfsnode *np)
474 mtx_lock(&np->n_mtx);
475 np->n_flag &= ~NDIRCOOKIELK;
477 mtx_unlock(&np->n_mtx);
481 nfs_upgrade_vnlock(struct vnode *vp, struct thread *td)
485 if ((old_lock = VOP_ISLOCKED(vp, td)) != LK_EXCLUSIVE) {
486 if (old_lock == LK_SHARED) {
487 /* Upgrade to exclusive lock, this might block */
488 vn_lock(vp, LK_UPGRADE | LK_RETRY, td);
490 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
497 nfs_downgrade_vnlock(struct vnode *vp, struct thread *td, int old_lock)
499 if (old_lock != LK_EXCLUSIVE) {
500 if (old_lock == LK_SHARED) {
501 /* Downgrade from exclusive lock, this might block */
502 vn_lock(vp, LK_DOWNGRADE, td);
504 VOP_UNLOCK(vp, 0, td);
510 nfs_printf(const char *fmt, ...)
522 * Attribute cache routines.
523 * nfs_loadattrcache() - loads or updates the cache contents from attributes
524 * that are on the mbuf list
525 * nfs_getattrcache() - returns valid attributes if found in cache, returns
530 * Load the attribute cache (that lives in the nfsnode entry) with
531 * the values on the mbuf list and
533 * copy the attributes to *vaper
536 nfs_loadattrcache(struct vnode **vpp, struct mbuf **mdp, caddr_t *dposp,
537 struct vattr *vaper, int dontshrink)
539 struct vnode *vp = *vpp;
541 struct nfs_fattr *fp;
549 struct timespec mtime;
550 int v3 = NFS_ISV3(vp);
551 struct thread *td = curthread;
554 t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
555 cp2 = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, M_TRYWAIT);
558 fp = (struct nfs_fattr *)cp2;
560 vtyp = nfsv3tov_type(fp->fa_type);
561 vmode = fxdr_unsigned(u_short, fp->fa_mode);
562 rdev = makedev(fxdr_unsigned(int, fp->fa3_rdev.specdata1),
563 fxdr_unsigned(int, fp->fa3_rdev.specdata2));
564 fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
566 vtyp = nfsv2tov_type(fp->fa_type);
567 vmode = fxdr_unsigned(u_short, fp->fa_mode);
571 * The duplicate information returned in fa_type and fa_mode
572 * is an ambiguity in the NFS version 2 protocol.
574 * VREG should be taken literally as a regular file. If a
575 * server intents to return some type information differently
576 * in the upper bits of the mode field (e.g. for sockets, or
577 * FIFOs), NFSv2 mandates fa_type to be VNON. Anyway, we
578 * leave the examination of the mode bits even in the VREG
579 * case to avoid breakage for bogus servers, but we make sure
580 * that there are actually type bits set in the upper part of
581 * fa_mode (and failing that, trust the va_type field).
583 * NFSv3 cleared the issue, and requires fa_mode to not
584 * contain any type information (while also introduing sockets
585 * and FIFOs for fa_type).
587 if (vtyp == VNON || (vtyp == VREG && (vmode & S_IFMT) != 0))
588 vtyp = IFTOVT(vmode);
589 rdev = fxdr_unsigned(int32_t, fp->fa2_rdev);
590 fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
593 * Really ugly NFSv2 kludge.
595 if (vtyp == VCHR && rdev == 0xffffffff)
600 * If v_type == VNON it is a new node, so fill in the v_type,
601 * n_mtime fields. Check to see if it represents a special
602 * device, and if so, check for a possible alias. Once the
603 * correct vnode has been obtained, fill in the rest of the
607 mtx_lock(&np->n_mtx);
608 if (vp->v_type != vtyp) {
610 if (vp->v_type == VFIFO)
611 vp->v_op = &nfs_fifoops;
616 vap->va_mode = (vmode & 07777);
618 vap->va_mtime = mtime;
619 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
621 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
622 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
623 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
624 vap->va_size = fxdr_hyper(&fp->fa3_size);
625 vap->va_blocksize = NFS_FABLKSIZE;
626 vap->va_bytes = fxdr_hyper(&fp->fa3_used);
627 vap->va_fileid = fxdr_unsigned(int32_t,
628 fp->fa3_fileid.nfsuquad[1]);
629 fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
630 fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
634 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
635 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
636 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
637 vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
638 vap->va_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize);
639 vap->va_bytes = (u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks)
641 vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
642 fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
644 vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t,
645 fp->fa2_ctime.nfsv2_sec);
646 vap->va_ctime.tv_nsec = 0;
647 vap->va_gen = fxdr_unsigned(u_int32_t, fp->fa2_ctime.nfsv2_usec);
650 np->n_attrstamp = time_second;
651 /* Timestamp the NFS otw getattr fetch */
653 np->n_ac_ts_tid = td->td_tid;
654 np->n_ac_ts_pid = td->td_proc->p_pid;
655 np->n_ac_ts_syscalls = td->td_syscalls;
657 bzero(&np->n_ac_ts, sizeof(struct nfs_attrcache_timestamp));
659 if (vap->va_size != np->n_size) {
660 if (vap->va_type == VREG) {
661 if (dontshrink && vap->va_size < np->n_size) {
663 * We've been told not to shrink the file;
664 * zero np->n_attrstamp to indicate that
665 * the attributes are stale.
667 vap->va_size = np->n_size;
669 } else if (np->n_flag & NMODIFIED) {
671 * We've modified the file: Use the larger
672 * of our size, and the server's size.
674 if (vap->va_size < np->n_size) {
675 vap->va_size = np->n_size;
677 np->n_size = vap->va_size;
678 np->n_flag |= NSIZECHANGED;
681 np->n_size = vap->va_size;
682 np->n_flag |= NSIZECHANGED;
684 vnode_pager_setsize(vp, np->n_size);
686 np->n_size = vap->va_size;
690 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
691 if (np->n_flag & NCHG) {
692 if (np->n_flag & NACC)
693 vaper->va_atime = np->n_atim;
694 if (np->n_flag & NUPD)
695 vaper->va_mtime = np->n_mtim;
698 mtx_unlock(&np->n_mtx);
703 #include <sys/sysctl.h>
704 SYSCTL_DECL(_vfs_nfs);
705 static int nfs_acdebug;
706 SYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, "");
710 * Check the time stamp
711 * If the cache is valid, copy contents to *vap and return 0
712 * otherwise return an error
715 nfs_getattrcache(struct vnode *vp, struct vattr *vaper)
719 struct nfsmount *nmp;
724 nmp = VFSTONFS(vp->v_mount);
726 mtx_lock(&Giant); /* nfs_printf() */
728 mtx_lock(&np->n_mtx);
729 /* XXX n_mtime doesn't seem to be updated on a miss-and-reload */
730 timeo = (time_second - np->n_mtime.tv_sec) / 10;
734 nfs_printf("nfs_getattrcache: initial timeo = %d\n", timeo);
737 if (vap->va_type == VDIR) {
738 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin)
739 timeo = nmp->nm_acdirmin;
740 else if (timeo > nmp->nm_acdirmax)
741 timeo = nmp->nm_acdirmax;
743 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin)
744 timeo = nmp->nm_acregmin;
745 else if (timeo > nmp->nm_acregmax)
746 timeo = nmp->nm_acregmax;
751 nfs_printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n",
752 nmp->nm_acregmin, nmp->nm_acregmax,
753 nmp->nm_acdirmin, nmp->nm_acdirmax);
756 nfs_printf("nfs_getattrcache: age = %d; final timeo = %d\n",
757 (time_second - np->n_attrstamp), timeo);
760 if ((time_second - np->n_attrstamp) >= timeo) {
761 nfsstats.attrcache_misses++;
762 mtx_unlock(&np->n_mtx);
765 nfsstats.attrcache_hits++;
766 if (vap->va_size != np->n_size) {
767 if (vap->va_type == VREG) {
768 if (np->n_flag & NMODIFIED) {
769 if (vap->va_size < np->n_size)
770 vap->va_size = np->n_size;
772 np->n_size = vap->va_size;
774 np->n_size = vap->va_size;
776 vnode_pager_setsize(vp, np->n_size);
778 np->n_size = vap->va_size;
781 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
782 if (np->n_flag & NCHG) {
783 if (np->n_flag & NACC)
784 vaper->va_atime = np->n_atim;
785 if (np->n_flag & NUPD)
786 vaper->va_mtime = np->n_mtim;
788 mtx_unlock(&np->n_mtx);
790 mtx_unlock(&Giant); /* nfs_printf() */
795 static nfsuint64 nfs_nullcookie = { { 0, 0 } };
797 * This function finds the directory cookie that corresponds to the
798 * logical byte offset given.
801 nfs_getcookie(struct nfsnode *np, off_t off, int add)
803 struct nfsdmap *dp, *dp2;
805 nfsuint64 *retval = NULL;
807 pos = (uoff_t)off / NFS_DIRBLKSIZ;
808 if (pos == 0 || off < 0) {
811 panic("nfs getcookie add at <= 0");
813 return (&nfs_nullcookie);
816 dp = LIST_FIRST(&np->n_cookies);
819 MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap),
820 M_NFSDIROFF, M_WAITOK);
821 dp->ndm_eocookie = 0;
822 LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
826 while (pos >= NFSNUMCOOKIES) {
827 pos -= NFSNUMCOOKIES;
828 if (LIST_NEXT(dp, ndm_list)) {
829 if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
830 pos >= dp->ndm_eocookie)
832 dp = LIST_NEXT(dp, ndm_list);
834 MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap),
835 M_NFSDIROFF, M_WAITOK);
836 dp2->ndm_eocookie = 0;
837 LIST_INSERT_AFTER(dp, dp2, ndm_list);
842 if (pos >= dp->ndm_eocookie) {
844 dp->ndm_eocookie = pos + 1;
848 retval = &dp->ndm_cookies[pos];
854 * Invalidate cached directory information, except for the actual directory
855 * blocks (which are invalidated separately).
856 * Done mainly to avoid the use of stale offset cookies.
859 nfs_invaldir(struct vnode *vp)
861 struct nfsnode *np = VTONFS(vp);
864 if (vp->v_type != VDIR)
865 panic("nfs: invaldir not dir");
867 nfs_dircookie_lock(np);
868 np->n_direofoffset = 0;
869 np->n_cookieverf.nfsuquad[0] = 0;
870 np->n_cookieverf.nfsuquad[1] = 0;
871 if (LIST_FIRST(&np->n_cookies))
872 LIST_FIRST(&np->n_cookies)->ndm_eocookie = 0;
873 nfs_dircookie_unlock(np);
877 * The write verifier has changed (probably due to a server reboot), so all
878 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
879 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
880 * and B_CLUSTEROK flags. Once done the new write verifier can be set for the
883 * B_CLUSTEROK must be cleared along with B_NEEDCOMMIT because stage 1 data
884 * writes are not clusterable.
887 nfs_clearcommit(struct mount *mp)
889 struct vnode *vp, *nvp;
890 struct buf *bp, *nbp;
895 MNT_VNODE_FOREACH(vp, mp, nvp) {
897 if (vp->v_iflag & VI_DOOMED) {
902 TAILQ_FOREACH_SAFE(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs, nbp) {
903 if (BUF_REFCNT(bp) == 0 &&
904 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
905 == (B_DELWRI | B_NEEDCOMMIT))
906 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
916 * Helper functions for former macros. Some of these should be
917 * moved to their callers.
921 nfsm_mtofh_xx(struct vnode *d, struct vnode **v, int v3, int *f,
922 struct mbuf **md, caddr_t *dpos)
924 struct nfsnode *ttnp;
932 tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
935 *f = fxdr_unsigned(int, *tl);
939 t1 = nfsm_getfh_xx(&ttfhp, &ttfhsize, (v3), md, dpos);
942 t1 = nfs_nget(d->v_mount, ttfhp, ttfhsize, &ttnp, LK_EXCLUSIVE);
948 tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
952 *f = fxdr_unsigned(int, *tl);
953 else if (fxdr_unsigned(int, *tl))
954 nfsm_adv_xx(NFSX_V3FATTR, md, dpos);
958 t1 = nfs_loadattrcache(&ttvp, md, dpos, NULL, 0);
967 nfsm_getfh_xx(nfsfh_t **f, int *s, int v3, struct mbuf **md, caddr_t *dpos)
972 tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
975 *s = fxdr_unsigned(int, *tl);
976 if (*s <= 0 || *s > NFSX_V3FHMAX)
980 *f = nfsm_dissect_xx(nfsm_rndup(*s), md, dpos);
989 nfsm_loadattr_xx(struct vnode **v, struct vattr *va, struct mbuf **md,
994 struct vnode *ttvp = *v;
995 t1 = nfs_loadattrcache(&ttvp, md, dpos, va, 0);
1003 nfsm_postop_attr_xx(struct vnode **v, int *f, struct mbuf **md,
1009 struct vnode *ttvp = *v;
1010 tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
1013 *f = fxdr_unsigned(int, *tl);
1015 t1 = nfs_loadattrcache(&ttvp, md, dpos, NULL, 1);
1026 nfsm_wcc_data_xx(struct vnode **v, int *f, struct mbuf **md, caddr_t *dpos)
1029 int ttattrf, ttretf = 0;
1032 tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
1035 if (*tl == nfs_true) {
1036 tl = nfsm_dissect_xx(6 * NFSX_UNSIGNED, md, dpos);
1039 mtx_lock(&(VTONFS(*v))->n_mtx);
1041 ttretf = (VTONFS(*v)->n_mtime.tv_sec == fxdr_unsigned(u_int32_t, *(tl + 2)) &&
1042 VTONFS(*v)->n_mtime.tv_nsec == fxdr_unsigned(u_int32_t, *(tl + 3)));
1043 mtx_unlock(&(VTONFS(*v))->n_mtx);
1045 t1 = nfsm_postop_attr_xx(v, &ttattrf, md, dpos);
1056 nfsm_strtom_xx(const char *a, int s, int m, struct mbuf **mb, caddr_t *bpos)
1062 return ENAMETOOLONG;
1063 t1 = nfsm_rndup(s) + NFSX_UNSIGNED;
1064 if (t1 <= M_TRAILINGSPACE(*mb)) {
1065 tl = nfsm_build_xx(t1, mb, bpos);
1066 *tl++ = txdr_unsigned(s);
1067 *(tl + ((t1 >> 2) - 2)) = 0;
1070 t1 = nfsm_strtmbuf(mb, bpos, a, s);
1078 nfsm_fhtom_xx(struct vnode *v, int v3, struct mbuf **mb, caddr_t *bpos)
1085 t1 = nfsm_rndup(VTONFS(v)->n_fhsize) + NFSX_UNSIGNED;
1086 if (t1 < M_TRAILINGSPACE(*mb)) {
1087 tl = nfsm_build_xx(t1, mb, bpos);
1088 *tl++ = txdr_unsigned(VTONFS(v)->n_fhsize);
1089 *(tl + ((t1 >> 2) - 2)) = 0;
1090 bcopy(VTONFS(v)->n_fhp, tl, VTONFS(v)->n_fhsize);
1092 t1 = nfsm_strtmbuf(mb, bpos,
1093 (const char *)VTONFS(v)->n_fhp,
1094 VTONFS(v)->n_fhsize);
1099 cp = nfsm_build_xx(NFSX_V2FH, mb, bpos);
1100 bcopy(VTONFS(v)->n_fhp, cp, NFSX_V2FH);
1106 nfsm_v3attrbuild_xx(struct vattr *va, int full, struct mbuf **mb,
1111 if (va->va_mode != (mode_t)VNOVAL) {
1112 tl = nfsm_build_xx(2 * NFSX_UNSIGNED, mb, bpos);
1114 *tl = txdr_unsigned(va->va_mode);
1116 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1119 if (full && va->va_uid != (uid_t)VNOVAL) {
1120 tl = nfsm_build_xx(2 * NFSX_UNSIGNED, mb, bpos);
1122 *tl = txdr_unsigned(va->va_uid);
1124 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1127 if (full && va->va_gid != (gid_t)VNOVAL) {
1128 tl = nfsm_build_xx(2 * NFSX_UNSIGNED, mb, bpos);
1130 *tl = txdr_unsigned(va->va_gid);
1132 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1135 if (full && va->va_size != VNOVAL) {
1136 tl = nfsm_build_xx(3 * NFSX_UNSIGNED, mb, bpos);
1138 txdr_hyper(va->va_size, tl);
1140 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1143 if (va->va_atime.tv_sec != VNOVAL) {
1144 if (va->va_atime.tv_sec != time_second) {
1145 tl = nfsm_build_xx(3 * NFSX_UNSIGNED, mb, bpos);
1146 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
1147 txdr_nfsv3time(&va->va_atime, tl);
1149 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1150 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
1153 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1154 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
1156 if (va->va_mtime.tv_sec != VNOVAL) {
1157 if (va->va_mtime.tv_sec != time_second) {
1158 tl = nfsm_build_xx(3 * NFSX_UNSIGNED, mb, bpos);
1159 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
1160 txdr_nfsv3time(&va->va_mtime, tl);
1162 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1163 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
1166 tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
1167 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);