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, 1991, 1993, 1995
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_socket.c 8.5 (Berkeley) 3/30/95
62 * FreeBSD-Id: nfs_socket.c,v 1.30 1997/10/28 15:59:07 bde Exp $
66 * Socket operations for use by nfs
69 #include <sys/param.h>
70 #include <sys/systm.h>
72 #include <sys/mount.h>
73 #include <sys/kernel.h>
75 #include <sys/malloc.h>
76 #include <sys/vnode.h>
77 #include <sys/domain.h>
78 #include <sys/protosw.h>
79 #include <sys/socket.h>
80 #include <sys/socketvar.h>
81 #include <sys/syslog.h>
82 #include <sys/tprintf.h>
83 #include <machine/spl.h>
86 #include <kern/clock.h>
89 #include <netinet/in.h>
90 #include <netinet/tcp.h>
92 #include <nfs/rpcv2.h>
93 #include <nfs/nfsproto.h>
95 #include <nfs/xdr_subs.h>
96 #include <nfs/nfsm_subs.h>
97 #include <nfs/nfsmount.h>
98 #include <nfs/nfsnode.h>
99 #include <nfs/nfsrtt.h>
100 #include <nfs/nqnfs.h>
102 #include <sys/kdebug.h>
104 #define FSDBG(A, B, C, D, E) \
105 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_NONE, \
106 (int)(B), (int)(C), (int)(D), (int)(E), 0)
107 #define FSDBG_TOP(A, B, C, D, E) \
108 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_START, \
109 (int)(B), (int)(C), (int)(D), (int)(E), 0)
110 #define FSDBG_BOT(A, B, C, D, E) \
111 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_END, \
112 (int)(B), (int)(C), (int)(D), (int)(E), 0)
118 * Estimate rto for an nfs rpc sent via. an unreliable datagram.
119 * Use the mean and mean deviation of rtt for the appropriate type of rpc
120 * for the frequent rpcs and a default for the others.
121 * The justification for doing "other" this way is that these rpcs
122 * happen so infrequently that timer est. would probably be stale.
123 * Also, since many of these rpcs are
124 * non-idempotent, a conservative timeout is desired.
125 * getattr, lookup - A+2D
129 #define NFS_RTO(n, t) \
130 ((t) == 0 ? (n)->nm_timeo : \
132 (((((n)->nm_srtt[t-1] + 3) >> 2) + (n)->nm_sdrtt[t-1] + 1) >> 1) : \
133 ((((n)->nm_srtt[t-1] + 7) >> 3) + (n)->nm_sdrtt[t-1] + 1)))
134 #define NFS_SRTT(r) (r)->r_nmp->nm_srtt[proct[(r)->r_procnum] - 1]
135 #define NFS_SDRTT(r) (r)->r_nmp->nm_sdrtt[proct[(r)->r_procnum] - 1]
137 * External data, mostly RPC constants in XDR form
139 extern u_long rpc_reply
, rpc_msgdenied
, rpc_mismatch
, rpc_vers
, rpc_auth_unix
,
140 rpc_msgaccepted
, rpc_call
, rpc_autherr
,
142 extern u_long nfs_prog
, nqnfs_prog
;
143 extern time_t nqnfsstarttime
;
144 extern struct nfsstats nfsstats
;
145 extern int nfsv3_procid
[NFS_NPROCS
];
146 extern int nfs_ticks
;
147 extern u_long nfs_xidwrap
;
150 * Defines which timer to use for the procnum.
157 static int proct
[NFS_NPROCS
] = {
158 0, 1, 0, 2, 1, 3, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0,
163 * There is a congestion window for outstanding rpcs maintained per mount
164 * point. The cwnd size is adjusted in roughly the way that:
165 * Van Jacobson, Congestion avoidance and Control, In "Proceedings of
166 * SIGCOMM '88". ACM, August 1988.
167 * describes for TCP. The cwnd size is chopped in half on a retransmit timeout
168 * and incremented by 1/cwnd when each rpc reply is received and a full cwnd
169 * of rpcs is in progress.
170 * (The sent count and cwnd are scaled for integer arith.)
171 * Variants of "slow start" were tried and were found to be too much of a
172 * performance hit (ave. rtt 3 times larger),
173 * I suspect due to the large rtt that nfs rpcs have.
175 #define NFS_CWNDSCALE 256
176 #define NFS_MAXCWND (NFS_CWNDSCALE * 32)
177 static int nfs_backoff
[8] = { 2, 4, 8, 16, 32, 64, 128, 256, };
179 struct nfsrtt nfsrtt
;
181 static int nfs_msg
__P((struct proc
*,char *,char *));
182 static int nfs_rcvlock
__P((struct nfsreq
*));
183 static void nfs_rcvunlock
__P((int *flagp
));
184 static int nfs_receive
__P((struct nfsreq
*rep
, struct mbuf
**aname
,
186 static int nfs_reconnect
__P((struct nfsreq
*rep
));
188 static int nfsrv_getstream
__P((struct nfssvc_sock
*,int));
190 int (*nfsrv3_procs
[NFS_NPROCS
]) __P((struct nfsrv_descript
*nd
,
191 struct nfssvc_sock
*slp
,
193 struct mbuf
**mreqp
)) = {
221 #endif /* NFS_NOSERVER */
224 * NFSTRACE points were changed to FSDBG (KERNEL_DEBUG)
225 * But some of this code may prove useful someday...
229 int nfstraceindx
= 0;
230 struct nfstracerec nfstracebuf
[NFSTBUFSIZ
] = {{0,0,0,0}};
232 #define NFSTRACESUSPENDERS
233 #ifdef NFSTRACESUSPENDERS
234 uint nfstracemask
= 0xfff00200;
235 int nfstracexid
= -1;
236 uint onfstracemask
= 0;
237 int nfstracesuspend
= -1;
238 #define NFSTRACE_SUSPEND \
240 if (nfstracemask) { \
241 onfstracemask = nfstracemask; \
245 #define NFSTRACE_RESUME \
247 nfstracesuspend = -1; \
249 nfstracemask = onfstracemask; \
251 #define NFSTRACE_STARTSUSPENDCOUNTDOWN \
253 nfstracesuspend = (nfstraceindx+100) % NFSTBUFSIZ; \
255 #define NFSTRACE_SUSPENDING (nfstracesuspend != -1)
256 #define NFSTRACE_SUSPENSEOVER \
257 (nfstracesuspend > 100 ? \
258 (nfstraceindx >= nfstracesuspend || \
259 nfstraceindx < nfstracesuspend - 100) : \
260 (nfstraceindx >= nfstracesuspend && \
261 nfstraceindx < nfstracesuspend + 8192 - 100))
263 uint nfstracemask
= 0;
264 #endif /* NFSTRACESUSPENDERS */
269 int nfsoprocnum
, nfsolen
;
270 int nfsbt
[32], nfsbtlen
;
274 backtrace(int *where
, int size
)
276 int register sp
, *fp
, numsaved
;
278 __asm__
volatile("mr %0,r1" : "=r" (sp
));
280 fp
= (int *)*((int *)sp
);
282 for (numsaved
= 0; numsaved
< size
; numsaved
++) {
290 #elif defined(__i386__)
294 return (0); /* Till someone implements a real routine */
297 #error architecture not implemented.
301 nfsdup(struct nfsreq
*rep
)
303 int *ip
, i
, first
= 1, end
;
307 if ((nfs_debug
& NFS_DEBUG_DUP
) == 0)
309 /* last mbuf in chain will be nfs content */
310 for (mb
= rep
->r_mreq
; mb
->m_next
; mb
= mb
->m_next
)
312 if (rep
->r_procnum
== nfsoprocnum
&& mb
->m_len
== nfsolen
&&
313 !bcmp((caddr_t
)nfsodata
, mb
->m_data
, nfsolen
)) {
314 s
= b
+ sprintf(b
, "nfsdup x=%x p=%d h=", rep
->r_xid
,
316 end
= (int)(VTONFS(rep
->r_vp
)->n_fhp
);
317 ip
= (int *)(end
& ~3);
318 end
+= VTONFS(rep
->r_vp
)->n_fhsize
;
319 while ((int)ip
< end
) {
321 if (first
) { /* avoid leading zeroes */
325 s
+= sprintf(s
, "%x", i
);
327 s
+= sprintf(s
, "%08x", i
);
331 else /* eliminate trailing zeroes */
335 * set a breakpoint here and you can view the
336 * current backtrace and the one saved in nfsbt
340 nfsoprocnum
= rep
->r_procnum
;
342 bcopy(mb
->m_data
, (caddr_t
)nfsodata
, mb
->m_len
);
343 nfsbtlen
= backtrace(&nfsbt
, sizeof(nfsbt
));
348 * Initialize sockets and congestion for a new NFS connection.
349 * We do not free the sockaddr if error.
352 nfs_connect(nmp
, rep
)
353 register struct nfsmount
*nmp
;
356 register struct socket
*so
;
357 int s
, error
, rcvreserve
, sndreserve
;
358 struct sockaddr
*saddr
;
359 struct sockaddr_in sin
;
362 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
363 nmp
->nm_so
= (struct socket
*)0;
364 saddr
= mtod(nmp
->nm_nam
, struct sockaddr
*);
365 error
= socreate(saddr
->sa_family
, &nmp
->nm_so
, nmp
->nm_sotype
,
371 nmp
->nm_soflags
= so
->so_proto
->pr_flags
;
374 * Some servers require that the client port be a reserved port number.
376 if (saddr
->sa_family
== AF_INET
&& (nmp
->nm_flag
& NFSMNT_RESVPORT
)) {
377 sin
.sin_len
= sizeof (struct sockaddr_in
);
378 sin
.sin_family
= AF_INET
;
379 sin
.sin_addr
.s_addr
= INADDR_ANY
;
380 tport
= IPPORT_RESERVED
- 1;
381 sin
.sin_port
= htons(tport
);
383 while ((error
= sobind(so
, (struct sockaddr
*) &sin
) == EADDRINUSE
) &&
384 (--tport
> IPPORT_RESERVED
/ 2))
385 sin
.sin_port
= htons(tport
);
392 * Protocols that do not require connections may be optionally left
393 * unconnected for servers that reply from a port other than NFS_PORT.
395 if (nmp
->nm_flag
& NFSMNT_NOCONN
) {
396 if (nmp
->nm_soflags
& PR_CONNREQUIRED
) {
401 error
= soconnect(so
, mtod(nmp
->nm_nam
, struct sockaddr
*));
407 * Wait for the connection to complete. Cribbed from the
408 * connect system call but with the wait timing out so
409 * that interruptible mounts don't hang here for a long time.
412 while ((so
->so_state
& SS_ISCONNECTING
) && so
->so_error
== 0) {
413 (void) tsleep((caddr_t
)&so
->so_timeo
, PSOCK
,
415 if ((so
->so_state
& SS_ISCONNECTING
) &&
416 so
->so_error
== 0 && rep
&&
417 (error
= nfs_sigintr(nmp
, rep
, rep
->r_procp
))) {
418 so
->so_state
&= ~SS_ISCONNECTING
;
424 error
= so
->so_error
;
431 if (nmp
->nm_flag
& (NFSMNT_SOFT
| NFSMNT_INT
)) {
432 so
->so_rcv
.sb_timeo
= (5 * hz
);
433 so
->so_snd
.sb_timeo
= (5 * hz
);
435 so
->so_rcv
.sb_timeo
= 0;
436 so
->so_snd
.sb_timeo
= 0;
438 if (nmp
->nm_sotype
== SOCK_DGRAM
) {
439 sndreserve
= (nmp
->nm_wsize
+ NFS_MAXPKTHDR
) * 2;
440 rcvreserve
= (nmp
->nm_rsize
+ NFS_MAXPKTHDR
) * 2;
441 } else if (nmp
->nm_sotype
== SOCK_SEQPACKET
) {
442 sndreserve
= (nmp
->nm_wsize
+ NFS_MAXPKTHDR
) * 2;
443 rcvreserve
= (nmp
->nm_rsize
+ NFS_MAXPKTHDR
) * 2;
445 if (nmp
->nm_sotype
!= SOCK_STREAM
)
446 panic("nfscon sotype");
448 if (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) {
452 bzero(&sopt
, sizeof sopt
);
453 sopt
.sopt_level
= SOL_SOCKET
;
454 sopt
.sopt_name
= SO_KEEPALIVE
;
455 sopt
.sopt_val
= &val
;
456 sopt
.sopt_valsize
= sizeof val
;
460 if (so
->so_proto
->pr_protocol
== IPPROTO_TCP
) {
464 bzero(&sopt
, sizeof sopt
);
465 sopt
.sopt_level
= IPPROTO_TCP
;
466 sopt
.sopt_name
= TCP_NODELAY
;
467 sopt
.sopt_val
= &val
;
468 sopt
.sopt_valsize
= sizeof val
;
473 sndreserve
= (nmp
->nm_wsize
+ NFS_MAXPKTHDR
+ sizeof (u_long
))
475 rcvreserve
= (nmp
->nm_rsize
+ NFS_MAXPKTHDR
+ sizeof (u_long
))
479 error
= soreserve(so
, sndreserve
, rcvreserve
);
483 so
->so_rcv
.sb_flags
|= SB_NOINTR
;
484 so
->so_snd
.sb_flags
|= SB_NOINTR
;
486 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
488 /* Initialize other non-zero congestion variables */
489 nmp
->nm_srtt
[0] = nmp
->nm_srtt
[1] = nmp
->nm_srtt
[2] =
490 nmp
->nm_srtt
[3] = (NFS_TIMEO
<< 3);
491 nmp
->nm_sdrtt
[0] = nmp
->nm_sdrtt
[1] = nmp
->nm_sdrtt
[2] =
492 nmp
->nm_sdrtt
[3] = 0;
493 nmp
->nm_cwnd
= NFS_MAXCWND
/ 2; /* Initial send window */
495 FSDBG(529, nmp
, nmp
->nm_flag
, nmp
->nm_soflags
, nmp
->nm_cwnd
);
496 nmp
->nm_timeouts
= 0;
500 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
507 * Called when a connection is broken on a reliable protocol.
508 * - clean up the old socket
509 * - nfs_connect() again
510 * - set R_MUSTRESEND for all outstanding requests on mount point
511 * If this fails the mount point is DEAD!
512 * nb: Must be called with the nfs_sndlock() set on the mount point.
516 register struct nfsreq
*rep
;
518 register struct nfsreq
*rp
;
519 register struct nfsmount
*nmp
= rep
->r_nmp
;
523 while ((error
= nfs_connect(nmp
, rep
))) {
524 if (error
== EINTR
|| error
== ERESTART
)
526 (void) tsleep((caddr_t
)&lbolt
, PSOCK
, "nfscon", 0);
529 NFS_DPF(DUP
, ("nfs_reconnect RESEND\n"));
531 * Loop through outstanding request list and fix up all requests
534 for (rp
= nfs_reqq
.tqh_first
; rp
!= 0; rp
= rp
->r_chain
.tqe_next
) {
535 if (rp
->r_nmp
== nmp
)
536 rp
->r_flags
|= R_MUSTRESEND
;
542 * NFS disconnect. Clean up and unlink.
546 register struct nfsmount
*nmp
;
548 register struct socket
*so
;
550 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
553 nmp
->nm_so
= (struct socket
*)0;
557 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
561 * This is the nfs send routine. For connection based socket types, it
562 * must be called with an nfs_sndlock() on the socket.
563 * "rep == NULL" indicates that it has been called from a server.
564 * For the client side:
565 * - return EINTR if the RPC is terminated, 0 otherwise
566 * - set R_MUSTRESEND if the send fails for any reason
567 * - do any cleanup required by recoverable socket errors (???)
568 * For the server side:
569 * - return EINTR or ERESTART if interrupted by a signal
570 * - return EPIPE if a connection is lost for connection based sockets (TCP...)
571 * - do any cleanup required by recoverable socket errors (???)
574 nfs_send(so
, nam
, top
, rep
)
575 register struct socket
*so
;
577 register struct mbuf
*top
;
580 struct sockaddr
*sendnam
;
581 int error
, soflags
, flags
;
584 char savenametolog
[MNAMELEN
];
587 if (rep
->r_flags
& R_SOFTTERM
) {
591 if ((so
= rep
->r_nmp
->nm_so
) == NULL
) {
592 rep
->r_flags
|= R_MUSTRESEND
;
596 rep
->r_flags
&= ~R_MUSTRESEND
;
597 soflags
= rep
->r_nmp
->nm_soflags
;
598 for (rp
= nfs_reqq
.tqh_first
; rp
; rp
= rp
->r_chain
.tqe_next
)
602 xidqueued
= rp
->r_xid
;
604 soflags
= so
->so_proto
->pr_flags
;
605 if ((soflags
& PR_CONNREQUIRED
) || (so
->so_state
& SS_ISCONNECTED
) ||
607 sendnam
= (struct sockaddr
*)0;
609 sendnam
= mtod(nam
, struct sockaddr
*);
611 if (so
->so_type
== SOCK_SEQPACKET
)
621 * Save the name here in case mount point goes away when we switch
622 * funnels. The name is using local stack and is large, but don't
623 * want to block if we malloc.
626 strncpy(savenametolog
,
627 rep
->r_nmp
->nm_mountp
->mnt_stat
.f_mntfromname
,
629 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
630 error
= sosend(so
, sendnam
, (struct uio
*)0, top
,
631 (struct mbuf
*)0, flags
);
632 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
637 for (rp
= nfs_reqq
.tqh_first
; rp
;
638 rp
= rp
->r_chain
.tqe_next
)
639 if (rp
== rep
&& rp
->r_xid
== xidqueued
)
642 panic("nfs_send: error %d xid %x gone",
645 log(LOG_INFO
, "nfs send error %d for server %s\n",
646 error
, savenametolog
);
648 * Deal with errors for the client side.
650 if (rep
->r_flags
& R_SOFTTERM
)
653 rep
->r_flags
|= R_MUSTRESEND
;
655 ("nfs_send RESEND error=%d\n", error
));
658 log(LOG_INFO
, "nfsd send error %d\n", error
);
661 * Handle any recoverable (soft) socket errors here. (???)
663 if (error
!= EINTR
&& error
!= ERESTART
&&
664 error
!= EWOULDBLOCK
&& error
!= EPIPE
)
671 * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all
672 * done by soreceive(), but for SOCK_STREAM we must deal with the Record
673 * Mark and consolidate the data into a new mbuf list.
674 * nb: Sometimes TCP passes the data up to soreceive() in long lists of
676 * For SOCK_STREAM we must be very careful to read an entire record once
677 * we have read any of it, even if the system call has been interrupted.
680 nfs_receive(rep
, aname
, mp
)
681 register struct nfsreq
*rep
;
685 register struct socket
*so
;
688 register struct mbuf
*m
;
689 struct mbuf
*control
;
691 struct sockaddr
**getnam
;
692 struct sockaddr
*tmp_nam
;
694 struct sockaddr_in
*sin
;
695 int error
, sotype
, rcvflg
;
696 struct proc
*p
= current_proc(); /* XXX */
699 * Set up arguments for soreceive()
701 *mp
= (struct mbuf
*)0;
702 *aname
= (struct mbuf
*)0;
703 sotype
= rep
->r_nmp
->nm_sotype
;
706 * For reliable protocols, lock against other senders/receivers
707 * in case a reconnect is necessary.
708 * For SOCK_STREAM, first get the Record Mark to find out how much
709 * more there is to get.
710 * We must lock the socket against other receivers
711 * until we have an entire rpc request/reply.
713 if (sotype
!= SOCK_DGRAM
) {
714 error
= nfs_sndlock(&rep
->r_nmp
->nm_flag
, rep
);
719 * Check for fatal errors and resending request.
722 * Ugh: If a reconnect attempt just happened, nm_so
723 * would have changed. NULL indicates a failed
724 * attempt that has essentially shut down this
727 if (rep
->r_mrep
|| (rep
->r_flags
& R_SOFTTERM
)) {
728 nfs_sndunlock(&rep
->r_nmp
->nm_flag
);
731 so
= rep
->r_nmp
->nm_so
;
733 error
= nfs_reconnect(rep
);
735 nfs_sndunlock(&rep
->r_nmp
->nm_flag
);
740 while (rep
->r_flags
& R_MUSTRESEND
) {
741 m
= m_copym(rep
->r_mreq
, 0, M_COPYALL
, M_WAIT
);
742 nfsstats
.rpcretries
++;
744 ("nfs_receive RESEND %s\n",
745 rep
->r_nmp
->nm_mountp
->mnt_stat
.f_mntfromname
));
746 error
= nfs_send(so
, rep
->r_nmp
->nm_nam
, m
, rep
);
748 * we also hold rcv lock so rep is still
752 if (error
== EINTR
|| error
== ERESTART
||
753 (error
= nfs_reconnect(rep
))) {
754 nfs_sndunlock(&rep
->r_nmp
->nm_flag
);
760 nfs_sndunlock(&rep
->r_nmp
->nm_flag
);
761 if (sotype
== SOCK_STREAM
) {
762 aio
.iov_base
= (caddr_t
) &len
;
763 aio
.iov_len
= sizeof(u_long
);
766 auio
.uio_segflg
= UIO_SYSSPACE
;
767 auio
.uio_rw
= UIO_READ
;
769 auio
.uio_resid
= sizeof(u_long
);
772 rcvflg
= MSG_WAITALL
;
773 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
774 error
= soreceive(so
, (struct sockaddr
**)0, &auio
,
775 (struct mbuf
**)0, (struct mbuf
**)0, &rcvflg
);
776 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
777 if (!rep
->r_nmp
) /* if unmounted then bailout */
779 if (error
== EWOULDBLOCK
&& rep
) {
780 if (rep
->r_flags
& R_SOFTTERM
)
783 } while (error
== EWOULDBLOCK
);
784 if (!error
&& auio
.uio_resid
> 0) {
786 "short receive (%d/%d) from nfs server %s\n",
787 sizeof(u_long
) - auio
.uio_resid
,
789 rep
->r_nmp
->nm_mountp
->mnt_stat
.f_mntfromname
);
794 len
= ntohl(len
) & ~0x80000000;
796 * This is SERIOUS! We are out of sync with the sender
797 * and forcing a disconnect/reconnect is all I can do.
799 if (len
> NFS_MAXPACKET
) {
800 log(LOG_ERR
, "%s (%d) from nfs server %s\n",
801 "impossible packet length",
803 rep
->r_nmp
->nm_mountp
->mnt_stat
.f_mntfromname
);
807 auio
.uio_resid
= len
;
809 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
811 rcvflg
= MSG_WAITALL
;
812 error
= soreceive(so
, (struct sockaddr
**)0,
813 &auio
, mp
, (struct mbuf
**)0, &rcvflg
);
814 if (!rep
->r_nmp
) /* if unmounted then bailout */ {
815 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
818 } while (error
== EWOULDBLOCK
|| error
== EINTR
||
821 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
823 if (!error
&& auio
.uio_resid
> 0) {
825 "short receive (%d/%d) from nfs server %s\n",
826 len
- auio
.uio_resid
, len
,
827 rep
->r_nmp
->nm_mountp
->mnt_stat
.f_mntfromname
);
832 * NB: Since uio_resid is big, MSG_WAITALL is ignored
833 * and soreceive() will return when it has either a
834 * control msg or a data msg.
835 * We have no use for control msg., but must grab them
836 * and then throw them away so we know what is going
839 auio
.uio_resid
= len
= 100000000; /* Anything Big */
842 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
845 error
= soreceive(so
, (struct sockaddr
**)0,
846 &auio
, mp
, &control
, &rcvflg
);
847 if (!rep
->r_nmp
) /* if unmounted then bailout */ {
848 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
853 if (error
== EWOULDBLOCK
&& rep
) {
854 if (rep
->r_flags
& R_SOFTTERM
) {
855 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
859 } while (error
== EWOULDBLOCK
||
860 (!error
&& *mp
== NULL
&& control
));
862 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
864 if ((rcvflg
& MSG_EOR
) == 0)
866 if (!error
&& *mp
== NULL
)
868 len
-= auio
.uio_resid
;
871 if (error
&& error
!= EINTR
&& error
!= ERESTART
) {
873 *mp
= (struct mbuf
*)0;
876 "receive error %d from nfs server %s\n",
878 rep
->r_nmp
->nm_mountp
->mnt_stat
.f_mntfromname
);
879 error
= nfs_sndlock(&rep
->r_nmp
->nm_flag
, rep
);
881 error
= nfs_reconnect(rep
);
886 if ((so
= rep
->r_nmp
->nm_so
) == NULL
)
888 if (so
->so_state
& SS_ISCONNECTED
)
889 getnam
= (struct sockaddr
**)0;
892 auio
.uio_resid
= len
= 1000000;
895 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
898 error
= soreceive(so
, getnam
, &auio
, mp
,
899 (struct mbuf
**)0, &rcvflg
);
901 if ((getnam
) && (*getnam
)) {
902 MGET(mhck
, M_WAIT
, MT_SONAME
);
903 mhck
->m_len
= (*getnam
)->sa_len
;
904 sin
= mtod(mhck
, struct sockaddr_in
*);
905 bcopy(*getnam
, sin
, sizeof(struct sockaddr_in
));
906 mhck
->m_hdr
.mh_len
= sizeof(struct sockaddr_in
);
907 FREE(*getnam
, M_SONAME
);
910 if (!rep
->r_nmp
) /* if unmounted then bailout */ {
911 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
915 if (error
== EWOULDBLOCK
&&
916 (rep
->r_flags
& R_SOFTTERM
)) {
917 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
920 } while (error
== EWOULDBLOCK
);
922 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
923 len
-= auio
.uio_resid
;
928 *mp
= (struct mbuf
*)0;
934 * Implement receipt of reply on a socket.
935 * We must search through the list of received datagrams matching them
936 * with outstanding requests using the xid, until ours is found.
941 struct nfsreq
*myrep
;
943 register struct nfsreq
*rep
;
944 register struct nfsmount
*nmp
= myrep
->r_nmp
;
946 struct mbuf
*mrep
, *md
;
953 * Loop around until we get our own reply
957 * Lock against other receivers so that I don't get stuck in
958 * sbwait() after someone else has received my reply for me.
959 * Also necessary for connection based protocols to avoid
960 * race conditions during a reconnect.
961 * If nfs_rcvlock() returns EALREADY, that means that
962 * the reply has already been recieved by another
963 * process and we can return immediately. In this
964 * case, the lock is not taken to avoid races with
967 error
= nfs_rcvlock(myrep
);
968 if (error
== EALREADY
)
974 * If we slept after putting bits otw, then reply may have
975 * arrived. In which case returning is required, or we
976 * would hang trying to nfs_receive an already received reply.
978 if (myrep
->r_mrep
!= NULL
) {
979 nfs_rcvunlock(&nmp
->nm_flag
);
980 FSDBG(530, myrep
->r_xid
, myrep
, myrep
->r_nmp
, -1);
984 * Get the next Rpc reply off the socket. Assume myrep->r_nmp
985 * is still intact by checks done in nfs_rcvlock.
987 error
= nfs_receive(myrep
, &nam
, &mrep
);
989 * Bailout asap if nfsmount struct gone (unmounted).
991 if (!myrep
->r_nmp
|| !nmp
->nm_so
) {
992 FSDBG(530, myrep
->r_xid
, myrep
, nmp
, -2);
993 return (ECONNABORTED
);
996 FSDBG(530, myrep
->r_xid
, myrep
, nmp
, error
);
997 nfs_rcvunlock(&nmp
->nm_flag
);
999 /* Bailout asap if nfsmount struct gone (unmounted). */
1000 if (!myrep
->r_nmp
|| !nmp
->nm_so
)
1001 return (ECONNABORTED
);
1004 * Ignore routing errors on connectionless protocols??
1006 if (NFSIGNORE_SOERROR(nmp
->nm_soflags
, error
)) {
1007 nmp
->nm_so
->so_error
= 0;
1008 if (myrep
->r_flags
& R_GETONEREP
)
1018 * We assume all is fine, but if we did not have an error
1019 * and mrep is 0, better not dereference it. nfs_receieve
1020 * calls soreceive which carefully sets error=0 when it got
1021 * errors on sbwait (tsleep). In most cases, I assume that's
1022 * so we could go back again. In tcp case, EPIPE is returned.
1023 * In udp, case nfs_receive gets back here with no error and no
1024 * mrep. Is the right fix to have soreceive check for process
1025 * aborted after sbwait and return something non-zero? Should
1026 * nfs_receive give an EPIPE? Too risky to play with those
1027 * two this late in game for a shutdown problem. Instead,
1028 * just check here and get out. (ekn)
1031 FSDBG(530, myrep
->r_xid
, myrep
, nmp
, -3);
1032 return (ECONNABORTED
); /* sounds good */
1036 * Get the xid and check that it is an rpc reply
1039 dpos
= mtod(md
, caddr_t
);
1040 nfsm_dissect(tl
, u_long
*, 2*NFSX_UNSIGNED
);
1042 if (*tl
!= rpc_reply
) {
1043 #ifndef NFS_NOSERVER
1044 if (nmp
->nm_flag
& NFSMNT_NQNFS
) {
1045 if (nqnfs_callback(nmp
, mrep
, md
, dpos
))
1046 nfsstats
.rpcinvalid
++;
1048 nfsstats
.rpcinvalid
++;
1052 nfsstats
.rpcinvalid
++;
1056 if (nmp
->nm_flag
& NFSMNT_RCVLOCK
)
1057 nfs_rcvunlock(&nmp
->nm_flag
);
1058 if (myrep
->r_flags
& R_GETONEREP
)
1059 return (0); /* this path used by NQNFS */
1064 * Loop through the request list to match up the reply
1065 * Iff no match, just drop the datagram
1067 for (rep
= nfs_reqq
.tqh_first
; rep
!= 0;
1068 rep
= rep
->r_chain
.tqe_next
) {
1069 if (rep
->r_mrep
== NULL
&& rxid
== rep
->r_xid
) {
1077 rt
= &nfsrtt
.rttl
[nfsrtt
.pos
];
1078 rt
->proc
= rep
->r_procnum
;
1079 rt
->rto
= NFS_RTO(nmp
, proct
[rep
->r_procnum
]);
1080 rt
->sent
= nmp
->nm_sent
;
1081 rt
->cwnd
= nmp
->nm_cwnd
;
1082 if (proct
[rep
->r_procnum
] == 0)
1083 panic("nfs_reply: proct[%d] is zero", rep
->r_procnum
);
1084 rt
->srtt
= nmp
->nm_srtt
[proct
[rep
->r_procnum
] - 1];
1085 rt
->sdrtt
= nmp
->nm_sdrtt
[proct
[rep
->r_procnum
] - 1];
1086 rt
->fsid
= nmp
->nm_mountp
->mnt_stat
.f_fsid
;
1088 if (rep
->r_flags
& R_TIMING
)
1089 rt
->rtt
= rep
->r_rtt
;
1092 nfsrtt
.pos
= (nfsrtt
.pos
+ 1) % NFSRTTLOGSIZ
;
1095 * Update congestion window.
1096 * Do the additive increase of
1099 FSDBG(530, rep
->r_xid
, rep
, nmp
->nm_sent
,
1101 if (nmp
->nm_cwnd
<= nmp
->nm_sent
) {
1103 (NFS_CWNDSCALE
* NFS_CWNDSCALE
+
1104 (nmp
->nm_cwnd
>> 1)) / nmp
->nm_cwnd
;
1105 if (nmp
->nm_cwnd
> NFS_MAXCWND
)
1106 nmp
->nm_cwnd
= NFS_MAXCWND
;
1108 if (!(rep
->r_flags
& R_SENT
))
1109 printf("nfs_reply: unsent xid=%x",
1111 rep
->r_flags
&= ~R_SENT
;
1112 nmp
->nm_sent
-= NFS_CWNDSCALE
;
1114 * Update rtt using a gain of 0.125 on the mean
1115 * and a gain of 0.25 on the deviation.
1117 if (rep
->r_flags
& R_TIMING
) {
1119 * Since the timer resolution of
1120 * NFS_HZ is so course, it can often
1121 * result in r_rtt == 0. Since
1122 * r_rtt == N means that the actual
1123 * rtt is between N+dt and N+2-dt ticks,
1126 if (proct
[rep
->r_procnum
] == 0)
1127 panic("nfs_reply: proct[%d] is zero", rep
->r_procnum
);
1128 t1
= rep
->r_rtt
+ 1;
1129 t1
-= (NFS_SRTT(rep
) >> 3);
1130 NFS_SRTT(rep
) += t1
;
1133 t1
-= (NFS_SDRTT(rep
) >> 2);
1134 NFS_SDRTT(rep
) += t1
;
1136 nmp
->nm_timeouts
= 0;
1140 nfs_rcvunlock(&nmp
->nm_flag
);
1142 * If not matched to a request, drop it.
1143 * If it's mine, get out.
1146 nfsstats
.rpcunexpected
++;
1148 } else if (rep
== myrep
) {
1149 if (rep
->r_mrep
== NULL
)
1150 panic("nfs_reply: nil r_mrep");
1153 FSDBG(530, myrep
->r_xid
, myrep
, rep
,
1154 rep
? rep
->r_xid
: myrep
->r_flags
);
1155 if (myrep
->r_flags
& R_GETONEREP
)
1156 return (0); /* this path used by NQNFS */
1161 * nfs_request - goes something like this
1162 * - fill in request struct
1163 * - links it into list
1164 * - calls nfs_send() for first transmit
1165 * - calls nfs_receive() to get reply
1166 * - break down rpc header and return with nfs reply pointed to
1168 * nb: always frees up mreq mbuf list
1171 nfs_request(vp
, mrest
, procnum
, procp
, cred
, mrp
, mdp
, dposp
, xidp
)
1182 register struct mbuf
*m
, *mrep
;
1183 register struct nfsreq
*rep
, *rp
;
1184 register u_long
*tl
;
1186 struct nfsmount
*nmp
;
1187 struct mbuf
*md
, *mheadend
;
1189 char nickv
[RPCX_NICKVERF
];
1190 time_t reqtime
, waituntil
;
1192 int t1
, nqlflag
, cachable
, s
, error
= 0, mrest_len
, auth_len
, auth_type
;
1193 int trylater_delay
= NQ_TRYLATERDEL
, trylater_cnt
= 0, failed_auth
= 0;
1194 int verf_len
, verf_type
;
1197 char *auth_str
, *verf_str
;
1198 NFSKERBKEY_T key
; /* save session key */
1202 nmp
= VFSTONFS(vp
->v_mount
);
1203 MALLOC_ZONE(rep
, struct nfsreq
*,
1204 sizeof(struct nfsreq
), M_NFSREQ
, M_WAITOK
);
1205 FSDBG_TOP(531, vp
, procnum
, nmp
, rep
);
1208 * make sure if we blocked above, that the file system didn't get
1209 * unmounted leaving nmp bogus value to trip on later and crash.
1210 * Note nfs_unmount will set rep->r_nmp if unmounted volume, but we
1211 * aren't that far yet. SO this is best we can do. I wanted to check
1212 * for vp->v_mount = 0 also below, but that caused reboot crash.
1213 * Something must think it's okay for vp-v_mount=0 during booting.
1214 * Thus the best I can do here is see if we still have a vnode.
1217 if (vp
->v_type
== VBAD
) {
1218 FSDBG_BOT(531, 1, vp
, nmp
, rep
);
1219 _FREE_ZONE((caddr_t
)rep
, sizeof (struct nfsreq
), M_NFSREQ
);
1224 rep
->r_procp
= procp
;
1225 rep
->r_procnum
= procnum
;
1235 * Get the RPC header with authorization.
1238 verf_str
= auth_str
= (char *)0;
1239 if (nmp
->nm_flag
& NFSMNT_KERB
) {
1241 verf_len
= sizeof (nickv
);
1242 auth_type
= RPCAUTH_KERB4
;
1243 bzero((caddr_t
)key
, sizeof (key
));
1244 if (failed_auth
|| nfs_getnickauth(nmp
, cred
, &auth_str
,
1245 &auth_len
, verf_str
, verf_len
)) {
1246 error
= nfs_getauth(nmp
, rep
, cred
, &auth_str
,
1247 &auth_len
, verf_str
, &verf_len
, key
);
1249 FSDBG_BOT(531, 2, vp
, error
, rep
);
1250 _FREE_ZONE((caddr_t
)rep
,
1251 sizeof (struct nfsreq
), M_NFSREQ
);
1257 auth_type
= RPCAUTH_UNIX
;
1258 if (cred
->cr_ngroups
< 1)
1259 panic("nfsreq nogrps");
1260 auth_len
= ((((cred
->cr_ngroups
- 1) > nmp
->nm_numgrps
) ?
1261 nmp
->nm_numgrps
: (cred
->cr_ngroups
- 1)) << 2) +
1264 m
= nfsm_rpchead(cred
, nmp
->nm_flag
, procnum
, auth_type
, auth_len
,
1265 auth_str
, verf_len
, verf_str
, mrest
, mrest_len
, &mheadend
, &xid
);
1267 *xidp
= ntohl(xid
) + ((u_int64_t
)nfs_xidwrap
<< 32);
1269 _FREE(auth_str
, M_TEMP
);
1272 * For stream protocols, insert a Sun RPC Record Mark.
1274 if (nmp
->nm_sotype
== SOCK_STREAM
) {
1275 M_PREPEND(m
, NFSX_UNSIGNED
, M_WAIT
);
1276 *mtod(m
, u_long
*) = htonl(0x80000000 |
1277 (m
->m_pkthdr
.len
- NFSX_UNSIGNED
));
1282 if (nmp
->nm_flag
& NFSMNT_SOFT
)
1283 rep
->r_retry
= nmp
->nm_retry
;
1285 rep
->r_retry
= NFS_MAXREXMIT
+ 1; /* past clip limit */
1286 rep
->r_rtt
= rep
->r_rexmit
= 0;
1287 if (proct
[procnum
] > 0)
1288 rep
->r_flags
= R_TIMING
;
1294 * Do the client side RPC.
1296 nfsstats
.rpcrequests
++;
1298 * Chain request into list of outstanding requests. Be sure
1299 * to put it LAST so timer finds oldest requests first.
1302 TAILQ_INSERT_TAIL(&nfs_reqq
, rep
, r_chain
);
1304 /* Get send time for nqnfs */
1305 reqtime
= time
.tv_sec
;
1308 * If backing off another request or avoiding congestion, don't
1309 * send this one now but let timer do it. If not timing a request,
1312 if (nmp
->nm_so
&& (nmp
->nm_sotype
!= SOCK_DGRAM
||
1313 (nmp
->nm_flag
& NFSMNT_DUMBTIMR
) ||
1314 nmp
->nm_sent
< nmp
->nm_cwnd
)) {
1316 if (nmp
->nm_soflags
& PR_CONNREQUIRED
)
1317 error
= nfs_sndlock(&nmp
->nm_flag
, rep
);
1320 * Set the R_SENT before doing the send in case another thread
1321 * processes the reply before the nfs_send returns here
1324 if ((rep
->r_flags
& R_MUSTRESEND
) == 0) {
1325 FSDBG(531, rep
->r_xid
, rep
, nmp
->nm_sent
,
1327 nmp
->nm_sent
+= NFS_CWNDSCALE
;
1328 rep
->r_flags
|= R_SENT
;
1331 m
= m_copym(m
, 0, M_COPYALL
, M_WAIT
);
1332 error
= nfs_send(nmp
->nm_so
, nmp
->nm_nam
, m
, rep
);
1333 if (nmp
->nm_soflags
& PR_CONNREQUIRED
)
1334 nfs_sndunlock(&nmp
->nm_flag
);
1337 nmp
->nm_sent
-= NFS_CWNDSCALE
;
1338 rep
->r_flags
&= ~R_SENT
;
1346 * Wait for the reply from our send or the timer's.
1348 if (!error
|| error
== EPIPE
)
1349 error
= nfs_reply(rep
);
1352 * RPC done, unlink the request.
1355 for (rp
= nfs_reqq
.tqh_first
; rp
;
1356 rp
= rp
->r_chain
.tqe_next
)
1357 if (rp
== rep
&& rp
->r_xid
== xid
)
1360 panic("nfs_request race, rep %x xid %x", rep
, xid
);
1361 TAILQ_REMOVE(&nfs_reqq
, rep
, r_chain
);
1365 * Decrement the outstanding request count.
1367 if (rep
->r_flags
& R_SENT
) {
1368 FSDBG(531, rep
->r_xid
, rep
, nmp
->nm_sent
, nmp
->nm_cwnd
);
1369 rep
->r_flags
&= ~R_SENT
; /* paranoia */
1370 nmp
->nm_sent
-= NFS_CWNDSCALE
;
1374 * If there was a successful reply and a tprintf msg.
1375 * tprintf a response.
1377 if (!error
&& (rep
->r_flags
& R_TPRINTFMSG
))
1378 nfs_msg(rep
->r_procp
, nmp
->nm_mountp
->mnt_stat
.f_mntfromname
,
1384 m_freem(rep
->r_mreq
);
1385 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1386 _FREE_ZONE((caddr_t
)rep
, sizeof (struct nfsreq
), M_NFSREQ
);
1391 * break down the rpc header and check if ok
1393 nfsm_dissect(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
1394 if (*tl
++ == rpc_msgdenied
) {
1395 if (*tl
== rpc_mismatch
)
1397 else if ((nmp
->nm_flag
& NFSMNT_KERB
) && *tl
++ == rpc_autherr
) {
1400 mheadend
->m_next
= (struct mbuf
*)0;
1402 m_freem(rep
->r_mreq
);
1409 m_freem(rep
->r_mreq
);
1410 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1411 _FREE_ZONE((caddr_t
)rep
, sizeof (struct nfsreq
), M_NFSREQ
);
1416 * Grab any Kerberos verifier, otherwise just throw it away.
1418 verf_type
= fxdr_unsigned(int, *tl
++);
1419 i
= fxdr_unsigned(int, *tl
);
1420 if ((nmp
->nm_flag
& NFSMNT_KERB
) && verf_type
== RPCAUTH_KERB4
) {
1421 error
= nfs_savenickauth(nmp
, cred
, i
, key
, &md
, &dpos
, mrep
);
1425 nfsm_adv(nfsm_rndup(i
));
1426 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
1429 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
1431 error
= fxdr_unsigned(int, *tl
);
1432 if ((nmp
->nm_flag
& NFSMNT_NFSV3
) &&
1433 error
== NFSERR_TRYLATER
) {
1436 waituntil
= time
.tv_sec
+ trylater_delay
;
1438 ("nfs_request %s flag=%x trylater_cnt=%x waituntil=%lx trylater_delay=%x\n",
1439 nmp
->nm_mountp
->mnt_stat
.f_mntfromname
,
1440 nmp
->nm_flag
, trylater_cnt
, waituntil
,
1442 while (time
.tv_sec
< waituntil
)
1443 (void)tsleep((caddr_t
)&lbolt
,
1444 PSOCK
, "nqnfstry", 0);
1445 trylater_delay
*= nfs_backoff
[trylater_cnt
];
1446 if (trylater_cnt
< 7)
1452 * If the File Handle was stale, invalidate the
1453 * lookup cache, just in case.
1455 if (error
== ESTALE
)
1457 if (nmp
->nm_flag
& NFSMNT_NFSV3
) {
1461 error
|= NFSERR_RETERR
;
1464 m_freem(rep
->r_mreq
);
1465 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1466 _FREE_ZONE((caddr_t
)rep
,
1467 sizeof (struct nfsreq
), M_NFSREQ
);
1472 * For nqnfs, get any lease in reply
1474 if (nmp
->nm_flag
& NFSMNT_NQNFS
) {
1475 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
1478 nqlflag
= fxdr_unsigned(int, *tl
);
1479 nfsm_dissect(tl
, u_long
*, 4*NFSX_UNSIGNED
);
1480 cachable
= fxdr_unsigned(int, *tl
++);
1481 reqtime
+= fxdr_unsigned(int, *tl
++);
1482 if (reqtime
> time
.tv_sec
) {
1483 fxdr_hyper(tl
, &frev
);
1484 nqnfs_clientlease(nmp
, np
, nqlflag
,
1485 cachable
, reqtime
, frev
);
1492 m_freem(rep
->r_mreq
);
1493 FSDBG_BOT(531, 0xf0f0f0f0, rep
->r_xid
, nmp
, rep
);
1494 FREE_ZONE((caddr_t
)rep
, sizeof (struct nfsreq
), M_NFSREQ
);
1498 error
= EPROTONOSUPPORT
;
1500 m_freem(rep
->r_mreq
);
1501 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1502 _FREE_ZONE((caddr_t
)rep
, sizeof (struct nfsreq
), M_NFSREQ
);
1506 #ifndef NFS_NOSERVER
1508 * Generate the rpc reply header
1509 * siz arg. is used to decide if adding a cluster is worthwhile
1512 nfs_rephead(siz
, nd
, slp
, err
, cache
, frev
, mrq
, mbp
, bposp
)
1514 struct nfsrv_descript
*nd
;
1515 struct nfssvc_sock
*slp
;
1523 register u_long
*tl
;
1524 register struct mbuf
*mreq
;
1526 struct mbuf
*mb
, *mb2
;
1528 MGETHDR(mreq
, M_WAIT
, MT_DATA
);
1531 * If this is a big reply, use a cluster else
1532 * try and leave leading space for the lower level headers.
1534 siz
+= RPC_REPLYSIZ
;
1535 if (siz
>= MINCLSIZE
) {
1536 MCLGET(mreq
, M_WAIT
);
1538 mreq
->m_data
+= max_hdr
;
1539 tl
= mtod(mreq
, u_long
*);
1540 mreq
->m_len
= 6 * NFSX_UNSIGNED
;
1541 bpos
= ((caddr_t
)tl
) + mreq
->m_len
;
1542 *tl
++ = txdr_unsigned(nd
->nd_retxid
);
1544 if (err
== ERPCMISMATCH
|| (err
& NFSERR_AUTHERR
)) {
1545 *tl
++ = rpc_msgdenied
;
1546 if (err
& NFSERR_AUTHERR
) {
1547 *tl
++ = rpc_autherr
;
1548 *tl
= txdr_unsigned(err
& ~NFSERR_AUTHERR
);
1549 mreq
->m_len
-= NFSX_UNSIGNED
;
1550 bpos
-= NFSX_UNSIGNED
;
1552 *tl
++ = rpc_mismatch
;
1553 *tl
++ = txdr_unsigned(RPC_VER2
);
1554 *tl
= txdr_unsigned(RPC_VER2
);
1557 *tl
++ = rpc_msgaccepted
;
1560 * For Kerberos authentication, we must send the nickname
1561 * verifier back, otherwise just RPCAUTH_NULL.
1563 if (nd
->nd_flag
& ND_KERBFULL
) {
1564 register struct nfsuid
*nuidp
;
1565 struct timeval ktvin
, ktvout
;
1567 for (nuidp
= NUIDHASH(slp
, nd
->nd_cr
.cr_uid
)->lh_first
;
1568 nuidp
!= 0; nuidp
= nuidp
->nu_hash
.le_next
) {
1569 if (nuidp
->nu_cr
.cr_uid
== nd
->nd_cr
.cr_uid
&&
1570 (!nd
->nd_nam2
|| netaddr_match(NU_NETFAM(nuidp
),
1571 &nuidp
->nu_haddr
, nd
->nd_nam2
)))
1576 txdr_unsigned(nuidp
->nu_timestamp
.tv_sec
- 1);
1578 txdr_unsigned(nuidp
->nu_timestamp
.tv_usec
);
1581 * Encrypt the timestamp in ecb mode using the
1588 *tl
++ = rpc_auth_kerb
;
1589 *tl
++ = txdr_unsigned(3 * NFSX_UNSIGNED
);
1590 *tl
= ktvout
.tv_sec
;
1591 nfsm_build(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
1592 *tl
++ = ktvout
.tv_usec
;
1593 *tl
++ = txdr_unsigned(nuidp
->nu_cr
.cr_uid
);
1604 *tl
= txdr_unsigned(RPC_PROGUNAVAIL
);
1607 *tl
= txdr_unsigned(RPC_PROGMISMATCH
);
1608 nfsm_build(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
1609 if (nd
->nd_flag
& ND_NQNFS
) {
1610 *tl
++ = txdr_unsigned(3);
1611 *tl
= txdr_unsigned(3);
1613 *tl
++ = txdr_unsigned(2);
1614 *tl
= txdr_unsigned(3);
1618 *tl
= txdr_unsigned(RPC_PROCUNAVAIL
);
1621 *tl
= txdr_unsigned(RPC_GARBAGE
);
1625 if (err
!= NFSERR_RETVOID
) {
1626 nfsm_build(tl
, u_long
*, NFSX_UNSIGNED
);
1628 *tl
= txdr_unsigned(nfsrv_errmap(nd
, err
));
1637 * For nqnfs, piggyback lease as requested.
1639 if ((nd
->nd_flag
& ND_NQNFS
) && err
== 0) {
1640 if (nd
->nd_flag
& ND_LEASE
) {
1641 nfsm_build(tl
, u_long
*, 5 * NFSX_UNSIGNED
);
1642 *tl
++ = txdr_unsigned(nd
->nd_flag
& ND_LEASE
);
1643 *tl
++ = txdr_unsigned(cache
);
1644 *tl
++ = txdr_unsigned(nd
->nd_duration
);
1645 txdr_hyper(frev
, tl
);
1647 nfsm_build(tl
, u_long
*, NFSX_UNSIGNED
);
1655 if (err
!= 0 && err
!= NFSERR_RETVOID
)
1656 nfsstats
.srvrpc_errs
++;
1661 #endif /* NFS_NOSERVER */
1665 * From FreeBSD 1.58, a Matt Dillon fix...
1666 * Flag a request as being about to terminate.
1667 * The nm_sent count is decremented now to avoid deadlocks when the process
1668 * in soreceive() hasn't yet managed to send its own request.
1671 nfs_softterm(struct nfsreq
*rep
)
1673 rep
->r_flags
|= R_SOFTTERM
;
1674 if (rep
->r_flags
& R_SENT
) {
1675 FSDBG(532, rep
->r_xid
, rep
, rep
->r_nmp
->nm_sent
,
1676 rep
->r_nmp
->nm_cwnd
);
1677 rep
->r_nmp
->nm_sent
-= NFS_CWNDSCALE
;
1678 rep
->r_flags
&= ~R_SENT
;
1683 nfs_timer_funnel(arg
)
1686 (void) thread_funnel_set(kernel_flock
, TRUE
);
1688 (void) thread_funnel_set(kernel_flock
, FALSE
);
1694 * Scan the nfsreq list and retranmit any requests that have timed out
1695 * To avoid retransmission attempts on STREAM sockets (in the future) make
1696 * sure to set the r_retry field to 0 (implies nm_retry == 0).
1700 void *arg
; /* never used */
1702 register struct nfsreq
*rep
, *rp
;
1703 register struct mbuf
*m
;
1704 register struct socket
*so
;
1705 register struct nfsmount
*nmp
;
1708 #ifndef NFS_NOSERVER
1709 static long lasttime
= 0;
1710 register struct nfssvc_sock
*slp
;
1712 #endif /* NFS_NOSERVER */
1716 int flags
, rexmit
, cwnd
, sent
;
1721 * XXX If preemptable threads are implemented the spls used for the
1722 * outstanding request queue must be replaced with mutexes.
1725 #ifdef NFSTRACESUSPENDERS
1726 if (NFSTRACE_SUSPENDING
) {
1727 for (rep
= nfs_reqq
.tqh_first
; rep
!= 0;
1728 rep
= rep
->r_chain
.tqe_next
)
1729 if (rep
->r_xid
== nfstracexid
)
1733 } else if (NFSTRACE_SUSPENSEOVER
) {
1738 for (rep
= nfs_reqq
.tqh_first
; rep
!= 0; rep
= rep
->r_chain
.tqe_next
) {
1739 #ifdef NFSTRACESUSPENDERS
1740 if (rep
->r_mrep
&& !NFSTRACE_SUSPENDING
) {
1741 nfstracexid
= rep
->r_xid
;
1742 NFSTRACE_STARTSUSPENDCOUNTDOWN
;
1746 if (!nmp
) /* unmounted */
1748 if (rep
->r_mrep
|| (rep
->r_flags
& R_SOFTTERM
))
1750 if (nfs_sigintr(nmp
, rep
, rep
->r_procp
)) {
1754 if (rep
->r_rtt
>= 0) {
1756 if (nmp
->nm_flag
& NFSMNT_DUMBTIMR
)
1757 timeo
= nmp
->nm_timeo
;
1759 timeo
= NFS_RTO(nmp
, proct
[rep
->r_procnum
]);
1760 /* ensure 62.5 ms floor */
1761 while (16 * timeo
< hz
)
1763 if (nmp
->nm_timeouts
> 0)
1764 timeo
*= nfs_backoff
[nmp
->nm_timeouts
- 1];
1765 if (rep
->r_rtt
<= timeo
)
1767 if (nmp
->nm_timeouts
< 8)
1771 * Check for server not responding
1773 if ((rep
->r_flags
& R_TPRINTFMSG
) == 0 &&
1774 rep
->r_rexmit
> nmp
->nm_deadthresh
) {
1775 nfs_msg(rep
->r_procp
,
1776 nmp
->nm_mountp
->mnt_stat
.f_mntfromname
,
1778 rep
->r_flags
|= R_TPRINTFMSG
;
1780 if (rep
->r_rexmit
>= rep
->r_retry
) { /* too many */
1781 nfsstats
.rpctimeouts
++;
1785 if (nmp
->nm_sotype
!= SOCK_DGRAM
) {
1786 if (++rep
->r_rexmit
> NFS_MAXREXMIT
)
1787 rep
->r_rexmit
= NFS_MAXREXMIT
;
1790 if ((so
= nmp
->nm_so
) == NULL
)
1794 * If there is enough space and the window allows..
1796 * Set r_rtt to -1 in case we fail to send it now.
1799 rttdiag
= rep
->r_rtt
;
1802 if (sbspace(&so
->so_snd
) >= rep
->r_mreq
->m_pkthdr
.len
&&
1803 ((nmp
->nm_flag
& NFSMNT_DUMBTIMR
) ||
1804 (rep
->r_flags
& R_SENT
) ||
1805 nmp
->nm_sent
< nmp
->nm_cwnd
) &&
1806 (m
= m_copym(rep
->r_mreq
, 0, M_COPYALL
, M_DONTWAIT
))){
1808 struct proc
*p
= current_proc();
1811 if (rep
->r_flags
& R_SENT
&& nfsprnttimo
&&
1812 nmp
->nm_timeouts
>= nfsprnttimo
) {
1813 int t
= proct
[rep
->r_procnum
];
1815 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]));
1817 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
));
1820 #endif /* NFSDIAG */
1822 * Iff first send, start timing
1823 * else turn timing off, backoff timer
1824 * and divide congestion window by 2.
1825 * We update these *before* the send to avoid
1826 * racing against receiving the reply.
1827 * We save them so we can restore them on send error.
1829 flags
= rep
->r_flags
;
1830 rexmit
= rep
->r_rexmit
;
1831 cwnd
= nmp
->nm_cwnd
;
1832 sent
= nmp
->nm_sent
;
1834 if (rep
->r_flags
& R_SENT
) {
1835 rep
->r_flags
&= ~R_TIMING
;
1836 if (++rep
->r_rexmit
> NFS_MAXREXMIT
)
1837 rep
->r_rexmit
= NFS_MAXREXMIT
;
1839 if (nmp
->nm_cwnd
< NFS_CWNDSCALE
)
1840 nmp
->nm_cwnd
= NFS_CWNDSCALE
;
1841 nfsstats
.rpcretries
++;
1843 rep
->r_flags
|= R_SENT
;
1844 nmp
->nm_sent
+= NFS_CWNDSCALE
;
1846 FSDBG(535, xid
, rep
, nmp
->nm_sent
, nmp
->nm_cwnd
);
1848 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1850 if ((nmp
->nm_flag
& NFSMNT_NOCONN
) == 0)
1851 error
= (*so
->so_proto
->pr_usrreqs
->pru_send
)
1852 (so
, 0, m
, 0, 0, p
);
1854 error
= (*so
->so_proto
->pr_usrreqs
->pru_send
)
1855 (so
, 0, m
, mtod(nmp
->nm_nam
, struct sockaddr
*), 0, p
);
1857 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1859 FSDBG(535, xid
, error
, sent
, cwnd
);
1861 * This is to fix "nfs_sigintr" DSI panics.
1862 * We may have slept during the send so the current
1863 * place in the request queue may have been released.
1864 * Due to zone_gc it may even be part of an
1865 * unrelated newly allocated data structure.
1866 * Restart the list scan from the top if needed...
1868 for (rp
= nfs_reqq
.tqh_first
; rp
;
1869 rp
= rp
->r_chain
.tqe_next
)
1870 if (rp
== rep
&& rp
->r_xid
== xid
)
1875 panic("nfs_timer: race error %d xid 0x%x\n",
1880 if (NFSIGNORE_SOERROR(nmp
->nm_soflags
, error
))
1882 rep
->r_flags
= flags
;
1883 rep
->r_rexmit
= rexmit
;
1884 nmp
->nm_cwnd
= cwnd
;
1885 nmp
->nm_sent
= sent
;
1887 nfsstats
.rpcretries
--;
1892 #ifndef NFS_NOSERVER
1894 * Call the nqnfs server timer once a second to handle leases.
1896 if (lasttime
!= time
.tv_sec
) {
1897 lasttime
= time
.tv_sec
;
1902 * Scan the write gathering queues for writes that need to be
1905 cur_usec
= (u_quad_t
)time
.tv_sec
* 1000000 + (u_quad_t
)time
.tv_usec
;
1906 for (slp
= nfssvc_sockhead
.tqh_first
; slp
!= 0;
1907 slp
= slp
->ns_chain
.tqe_next
) {
1908 if (slp
->ns_tq
.lh_first
&& slp
->ns_tq
.lh_first
->nd_time
<=cur_usec
)
1909 nfsrv_wakenfsd(slp
);
1911 #endif /* NFS_NOSERVER */
1913 timeout(nfs_timer_funnel
, (void *)0, nfs_ticks
);
1919 * Test for a termination condition pending on the process.
1920 * This is used for NFSMNT_INT mounts.
1923 nfs_sigintr(nmp
, rep
, p
)
1924 struct nfsmount
*nmp
;
1926 register struct proc
*p
;
1931 ut
= (struct uthread
*)get_bsdthread_info(current_act());
1933 if (rep
&& (rep
->r_flags
& R_SOFTTERM
))
1935 if (!(nmp
->nm_flag
& NFSMNT_INT
))
1937 if (p
&& ut
&& ut
->uu_siglist
&&
1938 (((ut
->uu_siglist
& ~ut
->uu_sigmask
) & ~p
->p_sigignore
) &
1945 * Lock a socket against others.
1946 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
1947 * and also to avoid race conditions between the processes with nfs requests
1948 * in progress when a reconnect is necessary.
1951 nfs_sndlock(flagp
, rep
)
1952 register int *flagp
;
1956 int slpflag
= 0, slptimeo
= 0;
1960 if (rep
->r_nmp
->nm_flag
& NFSMNT_INT
)
1963 p
= (struct proc
*)0;
1964 while (*flagp
& NFSMNT_SNDLOCK
) {
1965 if (nfs_sigintr(rep
->r_nmp
, rep
, p
))
1967 *flagp
|= NFSMNT_WANTSND
;
1968 (void) tsleep((caddr_t
)flagp
, slpflag
| (PZERO
- 1), "nfsndlck",
1970 if (slpflag
== PCATCH
) {
1975 * Make sure while we slept that the mountpoint didn't go away.
1976 * nfs_sigintr and callers expect it in tact.
1979 return (ECONNABORTED
); /* don't have lock until out of loop */
1981 *flagp
|= NFSMNT_SNDLOCK
;
1986 * Unlock the stream socket for others.
1989 nfs_sndunlock(flagp
)
1990 register int *flagp
;
1993 if ((*flagp
& NFSMNT_SNDLOCK
) == 0)
1994 panic("nfs sndunlock");
1995 *flagp
&= ~NFSMNT_SNDLOCK
;
1996 if (*flagp
& NFSMNT_WANTSND
) {
1997 *flagp
&= ~NFSMNT_WANTSND
;
1998 wakeup((caddr_t
)flagp
);
2004 register struct nfsreq
*rep
;
2006 register int *flagp
;
2007 int slpflag
, slptimeo
= 0;
2009 /* make sure we still have our mountpoint */
2011 if (rep
->r_mrep
!= NULL
)
2013 return (ECONNABORTED
);
2016 flagp
= &rep
->r_nmp
->nm_flag
;
2017 FSDBG_TOP(534, rep
->r_xid
, rep
, rep
->r_nmp
, *flagp
);
2018 if (*flagp
& NFSMNT_INT
)
2022 while (*flagp
& NFSMNT_RCVLOCK
) {
2023 if (nfs_sigintr(rep
->r_nmp
, rep
, rep
->r_procp
)) {
2024 FSDBG_BOT(534, rep
->r_xid
, rep
, rep
->r_nmp
, 0x100);
2026 } else if (rep
->r_mrep
!= NULL
) {
2028 * Don't bother sleeping if reply already arrived
2030 FSDBG_BOT(534, rep
->r_xid
, rep
, rep
->r_nmp
, 0x101);
2033 FSDBG(534, rep
->r_xid
, rep
, rep
->r_nmp
, 0x102);
2034 *flagp
|= NFSMNT_WANTRCV
;
2035 (void) tsleep((caddr_t
)flagp
, slpflag
| (PZERO
- 1), "nfsrcvlk",
2037 if (slpflag
== PCATCH
) {
2042 * Make sure while we slept that the mountpoint didn't go away.
2043 * nfs_sigintr and caller nfs_reply expect it intact.
2046 FSDBG_BOT(534, rep
->r_xid
, rep
, rep
->r_nmp
, 0x103);
2047 return (ECONNABORTED
); /* don't have lock until out of loop */
2051 * nfs_reply will handle it if reply already arrived.
2052 * (We may have slept or been preempted while on network funnel).
2054 FSDBG_BOT(534, rep
->r_xid
, rep
, rep
->r_nmp
, *flagp
);
2055 *flagp
|= NFSMNT_RCVLOCK
;
2060 * Unlock the stream socket for others.
2063 nfs_rcvunlock(flagp
)
2064 register int *flagp
;
2067 FSDBG(533, flagp
, *flagp
, 0, 0);
2068 if ((*flagp
& NFSMNT_RCVLOCK
) == 0)
2069 panic("nfs rcvunlock");
2070 *flagp
&= ~NFSMNT_RCVLOCK
;
2071 if (*flagp
& NFSMNT_WANTRCV
) {
2072 *flagp
&= ~NFSMNT_WANTRCV
;
2073 wakeup((caddr_t
)flagp
);
2078 #ifndef NFS_NOSERVER
2080 * Socket upcall routine for the nfsd sockets.
2081 * The caddr_t arg is a pointer to the "struct nfssvc_sock".
2082 * Essentially do as much as possible non-blocking, else punt and it will
2083 * be called with M_WAIT from an nfsd.
2086 * Needs to eun under network funnel
2089 nfsrv_rcv(so
, arg
, waitflag
)
2094 register struct nfssvc_sock
*slp
= (struct nfssvc_sock
*)arg
;
2095 register struct mbuf
*m
;
2096 struct mbuf
*mp
, *mhck
;
2097 struct sockaddr
*nam
=0;
2100 struct sockaddr_in
*sin
;
2102 if ((slp
->ns_flag
& SLP_VALID
) == 0)
2106 * Define this to test for nfsds handling this under heavy load.
2108 if (waitflag
== M_DONTWAIT
) {
2109 slp
->ns_flag
|= SLP_NEEDQ
; goto dorecs
;
2112 auio
.uio_procp
= NULL
;
2113 if (so
->so_type
== SOCK_STREAM
) {
2115 * If there are already records on the queue, defer soreceive()
2116 * to an nfsd so that there is feedback to the TCP layer that
2117 * the nfs servers are heavily loaded.
2119 if (slp
->ns_rec
&& waitflag
== M_DONTWAIT
) {
2120 slp
->ns_flag
|= SLP_NEEDQ
;
2127 auio
.uio_resid
= 1000000000;
2128 flags
= MSG_DONTWAIT
;
2129 error
= soreceive(so
, (struct sockaddr
**) 0, &auio
, &mp
, (struct mbuf
**)0, &flags
);
2130 if (error
|| mp
== (struct mbuf
*)0) {
2131 if (error
== EWOULDBLOCK
)
2132 slp
->ns_flag
|= SLP_NEEDQ
;
2134 slp
->ns_flag
|= SLP_DISCONN
;
2138 if (slp
->ns_rawend
) {
2139 slp
->ns_rawend
->m_next
= m
;
2140 slp
->ns_cc
+= 1000000000 - auio
.uio_resid
;
2143 slp
->ns_cc
= 1000000000 - auio
.uio_resid
;
2150 * Now try and parse record(s) out of the raw stream data.
2152 error
= nfsrv_getstream(slp
, waitflag
);
2155 slp
->ns_flag
|= SLP_DISCONN
;
2157 slp
->ns_flag
|= SLP_NEEDQ
;
2161 auio
.uio_resid
= 1000000000;
2162 flags
= MSG_DONTWAIT
;
2164 error
= soreceive(so
, &nam
, &auio
, &mp
,
2165 (struct mbuf
**)0, &flags
);
2169 MGET(mhck
, M_WAIT
, MT_SONAME
);
2170 mhck
->m_len
= nam
->sa_len
;
2171 sin
= mtod(mhck
, struct sockaddr_in
*);
2172 bcopy(nam
, sin
, sizeof(struct sockaddr_in
));
2173 mhck
->m_hdr
.mh_len
= sizeof(struct sockaddr_in
);
2174 FREE(nam
, M_SONAME
);
2181 slp
->ns_recend
->m_nextpkt
= m
;
2185 m
->m_nextpkt
= (struct mbuf
*)0;
2188 if ((so
->so_proto
->pr_flags
& PR_CONNREQUIRED
)
2189 && error
!= EWOULDBLOCK
) {
2190 slp
->ns_flag
|= SLP_DISCONN
;
2198 * Now try and process the request records, non-blocking.
2201 if (waitflag
== M_DONTWAIT
&&
2202 (slp
->ns_rec
|| (slp
->ns_flag
& (SLP_NEEDQ
| SLP_DISCONN
)))) {
2203 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
2204 nfsrv_wakenfsd(slp
);
2205 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
2210 * Try and extract an RPC request from the mbuf data list received on a
2211 * stream socket. The "waitflag" argument indicates whether or not it
2215 nfsrv_getstream(slp
, waitflag
)
2216 register struct nfssvc_sock
*slp
;
2219 register struct mbuf
*m
, **mpp
;
2220 register char *cp1
, *cp2
;
2222 struct mbuf
*om
, *m2
, *recm
;
2225 if (slp
->ns_flag
& SLP_GETSTREAM
)
2226 panic("nfs getstream");
2227 slp
->ns_flag
|= SLP_GETSTREAM
;
2229 if (slp
->ns_reclen
== 0) {
2230 if (slp
->ns_cc
< NFSX_UNSIGNED
) {
2231 slp
->ns_flag
&= ~SLP_GETSTREAM
;
2235 if (m
->m_len
>= NFSX_UNSIGNED
) {
2236 bcopy(mtod(m
, caddr_t
), (caddr_t
)&recmark
, NFSX_UNSIGNED
);
2237 m
->m_data
+= NFSX_UNSIGNED
;
2238 m
->m_len
-= NFSX_UNSIGNED
;
2240 cp1
= (caddr_t
)&recmark
;
2241 cp2
= mtod(m
, caddr_t
);
2242 while (cp1
< ((caddr_t
)&recmark
) + NFSX_UNSIGNED
) {
2243 while (m
->m_len
== 0) {
2245 cp2
= mtod(m
, caddr_t
);
2252 slp
->ns_cc
-= NFSX_UNSIGNED
;
2253 recmark
= ntohl(recmark
);
2254 slp
->ns_reclen
= recmark
& ~0x80000000;
2255 if (recmark
& 0x80000000)
2256 slp
->ns_flag
|= SLP_LASTFRAG
;
2258 slp
->ns_flag
&= ~SLP_LASTFRAG
;
2259 if (slp
->ns_reclen
< NFS_MINPACKET
|| slp
->ns_reclen
> NFS_MAXPACKET
) {
2260 slp
->ns_flag
&= ~SLP_GETSTREAM
;
2266 * Now get the record part.
2268 * Note that slp->ns_reclen may be 0. Linux sometimes
2269 * generates 0-length RPCs
2272 if (slp
->ns_cc
== slp
->ns_reclen
) {
2274 slp
->ns_raw
= slp
->ns_rawend
= (struct mbuf
*)0;
2275 slp
->ns_cc
= slp
->ns_reclen
= 0;
2276 } else if (slp
->ns_cc
> slp
->ns_reclen
) {
2279 om
= (struct mbuf
*)0;
2280 while (len
< slp
->ns_reclen
) {
2281 if ((len
+ m
->m_len
) > slp
->ns_reclen
) {
2282 m2
= m_copym(m
, 0, slp
->ns_reclen
- len
,
2290 m
->m_data
+= slp
->ns_reclen
- len
;
2291 m
->m_len
-= slp
->ns_reclen
- len
;
2292 len
= slp
->ns_reclen
;
2294 slp
->ns_flag
&= ~SLP_GETSTREAM
;
2295 return (EWOULDBLOCK
);
2297 } else if ((len
+ m
->m_len
) == slp
->ns_reclen
) {
2302 om
->m_next
= (struct mbuf
*)0;
2313 slp
->ns_flag
&= ~SLP_GETSTREAM
;
2318 * Accumulate the fragments into a record.
2320 mpp
= &slp
->ns_frag
;
2322 mpp
= &((*mpp
)->m_next
);
2324 if (slp
->ns_flag
& SLP_LASTFRAG
) {
2326 slp
->ns_recend
->m_nextpkt
= slp
->ns_frag
;
2328 slp
->ns_rec
= slp
->ns_frag
;
2329 slp
->ns_recend
= slp
->ns_frag
;
2330 slp
->ns_frag
= (struct mbuf
*)0;
2336 * Parse an RPC header.
2339 nfsrv_dorec(slp
, nfsd
, ndp
)
2340 register struct nfssvc_sock
*slp
;
2342 struct nfsrv_descript
**ndp
;
2344 register struct mbuf
*m
;
2345 register struct mbuf
*nam
;
2346 register struct nfsrv_descript
*nd
;
2350 if ((slp
->ns_flag
& SLP_VALID
) == 0 ||
2351 (m
= slp
->ns_rec
) == (struct mbuf
*)0)
2353 slp
->ns_rec
= m
->m_nextpkt
;
2355 m
->m_nextpkt
= (struct mbuf
*)0;
2357 slp
->ns_recend
= (struct mbuf
*)0;
2358 if (m
->m_type
== MT_SONAME
) {
2364 MALLOC_ZONE(nd
, struct nfsrv_descript
*,
2365 sizeof (struct nfsrv_descript
), M_NFSRVDESC
, M_WAITOK
);
2366 nd
->nd_md
= nd
->nd_mrep
= m
;
2368 nd
->nd_dpos
= mtod(m
, caddr_t
);
2369 error
= nfs_getreq(nd
, nfsd
, TRUE
);
2372 _FREE_ZONE((caddr_t
)nd
, sizeof *nd
, M_NFSRVDESC
);
2381 * Parse an RPC request
2383 * - fill in the cred struct.
2386 nfs_getreq(nd
, nfsd
, has_header
)
2387 register struct nfsrv_descript
*nd
;
2391 register int len
, i
;
2392 register u_long
*tl
;
2396 caddr_t dpos
, cp2
, cp
;
2397 u_long nfsvers
, auth_type
;
2399 int error
= 0, nqnfs
= 0, ticklen
;
2400 struct mbuf
*mrep
, *md
;
2401 register struct nfsuid
*nuidp
;
2402 struct timeval tvin
, tvout
;
2403 #if 0 /* until encrypted keys are implemented */
2404 NFSKERBKEYSCHED_T keys
; /* stores key schedule */
2411 nfsm_dissect(tl
, u_long
*, 10 * NFSX_UNSIGNED
);
2412 nd
->nd_retxid
= fxdr_unsigned(u_long
, *tl
++);
2413 if (*tl
++ != rpc_call
) {
2418 nfsm_dissect(tl
, u_long
*, 8 * NFSX_UNSIGNED
);
2421 if (*tl
++ != rpc_vers
) {
2422 nd
->nd_repstat
= ERPCMISMATCH
;
2423 nd
->nd_procnum
= NFSPROC_NOOP
;
2426 if (*tl
!= nfs_prog
) {
2427 if (*tl
== nqnfs_prog
)
2430 nd
->nd_repstat
= EPROGUNAVAIL
;
2431 nd
->nd_procnum
= NFSPROC_NOOP
;
2436 nfsvers
= fxdr_unsigned(u_long
, *tl
++);
2437 if (((nfsvers
< NFS_VER2
|| nfsvers
> NFS_VER3
) && !nqnfs
) ||
2438 (nfsvers
!= NQNFS_VER3
&& nqnfs
)) {
2439 nd
->nd_repstat
= EPROGMISMATCH
;
2440 nd
->nd_procnum
= NFSPROC_NOOP
;
2444 nd
->nd_flag
= (ND_NFSV3
| ND_NQNFS
);
2445 else if (nfsvers
== NFS_VER3
)
2446 nd
->nd_flag
= ND_NFSV3
;
2447 nd
->nd_procnum
= fxdr_unsigned(u_long
, *tl
++);
2448 if (nd
->nd_procnum
== NFSPROC_NULL
)
2450 if (nd
->nd_procnum
>= NFS_NPROCS
||
2451 (!nqnfs
&& nd
->nd_procnum
>= NQNFSPROC_GETLEASE
) ||
2452 (!nd
->nd_flag
&& nd
->nd_procnum
> NFSV2PROC_STATFS
)) {
2453 nd
->nd_repstat
= EPROCUNAVAIL
;
2454 nd
->nd_procnum
= NFSPROC_NOOP
;
2457 if ((nd
->nd_flag
& ND_NFSV3
) == 0)
2458 nd
->nd_procnum
= nfsv3_procid
[nd
->nd_procnum
];
2460 len
= fxdr_unsigned(int, *tl
++);
2461 if (len
< 0 || len
> RPCAUTH_MAXSIZ
) {
2466 nd
->nd_flag
&= ~ND_KERBAUTH
;
2468 * Handle auth_unix or auth_kerb.
2470 if (auth_type
== rpc_auth_unix
) {
2471 len
= fxdr_unsigned(int, *++tl
);
2472 if (len
< 0 || len
> NFS_MAXNAMLEN
) {
2476 nfsm_adv(nfsm_rndup(len
));
2477 nfsm_dissect(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
2478 bzero((caddr_t
)&nd
->nd_cr
, sizeof (struct ucred
));
2479 nd
->nd_cr
.cr_ref
= 1;
2480 nd
->nd_cr
.cr_uid
= fxdr_unsigned(uid_t
, *tl
++);
2481 nd
->nd_cr
.cr_gid
= fxdr_unsigned(gid_t
, *tl
++);
2482 len
= fxdr_unsigned(int, *tl
);
2483 if (len
< 0 || len
> RPCAUTH_UNIXGIDS
) {
2487 nfsm_dissect(tl
, u_long
*, (len
+ 2) * NFSX_UNSIGNED
);
2488 for (i
= 1; i
<= len
; i
++)
2490 nd
->nd_cr
.cr_groups
[i
] = fxdr_unsigned(gid_t
, *tl
++);
2493 nd
->nd_cr
.cr_ngroups
= (len
>= NGROUPS
) ? NGROUPS
: (len
+ 1);
2494 if (nd
->nd_cr
.cr_ngroups
> 1)
2495 nfsrvw_sort(nd
->nd_cr
.cr_groups
, nd
->nd_cr
.cr_ngroups
);
2496 len
= fxdr_unsigned(int, *++tl
);
2497 if (len
< 0 || len
> RPCAUTH_MAXSIZ
) {
2502 nfsm_adv(nfsm_rndup(len
));
2503 } else if (auth_type
== rpc_auth_kerb
) {
2504 switch (fxdr_unsigned(int, *tl
++)) {
2505 case RPCAKN_FULLNAME
:
2506 ticklen
= fxdr_unsigned(int, *tl
);
2507 *((u_long
*)nfsd
->nfsd_authstr
) = *tl
;
2508 uio
.uio_resid
= nfsm_rndup(ticklen
) + NFSX_UNSIGNED
;
2509 nfsd
->nfsd_authlen
= uio
.uio_resid
+ NFSX_UNSIGNED
;
2510 if (uio
.uio_resid
> (len
- 2 * NFSX_UNSIGNED
)) {
2517 uio
.uio_segflg
= UIO_SYSSPACE
;
2518 iov
.iov_base
= (caddr_t
)&nfsd
->nfsd_authstr
[4];
2519 iov
.iov_len
= RPCAUTH_MAXSIZ
- 4;
2520 nfsm_mtouio(&uio
, uio
.uio_resid
);
2521 nfsm_dissect(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
2522 if (*tl
++ != rpc_auth_kerb
||
2523 fxdr_unsigned(int, *tl
) != 4 * NFSX_UNSIGNED
) {
2524 printf("Bad kerb verifier\n");
2525 nd
->nd_repstat
= (NFSERR_AUTHERR
|AUTH_BADVERF
);
2526 nd
->nd_procnum
= NFSPROC_NOOP
;
2529 nfsm_dissect(cp
, caddr_t
, 4 * NFSX_UNSIGNED
);
2531 if (fxdr_unsigned(int, *tl
) != RPCAKN_FULLNAME
) {
2532 printf("Not fullname kerb verifier\n");
2533 nd
->nd_repstat
= (NFSERR_AUTHERR
|AUTH_BADVERF
);
2534 nd
->nd_procnum
= NFSPROC_NOOP
;
2537 cp
+= NFSX_UNSIGNED
;
2538 bcopy(cp
, nfsd
->nfsd_verfstr
, 3 * NFSX_UNSIGNED
);
2539 nfsd
->nfsd_verflen
= 3 * NFSX_UNSIGNED
;
2540 nd
->nd_flag
|= ND_KERBFULL
;
2541 nfsd
->nfsd_flag
|= NFSD_NEEDAUTH
;
2543 case RPCAKN_NICKNAME
:
2544 if (len
!= 2 * NFSX_UNSIGNED
) {
2545 printf("Kerb nickname short\n");
2546 nd
->nd_repstat
= (NFSERR_AUTHERR
|AUTH_BADCRED
);
2547 nd
->nd_procnum
= NFSPROC_NOOP
;
2550 nickuid
= fxdr_unsigned(uid_t
, *tl
);
2551 nfsm_dissect(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
2552 if (*tl
++ != rpc_auth_kerb
||
2553 fxdr_unsigned(int, *tl
) != 3 * NFSX_UNSIGNED
) {
2554 printf("Kerb nick verifier bad\n");
2555 nd
->nd_repstat
= (NFSERR_AUTHERR
|AUTH_BADVERF
);
2556 nd
->nd_procnum
= NFSPROC_NOOP
;
2559 nfsm_dissect(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
2560 tvin
.tv_sec
= *tl
++;
2563 for (nuidp
= NUIDHASH(nfsd
->nfsd_slp
,nickuid
)->lh_first
;
2564 nuidp
!= 0; nuidp
= nuidp
->nu_hash
.le_next
) {
2565 if (nuidp
->nu_cr
.cr_uid
== nickuid
&&
2567 netaddr_match(NU_NETFAM(nuidp
),
2568 &nuidp
->nu_haddr
, nd
->nd_nam2
)))
2573 (NFSERR_AUTHERR
|AUTH_REJECTCRED
);
2574 nd
->nd_procnum
= NFSPROC_NOOP
;
2579 * Now, decrypt the timestamp using the session key
2586 tvout
.tv_sec
= fxdr_unsigned(long, tvout
.tv_sec
);
2587 tvout
.tv_usec
= fxdr_unsigned(long, tvout
.tv_usec
);
2588 if (nuidp
->nu_expire
< time
.tv_sec
||
2589 nuidp
->nu_timestamp
.tv_sec
> tvout
.tv_sec
||
2590 (nuidp
->nu_timestamp
.tv_sec
== tvout
.tv_sec
&&
2591 nuidp
->nu_timestamp
.tv_usec
> tvout
.tv_usec
)) {
2592 nuidp
->nu_expire
= 0;
2594 (NFSERR_AUTHERR
|AUTH_REJECTVERF
);
2595 nd
->nd_procnum
= NFSPROC_NOOP
;
2598 nfsrv_setcred(&nuidp
->nu_cr
, &nd
->nd_cr
);
2599 nd
->nd_flag
|= ND_KERBNICK
;
2602 nd
->nd_repstat
= (NFSERR_AUTHERR
| AUTH_REJECTCRED
);
2603 nd
->nd_procnum
= NFSPROC_NOOP
;
2608 * For nqnfs, get piggybacked lease request.
2610 if (nqnfs
&& nd
->nd_procnum
!= NQNFSPROC_EVICTED
) {
2611 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
2612 nd
->nd_flag
|= fxdr_unsigned(int, *tl
);
2613 if (nd
->nd_flag
& ND_LEASE
) {
2614 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
2615 nd
->nd_duration
= fxdr_unsigned(int, *tl
);
2617 nd
->nd_duration
= NQ_MINLEASE
;
2619 nd
->nd_duration
= NQ_MINLEASE
;
2628 * Search for a sleeping nfsd and wake it up.
2629 * SIDE EFFECT: If none found, set NFSD_CHECKSLP flag, so that one of the
2630 * running nfsds will go look for the work in the nfssvc_sock list.
2634 struct nfssvc_sock
*slp
;
2636 register struct nfsd
*nd
;
2638 if ((slp
->ns_flag
& SLP_VALID
) == 0)
2640 for (nd
= nfsd_head
.tqh_first
; nd
!= 0; nd
= nd
->nfsd_chain
.tqe_next
) {
2641 if (nd
->nfsd_flag
& NFSD_WAITING
) {
2642 nd
->nfsd_flag
&= ~NFSD_WAITING
;
2644 panic("nfsd wakeup");
2647 wakeup((caddr_t
)nd
);
2651 slp
->ns_flag
|= SLP_DOREC
;
2652 nfsd_head_flag
|= NFSD_CHECKSLP
;
2654 #endif /* NFS_NOSERVER */
2657 nfs_msg(p
, server
, msg
)
2664 tpr
= tprintf_open(p
);
2667 tprintf(tpr
, "nfs server %s: %s\n", server
, msg
);