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.18 2004/06/02 14:43:04 eirikn 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/namei.h>
55 #include <sys/socket.h>
57 #include <sys/malloc.h>
58 #include <sys/sysent.h>
59 #include <sys/syscall.h>
63 #include <vm/vm_object.h>
64 #include <vm/vm_extern.h>
65 #include <vm/vm_zone.h>
75 #include "nfsm_subs.h"
79 #include <netinet/in.h>
82 * Data items converted to xdr at startup, since they are constant
83 * This is kinda hokey, but may save a little time doing byte swaps
85 u_int32_t nfs_xdrneg1;
86 u_int32_t rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr,
87 rpc_mismatch, rpc_auth_unix, rpc_msgaccepted,
89 u_int32_t nfs_prog, nqnfs_prog, nfs_true, nfs_false;
91 /* And other global data */
92 static u_int32_t nfs_xid = 0;
93 static enum vtype nv2tov_type[8]= {
94 VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON, VNON
96 enum vtype nv3tov_type[8]= {
97 VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO
101 int nfs_pbuf_freecnt = -1; /* start out unlimited */
103 struct nfs_reqq nfs_reqq;
104 struct nfssvc_sockhead nfssvc_sockhead;
105 int nfssvc_sockhead_flag;
106 struct nfsd_head nfsd_head;
108 struct nfs_bufq nfs_bufq;
109 struct nqtimerhead nqtimerhead;
110 struct nqfhhashhead *nqfhhashtbl;
113 static void (*nfs_prev_lease_updatetime) (int);
114 static int nfs_prev_nfssvc_sy_narg;
115 static sy_call_t *nfs_prev_nfssvc_sy_call;
119 static vop_t *nfs_prev_vop_lease_check;
122 * Mapping of old NFS Version 2 RPC numbers to generic numbers.
124 int nfsv3_procid[NFS_NPROCS] = {
153 #endif /* NFS_NOSERVER */
155 * and the reverse mapping from generic to Version 2 procedure numbers
157 int nfsv2_procid[NFS_NPROCS] = {
188 * Maps errno values to nfs error numbers.
189 * Use NFSERR_IO as the catch all for ones not specifically defined in
192 static u_char nfsrv_v2errmap[ELAST] = {
193 NFSERR_PERM, NFSERR_NOENT, NFSERR_IO, NFSERR_IO, NFSERR_IO,
194 NFSERR_NXIO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
195 NFSERR_IO, NFSERR_IO, NFSERR_ACCES, NFSERR_IO, NFSERR_IO,
196 NFSERR_IO, NFSERR_EXIST, NFSERR_IO, NFSERR_NODEV, NFSERR_NOTDIR,
197 NFSERR_ISDIR, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
198 NFSERR_IO, NFSERR_FBIG, NFSERR_NOSPC, NFSERR_IO, NFSERR_ROFS,
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_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_NAMETOL, NFSERR_IO, NFSERR_IO,
206 NFSERR_NOTEMPTY, NFSERR_IO, NFSERR_IO, NFSERR_DQUOT, NFSERR_STALE,
207 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
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 /* << Last is 86 */
214 * Maps errno values to nfs error numbers.
215 * Although it is not obvious whether or not NFS clients really care if
216 * a returned error value is in the specified list for the procedure, the
217 * safest thing to do is filter them appropriately. For Version 2, the
218 * X/Open XNFS document is the only specification that defines error values
219 * for each RPC (The RFC simply lists all possible error values for all RPCs),
220 * so I have decided to not do this for Version 2.
221 * The first entry is the default error return and the rest are the valid
222 * errors for that RPC in increasing numeric order.
224 static short nfsv3err_null[] = {
229 static short nfsv3err_getattr[] = {
238 static short nfsv3err_setattr[] = {
254 static short nfsv3err_lookup[] = {
267 static short nfsv3err_access[] = {
276 static short nfsv3err_readlink[] = {
288 static short nfsv3err_read[] = {
300 static short nfsv3err_write[] = {
315 static short nfsv3err_create[] = {
332 static short nfsv3err_mkdir[] = {
349 static short nfsv3err_symlink[] = {
366 static short nfsv3err_mknod[] = {
384 static short nfsv3err_remove[] = {
398 static short nfsv3err_rmdir[] = {
416 static short nfsv3err_rename[] = {
439 static short nfsv3err_link[] = {
459 static short nfsv3err_readdir[] = {
472 static short nfsv3err_readdirplus[] = {
486 static short nfsv3err_fsstat[] = {
495 static short nfsv3err_fsinfo[] = {
503 static short nfsv3err_pathconf[] = {
511 static short nfsv3err_commit[] = {
520 static short *nfsrv_v3errmap[] = {
538 nfsv3err_readdirplus,
545 #endif /* NFS_NOSERVER */
547 extern struct nfsrtt nfsrtt;
548 extern time_t nqnfsstarttime;
549 extern int nqsrv_clockskew;
550 extern int nqsrv_writeslack;
551 extern int nqsrv_maxlease;
552 extern struct nfsstats nfsstats;
553 extern int nqnfs_piggy[NFS_NPROCS];
554 extern nfstype nfsv2_type[9];
555 extern nfstype nfsv3_type[9];
556 extern struct nfsnodehashhead *nfsnodehashtbl;
557 extern u_long nfsnodehash;
560 extern int nfssvc(struct proc *, struct nfssvc_args *, int *);
562 LIST_HEAD(nfsnodehashhead, nfsnode);
564 int nfs_webnamei (struct nameidata *, struct vnode *, struct proc *);
572 return ((u_quad_t)tv.tv_sec * 1000000 + (u_quad_t)tv.tv_usec);
576 * Create the header for an rpc request packet
577 * The hsiz is the size of the rest of the nfs request header.
578 * (just used to decide if a cluster is a good idea)
581 nfsm_reqh(struct vnode *vp, u_long procid, int hsiz, caddr_t *bposp)
587 struct nfsmount *nmp;
590 MGET(mb, MB_WAIT, MT_DATA);
591 if (hsiz >= MINCLSIZE)
594 bpos = mtod(mb, caddr_t);
597 * For NQNFS, add lease request.
600 nmp = VFSTONFS(vp->v_mount);
601 if (nmp->nm_flag & NFSMNT_NQNFS) {
602 nqflag = NQNFS_NEEDLEASE(vp, procid);
604 nfsm_build(tl, u_int32_t *, 2*NFSX_UNSIGNED);
605 *tl++ = txdr_unsigned(nqflag);
606 *tl = txdr_unsigned(nmp->nm_leaseterm);
608 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
613 /* Finally, return values */
619 * Build the RPC header and fill in the authorization info.
620 * The authorization string argument is only used when the credentials
621 * come from outside of the kernel.
622 * Returns the head of the mbuf list.
625 nfsm_rpchead(struct ucred *cr, int nmflag, int procid, int auth_type,
626 int auth_len, char *auth_str, int verf_len, char *verf_str,
627 struct mbuf *mrest, int mrest_len, struct mbuf **mbp,
634 struct mbuf *mreq, *mb2;
635 int siz, grpsiz, authsiz;
637 authsiz = nfsm_rndup(auth_len);
638 MGETHDR(mb, MB_WAIT, MT_DATA);
639 if ((authsiz + 10 * NFSX_UNSIGNED) >= MINCLSIZE) {
641 } else if ((authsiz + 10 * NFSX_UNSIGNED) < MHLEN) {
642 MH_ALIGN(mb, authsiz + 10 * NFSX_UNSIGNED);
644 MH_ALIGN(mb, 8 * NFSX_UNSIGNED);
648 bpos = mtod(mb, caddr_t);
651 * First the RPC header.
653 nfsm_build(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
655 /* Get a pretty random xid to start with */
659 * Skip zero xid if it should ever happen.
664 *tl++ = *xidp = txdr_unsigned(nfs_xid);
667 if (nmflag & NFSMNT_NQNFS) {
668 *tl++ = txdr_unsigned(NQNFS_PROG);
669 *tl++ = txdr_unsigned(NQNFS_VER3);
671 *tl++ = txdr_unsigned(NFS_PROG);
672 if (nmflag & NFSMNT_NFSV3)
673 *tl++ = txdr_unsigned(NFS_VER3);
675 *tl++ = txdr_unsigned(NFS_VER2);
677 if (nmflag & NFSMNT_NFSV3)
678 *tl++ = txdr_unsigned(procid);
680 *tl++ = txdr_unsigned(nfsv2_procid[procid]);
683 * And then the authorization cred.
685 *tl++ = txdr_unsigned(auth_type);
686 *tl = txdr_unsigned(authsiz);
689 nfsm_build(tl, u_int32_t *, auth_len);
690 *tl++ = 0; /* stamp ?? */
691 *tl++ = 0; /* NULL hostname */
692 *tl++ = txdr_unsigned(cr->cr_uid);
693 *tl++ = txdr_unsigned(cr->cr_groups[0]);
694 grpsiz = (auth_len >> 2) - 5;
695 *tl++ = txdr_unsigned(grpsiz);
696 for (i = 1; i <= grpsiz; i++)
697 *tl++ = txdr_unsigned(cr->cr_groups[i]);
702 if (M_TRAILINGSPACE(mb) == 0) {
703 MGET(mb2, MB_WAIT, MT_DATA);
704 if (siz >= MINCLSIZE)
705 MCLGET(mb2, MB_WAIT);
709 bpos = mtod(mb, caddr_t);
711 i = min(siz, M_TRAILINGSPACE(mb));
712 bcopy(auth_str, bpos, i);
718 if ((siz = (nfsm_rndup(auth_len) - auth_len)) > 0) {
719 for (i = 0; i < siz; i++)
727 * And the verifier...
729 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
731 *tl++ = txdr_unsigned(RPCAUTH_KERB4);
732 *tl = txdr_unsigned(verf_len);
735 if (M_TRAILINGSPACE(mb) == 0) {
736 MGET(mb2, MB_WAIT, MT_DATA);
737 if (siz >= MINCLSIZE)
738 MCLGET(mb2, MB_WAIT);
742 bpos = mtod(mb, caddr_t);
744 i = min(siz, M_TRAILINGSPACE(mb));
745 bcopy(verf_str, bpos, i);
751 if ((siz = (nfsm_rndup(verf_len) - verf_len)) > 0) {
752 for (i = 0; i < siz; i++)
757 *tl++ = txdr_unsigned(RPCAUTH_NULL);
761 mreq->m_pkthdr.len = authsiz + 10 * NFSX_UNSIGNED + mrest_len;
762 mreq->m_pkthdr.rcvif = (struct ifnet *)0;
768 * copies mbuf chain to the uio scatter/gather list
771 nfsm_mbuftouio(struct mbuf **mrep, struct uio *uiop, int siz, caddr_t *dpos)
773 char *mbufcp, *uiocp;
781 len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
782 rem = nfsm_rndup(siz)-siz;
784 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
786 left = uiop->uio_iov->iov_len;
787 uiocp = uiop->uio_iov->iov_base;
796 mbufcp = mtod(mp, caddr_t);
799 xfer = (left > len) ? len : left;
802 if (uiop->uio_iov->iov_op != NULL)
803 (*(uiop->uio_iov->iov_op))
804 (mbufcp, uiocp, xfer);
807 if (uiop->uio_segflg == UIO_SYSSPACE)
808 bcopy(mbufcp, uiocp, xfer);
810 copyout(mbufcp, uiocp, xfer);
815 uiop->uio_offset += xfer;
816 uiop->uio_resid -= xfer;
818 if (uiop->uio_iov->iov_len <= siz) {
822 uiop->uio_iov->iov_base += uiosiz;
823 uiop->uio_iov->iov_len -= uiosiz;
831 error = nfs_adv(mrep, dpos, rem, len);
839 * copies a uio scatter/gather list to an mbuf chain.
840 * NOTE: can ony handle iovcnt == 1
843 nfsm_uiotombuf(struct uio *uiop, struct mbuf **mq, int siz, caddr_t *bpos)
846 struct mbuf *mp, *mp2;
847 int xfer, left, mlen;
848 int uiosiz, clflg, rem;
852 if (uiop->uio_iovcnt != 1)
853 panic("nfsm_uiotombuf: iovcnt != 1");
856 if (siz > MLEN) /* or should it >= MCLBYTES ?? */
860 rem = nfsm_rndup(siz)-siz;
863 left = uiop->uio_iov->iov_len;
864 uiocp = uiop->uio_iov->iov_base;
869 mlen = M_TRAILINGSPACE(mp);
871 MGET(mp, MB_WAIT, MT_DATA);
877 mlen = M_TRAILINGSPACE(mp);
879 xfer = (left > mlen) ? mlen : left;
882 if (uiop->uio_iov->iov_op != NULL)
883 (*(uiop->uio_iov->iov_op))
884 (uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
887 if (uiop->uio_segflg == UIO_SYSSPACE)
888 bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
890 copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
894 uiop->uio_offset += xfer;
895 uiop->uio_resid -= xfer;
897 uiop->uio_iov->iov_base += uiosiz;
898 uiop->uio_iov->iov_len -= uiosiz;
902 if (rem > M_TRAILINGSPACE(mp)) {
903 MGET(mp, MB_WAIT, MT_DATA);
907 cp = mtod(mp, caddr_t)+mp->m_len;
908 for (left = 0; left < rem; left++)
913 *bpos = mtod(mp, caddr_t)+mp->m_len;
919 * Help break down an mbuf chain by setting the first siz bytes contiguous
920 * pointed to by returned val.
921 * This is used by the macros nfsm_dissect and nfsm_dissecton for tough
922 * cases. (The macros use the vars. dpos and dpos2)
925 nfsm_disct(struct mbuf **mdp, caddr_t *dposp, int siz, int left, caddr_t *cp2)
927 struct mbuf *mp, *mp2;
933 *mdp = mp = mp->m_next;
937 *dposp = mtod(mp, caddr_t);
942 } else if (mp->m_next == NULL) {
944 } else if (siz > MHLEN) {
945 panic("nfs S too big");
947 MGET(mp2, MB_WAIT, MT_DATA);
948 mp2->m_next = mp->m_next;
952 *cp2 = p = mtod(mp, caddr_t);
953 bcopy(*dposp, p, left); /* Copy what was left */
957 /* Loop around copying up the siz2 bytes */
961 xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2;
963 bcopy(mtod(mp2, caddr_t), p, xfer);
974 *dposp = mtod(mp2, caddr_t);
980 * Advance the position in the mbuf chain.
983 nfs_adv(struct mbuf **mdp, caddr_t *dposp, int offs, int left)
998 *dposp = mtod(m, caddr_t)+offs;
1003 * Copy a string into mbufs for the hard cases...
1006 nfsm_strtmbuf(struct mbuf **mb, char **bpos, const char *cp, long siz)
1008 struct mbuf *m1 = NULL, *m2;
1009 long left, xfer, len, tlen;
1015 left = M_TRAILINGSPACE(m2);
1017 tl = ((u_int32_t *)(*bpos));
1018 *tl++ = txdr_unsigned(siz);
1020 left -= NFSX_UNSIGNED;
1021 m2->m_len += NFSX_UNSIGNED;
1023 bcopy(cp, (caddr_t) tl, left);
1030 /* Loop around adding mbufs */
1032 MGET(m1, MB_WAIT, MT_DATA);
1034 MCLGET(m1, MB_WAIT);
1035 m1->m_len = NFSMSIZ(m1);
1038 tl = mtod(m1, u_int32_t *);
1041 *tl++ = txdr_unsigned(siz);
1042 m1->m_len -= NFSX_UNSIGNED;
1043 tlen = NFSX_UNSIGNED;
1046 if (siz < m1->m_len) {
1047 len = nfsm_rndup(siz);
1050 *(tl+(xfer>>2)) = 0;
1052 xfer = len = m1->m_len;
1054 bcopy(cp, (caddr_t) tl, xfer);
1055 m1->m_len = len+tlen;
1060 *bpos = mtod(m1, caddr_t)+m1->m_len;
1065 * Called once to initialize data structures...
1068 nfs_init(struct vfsconf *vfsp)
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_vnodeop_p[VOFFSET(vop_lease)];
1128 default_vnodeop_p[VOFFSET(vop_lease)] = (vop_t *)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)
1146 untimeout(nfs_timer, (void *)NULL, nfs_timer_handle);
1147 nfs_mount_type = -1;
1148 #ifndef NFS_NOSERVER
1149 default_vnodeop_p[VOFFSET(vop_lease)] = nfs_prev_vop_lease_check;
1151 lease_updatetime = nfs_prev_lease_updatetime;
1152 sysent[SYS_nfssvc].sy_narg = nfs_prev_nfssvc_sy_narg;
1153 sysent[SYS_nfssvc].sy_call = nfs_prev_nfssvc_sy_call;
1158 * Attribute cache routines.
1159 * nfs_loadattrcache() - loads or updates the cache contents from attributes
1160 * that are on the mbuf list
1161 * nfs_getattrcache() - returns valid attributes if found in cache, returns
1166 * Load the attribute cache (that lives in the nfsnode entry) with
1167 * the values on the mbuf list and
1169 * copy the attributes to *vaper
1172 nfs_loadattrcache(struct vnode **vpp, struct mbuf **mdp, caddr_t *dposp,
1173 struct vattr *vaper, int dontshrink)
1175 struct vnode *vp = *vpp;
1177 struct nfs_fattr *fp;
1186 struct timespec mtime;
1187 int v3 = NFS_ISV3(vp);
1190 t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
1191 if ((error = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, &cp2)) != 0)
1193 fp = (struct nfs_fattr *)cp2;
1195 vtyp = nfsv3tov_type(fp->fa_type);
1196 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1197 rdev = makeudev(fxdr_unsigned(int, fp->fa3_rdev.specdata1),
1198 fxdr_unsigned(int, fp->fa3_rdev.specdata2));
1199 fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
1201 vtyp = nfsv2tov_type(fp->fa_type);
1202 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1206 * The duplicate information returned in fa_type and fa_mode
1207 * is an ambiguity in the NFS version 2 protocol.
1209 * VREG should be taken literally as a regular file. If a
1210 * server intents to return some type information differently
1211 * in the upper bits of the mode field (e.g. for sockets, or
1212 * FIFOs), NFSv2 mandates fa_type to be VNON. Anyway, we
1213 * leave the examination of the mode bits even in the VREG
1214 * case to avoid breakage for bogus servers, but we make sure
1215 * that there are actually type bits set in the upper part of
1216 * fa_mode (and failing that, trust the va_type field).
1218 * NFSv3 cleared the issue, and requires fa_mode to not
1219 * contain any type information (while also introduing sockets
1220 * and FIFOs for fa_type).
1222 if (vtyp == VNON || (vtyp == VREG && (vmode & S_IFMT) != 0))
1223 vtyp = IFTOVT(vmode);
1224 rdev = fxdr_unsigned(int32_t, fp->fa2_rdev);
1225 fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
1228 * Really ugly NFSv2 kludge.
1230 if (vtyp == VCHR && rdev == (udev_t)0xffffffff)
1235 * If v_type == VNON it is a new node, so fill in the v_type,
1236 * n_mtime fields. Check to see if it represents a special
1237 * device, and if so, check for a possible alias. Once the
1238 * correct vnode has been obtained, fill in the rest of the
1242 if (vp->v_type != vtyp) {
1244 if (vp->v_type == VFIFO) {
1245 vp->v_op = fifo_nfsv2nodeop_p;
1247 if (vp->v_type == VCHR || vp->v_type == VBLK) {
1248 vp->v_op = spec_nfsv2nodeop_p;
1249 addaliasu(vp, rdev);
1251 np->n_mtime = mtime.tv_sec;
1254 vap->va_type = vtyp;
1255 vap->va_mode = (vmode & 07777);
1256 vap->va_rdev = rdev;
1257 vap->va_mtime = mtime;
1258 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
1260 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1261 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1262 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1263 vap->va_size = fxdr_hyper(&fp->fa3_size);
1264 vap->va_blocksize = NFS_FABLKSIZE;
1265 vap->va_bytes = fxdr_hyper(&fp->fa3_used);
1266 vap->va_fileid = fxdr_unsigned(int32_t,
1267 fp->fa3_fileid.nfsuquad[1]);
1268 fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
1269 fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
1271 vap->va_filerev = 0;
1273 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1274 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1275 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1276 vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
1277 vap->va_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize);
1278 vap->va_bytes = (u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks)
1280 vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
1281 fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
1283 vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t,
1284 fp->fa2_ctime.nfsv2_sec);
1285 vap->va_ctime.tv_nsec = 0;
1286 vap->va_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec);
1287 vap->va_filerev = 0;
1289 np->n_attrstamp = time_second;
1290 if (vap->va_size != np->n_size) {
1291 if (vap->va_type == VREG) {
1292 if (dontshrink && vap->va_size < np->n_size) {
1294 * We've been told not to shrink the file;
1295 * zero np->n_attrstamp to indicate that
1296 * the attributes are stale.
1298 vap->va_size = np->n_size;
1299 np->n_attrstamp = 0;
1300 } else if (np->n_flag & NMODIFIED) {
1302 * We've modified the file: Use the larger
1303 * of our size, and the server's size.
1305 if (vap->va_size < np->n_size) {
1306 vap->va_size = np->n_size;
1308 np->n_size = vap->va_size;
1309 np->n_flag |= NSIZECHANGED;
1312 np->n_size = vap->va_size;
1313 np->n_flag |= NSIZECHANGED;
1315 vnode_pager_setsize(vp, np->n_size);
1317 np->n_size = vap->va_size;
1320 if (vaper != NULL) {
1321 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
1322 if (np->n_flag & NCHG) {
1323 if (np->n_flag & NACC)
1324 vaper->va_atime = np->n_atim;
1325 if (np->n_flag & NUPD)
1326 vaper->va_mtime = np->n_mtim;
1333 #include <sys/sysctl.h>
1334 SYSCTL_DECL(_vfs_nfs);
1335 static int nfs_acdebug;
1336 SYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, "");
1340 * Check the time stamp
1341 * If the cache is valid, copy contents to *vap and return 0
1342 * otherwise return an error
1345 nfs_getattrcache(struct vnode *vp, struct vattr *vaper)
1349 struct nfsmount *nmp;
1354 nmp = VFSTONFS(vp->v_mount);
1355 /* XXX n_mtime doesn't seem to be updated on a miss-and-reload */
1356 timeo = (time_second - np->n_mtime) / 10;
1360 printf("nfs_getattrcache: initial timeo = %d\n", timeo);
1363 if (vap->va_type == VDIR) {
1364 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin)
1365 timeo = nmp->nm_acdirmin;
1366 else if (timeo > nmp->nm_acdirmax)
1367 timeo = nmp->nm_acdirmax;
1369 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin)
1370 timeo = nmp->nm_acregmin;
1371 else if (timeo > nmp->nm_acregmax)
1372 timeo = nmp->nm_acregmax;
1376 if (nfs_acdebug > 2)
1377 printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n",
1378 nmp->nm_acregmin, nmp->nm_acregmax,
1379 nmp->nm_acdirmin, nmp->nm_acdirmax);
1382 printf("nfs_getattrcache: age = %d; final timeo = %d\n",
1383 (time_second - np->n_attrstamp), timeo);
1386 if ((time_second - np->n_attrstamp) >= timeo) {
1387 nfsstats.attrcache_misses++;
1390 nfsstats.attrcache_hits++;
1391 if (vap->va_size != np->n_size) {
1392 if (vap->va_type == VREG) {
1393 if (np->n_flag & NMODIFIED) {
1394 if (vap->va_size < np->n_size)
1395 vap->va_size = np->n_size;
1397 np->n_size = vap->va_size;
1399 np->n_size = vap->va_size;
1401 vnode_pager_setsize(vp, np->n_size);
1403 np->n_size = vap->va_size;
1406 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
1407 if (np->n_flag & NCHG) {
1408 if (np->n_flag & NACC)
1409 vaper->va_atime = np->n_atim;
1410 if (np->n_flag & NUPD)
1411 vaper->va_mtime = np->n_mtim;
1416 #ifndef NFS_NOSERVER
1418 * Set up nameidata for a lookup() call and do it.
1420 * If pubflag is set, this call is done for a lookup operation on the
1421 * public filehandle. In that case we allow crossing mountpoints and
1422 * absolute pathnames. However, the caller is expected to check that
1423 * the lookup result is within the public fs, and deny access if
1426 * nfs_namei() clears out garbage fields that namei() might leave garbage.
1427 * This is mainly ni_vp and ni_dvp when an error occurs, and ni_dvp when no
1428 * error occurs but the parent was not requested.
1430 * dirp may be set whether an error is returned or not, and must be
1431 * released by the caller.
1434 nfs_namei(struct nameidata *ndp, fhandle_t *fhp, int len,
1435 struct nfssvc_sock *slp, struct sockaddr *nam, struct mbuf **mdp,
1436 caddr_t *dposp, struct vnode **retdirp, struct thread *td,
1437 int kerbflag, int pubflag)
1441 char *fromcp, *tocp, *cp;
1445 int error, rdonly, linklen;
1446 struct componentname *cnp = &ndp->ni_cnd;
1448 *retdirp = (struct vnode *)0;
1449 cnp->cn_pnbuf = zalloc(namei_zone);
1452 * Copy the name from the mbuf list to ndp->ni_pnbuf
1453 * and set the various ndp fields appropriately.
1456 tocp = cnp->cn_pnbuf;
1458 rem = mtod(md, caddr_t) + md->m_len - fromcp;
1459 for (i = 0; i < len; i++) {
1466 fromcp = mtod(md, caddr_t);
1469 if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) {
1473 *tocp++ = *fromcp++;
1479 len = nfsm_rndup(len)-len;
1483 else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
1488 * Extract and set starting directory.
1490 error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp,
1491 nam, &rdonly, kerbflag, pubflag);
1494 if (dp->v_type != VDIR) {
1501 cnp->cn_flags |= CNP_RDONLY;
1504 * Set return directory. Reference to dp is implicitly transfered
1505 * to the returned pointer
1511 * Oh joy. For WebNFS, handle those pesky '%' escapes,
1512 * and the 'native path' indicator.
1514 cp = zalloc(namei_zone);
1515 fromcp = cnp->cn_pnbuf;
1517 if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) {
1518 switch ((unsigned char)*fromcp) {
1519 case WEBNFS_NATIVE_CHAR:
1521 * 'Native' path for us is the same
1522 * as a path according to the NFS spec,
1523 * just skip the escape char.
1528 * More may be added in the future, range 0x80-0xff
1532 zfree(namei_zone, cp);
1537 * Translate the '%' escapes, URL-style.
1539 while (*fromcp != '\0') {
1540 if (*fromcp == WEBNFS_ESC_CHAR) {
1541 if (fromcp[1] != '\0' && fromcp[2] != '\0') {
1543 *tocp++ = HEXSTRTOI(fromcp);
1548 zfree(namei_zone, cp);
1552 *tocp++ = *fromcp++;
1555 zfree(namei_zone, cnp->cn_pnbuf);
1559 ndp->ni_pathlen = (tocp - cnp->cn_pnbuf) + 1;
1560 ndp->ni_segflg = UIO_SYSSPACE;
1563 ndp->ni_rootdir = rootvnode;
1564 ndp->ni_loopcnt = 0;
1565 if (cnp->cn_pnbuf[0] == '/')
1568 cnp->cn_flags |= CNP_NOCROSSMOUNT;
1572 * Initialize for scan, set ni_startdir and bump ref on dp again
1573 * because lookup() will dereference ni_startdir.
1578 ndp->ni_startdir = dp;
1581 cnp->cn_nameptr = cnp->cn_pnbuf;
1583 * Call lookup() to do the real work. If an error occurs,
1584 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
1585 * we do not have to dereference anything before returning.
1586 * In either case ni_startdir will be dereferenced and NULLed
1589 error = lookup(ndp);
1594 * Check for encountering a symbolic link. Trivial
1595 * termination occurs if no symlink encountered.
1596 * Note: zfree is safe because error is 0, so we will
1597 * not zfree it again when we break.
1599 if ((cnp->cn_flags & CNP_ISSYMLINK) == 0) {
1600 nfsrv_object_create(ndp->ni_vp);
1601 if (cnp->cn_flags & (CNP_SAVENAME | CNP_SAVESTART))
1602 cnp->cn_flags |= CNP_HASBUF;
1604 zfree(namei_zone, cnp->cn_pnbuf);
1611 if ((cnp->cn_flags & CNP_LOCKPARENT) && ndp->ni_pathlen == 1)
1612 VOP_UNLOCK(ndp->ni_dvp, NULL, 0, td);
1618 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
1622 if (ndp->ni_pathlen > 1)
1623 cp = zalloc(namei_zone);
1627 aiov.iov_len = MAXPATHLEN;
1628 auio.uio_iov = &aiov;
1629 auio.uio_iovcnt = 1;
1630 auio.uio_offset = 0;
1631 auio.uio_rw = UIO_READ;
1632 auio.uio_segflg = UIO_SYSSPACE;
1634 auio.uio_resid = MAXPATHLEN;
1635 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
1638 if (ndp->ni_pathlen > 1)
1639 zfree(namei_zone, cp);
1645 linklen = MAXPATHLEN - auio.uio_resid;
1650 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
1651 error = ENAMETOOLONG;
1656 * Adjust or replace path
1658 if (ndp->ni_pathlen > 1) {
1659 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
1660 zfree(namei_zone, cnp->cn_pnbuf);
1663 cnp->cn_pnbuf[linklen] = '\0';
1664 ndp->ni_pathlen += linklen;
1667 * Cleanup refs for next loop and check if root directory
1668 * should replace current directory. Normally ni_dvp
1669 * becomes the new base directory and is cleaned up when
1670 * we loop. Explicitly null pointers after invalidation
1671 * to clarify operation.
1676 if (cnp->cn_pnbuf[0] == '/') {
1678 ndp->ni_dvp = ndp->ni_rootdir;
1681 ndp->ni_startdir = ndp->ni_dvp;
1686 * nfs_namei() guarentees that fields will not contain garbage
1687 * whether an error occurs or not. This allows the caller to track
1688 * cleanup state trivially.
1692 zfree(namei_zone, cnp->cn_pnbuf);
1695 ndp->ni_startdir = NULL;
1696 cnp->cn_flags &= ~CNP_HASBUF;
1697 } else if ((ndp->ni_cnd.cn_flags & (CNP_WANTPARENT|CNP_LOCKPARENT)) == 0) {
1704 * A fiddled version of m_adj() that ensures null fill to a long
1705 * boundary and only trims off the back end
1708 nfsm_adj(struct mbuf *mp, int len, int nul)
1715 * Trim from tail. Scan the mbuf chain,
1716 * calculating its length and finding the last mbuf.
1717 * If the adjustment only affects this mbuf, then just
1718 * adjust and return. Otherwise, rescan and truncate
1719 * after the remaining size.
1725 if (m->m_next == (struct mbuf *)0)
1729 if (m->m_len > len) {
1732 cp = mtod(m, caddr_t)+m->m_len-nul;
1733 for (i = 0; i < nul; i++)
1742 * Correct length for chain is "count".
1743 * Find the mbuf with last data, adjust its length,
1744 * and toss data from remaining mbufs on chain.
1746 for (m = mp; m; m = m->m_next) {
1747 if (m->m_len >= count) {
1750 cp = mtod(m, caddr_t)+m->m_len-nul;
1751 for (i = 0; i < nul; i++)
1758 for (m = m->m_next;m;m = m->m_next)
1763 * Make these functions instead of macros, so that the kernel text size
1764 * doesn't get too big...
1767 nfsm_srvwcc(struct nfsrv_descript *nfsd, int before_ret,
1768 struct vattr *before_vap, int after_ret, struct vattr *after_vap,
1769 struct mbuf **mbp, char **bposp)
1771 struct mbuf *mb = *mbp, *mb2;
1772 char *bpos = *bposp;
1776 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1779 nfsm_build(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
1781 txdr_hyper(before_vap->va_size, tl);
1783 txdr_nfsv3time(&(before_vap->va_mtime), tl);
1785 txdr_nfsv3time(&(before_vap->va_ctime), tl);
1789 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp);
1793 nfsm_srvpostopattr(struct nfsrv_descript *nfsd, int after_ret,
1794 struct vattr *after_vap, struct mbuf **mbp, char **bposp)
1796 struct mbuf *mb = *mbp, *mb2;
1797 char *bpos = *bposp;
1799 struct nfs_fattr *fp;
1802 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1805 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR);
1807 fp = (struct nfs_fattr *)tl;
1808 nfsm_srvfattr(nfsd, after_vap, fp);
1815 nfsm_srvfattr(struct nfsrv_descript *nfsd, struct vattr *vap,
1816 struct nfs_fattr *fp)
1819 fp->fa_nlink = txdr_unsigned(vap->va_nlink);
1820 fp->fa_uid = txdr_unsigned(vap->va_uid);
1821 fp->fa_gid = txdr_unsigned(vap->va_gid);
1822 if (nfsd->nd_flag & ND_NFSV3) {
1823 fp->fa_type = vtonfsv3_type(vap->va_type);
1824 fp->fa_mode = vtonfsv3_mode(vap->va_mode);
1825 txdr_hyper(vap->va_size, &fp->fa3_size);
1826 txdr_hyper(vap->va_bytes, &fp->fa3_used);
1827 fp->fa3_rdev.specdata1 = txdr_unsigned(umajor(vap->va_rdev));
1828 fp->fa3_rdev.specdata2 = txdr_unsigned(uminor(vap->va_rdev));
1829 fp->fa3_fsid.nfsuquad[0] = 0;
1830 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid);
1831 fp->fa3_fileid.nfsuquad[0] = 0;
1832 fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(vap->va_fileid);
1833 txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime);
1834 txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime);
1835 txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime);
1837 fp->fa_type = vtonfsv2_type(vap->va_type);
1838 fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1839 fp->fa2_size = txdr_unsigned(vap->va_size);
1840 fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize);
1841 if (vap->va_type == VFIFO)
1842 fp->fa2_rdev = 0xffffffff;
1844 fp->fa2_rdev = txdr_unsigned(vap->va_rdev);
1845 fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE);
1846 fp->fa2_fsid = txdr_unsigned(vap->va_fsid);
1847 fp->fa2_fileid = txdr_unsigned(vap->va_fileid);
1848 txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime);
1849 txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime);
1850 txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime);
1855 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
1856 * - look up fsid in mount list (if not found ret error)
1857 * - get vp and export rights by calling VFS_FHTOVP()
1858 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
1859 * - if not lockflag unlock it with VOP_UNLOCK()
1862 nfsrv_fhtovp(fhandle_t *fhp, int lockflag, struct vnode **vpp,
1863 struct ucred *cred, struct nfssvc_sock *slp, struct sockaddr *nam,
1864 int *rdonlyp, int kerbflag, int pubflag)
1866 struct thread *td = curthread; /* XXX */
1869 struct ucred *credanon;
1871 #ifdef MNT_EXNORESPORT /* XXX needs mountd and /etc/exports help yet */
1872 struct sockaddr_int *saddr;
1875 *vpp = (struct vnode *)0;
1877 if (nfs_ispublicfh(fhp)) {
1878 if (!pubflag || !nfs_pub.np_valid)
1880 fhp = &nfs_pub.np_handle;
1883 mp = vfs_getvfs(&fhp->fh_fsid);
1886 error = VFS_CHECKEXP(mp, nam, &exflags, &credanon);
1889 error = VFS_FHTOVP(mp, &fhp->fh_fid, vpp);
1892 #ifdef MNT_EXNORESPORT
1893 if (!(exflags & (MNT_EXNORESPORT|MNT_EXPUBLIC))) {
1894 saddr = (struct sockaddr_in *)nam;
1895 if (saddr->sin_family == AF_INET &&
1896 ntohs(saddr->sin_port) >= IPPORT_RESERVED) {
1899 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1904 * Check/setup credentials.
1906 if (exflags & MNT_EXKERB) {
1910 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1912 } else if (kerbflag) {
1915 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1916 } else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1917 cred->cr_uid = credanon->cr_uid;
1918 for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++)
1919 cred->cr_groups[i] = credanon->cr_groups[i];
1920 cred->cr_ngroups = i;
1922 if (exflags & MNT_EXRDONLY)
1927 nfsrv_object_create(*vpp);
1930 VOP_UNLOCK(*vpp, NULL, 0, td);
1936 * WebNFS: check if a filehandle is a public filehandle. For v3, this
1937 * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has
1938 * transformed this to all zeroes in both cases, so check for it.
1941 nfs_ispublicfh(fhandle_t *fhp)
1943 char *cp = (char *)fhp;
1946 for (i = 0; i < NFSX_V3FH; i++)
1952 #endif /* NFS_NOSERVER */
1954 * This function compares two net addresses by family and returns TRUE
1955 * if they are the same host.
1956 * If there is any doubt, return FALSE.
1957 * The AF_INET family is handled as a special case so that address mbufs
1958 * don't need to be saved to store "struct in_addr", which is only 4 bytes.
1961 netaddr_match(int family, union nethostaddr *haddr, struct sockaddr *nam)
1963 struct sockaddr_in *inetaddr;
1967 inetaddr = (struct sockaddr_in *)nam;
1968 if (inetaddr->sin_family == AF_INET &&
1969 inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
1978 static nfsuint64 nfs_nullcookie = { { 0, 0 } };
1980 * This function finds the directory cookie that corresponds to the
1981 * logical byte offset given.
1984 nfs_getcookie(struct nfsnode *np, off_t off, int add)
1986 struct nfsdmap *dp, *dp2;
1989 pos = (uoff_t)off / NFS_DIRBLKSIZ;
1990 if (pos == 0 || off < 0) {
1993 panic("nfs getcookie add at <= 0");
1995 return (&nfs_nullcookie);
1998 dp = np->n_cookies.lh_first;
2001 MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap),
2002 M_NFSDIROFF, M_WAITOK);
2003 dp->ndm_eocookie = 0;
2004 LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
2006 return ((nfsuint64 *)0);
2008 while (pos >= NFSNUMCOOKIES) {
2009 pos -= NFSNUMCOOKIES;
2010 if (dp->ndm_list.le_next) {
2011 if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
2012 pos >= dp->ndm_eocookie)
2013 return ((nfsuint64 *)0);
2014 dp = dp->ndm_list.le_next;
2016 MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap),
2017 M_NFSDIROFF, M_WAITOK);
2018 dp2->ndm_eocookie = 0;
2019 LIST_INSERT_AFTER(dp, dp2, ndm_list);
2022 return ((nfsuint64 *)0);
2024 if (pos >= dp->ndm_eocookie) {
2026 dp->ndm_eocookie = pos + 1;
2028 return ((nfsuint64 *)0);
2030 return (&dp->ndm_cookies[pos]);
2034 * Invalidate cached directory information, except for the actual directory
2035 * blocks (which are invalidated separately).
2036 * Done mainly to avoid the use of stale offset cookies.
2039 nfs_invaldir(struct vnode *vp)
2041 struct nfsnode *np = VTONFS(vp);
2044 if (vp->v_type != VDIR)
2045 panic("nfs: invaldir not dir");
2047 np->n_direofoffset = 0;
2048 np->n_cookieverf.nfsuquad[0] = 0;
2049 np->n_cookieverf.nfsuquad[1] = 0;
2050 if (np->n_cookies.lh_first)
2051 np->n_cookies.lh_first->ndm_eocookie = 0;
2055 * The write verifier has changed (probably due to a server reboot), so all
2056 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
2057 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
2058 * and B_CLUSTEROK flags. Once done the new write verifier can be set for the
2061 * B_CLUSTEROK must be cleared along with B_NEEDCOMMIT because stage 1 data
2062 * writes are not clusterable.
2065 nfs_clearcommit(struct mount *mp)
2067 struct vnode *vp, *nvp;
2068 struct buf *bp, *nbp;
2072 lwkt_gettoken(&ilock, &mntvnode_token);
2074 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp; vp = nvp) {
2075 nvp = TAILQ_NEXT(vp, v_nmntvnodes); /* ZZZ */
2076 if (vp->v_flag & VPLACEMARKER)
2078 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2079 nbp = TAILQ_NEXT(bp, b_vnbufs);
2080 if (BUF_REFCNT(bp) == 0 &&
2081 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2082 == (B_DELWRI | B_NEEDCOMMIT)) {
2083 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
2088 lwkt_reltoken(&ilock);
2091 #ifndef NFS_NOSERVER
2093 * Map errnos to NFS error numbers. For Version 3 also filter out error
2094 * numbers not specified for the associated procedure.
2097 nfsrv_errmap(struct nfsrv_descript *nd, int err)
2099 short *defaulterrp, *errp;
2101 if (nd->nd_flag & ND_NFSV3) {
2102 if (nd->nd_procnum <= NFSPROC_COMMIT) {
2103 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
2107 else if (*errp > err)
2110 return ((int)*defaulterrp);
2112 return (err & 0xffff);
2115 return ((int)nfsrv_v2errmap[err - 1]);
2120 nfsrv_object_create(struct vnode *vp)
2122 struct thread *td = curthread;
2124 if (vp == NULL || vp->v_type != VREG)
2126 return (vfs_object_create(vp, td));
2130 * Sort the group list in increasing numerical order.
2131 * (Insertion sort by Chris Torek, who was grossed out by the bubble sort
2132 * that used to be here.)
2135 nfsrvw_sort(gid_t *list, int num)
2140 /* Insertion sort. */
2141 for (i = 1; i < num; i++) {
2143 /* find correct slot for value v, moving others up */
2144 for (j = i; --j >= 0 && v < list[j];)
2145 list[j + 1] = list[j];
2151 * copy credentials making sure that the result can be compared with bcmp().
2154 nfsrv_setcred(struct ucred *incred, struct ucred *outcred)
2158 bzero((caddr_t)outcred, sizeof (struct ucred));
2159 outcred->cr_ref = 1;
2160 outcred->cr_uid = incred->cr_uid;
2161 outcred->cr_ngroups = incred->cr_ngroups;
2162 for (i = 0; i < incred->cr_ngroups; i++)
2163 outcred->cr_groups[i] = incred->cr_groups[i];
2164 nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups);
2166 #endif /* NFS_NOSERVER */