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 $
38 * $DragonFly: src/sys/vfs/nfs/nfs_subs.c,v 1.8 2003/08/20 09:56:33 rob 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>
74 #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(vp, procid, hsiz, bposp)
591 struct nfsmount *nmp;
594 MGET(mb, M_WAIT, MT_DATA);
595 if (hsiz >= MINCLSIZE)
598 bpos = mtod(mb, caddr_t);
601 * For NQNFS, add lease request.
604 nmp = VFSTONFS(vp->v_mount);
605 if (nmp->nm_flag & NFSMNT_NQNFS) {
606 nqflag = NQNFS_NEEDLEASE(vp, procid);
608 nfsm_build(tl, u_int32_t *, 2*NFSX_UNSIGNED);
609 *tl++ = txdr_unsigned(nqflag);
610 *tl = txdr_unsigned(nmp->nm_leaseterm);
612 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
617 /* Finally, return values */
623 * Build the RPC header and fill in the authorization info.
624 * The authorization string argument is only used when the credentials
625 * come from outside of the kernel.
626 * Returns the head of the mbuf list.
629 nfsm_rpchead(cr, nmflag, procid, auth_type, auth_len, auth_str, verf_len,
630 verf_str, mrest, mrest_len, mbp, xidp)
648 struct mbuf *mreq, *mb2;
649 int siz, grpsiz, authsiz;
651 authsiz = nfsm_rndup(auth_len);
652 MGETHDR(mb, M_WAIT, MT_DATA);
653 if ((authsiz + 10 * NFSX_UNSIGNED) >= MINCLSIZE) {
655 } else if ((authsiz + 10 * NFSX_UNSIGNED) < MHLEN) {
656 MH_ALIGN(mb, authsiz + 10 * NFSX_UNSIGNED);
658 MH_ALIGN(mb, 8 * NFSX_UNSIGNED);
662 bpos = mtod(mb, caddr_t);
665 * First the RPC header.
667 nfsm_build(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
669 /* Get a pretty random xid to start with */
673 * Skip zero xid if it should ever happen.
678 *tl++ = *xidp = txdr_unsigned(nfs_xid);
681 if (nmflag & NFSMNT_NQNFS) {
682 *tl++ = txdr_unsigned(NQNFS_PROG);
683 *tl++ = txdr_unsigned(NQNFS_VER3);
685 *tl++ = txdr_unsigned(NFS_PROG);
686 if (nmflag & NFSMNT_NFSV3)
687 *tl++ = txdr_unsigned(NFS_VER3);
689 *tl++ = txdr_unsigned(NFS_VER2);
691 if (nmflag & NFSMNT_NFSV3)
692 *tl++ = txdr_unsigned(procid);
694 *tl++ = txdr_unsigned(nfsv2_procid[procid]);
697 * And then the authorization cred.
699 *tl++ = txdr_unsigned(auth_type);
700 *tl = txdr_unsigned(authsiz);
703 nfsm_build(tl, u_int32_t *, auth_len);
704 *tl++ = 0; /* stamp ?? */
705 *tl++ = 0; /* NULL hostname */
706 *tl++ = txdr_unsigned(cr->cr_uid);
707 *tl++ = txdr_unsigned(cr->cr_groups[0]);
708 grpsiz = (auth_len >> 2) - 5;
709 *tl++ = txdr_unsigned(grpsiz);
710 for (i = 1; i <= grpsiz; i++)
711 *tl++ = txdr_unsigned(cr->cr_groups[i]);
716 if (M_TRAILINGSPACE(mb) == 0) {
717 MGET(mb2, M_WAIT, MT_DATA);
718 if (siz >= MINCLSIZE)
723 bpos = mtod(mb, caddr_t);
725 i = min(siz, M_TRAILINGSPACE(mb));
726 bcopy(auth_str, bpos, i);
732 if ((siz = (nfsm_rndup(auth_len) - auth_len)) > 0) {
733 for (i = 0; i < siz; i++)
741 * And the verifier...
743 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
745 *tl++ = txdr_unsigned(RPCAUTH_KERB4);
746 *tl = txdr_unsigned(verf_len);
749 if (M_TRAILINGSPACE(mb) == 0) {
750 MGET(mb2, M_WAIT, MT_DATA);
751 if (siz >= MINCLSIZE)
756 bpos = mtod(mb, caddr_t);
758 i = min(siz, M_TRAILINGSPACE(mb));
759 bcopy(verf_str, bpos, i);
765 if ((siz = (nfsm_rndup(verf_len) - verf_len)) > 0) {
766 for (i = 0; i < siz; i++)
771 *tl++ = txdr_unsigned(RPCAUTH_NULL);
775 mreq->m_pkthdr.len = authsiz + 10 * NFSX_UNSIGNED + mrest_len;
776 mreq->m_pkthdr.rcvif = (struct ifnet *)0;
782 * copies mbuf chain to the uio scatter/gather list
785 nfsm_mbuftouio(mrep, uiop, siz, dpos)
791 char *mbufcp, *uiocp;
799 len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
800 rem = nfsm_rndup(siz)-siz;
802 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
804 left = uiop->uio_iov->iov_len;
805 uiocp = uiop->uio_iov->iov_base;
814 mbufcp = mtod(mp, caddr_t);
817 xfer = (left > len) ? len : left;
820 if (uiop->uio_iov->iov_op != NULL)
821 (*(uiop->uio_iov->iov_op))
822 (mbufcp, uiocp, xfer);
825 if (uiop->uio_segflg == UIO_SYSSPACE)
826 bcopy(mbufcp, uiocp, xfer);
828 copyout(mbufcp, uiocp, xfer);
833 uiop->uio_offset += xfer;
834 uiop->uio_resid -= xfer;
836 if (uiop->uio_iov->iov_len <= siz) {
840 uiop->uio_iov->iov_base += uiosiz;
841 uiop->uio_iov->iov_len -= uiosiz;
849 error = nfs_adv(mrep, dpos, rem, len);
857 * copies a uio scatter/gather list to an mbuf chain.
858 * NOTE: can ony handle iovcnt == 1
861 nfsm_uiotombuf(uiop, mq, siz, bpos)
868 struct mbuf *mp, *mp2;
869 int xfer, left, mlen;
870 int uiosiz, clflg, rem;
874 if (uiop->uio_iovcnt != 1)
875 panic("nfsm_uiotombuf: iovcnt != 1");
878 if (siz > MLEN) /* or should it >= MCLBYTES ?? */
882 rem = nfsm_rndup(siz)-siz;
885 left = uiop->uio_iov->iov_len;
886 uiocp = uiop->uio_iov->iov_base;
891 mlen = M_TRAILINGSPACE(mp);
893 MGET(mp, M_WAIT, MT_DATA);
899 mlen = M_TRAILINGSPACE(mp);
901 xfer = (left > mlen) ? mlen : left;
904 if (uiop->uio_iov->iov_op != NULL)
905 (*(uiop->uio_iov->iov_op))
906 (uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
909 if (uiop->uio_segflg == UIO_SYSSPACE)
910 bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
912 copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
916 uiop->uio_offset += xfer;
917 uiop->uio_resid -= xfer;
919 uiop->uio_iov->iov_base += uiosiz;
920 uiop->uio_iov->iov_len -= uiosiz;
924 if (rem > M_TRAILINGSPACE(mp)) {
925 MGET(mp, M_WAIT, MT_DATA);
929 cp = mtod(mp, caddr_t)+mp->m_len;
930 for (left = 0; left < rem; left++)
935 *bpos = mtod(mp, caddr_t)+mp->m_len;
941 * Help break down an mbuf chain by setting the first siz bytes contiguous
942 * pointed to by returned val.
943 * This is used by the macros nfsm_dissect and nfsm_dissecton for tough
944 * cases. (The macros use the vars. dpos and dpos2)
947 nfsm_disct(mdp, dposp, siz, left, cp2)
954 struct mbuf *mp, *mp2;
960 *mdp = mp = mp->m_next;
964 *dposp = mtod(mp, caddr_t);
969 } else if (mp->m_next == NULL) {
971 } else if (siz > MHLEN) {
972 panic("nfs S too big");
974 MGET(mp2, M_WAIT, MT_DATA);
975 mp2->m_next = mp->m_next;
979 *cp2 = p = mtod(mp, caddr_t);
980 bcopy(*dposp, p, left); /* Copy what was left */
984 /* Loop around copying up the siz2 bytes */
988 xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2;
990 bcopy(mtod(mp2, caddr_t), p, xfer);
1001 *dposp = mtod(mp2, caddr_t);
1007 * Advance the position in the mbuf chain.
1010 nfs_adv(mdp, dposp, offs, left)
1029 *dposp = mtod(m, caddr_t)+offs;
1034 * Copy a string into mbufs for the hard cases...
1037 nfsm_strtmbuf(mb, bpos, cp, siz)
1043 struct mbuf *m1 = NULL, *m2;
1044 long left, xfer, len, tlen;
1050 left = M_TRAILINGSPACE(m2);
1052 tl = ((u_int32_t *)(*bpos));
1053 *tl++ = txdr_unsigned(siz);
1055 left -= NFSX_UNSIGNED;
1056 m2->m_len += NFSX_UNSIGNED;
1058 bcopy(cp, (caddr_t) tl, left);
1065 /* Loop around adding mbufs */
1067 MGET(m1, M_WAIT, MT_DATA);
1070 m1->m_len = NFSMSIZ(m1);
1073 tl = mtod(m1, u_int32_t *);
1076 *tl++ = txdr_unsigned(siz);
1077 m1->m_len -= NFSX_UNSIGNED;
1078 tlen = NFSX_UNSIGNED;
1081 if (siz < m1->m_len) {
1082 len = nfsm_rndup(siz);
1085 *(tl+(xfer>>2)) = 0;
1087 xfer = len = m1->m_len;
1089 bcopy(cp, (caddr_t) tl, xfer);
1090 m1->m_len = len+tlen;
1095 *bpos = mtod(m1, caddr_t)+m1->m_len;
1100 * Called once to initialize data structures...
1104 struct vfsconf *vfsp;
1108 nfsmount_zone = zinit("NFSMOUNT", sizeof(struct nfsmount), 0, 0, 1);
1110 nfs_mount_type = vfsp->vfc_typenum;
1112 rpc_vers = txdr_unsigned(RPC_VER2);
1113 rpc_call = txdr_unsigned(RPC_CALL);
1114 rpc_reply = txdr_unsigned(RPC_REPLY);
1115 rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
1116 rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
1117 rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
1118 rpc_autherr = txdr_unsigned(RPC_AUTHERR);
1119 rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
1120 rpc_auth_kerb = txdr_unsigned(RPCAUTH_KERB4);
1121 nfs_prog = txdr_unsigned(NFS_PROG);
1122 nqnfs_prog = txdr_unsigned(NQNFS_PROG);
1123 nfs_true = txdr_unsigned(TRUE);
1124 nfs_false = txdr_unsigned(FALSE);
1125 nfs_xdrneg1 = txdr_unsigned(-1);
1126 nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
1129 /* Ensure async daemons disabled */
1130 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
1131 nfs_iodwant[i] = NULL;
1132 nfs_iodmount[i] = (struct nfsmount *)0;
1134 nfs_nhinit(); /* Init the nfsnode table */
1135 #ifndef NFS_NOSERVER
1136 nfsrv_init(0); /* Init server data structures */
1137 nfsrv_initcache(); /* Init the server request cache */
1141 * Initialize the nqnfs server stuff.
1143 if (nqnfsstarttime == 0) {
1144 nqnfsstarttime = boottime.tv_sec + nqsrv_maxlease
1145 + nqsrv_clockskew + nqsrv_writeslack;
1146 NQLOADNOVRAM(nqnfsstarttime);
1147 CIRCLEQ_INIT(&nqtimerhead);
1148 nqfhhashtbl = hashinit(NQLCHSZ, M_NQLEASE, &nqfhhash);
1152 * Initialize reply list and start timer
1154 TAILQ_INIT(&nfs_reqq);
1159 * Set up lease_check and lease_updatetime so that other parts
1160 * of the system can call us, if we are loadable.
1162 #ifndef NFS_NOSERVER
1163 nfs_prev_vop_lease_check = default_vnodeop_p[VOFFSET(vop_lease)];
1164 default_vnodeop_p[VOFFSET(vop_lease)] = (vop_t *)nqnfs_vop_lease_check;
1166 nfs_prev_lease_updatetime = lease_updatetime;
1167 lease_updatetime = nfs_lease_updatetime;
1168 nfs_prev_nfssvc_sy_narg = sysent[SYS_nfssvc].sy_narg;
1169 sysent[SYS_nfssvc].sy_narg = 2;
1170 nfs_prev_nfssvc_sy_call = sysent[SYS_nfssvc].sy_call;
1171 sysent[SYS_nfssvc].sy_call = (sy_call_t *)nfssvc;
1173 nfs_pbuf_freecnt = nswbuf / 2 + 1;
1180 struct vfsconf *vfsp;
1183 untimeout(nfs_timer, (void *)NULL, nfs_timer_handle);
1184 nfs_mount_type = -1;
1185 #ifndef NFS_NOSERVER
1186 default_vnodeop_p[VOFFSET(vop_lease)] = nfs_prev_vop_lease_check;
1188 lease_updatetime = nfs_prev_lease_updatetime;
1189 sysent[SYS_nfssvc].sy_narg = nfs_prev_nfssvc_sy_narg;
1190 sysent[SYS_nfssvc].sy_call = nfs_prev_nfssvc_sy_call;
1195 * Attribute cache routines.
1196 * nfs_loadattrcache() - loads or updates the cache contents from attributes
1197 * that are on the mbuf list
1198 * nfs_getattrcache() - returns valid attributes if found in cache, returns
1203 * Load the attribute cache (that lives in the nfsnode entry) with
1204 * the values on the mbuf list and
1206 * copy the attributes to *vaper
1209 nfs_loadattrcache(vpp, mdp, dposp, vaper, dontshrink)
1213 struct vattr *vaper;
1216 struct vnode *vp = *vpp;
1218 struct nfs_fattr *fp;
1222 int error = 0, rdev;
1226 struct timespec mtime;
1227 int v3 = NFS_ISV3(vp);
1230 t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
1231 if ((error = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, &cp2)) != 0)
1233 fp = (struct nfs_fattr *)cp2;
1235 vtyp = nfsv3tov_type(fp->fa_type);
1236 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1237 rdev = makeudev(fxdr_unsigned(int, fp->fa3_rdev.specdata1),
1238 fxdr_unsigned(int, fp->fa3_rdev.specdata2));
1239 fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
1241 vtyp = nfsv2tov_type(fp->fa_type);
1242 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1246 * The duplicate information returned in fa_type and fa_mode
1247 * is an ambiguity in the NFS version 2 protocol.
1249 * VREG should be taken literally as a regular file. If a
1250 * server intents to return some type information differently
1251 * in the upper bits of the mode field (e.g. for sockets, or
1252 * FIFOs), NFSv2 mandates fa_type to be VNON. Anyway, we
1253 * leave the examination of the mode bits even in the VREG
1254 * case to avoid breakage for bogus servers, but we make sure
1255 * that there are actually type bits set in the upper part of
1256 * fa_mode (and failing that, trust the va_type field).
1258 * NFSv3 cleared the issue, and requires fa_mode to not
1259 * contain any type information (while also introduing sockets
1260 * and FIFOs for fa_type).
1262 if (vtyp == VNON || (vtyp == VREG && (vmode & S_IFMT) != 0))
1263 vtyp = IFTOVT(vmode);
1264 rdev = fxdr_unsigned(int32_t, fp->fa2_rdev);
1265 fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
1268 * Really ugly NFSv2 kludge.
1270 if (vtyp == VCHR && rdev == 0xffffffff)
1275 * If v_type == VNON it is a new node, so fill in the v_type,
1276 * n_mtime fields. Check to see if it represents a special
1277 * device, and if so, check for a possible alias. Once the
1278 * correct vnode has been obtained, fill in the rest of the
1282 if (vp->v_type != vtyp) {
1284 if (vp->v_type == VFIFO) {
1285 vp->v_op = fifo_nfsv2nodeop_p;
1287 if (vp->v_type == VCHR || vp->v_type == VBLK) {
1288 vp->v_op = spec_nfsv2nodeop_p;
1289 addaliasu(vp, rdev);
1291 np->n_mtime = mtime.tv_sec;
1294 vap->va_type = vtyp;
1295 vap->va_mode = (vmode & 07777);
1296 vap->va_rdev = rdev;
1297 vap->va_mtime = mtime;
1298 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
1300 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1301 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1302 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1303 vap->va_size = fxdr_hyper(&fp->fa3_size);
1304 vap->va_blocksize = NFS_FABLKSIZE;
1305 vap->va_bytes = fxdr_hyper(&fp->fa3_used);
1306 vap->va_fileid = fxdr_unsigned(int32_t,
1307 fp->fa3_fileid.nfsuquad[1]);
1308 fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
1309 fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
1311 vap->va_filerev = 0;
1313 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1314 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1315 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1316 vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
1317 vap->va_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize);
1318 vap->va_bytes = (u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks)
1320 vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
1321 fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
1323 vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t,
1324 fp->fa2_ctime.nfsv2_sec);
1325 vap->va_ctime.tv_nsec = 0;
1326 vap->va_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec);
1327 vap->va_filerev = 0;
1329 np->n_attrstamp = time_second;
1330 if (vap->va_size != np->n_size) {
1331 if (vap->va_type == VREG) {
1332 if (dontshrink && vap->va_size < np->n_size) {
1334 * We've been told not to shrink the file;
1335 * zero np->n_attrstamp to indicate that
1336 * the attributes are stale.
1338 vap->va_size = np->n_size;
1339 np->n_attrstamp = 0;
1340 } else if (np->n_flag & NMODIFIED) {
1341 if (vap->va_size < np->n_size)
1342 vap->va_size = np->n_size;
1344 np->n_size = vap->va_size;
1346 np->n_size = vap->va_size;
1348 vnode_pager_setsize(vp, np->n_size);
1350 np->n_size = vap->va_size;
1353 if (vaper != NULL) {
1354 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
1355 if (np->n_flag & NCHG) {
1356 if (np->n_flag & NACC)
1357 vaper->va_atime = np->n_atim;
1358 if (np->n_flag & NUPD)
1359 vaper->va_mtime = np->n_mtim;
1366 #include <sys/sysctl.h>
1367 SYSCTL_DECL(_vfs_nfs);
1368 static int nfs_acdebug;
1369 SYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, "");
1373 * Check the time stamp
1374 * If the cache is valid, copy contents to *vap and return 0
1375 * otherwise return an error
1378 nfs_getattrcache(vp, vaper)
1380 struct vattr *vaper;
1384 struct nfsmount *nmp;
1389 nmp = VFSTONFS(vp->v_mount);
1390 /* XXX n_mtime doesn't seem to be updated on a miss-and-reload */
1391 timeo = (time_second - np->n_mtime) / 10;
1395 printf("nfs_getattrcache: initial timeo = %d\n", timeo);
1398 if (vap->va_type == VDIR) {
1399 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin)
1400 timeo = nmp->nm_acdirmin;
1401 else if (timeo > nmp->nm_acdirmax)
1402 timeo = nmp->nm_acdirmax;
1404 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin)
1405 timeo = nmp->nm_acregmin;
1406 else if (timeo > nmp->nm_acregmax)
1407 timeo = nmp->nm_acregmax;
1411 if (nfs_acdebug > 2)
1412 printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n",
1413 nmp->nm_acregmin, nmp->nm_acregmax,
1414 nmp->nm_acdirmin, nmp->nm_acdirmax);
1417 printf("nfs_getattrcache: age = %d; final timeo = %d\n",
1418 (time_second - np->n_attrstamp), timeo);
1421 if ((time_second - np->n_attrstamp) >= timeo) {
1422 nfsstats.attrcache_misses++;
1425 nfsstats.attrcache_hits++;
1426 if (vap->va_size != np->n_size) {
1427 if (vap->va_type == VREG) {
1428 if (np->n_flag & NMODIFIED) {
1429 if (vap->va_size < np->n_size)
1430 vap->va_size = np->n_size;
1432 np->n_size = vap->va_size;
1434 np->n_size = vap->va_size;
1436 vnode_pager_setsize(vp, np->n_size);
1438 np->n_size = vap->va_size;
1441 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
1442 if (np->n_flag & NCHG) {
1443 if (np->n_flag & NACC)
1444 vaper->va_atime = np->n_atim;
1445 if (np->n_flag & NUPD)
1446 vaper->va_mtime = np->n_mtim;
1451 #ifndef NFS_NOSERVER
1453 * Set up nameidata for a lookup() call and do it.
1455 * If pubflag is set, this call is done for a lookup operation on the
1456 * public filehandle. In that case we allow crossing mountpoints and
1457 * absolute pathnames. However, the caller is expected to check that
1458 * the lookup result is within the public fs, and deny access if
1461 * nfs_namei() clears out garbage fields that namei() might leave garbage.
1462 * This is mainly ni_vp and ni_dvp when an error occurs, and ni_dvp when no
1463 * error occurs but the parent was not requested.
1465 * dirp may be set whether an error is returned or not, and must be
1466 * released by the caller.
1469 nfs_namei(ndp, fhp, len, slp, nam, mdp, dposp, retdirp, td, kerbflag, pubflag)
1470 struct nameidata *ndp;
1473 struct nfssvc_sock *slp;
1474 struct sockaddr *nam;
1477 struct vnode **retdirp;
1479 int kerbflag, pubflag;
1483 char *fromcp, *tocp, *cp;
1487 int error, rdonly, linklen;
1488 struct componentname *cnp = &ndp->ni_cnd;
1490 *retdirp = (struct vnode *)0;
1491 cnp->cn_pnbuf = zalloc(namei_zone);
1494 * Copy the name from the mbuf list to ndp->ni_pnbuf
1495 * and set the various ndp fields appropriately.
1498 tocp = cnp->cn_pnbuf;
1500 rem = mtod(md, caddr_t) + md->m_len - fromcp;
1501 for (i = 0; i < len; i++) {
1508 fromcp = mtod(md, caddr_t);
1511 if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) {
1515 *tocp++ = *fromcp++;
1521 len = nfsm_rndup(len)-len;
1525 else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
1530 * Extract and set starting directory.
1532 error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp,
1533 nam, &rdonly, kerbflag, pubflag);
1536 if (dp->v_type != VDIR) {
1543 cnp->cn_flags |= RDONLY;
1546 * Set return directory. Reference to dp is implicitly transfered
1547 * to the returned pointer
1553 * Oh joy. For WebNFS, handle those pesky '%' escapes,
1554 * and the 'native path' indicator.
1556 cp = zalloc(namei_zone);
1557 fromcp = cnp->cn_pnbuf;
1559 if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) {
1560 switch ((unsigned char)*fromcp) {
1561 case WEBNFS_NATIVE_CHAR:
1563 * 'Native' path for us is the same
1564 * as a path according to the NFS spec,
1565 * just skip the escape char.
1570 * More may be added in the future, range 0x80-0xff
1574 zfree(namei_zone, cp);
1579 * Translate the '%' escapes, URL-style.
1581 while (*fromcp != '\0') {
1582 if (*fromcp == WEBNFS_ESC_CHAR) {
1583 if (fromcp[1] != '\0' && fromcp[2] != '\0') {
1585 *tocp++ = HEXSTRTOI(fromcp);
1590 zfree(namei_zone, cp);
1594 *tocp++ = *fromcp++;
1597 zfree(namei_zone, cnp->cn_pnbuf);
1601 ndp->ni_pathlen = (tocp - cnp->cn_pnbuf) + 1;
1602 ndp->ni_segflg = UIO_SYSSPACE;
1605 ndp->ni_rootdir = rootvnode;
1606 ndp->ni_loopcnt = 0;
1607 if (cnp->cn_pnbuf[0] == '/')
1610 cnp->cn_flags |= NOCROSSMOUNT;
1614 * Initialize for scan, set ni_startdir and bump ref on dp again
1615 * becuase lookup() will dereference ni_startdir.
1620 ndp->ni_startdir = dp;
1623 cnp->cn_nameptr = cnp->cn_pnbuf;
1625 * Call lookup() to do the real work. If an error occurs,
1626 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
1627 * we do not have to dereference anything before returning.
1628 * In either case ni_startdir will be dereferenced and NULLed
1631 error = lookup(ndp);
1636 * Check for encountering a symbolic link. Trivial
1637 * termination occurs if no symlink encountered.
1638 * Note: zfree is safe because error is 0, so we will
1639 * not zfree it again when we break.
1641 if ((cnp->cn_flags & ISSYMLINK) == 0) {
1642 nfsrv_object_create(ndp->ni_vp);
1643 if (cnp->cn_flags & (SAVENAME | SAVESTART))
1644 cnp->cn_flags |= HASBUF;
1646 zfree(namei_zone, cnp->cn_pnbuf);
1653 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
1654 VOP_UNLOCK(ndp->ni_dvp, 0, td);
1660 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
1664 if (ndp->ni_pathlen > 1)
1665 cp = zalloc(namei_zone);
1669 aiov.iov_len = MAXPATHLEN;
1670 auio.uio_iov = &aiov;
1671 auio.uio_iovcnt = 1;
1672 auio.uio_offset = 0;
1673 auio.uio_rw = UIO_READ;
1674 auio.uio_segflg = UIO_SYSSPACE;
1676 auio.uio_resid = MAXPATHLEN;
1677 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
1680 if (ndp->ni_pathlen > 1)
1681 zfree(namei_zone, cp);
1687 linklen = MAXPATHLEN - auio.uio_resid;
1692 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
1693 error = ENAMETOOLONG;
1698 * Adjust or replace path
1700 if (ndp->ni_pathlen > 1) {
1701 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
1702 zfree(namei_zone, cnp->cn_pnbuf);
1705 cnp->cn_pnbuf[linklen] = '\0';
1706 ndp->ni_pathlen += linklen;
1709 * Cleanup refs for next loop and check if root directory
1710 * should replace current directory. Normally ni_dvp
1711 * becomes the new base directory and is cleaned up when
1712 * we loop. Explicitly null pointers after invalidation
1713 * to clarify operation.
1718 if (cnp->cn_pnbuf[0] == '/') {
1720 ndp->ni_dvp = ndp->ni_rootdir;
1723 ndp->ni_startdir = ndp->ni_dvp;
1728 * nfs_namei() guarentees that fields will not contain garbage
1729 * whether an error occurs or not. This allows the caller to track
1730 * cleanup state trivially.
1734 zfree(namei_zone, cnp->cn_pnbuf);
1737 ndp->ni_startdir = NULL;
1738 cnp->cn_flags &= ~HASBUF;
1739 } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) {
1746 * A fiddled version of m_adj() that ensures null fill to a long
1747 * boundary and only trims off the back end
1750 nfsm_adj(mp, len, nul)
1760 * Trim from tail. Scan the mbuf chain,
1761 * calculating its length and finding the last mbuf.
1762 * If the adjustment only affects this mbuf, then just
1763 * adjust and return. Otherwise, rescan and truncate
1764 * after the remaining size.
1770 if (m->m_next == (struct mbuf *)0)
1774 if (m->m_len > len) {
1777 cp = mtod(m, caddr_t)+m->m_len-nul;
1778 for (i = 0; i < nul; i++)
1787 * Correct length for chain is "count".
1788 * Find the mbuf with last data, adjust its length,
1789 * and toss data from remaining mbufs on chain.
1791 for (m = mp; m; m = m->m_next) {
1792 if (m->m_len >= count) {
1795 cp = mtod(m, caddr_t)+m->m_len-nul;
1796 for (i = 0; i < nul; i++)
1803 for (m = m->m_next;m;m = m->m_next)
1808 * Make these functions instead of macros, so that the kernel text size
1809 * doesn't get too big...
1812 nfsm_srvwcc(nfsd, before_ret, before_vap, after_ret, after_vap, mbp, bposp)
1813 struct nfsrv_descript *nfsd;
1815 struct vattr *before_vap;
1817 struct vattr *after_vap;
1821 struct mbuf *mb = *mbp, *mb2;
1822 char *bpos = *bposp;
1826 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1829 nfsm_build(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
1831 txdr_hyper(before_vap->va_size, tl);
1833 txdr_nfsv3time(&(before_vap->va_mtime), tl);
1835 txdr_nfsv3time(&(before_vap->va_ctime), tl);
1839 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp);
1843 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp)
1844 struct nfsrv_descript *nfsd;
1846 struct vattr *after_vap;
1850 struct mbuf *mb = *mbp, *mb2;
1851 char *bpos = *bposp;
1853 struct nfs_fattr *fp;
1856 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1859 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR);
1861 fp = (struct nfs_fattr *)tl;
1862 nfsm_srvfattr(nfsd, after_vap, fp);
1869 nfsm_srvfattr(nfsd, vap, fp)
1870 struct nfsrv_descript *nfsd;
1872 struct nfs_fattr *fp;
1875 fp->fa_nlink = txdr_unsigned(vap->va_nlink);
1876 fp->fa_uid = txdr_unsigned(vap->va_uid);
1877 fp->fa_gid = txdr_unsigned(vap->va_gid);
1878 if (nfsd->nd_flag & ND_NFSV3) {
1879 fp->fa_type = vtonfsv3_type(vap->va_type);
1880 fp->fa_mode = vtonfsv3_mode(vap->va_mode);
1881 txdr_hyper(vap->va_size, &fp->fa3_size);
1882 txdr_hyper(vap->va_bytes, &fp->fa3_used);
1883 fp->fa3_rdev.specdata1 = txdr_unsigned(umajor(vap->va_rdev));
1884 fp->fa3_rdev.specdata2 = txdr_unsigned(uminor(vap->va_rdev));
1885 fp->fa3_fsid.nfsuquad[0] = 0;
1886 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid);
1887 fp->fa3_fileid.nfsuquad[0] = 0;
1888 fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(vap->va_fileid);
1889 txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime);
1890 txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime);
1891 txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime);
1893 fp->fa_type = vtonfsv2_type(vap->va_type);
1894 fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1895 fp->fa2_size = txdr_unsigned(vap->va_size);
1896 fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize);
1897 if (vap->va_type == VFIFO)
1898 fp->fa2_rdev = 0xffffffff;
1900 fp->fa2_rdev = txdr_unsigned(vap->va_rdev);
1901 fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE);
1902 fp->fa2_fsid = txdr_unsigned(vap->va_fsid);
1903 fp->fa2_fileid = txdr_unsigned(vap->va_fileid);
1904 txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime);
1905 txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime);
1906 txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime);
1911 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
1912 * - look up fsid in mount list (if not found ret error)
1913 * - get vp and export rights by calling VFS_FHTOVP()
1914 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
1915 * - if not lockflag unlock it with VOP_UNLOCK()
1918 nfsrv_fhtovp(fhp, lockflag, vpp, cred, slp, nam, rdonlyp, kerbflag, pubflag)
1923 struct nfssvc_sock *slp;
1924 struct sockaddr *nam;
1929 struct thread *td = curthread; /* XXX */
1932 struct ucred *credanon;
1934 #ifdef MNT_EXNORESPORT /* XXX needs mountd and /etc/exports help yet */
1935 struct sockaddr_int *saddr;
1938 *vpp = (struct vnode *)0;
1940 if (nfs_ispublicfh(fhp)) {
1941 if (!pubflag || !nfs_pub.np_valid)
1943 fhp = &nfs_pub.np_handle;
1946 mp = vfs_getvfs(&fhp->fh_fsid);
1949 error = VFS_CHECKEXP(mp, nam, &exflags, &credanon);
1952 error = VFS_FHTOVP(mp, &fhp->fh_fid, vpp);
1955 #ifdef MNT_EXNORESPORT
1956 if (!(exflags & (MNT_EXNORESPORT|MNT_EXPUBLIC))) {
1957 saddr = (struct sockaddr_in *)nam;
1958 if (saddr->sin_family == AF_INET &&
1959 ntohs(saddr->sin_port) >= IPPORT_RESERVED) {
1962 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1967 * Check/setup credentials.
1969 if (exflags & MNT_EXKERB) {
1973 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1975 } else if (kerbflag) {
1978 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1979 } else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1980 cred->cr_uid = credanon->cr_uid;
1981 for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++)
1982 cred->cr_groups[i] = credanon->cr_groups[i];
1983 cred->cr_ngroups = i;
1985 if (exflags & MNT_EXRDONLY)
1990 nfsrv_object_create(*vpp);
1993 VOP_UNLOCK(*vpp, 0, td);
1999 * WebNFS: check if a filehandle is a public filehandle. For v3, this
2000 * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has
2001 * transformed this to all zeroes in both cases, so check for it.
2007 char *cp = (char *)fhp;
2010 for (i = 0; i < NFSX_V3FH; i++)
2016 #endif /* NFS_NOSERVER */
2018 * This function compares two net addresses by family and returns TRUE
2019 * if they are the same host.
2020 * If there is any doubt, return FALSE.
2021 * The AF_INET family is handled as a special case so that address mbufs
2022 * don't need to be saved to store "struct in_addr", which is only 4 bytes.
2025 netaddr_match(family, haddr, nam)
2027 union nethostaddr *haddr;
2028 struct sockaddr *nam;
2030 struct sockaddr_in *inetaddr;
2034 inetaddr = (struct sockaddr_in *)nam;
2035 if (inetaddr->sin_family == AF_INET &&
2036 inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
2045 static nfsuint64 nfs_nullcookie = { { 0, 0 } };
2047 * This function finds the directory cookie that corresponds to the
2048 * logical byte offset given.
2051 nfs_getcookie(np, off, add)
2056 struct nfsdmap *dp, *dp2;
2059 pos = (uoff_t)off / NFS_DIRBLKSIZ;
2060 if (pos == 0 || off < 0) {
2063 panic("nfs getcookie add at <= 0");
2065 return (&nfs_nullcookie);
2068 dp = np->n_cookies.lh_first;
2071 MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap),
2072 M_NFSDIROFF, M_WAITOK);
2073 dp->ndm_eocookie = 0;
2074 LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
2076 return ((nfsuint64 *)0);
2078 while (pos >= NFSNUMCOOKIES) {
2079 pos -= NFSNUMCOOKIES;
2080 if (dp->ndm_list.le_next) {
2081 if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
2082 pos >= dp->ndm_eocookie)
2083 return ((nfsuint64 *)0);
2084 dp = dp->ndm_list.le_next;
2086 MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap),
2087 M_NFSDIROFF, M_WAITOK);
2088 dp2->ndm_eocookie = 0;
2089 LIST_INSERT_AFTER(dp, dp2, ndm_list);
2092 return ((nfsuint64 *)0);
2094 if (pos >= dp->ndm_eocookie) {
2096 dp->ndm_eocookie = pos + 1;
2098 return ((nfsuint64 *)0);
2100 return (&dp->ndm_cookies[pos]);
2104 * Invalidate cached directory information, except for the actual directory
2105 * blocks (which are invalidated separately).
2106 * Done mainly to avoid the use of stale offset cookies.
2112 struct nfsnode *np = VTONFS(vp);
2115 if (vp->v_type != VDIR)
2116 panic("nfs: invaldir not dir");
2118 np->n_direofoffset = 0;
2119 np->n_cookieverf.nfsuquad[0] = 0;
2120 np->n_cookieverf.nfsuquad[1] = 0;
2121 if (np->n_cookies.lh_first)
2122 np->n_cookies.lh_first->ndm_eocookie = 0;
2126 * The write verifier has changed (probably due to a server reboot), so all
2127 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
2128 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
2129 * and B_CLUSTEROK flags. Once done the new write verifier can be set for the
2132 * B_CLUSTEROK must be cleared along with B_NEEDCOMMIT because stage 1 data
2133 * writes are not clusterable.
2139 struct vnode *vp, *nvp;
2140 struct buf *bp, *nbp;
2145 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp; vp = nvp) {
2146 if (vp->v_mount != mp) /* Paranoia */
2148 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
2149 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2150 nbp = TAILQ_NEXT(bp, b_vnbufs);
2151 if (BUF_REFCNT(bp) == 0 &&
2152 (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
2153 == (B_DELWRI | B_NEEDCOMMIT))
2154 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
2160 #ifndef NFS_NOSERVER
2162 * Map errnos to NFS error numbers. For Version 3 also filter out error
2163 * numbers not specified for the associated procedure.
2166 nfsrv_errmap(nd, err)
2167 struct nfsrv_descript *nd;
2170 short *defaulterrp, *errp;
2172 if (nd->nd_flag & ND_NFSV3) {
2173 if (nd->nd_procnum <= NFSPROC_COMMIT) {
2174 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
2178 else if (*errp > err)
2181 return ((int)*defaulterrp);
2183 return (err & 0xffff);
2186 return ((int)nfsrv_v2errmap[err - 1]);
2191 nfsrv_object_create(struct vnode *vp)
2193 struct thread *td = curthread;
2195 if (vp == NULL || vp->v_type != VREG)
2197 return (vfs_object_create(vp, td));
2201 * Sort the group list in increasing numerical order.
2202 * (Insertion sort by Chris Torek, who was grossed out by the bubble sort
2203 * that used to be here.)
2206 nfsrvw_sort(list, num)
2213 /* Insertion sort. */
2214 for (i = 1; i < num; i++) {
2216 /* find correct slot for value v, moving others up */
2217 for (j = i; --j >= 0 && v < list[j];)
2218 list[j + 1] = list[j];
2224 * copy credentials making sure that the result can be compared with bcmp().
2227 nfsrv_setcred(incred, outcred)
2228 struct ucred *incred, *outcred;
2232 bzero((caddr_t)outcred, sizeof (struct ucred));
2233 outcred->cr_ref = 1;
2234 outcred->cr_uid = incred->cr_uid;
2235 outcred->cr_ngroups = incred->cr_ngroups;
2236 for (i = 0; i < incred->cr_ngroups; i++)
2237 outcred->cr_groups[i] = incred->cr_groups[i];
2238 nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups);
2240 #endif /* NFS_NOSERVER */