2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1989, 1991, 1993, 1995
25 * The Regents of the University of California. All rights reserved.
27 * This code is derived from software contributed to Berkeley by
28 * Rick Macklem at The University of Guelph.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * @(#)nfs_socket.c 8.5 (Berkeley) 3/30/95
59 * FreeBSD-Id: nfs_socket.c,v 1.30 1997/10/28 15:59:07 bde Exp $
63 * Socket operations for use by nfs
66 #include <sys/param.h>
67 #include <sys/systm.h>
69 #include <sys/mount.h>
70 #include <sys/kernel.h>
72 #include <sys/malloc.h>
73 #include <sys/vnode.h>
74 #include <sys/domain.h>
75 #include <sys/protosw.h>
76 #include <sys/socket.h>
77 #include <sys/socketvar.h>
78 #include <sys/syslog.h>
79 #include <sys/tprintf.h>
80 #include <machine/spl.h>
83 #include <kern/clock.h>
85 #include <netinet/in.h>
86 #include <netinet/tcp.h>
88 #include <nfs/rpcv2.h>
89 #include <nfs/nfsproto.h>
91 #include <nfs/xdr_subs.h>
92 #include <nfs/nfsm_subs.h>
93 #include <nfs/nfsmount.h>
94 #include <nfs/nfsnode.h>
95 #include <nfs/nfsrtt.h>
96 #include <nfs/nqnfs.h>
98 #include <sys/kdebug.h>
100 #define FSDBG(A, B, C, D, E) \
101 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_NONE, \
102 (int)(B), (int)(C), (int)(D), (int)(E), 0)
103 #define FSDBG_TOP(A, B, C, D, E) \
104 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_START, \
105 (int)(B), (int)(C), (int)(D), (int)(E), 0)
106 #define FSDBG_BOT(A, B, C, D, E) \
107 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_END, \
108 (int)(B), (int)(C), (int)(D), (int)(E), 0)
114 * Estimate rto for an nfs rpc sent via. an unreliable datagram.
115 * Use the mean and mean deviation of rtt for the appropriate type of rpc
116 * for the frequent rpcs and a default for the others.
117 * The justification for doing "other" this way is that these rpcs
118 * happen so infrequently that timer est. would probably be stale.
119 * Also, since many of these rpcs are
120 * non-idempotent, a conservative timeout is desired.
121 * getattr, lookup - A+2D
125 #define NFS_RTO(n, t) \
126 ((t) == 0 ? (n)->nm_timeo : \
128 (((((n)->nm_srtt[t-1] + 3) >> 2) + (n)->nm_sdrtt[t-1] + 1) >> 1) : \
129 ((((n)->nm_srtt[t-1] + 7) >> 3) + (n)->nm_sdrtt[t-1] + 1)))
130 #define NFS_SRTT(r) (r)->r_nmp->nm_srtt[proct[(r)->r_procnum] - 1]
131 #define NFS_SDRTT(r) (r)->r_nmp->nm_sdrtt[proct[(r)->r_procnum] - 1]
133 * External data, mostly RPC constants in XDR form
135 extern u_long rpc_reply
, rpc_msgdenied
, rpc_mismatch
, rpc_vers
, rpc_auth_unix
,
136 rpc_msgaccepted
, rpc_call
, rpc_autherr
,
138 extern u_long nfs_prog
, nqnfs_prog
;
139 extern time_t nqnfsstarttime
;
140 extern struct nfsstats nfsstats
;
141 extern int nfsv3_procid
[NFS_NPROCS
];
142 extern int nfs_ticks
;
143 extern u_long nfs_xidwrap
;
146 * Defines which timer to use for the procnum.
153 static int proct
[NFS_NPROCS
] = {
154 0, 1, 0, 2, 1, 3, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0,
159 * There is a congestion window for outstanding rpcs maintained per mount
160 * point. The cwnd size is adjusted in roughly the way that:
161 * Van Jacobson, Congestion avoidance and Control, In "Proceedings of
162 * SIGCOMM '88". ACM, August 1988.
163 * describes for TCP. The cwnd size is chopped in half on a retransmit timeout
164 * and incremented by 1/cwnd when each rpc reply is received and a full cwnd
165 * of rpcs is in progress.
166 * (The sent count and cwnd are scaled for integer arith.)
167 * Variants of "slow start" were tried and were found to be too much of a
168 * performance hit (ave. rtt 3 times larger),
169 * I suspect due to the large rtt that nfs rpcs have.
171 #define NFS_CWNDSCALE 256
172 #define NFS_MAXCWND (NFS_CWNDSCALE * 32)
173 static int nfs_backoff
[8] = { 2, 4, 8, 16, 32, 64, 128, 256, };
175 struct nfsrtt nfsrtt
;
177 static int nfs_msg
__P((struct proc
*,char *,char *));
178 static int nfs_rcvlock
__P((struct nfsreq
*));
179 static void nfs_rcvunlock
__P((int *flagp
));
180 static int nfs_receive
__P((struct nfsreq
*rep
, struct mbuf
**aname
,
182 static int nfs_reconnect
__P((struct nfsreq
*rep
));
184 static int nfsrv_getstream
__P((struct nfssvc_sock
*,int));
186 int (*nfsrv3_procs
[NFS_NPROCS
]) __P((struct nfsrv_descript
*nd
,
187 struct nfssvc_sock
*slp
,
189 struct mbuf
**mreqp
)) = {
217 #endif /* NFS_NOSERVER */
220 * NFSTRACE points were changed to FSDBG (KERNEL_DEBUG)
221 * But some of this code may prove useful someday...
225 int nfstraceindx
= 0;
226 struct nfstracerec nfstracebuf
[NFSTBUFSIZ
] = {{0,0,0,0}};
228 #define NFSTRACESUSPENDERS
229 #ifdef NFSTRACESUSPENDERS
230 uint nfstracemask
= 0xfff00200;
231 int nfstracexid
= -1;
232 uint onfstracemask
= 0;
233 int nfstracesuspend
= -1;
234 #define NFSTRACE_SUSPEND \
236 if (nfstracemask) { \
237 onfstracemask = nfstracemask; \
241 #define NFSTRACE_RESUME \
243 nfstracesuspend = -1; \
245 nfstracemask = onfstracemask; \
247 #define NFSTRACE_STARTSUSPENDCOUNTDOWN \
249 nfstracesuspend = (nfstraceindx+100) % NFSTBUFSIZ; \
251 #define NFSTRACE_SUSPENDING (nfstracesuspend != -1)
252 #define NFSTRACE_SUSPENSEOVER \
253 (nfstracesuspend > 100 ? \
254 (nfstraceindx >= nfstracesuspend || \
255 nfstraceindx < nfstracesuspend - 100) : \
256 (nfstraceindx >= nfstracesuspend && \
257 nfstraceindx < nfstracesuspend + 8192 - 100))
259 uint nfstracemask
= 0;
260 #endif /* NFSTRACESUSPENDERS */
265 int nfsoprocnum
, nfsolen
;
266 int nfsbt
[32], nfsbtlen
;
270 backtrace(int *where
, int size
)
272 int register sp
, *fp
, numsaved
;
274 __asm__
volatile("mr %0,r1" : "=r" (sp
));
276 fp
= (int *)*((int *)sp
);
278 for (numsaved
= 0; numsaved
< size
; numsaved
++) {
286 #elif defined(__i386__)
290 return (0); /* Till someone implements a real routine */
293 #error architecture not implemented.
297 nfsdup(struct nfsreq
*rep
)
299 int *ip
, i
, first
= 1, end
;
303 if ((nfs_debug
& NFS_DEBUG_DUP
) == 0)
305 /* last mbuf in chain will be nfs content */
306 for (mb
= rep
->r_mreq
; mb
->m_next
; mb
= mb
->m_next
)
308 if (rep
->r_procnum
== nfsoprocnum
&& mb
->m_len
== nfsolen
&&
309 !bcmp((caddr_t
)nfsodata
, mb
->m_data
, nfsolen
)) {
310 s
= b
+ sprintf(b
, "nfsdup x=%x p=%d h=", rep
->r_xid
,
312 end
= (int)(VTONFS(rep
->r_vp
)->n_fhp
);
313 ip
= (int *)(end
& ~3);
314 end
+= VTONFS(rep
->r_vp
)->n_fhsize
;
315 while ((int)ip
< end
) {
317 if (first
) { /* avoid leading zeroes */
321 s
+= sprintf(s
, "%x", i
);
323 s
+= sprintf(s
, "%08x", i
);
327 else /* eliminate trailing zeroes */
331 * set a breakpoint here and you can view the
332 * current backtrace and the one saved in nfsbt
336 nfsoprocnum
= rep
->r_procnum
;
338 bcopy(mb
->m_data
, (caddr_t
)nfsodata
, mb
->m_len
);
339 nfsbtlen
= backtrace(&nfsbt
, sizeof(nfsbt
));
344 * Initialize sockets and congestion for a new NFS connection.
345 * We do not free the sockaddr if error.
348 nfs_connect(nmp
, rep
)
349 register struct nfsmount
*nmp
;
352 register struct socket
*so
;
353 int s
, error
, rcvreserve
, sndreserve
;
354 struct sockaddr
*saddr
;
355 struct sockaddr_in sin
;
358 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
359 nmp
->nm_so
= (struct socket
*)0;
360 saddr
= mtod(nmp
->nm_nam
, struct sockaddr
*);
361 error
= socreate(saddr
->sa_family
, &nmp
->nm_so
, nmp
->nm_sotype
,
367 nmp
->nm_soflags
= so
->so_proto
->pr_flags
;
370 * Some servers require that the client port be a reserved port number.
372 if (saddr
->sa_family
== AF_INET
&& (nmp
->nm_flag
& NFSMNT_RESVPORT
)) {
373 sin
.sin_len
= sizeof (struct sockaddr_in
);
374 sin
.sin_family
= AF_INET
;
375 sin
.sin_addr
.s_addr
= INADDR_ANY
;
376 tport
= IPPORT_RESERVED
- 1;
377 sin
.sin_port
= htons(tport
);
379 while ((error
= sobind(so
, (struct sockaddr
*) &sin
) == EADDRINUSE
) &&
380 (--tport
> IPPORT_RESERVED
/ 2))
381 sin
.sin_port
= htons(tport
);
388 * Protocols that do not require connections may be optionally left
389 * unconnected for servers that reply from a port other than NFS_PORT.
391 if (nmp
->nm_flag
& NFSMNT_NOCONN
) {
392 if (nmp
->nm_soflags
& PR_CONNREQUIRED
) {
397 error
= soconnect(so
, mtod(nmp
->nm_nam
, struct sockaddr
*));
403 * Wait for the connection to complete. Cribbed from the
404 * connect system call but with the wait timing out so
405 * that interruptible mounts don't hang here for a long time.
408 while ((so
->so_state
& SS_ISCONNECTING
) && so
->so_error
== 0) {
409 (void) tsleep((caddr_t
)&so
->so_timeo
, PSOCK
,
411 if ((so
->so_state
& SS_ISCONNECTING
) &&
412 so
->so_error
== 0 && rep
&&
413 (error
= nfs_sigintr(nmp
, rep
, rep
->r_procp
))) {
414 so
->so_state
&= ~SS_ISCONNECTING
;
420 error
= so
->so_error
;
427 if (nmp
->nm_flag
& (NFSMNT_SOFT
| NFSMNT_INT
)) {
428 so
->so_rcv
.sb_timeo
= (5 * hz
);
429 so
->so_snd
.sb_timeo
= (5 * hz
);
431 so
->so_rcv
.sb_timeo
= 0;
432 so
->so_snd
.sb_timeo
= 0;
434 if (nmp
->nm_sotype
== SOCK_DGRAM
) {
435 sndreserve
= (nmp
->nm_wsize
+ NFS_MAXPKTHDR
) * 2;
436 rcvreserve
= (nmp
->nm_rsize
+ NFS_MAXPKTHDR
) * 2;
437 } else if (nmp
->nm_sotype
== SOCK_SEQPACKET
) {
438 sndreserve
= (nmp
->nm_wsize
+ NFS_MAXPKTHDR
) * 2;
439 rcvreserve
= (nmp
->nm_rsize
+ NFS_MAXPKTHDR
) * 2;
441 if (nmp
->nm_sotype
!= SOCK_STREAM
)
442 panic("nfscon sotype");
444 if (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) {
448 bzero(&sopt
, sizeof sopt
);
449 sopt
.sopt_level
= SOL_SOCKET
;
450 sopt
.sopt_name
= SO_KEEPALIVE
;
451 sopt
.sopt_val
= &val
;
452 sopt
.sopt_valsize
= sizeof val
;
456 if (so
->so_proto
->pr_protocol
== IPPROTO_TCP
) {
460 bzero(&sopt
, sizeof sopt
);
461 sopt
.sopt_level
= IPPROTO_TCP
;
462 sopt
.sopt_name
= TCP_NODELAY
;
463 sopt
.sopt_val
= &val
;
464 sopt
.sopt_valsize
= sizeof val
;
469 sndreserve
= (nmp
->nm_wsize
+ NFS_MAXPKTHDR
+ sizeof (u_long
))
471 rcvreserve
= (nmp
->nm_rsize
+ NFS_MAXPKTHDR
+ sizeof (u_long
))
475 error
= soreserve(so
, sndreserve
, rcvreserve
);
479 so
->so_rcv
.sb_flags
|= SB_NOINTR
;
480 so
->so_snd
.sb_flags
|= SB_NOINTR
;
482 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
484 /* Initialize other non-zero congestion variables */
485 nmp
->nm_srtt
[0] = nmp
->nm_srtt
[1] = nmp
->nm_srtt
[2] =
486 nmp
->nm_srtt
[3] = (NFS_TIMEO
<< 3);
487 nmp
->nm_sdrtt
[0] = nmp
->nm_sdrtt
[1] = nmp
->nm_sdrtt
[2] =
488 nmp
->nm_sdrtt
[3] = 0;
489 nmp
->nm_cwnd
= NFS_MAXCWND
/ 2; /* Initial send window */
491 FSDBG(529, nmp
, nmp
->nm_flag
, nmp
->nm_soflags
, nmp
->nm_cwnd
);
492 nmp
->nm_timeouts
= 0;
496 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
503 * Called when a connection is broken on a reliable protocol.
504 * - clean up the old socket
505 * - nfs_connect() again
506 * - set R_MUSTRESEND for all outstanding requests on mount point
507 * If this fails the mount point is DEAD!
508 * nb: Must be called with the nfs_sndlock() set on the mount point.
512 register struct nfsreq
*rep
;
514 register struct nfsreq
*rp
;
515 register struct nfsmount
*nmp
= rep
->r_nmp
;
519 while ((error
= nfs_connect(nmp
, rep
))) {
520 if (error
== EINTR
|| error
== ERESTART
)
522 (void) tsleep((caddr_t
)&lbolt
, PSOCK
, "nfscon", 0);
525 NFS_DPF(DUP
, ("nfs_reconnect RESEND\n"));
527 * Loop through outstanding request list and fix up all requests
530 for (rp
= nfs_reqq
.tqh_first
; rp
!= 0; rp
= rp
->r_chain
.tqe_next
) {
531 if (rp
->r_nmp
== nmp
)
532 rp
->r_flags
|= R_MUSTRESEND
;
538 * NFS disconnect. Clean up and unlink.
542 register struct nfsmount
*nmp
;
544 register struct socket
*so
;
546 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
549 nmp
->nm_so
= (struct socket
*)0;
553 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
557 * This is the nfs send routine. For connection based socket types, it
558 * must be called with an nfs_sndlock() on the socket.
559 * "rep == NULL" indicates that it has been called from a server.
560 * For the client side:
561 * - return EINTR if the RPC is terminated, 0 otherwise
562 * - set R_MUSTRESEND if the send fails for any reason
563 * - do any cleanup required by recoverable socket errors (???)
564 * For the server side:
565 * - return EINTR or ERESTART if interrupted by a signal
566 * - return EPIPE if a connection is lost for connection based sockets (TCP...)
567 * - do any cleanup required by recoverable socket errors (???)
570 nfs_send(so
, nam
, top
, rep
)
571 register struct socket
*so
;
573 register struct mbuf
*top
;
576 struct sockaddr
*sendnam
;
577 int error
, soflags
, flags
;
580 char savenametolog
[MNAMELEN
];
583 if (rep
->r_flags
& R_SOFTTERM
) {
587 if ((so
= rep
->r_nmp
->nm_so
) == NULL
) {
588 rep
->r_flags
|= R_MUSTRESEND
;
592 rep
->r_flags
&= ~R_MUSTRESEND
;
593 soflags
= rep
->r_nmp
->nm_soflags
;
594 for (rp
= nfs_reqq
.tqh_first
; rp
; rp
= rp
->r_chain
.tqe_next
)
598 xidqueued
= rp
->r_xid
;
600 soflags
= so
->so_proto
->pr_flags
;
601 if ((soflags
& PR_CONNREQUIRED
) || (so
->so_state
& SS_ISCONNECTED
) ||
603 sendnam
= (struct sockaddr
*)0;
605 sendnam
= mtod(nam
, struct sockaddr
*);
607 if (so
->so_type
== SOCK_SEQPACKET
)
617 * Save the name here in case mount point goes away when we switch
618 * funnels. The name is using local stack and is large, but don't
619 * want to block if we malloc.
622 strncpy(savenametolog
,
623 rep
->r_nmp
->nm_mountp
->mnt_stat
.f_mntfromname
,
625 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
626 error
= sosend(so
, sendnam
, (struct uio
*)0, top
,
627 (struct mbuf
*)0, flags
);
628 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
633 for (rp
= nfs_reqq
.tqh_first
; rp
;
634 rp
= rp
->r_chain
.tqe_next
)
635 if (rp
== rep
&& rp
->r_xid
== xidqueued
)
638 panic("nfs_send: error %d xid %x gone",
641 log(LOG_INFO
, "nfs send error %d for server %s\n",
642 error
, savenametolog
);
644 * Deal with errors for the client side.
646 if (rep
->r_flags
& R_SOFTTERM
)
649 rep
->r_flags
|= R_MUSTRESEND
;
651 ("nfs_send RESEND error=%d\n", error
));
654 log(LOG_INFO
, "nfsd send error %d\n", error
);
657 * Handle any recoverable (soft) socket errors here. (???)
659 if (error
!= EINTR
&& error
!= ERESTART
&&
660 error
!= EWOULDBLOCK
&& error
!= EPIPE
)
667 * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all
668 * done by soreceive(), but for SOCK_STREAM we must deal with the Record
669 * Mark and consolidate the data into a new mbuf list.
670 * nb: Sometimes TCP passes the data up to soreceive() in long lists of
672 * For SOCK_STREAM we must be very careful to read an entire record once
673 * we have read any of it, even if the system call has been interrupted.
676 nfs_receive(rep
, aname
, mp
)
677 register struct nfsreq
*rep
;
681 register struct socket
*so
;
684 register struct mbuf
*m
;
685 struct mbuf
*control
;
687 struct sockaddr
**getnam
;
688 struct sockaddr
*tmp_nam
;
690 struct sockaddr_in
*sin
;
691 int error
, sotype
, rcvflg
;
692 struct proc
*p
= current_proc(); /* XXX */
695 * Set up arguments for soreceive()
697 *mp
= (struct mbuf
*)0;
698 *aname
= (struct mbuf
*)0;
699 sotype
= rep
->r_nmp
->nm_sotype
;
702 * For reliable protocols, lock against other senders/receivers
703 * in case a reconnect is necessary.
704 * For SOCK_STREAM, first get the Record Mark to find out how much
705 * more there is to get.
706 * We must lock the socket against other receivers
707 * until we have an entire rpc request/reply.
709 if (sotype
!= SOCK_DGRAM
) {
710 error
= nfs_sndlock(&rep
->r_nmp
->nm_flag
, rep
);
715 * Check for fatal errors and resending request.
718 * Ugh: If a reconnect attempt just happened, nm_so
719 * would have changed. NULL indicates a failed
720 * attempt that has essentially shut down this
723 if (rep
->r_mrep
|| (rep
->r_flags
& R_SOFTTERM
)) {
724 nfs_sndunlock(&rep
->r_nmp
->nm_flag
);
727 so
= rep
->r_nmp
->nm_so
;
729 error
= nfs_reconnect(rep
);
731 nfs_sndunlock(&rep
->r_nmp
->nm_flag
);
736 while (rep
->r_flags
& R_MUSTRESEND
) {
737 m
= m_copym(rep
->r_mreq
, 0, M_COPYALL
, M_WAIT
);
738 nfsstats
.rpcretries
++;
740 ("nfs_receive RESEND %s\n",
741 rep
->r_nmp
->nm_mountp
->mnt_stat
.f_mntfromname
));
742 error
= nfs_send(so
, rep
->r_nmp
->nm_nam
, m
, rep
);
744 * we also hold rcv lock so rep is still
748 if (error
== EINTR
|| error
== ERESTART
||
749 (error
= nfs_reconnect(rep
))) {
750 nfs_sndunlock(&rep
->r_nmp
->nm_flag
);
756 nfs_sndunlock(&rep
->r_nmp
->nm_flag
);
757 if (sotype
== SOCK_STREAM
) {
758 aio
.iov_base
= (caddr_t
) &len
;
759 aio
.iov_len
= sizeof(u_long
);
762 auio
.uio_segflg
= UIO_SYSSPACE
;
763 auio
.uio_rw
= UIO_READ
;
765 auio
.uio_resid
= sizeof(u_long
);
768 rcvflg
= MSG_WAITALL
;
769 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
770 error
= soreceive(so
, (struct sockaddr
**)0, &auio
,
771 (struct mbuf
**)0, (struct mbuf
**)0, &rcvflg
);
772 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
773 if (!rep
->r_nmp
) /* if unmounted then bailout */
775 if (error
== EWOULDBLOCK
&& rep
) {
776 if (rep
->r_flags
& R_SOFTTERM
)
779 } while (error
== EWOULDBLOCK
);
780 if (!error
&& auio
.uio_resid
> 0) {
782 "short receive (%d/%d) from nfs server %s\n",
783 sizeof(u_long
) - auio
.uio_resid
,
785 rep
->r_nmp
->nm_mountp
->mnt_stat
.f_mntfromname
);
790 len
= ntohl(len
) & ~0x80000000;
792 * This is SERIOUS! We are out of sync with the sender
793 * and forcing a disconnect/reconnect is all I can do.
795 if (len
> NFS_MAXPACKET
) {
796 log(LOG_ERR
, "%s (%d) from nfs server %s\n",
797 "impossible packet length",
799 rep
->r_nmp
->nm_mountp
->mnt_stat
.f_mntfromname
);
803 auio
.uio_resid
= len
;
805 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
807 rcvflg
= MSG_WAITALL
;
808 error
= soreceive(so
, (struct sockaddr
**)0,
809 &auio
, mp
, (struct mbuf
**)0, &rcvflg
);
810 if (!rep
->r_nmp
) /* if unmounted then bailout */ {
811 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
814 } while (error
== EWOULDBLOCK
|| error
== EINTR
||
817 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
819 if (!error
&& auio
.uio_resid
> 0) {
821 "short receive (%d/%d) from nfs server %s\n",
822 len
- auio
.uio_resid
, len
,
823 rep
->r_nmp
->nm_mountp
->mnt_stat
.f_mntfromname
);
828 * NB: Since uio_resid is big, MSG_WAITALL is ignored
829 * and soreceive() will return when it has either a
830 * control msg or a data msg.
831 * We have no use for control msg., but must grab them
832 * and then throw them away so we know what is going
835 auio
.uio_resid
= len
= 100000000; /* Anything Big */
838 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
841 error
= soreceive(so
, (struct sockaddr
**)0,
842 &auio
, mp
, &control
, &rcvflg
);
843 if (!rep
->r_nmp
) /* if unmounted then bailout */ {
844 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
849 if (error
== EWOULDBLOCK
&& rep
) {
850 if (rep
->r_flags
& R_SOFTTERM
) {
851 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
855 } while (error
== EWOULDBLOCK
||
856 (!error
&& *mp
== NULL
&& control
));
858 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
860 if ((rcvflg
& MSG_EOR
) == 0)
862 if (!error
&& *mp
== NULL
)
864 len
-= auio
.uio_resid
;
867 if (error
&& error
!= EINTR
&& error
!= ERESTART
) {
869 *mp
= (struct mbuf
*)0;
872 "receive error %d from nfs server %s\n",
874 rep
->r_nmp
->nm_mountp
->mnt_stat
.f_mntfromname
);
875 error
= nfs_sndlock(&rep
->r_nmp
->nm_flag
, rep
);
877 error
= nfs_reconnect(rep
);
882 if ((so
= rep
->r_nmp
->nm_so
) == NULL
)
884 if (so
->so_state
& SS_ISCONNECTED
)
885 getnam
= (struct sockaddr
**)0;
888 auio
.uio_resid
= len
= 1000000;
891 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
894 error
= soreceive(so
, getnam
, &auio
, mp
,
895 (struct mbuf
**)0, &rcvflg
);
897 if ((getnam
) && (*getnam
)) {
898 MGET(mhck
, M_WAIT
, MT_SONAME
);
899 mhck
->m_len
= (*getnam
)->sa_len
;
900 sin
= mtod(mhck
, struct sockaddr_in
*);
901 bcopy(*getnam
, sin
, sizeof(struct sockaddr_in
));
902 mhck
->m_hdr
.mh_len
= sizeof(struct sockaddr_in
);
903 FREE(*getnam
, M_SONAME
);
906 if (!rep
->r_nmp
) /* if unmounted then bailout */ {
907 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
911 if (error
== EWOULDBLOCK
&&
912 (rep
->r_flags
& R_SOFTTERM
)) {
913 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
916 } while (error
== EWOULDBLOCK
);
918 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
919 len
-= auio
.uio_resid
;
924 *mp
= (struct mbuf
*)0;
930 * Implement receipt of reply on a socket.
931 * We must search through the list of received datagrams matching them
932 * with outstanding requests using the xid, until ours is found.
937 struct nfsreq
*myrep
;
939 register struct nfsreq
*rep
;
940 register struct nfsmount
*nmp
= myrep
->r_nmp
;
942 struct mbuf
*mrep
, *md
;
949 * Loop around until we get our own reply
953 * Lock against other receivers so that I don't get stuck in
954 * sbwait() after someone else has received my reply for me.
955 * Also necessary for connection based protocols to avoid
956 * race conditions during a reconnect.
957 * If nfs_rcvlock() returns EALREADY, that means that
958 * the reply has already been recieved by another
959 * process and we can return immediately. In this
960 * case, the lock is not taken to avoid races with
963 error
= nfs_rcvlock(myrep
);
964 if (error
== EALREADY
)
970 * If we slept after putting bits otw, then reply may have
971 * arrived. In which case returning is required, or we
972 * would hang trying to nfs_receive an already received reply.
974 if (myrep
->r_mrep
!= NULL
) {
975 nfs_rcvunlock(&nmp
->nm_flag
);
976 FSDBG(530, myrep
->r_xid
, myrep
, myrep
->r_nmp
, -1);
980 * Get the next Rpc reply off the socket. Assume myrep->r_nmp
981 * is still intact by checks done in nfs_rcvlock.
983 error
= nfs_receive(myrep
, &nam
, &mrep
);
985 * Bailout asap if nfsmount struct gone (unmounted).
988 FSDBG(530, myrep
->r_xid
, myrep
, nmp
, -2);
989 return (ECONNABORTED
);
992 FSDBG(530, myrep
->r_xid
, myrep
, nmp
, error
);
993 nfs_rcvunlock(&nmp
->nm_flag
);
996 * Ignore routing errors on connectionless protocols??
998 if (NFSIGNORE_SOERROR(nmp
->nm_soflags
, error
)) {
999 nmp
->nm_so
->so_error
= 0;
1000 if (myrep
->r_flags
& R_GETONEREP
)
1010 * We assume all is fine, but if we did not have an error
1011 * and mrep is 0, better not dereference it. nfs_receieve
1012 * calls soreceive which carefully sets error=0 when it got
1013 * errors on sbwait (tsleep). In most cases, I assume that's
1014 * so we could go back again. In tcp case, EPIPE is returned.
1015 * In udp, case nfs_receive gets back here with no error and no
1016 * mrep. Is the right fix to have soreceive check for process
1017 * aborted after sbwait and return something non-zero? Should
1018 * nfs_receive give an EPIPE? Too risky to play with those
1019 * two this late in game for a shutdown problem. Instead,
1020 * just check here and get out. (ekn)
1023 FSDBG(530, myrep
->r_xid
, myrep
, nmp
, -3);
1024 return (ECONNABORTED
); /* sounds good */
1028 * Get the xid and check that it is an rpc reply
1031 dpos
= mtod(md
, caddr_t
);
1032 nfsm_dissect(tl
, u_long
*, 2*NFSX_UNSIGNED
);
1034 if (*tl
!= rpc_reply
) {
1035 #ifndef NFS_NOSERVER
1036 if (nmp
->nm_flag
& NFSMNT_NQNFS
) {
1037 if (nqnfs_callback(nmp
, mrep
, md
, dpos
))
1038 nfsstats
.rpcinvalid
++;
1040 nfsstats
.rpcinvalid
++;
1044 nfsstats
.rpcinvalid
++;
1048 if (nmp
->nm_flag
& NFSMNT_RCVLOCK
)
1049 nfs_rcvunlock(&nmp
->nm_flag
);
1050 if (myrep
->r_flags
& R_GETONEREP
)
1051 return (0); /* this path used by NQNFS */
1056 * Loop through the request list to match up the reply
1057 * Iff no match, just drop the datagram
1059 for (rep
= nfs_reqq
.tqh_first
; rep
!= 0;
1060 rep
= rep
->r_chain
.tqe_next
) {
1061 if (rep
->r_mrep
== NULL
&& rxid
== rep
->r_xid
) {
1069 rt
= &nfsrtt
.rttl
[nfsrtt
.pos
];
1070 rt
->proc
= rep
->r_procnum
;
1071 rt
->rto
= NFS_RTO(nmp
, proct
[rep
->r_procnum
]);
1072 rt
->sent
= nmp
->nm_sent
;
1073 rt
->cwnd
= nmp
->nm_cwnd
;
1074 if (proct
[rep
->r_procnum
] == 0)
1075 panic("nfs_reply: proct[%d] is zero", rep
->r_procnum
);
1076 rt
->srtt
= nmp
->nm_srtt
[proct
[rep
->r_procnum
] - 1];
1077 rt
->sdrtt
= nmp
->nm_sdrtt
[proct
[rep
->r_procnum
] - 1];
1078 rt
->fsid
= nmp
->nm_mountp
->mnt_stat
.f_fsid
;
1080 if (rep
->r_flags
& R_TIMING
)
1081 rt
->rtt
= rep
->r_rtt
;
1084 nfsrtt
.pos
= (nfsrtt
.pos
+ 1) % NFSRTTLOGSIZ
;
1087 * Update congestion window.
1088 * Do the additive increase of
1091 FSDBG(530, rep
->r_xid
, rep
, nmp
->nm_sent
,
1093 if (nmp
->nm_cwnd
<= nmp
->nm_sent
) {
1095 (NFS_CWNDSCALE
* NFS_CWNDSCALE
+
1096 (nmp
->nm_cwnd
>> 1)) / nmp
->nm_cwnd
;
1097 if (nmp
->nm_cwnd
> NFS_MAXCWND
)
1098 nmp
->nm_cwnd
= NFS_MAXCWND
;
1100 if (!(rep
->r_flags
& R_SENT
))
1101 printf("nfs_reply: unsent xid=%x",
1103 rep
->r_flags
&= ~R_SENT
;
1104 nmp
->nm_sent
-= NFS_CWNDSCALE
;
1106 * Update rtt using a gain of 0.125 on the mean
1107 * and a gain of 0.25 on the deviation.
1109 if (rep
->r_flags
& R_TIMING
) {
1111 * Since the timer resolution of
1112 * NFS_HZ is so course, it can often
1113 * result in r_rtt == 0. Since
1114 * r_rtt == N means that the actual
1115 * rtt is between N+dt and N+2-dt ticks,
1118 if (proct
[rep
->r_procnum
] == 0)
1119 panic("nfs_reply: proct[%d] is zero", rep
->r_procnum
);
1120 t1
= rep
->r_rtt
+ 1;
1121 t1
-= (NFS_SRTT(rep
) >> 3);
1122 NFS_SRTT(rep
) += t1
;
1125 t1
-= (NFS_SDRTT(rep
) >> 2);
1126 NFS_SDRTT(rep
) += t1
;
1128 nmp
->nm_timeouts
= 0;
1132 nfs_rcvunlock(&nmp
->nm_flag
);
1134 * If not matched to a request, drop it.
1135 * If it's mine, get out.
1138 nfsstats
.rpcunexpected
++;
1140 } else if (rep
== myrep
) {
1141 if (rep
->r_mrep
== NULL
)
1142 panic("nfs_reply: nil r_mrep");
1145 FSDBG(530, myrep
->r_xid
, myrep
, rep
,
1146 rep
? rep
->r_xid
: myrep
->r_flags
);
1147 if (myrep
->r_flags
& R_GETONEREP
)
1148 return (0); /* this path used by NQNFS */
1153 * nfs_request - goes something like this
1154 * - fill in request struct
1155 * - links it into list
1156 * - calls nfs_send() for first transmit
1157 * - calls nfs_receive() to get reply
1158 * - break down rpc header and return with nfs reply pointed to
1160 * nb: always frees up mreq mbuf list
1163 nfs_request(vp
, mrest
, procnum
, procp
, cred
, mrp
, mdp
, dposp
, xidp
)
1174 register struct mbuf
*m
, *mrep
;
1175 register struct nfsreq
*rep
, *rp
;
1176 register u_long
*tl
;
1178 struct nfsmount
*nmp
;
1179 struct mbuf
*md
, *mheadend
;
1181 char nickv
[RPCX_NICKVERF
];
1182 time_t reqtime
, waituntil
;
1184 int t1
, nqlflag
, cachable
, s
, error
= 0, mrest_len
, auth_len
, auth_type
;
1185 int trylater_delay
= NQ_TRYLATERDEL
, trylater_cnt
= 0, failed_auth
= 0;
1186 int verf_len
, verf_type
;
1189 char *auth_str
, *verf_str
;
1190 NFSKERBKEY_T key
; /* save session key */
1194 nmp
= VFSTONFS(vp
->v_mount
);
1195 MALLOC_ZONE(rep
, struct nfsreq
*,
1196 sizeof(struct nfsreq
), M_NFSREQ
, M_WAITOK
);
1197 FSDBG_TOP(531, vp
, procnum
, nmp
, rep
);
1200 * make sure if we blocked above, that the file system didn't get
1201 * unmounted leaving nmp bogus value to trip on later and crash.
1202 * Note nfs_unmount will set rep->r_nmp if unmounted volume, but we
1203 * aren't that far yet. SO this is best we can do. I wanted to check
1204 * for vp->v_mount = 0 also below, but that caused reboot crash.
1205 * Something must think it's okay for vp-v_mount=0 during booting.
1206 * Thus the best I can do here is see if we still have a vnode.
1209 if (vp
->v_type
== VBAD
) {
1210 FSDBG_BOT(531, 1, vp
, nmp
, rep
);
1211 _FREE_ZONE((caddr_t
)rep
, sizeof (struct nfsreq
), M_NFSREQ
);
1216 rep
->r_procp
= procp
;
1217 rep
->r_procnum
= procnum
;
1227 * Get the RPC header with authorization.
1230 verf_str
= auth_str
= (char *)0;
1231 if (nmp
->nm_flag
& NFSMNT_KERB
) {
1233 verf_len
= sizeof (nickv
);
1234 auth_type
= RPCAUTH_KERB4
;
1235 bzero((caddr_t
)key
, sizeof (key
));
1236 if (failed_auth
|| nfs_getnickauth(nmp
, cred
, &auth_str
,
1237 &auth_len
, verf_str
, verf_len
)) {
1238 error
= nfs_getauth(nmp
, rep
, cred
, &auth_str
,
1239 &auth_len
, verf_str
, &verf_len
, key
);
1241 FSDBG_BOT(531, 2, vp
, error
, rep
);
1242 _FREE_ZONE((caddr_t
)rep
,
1243 sizeof (struct nfsreq
), M_NFSREQ
);
1249 auth_type
= RPCAUTH_UNIX
;
1250 if (cred
->cr_ngroups
< 1)
1251 panic("nfsreq nogrps");
1252 auth_len
= ((((cred
->cr_ngroups
- 1) > nmp
->nm_numgrps
) ?
1253 nmp
->nm_numgrps
: (cred
->cr_ngroups
- 1)) << 2) +
1256 m
= nfsm_rpchead(cred
, nmp
->nm_flag
, procnum
, auth_type
, auth_len
,
1257 auth_str
, verf_len
, verf_str
, mrest
, mrest_len
, &mheadend
, &xid
);
1259 *xidp
= xid
+ ((u_int64_t
)nfs_xidwrap
<< 32);
1261 _FREE(auth_str
, M_TEMP
);
1264 * For stream protocols, insert a Sun RPC Record Mark.
1266 if (nmp
->nm_sotype
== SOCK_STREAM
) {
1267 M_PREPEND(m
, NFSX_UNSIGNED
, M_WAIT
);
1268 *mtod(m
, u_long
*) = htonl(0x80000000 |
1269 (m
->m_pkthdr
.len
- NFSX_UNSIGNED
));
1274 if (nmp
->nm_flag
& NFSMNT_SOFT
)
1275 rep
->r_retry
= nmp
->nm_retry
;
1277 rep
->r_retry
= NFS_MAXREXMIT
+ 1; /* past clip limit */
1278 rep
->r_rtt
= rep
->r_rexmit
= 0;
1279 if (proct
[procnum
] > 0)
1280 rep
->r_flags
= R_TIMING
;
1286 * Do the client side RPC.
1288 nfsstats
.rpcrequests
++;
1290 * Chain request into list of outstanding requests. Be sure
1291 * to put it LAST so timer finds oldest requests first.
1294 TAILQ_INSERT_TAIL(&nfs_reqq
, rep
, r_chain
);
1296 /* Get send time for nqnfs */
1297 reqtime
= time
.tv_sec
;
1300 * If backing off another request or avoiding congestion, don't
1301 * send this one now but let timer do it. If not timing a request,
1304 if (nmp
->nm_so
&& (nmp
->nm_sotype
!= SOCK_DGRAM
||
1305 (nmp
->nm_flag
& NFSMNT_DUMBTIMR
) ||
1306 nmp
->nm_sent
< nmp
->nm_cwnd
)) {
1308 if (nmp
->nm_soflags
& PR_CONNREQUIRED
)
1309 error
= nfs_sndlock(&nmp
->nm_flag
, rep
);
1312 * Set the R_SENT before doing the send in case another thread
1313 * processes the reply before the nfs_send returns here
1316 if ((rep
->r_flags
& R_MUSTRESEND
) == 0) {
1317 FSDBG(531, rep
->r_xid
, rep
, nmp
->nm_sent
,
1319 nmp
->nm_sent
+= NFS_CWNDSCALE
;
1320 rep
->r_flags
|= R_SENT
;
1323 m
= m_copym(m
, 0, M_COPYALL
, M_WAIT
);
1324 error
= nfs_send(nmp
->nm_so
, nmp
->nm_nam
, m
, rep
);
1325 if (nmp
->nm_soflags
& PR_CONNREQUIRED
)
1326 nfs_sndunlock(&nmp
->nm_flag
);
1329 nmp
->nm_sent
-= NFS_CWNDSCALE
;
1330 rep
->r_flags
&= ~R_SENT
;
1338 * Wait for the reply from our send or the timer's.
1340 if (!error
|| error
== EPIPE
)
1341 error
= nfs_reply(rep
);
1344 * RPC done, unlink the request.
1347 for (rp
= nfs_reqq
.tqh_first
; rp
;
1348 rp
= rp
->r_chain
.tqe_next
)
1349 if (rp
== rep
&& rp
->r_xid
== xid
)
1352 panic("nfs_request race, rep %x xid %x", rep
, xid
);
1353 TAILQ_REMOVE(&nfs_reqq
, rep
, r_chain
);
1357 * Decrement the outstanding request count.
1359 if (rep
->r_flags
& R_SENT
) {
1360 FSDBG(531, rep
->r_xid
, rep
, nmp
->nm_sent
, nmp
->nm_cwnd
);
1361 rep
->r_flags
&= ~R_SENT
; /* paranoia */
1362 nmp
->nm_sent
-= NFS_CWNDSCALE
;
1366 * If there was a successful reply and a tprintf msg.
1367 * tprintf a response.
1369 if (!error
&& (rep
->r_flags
& R_TPRINTFMSG
))
1370 nfs_msg(rep
->r_procp
, nmp
->nm_mountp
->mnt_stat
.f_mntfromname
,
1376 m_freem(rep
->r_mreq
);
1377 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1378 _FREE_ZONE((caddr_t
)rep
, sizeof (struct nfsreq
), M_NFSREQ
);
1383 * break down the rpc header and check if ok
1385 nfsm_dissect(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
1386 if (*tl
++ == rpc_msgdenied
) {
1387 if (*tl
== rpc_mismatch
)
1389 else if ((nmp
->nm_flag
& NFSMNT_KERB
) && *tl
++ == rpc_autherr
) {
1392 mheadend
->m_next
= (struct mbuf
*)0;
1394 m_freem(rep
->r_mreq
);
1401 m_freem(rep
->r_mreq
);
1402 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1403 _FREE_ZONE((caddr_t
)rep
, sizeof (struct nfsreq
), M_NFSREQ
);
1408 * Grab any Kerberos verifier, otherwise just throw it away.
1410 verf_type
= fxdr_unsigned(int, *tl
++);
1411 i
= fxdr_unsigned(int, *tl
);
1412 if ((nmp
->nm_flag
& NFSMNT_KERB
) && verf_type
== RPCAUTH_KERB4
) {
1413 error
= nfs_savenickauth(nmp
, cred
, i
, key
, &md
, &dpos
, mrep
);
1417 nfsm_adv(nfsm_rndup(i
));
1418 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
1421 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
1423 error
= fxdr_unsigned(int, *tl
);
1424 if ((nmp
->nm_flag
& NFSMNT_NFSV3
) &&
1425 error
== NFSERR_TRYLATER
) {
1428 waituntil
= time
.tv_sec
+ trylater_delay
;
1430 ("nfs_request %s flag=%x trylater_cnt=%x waituntil=%lx trylater_delay=%x\n",
1431 nmp
->nm_mountp
->mnt_stat
.f_mntfromname
,
1432 nmp
->nm_flag
, trylater_cnt
, waituntil
,
1434 while (time
.tv_sec
< waituntil
)
1435 (void)tsleep((caddr_t
)&lbolt
,
1436 PSOCK
, "nqnfstry", 0);
1437 trylater_delay
*= nfs_backoff
[trylater_cnt
];
1438 if (trylater_cnt
< 7)
1444 * If the File Handle was stale, invalidate the
1445 * lookup cache, just in case.
1447 if (error
== ESTALE
)
1449 if (nmp
->nm_flag
& NFSMNT_NFSV3
) {
1453 error
|= NFSERR_RETERR
;
1456 m_freem(rep
->r_mreq
);
1457 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1458 _FREE_ZONE((caddr_t
)rep
,
1459 sizeof (struct nfsreq
), M_NFSREQ
);
1464 * For nqnfs, get any lease in reply
1466 if (nmp
->nm_flag
& NFSMNT_NQNFS
) {
1467 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
1470 nqlflag
= fxdr_unsigned(int, *tl
);
1471 nfsm_dissect(tl
, u_long
*, 4*NFSX_UNSIGNED
);
1472 cachable
= fxdr_unsigned(int, *tl
++);
1473 reqtime
+= fxdr_unsigned(int, *tl
++);
1474 if (reqtime
> time
.tv_sec
) {
1475 fxdr_hyper(tl
, &frev
);
1476 nqnfs_clientlease(nmp
, np
, nqlflag
,
1477 cachable
, reqtime
, frev
);
1484 m_freem(rep
->r_mreq
);
1485 FSDBG_BOT(531, 0xf0f0f0f0, rep
->r_xid
, nmp
, rep
);
1486 FREE_ZONE((caddr_t
)rep
, sizeof (struct nfsreq
), M_NFSREQ
);
1490 error
= EPROTONOSUPPORT
;
1492 m_freem(rep
->r_mreq
);
1493 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1494 _FREE_ZONE((caddr_t
)rep
, sizeof (struct nfsreq
), M_NFSREQ
);
1498 #ifndef NFS_NOSERVER
1500 * Generate the rpc reply header
1501 * siz arg. is used to decide if adding a cluster is worthwhile
1504 nfs_rephead(siz
, nd
, slp
, err
, cache
, frev
, mrq
, mbp
, bposp
)
1506 struct nfsrv_descript
*nd
;
1507 struct nfssvc_sock
*slp
;
1515 register u_long
*tl
;
1516 register struct mbuf
*mreq
;
1518 struct mbuf
*mb
, *mb2
;
1520 MGETHDR(mreq
, M_WAIT
, MT_DATA
);
1523 * If this is a big reply, use a cluster else
1524 * try and leave leading space for the lower level headers.
1526 siz
+= RPC_REPLYSIZ
;
1527 if (siz
>= MINCLSIZE
) {
1528 MCLGET(mreq
, M_WAIT
);
1530 mreq
->m_data
+= max_hdr
;
1531 tl
= mtod(mreq
, u_long
*);
1532 mreq
->m_len
= 6 * NFSX_UNSIGNED
;
1533 bpos
= ((caddr_t
)tl
) + mreq
->m_len
;
1534 *tl
++ = txdr_unsigned(nd
->nd_retxid
);
1536 if (err
== ERPCMISMATCH
|| (err
& NFSERR_AUTHERR
)) {
1537 *tl
++ = rpc_msgdenied
;
1538 if (err
& NFSERR_AUTHERR
) {
1539 *tl
++ = rpc_autherr
;
1540 *tl
= txdr_unsigned(err
& ~NFSERR_AUTHERR
);
1541 mreq
->m_len
-= NFSX_UNSIGNED
;
1542 bpos
-= NFSX_UNSIGNED
;
1544 *tl
++ = rpc_mismatch
;
1545 *tl
++ = txdr_unsigned(RPC_VER2
);
1546 *tl
= txdr_unsigned(RPC_VER2
);
1549 *tl
++ = rpc_msgaccepted
;
1552 * For Kerberos authentication, we must send the nickname
1553 * verifier back, otherwise just RPCAUTH_NULL.
1555 if (nd
->nd_flag
& ND_KERBFULL
) {
1556 register struct nfsuid
*nuidp
;
1557 struct timeval ktvin
, ktvout
;
1559 for (nuidp
= NUIDHASH(slp
, nd
->nd_cr
.cr_uid
)->lh_first
;
1560 nuidp
!= 0; nuidp
= nuidp
->nu_hash
.le_next
) {
1561 if (nuidp
->nu_cr
.cr_uid
== nd
->nd_cr
.cr_uid
&&
1562 (!nd
->nd_nam2
|| netaddr_match(NU_NETFAM(nuidp
),
1563 &nuidp
->nu_haddr
, nd
->nd_nam2
)))
1568 txdr_unsigned(nuidp
->nu_timestamp
.tv_sec
- 1);
1570 txdr_unsigned(nuidp
->nu_timestamp
.tv_usec
);
1573 * Encrypt the timestamp in ecb mode using the
1580 *tl
++ = rpc_auth_kerb
;
1581 *tl
++ = txdr_unsigned(3 * NFSX_UNSIGNED
);
1582 *tl
= ktvout
.tv_sec
;
1583 nfsm_build(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
1584 *tl
++ = ktvout
.tv_usec
;
1585 *tl
++ = txdr_unsigned(nuidp
->nu_cr
.cr_uid
);
1596 *tl
= txdr_unsigned(RPC_PROGUNAVAIL
);
1599 *tl
= txdr_unsigned(RPC_PROGMISMATCH
);
1600 nfsm_build(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
1601 if (nd
->nd_flag
& ND_NQNFS
) {
1602 *tl
++ = txdr_unsigned(3);
1603 *tl
= txdr_unsigned(3);
1605 *tl
++ = txdr_unsigned(2);
1606 *tl
= txdr_unsigned(3);
1610 *tl
= txdr_unsigned(RPC_PROCUNAVAIL
);
1613 *tl
= txdr_unsigned(RPC_GARBAGE
);
1617 if (err
!= NFSERR_RETVOID
) {
1618 nfsm_build(tl
, u_long
*, NFSX_UNSIGNED
);
1620 *tl
= txdr_unsigned(nfsrv_errmap(nd
, err
));
1629 * For nqnfs, piggyback lease as requested.
1631 if ((nd
->nd_flag
& ND_NQNFS
) && err
== 0) {
1632 if (nd
->nd_flag
& ND_LEASE
) {
1633 nfsm_build(tl
, u_long
*, 5 * NFSX_UNSIGNED
);
1634 *tl
++ = txdr_unsigned(nd
->nd_flag
& ND_LEASE
);
1635 *tl
++ = txdr_unsigned(cache
);
1636 *tl
++ = txdr_unsigned(nd
->nd_duration
);
1637 txdr_hyper(frev
, tl
);
1639 nfsm_build(tl
, u_long
*, NFSX_UNSIGNED
);
1647 if (err
!= 0 && err
!= NFSERR_RETVOID
)
1648 nfsstats
.srvrpc_errs
++;
1653 #endif /* NFS_NOSERVER */
1657 * From FreeBSD 1.58, a Matt Dillon fix...
1658 * Flag a request as being about to terminate.
1659 * The nm_sent count is decremented now to avoid deadlocks when the process
1660 * in soreceive() hasn't yet managed to send its own request.
1663 nfs_softterm(struct nfsreq
*rep
)
1665 rep
->r_flags
|= R_SOFTTERM
;
1666 if (rep
->r_flags
& R_SENT
) {
1667 FSDBG(532, rep
->r_xid
, rep
, rep
->r_nmp
->nm_sent
,
1668 rep
->r_nmp
->nm_cwnd
);
1669 rep
->r_nmp
->nm_sent
-= NFS_CWNDSCALE
;
1670 rep
->r_flags
&= ~R_SENT
;
1675 nfs_timer_funnel(arg
)
1678 (void) thread_funnel_set(kernel_flock
, TRUE
);
1680 (void) thread_funnel_set(kernel_flock
, FALSE
);
1686 * Scan the nfsreq list and retranmit any requests that have timed out
1687 * To avoid retransmission attempts on STREAM sockets (in the future) make
1688 * sure to set the r_retry field to 0 (implies nm_retry == 0).
1692 void *arg
; /* never used */
1694 register struct nfsreq
*rep
, *rp
;
1695 register struct mbuf
*m
;
1696 register struct socket
*so
;
1697 register struct nfsmount
*nmp
;
1700 #ifndef NFS_NOSERVER
1701 static long lasttime
= 0;
1702 register struct nfssvc_sock
*slp
;
1704 #endif /* NFS_NOSERVER */
1708 int flags
, rexmit
, cwnd
, sent
;
1713 * XXX If preemptable threads are implemented the spls used for the
1714 * outstanding request queue must be replaced with mutexes.
1717 #ifdef NFSTRACESUSPENDERS
1718 if (NFSTRACE_SUSPENDING
) {
1719 for (rep
= nfs_reqq
.tqh_first
; rep
!= 0;
1720 rep
= rep
->r_chain
.tqe_next
)
1721 if (rep
->r_xid
== nfstracexid
)
1725 } else if (NFSTRACE_SUSPENSEOVER
) {
1730 for (rep
= nfs_reqq
.tqh_first
; rep
!= 0; rep
= rep
->r_chain
.tqe_next
) {
1731 #ifdef NFSTRACESUSPENDERS
1732 if (rep
->r_mrep
&& !NFSTRACE_SUSPENDING
) {
1733 nfstracexid
= rep
->r_xid
;
1734 NFSTRACE_STARTSUSPENDCOUNTDOWN
;
1738 if (!nmp
) /* unmounted */
1740 if (rep
->r_mrep
|| (rep
->r_flags
& R_SOFTTERM
))
1742 if (nfs_sigintr(nmp
, rep
, rep
->r_procp
)) {
1746 if (rep
->r_rtt
>= 0) {
1748 if (nmp
->nm_flag
& NFSMNT_DUMBTIMR
)
1749 timeo
= nmp
->nm_timeo
;
1751 timeo
= NFS_RTO(nmp
, proct
[rep
->r_procnum
]);
1752 /* ensure 62.5 ms floor */
1753 while (16 * timeo
< hz
)
1755 if (nmp
->nm_timeouts
> 0)
1756 timeo
*= nfs_backoff
[nmp
->nm_timeouts
- 1];
1757 if (rep
->r_rtt
<= timeo
)
1759 if (nmp
->nm_timeouts
< 8)
1763 * Check for server not responding
1765 if ((rep
->r_flags
& R_TPRINTFMSG
) == 0 &&
1766 rep
->r_rexmit
> nmp
->nm_deadthresh
) {
1767 nfs_msg(rep
->r_procp
,
1768 nmp
->nm_mountp
->mnt_stat
.f_mntfromname
,
1770 rep
->r_flags
|= R_TPRINTFMSG
;
1772 if (rep
->r_rexmit
>= rep
->r_retry
) { /* too many */
1773 nfsstats
.rpctimeouts
++;
1777 if (nmp
->nm_sotype
!= SOCK_DGRAM
) {
1778 if (++rep
->r_rexmit
> NFS_MAXREXMIT
)
1779 rep
->r_rexmit
= NFS_MAXREXMIT
;
1782 if ((so
= nmp
->nm_so
) == NULL
)
1786 * If there is enough space and the window allows..
1788 * Set r_rtt to -1 in case we fail to send it now.
1791 rttdiag
= rep
->r_rtt
;
1794 if (sbspace(&so
->so_snd
) >= rep
->r_mreq
->m_pkthdr
.len
&&
1795 ((nmp
->nm_flag
& NFSMNT_DUMBTIMR
) ||
1796 (rep
->r_flags
& R_SENT
) ||
1797 nmp
->nm_sent
< nmp
->nm_cwnd
) &&
1798 (m
= m_copym(rep
->r_mreq
, 0, M_COPYALL
, M_DONTWAIT
))){
1800 struct proc
*p
= current_proc();
1803 if (rep
->r_flags
& R_SENT
&& nfsprnttimo
&&
1804 nmp
->nm_timeouts
>= nfsprnttimo
) {
1805 int t
= proct
[rep
->r_procnum
];
1807 NFS_DPF(DUP
, ("nfs_timer %s nmtm=%d tms=%d rtt=%d tm=%d p=%d A=%d D=%d\n", nmp
->nm_mountp
->mnt_stat
.f_mntfromname
, nmp
->nm_timeo
, nmp
->nm_timeouts
, rttdiag
, timeo
, rep
->r_procnum
, nmp
->nm_srtt
[t
-1], nmp
->nm_sdrtt
[t
-1]));
1809 NFS_DPF(DUP
, ("nfs_timer %s nmtm=%d tms=%d rtt=%d tm=%d p=%d\n", nmp
->nm_mountp
->mnt_stat
.f_mntfromname
, nmp
->nm_timeo
, nmp
->nm_timeouts
, rttdiag
, timeo
, rep
->r_procnum
));
1812 #endif /* NFSDIAG */
1814 * Iff first send, start timing
1815 * else turn timing off, backoff timer
1816 * and divide congestion window by 2.
1817 * We update these *before* the send to avoid
1818 * racing against receiving the reply.
1819 * We save them so we can restore them on send error.
1821 flags
= rep
->r_flags
;
1822 rexmit
= rep
->r_rexmit
;
1823 cwnd
= nmp
->nm_cwnd
;
1824 sent
= nmp
->nm_sent
;
1826 if (rep
->r_flags
& R_SENT
) {
1827 rep
->r_flags
&= ~R_TIMING
;
1828 if (++rep
->r_rexmit
> NFS_MAXREXMIT
)
1829 rep
->r_rexmit
= NFS_MAXREXMIT
;
1831 if (nmp
->nm_cwnd
< NFS_CWNDSCALE
)
1832 nmp
->nm_cwnd
= NFS_CWNDSCALE
;
1833 nfsstats
.rpcretries
++;
1835 rep
->r_flags
|= R_SENT
;
1836 nmp
->nm_sent
+= NFS_CWNDSCALE
;
1838 FSDBG(535, xid
, rep
, nmp
->nm_sent
, nmp
->nm_cwnd
);
1840 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1842 if ((nmp
->nm_flag
& NFSMNT_NOCONN
) == 0)
1843 error
= (*so
->so_proto
->pr_usrreqs
->pru_send
)
1844 (so
, 0, m
, 0, 0, p
);
1846 error
= (*so
->so_proto
->pr_usrreqs
->pru_send
)
1847 (so
, 0, m
, mtod(nmp
->nm_nam
, struct sockaddr
*), 0, p
);
1849 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1851 FSDBG(535, xid
, error
, sent
, cwnd
);
1853 * This is to fix "nfs_sigintr" DSI panics.
1854 * We may have slept during the send so the current
1855 * place in the request queue may have been released.
1856 * Due to zone_gc it may even be part of an
1857 * unrelated newly allocated data structure.
1858 * Restart the list scan from the top if needed...
1860 for (rp
= nfs_reqq
.tqh_first
; rp
;
1861 rp
= rp
->r_chain
.tqe_next
)
1862 if (rp
== rep
&& rp
->r_xid
== xid
)
1867 panic("nfs_timer: race error %d xid 0x%x\n",
1872 if (NFSIGNORE_SOERROR(nmp
->nm_soflags
, error
))
1874 rep
->r_flags
= flags
;
1875 rep
->r_rexmit
= rexmit
;
1876 nmp
->nm_cwnd
= cwnd
;
1877 nmp
->nm_sent
= sent
;
1879 nfsstats
.rpcretries
--;
1884 #ifndef NFS_NOSERVER
1886 * Call the nqnfs server timer once a second to handle leases.
1888 if (lasttime
!= time
.tv_sec
) {
1889 lasttime
= time
.tv_sec
;
1894 * Scan the write gathering queues for writes that need to be
1897 cur_usec
= (u_quad_t
)time
.tv_sec
* 1000000 + (u_quad_t
)time
.tv_usec
;
1898 for (slp
= nfssvc_sockhead
.tqh_first
; slp
!= 0;
1899 slp
= slp
->ns_chain
.tqe_next
) {
1900 if (slp
->ns_tq
.lh_first
&& slp
->ns_tq
.lh_first
->nd_time
<=cur_usec
)
1901 nfsrv_wakenfsd(slp
);
1903 #endif /* NFS_NOSERVER */
1905 timeout(nfs_timer_funnel
, (void *)0, nfs_ticks
);
1911 * Test for a termination condition pending on the process.
1912 * This is used for NFSMNT_INT mounts.
1915 nfs_sigintr(nmp
, rep
, p
)
1916 struct nfsmount
*nmp
;
1918 register struct proc
*p
;
1921 if (rep
&& (rep
->r_flags
& R_SOFTTERM
))
1923 if (!(nmp
->nm_flag
& NFSMNT_INT
))
1925 if (p
&& p
->p_siglist
&&
1926 (((p
->p_siglist
& ~p
->p_sigmask
) & ~p
->p_sigignore
) &
1933 * Lock a socket against others.
1934 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
1935 * and also to avoid race conditions between the processes with nfs requests
1936 * in progress when a reconnect is necessary.
1939 nfs_sndlock(flagp
, rep
)
1940 register int *flagp
;
1944 int slpflag
= 0, slptimeo
= 0;
1948 if (rep
->r_nmp
->nm_flag
& NFSMNT_INT
)
1951 p
= (struct proc
*)0;
1952 while (*flagp
& NFSMNT_SNDLOCK
) {
1953 if (nfs_sigintr(rep
->r_nmp
, rep
, p
))
1955 *flagp
|= NFSMNT_WANTSND
;
1956 (void) tsleep((caddr_t
)flagp
, slpflag
| (PZERO
- 1), "nfsndlck",
1958 if (slpflag
== PCATCH
) {
1963 * Make sure while we slept that the mountpoint didn't go away.
1964 * nfs_sigintr and callers expect it in tact.
1967 return (ECONNABORTED
); /* don't have lock until out of loop */
1969 *flagp
|= NFSMNT_SNDLOCK
;
1974 * Unlock the stream socket for others.
1977 nfs_sndunlock(flagp
)
1978 register int *flagp
;
1981 if ((*flagp
& NFSMNT_SNDLOCK
) == 0)
1982 panic("nfs sndunlock");
1983 *flagp
&= ~NFSMNT_SNDLOCK
;
1984 if (*flagp
& NFSMNT_WANTSND
) {
1985 *flagp
&= ~NFSMNT_WANTSND
;
1986 wakeup((caddr_t
)flagp
);
1992 register struct nfsreq
*rep
;
1994 register int *flagp
= &rep
->r_nmp
->nm_flag
;
1995 int slpflag
, slptimeo
= 0;
1997 FSDBG_TOP(534, rep
->r_xid
, rep
, rep
->r_nmp
, *flagp
);
1998 if (*flagp
& NFSMNT_INT
)
2002 while (*flagp
& NFSMNT_RCVLOCK
) {
2003 if (nfs_sigintr(rep
->r_nmp
, rep
, rep
->r_procp
)) {
2004 FSDBG_BOT(534, rep
->r_xid
, rep
, rep
->r_nmp
, 0x100);
2006 } else if (rep
->r_mrep
!= NULL
) {
2008 * Don't bother sleeping if reply already arrived
2010 FSDBG_BOT(534, rep
->r_xid
, rep
, rep
->r_nmp
, 0x101);
2013 FSDBG(534, rep
->r_xid
, rep
, rep
->r_nmp
, 0x102);
2014 *flagp
|= NFSMNT_WANTRCV
;
2015 (void) tsleep((caddr_t
)flagp
, slpflag
| (PZERO
- 1), "nfsrcvlk",
2017 if (slpflag
== PCATCH
) {
2022 * Make sure while we slept that the mountpoint didn't go away.
2023 * nfs_sigintr and caller nfs_reply expect it intact.
2026 FSDBG_BOT(534, rep
->r_xid
, rep
, rep
->r_nmp
, 0x103);
2027 return (ECONNABORTED
); /* don't have lock until out of loop */
2031 * nfs_reply will handle it if reply already arrived.
2032 * (We may have slept or been preempted while on network funnel).
2034 FSDBG_BOT(534, rep
->r_xid
, rep
, rep
->r_nmp
, *flagp
);
2035 *flagp
|= NFSMNT_RCVLOCK
;
2040 * Unlock the stream socket for others.
2043 nfs_rcvunlock(flagp
)
2044 register int *flagp
;
2047 FSDBG(533, flagp
, *flagp
, 0, 0);
2048 if ((*flagp
& NFSMNT_RCVLOCK
) == 0)
2049 panic("nfs rcvunlock");
2050 *flagp
&= ~NFSMNT_RCVLOCK
;
2051 if (*flagp
& NFSMNT_WANTRCV
) {
2052 *flagp
&= ~NFSMNT_WANTRCV
;
2053 wakeup((caddr_t
)flagp
);
2058 #ifndef NFS_NOSERVER
2060 * Socket upcall routine for the nfsd sockets.
2061 * The caddr_t arg is a pointer to the "struct nfssvc_sock".
2062 * Essentially do as much as possible non-blocking, else punt and it will
2063 * be called with M_WAIT from an nfsd.
2066 * Needs to eun under network funnel
2069 nfsrv_rcv(so
, arg
, waitflag
)
2074 register struct nfssvc_sock
*slp
= (struct nfssvc_sock
*)arg
;
2075 register struct mbuf
*m
;
2076 struct mbuf
*mp
, *mhck
;
2077 struct sockaddr
*nam
=0;
2080 struct sockaddr_in
*sin
;
2082 if ((slp
->ns_flag
& SLP_VALID
) == 0)
2086 * Define this to test for nfsds handling this under heavy load.
2088 if (waitflag
== M_DONTWAIT
) {
2089 slp
->ns_flag
|= SLP_NEEDQ
; goto dorecs
;
2092 auio
.uio_procp
= NULL
;
2093 if (so
->so_type
== SOCK_STREAM
) {
2095 * If there are already records on the queue, defer soreceive()
2096 * to an nfsd so that there is feedback to the TCP layer that
2097 * the nfs servers are heavily loaded.
2099 if (slp
->ns_rec
&& waitflag
== M_DONTWAIT
) {
2100 slp
->ns_flag
|= SLP_NEEDQ
;
2107 auio
.uio_resid
= 1000000000;
2108 flags
= MSG_DONTWAIT
;
2109 error
= soreceive(so
, (struct sockaddr
**) 0, &auio
, &mp
, (struct mbuf
**)0, &flags
);
2110 if (error
|| mp
== (struct mbuf
*)0) {
2111 if (error
== EWOULDBLOCK
)
2112 slp
->ns_flag
|= SLP_NEEDQ
;
2114 slp
->ns_flag
|= SLP_DISCONN
;
2118 if (slp
->ns_rawend
) {
2119 slp
->ns_rawend
->m_next
= m
;
2120 slp
->ns_cc
+= 1000000000 - auio
.uio_resid
;
2123 slp
->ns_cc
= 1000000000 - auio
.uio_resid
;
2130 * Now try and parse record(s) out of the raw stream data.
2132 error
= nfsrv_getstream(slp
, waitflag
);
2135 slp
->ns_flag
|= SLP_DISCONN
;
2137 slp
->ns_flag
|= SLP_NEEDQ
;
2141 auio
.uio_resid
= 1000000000;
2142 flags
= MSG_DONTWAIT
;
2144 error
= soreceive(so
, &nam
, &auio
, &mp
,
2145 (struct mbuf
**)0, &flags
);
2149 MGET(mhck
, M_WAIT
, MT_SONAME
);
2150 mhck
->m_len
= nam
->sa_len
;
2151 sin
= mtod(mhck
, struct sockaddr_in
*);
2152 bcopy(nam
, sin
, sizeof(struct sockaddr_in
));
2153 mhck
->m_hdr
.mh_len
= sizeof(struct sockaddr_in
);
2154 FREE(nam
, M_SONAME
);
2161 slp
->ns_recend
->m_nextpkt
= m
;
2165 m
->m_nextpkt
= (struct mbuf
*)0;
2168 if ((so
->so_proto
->pr_flags
& PR_CONNREQUIRED
)
2169 && error
!= EWOULDBLOCK
) {
2170 slp
->ns_flag
|= SLP_DISCONN
;
2178 * Now try and process the request records, non-blocking.
2181 if (waitflag
== M_DONTWAIT
&&
2182 (slp
->ns_rec
|| (slp
->ns_flag
& (SLP_NEEDQ
| SLP_DISCONN
)))) {
2183 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
2184 nfsrv_wakenfsd(slp
);
2185 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
2190 * Try and extract an RPC request from the mbuf data list received on a
2191 * stream socket. The "waitflag" argument indicates whether or not it
2195 nfsrv_getstream(slp
, waitflag
)
2196 register struct nfssvc_sock
*slp
;
2199 register struct mbuf
*m
, **mpp
;
2200 register char *cp1
, *cp2
;
2202 struct mbuf
*om
, *m2
, *recm
= 0;
2205 if (slp
->ns_flag
& SLP_GETSTREAM
)
2206 panic("nfs getstream");
2207 slp
->ns_flag
|= SLP_GETSTREAM
;
2209 if (slp
->ns_reclen
== 0) {
2210 if (slp
->ns_cc
< NFSX_UNSIGNED
) {
2211 slp
->ns_flag
&= ~SLP_GETSTREAM
;
2215 if (m
->m_len
>= NFSX_UNSIGNED
) {
2216 bcopy(mtod(m
, caddr_t
), (caddr_t
)&recmark
, NFSX_UNSIGNED
);
2217 m
->m_data
+= NFSX_UNSIGNED
;
2218 m
->m_len
-= NFSX_UNSIGNED
;
2220 cp1
= (caddr_t
)&recmark
;
2221 cp2
= mtod(m
, caddr_t
);
2222 while (cp1
< ((caddr_t
)&recmark
) + NFSX_UNSIGNED
) {
2223 while (m
->m_len
== 0) {
2225 cp2
= mtod(m
, caddr_t
);
2232 slp
->ns_cc
-= NFSX_UNSIGNED
;
2233 recmark
= ntohl(recmark
);
2234 slp
->ns_reclen
= recmark
& ~0x80000000;
2235 if (recmark
& 0x80000000)
2236 slp
->ns_flag
|= SLP_LASTFRAG
;
2238 slp
->ns_flag
&= ~SLP_LASTFRAG
;
2239 if (slp
->ns_reclen
< NFS_MINPACKET
|| slp
->ns_reclen
> NFS_MAXPACKET
) {
2240 slp
->ns_flag
&= ~SLP_GETSTREAM
;
2246 * Now get the record part.
2248 if (slp
->ns_cc
== slp
->ns_reclen
) {
2250 slp
->ns_raw
= slp
->ns_rawend
= (struct mbuf
*)0;
2251 slp
->ns_cc
= slp
->ns_reclen
= 0;
2252 } else if (slp
->ns_cc
> slp
->ns_reclen
) {
2255 om
= (struct mbuf
*)0;
2256 while (len
< slp
->ns_reclen
) {
2257 if ((len
+ m
->m_len
) > slp
->ns_reclen
) {
2258 m2
= m_copym(m
, 0, slp
->ns_reclen
- len
,
2266 m
->m_data
+= slp
->ns_reclen
- len
;
2267 m
->m_len
-= slp
->ns_reclen
- len
;
2268 len
= slp
->ns_reclen
;
2270 slp
->ns_flag
&= ~SLP_GETSTREAM
;
2271 return (EWOULDBLOCK
);
2273 } else if ((len
+ m
->m_len
) == slp
->ns_reclen
) {
2278 om
->m_next
= (struct mbuf
*)0;
2289 slp
->ns_flag
&= ~SLP_GETSTREAM
;
2294 * Accumulate the fragments into a record.
2296 mpp
= &slp
->ns_frag
;
2298 mpp
= &((*mpp
)->m_next
);
2300 if (slp
->ns_flag
& SLP_LASTFRAG
) {
2302 slp
->ns_recend
->m_nextpkt
= slp
->ns_frag
;
2304 slp
->ns_rec
= slp
->ns_frag
;
2305 slp
->ns_recend
= slp
->ns_frag
;
2306 slp
->ns_frag
= (struct mbuf
*)0;
2312 * Parse an RPC header.
2315 nfsrv_dorec(slp
, nfsd
, ndp
)
2316 register struct nfssvc_sock
*slp
;
2318 struct nfsrv_descript
**ndp
;
2320 register struct mbuf
*m
;
2321 register struct mbuf
*nam
;
2322 register struct nfsrv_descript
*nd
;
2326 if ((slp
->ns_flag
& SLP_VALID
) == 0 ||
2327 (m
= slp
->ns_rec
) == (struct mbuf
*)0)
2329 slp
->ns_rec
= m
->m_nextpkt
;
2331 m
->m_nextpkt
= (struct mbuf
*)0;
2333 slp
->ns_recend
= (struct mbuf
*)0;
2334 if (m
->m_type
== MT_SONAME
) {
2340 MALLOC_ZONE(nd
, struct nfsrv_descript
*,
2341 sizeof (struct nfsrv_descript
), M_NFSRVDESC
, M_WAITOK
);
2342 nd
->nd_md
= nd
->nd_mrep
= m
;
2344 nd
->nd_dpos
= mtod(m
, caddr_t
);
2345 error
= nfs_getreq(nd
, nfsd
, TRUE
);
2348 _FREE_ZONE((caddr_t
)nd
, sizeof *nd
, M_NFSRVDESC
);
2357 * Parse an RPC request
2359 * - fill in the cred struct.
2362 nfs_getreq(nd
, nfsd
, has_header
)
2363 register struct nfsrv_descript
*nd
;
2367 register int len
, i
;
2368 register u_long
*tl
;
2372 caddr_t dpos
, cp2
, cp
;
2373 u_long nfsvers
, auth_type
;
2375 int error
= 0, nqnfs
= 0, ticklen
;
2376 struct mbuf
*mrep
, *md
;
2377 register struct nfsuid
*nuidp
;
2378 struct timeval tvin
, tvout
;
2379 #if 0 /* until encrypted keys are implemented */
2380 NFSKERBKEYSCHED_T keys
; /* stores key schedule */
2387 nfsm_dissect(tl
, u_long
*, 10 * NFSX_UNSIGNED
);
2388 nd
->nd_retxid
= fxdr_unsigned(u_long
, *tl
++);
2389 if (*tl
++ != rpc_call
) {
2394 nfsm_dissect(tl
, u_long
*, 8 * NFSX_UNSIGNED
);
2397 if (*tl
++ != rpc_vers
) {
2398 nd
->nd_repstat
= ERPCMISMATCH
;
2399 nd
->nd_procnum
= NFSPROC_NOOP
;
2402 if (*tl
!= nfs_prog
) {
2403 if (*tl
== nqnfs_prog
)
2406 nd
->nd_repstat
= EPROGUNAVAIL
;
2407 nd
->nd_procnum
= NFSPROC_NOOP
;
2412 nfsvers
= fxdr_unsigned(u_long
, *tl
++);
2413 if (((nfsvers
< NFS_VER2
|| nfsvers
> NFS_VER3
) && !nqnfs
) ||
2414 (nfsvers
!= NQNFS_VER3
&& nqnfs
)) {
2415 nd
->nd_repstat
= EPROGMISMATCH
;
2416 nd
->nd_procnum
= NFSPROC_NOOP
;
2420 nd
->nd_flag
= (ND_NFSV3
| ND_NQNFS
);
2421 else if (nfsvers
== NFS_VER3
)
2422 nd
->nd_flag
= ND_NFSV3
;
2423 nd
->nd_procnum
= fxdr_unsigned(u_long
, *tl
++);
2424 if (nd
->nd_procnum
== NFSPROC_NULL
)
2426 if (nd
->nd_procnum
>= NFS_NPROCS
||
2427 (!nqnfs
&& nd
->nd_procnum
>= NQNFSPROC_GETLEASE
) ||
2428 (!nd
->nd_flag
&& nd
->nd_procnum
> NFSV2PROC_STATFS
)) {
2429 nd
->nd_repstat
= EPROCUNAVAIL
;
2430 nd
->nd_procnum
= NFSPROC_NOOP
;
2433 if ((nd
->nd_flag
& ND_NFSV3
) == 0)
2434 nd
->nd_procnum
= nfsv3_procid
[nd
->nd_procnum
];
2436 len
= fxdr_unsigned(int, *tl
++);
2437 if (len
< 0 || len
> RPCAUTH_MAXSIZ
) {
2442 nd
->nd_flag
&= ~ND_KERBAUTH
;
2444 * Handle auth_unix or auth_kerb.
2446 if (auth_type
== rpc_auth_unix
) {
2447 len
= fxdr_unsigned(int, *++tl
);
2448 if (len
< 0 || len
> NFS_MAXNAMLEN
) {
2452 nfsm_adv(nfsm_rndup(len
));
2453 nfsm_dissect(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
2454 bzero((caddr_t
)&nd
->nd_cr
, sizeof (struct ucred
));
2455 nd
->nd_cr
.cr_ref
= 1;
2456 nd
->nd_cr
.cr_uid
= fxdr_unsigned(uid_t
, *tl
++);
2457 nd
->nd_cr
.cr_gid
= fxdr_unsigned(gid_t
, *tl
++);
2458 len
= fxdr_unsigned(int, *tl
);
2459 if (len
< 0 || len
> RPCAUTH_UNIXGIDS
) {
2463 nfsm_dissect(tl
, u_long
*, (len
+ 2) * NFSX_UNSIGNED
);
2464 for (i
= 1; i
<= len
; i
++)
2466 nd
->nd_cr
.cr_groups
[i
] = fxdr_unsigned(gid_t
, *tl
++);
2469 nd
->nd_cr
.cr_ngroups
= (len
>= NGROUPS
) ? NGROUPS
: (len
+ 1);
2470 if (nd
->nd_cr
.cr_ngroups
> 1)
2471 nfsrvw_sort(nd
->nd_cr
.cr_groups
, nd
->nd_cr
.cr_ngroups
);
2472 len
= fxdr_unsigned(int, *++tl
);
2473 if (len
< 0 || len
> RPCAUTH_MAXSIZ
) {
2478 nfsm_adv(nfsm_rndup(len
));
2479 } else if (auth_type
== rpc_auth_kerb
) {
2480 switch (fxdr_unsigned(int, *tl
++)) {
2481 case RPCAKN_FULLNAME
:
2482 ticklen
= fxdr_unsigned(int, *tl
);
2483 *((u_long
*)nfsd
->nfsd_authstr
) = *tl
;
2484 uio
.uio_resid
= nfsm_rndup(ticklen
) + NFSX_UNSIGNED
;
2485 nfsd
->nfsd_authlen
= uio
.uio_resid
+ NFSX_UNSIGNED
;
2486 if (uio
.uio_resid
> (len
- 2 * NFSX_UNSIGNED
)) {
2493 uio
.uio_segflg
= UIO_SYSSPACE
;
2494 iov
.iov_base
= (caddr_t
)&nfsd
->nfsd_authstr
[4];
2495 iov
.iov_len
= RPCAUTH_MAXSIZ
- 4;
2496 nfsm_mtouio(&uio
, uio
.uio_resid
);
2497 nfsm_dissect(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
2498 if (*tl
++ != rpc_auth_kerb
||
2499 fxdr_unsigned(int, *tl
) != 4 * NFSX_UNSIGNED
) {
2500 printf("Bad kerb verifier\n");
2501 nd
->nd_repstat
= (NFSERR_AUTHERR
|AUTH_BADVERF
);
2502 nd
->nd_procnum
= NFSPROC_NOOP
;
2505 nfsm_dissect(cp
, caddr_t
, 4 * NFSX_UNSIGNED
);
2507 if (fxdr_unsigned(int, *tl
) != RPCAKN_FULLNAME
) {
2508 printf("Not fullname kerb verifier\n");
2509 nd
->nd_repstat
= (NFSERR_AUTHERR
|AUTH_BADVERF
);
2510 nd
->nd_procnum
= NFSPROC_NOOP
;
2513 cp
+= NFSX_UNSIGNED
;
2514 bcopy(cp
, nfsd
->nfsd_verfstr
, 3 * NFSX_UNSIGNED
);
2515 nfsd
->nfsd_verflen
= 3 * NFSX_UNSIGNED
;
2516 nd
->nd_flag
|= ND_KERBFULL
;
2517 nfsd
->nfsd_flag
|= NFSD_NEEDAUTH
;
2519 case RPCAKN_NICKNAME
:
2520 if (len
!= 2 * NFSX_UNSIGNED
) {
2521 printf("Kerb nickname short\n");
2522 nd
->nd_repstat
= (NFSERR_AUTHERR
|AUTH_BADCRED
);
2523 nd
->nd_procnum
= NFSPROC_NOOP
;
2526 nickuid
= fxdr_unsigned(uid_t
, *tl
);
2527 nfsm_dissect(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
2528 if (*tl
++ != rpc_auth_kerb
||
2529 fxdr_unsigned(int, *tl
) != 3 * NFSX_UNSIGNED
) {
2530 printf("Kerb nick verifier bad\n");
2531 nd
->nd_repstat
= (NFSERR_AUTHERR
|AUTH_BADVERF
);
2532 nd
->nd_procnum
= NFSPROC_NOOP
;
2535 nfsm_dissect(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
2536 tvin
.tv_sec
= *tl
++;
2539 for (nuidp
= NUIDHASH(nfsd
->nfsd_slp
,nickuid
)->lh_first
;
2540 nuidp
!= 0; nuidp
= nuidp
->nu_hash
.le_next
) {
2541 if (nuidp
->nu_cr
.cr_uid
== nickuid
&&
2543 netaddr_match(NU_NETFAM(nuidp
),
2544 &nuidp
->nu_haddr
, nd
->nd_nam2
)))
2549 (NFSERR_AUTHERR
|AUTH_REJECTCRED
);
2550 nd
->nd_procnum
= NFSPROC_NOOP
;
2555 * Now, decrypt the timestamp using the session key
2562 tvout
.tv_sec
= fxdr_unsigned(long, tvout
.tv_sec
);
2563 tvout
.tv_usec
= fxdr_unsigned(long, tvout
.tv_usec
);
2564 if (nuidp
->nu_expire
< time
.tv_sec
||
2565 nuidp
->nu_timestamp
.tv_sec
> tvout
.tv_sec
||
2566 (nuidp
->nu_timestamp
.tv_sec
== tvout
.tv_sec
&&
2567 nuidp
->nu_timestamp
.tv_usec
> tvout
.tv_usec
)) {
2568 nuidp
->nu_expire
= 0;
2570 (NFSERR_AUTHERR
|AUTH_REJECTVERF
);
2571 nd
->nd_procnum
= NFSPROC_NOOP
;
2574 nfsrv_setcred(&nuidp
->nu_cr
, &nd
->nd_cr
);
2575 nd
->nd_flag
|= ND_KERBNICK
;
2578 nd
->nd_repstat
= (NFSERR_AUTHERR
| AUTH_REJECTCRED
);
2579 nd
->nd_procnum
= NFSPROC_NOOP
;
2584 * For nqnfs, get piggybacked lease request.
2586 if (nqnfs
&& nd
->nd_procnum
!= NQNFSPROC_EVICTED
) {
2587 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
2588 nd
->nd_flag
|= fxdr_unsigned(int, *tl
);
2589 if (nd
->nd_flag
& ND_LEASE
) {
2590 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
2591 nd
->nd_duration
= fxdr_unsigned(int, *tl
);
2593 nd
->nd_duration
= NQ_MINLEASE
;
2595 nd
->nd_duration
= NQ_MINLEASE
;
2604 * Search for a sleeping nfsd and wake it up.
2605 * SIDE EFFECT: If none found, set NFSD_CHECKSLP flag, so that one of the
2606 * running nfsds will go look for the work in the nfssvc_sock list.
2610 struct nfssvc_sock
*slp
;
2612 register struct nfsd
*nd
;
2614 if ((slp
->ns_flag
& SLP_VALID
) == 0)
2616 for (nd
= nfsd_head
.tqh_first
; nd
!= 0; nd
= nd
->nfsd_chain
.tqe_next
) {
2617 if (nd
->nfsd_flag
& NFSD_WAITING
) {
2618 nd
->nfsd_flag
&= ~NFSD_WAITING
;
2620 panic("nfsd wakeup");
2623 wakeup((caddr_t
)nd
);
2627 slp
->ns_flag
|= SLP_DOREC
;
2628 nfsd_head_flag
|= NFSD_CHECKSLP
;
2630 #endif /* NFS_NOSERVER */
2633 nfs_msg(p
, server
, msg
)
2640 tpr
= tprintf_open(p
);
2643 tprintf(tpr
, "nfs server %s: %s\n", server
, msg
);