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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)nfs_subs.c 8.8 (Berkeley) 5/22/95
37 * $FreeBSD: /repoman/r/ncvs/src/sys/nfsclient/nfs_subs.c,v 1.128 2004/04/14 23:23:55 peadar Exp $
38 * $DragonFly: src/sys/vfs/nfs/nfs_subs.c,v 1.45 2006/12/23 00:41:29 swildner Exp $
42 * These functions support the macros and help fiddle mbuf chains for
43 * the nfs op functions. They do things like create the rpc header and
44 * copy data between mbuf chains and uio lists.
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
51 #include <sys/mount.h>
52 #include <sys/vnode.h>
53 #include <sys/nlookup.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>
62 #include <sys/objcache.h>
65 #include <vm/vm_object.h>
66 #include <vm/vm_extern.h>
67 #include <vm/vm_zone.h>
77 #include "nfsm_subs.h"
80 #include <netinet/in.h>
83 * Data items converted to xdr at startup, since they are constant
84 * This is kinda hokey, but may save a little time doing byte swaps
86 u_int32_t nfs_xdrneg1;
87 u_int32_t rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr,
88 rpc_mismatch, rpc_auth_unix, rpc_msgaccepted,
90 u_int32_t nfs_prog, nfs_true, nfs_false;
92 /* And other global data */
93 static u_int32_t nfs_xid = 0;
94 static enum vtype nv2tov_type[8]= {
95 VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON, VNON
97 enum vtype nv3tov_type[8]= {
98 VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO
102 int nfs_pbuf_freecnt = -1; /* start out unlimited */
104 struct nfs_reqq nfs_reqq;
105 struct nfssvc_sockhead nfssvc_sockhead;
106 int nfssvc_sockhead_flag;
107 struct nfsd_head nfsd_head;
109 struct nfs_bufq nfs_bufq;
110 struct nqfhhashhead *nqfhhashtbl;
113 static int nfs_prev_nfssvc_sy_narg;
114 static sy_call_t *nfs_prev_nfssvc_sy_call;
119 * Mapping of old NFS Version 2 RPC numbers to generic numbers.
121 int nfsv3_procid[NFS_NPROCS] = {
150 #endif /* NFS_NOSERVER */
152 * and the reverse mapping from generic to Version 2 procedure numbers
154 int nfsv2_procid[NFS_NPROCS] = {
185 * Maps errno values to nfs error numbers.
186 * Use NFSERR_IO as the catch all for ones not specifically defined in
189 static u_char nfsrv_v2errmap[ELAST] = {
190 NFSERR_PERM, NFSERR_NOENT, NFSERR_IO, NFSERR_IO, NFSERR_IO,
191 NFSERR_NXIO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
192 NFSERR_IO, NFSERR_IO, NFSERR_ACCES, NFSERR_IO, NFSERR_IO,
193 NFSERR_IO, NFSERR_EXIST, NFSERR_IO, NFSERR_NODEV, NFSERR_NOTDIR,
194 NFSERR_ISDIR, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
195 NFSERR_IO, NFSERR_FBIG, NFSERR_NOSPC, NFSERR_IO, NFSERR_ROFS,
196 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
197 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
198 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
199 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
200 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
201 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
202 NFSERR_IO, NFSERR_IO, NFSERR_NAMETOL, NFSERR_IO, NFSERR_IO,
203 NFSERR_NOTEMPTY, NFSERR_IO, NFSERR_IO, NFSERR_DQUOT, NFSERR_STALE,
204 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
205 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
206 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
207 NFSERR_IO /* << Last is 86 */
211 * Maps errno values to nfs error numbers.
212 * Although it is not obvious whether or not NFS clients really care if
213 * a returned error value is in the specified list for the procedure, the
214 * safest thing to do is filter them appropriately. For Version 2, the
215 * X/Open XNFS document is the only specification that defines error values
216 * for each RPC (The RFC simply lists all possible error values for all RPCs),
217 * so I have decided to not do this for Version 2.
218 * The first entry is the default error return and the rest are the valid
219 * errors for that RPC in increasing numeric order.
221 static short nfsv3err_null[] = {
226 static short nfsv3err_getattr[] = {
235 static short nfsv3err_setattr[] = {
251 static short nfsv3err_lookup[] = {
264 static short nfsv3err_access[] = {
273 static short nfsv3err_readlink[] = {
285 static short nfsv3err_read[] = {
297 static short nfsv3err_write[] = {
312 static short nfsv3err_create[] = {
329 static short nfsv3err_mkdir[] = {
346 static short nfsv3err_symlink[] = {
363 static short nfsv3err_mknod[] = {
381 static short nfsv3err_remove[] = {
395 static short nfsv3err_rmdir[] = {
413 static short nfsv3err_rename[] = {
436 static short nfsv3err_link[] = {
456 static short nfsv3err_readdir[] = {
469 static short nfsv3err_readdirplus[] = {
483 static short nfsv3err_fsstat[] = {
492 static short nfsv3err_fsinfo[] = {
500 static short nfsv3err_pathconf[] = {
508 static short nfsv3err_commit[] = {
517 static short *nfsrv_v3errmap[] = {
535 nfsv3err_readdirplus,
542 #endif /* NFS_NOSERVER */
544 extern struct nfsrtt nfsrtt;
545 extern struct nfsstats nfsstats;
546 extern nfstype nfsv2_type[9];
547 extern nfstype nfsv3_type[9];
548 extern struct nfsnodehashhead *nfsnodehashtbl;
549 extern u_long nfsnodehash;
552 extern int sys_nfssvc(struct proc *, struct nfssvc_args *, int *);
554 LIST_HEAD(nfsnodehashhead, nfsnode);
557 * This needs to return a monotonically increasing or close to monotonically
558 * increasing result, otherwise the write gathering queues won't work
567 return ((u_quad_t)tv.tv_sec * 1000000 + (u_quad_t)tv.tv_usec);
571 * Create the header for an rpc request packet
572 * The hsiz is the size of the rest of the nfs request header.
573 * (just used to decide if a cluster is a good idea)
576 nfsm_reqh(struct vnode *vp, u_long procid, int hsiz, caddr_t *bposp)
581 mb = m_getl(hsiz, MB_WAIT, MT_DATA, 0, NULL);
583 bpos = mtod(mb, caddr_t);
585 /* Finally, return values */
591 * Build the RPC header and fill in the authorization info.
592 * The authorization string argument is only used when the credentials
593 * come from outside of the kernel.
594 * Returns the head of the mbuf list.
597 nfsm_rpchead(struct ucred *cr, int nmflag, int procid, int auth_type,
598 int auth_len, char *auth_str, int verf_len, char *verf_str,
599 struct mbuf *mrest, int mrest_len, struct mbuf **mbp,
606 struct mbuf *mreq, *mb2;
607 int siz, grpsiz, authsiz, dsiz;
609 authsiz = nfsm_rndup(auth_len);
610 dsiz = authsiz + 10 * NFSX_UNSIGNED;
611 mb = m_getl(dsiz, MB_WAIT, MT_DATA, M_PKTHDR, NULL);
612 if (dsiz < MINCLSIZE) {
616 MH_ALIGN(mb, 8 * NFSX_UNSIGNED);
618 mb->m_len = mb->m_pkthdr.len = 0;
620 bpos = mtod(mb, caddr_t);
623 * First the RPC header.
625 nfsm_build(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
627 /* Get a pretty random xid to start with */
631 * Skip zero xid if it should ever happen.
636 *tl++ = *xidp = txdr_unsigned(nfs_xid);
639 *tl++ = txdr_unsigned(NFS_PROG);
640 if (nmflag & NFSMNT_NFSV3)
641 *tl++ = txdr_unsigned(NFS_VER3);
643 *tl++ = txdr_unsigned(NFS_VER2);
644 if (nmflag & NFSMNT_NFSV3)
645 *tl++ = txdr_unsigned(procid);
647 *tl++ = txdr_unsigned(nfsv2_procid[procid]);
650 * And then the authorization cred.
652 *tl++ = txdr_unsigned(auth_type);
653 *tl = txdr_unsigned(authsiz);
656 nfsm_build(tl, u_int32_t *, auth_len);
657 *tl++ = 0; /* stamp ?? */
658 *tl++ = 0; /* NULL hostname */
659 *tl++ = txdr_unsigned(cr->cr_uid);
660 *tl++ = txdr_unsigned(cr->cr_groups[0]);
661 grpsiz = (auth_len >> 2) - 5;
662 *tl++ = txdr_unsigned(grpsiz);
663 for (i = 1; i <= grpsiz; i++)
664 *tl++ = txdr_unsigned(cr->cr_groups[i]);
669 if (M_TRAILINGSPACE(mb) == 0) {
670 mb2 = m_getl(siz, MB_WAIT, MT_DATA, 0, NULL);
674 bpos = mtod(mb, caddr_t);
676 i = min(siz, M_TRAILINGSPACE(mb));
677 bcopy(auth_str, bpos, i);
683 if ((siz = (nfsm_rndup(auth_len) - auth_len)) > 0) {
684 for (i = 0; i < siz; i++)
692 * And the verifier...
694 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
696 *tl++ = txdr_unsigned(RPCAUTH_KERB4);
697 *tl = txdr_unsigned(verf_len);
700 if (M_TRAILINGSPACE(mb) == 0) {
701 mb2 = m_getl(siz, MB_WAIT, MT_DATA, 0, NULL);
705 bpos = mtod(mb, caddr_t);
707 i = min(siz, M_TRAILINGSPACE(mb));
708 bcopy(verf_str, bpos, i);
714 if ((siz = (nfsm_rndup(verf_len) - verf_len)) > 0) {
715 for (i = 0; i < siz; i++)
720 *tl++ = txdr_unsigned(RPCAUTH_NULL);
724 mreq->m_pkthdr.len = authsiz + 10 * NFSX_UNSIGNED + mrest_len;
725 mreq->m_pkthdr.rcvif = (struct ifnet *)0;
731 * copies mbuf chain to the uio scatter/gather list
734 nfsm_mbuftouio(struct mbuf **mrep, struct uio *uiop, int siz, caddr_t *dpos)
736 char *mbufcp, *uiocp;
744 len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
745 rem = nfsm_rndup(siz)-siz;
747 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
749 left = uiop->uio_iov->iov_len;
750 uiocp = uiop->uio_iov->iov_base;
759 mbufcp = mtod(mp, caddr_t);
762 xfer = (left > len) ? len : left;
765 if (uiop->uio_iov->iov_op != NULL)
766 (*(uiop->uio_iov->iov_op))
767 (mbufcp, uiocp, xfer);
770 if (uiop->uio_segflg == UIO_SYSSPACE)
771 bcopy(mbufcp, uiocp, xfer);
773 copyout(mbufcp, uiocp, xfer);
778 uiop->uio_offset += xfer;
779 uiop->uio_resid -= xfer;
781 if (uiop->uio_iov->iov_len <= siz) {
785 uiop->uio_iov->iov_base += uiosiz;
786 uiop->uio_iov->iov_len -= uiosiz;
794 error = nfs_adv(mrep, dpos, rem, len);
802 * copies a uio scatter/gather list to an mbuf chain.
803 * NOTE: can ony handle iovcnt == 1
806 nfsm_uiotombuf(struct uio *uiop, struct mbuf **mq, int siz, caddr_t *bpos)
809 struct mbuf *mp, *mp2;
810 int xfer, left, mlen;
812 boolean_t getcluster;
816 if (uiop->uio_iovcnt != 1)
817 panic("nfsm_uiotombuf: iovcnt != 1");
820 if (siz >= MINCLSIZE)
824 rem = nfsm_rndup(siz) - siz;
827 left = uiop->uio_iov->iov_len;
828 uiocp = uiop->uio_iov->iov_base;
833 mlen = M_TRAILINGSPACE(mp);
836 mp = m_getcl(MB_WAIT, MT_DATA, 0);
838 mp = m_get(MB_WAIT, MT_DATA);
842 mlen = M_TRAILINGSPACE(mp);
844 xfer = (left > mlen) ? mlen : left;
847 if (uiop->uio_iov->iov_op != NULL)
848 (*(uiop->uio_iov->iov_op))
849 (uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
852 if (uiop->uio_segflg == UIO_SYSSPACE)
853 bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
855 copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
859 uiop->uio_offset += xfer;
860 uiop->uio_resid -= xfer;
862 uiop->uio_iov->iov_base += uiosiz;
863 uiop->uio_iov->iov_len -= uiosiz;
867 if (rem > M_TRAILINGSPACE(mp)) {
868 MGET(mp, MB_WAIT, MT_DATA);
872 cp = mtod(mp, caddr_t)+mp->m_len;
873 for (left = 0; left < rem; left++)
878 *bpos = mtod(mp, caddr_t)+mp->m_len;
884 * Help break down an mbuf chain by setting the first siz bytes contiguous
885 * pointed to by returned val.
886 * This is used by the macros nfsm_dissect and nfsm_dissecton for tough
887 * cases. (The macros use the vars. dpos and dpos2)
890 nfsm_disct(struct mbuf **mdp, caddr_t *dposp, int siz, int left, caddr_t *cp2)
892 struct mbuf *mp, *mp2;
898 *mdp = mp = mp->m_next;
902 *dposp = mtod(mp, caddr_t);
907 } else if (mp->m_next == NULL) {
909 } else if (siz > MHLEN) {
910 panic("nfs S too big");
912 MGET(mp2, MB_WAIT, MT_DATA);
913 mp2->m_next = mp->m_next;
917 *cp2 = p = mtod(mp, caddr_t);
918 bcopy(*dposp, p, left); /* Copy what was left */
922 /* Loop around copying up the siz2 bytes */
926 xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2;
928 bcopy(mtod(mp2, caddr_t), p, xfer);
939 *dposp = mtod(mp2, caddr_t);
945 * Advance the position in the mbuf chain.
948 nfs_adv(struct mbuf **mdp, caddr_t *dposp, int offs, int left)
963 *dposp = mtod(m, caddr_t)+offs;
968 * Copy a string into mbufs for the hard cases...
971 nfsm_strtmbuf(struct mbuf **mb, char **bpos, const char *cp, long siz)
973 struct mbuf *m1 = NULL, *m2;
974 long left, xfer, len, tlen;
980 left = M_TRAILINGSPACE(m2);
982 tl = ((u_int32_t *)(*bpos));
983 *tl++ = txdr_unsigned(siz);
985 left -= NFSX_UNSIGNED;
986 m2->m_len += NFSX_UNSIGNED;
988 bcopy(cp, (caddr_t) tl, left);
995 /* Loop around adding mbufs */
999 m1 = m_getl(siz, MB_WAIT, MT_DATA, 0, &msize);
1003 tl = mtod(m1, u_int32_t *);
1006 *tl++ = txdr_unsigned(siz);
1007 m1->m_len -= NFSX_UNSIGNED;
1008 tlen = NFSX_UNSIGNED;
1011 if (siz < m1->m_len) {
1012 len = nfsm_rndup(siz);
1015 *(tl+(xfer>>2)) = 0;
1017 xfer = len = m1->m_len;
1019 bcopy(cp, (caddr_t) tl, xfer);
1020 m1->m_len = len+tlen;
1025 *bpos = mtod(m1, caddr_t)+m1->m_len;
1030 * Called once to initialize data structures...
1033 nfs_init(struct vfsconf *vfsp)
1037 callout_init(&nfs_timer_handle);
1038 nfsmount_zone = zinit("NFSMOUNT", sizeof(struct nfsmount), 0, 0, 1);
1040 nfs_mount_type = vfsp->vfc_typenum;
1042 rpc_vers = txdr_unsigned(RPC_VER2);
1043 rpc_call = txdr_unsigned(RPC_CALL);
1044 rpc_reply = txdr_unsigned(RPC_REPLY);
1045 rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
1046 rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
1047 rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
1048 rpc_autherr = txdr_unsigned(RPC_AUTHERR);
1049 rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
1050 rpc_auth_kerb = txdr_unsigned(RPCAUTH_KERB4);
1051 nfs_prog = txdr_unsigned(NFS_PROG);
1052 nfs_true = txdr_unsigned(TRUE);
1053 nfs_false = txdr_unsigned(FALSE);
1054 nfs_xdrneg1 = txdr_unsigned(-1);
1055 nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
1058 /* Ensure async daemons disabled */
1059 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
1060 nfs_iodwant[i] = NULL;
1061 nfs_iodmount[i] = (struct nfsmount *)0;
1063 nfs_nhinit(); /* Init the nfsnode table */
1064 #ifndef NFS_NOSERVER
1065 nfsrv_init(0); /* Init server data structures */
1066 nfsrv_initcache(); /* Init the server request cache */
1070 * Initialize reply list and start timer
1072 TAILQ_INIT(&nfs_reqq);
1076 nfs_prev_nfssvc_sy_narg = sysent[SYS_nfssvc].sy_narg;
1077 sysent[SYS_nfssvc].sy_narg = 2;
1078 nfs_prev_nfssvc_sy_call = sysent[SYS_nfssvc].sy_call;
1079 sysent[SYS_nfssvc].sy_call = (sy_call_t *)sys_nfssvc;
1081 nfs_pbuf_freecnt = nswbuf / 2 + 1;
1087 nfs_uninit(struct vfsconf *vfsp)
1089 callout_stop(&nfs_timer_handle);
1090 nfs_mount_type = -1;
1091 sysent[SYS_nfssvc].sy_narg = nfs_prev_nfssvc_sy_narg;
1092 sysent[SYS_nfssvc].sy_call = nfs_prev_nfssvc_sy_call;
1097 * Attribute cache routines.
1098 * nfs_loadattrcache() - loads or updates the cache contents from attributes
1099 * that are on the mbuf list
1100 * nfs_getattrcache() - returns valid attributes if found in cache, returns
1105 * Load the attribute cache (that lives in the nfsnode entry) with
1106 * the values on the mbuf list. Load *vaper with the attributes. vaper
1109 * As a side effect n_mtime, which we use to determine if the file was
1110 * modified by some other host, is set to the attribute timestamp and
1111 * NRMODIFIED is set if the two values differ.
1113 * WARNING: the mtime loaded into vaper does not necessarily represent
1114 * n_mtime or n_attr.mtime due to NACC and NUPD.
1117 nfs_loadattrcache(struct vnode **vpp, struct mbuf **mdp, caddr_t *dposp,
1118 struct vattr *vaper, int lattr_flags)
1120 struct vnode *vp = *vpp;
1122 struct nfs_fattr *fp;
1131 struct timespec mtime;
1132 int v3 = NFS_ISV3(vp);
1135 t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
1136 if ((error = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, &cp2)) != 0)
1138 fp = (struct nfs_fattr *)cp2;
1140 vtyp = nfsv3tov_type(fp->fa_type);
1141 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1142 rdev = makeudev(fxdr_unsigned(int, fp->fa3_rdev.specdata1),
1143 fxdr_unsigned(int, fp->fa3_rdev.specdata2));
1144 fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
1146 vtyp = nfsv2tov_type(fp->fa_type);
1147 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1151 * The duplicate information returned in fa_type and fa_mode
1152 * is an ambiguity in the NFS version 2 protocol.
1154 * VREG should be taken literally as a regular file. If a
1155 * server intents to return some type information differently
1156 * in the upper bits of the mode field (e.g. for sockets, or
1157 * FIFOs), NFSv2 mandates fa_type to be VNON. Anyway, we
1158 * leave the examination of the mode bits even in the VREG
1159 * case to avoid breakage for bogus servers, but we make sure
1160 * that there are actually type bits set in the upper part of
1161 * fa_mode (and failing that, trust the va_type field).
1163 * NFSv3 cleared the issue, and requires fa_mode to not
1164 * contain any type information (while also introduing sockets
1165 * and FIFOs for fa_type).
1167 if (vtyp == VNON || (vtyp == VREG && (vmode & S_IFMT) != 0))
1168 vtyp = IFTOVT(vmode);
1169 rdev = fxdr_unsigned(int32_t, fp->fa2_rdev);
1170 fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
1173 * Really ugly NFSv2 kludge.
1175 if (vtyp == VCHR && rdev == (udev_t)0xffffffff)
1180 * If v_type == VNON it is a new node, so fill in the v_type,
1181 * n_mtime fields. Check to see if it represents a special
1182 * device, and if so, check for a possible alias. Once the
1183 * correct vnode has been obtained, fill in the rest of the
1187 if (vp->v_type != vtyp) {
1188 nfs_setvtype(vp, vtyp);
1189 if (vp->v_type == VFIFO) {
1190 vp->v_ops = &vp->v_mount->mnt_vn_fifo_ops;
1191 } else if (vp->v_type == VCHR || vp->v_type == VBLK) {
1192 vp->v_ops = &vp->v_mount->mnt_vn_spec_ops;
1193 addaliasu(vp, rdev);
1195 vp->v_ops = &vp->v_mount->mnt_vn_use_ops;
1197 np->n_mtime = mtime.tv_sec;
1198 } else if (np->n_mtime != mtime.tv_sec) {
1200 * If we haven't modified the file locally and the server
1201 * timestamp does not match, then the server probably
1202 * modified the file. We must flag this condition so
1203 * the proper syncnronization can be done. We do not
1204 * try to synchronize the state here because that
1205 * could lead to an endless recursion.
1207 * XXX loadattrcache can be set during the reply to a write,
1208 * before the write timestamp is properly processed. To
1209 * avoid unconditionally setting the rmodified bit (which
1210 * has the effect of flushing the cache), we only do this
1211 * check if the lmodified bit is not set.
1213 np->n_mtime = mtime.tv_sec;
1214 if ((lattr_flags & NFS_LATTR_NOMTIMECHECK) == 0)
1215 np->n_flag |= NRMODIFIED;
1218 vap->va_type = vtyp;
1219 vap->va_mode = (vmode & 07777);
1220 vap->va_rdev = rdev;
1221 vap->va_mtime = mtime;
1222 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
1224 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1225 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1226 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1227 vap->va_size = fxdr_hyper(&fp->fa3_size);
1228 vap->va_blocksize = NFS_FABLKSIZE;
1229 vap->va_bytes = fxdr_hyper(&fp->fa3_used);
1230 vap->va_fileid = fxdr_unsigned(int32_t,
1231 fp->fa3_fileid.nfsuquad[1]);
1232 fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
1233 fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
1235 vap->va_filerev = 0;
1237 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1238 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1239 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1240 vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
1241 vap->va_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize);
1242 vap->va_bytes = (u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks)
1244 vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
1245 fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
1247 vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t,
1248 fp->fa2_ctime.nfsv2_sec);
1249 vap->va_ctime.tv_nsec = 0;
1250 vap->va_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec);
1251 vap->va_filerev = 0;
1253 np->n_attrstamp = time_second;
1254 if (vap->va_size != np->n_size) {
1255 if (vap->va_type == VREG) {
1256 if ((lattr_flags & NFS_LATTR_NOSHRINK) &&
1257 vap->va_size < np->n_size) {
1259 * We've been told not to shrink the file;
1260 * zero np->n_attrstamp to indicate that
1261 * the attributes are stale.
1263 * This occurs primarily due to recursive
1264 * NFS ops that are executed during periods
1265 * where we cannot safely reduce the size of
1268 * Additionally, write rpcs are broken down
1269 * into buffers and np->n_size is
1270 * pre-extended. Setting NRMODIFIED here
1271 * can result in n_size getting reset to a
1272 * lower value, which is NOT what we want.
1273 * XXX this needs to be cleaned up a lot
1276 vap->va_size = np->n_size;
1277 np->n_attrstamp = 0;
1278 if ((np->n_flag & NLMODIFIED) == 0)
1279 np->n_flag |= NRMODIFIED;
1280 } else if (np->n_flag & NLMODIFIED) {
1282 * We've modified the file: Use the larger
1283 * of our size, and the server's size. At
1284 * this point the cache coherency is all
1285 * shot to hell. To try to handle multiple
1286 * clients appending to the file at the same
1287 * time mark that the server has changed
1288 * the file if the server's notion of the
1289 * file size is larger then our notion.
1291 * XXX this needs work.
1293 if (vap->va_size < np->n_size) {
1294 vap->va_size = np->n_size;
1296 np->n_size = vap->va_size;
1297 np->n_flag |= NRMODIFIED;
1301 * Someone changed the file's size on the
1302 * server and there are no local changes
1303 * to get in the way, set the size and mark
1306 np->n_size = vap->va_size;
1307 np->n_flag |= NRMODIFIED;
1309 vnode_pager_setsize(vp, np->n_size);
1311 np->n_size = vap->va_size;
1314 if (vaper != NULL) {
1315 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
1316 if (np->n_flag & NCHG) {
1317 if (np->n_flag & NACC)
1318 vaper->va_atime = np->n_atim;
1319 if (np->n_flag & NUPD)
1320 vaper->va_mtime = np->n_mtim;
1327 #include <sys/sysctl.h>
1328 SYSCTL_DECL(_vfs_nfs);
1329 static int nfs_acdebug;
1330 SYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, "");
1334 * Check the time stamp
1335 * If the cache is valid, copy contents to *vap and return 0
1336 * otherwise return an error
1339 nfs_getattrcache(struct vnode *vp, struct vattr *vaper)
1343 struct nfsmount *nmp;
1348 nmp = VFSTONFS(vp->v_mount);
1351 * Dynamic timeout based on how recently the file was modified.
1352 * n_mtime is always valid.
1354 timeo = (get_approximate_time_t() - np->n_mtime) / 60;
1358 kprintf("nfs_getattrcache: initial timeo = %d\n", timeo);
1361 if (vap->va_type == VDIR) {
1362 if ((np->n_flag & NLMODIFIED) || timeo < nmp->nm_acdirmin)
1363 timeo = nmp->nm_acdirmin;
1364 else if (timeo > nmp->nm_acdirmax)
1365 timeo = nmp->nm_acdirmax;
1367 if ((np->n_flag & NLMODIFIED) || timeo < nmp->nm_acregmin)
1368 timeo = nmp->nm_acregmin;
1369 else if (timeo > nmp->nm_acregmax)
1370 timeo = nmp->nm_acregmax;
1374 if (nfs_acdebug > 2)
1375 kprintf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n",
1376 nmp->nm_acregmin, nmp->nm_acregmax,
1377 nmp->nm_acdirmin, nmp->nm_acdirmax);
1380 kprintf("nfs_getattrcache: age = %d; final timeo = %d\n",
1381 (int)(time_second - np->n_attrstamp), timeo);
1384 if (np->n_attrstamp == 0 || (time_second - np->n_attrstamp) >= timeo) {
1385 nfsstats.attrcache_misses++;
1388 nfsstats.attrcache_hits++;
1391 * Our attribute cache can be stale due to modifications made on
1392 * this host. XXX this is a bad hack. We need a more deterministic
1393 * means of finding out which np fields are valid verses attr cache
1394 * fields. We really should update the vattr info on the fly when
1395 * making local changes.
1397 if (vap->va_size != np->n_size) {
1398 if (vap->va_type == VREG) {
1399 if (np->n_flag & NLMODIFIED) {
1400 if (vap->va_size < np->n_size)
1401 vap->va_size = np->n_size;
1403 np->n_size = vap->va_size;
1405 np->n_size = vap->va_size;
1407 vnode_pager_setsize(vp, np->n_size);
1409 np->n_size = vap->va_size;
1412 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
1413 if (np->n_flag & NCHG) {
1414 if (np->n_flag & NACC)
1415 vaper->va_atime = np->n_atim;
1416 if (np->n_flag & NUPD)
1417 vaper->va_mtime = np->n_mtim;
1422 #ifndef NFS_NOSERVER
1425 * Set up nameidata for a lookup() call and do it.
1427 * If pubflag is set, this call is done for a lookup operation on the
1428 * public filehandle. In that case we allow crossing mountpoints and
1429 * absolute pathnames. However, the caller is expected to check that
1430 * the lookup result is within the public fs, and deny access if
1433 * dirp may be set whether an error is returned or not, and must be
1434 * released by the caller.
1436 * On return nd->nl_nch usually points to the target ncp, which may represent
1439 * NOTE: the caller must call nlookup_done(nd) unconditionally on return
1443 nfs_namei(struct nlookupdata *nd, struct ucred *cred, int nameiop,
1444 struct vnode **dvpp, struct vnode **vpp,
1445 fhandle_t *fhp, int len,
1446 struct nfssvc_sock *slp, struct sockaddr *nam, struct mbuf **mdp,
1447 caddr_t *dposp, struct vnode **dirpp, struct thread *td,
1448 int kerbflag, int pubflag)
1453 char *fromcp, *tocp, *cp;
1455 struct nchandle nch;
1459 namebuf = objcache_get(namei_oc, M_WAITOK);
1464 * Copy the name from the mbuf list to namebuf.
1469 rem = mtod(md, caddr_t) + md->m_len - fromcp;
1470 for (i = 0; i < len; i++) {
1477 fromcp = mtod(md, caddr_t);
1480 if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) {
1484 *tocp++ = *fromcp++;
1490 len = nfsm_rndup(len)-len;
1494 else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
1499 * Extract and set starting directory. The returned dp is refd
1502 error = nfsrv_fhtovp(fhp, FALSE, &dp, cred, slp,
1503 nam, &rdonly, kerbflag, pubflag);
1506 if (dp->v_type != VDIR) {
1513 * Set return directory. Reference to dp is implicitly transfered
1514 * to the returned pointer. This must be set before we potentially
1521 * Oh joy. For WebNFS, handle those pesky '%' escapes,
1522 * and the 'native path' indicator.
1524 cp = objcache_get(namei_oc, M_WAITOK);
1527 if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) {
1528 switch ((unsigned char)*fromcp) {
1529 case WEBNFS_NATIVE_CHAR:
1531 * 'Native' path for us is the same
1532 * as a path according to the NFS spec,
1533 * just skip the escape char.
1538 * More may be added in the future, range 0x80-0xff
1542 objcache_put(namei_oc, cp);
1547 * Translate the '%' escapes, URL-style.
1549 while (*fromcp != '\0') {
1550 if (*fromcp == WEBNFS_ESC_CHAR) {
1551 if (fromcp[1] != '\0' && fromcp[2] != '\0') {
1553 *tocp++ = HEXSTRTOI(fromcp);
1558 objcache_put(namei_oc, cp);
1562 *tocp++ = *fromcp++;
1565 objcache_put(namei_oc, namebuf);
1570 * Setup for search. We need to get a start directory from dp. Note
1571 * that dp is ref'd, but we no longer 'own' the ref (*dirpp owns it).
1574 flags |= NLC_NFS_NOSOFTLINKTRAV;
1575 flags |= NLC_NOCROSSMOUNT;
1578 flags |= NLC_NFS_RDONLY;
1579 if (nameiop == NAMEI_CREATE || nameiop == NAMEI_RENAME)
1580 flags |= NLC_CREATE;
1583 * We need a starting ncp from the directory vnode dp. dp must not
1584 * be locked. The returned ncp will be refd but not locked.
1586 * If no suitable ncp is found we instruct cache_fromdvp() to create
1587 * one. If this fails the directory has probably been removed while
1588 * the target was chdir'd into it and any further lookup will fail.
1590 if ((error = cache_fromdvp(dp, cred, 1, &nch)) != 0)
1592 nlookup_init_raw(nd, namebuf, UIO_SYSSPACE, flags, cred, &nch);
1596 * Ok, do the lookup.
1598 error = nlookup(nd);
1601 * If no error occured return the requested dvpp and vpp. If
1602 * NLC_CREATE was specified nd->nl_nch may represent a negative
1603 * cache hit in which case we do not attempt to obtain the vp.
1607 if (nd->nl_nch.ncp->nc_parent) {
1609 nch.ncp = nch.ncp->nc_parent;
1610 error = cache_vget(&nch, nd->nl_cred,
1611 LK_EXCLUSIVE, dvpp);
1616 if (vpp && nd->nl_nch.ncp->nc_vp) {
1617 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, vpp);
1620 if (dvpp && *dvpp) {
1635 objcache_put(namei_oc, namebuf);
1640 * A fiddled version of m_adj() that ensures null fill to a long
1641 * boundary and only trims off the back end
1644 nfsm_adj(struct mbuf *mp, int len, int nul)
1651 * Trim from tail. Scan the mbuf chain,
1652 * calculating its length and finding the last mbuf.
1653 * If the adjustment only affects this mbuf, then just
1654 * adjust and return. Otherwise, rescan and truncate
1655 * after the remaining size.
1661 if (m->m_next == (struct mbuf *)0)
1665 if (m->m_len > len) {
1668 cp = mtod(m, caddr_t)+m->m_len-nul;
1669 for (i = 0; i < nul; i++)
1678 * Correct length for chain is "count".
1679 * Find the mbuf with last data, adjust its length,
1680 * and toss data from remaining mbufs on chain.
1682 for (m = mp; m; m = m->m_next) {
1683 if (m->m_len >= count) {
1686 cp = mtod(m, caddr_t)+m->m_len-nul;
1687 for (i = 0; i < nul; i++)
1694 for (m = m->m_next;m;m = m->m_next)
1699 * Make these functions instead of macros, so that the kernel text size
1700 * doesn't get too big...
1703 nfsm_srvwcc(struct nfsrv_descript *nfsd, int before_ret,
1704 struct vattr *before_vap, int after_ret, struct vattr *after_vap,
1705 struct mbuf **mbp, char **bposp)
1707 struct mbuf *mb = *mbp, *mb2;
1708 char *bpos = *bposp;
1712 * before_ret is 0 if before_vap is valid, non-zero if it isn't.
1715 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1718 nfsm_build(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
1720 txdr_hyper(before_vap->va_size, tl);
1722 txdr_nfsv3time(&(before_vap->va_mtime), tl);
1724 txdr_nfsv3time(&(before_vap->va_ctime), tl);
1728 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp);
1732 nfsm_srvpostopattr(struct nfsrv_descript *nfsd, int after_ret,
1733 struct vattr *after_vap, struct mbuf **mbp, char **bposp)
1735 struct mbuf *mb = *mbp, *mb2;
1736 char *bpos = *bposp;
1738 struct nfs_fattr *fp;
1741 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1744 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR);
1746 fp = (struct nfs_fattr *)tl;
1747 nfsm_srvfattr(nfsd, after_vap, fp);
1754 nfsm_srvfattr(struct nfsrv_descript *nfsd, struct vattr *vap,
1755 struct nfs_fattr *fp)
1758 fp->fa_nlink = txdr_unsigned(vap->va_nlink);
1759 fp->fa_uid = txdr_unsigned(vap->va_uid);
1760 fp->fa_gid = txdr_unsigned(vap->va_gid);
1761 if (nfsd->nd_flag & ND_NFSV3) {
1762 fp->fa_type = vtonfsv3_type(vap->va_type);
1763 fp->fa_mode = vtonfsv3_mode(vap->va_mode);
1764 txdr_hyper(vap->va_size, &fp->fa3_size);
1765 txdr_hyper(vap->va_bytes, &fp->fa3_used);
1766 fp->fa3_rdev.specdata1 = txdr_unsigned(umajor(vap->va_rdev));
1767 fp->fa3_rdev.specdata2 = txdr_unsigned(uminor(vap->va_rdev));
1768 fp->fa3_fsid.nfsuquad[0] = 0;
1769 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid);
1770 fp->fa3_fileid.nfsuquad[0] = 0;
1771 fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(vap->va_fileid);
1772 txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime);
1773 txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime);
1774 txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime);
1776 fp->fa_type = vtonfsv2_type(vap->va_type);
1777 fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1778 fp->fa2_size = txdr_unsigned(vap->va_size);
1779 fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize);
1780 if (vap->va_type == VFIFO)
1781 fp->fa2_rdev = 0xffffffff;
1783 fp->fa2_rdev = txdr_unsigned(vap->va_rdev);
1784 fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE);
1785 fp->fa2_fsid = txdr_unsigned(vap->va_fsid);
1786 fp->fa2_fileid = txdr_unsigned(vap->va_fileid);
1787 txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime);
1788 txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime);
1789 txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime);
1794 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
1795 * - look up fsid in mount list (if not found ret error)
1796 * - get vp and export rights by calling VFS_FHTOVP()
1797 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
1798 * - if not lockflag unlock it with vn_unlock()
1801 nfsrv_fhtovp(fhandle_t *fhp, int lockflag, struct vnode **vpp,
1802 struct ucred *cred, struct nfssvc_sock *slp, struct sockaddr *nam,
1803 int *rdonlyp, int kerbflag, int pubflag)
1807 struct ucred *credanon;
1809 #ifdef MNT_EXNORESPORT /* XXX needs mountd and /etc/exports help yet */
1810 struct sockaddr_int *saddr;
1813 *vpp = (struct vnode *)0;
1815 if (nfs_ispublicfh(fhp)) {
1816 if (!pubflag || !nfs_pub.np_valid)
1818 fhp = &nfs_pub.np_handle;
1821 mp = vfs_getvfs(&fhp->fh_fsid);
1824 error = VFS_CHECKEXP(mp, nam, &exflags, &credanon);
1827 error = VFS_FHTOVP(mp, &fhp->fh_fid, vpp);
1830 #ifdef MNT_EXNORESPORT
1831 if (!(exflags & (MNT_EXNORESPORT|MNT_EXPUBLIC))) {
1832 saddr = (struct sockaddr_in *)nam;
1833 if (saddr->sin_family == AF_INET &&
1834 ntohs(saddr->sin_port) >= IPPORT_RESERVED) {
1837 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1842 * Check/setup credentials.
1844 if (exflags & MNT_EXKERB) {
1848 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1850 } else if (kerbflag) {
1853 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1854 } else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1855 cred->cr_uid = credanon->cr_uid;
1856 for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++)
1857 cred->cr_groups[i] = credanon->cr_groups[i];
1858 cred->cr_ngroups = i;
1860 if (exflags & MNT_EXRDONLY)
1872 * WebNFS: check if a filehandle is a public filehandle. For v3, this
1873 * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has
1874 * transformed this to all zeroes in both cases, so check for it.
1877 nfs_ispublicfh(fhandle_t *fhp)
1879 char *cp = (char *)fhp;
1882 for (i = 0; i < NFSX_V3FH; i++)
1888 #endif /* NFS_NOSERVER */
1890 * This function compares two net addresses by family and returns TRUE
1891 * if they are the same host.
1892 * If there is any doubt, return FALSE.
1893 * The AF_INET family is handled as a special case so that address mbufs
1894 * don't need to be saved to store "struct in_addr", which is only 4 bytes.
1897 netaddr_match(int family, union nethostaddr *haddr, struct sockaddr *nam)
1899 struct sockaddr_in *inetaddr;
1903 inetaddr = (struct sockaddr_in *)nam;
1904 if (inetaddr->sin_family == AF_INET &&
1905 inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
1914 static nfsuint64 nfs_nullcookie = { { 0, 0 } };
1916 * This function finds the directory cookie that corresponds to the
1917 * logical byte offset given.
1920 nfs_getcookie(struct nfsnode *np, off_t off, int add)
1922 struct nfsdmap *dp, *dp2;
1925 pos = (uoff_t)off / NFS_DIRBLKSIZ;
1926 if (pos == 0 || off < 0) {
1929 panic("nfs getcookie add at <= 0");
1931 return (&nfs_nullcookie);
1934 dp = np->n_cookies.lh_first;
1937 MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap),
1938 M_NFSDIROFF, M_WAITOK);
1939 dp->ndm_eocookie = 0;
1940 LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
1942 return ((nfsuint64 *)0);
1944 while (pos >= NFSNUMCOOKIES) {
1945 pos -= NFSNUMCOOKIES;
1946 if (dp->ndm_list.le_next) {
1947 if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
1948 pos >= dp->ndm_eocookie)
1949 return ((nfsuint64 *)0);
1950 dp = dp->ndm_list.le_next;
1952 MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap),
1953 M_NFSDIROFF, M_WAITOK);
1954 dp2->ndm_eocookie = 0;
1955 LIST_INSERT_AFTER(dp, dp2, ndm_list);
1958 return ((nfsuint64 *)0);
1960 if (pos >= dp->ndm_eocookie) {
1962 dp->ndm_eocookie = pos + 1;
1964 return ((nfsuint64 *)0);
1966 return (&dp->ndm_cookies[pos]);
1970 * Invalidate cached directory information, except for the actual directory
1971 * blocks (which are invalidated separately).
1972 * Done mainly to avoid the use of stale offset cookies.
1975 nfs_invaldir(struct vnode *vp)
1977 struct nfsnode *np = VTONFS(vp);
1980 if (vp->v_type != VDIR)
1981 panic("nfs: invaldir not dir");
1983 np->n_direofoffset = 0;
1984 np->n_cookieverf.nfsuquad[0] = 0;
1985 np->n_cookieverf.nfsuquad[1] = 0;
1986 if (np->n_cookies.lh_first)
1987 np->n_cookies.lh_first->ndm_eocookie = 0;
1991 * Set the v_type field for an NFS client's vnode and initialize for
1992 * buffer cache operations if necessary.
1995 nfs_setvtype(struct vnode *vp, enum vtype vtyp)
2003 vinitvmio(vp, 0); /* needs VMIO, size not yet known */
2011 * The write verifier has changed (probably due to a server reboot), so all
2012 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
2013 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
2014 * and B_CLUSTEROK flags. Once done the new write verifier can be set for the
2017 * B_CLUSTEROK must be cleared along with B_NEEDCOMMIT because stage 1 data
2018 * writes are not clusterable.
2021 static int nfs_clearcommit_bp(struct buf *bp, void *data __unused);
2024 nfs_clearcommit(struct mount *mp)
2026 struct vnode *vp, *nvp;
2029 lwkt_gettoken(&ilock, &mntvnode_token);
2031 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp; vp = nvp) {
2032 nvp = TAILQ_NEXT(vp, v_nmntvnodes); /* ZZZ */
2033 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
2034 nfs_clearcommit_bp, NULL);
2037 lwkt_reltoken(&ilock);
2041 nfs_clearcommit_bp(struct buf *bp, void *data __unused)
2043 if (BUF_REFCNT(bp) == 0 &&
2044 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2045 == (B_DELWRI | B_NEEDCOMMIT)) {
2046 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
2051 #ifndef NFS_NOSERVER
2053 * Map errnos to NFS error numbers. For Version 3 also filter out error
2054 * numbers not specified for the associated procedure.
2057 nfsrv_errmap(struct nfsrv_descript *nd, int err)
2059 short *defaulterrp, *errp;
2061 if (nd->nd_flag & ND_NFSV3) {
2062 if (nd->nd_procnum <= NFSPROC_COMMIT) {
2063 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
2067 else if (*errp > err)
2070 return ((int)*defaulterrp);
2072 return (err & 0xffff);
2075 return ((int)nfsrv_v2errmap[err - 1]);
2080 * Sort the group list in increasing numerical order.
2081 * (Insertion sort by Chris Torek, who was grossed out by the bubble sort
2082 * that used to be here.)
2085 nfsrvw_sort(gid_t *list, int num)
2090 /* Insertion sort. */
2091 for (i = 1; i < num; i++) {
2093 /* find correct slot for value v, moving others up */
2094 for (j = i; --j >= 0 && v < list[j];)
2095 list[j + 1] = list[j];
2101 * copy credentials making sure that the result can be compared with bcmp().
2104 nfsrv_setcred(struct ucred *incred, struct ucred *outcred)
2108 bzero((caddr_t)outcred, sizeof (struct ucred));
2109 outcred->cr_ref = 1;
2110 outcred->cr_uid = incred->cr_uid;
2111 outcred->cr_ngroups = incred->cr_ngroups;
2112 for (i = 0; i < incred->cr_ngroups; i++)
2113 outcred->cr_groups[i] = incred->cr_groups[i];
2114 nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups);
2116 #endif /* NFS_NOSERVER */