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.26 2005/03/16 22:17:59 dillon 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>
64 #include <vm/vm_object.h>
65 #include <vm/vm_extern.h>
66 #include <vm/vm_zone.h>
76 #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, nqnfs_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 nqtimerhead nqtimerhead;
111 struct nqfhhashhead *nqfhhashtbl;
114 static void (*nfs_prev_lease_updatetime) (int);
115 static int nfs_prev_nfssvc_sy_narg;
116 static sy_call_t *nfs_prev_nfssvc_sy_call;
120 static int (*nfs_prev_vop_lease_check)(struct vop_lease_args *);
123 * Mapping of old NFS Version 2 RPC numbers to generic numbers.
125 int nfsv3_procid[NFS_NPROCS] = {
154 #endif /* NFS_NOSERVER */
156 * and the reverse mapping from generic to Version 2 procedure numbers
158 int nfsv2_procid[NFS_NPROCS] = {
189 * Maps errno values to nfs error numbers.
190 * Use NFSERR_IO as the catch all for ones not specifically defined in
193 static u_char nfsrv_v2errmap[ELAST] = {
194 NFSERR_PERM, NFSERR_NOENT, NFSERR_IO, NFSERR_IO, NFSERR_IO,
195 NFSERR_NXIO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
196 NFSERR_IO, NFSERR_IO, NFSERR_ACCES, NFSERR_IO, NFSERR_IO,
197 NFSERR_IO, NFSERR_EXIST, NFSERR_IO, NFSERR_NODEV, NFSERR_NOTDIR,
198 NFSERR_ISDIR, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
199 NFSERR_IO, NFSERR_FBIG, NFSERR_NOSPC, NFSERR_IO, NFSERR_ROFS,
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_IO, NFSERR_IO, NFSERR_IO,
203 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
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_NAMETOL, NFSERR_IO, NFSERR_IO,
207 NFSERR_NOTEMPTY, NFSERR_IO, NFSERR_IO, NFSERR_DQUOT, NFSERR_STALE,
208 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
209 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
210 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
211 NFSERR_IO /* << Last is 86 */
215 * Maps errno values to nfs error numbers.
216 * Although it is not obvious whether or not NFS clients really care if
217 * a returned error value is in the specified list for the procedure, the
218 * safest thing to do is filter them appropriately. For Version 2, the
219 * X/Open XNFS document is the only specification that defines error values
220 * for each RPC (The RFC simply lists all possible error values for all RPCs),
221 * so I have decided to not do this for Version 2.
222 * The first entry is the default error return and the rest are the valid
223 * errors for that RPC in increasing numeric order.
225 static short nfsv3err_null[] = {
230 static short nfsv3err_getattr[] = {
239 static short nfsv3err_setattr[] = {
255 static short nfsv3err_lookup[] = {
268 static short nfsv3err_access[] = {
277 static short nfsv3err_readlink[] = {
289 static short nfsv3err_read[] = {
301 static short nfsv3err_write[] = {
316 static short nfsv3err_create[] = {
333 static short nfsv3err_mkdir[] = {
350 static short nfsv3err_symlink[] = {
367 static short nfsv3err_mknod[] = {
385 static short nfsv3err_remove[] = {
399 static short nfsv3err_rmdir[] = {
417 static short nfsv3err_rename[] = {
440 static short nfsv3err_link[] = {
460 static short nfsv3err_readdir[] = {
473 static short nfsv3err_readdirplus[] = {
487 static short nfsv3err_fsstat[] = {
496 static short nfsv3err_fsinfo[] = {
504 static short nfsv3err_pathconf[] = {
512 static short nfsv3err_commit[] = {
521 static short *nfsrv_v3errmap[] = {
539 nfsv3err_readdirplus,
546 #endif /* NFS_NOSERVER */
548 extern struct nfsrtt nfsrtt;
549 extern time_t nqnfsstarttime;
550 extern int nqsrv_clockskew;
551 extern int nqsrv_writeslack;
552 extern int nqsrv_maxlease;
553 extern struct nfsstats nfsstats;
554 extern int nqnfs_piggy[NFS_NPROCS];
555 extern nfstype nfsv2_type[9];
556 extern nfstype nfsv3_type[9];
557 extern struct nfsnodehashhead *nfsnodehashtbl;
558 extern u_long nfsnodehash;
561 extern int nfssvc(struct proc *, struct nfssvc_args *, int *);
563 LIST_HEAD(nfsnodehashhead, nfsnode);
571 return ((u_quad_t)tv.tv_sec * 1000000 + (u_quad_t)tv.tv_usec);
575 * Create the header for an rpc request packet
576 * The hsiz is the size of the rest of the nfs request header.
577 * (just used to decide if a cluster is a good idea)
580 nfsm_reqh(struct vnode *vp, u_long procid, int hsiz, caddr_t *bposp)
586 struct nfsmount *nmp;
589 MGET(mb, MB_WAIT, MT_DATA);
590 if (hsiz >= MINCLSIZE)
593 bpos = mtod(mb, caddr_t);
596 * For NQNFS, add lease request.
599 nmp = VFSTONFS(vp->v_mount);
600 if (nmp->nm_flag & NFSMNT_NQNFS) {
601 nqflag = NQNFS_NEEDLEASE(vp, procid);
603 nfsm_build(tl, u_int32_t *, 2*NFSX_UNSIGNED);
604 *tl++ = txdr_unsigned(nqflag);
605 *tl = txdr_unsigned(nmp->nm_leaseterm);
607 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
612 /* Finally, return values */
618 * Build the RPC header and fill in the authorization info.
619 * The authorization string argument is only used when the credentials
620 * come from outside of the kernel.
621 * Returns the head of the mbuf list.
624 nfsm_rpchead(struct ucred *cr, int nmflag, int procid, int auth_type,
625 int auth_len, char *auth_str, int verf_len, char *verf_str,
626 struct mbuf *mrest, int mrest_len, struct mbuf **mbp,
633 struct mbuf *mreq, *mb2;
634 int siz, grpsiz, authsiz;
636 authsiz = nfsm_rndup(auth_len);
637 MGETHDR(mb, MB_WAIT, MT_DATA);
638 if ((authsiz + 10 * NFSX_UNSIGNED) >= MINCLSIZE) {
640 } else if ((authsiz + 10 * NFSX_UNSIGNED) < MHLEN) {
641 MH_ALIGN(mb, authsiz + 10 * NFSX_UNSIGNED);
643 MH_ALIGN(mb, 8 * NFSX_UNSIGNED);
647 bpos = mtod(mb, caddr_t);
650 * First the RPC header.
652 nfsm_build(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
654 /* Get a pretty random xid to start with */
658 * Skip zero xid if it should ever happen.
663 *tl++ = *xidp = txdr_unsigned(nfs_xid);
666 if (nmflag & NFSMNT_NQNFS) {
667 *tl++ = txdr_unsigned(NQNFS_PROG);
668 *tl++ = txdr_unsigned(NQNFS_VER3);
670 *tl++ = txdr_unsigned(NFS_PROG);
671 if (nmflag & NFSMNT_NFSV3)
672 *tl++ = txdr_unsigned(NFS_VER3);
674 *tl++ = txdr_unsigned(NFS_VER2);
676 if (nmflag & NFSMNT_NFSV3)
677 *tl++ = txdr_unsigned(procid);
679 *tl++ = txdr_unsigned(nfsv2_procid[procid]);
682 * And then the authorization cred.
684 *tl++ = txdr_unsigned(auth_type);
685 *tl = txdr_unsigned(authsiz);
688 nfsm_build(tl, u_int32_t *, auth_len);
689 *tl++ = 0; /* stamp ?? */
690 *tl++ = 0; /* NULL hostname */
691 *tl++ = txdr_unsigned(cr->cr_uid);
692 *tl++ = txdr_unsigned(cr->cr_groups[0]);
693 grpsiz = (auth_len >> 2) - 5;
694 *tl++ = txdr_unsigned(grpsiz);
695 for (i = 1; i <= grpsiz; i++)
696 *tl++ = txdr_unsigned(cr->cr_groups[i]);
701 if (M_TRAILINGSPACE(mb) == 0) {
702 MGET(mb2, MB_WAIT, MT_DATA);
703 if (siz >= MINCLSIZE)
704 MCLGET(mb2, MB_WAIT);
708 bpos = mtod(mb, caddr_t);
710 i = min(siz, M_TRAILINGSPACE(mb));
711 bcopy(auth_str, bpos, i);
717 if ((siz = (nfsm_rndup(auth_len) - auth_len)) > 0) {
718 for (i = 0; i < siz; i++)
726 * And the verifier...
728 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
730 *tl++ = txdr_unsigned(RPCAUTH_KERB4);
731 *tl = txdr_unsigned(verf_len);
734 if (M_TRAILINGSPACE(mb) == 0) {
735 MGET(mb2, MB_WAIT, MT_DATA);
736 if (siz >= MINCLSIZE)
737 MCLGET(mb2, MB_WAIT);
741 bpos = mtod(mb, caddr_t);
743 i = min(siz, M_TRAILINGSPACE(mb));
744 bcopy(verf_str, bpos, i);
750 if ((siz = (nfsm_rndup(verf_len) - verf_len)) > 0) {
751 for (i = 0; i < siz; i++)
756 *tl++ = txdr_unsigned(RPCAUTH_NULL);
760 mreq->m_pkthdr.len = authsiz + 10 * NFSX_UNSIGNED + mrest_len;
761 mreq->m_pkthdr.rcvif = (struct ifnet *)0;
767 * copies mbuf chain to the uio scatter/gather list
770 nfsm_mbuftouio(struct mbuf **mrep, struct uio *uiop, int siz, caddr_t *dpos)
772 char *mbufcp, *uiocp;
780 len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
781 rem = nfsm_rndup(siz)-siz;
783 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
785 left = uiop->uio_iov->iov_len;
786 uiocp = uiop->uio_iov->iov_base;
795 mbufcp = mtod(mp, caddr_t);
798 xfer = (left > len) ? len : left;
801 if (uiop->uio_iov->iov_op != NULL)
802 (*(uiop->uio_iov->iov_op))
803 (mbufcp, uiocp, xfer);
806 if (uiop->uio_segflg == UIO_SYSSPACE)
807 bcopy(mbufcp, uiocp, xfer);
809 copyout(mbufcp, uiocp, xfer);
814 uiop->uio_offset += xfer;
815 uiop->uio_resid -= xfer;
817 if (uiop->uio_iov->iov_len <= siz) {
821 uiop->uio_iov->iov_base += uiosiz;
822 uiop->uio_iov->iov_len -= uiosiz;
830 error = nfs_adv(mrep, dpos, rem, len);
838 * copies a uio scatter/gather list to an mbuf chain.
839 * NOTE: can ony handle iovcnt == 1
842 nfsm_uiotombuf(struct uio *uiop, struct mbuf **mq, int siz, caddr_t *bpos)
845 struct mbuf *mp, *mp2;
846 int xfer, left, mlen;
847 int uiosiz, clflg, rem;
851 if (uiop->uio_iovcnt != 1)
852 panic("nfsm_uiotombuf: iovcnt != 1");
855 if (siz > MLEN) /* or should it >= MCLBYTES ?? */
859 rem = nfsm_rndup(siz)-siz;
862 left = uiop->uio_iov->iov_len;
863 uiocp = uiop->uio_iov->iov_base;
868 mlen = M_TRAILINGSPACE(mp);
870 MGET(mp, MB_WAIT, MT_DATA);
876 mlen = M_TRAILINGSPACE(mp);
878 xfer = (left > mlen) ? mlen : left;
881 if (uiop->uio_iov->iov_op != NULL)
882 (*(uiop->uio_iov->iov_op))
883 (uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
886 if (uiop->uio_segflg == UIO_SYSSPACE)
887 bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
889 copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
893 uiop->uio_offset += xfer;
894 uiop->uio_resid -= xfer;
896 uiop->uio_iov->iov_base += uiosiz;
897 uiop->uio_iov->iov_len -= uiosiz;
901 if (rem > M_TRAILINGSPACE(mp)) {
902 MGET(mp, MB_WAIT, MT_DATA);
906 cp = mtod(mp, caddr_t)+mp->m_len;
907 for (left = 0; left < rem; left++)
912 *bpos = mtod(mp, caddr_t)+mp->m_len;
918 * Help break down an mbuf chain by setting the first siz bytes contiguous
919 * pointed to by returned val.
920 * This is used by the macros nfsm_dissect and nfsm_dissecton for tough
921 * cases. (The macros use the vars. dpos and dpos2)
924 nfsm_disct(struct mbuf **mdp, caddr_t *dposp, int siz, int left, caddr_t *cp2)
926 struct mbuf *mp, *mp2;
932 *mdp = mp = mp->m_next;
936 *dposp = mtod(mp, caddr_t);
941 } else if (mp->m_next == NULL) {
943 } else if (siz > MHLEN) {
944 panic("nfs S too big");
946 MGET(mp2, MB_WAIT, MT_DATA);
947 mp2->m_next = mp->m_next;
951 *cp2 = p = mtod(mp, caddr_t);
952 bcopy(*dposp, p, left); /* Copy what was left */
956 /* Loop around copying up the siz2 bytes */
960 xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2;
962 bcopy(mtod(mp2, caddr_t), p, xfer);
973 *dposp = mtod(mp2, caddr_t);
979 * Advance the position in the mbuf chain.
982 nfs_adv(struct mbuf **mdp, caddr_t *dposp, int offs, int left)
997 *dposp = mtod(m, caddr_t)+offs;
1002 * Copy a string into mbufs for the hard cases...
1005 nfsm_strtmbuf(struct mbuf **mb, char **bpos, const char *cp, long siz)
1007 struct mbuf *m1 = NULL, *m2;
1008 long left, xfer, len, tlen;
1014 left = M_TRAILINGSPACE(m2);
1016 tl = ((u_int32_t *)(*bpos));
1017 *tl++ = txdr_unsigned(siz);
1019 left -= NFSX_UNSIGNED;
1020 m2->m_len += NFSX_UNSIGNED;
1022 bcopy(cp, (caddr_t) tl, left);
1029 /* Loop around adding mbufs */
1031 MGET(m1, MB_WAIT, MT_DATA);
1033 MCLGET(m1, MB_WAIT);
1034 m1->m_len = NFSMSIZ(m1);
1037 tl = mtod(m1, u_int32_t *);
1040 *tl++ = txdr_unsigned(siz);
1041 m1->m_len -= NFSX_UNSIGNED;
1042 tlen = NFSX_UNSIGNED;
1045 if (siz < m1->m_len) {
1046 len = nfsm_rndup(siz);
1049 *(tl+(xfer>>2)) = 0;
1051 xfer = len = m1->m_len;
1053 bcopy(cp, (caddr_t) tl, xfer);
1054 m1->m_len = len+tlen;
1059 *bpos = mtod(m1, caddr_t)+m1->m_len;
1064 * Called once to initialize data structures...
1067 nfs_init(struct vfsconf *vfsp)
1071 callout_init(&nfs_timer_handle);
1072 nfsmount_zone = zinit("NFSMOUNT", sizeof(struct nfsmount), 0, 0, 1);
1074 nfs_mount_type = vfsp->vfc_typenum;
1076 rpc_vers = txdr_unsigned(RPC_VER2);
1077 rpc_call = txdr_unsigned(RPC_CALL);
1078 rpc_reply = txdr_unsigned(RPC_REPLY);
1079 rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
1080 rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
1081 rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
1082 rpc_autherr = txdr_unsigned(RPC_AUTHERR);
1083 rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
1084 rpc_auth_kerb = txdr_unsigned(RPCAUTH_KERB4);
1085 nfs_prog = txdr_unsigned(NFS_PROG);
1086 nqnfs_prog = txdr_unsigned(NQNFS_PROG);
1087 nfs_true = txdr_unsigned(TRUE);
1088 nfs_false = txdr_unsigned(FALSE);
1089 nfs_xdrneg1 = txdr_unsigned(-1);
1090 nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
1093 /* Ensure async daemons disabled */
1094 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
1095 nfs_iodwant[i] = NULL;
1096 nfs_iodmount[i] = (struct nfsmount *)0;
1098 nfs_nhinit(); /* Init the nfsnode table */
1099 #ifndef NFS_NOSERVER
1100 nfsrv_init(0); /* Init server data structures */
1101 nfsrv_initcache(); /* Init the server request cache */
1105 * Initialize the nqnfs server stuff.
1107 if (nqnfsstarttime == 0) {
1108 nqnfsstarttime = boottime.tv_sec + nqsrv_maxlease
1109 + nqsrv_clockskew + nqsrv_writeslack;
1110 NQLOADNOVRAM(nqnfsstarttime);
1111 CIRCLEQ_INIT(&nqtimerhead);
1112 nqfhhashtbl = hashinit(NQLCHSZ, M_NQLEASE, &nqfhhash);
1116 * Initialize reply list and start timer
1118 TAILQ_INIT(&nfs_reqq);
1123 * Set up lease_check and lease_updatetime so that other parts
1124 * of the system can call us, if we are loadable.
1126 #ifndef NFS_NOSERVER
1127 nfs_prev_vop_lease_check = default_vnode_vops->vop_lease;
1128 default_vnode_vops->vop_lease = nqnfs_vop_lease_check;
1130 nfs_prev_lease_updatetime = lease_updatetime;
1131 lease_updatetime = nfs_lease_updatetime;
1132 nfs_prev_nfssvc_sy_narg = sysent[SYS_nfssvc].sy_narg;
1133 sysent[SYS_nfssvc].sy_narg = 2;
1134 nfs_prev_nfssvc_sy_call = sysent[SYS_nfssvc].sy_call;
1135 sysent[SYS_nfssvc].sy_call = (sy_call_t *)nfssvc;
1137 nfs_pbuf_freecnt = nswbuf / 2 + 1;
1143 nfs_uninit(struct vfsconf *vfsp)
1145 callout_stop(&nfs_timer_handle);
1146 nfs_mount_type = -1;
1147 #ifndef NFS_NOSERVER
1148 default_vnode_vops->vop_lease = nfs_prev_vop_lease_check;
1150 lease_updatetime = nfs_prev_lease_updatetime;
1151 sysent[SYS_nfssvc].sy_narg = nfs_prev_nfssvc_sy_narg;
1152 sysent[SYS_nfssvc].sy_call = nfs_prev_nfssvc_sy_call;
1157 * Attribute cache routines.
1158 * nfs_loadattrcache() - loads or updates the cache contents from attributes
1159 * that are on the mbuf list
1160 * nfs_getattrcache() - returns valid attributes if found in cache, returns
1165 * Load the attribute cache (that lives in the nfsnode entry) with
1166 * the values on the mbuf list and
1168 * copy the attributes to *vaper
1171 nfs_loadattrcache(struct vnode **vpp, struct mbuf **mdp, caddr_t *dposp,
1172 struct vattr *vaper, int dontshrink)
1174 struct vnode *vp = *vpp;
1176 struct nfs_fattr *fp;
1185 struct timespec mtime;
1186 int v3 = NFS_ISV3(vp);
1189 t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
1190 if ((error = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, &cp2)) != 0)
1192 fp = (struct nfs_fattr *)cp2;
1194 vtyp = nfsv3tov_type(fp->fa_type);
1195 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1196 rdev = makeudev(fxdr_unsigned(int, fp->fa3_rdev.specdata1),
1197 fxdr_unsigned(int, fp->fa3_rdev.specdata2));
1198 fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
1200 vtyp = nfsv2tov_type(fp->fa_type);
1201 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1205 * The duplicate information returned in fa_type and fa_mode
1206 * is an ambiguity in the NFS version 2 protocol.
1208 * VREG should be taken literally as a regular file. If a
1209 * server intents to return some type information differently
1210 * in the upper bits of the mode field (e.g. for sockets, or
1211 * FIFOs), NFSv2 mandates fa_type to be VNON. Anyway, we
1212 * leave the examination of the mode bits even in the VREG
1213 * case to avoid breakage for bogus servers, but we make sure
1214 * that there are actually type bits set in the upper part of
1215 * fa_mode (and failing that, trust the va_type field).
1217 * NFSv3 cleared the issue, and requires fa_mode to not
1218 * contain any type information (while also introduing sockets
1219 * and FIFOs for fa_type).
1221 if (vtyp == VNON || (vtyp == VREG && (vmode & S_IFMT) != 0))
1222 vtyp = IFTOVT(vmode);
1223 rdev = fxdr_unsigned(int32_t, fp->fa2_rdev);
1224 fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
1227 * Really ugly NFSv2 kludge.
1229 if (vtyp == VCHR && rdev == (udev_t)0xffffffff)
1234 * If v_type == VNON it is a new node, so fill in the v_type,
1235 * n_mtime fields. Check to see if it represents a special
1236 * device, and if so, check for a possible alias. Once the
1237 * correct vnode has been obtained, fill in the rest of the
1241 if (vp->v_type != vtyp) {
1243 if (vp->v_type == VFIFO) {
1244 vp->v_ops = &vp->v_mount->mnt_vn_fifo_ops;
1245 } else if (vp->v_type == VCHR || vp->v_type == VBLK) {
1246 vp->v_ops = &vp->v_mount->mnt_vn_spec_ops;
1247 addaliasu(vp, rdev);
1249 vp->v_ops = &vp->v_mount->mnt_vn_use_ops;
1251 np->n_mtime = mtime.tv_sec;
1252 } else if ((np->n_flag & NMODIFIED) == 0 &&
1253 np->n_mtime != mtime.tv_sec) {
1255 * If we haven't modified the file locally update our notion
1256 * of the last-modified time based on the server's
1257 * information, otherwise certain cache timeout calculations
1258 * will break. If it has changed, set the NSIZECHANGED flag
1259 * to ensure that the buffer cache is flushed. Do NOT flush
1260 * the buffer cache in this routine.
1262 np->n_mtime = mtime.tv_sec;
1263 np->n_flag |= NSIZECHANGED;
1266 vap->va_type = vtyp;
1267 vap->va_mode = (vmode & 07777);
1268 vap->va_rdev = rdev;
1269 vap->va_mtime = mtime;
1270 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
1272 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1273 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1274 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1275 vap->va_size = fxdr_hyper(&fp->fa3_size);
1276 vap->va_blocksize = NFS_FABLKSIZE;
1277 vap->va_bytes = fxdr_hyper(&fp->fa3_used);
1278 vap->va_fileid = fxdr_unsigned(int32_t,
1279 fp->fa3_fileid.nfsuquad[1]);
1280 fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
1281 fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
1283 vap->va_filerev = 0;
1285 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1286 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1287 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1288 vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
1289 vap->va_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize);
1290 vap->va_bytes = (u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks)
1292 vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
1293 fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
1295 vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t,
1296 fp->fa2_ctime.nfsv2_sec);
1297 vap->va_ctime.tv_nsec = 0;
1298 vap->va_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec);
1299 vap->va_filerev = 0;
1301 np->n_attrstamp = time_second;
1302 if (vap->va_size != np->n_size) {
1303 if (vap->va_type == VREG) {
1304 if (dontshrink && vap->va_size < np->n_size) {
1306 * We've been told not to shrink the file;
1307 * zero np->n_attrstamp to indicate that
1308 * the attributes are stale.
1310 vap->va_size = np->n_size;
1311 np->n_attrstamp = 0;
1312 } else if (np->n_flag & NMODIFIED) {
1314 * We've modified the file: Use the larger
1315 * of our size, and the server's size.
1317 if (vap->va_size < np->n_size) {
1318 vap->va_size = np->n_size;
1320 np->n_size = vap->va_size;
1321 np->n_flag |= NSIZECHANGED;
1324 np->n_size = vap->va_size;
1325 np->n_flag |= NSIZECHANGED;
1327 vnode_pager_setsize(vp, np->n_size);
1329 np->n_size = vap->va_size;
1332 if (vaper != NULL) {
1333 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
1334 if (np->n_flag & NCHG) {
1335 if (np->n_flag & NACC)
1336 vaper->va_atime = np->n_atim;
1337 if (np->n_flag & NUPD)
1338 vaper->va_mtime = np->n_mtim;
1345 #include <sys/sysctl.h>
1346 SYSCTL_DECL(_vfs_nfs);
1347 static int nfs_acdebug;
1348 SYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, "");
1352 * Check the time stamp
1353 * If the cache is valid, copy contents to *vap and return 0
1354 * otherwise return an error
1357 nfs_getattrcache(struct vnode *vp, struct vattr *vaper)
1361 struct nfsmount *nmp;
1366 nmp = VFSTONFS(vp->v_mount);
1369 * Dynamic timeout based on how recently the file was modified.
1371 timeo = (get_approximate_time_t() - np->n_mtime) / 10;
1375 printf("nfs_getattrcache: initial timeo = %d\n", timeo);
1378 if (vap->va_type == VDIR) {
1379 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin)
1380 timeo = nmp->nm_acdirmin;
1381 else if (timeo > nmp->nm_acdirmax)
1382 timeo = nmp->nm_acdirmax;
1384 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin)
1385 timeo = nmp->nm_acregmin;
1386 else if (timeo > nmp->nm_acregmax)
1387 timeo = nmp->nm_acregmax;
1391 if (nfs_acdebug > 2)
1392 printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n",
1393 nmp->nm_acregmin, nmp->nm_acregmax,
1394 nmp->nm_acdirmin, nmp->nm_acdirmax);
1397 printf("nfs_getattrcache: age = %d; final timeo = %d\n",
1398 (int)(time_second - np->n_attrstamp), timeo);
1401 if (np->n_attrstamp == 0 || (time_second - np->n_attrstamp) >= timeo) {
1402 nfsstats.attrcache_misses++;
1405 nfsstats.attrcache_hits++;
1406 if (vap->va_size != np->n_size) {
1407 if (vap->va_type == VREG) {
1408 if (np->n_flag & NMODIFIED) {
1409 if (vap->va_size < np->n_size)
1410 vap->va_size = np->n_size;
1412 np->n_size = vap->va_size;
1414 np->n_size = vap->va_size;
1416 vnode_pager_setsize(vp, np->n_size);
1418 np->n_size = vap->va_size;
1421 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
1422 if (np->n_flag & NCHG) {
1423 if (np->n_flag & NACC)
1424 vaper->va_atime = np->n_atim;
1425 if (np->n_flag & NUPD)
1426 vaper->va_mtime = np->n_mtim;
1431 #ifndef NFS_NOSERVER
1434 * Set up nameidata for a lookup() call and do it.
1436 * If pubflag is set, this call is done for a lookup operation on the
1437 * public filehandle. In that case we allow crossing mountpoints and
1438 * absolute pathnames. However, the caller is expected to check that
1439 * the lookup result is within the public fs, and deny access if
1442 * dirp may be set whether an error is returned or not, and must be
1443 * released by the caller.
1445 * On return nd->nl_ncp usually points to the target ncp, which may represent
1448 * NOTE: the caller must call nlookup_done(nd) unconditionally on return
1452 nfs_namei(struct nlookupdata *nd, struct ucred *cred, int nameiop,
1453 struct vnode **dvpp, struct vnode **vpp,
1454 fhandle_t *fhp, int len,
1455 struct nfssvc_sock *slp, struct sockaddr *nam, struct mbuf **mdp,
1456 caddr_t *dposp, struct vnode **dirpp, struct thread *td,
1457 int kerbflag, int pubflag)
1462 char *fromcp, *tocp, *cp;
1464 struct namecache *ncp;
1468 namebuf = zalloc(namei_zone);
1473 * Copy the name from the mbuf list to namebuf.
1478 rem = mtod(md, caddr_t) + md->m_len - fromcp;
1479 for (i = 0; i < len; i++) {
1486 fromcp = mtod(md, caddr_t);
1489 if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) {
1493 *tocp++ = *fromcp++;
1499 len = nfsm_rndup(len)-len;
1503 else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
1508 * Extract and set starting directory. The returned dp is refd
1511 error = nfsrv_fhtovp(fhp, FALSE, &dp, cred, slp,
1512 nam, &rdonly, kerbflag, pubflag);
1515 if (dp->v_type != VDIR) {
1522 * Set return directory. Reference to dp is implicitly transfered
1523 * to the returned pointer. This must be set before we potentially
1530 * Oh joy. For WebNFS, handle those pesky '%' escapes,
1531 * and the 'native path' indicator.
1533 cp = zalloc(namei_zone);
1536 if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) {
1537 switch ((unsigned char)*fromcp) {
1538 case WEBNFS_NATIVE_CHAR:
1540 * 'Native' path for us is the same
1541 * as a path according to the NFS spec,
1542 * just skip the escape char.
1547 * More may be added in the future, range 0x80-0xff
1551 zfree(namei_zone, cp);
1556 * Translate the '%' escapes, URL-style.
1558 while (*fromcp != '\0') {
1559 if (*fromcp == WEBNFS_ESC_CHAR) {
1560 if (fromcp[1] != '\0' && fromcp[2] != '\0') {
1562 *tocp++ = HEXSTRTOI(fromcp);
1567 zfree(namei_zone, cp);
1571 *tocp++ = *fromcp++;
1574 zfree(namei_zone, namebuf);
1579 * Setup for search. We need to get a start directory from dp. Note
1580 * that dp is ref'd, but we no longer 'own' the ref (*dirpp owns it).
1583 flags |= NLC_NFS_NOSOFTLINKTRAV;
1584 flags |= NLC_NOCROSSMOUNT;
1587 flags |= NLC_NFS_RDONLY;
1588 if (nameiop == NAMEI_CREATE || nameiop == NAMEI_RENAME)
1589 flags |= NLC_CREATE;
1592 * We need a starting ncp from the directory vnode dp. dp must not
1593 * be locked. The returned ncp will be refd but not locked.
1595 * If no suitable ncp is found we instruct cache_fromdvp() to create
1596 * one. If this fails the directory has probably been removed while
1597 * the target was chdir'd into it and any further lookup will fail.
1599 if ((ncp = cache_fromdvp(dp, cred, 1)) == NULL) {
1603 nlookup_init_raw(nd, namebuf, UIO_SYSSPACE, flags, cred, ncp);
1607 * Ok, do the lookup.
1609 error = nlookup(nd);
1612 * If no error occured return the requested dvpp and vpp. If
1613 * NLC_CREATE was specified nd->nl_ncp may represent a negative
1614 * cache hit in which case we do not attempt to obtain the vp.
1619 if (ncp->nc_parent &&
1620 ncp->nc_parent->nc_mount == ncp->nc_mount) {
1621 error = cache_vget(ncp->nc_parent, nd->nl_cred,
1622 LK_EXCLUSIVE, dvpp);
1627 if (vpp && ncp->nc_vp) {
1628 error = cache_vget(ncp, nd->nl_cred, LK_EXCLUSIVE, vpp);
1631 if (dvpp && *dvpp) {
1646 zfree(namei_zone, namebuf);
1651 * A fiddled version of m_adj() that ensures null fill to a long
1652 * boundary and only trims off the back end
1655 nfsm_adj(struct mbuf *mp, int len, int nul)
1662 * Trim from tail. Scan the mbuf chain,
1663 * calculating its length and finding the last mbuf.
1664 * If the adjustment only affects this mbuf, then just
1665 * adjust and return. Otherwise, rescan and truncate
1666 * after the remaining size.
1672 if (m->m_next == (struct mbuf *)0)
1676 if (m->m_len > len) {
1679 cp = mtod(m, caddr_t)+m->m_len-nul;
1680 for (i = 0; i < nul; i++)
1689 * Correct length for chain is "count".
1690 * Find the mbuf with last data, adjust its length,
1691 * and toss data from remaining mbufs on chain.
1693 for (m = mp; m; m = m->m_next) {
1694 if (m->m_len >= count) {
1697 cp = mtod(m, caddr_t)+m->m_len-nul;
1698 for (i = 0; i < nul; i++)
1705 for (m = m->m_next;m;m = m->m_next)
1710 * Make these functions instead of macros, so that the kernel text size
1711 * doesn't get too big...
1714 nfsm_srvwcc(struct nfsrv_descript *nfsd, int before_ret,
1715 struct vattr *before_vap, int after_ret, struct vattr *after_vap,
1716 struct mbuf **mbp, char **bposp)
1718 struct mbuf *mb = *mbp, *mb2;
1719 char *bpos = *bposp;
1723 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1726 nfsm_build(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
1728 txdr_hyper(before_vap->va_size, tl);
1730 txdr_nfsv3time(&(before_vap->va_mtime), tl);
1732 txdr_nfsv3time(&(before_vap->va_ctime), tl);
1736 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp);
1740 nfsm_srvpostopattr(struct nfsrv_descript *nfsd, int after_ret,
1741 struct vattr *after_vap, struct mbuf **mbp, char **bposp)
1743 struct mbuf *mb = *mbp, *mb2;
1744 char *bpos = *bposp;
1746 struct nfs_fattr *fp;
1749 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1752 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR);
1754 fp = (struct nfs_fattr *)tl;
1755 nfsm_srvfattr(nfsd, after_vap, fp);
1762 nfsm_srvfattr(struct nfsrv_descript *nfsd, struct vattr *vap,
1763 struct nfs_fattr *fp)
1766 fp->fa_nlink = txdr_unsigned(vap->va_nlink);
1767 fp->fa_uid = txdr_unsigned(vap->va_uid);
1768 fp->fa_gid = txdr_unsigned(vap->va_gid);
1769 if (nfsd->nd_flag & ND_NFSV3) {
1770 fp->fa_type = vtonfsv3_type(vap->va_type);
1771 fp->fa_mode = vtonfsv3_mode(vap->va_mode);
1772 txdr_hyper(vap->va_size, &fp->fa3_size);
1773 txdr_hyper(vap->va_bytes, &fp->fa3_used);
1774 fp->fa3_rdev.specdata1 = txdr_unsigned(umajor(vap->va_rdev));
1775 fp->fa3_rdev.specdata2 = txdr_unsigned(uminor(vap->va_rdev));
1776 fp->fa3_fsid.nfsuquad[0] = 0;
1777 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid);
1778 fp->fa3_fileid.nfsuquad[0] = 0;
1779 fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(vap->va_fileid);
1780 txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime);
1781 txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime);
1782 txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime);
1784 fp->fa_type = vtonfsv2_type(vap->va_type);
1785 fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1786 fp->fa2_size = txdr_unsigned(vap->va_size);
1787 fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize);
1788 if (vap->va_type == VFIFO)
1789 fp->fa2_rdev = 0xffffffff;
1791 fp->fa2_rdev = txdr_unsigned(vap->va_rdev);
1792 fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE);
1793 fp->fa2_fsid = txdr_unsigned(vap->va_fsid);
1794 fp->fa2_fileid = txdr_unsigned(vap->va_fileid);
1795 txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime);
1796 txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime);
1797 txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime);
1802 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
1803 * - look up fsid in mount list (if not found ret error)
1804 * - get vp and export rights by calling VFS_FHTOVP()
1805 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
1806 * - if not lockflag unlock it with VOP_UNLOCK()
1809 nfsrv_fhtovp(fhandle_t *fhp, int lockflag, struct vnode **vpp,
1810 struct ucred *cred, struct nfssvc_sock *slp, struct sockaddr *nam,
1811 int *rdonlyp, int kerbflag, int pubflag)
1813 struct thread *td = curthread; /* XXX */
1816 struct ucred *credanon;
1818 #ifdef MNT_EXNORESPORT /* XXX needs mountd and /etc/exports help yet */
1819 struct sockaddr_int *saddr;
1822 *vpp = (struct vnode *)0;
1824 if (nfs_ispublicfh(fhp)) {
1825 if (!pubflag || !nfs_pub.np_valid)
1827 fhp = &nfs_pub.np_handle;
1830 mp = vfs_getvfs(&fhp->fh_fsid);
1833 error = VFS_CHECKEXP(mp, nam, &exflags, &credanon);
1836 error = VFS_FHTOVP(mp, &fhp->fh_fid, vpp);
1839 #ifdef MNT_EXNORESPORT
1840 if (!(exflags & (MNT_EXNORESPORT|MNT_EXPUBLIC))) {
1841 saddr = (struct sockaddr_in *)nam;
1842 if (saddr->sin_family == AF_INET &&
1843 ntohs(saddr->sin_port) >= IPPORT_RESERVED) {
1846 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1851 * Check/setup credentials.
1853 if (exflags & MNT_EXKERB) {
1857 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1859 } else if (kerbflag) {
1862 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1863 } else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1864 cred->cr_uid = credanon->cr_uid;
1865 for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++)
1866 cred->cr_groups[i] = credanon->cr_groups[i];
1867 cred->cr_ngroups = i;
1869 if (exflags & MNT_EXRDONLY)
1874 nfsrv_object_create(*vpp);
1877 VOP_UNLOCK(*vpp, 0, td);
1883 * WebNFS: check if a filehandle is a public filehandle. For v3, this
1884 * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has
1885 * transformed this to all zeroes in both cases, so check for it.
1888 nfs_ispublicfh(fhandle_t *fhp)
1890 char *cp = (char *)fhp;
1893 for (i = 0; i < NFSX_V3FH; i++)
1899 #endif /* NFS_NOSERVER */
1901 * This function compares two net addresses by family and returns TRUE
1902 * if they are the same host.
1903 * If there is any doubt, return FALSE.
1904 * The AF_INET family is handled as a special case so that address mbufs
1905 * don't need to be saved to store "struct in_addr", which is only 4 bytes.
1908 netaddr_match(int family, union nethostaddr *haddr, struct sockaddr *nam)
1910 struct sockaddr_in *inetaddr;
1914 inetaddr = (struct sockaddr_in *)nam;
1915 if (inetaddr->sin_family == AF_INET &&
1916 inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
1925 static nfsuint64 nfs_nullcookie = { { 0, 0 } };
1927 * This function finds the directory cookie that corresponds to the
1928 * logical byte offset given.
1931 nfs_getcookie(struct nfsnode *np, off_t off, int add)
1933 struct nfsdmap *dp, *dp2;
1936 pos = (uoff_t)off / NFS_DIRBLKSIZ;
1937 if (pos == 0 || off < 0) {
1940 panic("nfs getcookie add at <= 0");
1942 return (&nfs_nullcookie);
1945 dp = np->n_cookies.lh_first;
1948 MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap),
1949 M_NFSDIROFF, M_WAITOK);
1950 dp->ndm_eocookie = 0;
1951 LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
1953 return ((nfsuint64 *)0);
1955 while (pos >= NFSNUMCOOKIES) {
1956 pos -= NFSNUMCOOKIES;
1957 if (dp->ndm_list.le_next) {
1958 if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
1959 pos >= dp->ndm_eocookie)
1960 return ((nfsuint64 *)0);
1961 dp = dp->ndm_list.le_next;
1963 MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap),
1964 M_NFSDIROFF, M_WAITOK);
1965 dp2->ndm_eocookie = 0;
1966 LIST_INSERT_AFTER(dp, dp2, ndm_list);
1969 return ((nfsuint64 *)0);
1971 if (pos >= dp->ndm_eocookie) {
1973 dp->ndm_eocookie = pos + 1;
1975 return ((nfsuint64 *)0);
1977 return (&dp->ndm_cookies[pos]);
1981 * Invalidate cached directory information, except for the actual directory
1982 * blocks (which are invalidated separately).
1983 * Done mainly to avoid the use of stale offset cookies.
1986 nfs_invaldir(struct vnode *vp)
1988 struct nfsnode *np = VTONFS(vp);
1991 if (vp->v_type != VDIR)
1992 panic("nfs: invaldir not dir");
1994 np->n_direofoffset = 0;
1995 np->n_cookieverf.nfsuquad[0] = 0;
1996 np->n_cookieverf.nfsuquad[1] = 0;
1997 if (np->n_cookies.lh_first)
1998 np->n_cookies.lh_first->ndm_eocookie = 0;
2002 * The write verifier has changed (probably due to a server reboot), so all
2003 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
2004 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
2005 * and B_CLUSTEROK flags. Once done the new write verifier can be set for the
2008 * B_CLUSTEROK must be cleared along with B_NEEDCOMMIT because stage 1 data
2009 * writes are not clusterable.
2012 nfs_clearcommit(struct mount *mp)
2014 struct vnode *vp, *nvp;
2015 struct buf *bp, *nbp;
2019 lwkt_gettoken(&ilock, &mntvnode_token);
2021 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp; vp = nvp) {
2022 nvp = TAILQ_NEXT(vp, v_nmntvnodes); /* ZZZ */
2023 if (vp->v_flag & VPLACEMARKER)
2025 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2026 nbp = TAILQ_NEXT(bp, b_vnbufs);
2027 if (BUF_REFCNT(bp) == 0 &&
2028 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2029 == (B_DELWRI | B_NEEDCOMMIT)) {
2030 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
2035 lwkt_reltoken(&ilock);
2038 #ifndef NFS_NOSERVER
2040 * Map errnos to NFS error numbers. For Version 3 also filter out error
2041 * numbers not specified for the associated procedure.
2044 nfsrv_errmap(struct nfsrv_descript *nd, int err)
2046 short *defaulterrp, *errp;
2048 if (nd->nd_flag & ND_NFSV3) {
2049 if (nd->nd_procnum <= NFSPROC_COMMIT) {
2050 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
2054 else if (*errp > err)
2057 return ((int)*defaulterrp);
2059 return (err & 0xffff);
2062 return ((int)nfsrv_v2errmap[err - 1]);
2067 nfsrv_object_create(struct vnode *vp)
2069 struct thread *td = curthread;
2071 if (vp == NULL || vp->v_type != VREG)
2073 return (vfs_object_create(vp, td));
2077 * Sort the group list in increasing numerical order.
2078 * (Insertion sort by Chris Torek, who was grossed out by the bubble sort
2079 * that used to be here.)
2082 nfsrvw_sort(gid_t *list, int num)
2087 /* Insertion sort. */
2088 for (i = 1; i < num; i++) {
2090 /* find correct slot for value v, moving others up */
2091 for (j = i; --j >= 0 && v < list[j];)
2092 list[j + 1] = list[j];
2098 * copy credentials making sure that the result can be compared with bcmp().
2101 nfsrv_setcred(struct ucred *incred, struct ucred *outcred)
2105 bzero((caddr_t)outcred, sizeof (struct ucred));
2106 outcred->cr_ref = 1;
2107 outcred->cr_uid = incred->cr_uid;
2108 outcred->cr_ngroups = incred->cr_ngroups;
2109 for (i = 0; i < incred->cr_ngroups; i++)
2110 outcred->cr_groups[i] = incred->cr_groups[i];
2111 nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups);
2113 #endif /* NFS_NOSERVER */