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: src/sys/nfs/nfs_subs.c,v 1.90.2.2 2001/10/25 19:18:53 dillon Exp $
41 * These functions support the macros and help fiddle mbuf chains for
42 * the nfs op functions. They do things like create the rpc header and
43 * copy data between mbuf chains and uio lists.
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
50 #include <sys/mount.h>
51 #include <sys/vnode.h>
52 #include <sys/namei.h>
54 #include <sys/socket.h>
56 #include <sys/malloc.h>
57 #include <sys/sysent.h>
58 #include <sys/syscall.h>
62 #include <vm/vm_object.h>
63 #include <vm/vm_extern.h>
64 #include <vm/vm_zone.h>
66 #include <nfs/rpcv2.h>
67 #include <nfs/nfsproto.h>
69 #include <nfs/nfsnode.h>
70 #include <nfs/xdr_subs.h>
71 #include <nfs/nfsm_subs.h>
72 #include <nfs/nfsmount.h>
73 #include <nfs/nqnfs.h>
74 #include <nfs/nfsrtt.h>
76 #include <netinet/in.h>
79 * Data items converted to xdr at startup, since they are constant
80 * This is kinda hokey, but may save a little time doing byte swaps
82 u_int32_t nfs_xdrneg1;
83 u_int32_t rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr,
84 rpc_mismatch, rpc_auth_unix, rpc_msgaccepted,
86 u_int32_t nfs_prog, nqnfs_prog, nfs_true, nfs_false;
88 /* And other global data */
89 static u_int32_t nfs_xid = 0;
90 static enum vtype nv2tov_type[8]= {
91 VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON, VNON
93 enum vtype nv3tov_type[8]= {
94 VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO
98 int nfs_pbuf_freecnt = -1; /* start out unlimited */
100 struct nfs_reqq nfs_reqq;
101 struct nfssvc_sockhead nfssvc_sockhead;
102 int nfssvc_sockhead_flag;
103 struct nfsd_head nfsd_head;
105 struct nfs_bufq nfs_bufq;
106 struct nqtimerhead nqtimerhead;
107 struct nqfhhashhead *nqfhhashtbl;
110 static void (*nfs_prev_lease_updatetime) __P((int));
111 static int nfs_prev_nfssvc_sy_narg;
112 static sy_call_t *nfs_prev_nfssvc_sy_call;
116 static vop_t *nfs_prev_vop_lease_check;
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 time_t nqnfsstarttime;
546 extern int nqsrv_clockskew;
547 extern int nqsrv_writeslack;
548 extern int nqsrv_maxlease;
549 extern struct nfsstats nfsstats;
550 extern int nqnfs_piggy[NFS_NPROCS];
551 extern nfstype nfsv2_type[9];
552 extern nfstype nfsv3_type[9];
553 extern struct nfsnodehashhead *nfsnodehashtbl;
554 extern u_long nfsnodehash;
557 extern int nfssvc(struct proc *, struct nfssvc_args *, int *);
559 LIST_HEAD(nfsnodehashhead, nfsnode);
561 int nfs_webnamei __P((struct nameidata *, struct vnode *, struct proc *));
569 return ((u_quad_t)tv.tv_sec * 1000000 + (u_quad_t)tv.tv_usec);
573 * Create the header for an rpc request packet
574 * The hsiz is the size of the rest of the nfs request header.
575 * (just used to decide if a cluster is a good idea)
578 nfsm_reqh(vp, procid, hsiz, bposp)
584 register struct mbuf *mb;
585 register u_int32_t *tl;
586 register caddr_t bpos;
588 struct nfsmount *nmp;
591 MGET(mb, M_WAIT, MT_DATA);
592 if (hsiz >= MINCLSIZE)
595 bpos = mtod(mb, caddr_t);
598 * For NQNFS, add lease request.
601 nmp = VFSTONFS(vp->v_mount);
602 if (nmp->nm_flag & NFSMNT_NQNFS) {
603 nqflag = NQNFS_NEEDLEASE(vp, procid);
605 nfsm_build(tl, u_int32_t *, 2*NFSX_UNSIGNED);
606 *tl++ = txdr_unsigned(nqflag);
607 *tl = txdr_unsigned(nmp->nm_leaseterm);
609 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
614 /* Finally, return values */
620 * Build the RPC header and fill in the authorization info.
621 * The authorization string argument is only used when the credentials
622 * come from outside of the kernel.
623 * Returns the head of the mbuf list.
626 nfsm_rpchead(cr, nmflag, procid, auth_type, auth_len, auth_str, verf_len,
627 verf_str, mrest, mrest_len, mbp, xidp)
628 register struct ucred *cr;
641 register struct mbuf *mb;
642 register u_int32_t *tl;
643 register caddr_t bpos;
645 struct mbuf *mreq, *mb2;
646 int siz, grpsiz, authsiz;
648 authsiz = nfsm_rndup(auth_len);
649 MGETHDR(mb, M_WAIT, MT_DATA);
650 if ((authsiz + 10 * NFSX_UNSIGNED) >= MINCLSIZE) {
652 } else if ((authsiz + 10 * NFSX_UNSIGNED) < MHLEN) {
653 MH_ALIGN(mb, authsiz + 10 * NFSX_UNSIGNED);
655 MH_ALIGN(mb, 8 * NFSX_UNSIGNED);
659 bpos = mtod(mb, caddr_t);
662 * First the RPC header.
664 nfsm_build(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
666 /* Get a pretty random xid to start with */
670 * Skip zero xid if it should ever happen.
675 *tl++ = *xidp = txdr_unsigned(nfs_xid);
678 if (nmflag & NFSMNT_NQNFS) {
679 *tl++ = txdr_unsigned(NQNFS_PROG);
680 *tl++ = txdr_unsigned(NQNFS_VER3);
682 *tl++ = txdr_unsigned(NFS_PROG);
683 if (nmflag & NFSMNT_NFSV3)
684 *tl++ = txdr_unsigned(NFS_VER3);
686 *tl++ = txdr_unsigned(NFS_VER2);
688 if (nmflag & NFSMNT_NFSV3)
689 *tl++ = txdr_unsigned(procid);
691 *tl++ = txdr_unsigned(nfsv2_procid[procid]);
694 * And then the authorization cred.
696 *tl++ = txdr_unsigned(auth_type);
697 *tl = txdr_unsigned(authsiz);
700 nfsm_build(tl, u_int32_t *, auth_len);
701 *tl++ = 0; /* stamp ?? */
702 *tl++ = 0; /* NULL hostname */
703 *tl++ = txdr_unsigned(cr->cr_uid);
704 *tl++ = txdr_unsigned(cr->cr_groups[0]);
705 grpsiz = (auth_len >> 2) - 5;
706 *tl++ = txdr_unsigned(grpsiz);
707 for (i = 1; i <= grpsiz; i++)
708 *tl++ = txdr_unsigned(cr->cr_groups[i]);
713 if (M_TRAILINGSPACE(mb) == 0) {
714 MGET(mb2, M_WAIT, MT_DATA);
715 if (siz >= MINCLSIZE)
720 bpos = mtod(mb, caddr_t);
722 i = min(siz, M_TRAILINGSPACE(mb));
723 bcopy(auth_str, bpos, i);
729 if ((siz = (nfsm_rndup(auth_len) - auth_len)) > 0) {
730 for (i = 0; i < siz; i++)
738 * And the verifier...
740 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
742 *tl++ = txdr_unsigned(RPCAUTH_KERB4);
743 *tl = txdr_unsigned(verf_len);
746 if (M_TRAILINGSPACE(mb) == 0) {
747 MGET(mb2, M_WAIT, MT_DATA);
748 if (siz >= MINCLSIZE)
753 bpos = mtod(mb, caddr_t);
755 i = min(siz, M_TRAILINGSPACE(mb));
756 bcopy(verf_str, bpos, i);
762 if ((siz = (nfsm_rndup(verf_len) - verf_len)) > 0) {
763 for (i = 0; i < siz; i++)
768 *tl++ = txdr_unsigned(RPCAUTH_NULL);
772 mreq->m_pkthdr.len = authsiz + 10 * NFSX_UNSIGNED + mrest_len;
773 mreq->m_pkthdr.rcvif = (struct ifnet *)0;
779 * copies mbuf chain to the uio scatter/gather list
782 nfsm_mbuftouio(mrep, uiop, siz, dpos)
784 register struct uio *uiop;
788 register char *mbufcp, *uiocp;
789 register int xfer, left, len;
790 register struct mbuf *mp;
796 len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
797 rem = nfsm_rndup(siz)-siz;
799 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
801 left = uiop->uio_iov->iov_len;
802 uiocp = uiop->uio_iov->iov_base;
811 mbufcp = mtod(mp, caddr_t);
814 xfer = (left > len) ? len : left;
817 if (uiop->uio_iov->iov_op != NULL)
818 (*(uiop->uio_iov->iov_op))
819 (mbufcp, uiocp, xfer);
822 if (uiop->uio_segflg == UIO_SYSSPACE)
823 bcopy(mbufcp, uiocp, xfer);
825 copyout(mbufcp, uiocp, xfer);
830 uiop->uio_offset += xfer;
831 uiop->uio_resid -= xfer;
833 if (uiop->uio_iov->iov_len <= siz) {
837 uiop->uio_iov->iov_base += uiosiz;
838 uiop->uio_iov->iov_len -= uiosiz;
846 error = nfs_adv(mrep, dpos, rem, len);
854 * copies a uio scatter/gather list to an mbuf chain.
855 * NOTE: can ony handle iovcnt == 1
858 nfsm_uiotombuf(uiop, mq, siz, bpos)
859 register struct uio *uiop;
864 register char *uiocp;
865 register struct mbuf *mp, *mp2;
866 register int xfer, left, mlen;
867 int uiosiz, clflg, rem;
871 if (uiop->uio_iovcnt != 1)
872 panic("nfsm_uiotombuf: iovcnt != 1");
875 if (siz > MLEN) /* or should it >= MCLBYTES ?? */
879 rem = nfsm_rndup(siz)-siz;
882 left = uiop->uio_iov->iov_len;
883 uiocp = uiop->uio_iov->iov_base;
888 mlen = M_TRAILINGSPACE(mp);
890 MGET(mp, M_WAIT, MT_DATA);
896 mlen = M_TRAILINGSPACE(mp);
898 xfer = (left > mlen) ? mlen : left;
901 if (uiop->uio_iov->iov_op != NULL)
902 (*(uiop->uio_iov->iov_op))
903 (uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
906 if (uiop->uio_segflg == UIO_SYSSPACE)
907 bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
909 copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
913 uiop->uio_offset += xfer;
914 uiop->uio_resid -= xfer;
916 uiop->uio_iov->iov_base += uiosiz;
917 uiop->uio_iov->iov_len -= uiosiz;
921 if (rem > M_TRAILINGSPACE(mp)) {
922 MGET(mp, M_WAIT, MT_DATA);
926 cp = mtod(mp, caddr_t)+mp->m_len;
927 for (left = 0; left < rem; left++)
932 *bpos = mtod(mp, caddr_t)+mp->m_len;
938 * Help break down an mbuf chain by setting the first siz bytes contiguous
939 * pointed to by returned val.
940 * This is used by the macros nfsm_dissect and nfsm_dissecton for tough
941 * cases. (The macros use the vars. dpos and dpos2)
944 nfsm_disct(mdp, dposp, siz, left, cp2)
951 register struct mbuf *mp, *mp2;
952 register int siz2, xfer;
957 *mdp = mp = mp->m_next;
961 *dposp = mtod(mp, caddr_t);
966 } else if (mp->m_next == NULL) {
968 } else if (siz > MHLEN) {
969 panic("nfs S too big");
971 MGET(mp2, M_WAIT, MT_DATA);
972 mp2->m_next = mp->m_next;
976 *cp2 = p = mtod(mp, caddr_t);
977 bcopy(*dposp, p, left); /* Copy what was left */
981 /* Loop around copying up the siz2 bytes */
985 xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2;
987 bcopy(mtod(mp2, caddr_t), p, xfer);
998 *dposp = mtod(mp2, caddr_t);
1004 * Advance the position in the mbuf chain.
1007 nfs_adv(mdp, dposp, offs, left)
1013 register struct mbuf *m;
1026 *dposp = mtod(m, caddr_t)+offs;
1031 * Copy a string into mbufs for the hard cases...
1034 nfsm_strtmbuf(mb, bpos, cp, siz)
1040 register struct mbuf *m1 = NULL, *m2;
1041 long left, xfer, len, tlen;
1047 left = M_TRAILINGSPACE(m2);
1049 tl = ((u_int32_t *)(*bpos));
1050 *tl++ = txdr_unsigned(siz);
1052 left -= NFSX_UNSIGNED;
1053 m2->m_len += NFSX_UNSIGNED;
1055 bcopy(cp, (caddr_t) tl, left);
1062 /* Loop around adding mbufs */
1064 MGET(m1, M_WAIT, MT_DATA);
1067 m1->m_len = NFSMSIZ(m1);
1070 tl = mtod(m1, u_int32_t *);
1073 *tl++ = txdr_unsigned(siz);
1074 m1->m_len -= NFSX_UNSIGNED;
1075 tlen = NFSX_UNSIGNED;
1078 if (siz < m1->m_len) {
1079 len = nfsm_rndup(siz);
1082 *(tl+(xfer>>2)) = 0;
1084 xfer = len = m1->m_len;
1086 bcopy(cp, (caddr_t) tl, xfer);
1087 m1->m_len = len+tlen;
1092 *bpos = mtod(m1, caddr_t)+m1->m_len;
1097 * Called once to initialize data structures...
1101 struct vfsconf *vfsp;
1105 nfsmount_zone = zinit("NFSMOUNT", sizeof(struct nfsmount), 0, 0, 1);
1107 nfs_mount_type = vfsp->vfc_typenum;
1109 rpc_vers = txdr_unsigned(RPC_VER2);
1110 rpc_call = txdr_unsigned(RPC_CALL);
1111 rpc_reply = txdr_unsigned(RPC_REPLY);
1112 rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
1113 rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
1114 rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
1115 rpc_autherr = txdr_unsigned(RPC_AUTHERR);
1116 rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
1117 rpc_auth_kerb = txdr_unsigned(RPCAUTH_KERB4);
1118 nfs_prog = txdr_unsigned(NFS_PROG);
1119 nqnfs_prog = txdr_unsigned(NQNFS_PROG);
1120 nfs_true = txdr_unsigned(TRUE);
1121 nfs_false = txdr_unsigned(FALSE);
1122 nfs_xdrneg1 = txdr_unsigned(-1);
1123 nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
1126 /* Ensure async daemons disabled */
1127 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
1128 nfs_iodwant[i] = (struct proc *)0;
1129 nfs_iodmount[i] = (struct nfsmount *)0;
1131 nfs_nhinit(); /* Init the nfsnode table */
1132 #ifndef NFS_NOSERVER
1133 nfsrv_init(0); /* Init server data structures */
1134 nfsrv_initcache(); /* Init the server request cache */
1138 * Initialize the nqnfs server stuff.
1140 if (nqnfsstarttime == 0) {
1141 nqnfsstarttime = boottime.tv_sec + nqsrv_maxlease
1142 + nqsrv_clockskew + nqsrv_writeslack;
1143 NQLOADNOVRAM(nqnfsstarttime);
1144 CIRCLEQ_INIT(&nqtimerhead);
1145 nqfhhashtbl = hashinit(NQLCHSZ, M_NQLEASE, &nqfhhash);
1149 * Initialize reply list and start timer
1151 TAILQ_INIT(&nfs_reqq);
1156 * Set up lease_check and lease_updatetime so that other parts
1157 * of the system can call us, if we are loadable.
1159 #ifndef NFS_NOSERVER
1160 nfs_prev_vop_lease_check = default_vnodeop_p[VOFFSET(vop_lease)];
1161 default_vnodeop_p[VOFFSET(vop_lease)] = (vop_t *)nqnfs_vop_lease_check;
1163 nfs_prev_lease_updatetime = lease_updatetime;
1164 lease_updatetime = nfs_lease_updatetime;
1165 nfs_prev_nfssvc_sy_narg = sysent[SYS_nfssvc].sy_narg;
1166 sysent[SYS_nfssvc].sy_narg = 2;
1167 nfs_prev_nfssvc_sy_call = sysent[SYS_nfssvc].sy_call;
1168 sysent[SYS_nfssvc].sy_call = (sy_call_t *)nfssvc;
1170 nfs_pbuf_freecnt = nswbuf / 2 + 1;
1177 struct vfsconf *vfsp;
1180 untimeout(nfs_timer, (void *)NULL, nfs_timer_handle);
1181 nfs_mount_type = -1;
1182 #ifndef NFS_NOSERVER
1183 default_vnodeop_p[VOFFSET(vop_lease)] = nfs_prev_vop_lease_check;
1185 lease_updatetime = nfs_prev_lease_updatetime;
1186 sysent[SYS_nfssvc].sy_narg = nfs_prev_nfssvc_sy_narg;
1187 sysent[SYS_nfssvc].sy_call = nfs_prev_nfssvc_sy_call;
1192 * Attribute cache routines.
1193 * nfs_loadattrcache() - loads or updates the cache contents from attributes
1194 * that are on the mbuf list
1195 * nfs_getattrcache() - returns valid attributes if found in cache, returns
1200 * Load the attribute cache (that lives in the nfsnode entry) with
1201 * the values on the mbuf list and
1203 * copy the attributes to *vaper
1206 nfs_loadattrcache(vpp, mdp, dposp, vaper, dontshrink)
1210 struct vattr *vaper;
1213 register struct vnode *vp = *vpp;
1214 register struct vattr *vap;
1215 register struct nfs_fattr *fp;
1216 register struct nfsnode *np;
1217 register int32_t t1;
1219 int error = 0, rdev;
1223 struct timespec mtime;
1224 int v3 = NFS_ISV3(vp);
1227 t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
1228 if ((error = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, &cp2)) != 0)
1230 fp = (struct nfs_fattr *)cp2;
1232 vtyp = nfsv3tov_type(fp->fa_type);
1233 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1234 rdev = makeudev(fxdr_unsigned(int, fp->fa3_rdev.specdata1),
1235 fxdr_unsigned(int, fp->fa3_rdev.specdata2));
1236 fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
1238 vtyp = nfsv2tov_type(fp->fa_type);
1239 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1243 * The duplicate information returned in fa_type and fa_mode
1244 * is an ambiguity in the NFS version 2 protocol.
1246 * VREG should be taken literally as a regular file. If a
1247 * server intents to return some type information differently
1248 * in the upper bits of the mode field (e.g. for sockets, or
1249 * FIFOs), NFSv2 mandates fa_type to be VNON. Anyway, we
1250 * leave the examination of the mode bits even in the VREG
1251 * case to avoid breakage for bogus servers, but we make sure
1252 * that there are actually type bits set in the upper part of
1253 * fa_mode (and failing that, trust the va_type field).
1255 * NFSv3 cleared the issue, and requires fa_mode to not
1256 * contain any type information (while also introduing sockets
1257 * and FIFOs for fa_type).
1259 if (vtyp == VNON || (vtyp == VREG && (vmode & S_IFMT) != 0))
1260 vtyp = IFTOVT(vmode);
1261 rdev = fxdr_unsigned(int32_t, fp->fa2_rdev);
1262 fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
1265 * Really ugly NFSv2 kludge.
1267 if (vtyp == VCHR && rdev == 0xffffffff)
1272 * If v_type == VNON it is a new node, so fill in the v_type,
1273 * n_mtime fields. Check to see if it represents a special
1274 * device, and if so, check for a possible alias. Once the
1275 * correct vnode has been obtained, fill in the rest of the
1279 if (vp->v_type != vtyp) {
1281 if (vp->v_type == VFIFO) {
1282 vp->v_op = fifo_nfsv2nodeop_p;
1284 if (vp->v_type == VCHR || vp->v_type == VBLK) {
1285 vp->v_op = spec_nfsv2nodeop_p;
1286 addaliasu(vp, rdev);
1288 np->n_mtime = mtime.tv_sec;
1291 vap->va_type = vtyp;
1292 vap->va_mode = (vmode & 07777);
1293 vap->va_rdev = rdev;
1294 vap->va_mtime = mtime;
1295 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
1297 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1298 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1299 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1300 vap->va_size = fxdr_hyper(&fp->fa3_size);
1301 vap->va_blocksize = NFS_FABLKSIZE;
1302 vap->va_bytes = fxdr_hyper(&fp->fa3_used);
1303 vap->va_fileid = fxdr_unsigned(int32_t,
1304 fp->fa3_fileid.nfsuquad[1]);
1305 fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
1306 fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
1308 vap->va_filerev = 0;
1310 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1311 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1312 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1313 vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
1314 vap->va_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize);
1315 vap->va_bytes = (u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks)
1317 vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
1318 fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
1320 vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t,
1321 fp->fa2_ctime.nfsv2_sec);
1322 vap->va_ctime.tv_nsec = 0;
1323 vap->va_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec);
1324 vap->va_filerev = 0;
1326 np->n_attrstamp = time_second;
1327 if (vap->va_size != np->n_size) {
1328 if (vap->va_type == VREG) {
1329 if (dontshrink && vap->va_size < np->n_size) {
1331 * We've been told not to shrink the file;
1332 * zero np->n_attrstamp to indicate that
1333 * the attributes are stale.
1335 vap->va_size = np->n_size;
1336 np->n_attrstamp = 0;
1337 } else if (np->n_flag & NMODIFIED) {
1338 if (vap->va_size < np->n_size)
1339 vap->va_size = np->n_size;
1341 np->n_size = vap->va_size;
1343 np->n_size = vap->va_size;
1345 vnode_pager_setsize(vp, np->n_size);
1347 np->n_size = vap->va_size;
1350 if (vaper != NULL) {
1351 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
1352 if (np->n_flag & NCHG) {
1353 if (np->n_flag & NACC)
1354 vaper->va_atime = np->n_atim;
1355 if (np->n_flag & NUPD)
1356 vaper->va_mtime = np->n_mtim;
1363 #include <sys/sysctl.h>
1364 SYSCTL_DECL(_vfs_nfs);
1365 static int nfs_acdebug;
1366 SYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, "");
1370 * Check the time stamp
1371 * If the cache is valid, copy contents to *vap and return 0
1372 * otherwise return an error
1375 nfs_getattrcache(vp, vaper)
1376 register struct vnode *vp;
1377 struct vattr *vaper;
1379 register struct nfsnode *np;
1380 register struct vattr *vap;
1381 struct nfsmount *nmp;
1386 nmp = VFSTONFS(vp->v_mount);
1387 /* XXX n_mtime doesn't seem to be updated on a miss-and-reload */
1388 timeo = (time_second - np->n_mtime) / 10;
1392 printf("nfs_getattrcache: initial timeo = %d\n", timeo);
1395 if (vap->va_type == VDIR) {
1396 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin)
1397 timeo = nmp->nm_acdirmin;
1398 else if (timeo > nmp->nm_acdirmax)
1399 timeo = nmp->nm_acdirmax;
1401 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin)
1402 timeo = nmp->nm_acregmin;
1403 else if (timeo > nmp->nm_acregmax)
1404 timeo = nmp->nm_acregmax;
1408 if (nfs_acdebug > 2)
1409 printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n",
1410 nmp->nm_acregmin, nmp->nm_acregmax,
1411 nmp->nm_acdirmin, nmp->nm_acdirmax);
1414 printf("nfs_getattrcache: age = %d; final timeo = %d\n",
1415 (time_second - np->n_attrstamp), timeo);
1418 if ((time_second - np->n_attrstamp) >= timeo) {
1419 nfsstats.attrcache_misses++;
1422 nfsstats.attrcache_hits++;
1423 if (vap->va_size != np->n_size) {
1424 if (vap->va_type == VREG) {
1425 if (np->n_flag & NMODIFIED) {
1426 if (vap->va_size < np->n_size)
1427 vap->va_size = np->n_size;
1429 np->n_size = vap->va_size;
1431 np->n_size = vap->va_size;
1433 vnode_pager_setsize(vp, np->n_size);
1435 np->n_size = vap->va_size;
1438 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
1439 if (np->n_flag & NCHG) {
1440 if (np->n_flag & NACC)
1441 vaper->va_atime = np->n_atim;
1442 if (np->n_flag & NUPD)
1443 vaper->va_mtime = np->n_mtim;
1448 #ifndef NFS_NOSERVER
1450 * Set up nameidata for a lookup() call and do it.
1452 * If pubflag is set, this call is done for a lookup operation on the
1453 * public filehandle. In that case we allow crossing mountpoints and
1454 * absolute pathnames. However, the caller is expected to check that
1455 * the lookup result is within the public fs, and deny access if
1458 * nfs_namei() clears out garbage fields that namei() might leave garbage.
1459 * This is mainly ni_vp and ni_dvp when an error occurs, and ni_dvp when no
1460 * error occurs but the parent was not requested.
1462 * dirp may be set whether an error is returned or not, and must be
1463 * released by the caller.
1466 nfs_namei(ndp, fhp, len, slp, nam, mdp, dposp, retdirp, p, kerbflag, pubflag)
1467 register struct nameidata *ndp;
1470 struct nfssvc_sock *slp;
1471 struct sockaddr *nam;
1474 struct vnode **retdirp;
1476 int kerbflag, pubflag;
1478 register int i, rem;
1479 register struct mbuf *md;
1480 register char *fromcp, *tocp, *cp;
1484 int error, rdonly, linklen;
1485 struct componentname *cnp = &ndp->ni_cnd;
1487 *retdirp = (struct vnode *)0;
1488 cnp->cn_pnbuf = zalloc(namei_zone);
1491 * Copy the name from the mbuf list to ndp->ni_pnbuf
1492 * and set the various ndp fields appropriately.
1495 tocp = cnp->cn_pnbuf;
1497 rem = mtod(md, caddr_t) + md->m_len - fromcp;
1498 for (i = 0; i < len; i++) {
1505 fromcp = mtod(md, caddr_t);
1508 if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) {
1512 *tocp++ = *fromcp++;
1518 len = nfsm_rndup(len)-len;
1522 else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
1527 * Extract and set starting directory.
1529 error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp,
1530 nam, &rdonly, kerbflag, pubflag);
1533 if (dp->v_type != VDIR) {
1540 cnp->cn_flags |= RDONLY;
1543 * Set return directory. Reference to dp is implicitly transfered
1544 * to the returned pointer
1550 * Oh joy. For WebNFS, handle those pesky '%' escapes,
1551 * and the 'native path' indicator.
1553 cp = zalloc(namei_zone);
1554 fromcp = cnp->cn_pnbuf;
1556 if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) {
1557 switch ((unsigned char)*fromcp) {
1558 case WEBNFS_NATIVE_CHAR:
1560 * 'Native' path for us is the same
1561 * as a path according to the NFS spec,
1562 * just skip the escape char.
1567 * More may be added in the future, range 0x80-0xff
1571 zfree(namei_zone, cp);
1576 * Translate the '%' escapes, URL-style.
1578 while (*fromcp != '\0') {
1579 if (*fromcp == WEBNFS_ESC_CHAR) {
1580 if (fromcp[1] != '\0' && fromcp[2] != '\0') {
1582 *tocp++ = HEXSTRTOI(fromcp);
1587 zfree(namei_zone, cp);
1591 *tocp++ = *fromcp++;
1594 zfree(namei_zone, cnp->cn_pnbuf);
1598 ndp->ni_pathlen = (tocp - cnp->cn_pnbuf) + 1;
1599 ndp->ni_segflg = UIO_SYSSPACE;
1602 ndp->ni_rootdir = rootvnode;
1603 ndp->ni_loopcnt = 0;
1604 if (cnp->cn_pnbuf[0] == '/')
1607 cnp->cn_flags |= NOCROSSMOUNT;
1611 * Initialize for scan, set ni_startdir and bump ref on dp again
1612 * becuase lookup() will dereference ni_startdir.
1617 ndp->ni_startdir = dp;
1620 cnp->cn_nameptr = cnp->cn_pnbuf;
1622 * Call lookup() to do the real work. If an error occurs,
1623 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
1624 * we do not have to dereference anything before returning.
1625 * In either case ni_startdir will be dereferenced and NULLed
1628 error = lookup(ndp);
1633 * Check for encountering a symbolic link. Trivial
1634 * termination occurs if no symlink encountered.
1635 * Note: zfree is safe because error is 0, so we will
1636 * not zfree it again when we break.
1638 if ((cnp->cn_flags & ISSYMLINK) == 0) {
1639 nfsrv_object_create(ndp->ni_vp);
1640 if (cnp->cn_flags & (SAVENAME | SAVESTART))
1641 cnp->cn_flags |= HASBUF;
1643 zfree(namei_zone, cnp->cn_pnbuf);
1650 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
1651 VOP_UNLOCK(ndp->ni_dvp, 0, p);
1657 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
1661 if (ndp->ni_pathlen > 1)
1662 cp = zalloc(namei_zone);
1666 aiov.iov_len = MAXPATHLEN;
1667 auio.uio_iov = &aiov;
1668 auio.uio_iovcnt = 1;
1669 auio.uio_offset = 0;
1670 auio.uio_rw = UIO_READ;
1671 auio.uio_segflg = UIO_SYSSPACE;
1672 auio.uio_procp = (struct proc *)0;
1673 auio.uio_resid = MAXPATHLEN;
1674 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
1677 if (ndp->ni_pathlen > 1)
1678 zfree(namei_zone, cp);
1684 linklen = MAXPATHLEN - auio.uio_resid;
1689 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
1690 error = ENAMETOOLONG;
1695 * Adjust or replace path
1697 if (ndp->ni_pathlen > 1) {
1698 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
1699 zfree(namei_zone, cnp->cn_pnbuf);
1702 cnp->cn_pnbuf[linklen] = '\0';
1703 ndp->ni_pathlen += linklen;
1706 * Cleanup refs for next loop and check if root directory
1707 * should replace current directory. Normally ni_dvp
1708 * becomes the new base directory and is cleaned up when
1709 * we loop. Explicitly null pointers after invalidation
1710 * to clarify operation.
1715 if (cnp->cn_pnbuf[0] == '/') {
1717 ndp->ni_dvp = ndp->ni_rootdir;
1720 ndp->ni_startdir = ndp->ni_dvp;
1725 * nfs_namei() guarentees that fields will not contain garbage
1726 * whether an error occurs or not. This allows the caller to track
1727 * cleanup state trivially.
1731 zfree(namei_zone, cnp->cn_pnbuf);
1734 ndp->ni_startdir = NULL;
1735 cnp->cn_flags &= ~HASBUF;
1736 } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) {
1743 * A fiddled version of m_adj() that ensures null fill to a long
1744 * boundary and only trims off the back end
1747 nfsm_adj(mp, len, nul)
1752 register struct mbuf *m;
1753 register int count, i;
1757 * Trim from tail. Scan the mbuf chain,
1758 * calculating its length and finding the last mbuf.
1759 * If the adjustment only affects this mbuf, then just
1760 * adjust and return. Otherwise, rescan and truncate
1761 * after the remaining size.
1767 if (m->m_next == (struct mbuf *)0)
1771 if (m->m_len > len) {
1774 cp = mtod(m, caddr_t)+m->m_len-nul;
1775 for (i = 0; i < nul; i++)
1784 * Correct length for chain is "count".
1785 * Find the mbuf with last data, adjust its length,
1786 * and toss data from remaining mbufs on chain.
1788 for (m = mp; m; m = m->m_next) {
1789 if (m->m_len >= count) {
1792 cp = mtod(m, caddr_t)+m->m_len-nul;
1793 for (i = 0; i < nul; i++)
1800 for (m = m->m_next;m;m = m->m_next)
1805 * Make these functions instead of macros, so that the kernel text size
1806 * doesn't get too big...
1809 nfsm_srvwcc(nfsd, before_ret, before_vap, after_ret, after_vap, mbp, bposp)
1810 struct nfsrv_descript *nfsd;
1812 register struct vattr *before_vap;
1814 struct vattr *after_vap;
1818 register struct mbuf *mb = *mbp, *mb2;
1819 register char *bpos = *bposp;
1820 register u_int32_t *tl;
1823 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1826 nfsm_build(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
1828 txdr_hyper(before_vap->va_size, tl);
1830 txdr_nfsv3time(&(before_vap->va_mtime), tl);
1832 txdr_nfsv3time(&(before_vap->va_ctime), tl);
1836 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp);
1840 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp)
1841 struct nfsrv_descript *nfsd;
1843 struct vattr *after_vap;
1847 register struct mbuf *mb = *mbp, *mb2;
1848 register char *bpos = *bposp;
1849 register u_int32_t *tl;
1850 register struct nfs_fattr *fp;
1853 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1856 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR);
1858 fp = (struct nfs_fattr *)tl;
1859 nfsm_srvfattr(nfsd, after_vap, fp);
1866 nfsm_srvfattr(nfsd, vap, fp)
1867 register struct nfsrv_descript *nfsd;
1868 register struct vattr *vap;
1869 register struct nfs_fattr *fp;
1872 fp->fa_nlink = txdr_unsigned(vap->va_nlink);
1873 fp->fa_uid = txdr_unsigned(vap->va_uid);
1874 fp->fa_gid = txdr_unsigned(vap->va_gid);
1875 if (nfsd->nd_flag & ND_NFSV3) {
1876 fp->fa_type = vtonfsv3_type(vap->va_type);
1877 fp->fa_mode = vtonfsv3_mode(vap->va_mode);
1878 txdr_hyper(vap->va_size, &fp->fa3_size);
1879 txdr_hyper(vap->va_bytes, &fp->fa3_used);
1880 fp->fa3_rdev.specdata1 = txdr_unsigned(umajor(vap->va_rdev));
1881 fp->fa3_rdev.specdata2 = txdr_unsigned(uminor(vap->va_rdev));
1882 fp->fa3_fsid.nfsuquad[0] = 0;
1883 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid);
1884 fp->fa3_fileid.nfsuquad[0] = 0;
1885 fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(vap->va_fileid);
1886 txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime);
1887 txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime);
1888 txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime);
1890 fp->fa_type = vtonfsv2_type(vap->va_type);
1891 fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1892 fp->fa2_size = txdr_unsigned(vap->va_size);
1893 fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize);
1894 if (vap->va_type == VFIFO)
1895 fp->fa2_rdev = 0xffffffff;
1897 fp->fa2_rdev = txdr_unsigned(vap->va_rdev);
1898 fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE);
1899 fp->fa2_fsid = txdr_unsigned(vap->va_fsid);
1900 fp->fa2_fileid = txdr_unsigned(vap->va_fileid);
1901 txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime);
1902 txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime);
1903 txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime);
1908 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
1909 * - look up fsid in mount list (if not found ret error)
1910 * - get vp and export rights by calling VFS_FHTOVP()
1911 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
1912 * - if not lockflag unlock it with VOP_UNLOCK()
1915 nfsrv_fhtovp(fhp, lockflag, vpp, cred, slp, nam, rdonlyp, kerbflag, pubflag)
1920 struct nfssvc_sock *slp;
1921 struct sockaddr *nam;
1926 struct proc *p = curproc; /* XXX */
1927 register struct mount *mp;
1929 struct ucred *credanon;
1931 #ifdef MNT_EXNORESPORT /* XXX needs mountd and /etc/exports help yet */
1932 struct sockaddr_int *saddr;
1935 *vpp = (struct vnode *)0;
1937 if (nfs_ispublicfh(fhp)) {
1938 if (!pubflag || !nfs_pub.np_valid)
1940 fhp = &nfs_pub.np_handle;
1943 mp = vfs_getvfs(&fhp->fh_fsid);
1946 error = VFS_CHECKEXP(mp, nam, &exflags, &credanon);
1949 error = VFS_FHTOVP(mp, &fhp->fh_fid, vpp);
1952 #ifdef MNT_EXNORESPORT
1953 if (!(exflags & (MNT_EXNORESPORT|MNT_EXPUBLIC))) {
1954 saddr = (struct sockaddr_in *)nam;
1955 if (saddr->sin_family == AF_INET &&
1956 ntohs(saddr->sin_port) >= IPPORT_RESERVED) {
1959 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1964 * Check/setup credentials.
1966 if (exflags & MNT_EXKERB) {
1970 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1972 } else if (kerbflag) {
1975 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1976 } else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1977 cred->cr_uid = credanon->cr_uid;
1978 for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++)
1979 cred->cr_groups[i] = credanon->cr_groups[i];
1980 cred->cr_ngroups = i;
1982 if (exflags & MNT_EXRDONLY)
1987 nfsrv_object_create(*vpp);
1990 VOP_UNLOCK(*vpp, 0, p);
1996 * WebNFS: check if a filehandle is a public filehandle. For v3, this
1997 * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has
1998 * transformed this to all zeroes in both cases, so check for it.
2004 char *cp = (char *)fhp;
2007 for (i = 0; i < NFSX_V3FH; i++)
2013 #endif /* NFS_NOSERVER */
2015 * This function compares two net addresses by family and returns TRUE
2016 * if they are the same host.
2017 * If there is any doubt, return FALSE.
2018 * The AF_INET family is handled as a special case so that address mbufs
2019 * don't need to be saved to store "struct in_addr", which is only 4 bytes.
2022 netaddr_match(family, haddr, nam)
2024 union nethostaddr *haddr;
2025 struct sockaddr *nam;
2027 register struct sockaddr_in *inetaddr;
2031 inetaddr = (struct sockaddr_in *)nam;
2032 if (inetaddr->sin_family == AF_INET &&
2033 inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
2042 static nfsuint64 nfs_nullcookie = { { 0, 0 } };
2044 * This function finds the directory cookie that corresponds to the
2045 * logical byte offset given.
2048 nfs_getcookie(np, off, add)
2049 register struct nfsnode *np;
2053 register struct nfsdmap *dp, *dp2;
2056 pos = (uoff_t)off / NFS_DIRBLKSIZ;
2057 if (pos == 0 || off < 0) {
2060 panic("nfs getcookie add at <= 0");
2062 return (&nfs_nullcookie);
2065 dp = np->n_cookies.lh_first;
2068 MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap),
2069 M_NFSDIROFF, M_WAITOK);
2070 dp->ndm_eocookie = 0;
2071 LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
2073 return ((nfsuint64 *)0);
2075 while (pos >= NFSNUMCOOKIES) {
2076 pos -= NFSNUMCOOKIES;
2077 if (dp->ndm_list.le_next) {
2078 if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
2079 pos >= dp->ndm_eocookie)
2080 return ((nfsuint64 *)0);
2081 dp = dp->ndm_list.le_next;
2083 MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap),
2084 M_NFSDIROFF, M_WAITOK);
2085 dp2->ndm_eocookie = 0;
2086 LIST_INSERT_AFTER(dp, dp2, ndm_list);
2089 return ((nfsuint64 *)0);
2091 if (pos >= dp->ndm_eocookie) {
2093 dp->ndm_eocookie = pos + 1;
2095 return ((nfsuint64 *)0);
2097 return (&dp->ndm_cookies[pos]);
2101 * Invalidate cached directory information, except for the actual directory
2102 * blocks (which are invalidated separately).
2103 * Done mainly to avoid the use of stale offset cookies.
2107 register struct vnode *vp;
2109 register struct nfsnode *np = VTONFS(vp);
2112 if (vp->v_type != VDIR)
2113 panic("nfs: invaldir not dir");
2115 np->n_direofoffset = 0;
2116 np->n_cookieverf.nfsuquad[0] = 0;
2117 np->n_cookieverf.nfsuquad[1] = 0;
2118 if (np->n_cookies.lh_first)
2119 np->n_cookies.lh_first->ndm_eocookie = 0;
2123 * The write verifier has changed (probably due to a server reboot), so all
2124 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
2125 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
2126 * and B_CLUSTEROK flags. Once done the new write verifier can be set for the
2129 * B_CLUSTEROK must be cleared along with B_NEEDCOMMIT because stage 1 data
2130 * writes are not clusterable.
2136 register struct vnode *vp, *nvp;
2137 register struct buf *bp, *nbp;
2142 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp; vp = nvp) {
2143 if (vp->v_mount != mp) /* Paranoia */
2145 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
2146 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2147 nbp = TAILQ_NEXT(bp, b_vnbufs);
2148 if (BUF_REFCNT(bp) == 0 &&
2149 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2150 == (B_DELWRI | B_NEEDCOMMIT))
2151 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
2157 #ifndef NFS_NOSERVER
2159 * Map errnos to NFS error numbers. For Version 3 also filter out error
2160 * numbers not specified for the associated procedure.
2163 nfsrv_errmap(nd, err)
2164 struct nfsrv_descript *nd;
2167 register short *defaulterrp, *errp;
2169 if (nd->nd_flag & ND_NFSV3) {
2170 if (nd->nd_procnum <= NFSPROC_COMMIT) {
2171 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
2175 else if (*errp > err)
2178 return ((int)*defaulterrp);
2180 return (err & 0xffff);
2183 return ((int)nfsrv_v2errmap[err - 1]);
2188 nfsrv_object_create(vp)
2192 if (vp == NULL || vp->v_type != VREG)
2194 return (vfs_object_create(vp, curproc,
2195 curproc ? curproc->p_ucred : NULL));
2199 * Sort the group list in increasing numerical order.
2200 * (Insertion sort by Chris Torek, who was grossed out by the bubble sort
2201 * that used to be here.)
2204 nfsrvw_sort(list, num)
2205 register gid_t *list;
2211 /* Insertion sort. */
2212 for (i = 1; i < num; i++) {
2214 /* find correct slot for value v, moving others up */
2215 for (j = i; --j >= 0 && v < list[j];)
2216 list[j + 1] = list[j];
2222 * copy credentials making sure that the result can be compared with bcmp().
2225 nfsrv_setcred(incred, outcred)
2226 register struct ucred *incred, *outcred;
2230 bzero((caddr_t)outcred, sizeof (struct ucred));
2231 outcred->cr_ref = 1;
2232 outcred->cr_uid = incred->cr_uid;
2233 outcred->cr_ngroups = incred->cr_ngroups;
2234 for (i = 0; i < incred->cr_ngroups; i++)
2235 outcred->cr_groups[i] = incred->cr_groups[i];
2236 nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups);
2238 #endif /* NFS_NOSERVER */