2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
27 * Copyright (c) 1989, 1993
28 * The Regents of the University of California. All rights reserved.
30 * This code is derived from software contributed to Berkeley by
31 * Rick Macklem at The University of Guelph.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)nfs_subs.c 8.8 (Berkeley) 5/22/95
62 * FreeBSD-Id: nfs_subs.c,v 1.47 1997/11/07 08:53:24 phk Exp $
66 * These functions support the macros and help fiddle mbuf chains for
67 * the nfs op functions. They do things like create the rpc header and
68 * copy data between mbuf chains and uio lists.
70 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/kernel.h>
74 #include <sys/mount.h>
75 #include <sys/vnode.h>
76 #include <sys/namei.h>
78 #include <sys/socket.h>
80 #include <sys/malloc.h>
81 #include <sys/syscall.h>
85 #include <sys/vmparam.h>
86 #include <machine/spl.h>
91 #include <kern/clock.h>
93 #include <nfs/rpcv2.h>
94 #include <nfs/nfsproto.h>
96 #include <nfs/nfsnode.h>
97 #include <nfs/xdr_subs.h>
98 #include <nfs/nfsm_subs.h>
99 #include <nfs/nfsmount.h>
100 #include <nfs/nqnfs.h>
101 #include <nfs/nfsrtt.h>
103 #include <miscfs/specfs/specdev.h>
105 #include <netinet/in.h>
107 #include <netiso/iso.h>
110 #include <sys/kdebug.h>
112 #define FSDBG(A, B, C, D, E) \
113 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_NONE, \
114 (int)(B), (int)(C), (int)(D), (int)(E), 0)
115 #define FSDBG_TOP(A, B, C, D, E) \
116 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_START, \
117 (int)(B), (int)(C), (int)(D), (int)(E), 0)
118 #define FSDBG_BOT(A, B, C, D, E) \
119 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_END, \
120 (int)(B), (int)(C), (int)(D), (int)(E), 0)
122 * Data items converted to xdr at startup, since they are constant
123 * This is kinda hokey, but may save a little time doing byte swaps
126 u_long rpc_call
, rpc_vers
, rpc_reply
, rpc_msgdenied
, rpc_autherr
,
127 rpc_mismatch
, rpc_auth_unix
, rpc_msgaccepted
,
129 u_long nfs_prog
, nqnfs_prog
, nfs_true
, nfs_false
;
131 /* And other global data */
132 static u_long nfs_xid
= 0;
133 u_long nfs_xidwrap
= 0; /* to build a (non-wwrapping) 64 bit xid */
134 static enum vtype nv2tov_type
[8]= {
135 VNON
, VREG
, VDIR
, VBLK
, VCHR
, VLNK
, VNON
, VNON
137 enum vtype nv3tov_type
[8]= {
138 VNON
, VREG
, VDIR
, VBLK
, VCHR
, VLNK
, VSOCK
, VFIFO
144 struct nfs_reqq nfs_reqq
;
145 struct nfssvc_sockhead nfssvc_sockhead
;
146 int nfssvc_sockhead_flag
;
147 struct nfsd_head nfsd_head
;
149 struct nfs_bufq nfs_bufq
;
150 struct nqtimerhead nqtimerhead
;
151 struct nqfhhashhead
*nqfhhashtbl
;
156 * Mapping of old NFS Version 2 RPC numbers to generic numbers.
158 int nfsv3_procid
[NFS_NPROCS
] = {
187 #endif /* NFS_NOSERVER */
189 * and the reverse mapping from generic to Version 2 procedure numbers
191 int nfsv2_procid
[NFS_NPROCS
] = {
222 * Maps errno values to nfs error numbers.
223 * Use NFSERR_IO as the catch all for ones not specifically defined in
226 static u_char nfsrv_v2errmap
[ELAST
] = {
227 NFSERR_PERM
, NFSERR_NOENT
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
,
228 NFSERR_NXIO
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
,
229 NFSERR_IO
, NFSERR_IO
, NFSERR_ACCES
, NFSERR_IO
, NFSERR_IO
,
230 NFSERR_IO
, NFSERR_EXIST
, NFSERR_IO
, NFSERR_NODEV
, NFSERR_NOTDIR
,
231 NFSERR_ISDIR
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
,
232 NFSERR_IO
, NFSERR_FBIG
, NFSERR_NOSPC
, NFSERR_IO
, NFSERR_ROFS
,
233 NFSERR_IO
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
,
234 NFSERR_IO
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
,
235 NFSERR_IO
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
,
236 NFSERR_IO
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
,
237 NFSERR_IO
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
,
238 NFSERR_IO
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
,
239 NFSERR_IO
, NFSERR_IO
, NFSERR_NAMETOL
, NFSERR_IO
, NFSERR_IO
,
240 NFSERR_NOTEMPTY
, NFSERR_IO
, NFSERR_IO
, NFSERR_DQUOT
, NFSERR_STALE
,
241 NFSERR_IO
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
,
242 NFSERR_IO
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
,
243 NFSERR_IO
, NFSERR_IO
, NFSERR_IO
, NFSERR_IO
,
247 * Maps errno values to nfs error numbers.
248 * Although it is not obvious whether or not NFS clients really care if
249 * a returned error value is in the specified list for the procedure, the
250 * safest thing to do is filter them appropriately. For Version 2, the
251 * X/Open XNFS document is the only specification that defines error values
252 * for each RPC (The RFC simply lists all possible error values for all RPCs),
253 * so I have decided to not do this for Version 2.
254 * The first entry is the default error return and the rest are the valid
255 * errors for that RPC in increasing numeric order.
257 static short nfsv3err_null
[] = {
262 static short nfsv3err_getattr
[] = {
271 static short nfsv3err_setattr
[] = {
287 static short nfsv3err_lookup
[] = {
300 static short nfsv3err_access
[] = {
309 static short nfsv3err_readlink
[] = {
321 static short nfsv3err_read
[] = {
333 static short nfsv3err_write
[] = {
348 static short nfsv3err_create
[] = {
365 static short nfsv3err_mkdir
[] = {
382 static short nfsv3err_symlink
[] = {
399 static short nfsv3err_mknod
[] = {
417 static short nfsv3err_remove
[] = {
431 static short nfsv3err_rmdir
[] = {
449 static short nfsv3err_rename
[] = {
472 static short nfsv3err_link
[] = {
492 static short nfsv3err_readdir
[] = {
505 static short nfsv3err_readdirplus
[] = {
519 static short nfsv3err_fsstat
[] = {
528 static short nfsv3err_fsinfo
[] = {
536 static short nfsv3err_pathconf
[] = {
544 static short nfsv3err_commit
[] = {
553 static short *nfsrv_v3errmap
[] = {
571 nfsv3err_readdirplus
,
578 #endif /* NFS_NOSERVER */
580 extern struct nfsrtt nfsrtt
;
581 extern time_t nqnfsstarttime
;
582 extern int nqsrv_clockskew
;
583 extern int nqsrv_writeslack
;
584 extern int nqsrv_maxlease
;
585 extern struct nfsstats nfsstats
;
586 extern int nqnfs_piggy
[NFS_NPROCS
];
587 extern nfstype nfsv2_type
[9];
588 extern nfstype nfsv3_type
[9];
589 extern struct nfsnodehashhead
*nfsnodehashtbl
;
590 extern u_long nfsnodehash
;
593 extern int getfh(struct proc
*, struct getfh_args
*, int *);
595 extern int nfssvc(struct proc
*, struct nfssvc_args
*, int *);
597 LIST_HEAD(nfsnodehashhead
, nfsnode
);
599 int nfs_webnamei
__P((struct nameidata
*, struct vnode
*, struct proc
*));
602 * Create the header for an rpc request packet
603 * The hsiz is the size of the rest of the nfs request header.
604 * (just used to decide if a cluster is a good idea)
607 nfsm_reqh(vp
, procid
, hsiz
, bposp
)
613 register struct mbuf
*mb
;
615 register caddr_t bpos
;
617 struct nfsmount
*nmp
;
620 MGET(mb
, M_WAIT
, MT_DATA
);
621 if (hsiz
>= MINCLSIZE
)
624 bpos
= mtod(mb
, caddr_t
);
627 * For NQNFS, add lease request.
630 nmp
= VFSTONFS(vp
->v_mount
);
631 if (nmp
->nm_flag
& NFSMNT_NQNFS
) {
632 nqflag
= NQNFS_NEEDLEASE(vp
, procid
);
634 nfsm_build(tl
, u_long
*, 2*NFSX_UNSIGNED
);
635 *tl
++ = txdr_unsigned(nqflag
);
636 *tl
= txdr_unsigned(nmp
->nm_leaseterm
);
638 nfsm_build(tl
, u_long
*, NFSX_UNSIGNED
);
643 /* Finally, return values */
649 * Build the RPC header and fill in the authorization info.
650 * The authorization string argument is only used when the credentials
651 * come from outside of the kernel.
652 * Returns the head of the mbuf list.
655 nfsm_rpchead(cr
, nmflag
, procid
, auth_type
, auth_len
, auth_str
, verf_len
,
656 verf_str
, mrest
, mrest_len
, mbp
, xidp
)
657 register struct ucred
*cr
;
670 register struct mbuf
*mb
;
672 register caddr_t bpos
;
674 struct mbuf
*mreq
, *mb2
;
675 int siz
, grpsiz
, authsiz
;
679 authsiz
= nfsm_rndup(auth_len
);
680 MGETHDR(mb
, M_WAIT
, MT_DATA
);
681 if ((authsiz
+ 10 * NFSX_UNSIGNED
) >= MINCLSIZE
) {
683 } else if ((authsiz
+ 10 * NFSX_UNSIGNED
) < MHLEN
) {
684 MH_ALIGN(mb
, authsiz
+ 10 * NFSX_UNSIGNED
);
686 MH_ALIGN(mb
, 8 * NFSX_UNSIGNED
);
690 bpos
= mtod(mb
, caddr_t
);
693 * First the RPC header.
695 nfsm_build(tl
, u_long
*, 8 * NFSX_UNSIGNED
);
698 * derive initial xid from system time
699 * XXX time is invalid if root not yet mounted
701 if (!base
&& (rootvp
)) {
703 base
= tv
.tv_sec
<< 12;
707 * Skip zero xid if it should ever happen.
709 if (++nfs_xid
== 0) {
714 *tl
++ = *xidp
= txdr_unsigned(nfs_xid
);
717 if (nmflag
& NFSMNT_NQNFS
) {
718 *tl
++ = txdr_unsigned(NQNFS_PROG
);
719 *tl
++ = txdr_unsigned(NQNFS_VER3
);
721 *tl
++ = txdr_unsigned(NFS_PROG
);
722 if (nmflag
& NFSMNT_NFSV3
)
723 *tl
++ = txdr_unsigned(NFS_VER3
);
725 *tl
++ = txdr_unsigned(NFS_VER2
);
727 if (nmflag
& NFSMNT_NFSV3
)
728 *tl
++ = txdr_unsigned(procid
);
730 *tl
++ = txdr_unsigned(nfsv2_procid
[procid
]);
733 * And then the authorization cred.
735 *tl
++ = txdr_unsigned(auth_type
);
736 *tl
= txdr_unsigned(authsiz
);
739 nfsm_build(tl
, u_long
*, auth_len
);
740 *tl
++ = 0; /* stamp ?? */
741 *tl
++ = 0; /* NULL hostname */
742 *tl
++ = txdr_unsigned(cr
->cr_uid
);
743 *tl
++ = txdr_unsigned(cr
->cr_groups
[0]);
744 grpsiz
= (auth_len
>> 2) - 5;
745 *tl
++ = txdr_unsigned(grpsiz
);
746 for (i
= 1; i
<= grpsiz
; i
++)
747 *tl
++ = txdr_unsigned(cr
->cr_groups
[i
]);
752 if (M_TRAILINGSPACE(mb
) == 0) {
753 MGET(mb2
, M_WAIT
, MT_DATA
);
754 if (siz
>= MINCLSIZE
)
759 bpos
= mtod(mb
, caddr_t
);
761 i
= min(siz
, M_TRAILINGSPACE(mb
));
762 bcopy(auth_str
, bpos
, i
);
768 if ((siz
= (nfsm_rndup(auth_len
) - auth_len
)) > 0) {
769 for (i
= 0; i
< siz
; i
++)
777 * And the verifier...
779 nfsm_build(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
781 *tl
++ = txdr_unsigned(RPCAUTH_KERB4
);
782 *tl
= txdr_unsigned(verf_len
);
785 if (M_TRAILINGSPACE(mb
) == 0) {
786 MGET(mb2
, M_WAIT
, MT_DATA
);
787 if (siz
>= MINCLSIZE
)
792 bpos
= mtod(mb
, caddr_t
);
794 i
= min(siz
, M_TRAILINGSPACE(mb
));
795 bcopy(verf_str
, bpos
, i
);
801 if ((siz
= (nfsm_rndup(verf_len
) - verf_len
)) > 0) {
802 for (i
= 0; i
< siz
; i
++)
807 *tl
++ = txdr_unsigned(RPCAUTH_NULL
);
811 mreq
->m_pkthdr
.len
= authsiz
+ 10 * NFSX_UNSIGNED
+ mrest_len
;
812 mreq
->m_pkthdr
.rcvif
= (struct ifnet
*)0;
818 * copies mbuf chain to the uio scatter/gather list
821 nfsm_mbuftouio(mrep
, uiop
, siz
, dpos
)
823 register struct uio
*uiop
;
827 register char *mbufcp
, *uiocp
;
828 register int xfer
, left
, len
;
829 register struct mbuf
*mp
;
835 len
= mtod(mp
, caddr_t
)+mp
->m_len
-mbufcp
;
836 rem
= nfsm_rndup(siz
)-siz
;
838 if (uiop
->uio_iovcnt
<= 0 || uiop
->uio_iov
== NULL
)
840 left
= uiop
->uio_iov
->iov_len
;
841 uiocp
= uiop
->uio_iov
->iov_base
;
850 mbufcp
= mtod(mp
, caddr_t
);
853 xfer
= (left
> len
) ? len
: left
;
856 if (uiop
->uio_iov
->iov_op
!= NULL
)
857 (*(uiop
->uio_iov
->iov_op
))
858 (mbufcp
, uiocp
, xfer
);
861 if (uiop
->uio_segflg
== UIO_SYSSPACE
)
862 bcopy(mbufcp
, uiocp
, xfer
);
864 copyout(mbufcp
, uiocp
, xfer
);
869 uiop
->uio_offset
+= xfer
;
870 uiop
->uio_resid
-= xfer
;
872 if (uiop
->uio_iov
->iov_len
<= siz
) {
876 uiop
->uio_iov
->iov_base
+= uiosiz
;
877 uiop
->uio_iov
->iov_len
-= uiosiz
;
885 error
= nfs_adv(mrep
, dpos
, rem
, len
);
893 * copies a uio scatter/gather list to an mbuf chain.
894 * NOTE: can ony handle iovcnt == 1
897 nfsm_uiotombuf(uiop
, mq
, siz
, bpos
)
898 register struct uio
*uiop
;
903 register char *uiocp
;
904 register struct mbuf
*mp
, *mp2
;
905 register int xfer
, left
, mlen
;
906 int uiosiz
, clflg
, rem
;
909 if (uiop
->uio_iovcnt
!= 1)
910 panic("nfsm_uiotombuf: iovcnt != 1");
912 if (siz
> MLEN
) /* or should it >= MCLBYTES ?? */
916 rem
= nfsm_rndup(siz
)-siz
;
919 left
= uiop
->uio_iov
->iov_len
;
920 uiocp
= uiop
->uio_iov
->iov_base
;
925 mlen
= M_TRAILINGSPACE(mp
);
927 MGET(mp
, M_WAIT
, MT_DATA
);
933 mlen
= M_TRAILINGSPACE(mp
);
935 xfer
= (left
> mlen
) ? mlen
: left
;
938 if (uiop
->uio_iov
->iov_op
!= NULL
)
939 (*(uiop
->uio_iov
->iov_op
))
940 (uiocp
, mtod(mp
, caddr_t
)+mp
->m_len
, xfer
);
943 if (uiop
->uio_segflg
== UIO_SYSSPACE
)
944 bcopy(uiocp
, mtod(mp
, caddr_t
)+mp
->m_len
, xfer
);
946 copyin(uiocp
, mtod(mp
, caddr_t
)+mp
->m_len
, xfer
);
950 uiop
->uio_offset
+= xfer
;
951 uiop
->uio_resid
-= xfer
;
953 uiop
->uio_iov
->iov_base
+= uiosiz
;
954 uiop
->uio_iov
->iov_len
-= uiosiz
;
958 if (rem
> M_TRAILINGSPACE(mp
)) {
959 MGET(mp
, M_WAIT
, MT_DATA
);
963 cp
= mtod(mp
, caddr_t
)+mp
->m_len
;
964 for (left
= 0; left
< rem
; left
++)
969 *bpos
= mtod(mp
, caddr_t
)+mp
->m_len
;
975 * Help break down an mbuf chain by setting the first siz bytes contiguous
976 * pointed to by returned val.
977 * This is used by the macros nfsm_dissect and nfsm_dissecton for tough
978 * cases. (The macros use the vars. dpos and dpos2)
981 nfsm_disct(mdp
, dposp
, siz
, left
, cp2
)
988 register struct mbuf
*mp
, *mp2
;
989 register int siz2
, xfer
;
994 *mdp
= mp
= mp
->m_next
;
998 *dposp
= mtod(mp
, caddr_t
);
1003 } else if (mp
->m_next
== NULL
) {
1005 } else if (siz
> MHLEN
) {
1006 panic("nfs S too big");
1008 MGET(mp2
, M_WAIT
, MT_DATA
);
1009 mp2
->m_next
= mp
->m_next
;
1013 *cp2
= p
= mtod(mp
, caddr_t
);
1014 bcopy(*dposp
, p
, left
); /* Copy what was left */
1018 /* Loop around copying up the siz2 bytes */
1022 xfer
= (siz2
> mp2
->m_len
) ? mp2
->m_len
: siz2
;
1024 bcopy(mtod(mp2
, caddr_t
), p
, xfer
);
1035 *dposp
= mtod(mp2
, caddr_t
);
1041 * Advance the position in the mbuf chain.
1044 nfs_adv(mdp
, dposp
, offs
, left
)
1050 register struct mbuf
*m
;
1063 *dposp
= mtod(m
, caddr_t
)+offs
;
1068 * Copy a string into mbufs for the hard cases...
1071 nfsm_strtmbuf(mb
, bpos
, cp
, siz
)
1077 register struct mbuf
*m1
= 0, *m2
;
1078 long left
, xfer
, len
, tlen
;
1084 left
= M_TRAILINGSPACE(m2
);
1086 tl
= ((u_long
*)(*bpos
));
1087 *tl
++ = txdr_unsigned(siz
);
1089 left
-= NFSX_UNSIGNED
;
1090 m2
->m_len
+= NFSX_UNSIGNED
;
1092 bcopy(cp
, (caddr_t
) tl
, left
);
1099 /* Loop around adding mbufs */
1101 MGET(m1
, M_WAIT
, MT_DATA
);
1104 m1
->m_len
= NFSMSIZ(m1
);
1107 tl
= mtod(m1
, u_long
*);
1110 *tl
++ = txdr_unsigned(siz
);
1111 m1
->m_len
-= NFSX_UNSIGNED
;
1112 tlen
= NFSX_UNSIGNED
;
1115 if (siz
< m1
->m_len
) {
1116 len
= nfsm_rndup(siz
);
1119 *(tl
+(xfer
>>2)) = 0;
1121 xfer
= len
= m1
->m_len
;
1123 bcopy(cp
, (caddr_t
) tl
, xfer
);
1124 m1
->m_len
= len
+tlen
;
1129 *bpos
= mtod(m1
, caddr_t
)+m1
->m_len
;
1134 * Called once to initialize data structures...
1138 struct vfsconf
*vfsp
;
1143 * Check to see if major data structures haven't bloated.
1145 if (sizeof (struct nfsnode
) > NFS_NODEALLOC
) {
1146 printf("struct nfsnode bloated (> %dbytes)\n", NFS_NODEALLOC
);
1147 printf("Try reducing NFS_SMALLFH\n");
1149 if (sizeof (struct nfsmount
) > NFS_MNTALLOC
) {
1150 printf("struct nfsmount bloated (> %dbytes)\n", NFS_MNTALLOC
);
1151 printf("Try reducing NFS_MUIDHASHSIZ\n");
1153 if (sizeof (struct nfssvc_sock
) > NFS_SVCALLOC
) {
1154 printf("struct nfssvc_sock bloated (> %dbytes)\n",NFS_SVCALLOC
);
1155 printf("Try reducing NFS_UIDHASHSIZ\n");
1157 if (sizeof (struct nfsuid
) > NFS_UIDALLOC
) {
1158 printf("struct nfsuid bloated (> %dbytes)\n",NFS_UIDALLOC
);
1159 printf("Try unionizing the nu_nickname and nu_flag fields\n");
1161 nfs_mount_type
= vfsp
->vfc_typenum
;
1163 rpc_vers
= txdr_unsigned(RPC_VER2
);
1164 rpc_call
= txdr_unsigned(RPC_CALL
);
1165 rpc_reply
= txdr_unsigned(RPC_REPLY
);
1166 rpc_msgdenied
= txdr_unsigned(RPC_MSGDENIED
);
1167 rpc_msgaccepted
= txdr_unsigned(RPC_MSGACCEPTED
);
1168 rpc_mismatch
= txdr_unsigned(RPC_MISMATCH
);
1169 rpc_autherr
= txdr_unsigned(RPC_AUTHERR
);
1170 rpc_auth_unix
= txdr_unsigned(RPCAUTH_UNIX
);
1171 rpc_auth_kerb
= txdr_unsigned(RPCAUTH_KERB4
);
1172 nfs_prog
= txdr_unsigned(NFS_PROG
);
1173 nqnfs_prog
= txdr_unsigned(NQNFS_PROG
);
1174 nfs_true
= txdr_unsigned(TRUE
);
1175 nfs_false
= txdr_unsigned(FALSE
);
1176 nfs_xdrneg1
= txdr_unsigned(-1);
1177 nfs_ticks
= (hz
* NFS_TICKINTVL
+ 500) / 1000;
1180 /* Ensure async daemons disabled */
1181 for (i
= 0; i
< NFS_MAXASYNCDAEMON
; i
++) {
1182 nfs_iodwant
[i
] = (struct proc
*)0;
1183 nfs_iodmount
[i
] = (struct nfsmount
*)0;
1185 nfs_nhinit(); /* Init the nfsnode table */
1186 #ifndef NFS_NOSERVER
1187 nfsrv_init(0); /* Init server data structures */
1188 nfsrv_initcache(); /* Init the server request cache */
1192 * Initialize the nqnfs server stuff.
1194 if (nqnfsstarttime
== 0) {
1195 nqnfsstarttime
= boottime
.tv_sec
+ nqsrv_maxlease
1196 + nqsrv_clockskew
+ nqsrv_writeslack
;
1197 NQLOADNOVRAM(nqnfsstarttime
);
1198 CIRCLEQ_INIT(&nqtimerhead
);
1199 nqfhhashtbl
= hashinit(NQLCHSZ
, M_NQLEASE
, &nqfhhash
);
1203 * Initialize reply list and start timer
1205 TAILQ_INIT(&nfs_reqq
);
1210 /* XXX CSM 12/4/97 Where are these declared in FreeBSD? */
1213 * Set up lease_check and lease_updatetime so that other parts
1214 * of the system can call us, if we are loadable.
1216 #ifndef NFS_NOSERVER
1217 default_vnodeop_p
[VOFFSET(vop_lease
)] = (vop_t
*)nqnfs_vop_lease_check
;
1219 lease_updatetime
= nfs_lease_updatetime
;
1221 vfsp
->vfc_refcount
++; /* make us non-unloadable */
1222 sysent
[SYS_nfssvc
].sy_narg
= 2;
1223 sysent
[SYS_nfssvc
].sy_call
= nfssvc
;
1224 #ifndef NFS_NOSERVER
1225 sysent
[SYS_getfh
].sy_narg
= 2;
1226 sysent
[SYS_getfh
].sy_call
= getfh
;
1233 * Attribute cache routines.
1234 * nfs_loadattrcache() - loads or updates the cache contents from attributes
1235 * that are on the mbuf list
1236 * nfs_getattrcache() - returns valid attributes if found in cache, returns
1241 * Load the attribute cache (that lives in the nfsnode entry) with
1242 * the values on the mbuf list and
1244 * copy the attributes to *vaper
1247 nfs_loadattrcache(vpp
, mdp
, dposp
, vaper
, dontshrink
, xidp
)
1251 struct vattr
*vaper
;
1255 register struct vnode
*vp
= *vpp
;
1256 register struct vattr
*vap
;
1257 register struct nfs_fattr
*fp
;
1258 register struct nfsnode
*np
;
1261 int error
= 0, rdev
;
1265 struct timespec mtime
;
1269 FSDBG_TOP(527, vp
, 0, *xidp
>> 32, *xidp
);
1271 * this routine is a good place to check for VBAD again. We caught
1272 * most of them in nfsm_request, but postprocessing may indirectly get
1273 * here, so check again.
1275 if (vp
->v_type
== VBAD
) {
1276 FSDBG_BOT(527, EINVAL
, 1, 0, *xidp
);
1282 t1
= (mtod(md
, caddr_t
) + md
->m_len
) - *dposp
;
1283 if ((error
= nfsm_disct(mdp
, dposp
, NFSX_FATTR(v3
), t1
, &cp2
))) {
1284 FSDBG_BOT(527, error
, 2, 0, *xidp
);
1287 fp
= (struct nfs_fattr
*)cp2
;
1289 vtyp
= nfsv3tov_type(fp
->fa_type
);
1290 vmode
= fxdr_unsigned(u_short
, fp
->fa_mode
);
1291 rdev
= makedev(fxdr_unsigned(int, fp
->fa3_rdev
.specdata1
),
1292 fxdr_unsigned(int, fp
->fa3_rdev
.specdata2
));
1293 fxdr_nfsv3time(&fp
->fa3_mtime
, &mtime
);
1295 vtyp
= nfsv2tov_type(fp
->fa_type
);
1296 vmode
= fxdr_unsigned(u_short
, fp
->fa_mode
);
1300 * The duplicate information returned in fa_type and fa_mode
1301 * is an ambiguity in the NFS version 2 protocol.
1303 * VREG should be taken literally as a regular file. If a
1304 * server intents to return some type information differently
1305 * in the upper bits of the mode field (e.g. for sockets, or
1306 * FIFOs), NFSv2 mandates fa_type to be VNON. Anyway, we
1307 * leave the examination of the mode bits even in the VREG
1308 * case to avoid breakage for bogus servers, but we make sure
1309 * that there are actually type bits set in the upper part of
1310 * fa_mode (and failing that, trust the va_type field).
1312 * NFSv3 cleared the issue, and requires fa_mode to not
1313 * contain any type information (while also introduing sockets
1314 * and FIFOs for fa_type).
1316 if (vtyp
== VNON
|| (vtyp
== VREG
&& (vmode
& S_IFMT
) != 0))
1317 vtyp
= IFTOVT(vmode
);
1318 rdev
= fxdr_unsigned(long, fp
->fa2_rdev
);
1319 fxdr_nfsv2time(&fp
->fa2_mtime
, &mtime
);
1322 * Really ugly NFSv2 kludge.
1324 if (vtyp
== VCHR
&& rdev
== 0xffffffff)
1329 * If v_type == VNON it is a new node, so fill in the v_type,
1330 * n_mtime fields. Check to see if it represents a special
1331 * device, and if so, check for a possible alias. Once the
1332 * correct vnode has been obtained, fill in the rest of the
1336 if (*xidp
< np
->n_xid
) {
1338 * We have already updated attributes with a response from
1339 * a later request. The attributes we have here are probably
1340 * stale so we drop them (just return). However, our
1341 * out-of-order receipt could be correct - if the requests were
1342 * processed out of order at the server. Given the uncertainty
1343 * we invalidate our cached attributes. *xidp is zeroed here
1344 * to indicate the attributes were dropped - only getattr
1345 * cares - it needs to retry the rpc.
1347 np
->n_attrstamp
= 0;
1348 FSDBG_BOT(527, 0, np
, np
->n_xid
, *xidp
);
1352 if (vp
->v_type
!= vtyp
) {
1355 if (UBCINFOMISSING(vp
) || UBCINFORECLAIMED(vp
))
1356 if ((error
= ubc_info_init(vp
))) { /* VREG */
1357 FSDBG_BOT(527, error
, 3, 0, *xidp
);
1361 if (vp
->v_type
== VFIFO
) {
1362 vp
->v_op
= fifo_nfsv2nodeop_p
;
1364 if (vp
->v_type
== VCHR
|| vp
->v_type
== VBLK
) {
1365 vp
->v_op
= spec_nfsv2nodeop_p
;
1366 nvp
= checkalias(vp
, (dev_t
)rdev
, vp
->v_mount
);
1369 * Discard unneeded vnode, but save its nfsnode.
1370 * Since the nfsnode does not have a lock, its
1371 * vnode lock has to be carried over.
1373 nvp
->v_vnlock
= vp
->v_vnlock
;
1374 vp
->v_vnlock
= NULL
;
1375 nvp
->v_data
= vp
->v_data
;
1377 vp
->v_op
= spec_vnodeop_p
;
1381 * Reinitialize aliased node.
1387 np
->n_mtime
= mtime
.tv_sec
;
1388 FSDBG(527, vp
, np
->n_mtime
, 0, 0);
1392 vap
->va_type
= vtyp
;
1393 vap
->va_mode
= (vmode
& 07777);
1394 vap
->va_rdev
= (dev_t
)rdev
;
1395 vap
->va_mtime
= mtime
;
1396 vap
->va_fsid
= vp
->v_mount
->mnt_stat
.f_fsid
.val
[0];
1398 vap
->va_nlink
= fxdr_unsigned(u_short
, fp
->fa_nlink
);
1399 vap
->va_uid
= fxdr_unsigned(uid_t
, fp
->fa_uid
);
1400 vap
->va_gid
= fxdr_unsigned(gid_t
, fp
->fa_gid
);
1401 fxdr_hyper(&fp
->fa3_size
, &vap
->va_size
);
1402 vap
->va_blocksize
= NFS_FABLKSIZE
;
1403 fxdr_hyper(&fp
->fa3_used
, &vap
->va_bytes
);
1404 vap
->va_fileid
= fxdr_unsigned(int, fp
->fa3_fileid
.nfsuquad
[1]);
1405 fxdr_nfsv3time(&fp
->fa3_atime
, &vap
->va_atime
);
1406 fxdr_nfsv3time(&fp
->fa3_ctime
, &vap
->va_ctime
);
1408 vap
->va_filerev
= 0;
1410 vap
->va_nlink
= fxdr_unsigned(u_short
, fp
->fa_nlink
);
1411 vap
->va_uid
= fxdr_unsigned(uid_t
, fp
->fa_uid
);
1412 vap
->va_gid
= fxdr_unsigned(gid_t
, fp
->fa_gid
);
1413 vap
->va_size
= fxdr_unsigned(u_long
, fp
->fa2_size
);
1414 vap
->va_blocksize
= fxdr_unsigned(long, fp
->fa2_blocksize
);
1415 vap
->va_bytes
= fxdr_unsigned(long, fp
->fa2_blocks
) * NFS_FABLKSIZE
;
1416 vap
->va_fileid
= fxdr_unsigned(long, fp
->fa2_fileid
);
1417 fxdr_nfsv2time(&fp
->fa2_atime
, &vap
->va_atime
);
1419 vap
->va_ctime
.tv_sec
= fxdr_unsigned(long, fp
->fa2_ctime
.nfsv2_sec
);
1420 vap
->va_ctime
.tv_nsec
= 0;
1421 vap
->va_gen
= fxdr_unsigned(u_long
, fp
->fa2_ctime
.nfsv2_usec
);
1422 vap
->va_filerev
= 0;
1425 np
->n_attrstamp
= time
.tv_sec
;
1426 if (vap
->va_size
!= np
->n_size
) {
1427 FSDBG(527, vp
, vap
->va_size
, np
->n_size
,
1428 (vap
->va_type
== VREG
) |
1429 (np
->n_flag
& NMODIFIED
? 6 : 4));
1430 if (vap
->va_type
== VREG
) {
1433 orig_size
= np
->n_size
;
1434 if (np
->n_flag
& NMODIFIED
) {
1435 if (vap
->va_size
< np
->n_size
)
1436 vap
->va_size
= np
->n_size
;
1438 np
->n_size
= vap
->va_size
;
1440 np
->n_size
= vap
->va_size
;
1441 if (!UBCINFOEXISTS(vp
) ||
1442 dontshrink
&& np
->n_size
< ubc_getsize(vp
)) {
1443 vap
->va_size
= np
->n_size
= orig_size
;
1444 np
->n_attrstamp
= 0;
1446 ubc_setsize(vp
, (off_t
)np
->n_size
); /* XXX */
1448 np
->n_size
= vap
->va_size
;
1451 if (vaper
!= NULL
) {
1452 bcopy((caddr_t
)vap
, (caddr_t
)vaper
, sizeof(*vap
));
1453 if (np
->n_flag
& NCHG
) {
1454 if (np
->n_flag
& NACC
)
1455 vaper
->va_atime
= np
->n_atim
;
1456 if (np
->n_flag
& NUPD
)
1457 vaper
->va_mtime
= np
->n_mtim
;
1460 FSDBG_BOT(527, 0, np
, 0, *xidp
);
1465 * Check the time stamp
1466 * If the cache is valid, copy contents to *vap and return 0
1467 * otherwise return an error
1470 nfs_getattrcache(vp
, vaper
)
1471 register struct vnode
*vp
;
1472 struct vattr
*vaper
;
1474 register struct nfsnode
*np
= VTONFS(vp
);
1475 register struct vattr
*vap
;
1477 if ((time
.tv_sec
- np
->n_attrstamp
) >= NFS_ATTRTIMEO(np
)) {
1478 FSDBG(528, vp
, 0, 0, 1);
1479 nfsstats
.attrcache_misses
++;
1482 FSDBG(528, vp
, 0, 0, 2);
1483 nfsstats
.attrcache_hits
++;
1486 if (vap
->va_size
!= np
->n_size
) {
1487 FSDBG(528, vp
, vap
->va_size
, np
->n_size
,
1488 (vap
->va_type
== VREG
) |
1489 (np
->n_flag
& NMODIFIED
? 6 : 4));
1490 if (vap
->va_type
== VREG
) {
1491 if (np
->n_flag
& NMODIFIED
) {
1492 if (vap
->va_size
< np
->n_size
)
1493 vap
->va_size
= np
->n_size
;
1495 np
->n_size
= vap
->va_size
;
1497 np
->n_size
= vap
->va_size
;
1498 ubc_setsize(vp
, (off_t
)np
->n_size
); /* XXX */
1500 np
->n_size
= vap
->va_size
;
1503 bcopy((caddr_t
)vap
, (caddr_t
)vaper
, sizeof(struct vattr
));
1504 if (np
->n_flag
& NCHG
) {
1505 if (np
->n_flag
& NACC
)
1506 vaper
->va_atime
= np
->n_atim
;
1507 if (np
->n_flag
& NUPD
)
1508 vaper
->va_mtime
= np
->n_mtim
;
1513 #ifndef NFS_NOSERVER
1515 * Set up nameidata for a lookup() call and do it.
1517 * If pubflag is set, this call is done for a lookup operation on the
1518 * public filehandle. In that case we allow crossing mountpoints and
1519 * absolute pathnames. However, the caller is expected to check that
1520 * the lookup result is within the public fs, and deny access if
1524 nfs_namei(ndp
, fhp
, len
, slp
, nam
, mdp
, dposp
, retdirp
, p
, kerbflag
, pubflag
)
1525 register struct nameidata
*ndp
;
1528 struct nfssvc_sock
*slp
;
1532 struct vnode
**retdirp
;
1534 int kerbflag
, pubflag
;
1536 register int i
, rem
;
1537 register struct mbuf
*md
;
1538 register char *fromcp
, *tocp
, *cp
;
1542 int error
, rdonly
, linklen
;
1543 struct componentname
*cnp
= &ndp
->ni_cnd
;
1546 *retdirp
= (struct vnode
*)0;
1547 MALLOC_ZONE(cnp
->cn_pnbuf
, char *, len
+ 1, M_NAMEI
, M_WAITOK
);
1548 cnp
->cn_pnlen
= len
+ 1;
1551 * Copy the name from the mbuf list to ndp->ni_pnbuf
1552 * and set the various ndp fields appropriately.
1555 tocp
= cnp
->cn_pnbuf
;
1557 rem
= mtod(md
, caddr_t
) + md
->m_len
- fromcp
;
1559 for (i
= 1; i
<= len
; i
++) {
1566 fromcp
= mtod(md
, caddr_t
);
1569 /* XXX CSM 12/4/97 Revisit when enabling WebNFS */
1571 if (*fromcp
== '\0' || (!pubflag
&& *fromcp
== '/')) {
1573 if (*fromcp
== '\0' || *fromcp
== '/') {
1578 cnp
->cn_hash
+= (unsigned char)*fromcp
* i
;
1579 *tocp
++ = *fromcp
++;
1585 len
= nfsm_rndup(len
)-len
;
1589 else if ((error
= nfs_adv(mdp
, dposp
, len
, rem
)) != 0)
1594 * Extract and set starting directory.
1596 error
= nfsrv_fhtovp(fhp
, FALSE
, &dp
, ndp
->ni_cnd
.cn_cred
, slp
,
1597 nam
, &rdonly
, kerbflag
, pubflag
);
1600 if (dp
->v_type
!= VDIR
) {
1607 cnp
->cn_flags
|= RDONLY
;
1611 /* XXX CSM 12/4/97 Revisit when enabling WebNFS */
1612 /* XXX debo 12/15/97 Need to fix M_NAMEI allocations to use zone protocol */
1616 * Oh joy. For WebNFS, handle those pesky '%' escapes,
1617 * and the 'native path' indicator.
1619 MALLOC(cp
, char *, olen
+ 1, M_NAMEI
, M_WAITOK
);
1620 fromcp
= cnp
->cn_pnbuf
;
1622 if ((unsigned char)*fromcp
>= WEBNFS_SPECCHAR_START
) {
1623 switch ((unsigned char)*fromcp
) {
1624 case WEBNFS_NATIVE_CHAR
:
1626 * 'Native' path for us is the same
1627 * as a path according to the NFS spec,
1628 * just skip the escape char.
1633 * More may be added in the future, range 0x80-0xff
1642 * Translate the '%' escapes, URL-style.
1644 while (*fromcp
!= '\0') {
1645 if (*fromcp
== WEBNFS_ESC_CHAR
) {
1646 if (fromcp
[1] != '\0' && fromcp
[2] != '\0') {
1648 *tocp
++ = HEXSTRTOI(fromcp
);
1657 *tocp
++ = *fromcp
++;
1660 FREE(cnp
->cn_pnbuf
, M_NAMEI
);
1665 ndp
->ni_pathlen
= (tocp
- cnp
->cn_pnbuf
) + 1;
1666 ndp
->ni_segflg
= UIO_SYSSPACE
;
1668 /* XXX CSM 12/4/97 Revisit when enabling WebNFS */
1671 ndp
->ni_rootdir
= rootvnode
;
1672 ndp
->ni_loopcnt
= 0;
1673 if (cnp
->cn_pnbuf
[0] == '/')
1676 cnp
->cn_flags
|= NOCROSSMOUNT
;
1679 cnp
->cn_flags
|= NOCROSSMOUNT
;
1686 cnp
->cn_nameptr
= cnp
->cn_pnbuf
;
1687 ndp
->ni_startdir
= dp
;
1689 * And call lookup() to do the real work
1691 error
= lookup(ndp
);
1695 * Check for encountering a symbolic link
1697 if ((cnp
->cn_flags
& ISSYMLINK
) == 0) {
1698 nfsrv_object_create(ndp
->ni_vp
);
1699 if (cnp
->cn_flags
& (SAVENAME
| SAVESTART
)) {
1700 cnp
->cn_flags
|= HASBUF
;
1705 if ((cnp
->cn_flags
& LOCKPARENT
) && ndp
->ni_pathlen
== 1)
1706 VOP_UNLOCK(ndp
->ni_dvp
, 0, p
);
1707 /* XXX CSM 12/4/97 Revisit when enabling WebNFS */
1716 /* XXX CSM 12/4/97 Revisit when enabling WebNFS */
1717 /* XXX debo 12/15/97 Need to fix M_NAMEI allocations to use zone protocol */
1721 if (ndp
->ni_loopcnt
++ >= MAXSYMLINKS
) {
1725 if (ndp
->ni_pathlen
> 1)
1726 MALLOC(cp
, char *, olen
+ 1, M_NAMEI
, M_WAITOK
);
1730 aiov
.iov_len
= MAXPATHLEN
;
1731 auio
.uio_iov
= &aiov
;
1732 auio
.uio_iovcnt
= 1;
1733 auio
.uio_offset
= 0;
1734 auio
.uio_rw
= UIO_READ
;
1735 auio
.uio_segflg
= UIO_SYSSPACE
;
1736 auio
.uio_procp
= (struct proc
*)0;
1737 auio
.uio_resid
= MAXPATHLEN
;
1738 error
= VOP_READLINK(ndp
->ni_vp
, &auio
, cnp
->cn_cred
);
1741 if (ndp
->ni_pathlen
> 1)
1745 linklen
= MAXPATHLEN
- auio
.uio_resid
;
1750 if (linklen
+ ndp
->ni_pathlen
>= MAXPATHLEN
) {
1751 error
= ENAMETOOLONG
;
1754 if (ndp
->ni_pathlen
> 1) {
1755 bcopy(ndp
->ni_next
, cp
+ linklen
, ndp
->ni_pathlen
);
1756 FREE(cnp
->cn_pnbuf
, M_NAMEI
);
1759 cnp
->cn_pnbuf
[linklen
] = '\0';
1760 ndp
->ni_pathlen
+= linklen
;
1764 * Check if root directory should replace current directory.
1766 if (cnp
->cn_pnbuf
[0] == '/') {
1768 dp
= ndp
->ni_rootdir
;
1775 FREE_ZONE(cnp
->cn_pnbuf
, cnp
->cn_pnlen
, M_NAMEI
);
1780 * A fiddled version of m_adj() that ensures null fill to a long
1781 * boundary and only trims off the back end
1784 nfsm_adj(mp
, len
, nul
)
1789 register struct mbuf
*m
;
1790 register int count
, i
;
1794 * Trim from tail. Scan the mbuf chain,
1795 * calculating its length and finding the last mbuf.
1796 * If the adjustment only affects this mbuf, then just
1797 * adjust and return. Otherwise, rescan and truncate
1798 * after the remaining size.
1804 if (m
->m_next
== (struct mbuf
*)0)
1808 if (m
->m_len
> len
) {
1811 cp
= mtod(m
, caddr_t
)+m
->m_len
-nul
;
1812 for (i
= 0; i
< nul
; i
++)
1821 * Correct length for chain is "count".
1822 * Find the mbuf with last data, adjust its length,
1823 * and toss data from remaining mbufs on chain.
1825 for (m
= mp
; m
; m
= m
->m_next
) {
1826 if (m
->m_len
>= count
) {
1829 cp
= mtod(m
, caddr_t
)+m
->m_len
-nul
;
1830 for (i
= 0; i
< nul
; i
++)
1837 for (m
= m
->m_next
;m
;m
= m
->m_next
)
1842 * Make these functions instead of macros, so that the kernel text size
1843 * doesn't get too big...
1846 nfsm_srvwcc(nfsd
, before_ret
, before_vap
, after_ret
, after_vap
, mbp
, bposp
)
1847 struct nfsrv_descript
*nfsd
;
1849 register struct vattr
*before_vap
;
1851 struct vattr
*after_vap
;
1855 register struct mbuf
*mb
= *mbp
, *mb2
;
1856 register char *bpos
= *bposp
;
1857 register u_long
*tl
;
1860 nfsm_build(tl
, u_long
*, NFSX_UNSIGNED
);
1863 nfsm_build(tl
, u_long
*, 7 * NFSX_UNSIGNED
);
1865 txdr_hyper(&(before_vap
->va_size
), tl
);
1867 txdr_nfsv3time(&(before_vap
->va_mtime
), tl
);
1869 txdr_nfsv3time(&(before_vap
->va_ctime
), tl
);
1873 nfsm_srvpostopattr(nfsd
, after_ret
, after_vap
, mbp
, bposp
);
1877 nfsm_srvpostopattr(nfsd
, after_ret
, after_vap
, mbp
, bposp
)
1878 struct nfsrv_descript
*nfsd
;
1880 struct vattr
*after_vap
;
1884 register struct mbuf
*mb
= *mbp
, *mb2
;
1885 register char *bpos
= *bposp
;
1886 register u_long
*tl
;
1887 register struct nfs_fattr
*fp
;
1890 nfsm_build(tl
, u_long
*, NFSX_UNSIGNED
);
1893 nfsm_build(tl
, u_long
*, NFSX_UNSIGNED
+ NFSX_V3FATTR
);
1895 fp
= (struct nfs_fattr
*)tl
;
1896 nfsm_srvfattr(nfsd
, after_vap
, fp
);
1903 nfsm_srvfattr(nfsd
, vap
, fp
)
1904 register struct nfsrv_descript
*nfsd
;
1905 register struct vattr
*vap
;
1906 register struct nfs_fattr
*fp
;
1909 fp
->fa_nlink
= txdr_unsigned(vap
->va_nlink
);
1910 fp
->fa_uid
= txdr_unsigned(vap
->va_uid
);
1911 fp
->fa_gid
= txdr_unsigned(vap
->va_gid
);
1912 if (nfsd
->nd_flag
& ND_NFSV3
) {
1913 fp
->fa_type
= vtonfsv3_type(vap
->va_type
);
1914 fp
->fa_mode
= vtonfsv3_mode(vap
->va_mode
);
1915 txdr_hyper(&vap
->va_size
, &fp
->fa3_size
);
1916 txdr_hyper(&vap
->va_bytes
, &fp
->fa3_used
);
1917 fp
->fa3_rdev
.specdata1
= txdr_unsigned(major(vap
->va_rdev
));
1918 fp
->fa3_rdev
.specdata2
= txdr_unsigned(minor(vap
->va_rdev
));
1919 fp
->fa3_fsid
.nfsuquad
[0] = 0;
1920 fp
->fa3_fsid
.nfsuquad
[1] = txdr_unsigned(vap
->va_fsid
);
1921 fp
->fa3_fileid
.nfsuquad
[0] = 0;
1922 fp
->fa3_fileid
.nfsuquad
[1] = txdr_unsigned(vap
->va_fileid
);
1923 txdr_nfsv3time(&vap
->va_atime
, &fp
->fa3_atime
);
1924 txdr_nfsv3time(&vap
->va_mtime
, &fp
->fa3_mtime
);
1925 txdr_nfsv3time(&vap
->va_ctime
, &fp
->fa3_ctime
);
1927 fp
->fa_type
= vtonfsv2_type(vap
->va_type
);
1928 fp
->fa_mode
= vtonfsv2_mode(vap
->va_type
, vap
->va_mode
);
1929 fp
->fa2_size
= txdr_unsigned(vap
->va_size
);
1930 fp
->fa2_blocksize
= txdr_unsigned(vap
->va_blocksize
);
1931 if (vap
->va_type
== VFIFO
)
1932 fp
->fa2_rdev
= 0xffffffff;
1934 fp
->fa2_rdev
= txdr_unsigned(vap
->va_rdev
);
1935 fp
->fa2_blocks
= txdr_unsigned(vap
->va_bytes
/ NFS_FABLKSIZE
);
1936 fp
->fa2_fsid
= txdr_unsigned(vap
->va_fsid
);
1937 fp
->fa2_fileid
= txdr_unsigned(vap
->va_fileid
);
1938 txdr_nfsv2time(&vap
->va_atime
, &fp
->fa2_atime
);
1939 txdr_nfsv2time(&vap
->va_mtime
, &fp
->fa2_mtime
);
1940 txdr_nfsv2time(&vap
->va_ctime
, &fp
->fa2_ctime
);
1945 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
1946 * - look up fsid in mount list (if not found ret error)
1947 * - get vp and export rights by calling VFS_FHTOVP()
1948 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
1949 * - if not lockflag unlock it with VOP_UNLOCK()
1952 nfsrv_fhtovp(fhp
, lockflag
, vpp
, cred
, slp
, nam
, rdonlyp
, kerbflag
, pubflag
)
1957 struct nfssvc_sock
*slp
;
1963 struct proc
*p
= current_proc(); /* XXX */
1964 register struct mount
*mp
;
1966 struct ucred
*credanon
;
1969 *vpp
= (struct vnode
*)0;
1971 /* XXX CSM 12/4/97 Revisit when enabling WebNFS */
1973 if (nfs_ispublicfh(fhp
)) {
1974 if (!pubflag
|| !nfs_pub
.np_valid
)
1976 fhp
= &nfs_pub
.np_handle
;
1980 mp
= vfs_getvfs(&fhp
->fh_fsid
);
1983 error
= VFS_FHTOVP(mp
, &fhp
->fh_fid
, nam
, vpp
, &exflags
, &credanon
);
1986 /* vnode pointer should be good at this point or ... */
1990 * Check/setup credentials.
1992 if (exflags
& MNT_EXKERB
) {
1995 return (NFSERR_AUTHERR
| AUTH_TOOWEAK
);
1997 } else if (kerbflag
) {
1999 return (NFSERR_AUTHERR
| AUTH_TOOWEAK
);
2000 } else if (cred
->cr_uid
== 0 || (exflags
& MNT_EXPORTANON
)) {
2001 cred
->cr_uid
= credanon
->cr_uid
;
2002 for (i
= 0; i
< credanon
->cr_ngroups
&& i
< NGROUPS
; i
++)
2003 cred
->cr_groups
[i
] = credanon
->cr_groups
[i
];
2004 cred
->cr_ngroups
= i
;
2006 if (exflags
& MNT_EXRDONLY
)
2011 nfsrv_object_create(*vpp
);
2014 VOP_UNLOCK(*vpp
, 0, p
);
2020 * WebNFS: check if a filehandle is a public filehandle. For v3, this
2021 * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has
2022 * transformed this to all zeroes in both cases, so check for it.
2028 char *cp
= (char *)fhp
;
2031 for (i
= 0; i
< NFSX_V3FH
; i
++)
2037 #endif /* NFS_NOSERVER */
2039 * This function compares two net addresses by family and returns TRUE
2040 * if they are the same host.
2041 * If there is any doubt, return FALSE.
2042 * The AF_INET family is handled as a special case so that address mbufs
2043 * don't need to be saved to store "struct in_addr", which is only 4 bytes.
2046 netaddr_match(family
, haddr
, nam
)
2048 union nethostaddr
*haddr
;
2051 register struct sockaddr_in
*inetaddr
;
2055 inetaddr
= mtod(nam
, struct sockaddr_in
*);
2056 if (inetaddr
->sin_family
== AF_INET
&&
2057 inetaddr
->sin_addr
.s_addr
== haddr
->had_inetaddr
)
2063 register struct sockaddr_iso
*isoaddr1
, *isoaddr2
;
2065 isoaddr1
= mtod(nam
, struct sockaddr_iso
*);
2066 isoaddr2
= mtod(haddr
->had_nam
, struct sockaddr_iso
*);
2067 if (isoaddr1
->siso_family
== AF_ISO
&&
2068 isoaddr1
->siso_nlen
> 0 &&
2069 isoaddr1
->siso_nlen
== isoaddr2
->siso_nlen
&&
2070 SAME_ISOADDR(isoaddr1
, isoaddr2
))
2081 static nfsuint64 nfs_nullcookie
= { 0, 0 };
2083 * This function finds the directory cookie that corresponds to the
2084 * logical byte offset given.
2087 nfs_getcookie(np
, off
, add
)
2088 register struct nfsnode
*np
;
2092 register struct nfsdmap
*dp
, *dp2
;
2095 pos
= off
/ NFS_DIRBLKSIZ
;
2099 panic("nfs getcookie add at 0");
2101 return (&nfs_nullcookie
);
2104 dp
= np
->n_cookies
.lh_first
;
2107 MALLOC_ZONE(dp
, struct nfsdmap
*,
2108 sizeof (struct nfsdmap
),
2109 M_NFSDIROFF
, M_WAITOK
);
2110 dp
->ndm_eocookie
= 0;
2111 LIST_INSERT_HEAD(&np
->n_cookies
, dp
, ndm_list
);
2113 return ((nfsuint64
*)0);
2115 while (pos
>= NFSNUMCOOKIES
) {
2116 pos
-= NFSNUMCOOKIES
;
2117 if (dp
->ndm_list
.le_next
) {
2118 if (!add
&& dp
->ndm_eocookie
< NFSNUMCOOKIES
&&
2119 pos
>= dp
->ndm_eocookie
)
2120 return ((nfsuint64
*)0);
2121 dp
= dp
->ndm_list
.le_next
;
2123 MALLOC_ZONE(dp2
, struct nfsdmap
*,
2124 sizeof (struct nfsdmap
),
2125 M_NFSDIROFF
, M_WAITOK
);
2126 dp2
->ndm_eocookie
= 0;
2127 LIST_INSERT_AFTER(dp
, dp2
, ndm_list
);
2130 return ((nfsuint64
*)0);
2132 if (pos
>= dp
->ndm_eocookie
) {
2134 dp
->ndm_eocookie
= pos
+ 1;
2136 return ((nfsuint64
*)0);
2138 return (&dp
->ndm_cookies
[pos
]);
2142 * Invalidate cached directory information, except for the actual directory
2143 * blocks (which are invalidated separately).
2144 * Done mainly to avoid the use of stale offset cookies.
2148 register struct vnode
*vp
;
2150 register struct nfsnode
*np
= VTONFS(vp
);
2153 if (vp
->v_type
!= VDIR
)
2154 panic("nfs: invaldir not dir");
2156 np
->n_direofoffset
= 0;
2157 np
->n_cookieverf
.nfsuquad
[0] = 0;
2158 np
->n_cookieverf
.nfsuquad
[1] = 0;
2159 if (np
->n_cookies
.lh_first
)
2160 np
->n_cookies
.lh_first
->ndm_eocookie
= 0;
2164 * The write verifier has changed (probably due to a server reboot), so all
2165 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
2166 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
2167 * flag. Once done the new write verifier can be set for the mount point.
2173 register struct vnode
*vp
, *nvp
;
2174 register struct buf
*bp
, *nbp
;
2179 for (vp
= mp
->mnt_vnodelist
.lh_first
; vp
; vp
= nvp
) {
2180 if (vp
->v_mount
!= mp
) /* Paranoia */
2182 nvp
= vp
->v_mntvnodes
.le_next
;
2183 for (bp
= vp
->v_dirtyblkhd
.lh_first
; bp
; bp
= nbp
) {
2184 nbp
= bp
->b_vnbufs
.le_next
;
2185 if ((bp
->b_flags
& (B_BUSY
| B_DELWRI
| B_NEEDCOMMIT
))
2186 == (B_DELWRI
| B_NEEDCOMMIT
))
2187 bp
->b_flags
&= ~B_NEEDCOMMIT
;
2193 #ifndef NFS_NOSERVER
2195 * Map errnos to NFS error numbers. For Version 3 also filter out error
2196 * numbers not specified for the associated procedure.
2199 nfsrv_errmap(nd
, err
)
2200 struct nfsrv_descript
*nd
;
2203 register short *defaulterrp
, *errp
;
2205 if (nd
->nd_flag
& ND_NFSV3
) {
2206 if (nd
->nd_procnum
<= NFSPROC_COMMIT
) {
2207 errp
= defaulterrp
= nfsrv_v3errmap
[nd
->nd_procnum
];
2211 else if (*errp
> err
)
2214 return ((int)*defaulterrp
);
2216 return (err
& 0xffff);
2219 return ((int)nfsrv_v2errmap
[err
- 1]);
2223 /* XXX CSM 11/25/97 Revisit when Ramesh merges vm with buffer cache */
2224 #define vfs_object_create(v, p, c, l) (0)
2227 nfsrv_object_create(struct vnode
*vp
) {
2228 struct proc
*curproc
= current_proc();
2230 if ((vp
== NULL
) || (vp
->v_type
!= VREG
))
2232 return vfs_object_create(vp
, curproc
, curproc
?curproc
->p_ucred
:NULL
, 1);
2234 #endif /* NFS_NOSERVER */