2 * Copyright (c) 2000-2003 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
*, const char *, const char *, int));
182 static void nfs_up(struct nfsreq
*, const char *, int);
183 static void nfs_down(struct nfsreq
*, const char *, int);
184 static int nfs_rcvlock
__P((struct nfsreq
*));
185 static void nfs_rcvunlock
__P((struct nfsreq
*));
186 static int nfs_receive
__P((struct nfsreq
*rep
, struct mbuf
**aname
,
188 static int nfs_reconnect
__P((struct nfsreq
*rep
));
189 static void nfs_repbusy(struct nfsreq
*rep
);
190 static struct nfsreq
* nfs_repnext(struct nfsreq
*rep
);
191 static void nfs_repdequeue(struct nfsreq
*rep
);
193 static int nfsrv_getstream
__P((struct nfssvc_sock
*,int));
195 int (*nfsrv3_procs
[NFS_NPROCS
]) __P((struct nfsrv_descript
*nd
,
196 struct nfssvc_sock
*slp
,
198 struct mbuf
**mreqp
)) = {
226 #endif /* NFS_NOSERVER */
229 * NFSTRACE points were changed to FSDBG (KERNEL_DEBUG)
230 * But some of this code may prove useful someday...
234 int nfstraceindx
= 0;
235 struct nfstracerec nfstracebuf
[NFSTBUFSIZ
] = {{0,0,0,0}};
237 #define NFSTRACESUSPENDERS
238 #ifdef NFSTRACESUSPENDERS
239 uint nfstracemask
= 0xfff00200;
240 int nfstracexid
= -1;
241 uint onfstracemask
= 0;
242 int nfstracesuspend
= -1;
243 #define NFSTRACE_SUSPEND \
245 if (nfstracemask) { \
246 onfstracemask = nfstracemask; \
250 #define NFSTRACE_RESUME \
252 nfstracesuspend = -1; \
254 nfstracemask = onfstracemask; \
256 #define NFSTRACE_STARTSUSPENDCOUNTDOWN \
258 nfstracesuspend = (nfstraceindx+100) % NFSTBUFSIZ; \
260 #define NFSTRACE_SUSPENDING (nfstracesuspend != -1)
261 #define NFSTRACE_SUSPENSEOVER \
262 (nfstracesuspend > 100 ? \
263 (nfstraceindx >= nfstracesuspend || \
264 nfstraceindx < nfstracesuspend - 100) : \
265 (nfstraceindx >= nfstracesuspend && \
266 nfstraceindx < nfstracesuspend + 8192 - 100))
268 uint nfstracemask
= 0;
269 #endif /* NFSTRACESUSPENDERS */
274 int nfsoprocnum
, nfsolen
;
275 int nfsbt
[32], nfsbtlen
;
279 backtrace(int *where
, int size
)
281 int register sp
, *fp
, numsaved
;
283 __asm__
volatile("mr %0,r1" : "=r" (sp
));
285 fp
= (int *)*((int *)sp
);
287 for (numsaved
= 0; numsaved
< size
; numsaved
++) {
295 #elif defined(__i386__)
299 return (0); /* Till someone implements a real routine */
302 #error architecture not implemented.
306 nfsdup(struct nfsreq
*rep
)
308 int *ip
, i
, first
= 1, end
;
312 if ((nfs_debug
& NFS_DEBUG_DUP
) == 0)
314 /* last mbuf in chain will be nfs content */
315 for (mb
= rep
->r_mreq
; mb
->m_next
; mb
= mb
->m_next
)
317 if (rep
->r_procnum
== nfsoprocnum
&& mb
->m_len
== nfsolen
&&
318 !bcmp((caddr_t
)nfsodata
, mb
->m_data
, nfsolen
)) {
319 s
= b
+ sprintf(b
, "nfsdup x=%x p=%d h=", rep
->r_xid
,
321 end
= (int)(VTONFS(rep
->r_vp
)->n_fhp
);
322 ip
= (int *)(end
& ~3);
323 end
+= VTONFS(rep
->r_vp
)->n_fhsize
;
324 while ((int)ip
< end
) {
326 if (first
) { /* avoid leading zeroes */
330 s
+= sprintf(s
, "%x", i
);
332 s
+= sprintf(s
, "%08x", i
);
336 else /* eliminate trailing zeroes */
340 * set a breakpoint here and you can view the
341 * current backtrace and the one saved in nfsbt
345 nfsoprocnum
= rep
->r_procnum
;
347 bcopy(mb
->m_data
, (caddr_t
)nfsodata
, mb
->m_len
);
348 nfsbtlen
= backtrace(&nfsbt
, sizeof(nfsbt
));
353 * Initialize sockets and congestion for a new NFS connection.
354 * We do not free the sockaddr if error.
357 nfs_connect(nmp
, rep
)
358 register struct nfsmount
*nmp
;
361 register struct socket
*so
;
362 int s
, error
, rcvreserve
, sndreserve
;
363 struct sockaddr
*saddr
;
364 struct sockaddr_in sin
;
367 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
368 nmp
->nm_so
= (struct socket
*)0;
369 saddr
= mtod(nmp
->nm_nam
, struct sockaddr
*);
370 error
= socreate(saddr
->sa_family
, &nmp
->nm_so
, nmp
->nm_sotype
,
376 nmp
->nm_soflags
= so
->so_proto
->pr_flags
;
379 * Some servers require that the client port be a reserved port number.
381 if (saddr
->sa_family
== AF_INET
&& (nmp
->nm_flag
& NFSMNT_RESVPORT
)) {
382 sin
.sin_len
= sizeof (struct sockaddr_in
);
383 sin
.sin_family
= AF_INET
;
384 sin
.sin_addr
.s_addr
= INADDR_ANY
;
385 tport
= IPPORT_RESERVED
- 1;
386 sin
.sin_port
= htons(tport
);
388 while ((error
= sobind(so
, (struct sockaddr
*) &sin
) == EADDRINUSE
) &&
389 (--tport
> IPPORT_RESERVED
/ 2))
390 sin
.sin_port
= htons(tport
);
397 * Protocols that do not require connections may be optionally left
398 * unconnected for servers that reply from a port other than NFS_PORT.
400 if (nmp
->nm_flag
& NFSMNT_NOCONN
) {
401 if (nmp
->nm_soflags
& PR_CONNREQUIRED
) {
406 error
= soconnect(so
, mtod(nmp
->nm_nam
, struct sockaddr
*));
412 * Wait for the connection to complete. Cribbed from the
413 * connect system call but with the wait timing out so
414 * that interruptible mounts don't hang here for a long time.
417 while ((so
->so_state
& SS_ISCONNECTING
) && so
->so_error
== 0) {
418 (void) tsleep((caddr_t
)&so
->so_timeo
, PSOCK
,
420 if ((so
->so_state
& SS_ISCONNECTING
) &&
421 so
->so_error
== 0 && rep
&&
422 (error
= nfs_sigintr(nmp
, rep
, rep
->r_procp
))) {
423 so
->so_state
&= ~SS_ISCONNECTING
;
429 error
= so
->so_error
;
437 * Always time out on recieve, this allows us to reconnect the
438 * socket to deal with network changes.
440 so
->so_rcv
.sb_timeo
= (2 * hz
);
441 if (nmp
->nm_flag
& (NFSMNT_SOFT
| NFSMNT_INT
)) {
442 so
->so_snd
.sb_timeo
= (5 * hz
);
444 so
->so_snd
.sb_timeo
= 0;
446 if (nmp
->nm_sotype
== SOCK_DGRAM
) {
447 sndreserve
= (nmp
->nm_wsize
+ NFS_MAXPKTHDR
) * 3;
448 rcvreserve
= (nmp
->nm_rsize
+ NFS_MAXPKTHDR
) *
449 (nmp
->nm_readahead
> 0 ? nmp
->nm_readahead
+ 1 : 2);
450 } else if (nmp
->nm_sotype
== SOCK_SEQPACKET
) {
451 sndreserve
= (nmp
->nm_wsize
+ NFS_MAXPKTHDR
) * 3;
452 rcvreserve
= (nmp
->nm_rsize
+ NFS_MAXPKTHDR
) *
453 (nmp
->nm_readahead
> 0 ? nmp
->nm_readahead
+ 1 : 2);
455 if (nmp
->nm_sotype
!= SOCK_STREAM
)
456 panic("nfscon sotype");
458 if (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) {
462 bzero(&sopt
, sizeof sopt
);
463 sopt
.sopt_dir
= SOPT_SET
;
464 sopt
.sopt_level
= SOL_SOCKET
;
465 sopt
.sopt_name
= SO_KEEPALIVE
;
466 sopt
.sopt_val
= &val
;
467 sopt
.sopt_valsize
= sizeof val
;
471 if (so
->so_proto
->pr_protocol
== IPPROTO_TCP
) {
475 bzero(&sopt
, sizeof sopt
);
476 sopt
.sopt_dir
= SOPT_SET
;
477 sopt
.sopt_level
= IPPROTO_TCP
;
478 sopt
.sopt_name
= TCP_NODELAY
;
479 sopt
.sopt_val
= &val
;
480 sopt
.sopt_valsize
= sizeof val
;
485 sndreserve
= (nmp
->nm_wsize
+ NFS_MAXPKTHDR
+ sizeof (u_long
)) * 3;
486 rcvreserve
= (nmp
->nm_rsize
+ NFS_MAXPKTHDR
+ sizeof (u_long
)) *
487 (nmp
->nm_readahead
> 0 ? nmp
->nm_readahead
+ 1 : 2);
490 if (sndreserve
> NFS_MAXSOCKBUF
)
491 sndreserve
= NFS_MAXSOCKBUF
;
492 if (rcvreserve
> NFS_MAXSOCKBUF
)
493 rcvreserve
= NFS_MAXSOCKBUF
;
494 error
= soreserve(so
, sndreserve
, rcvreserve
);
498 so
->so_rcv
.sb_flags
|= SB_NOINTR
;
499 so
->so_snd
.sb_flags
|= SB_NOINTR
;
501 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
503 /* Initialize other non-zero congestion variables */
504 nmp
->nm_srtt
[0] = nmp
->nm_srtt
[1] = nmp
->nm_srtt
[2] =
505 nmp
->nm_srtt
[3] = (NFS_TIMEO
<< 3);
506 nmp
->nm_sdrtt
[0] = nmp
->nm_sdrtt
[1] = nmp
->nm_sdrtt
[2] =
507 nmp
->nm_sdrtt
[3] = 0;
508 nmp
->nm_cwnd
= NFS_MAXCWND
/ 2; /* Initial send window */
510 FSDBG(529, nmp
, nmp
->nm_state
, nmp
->nm_soflags
, nmp
->nm_cwnd
);
511 nmp
->nm_timeouts
= 0;
515 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
522 * Called when a connection is broken on a reliable protocol.
523 * - clean up the old socket
524 * - nfs_connect() again
525 * - set R_MUSTRESEND for all outstanding requests on mount point
526 * If this fails the mount point is DEAD!
527 * nb: Must be called with the nfs_sndlock() set on the mount point.
531 register struct nfsreq
*rep
;
533 register struct nfsreq
*rp
;
534 register struct nfsmount
*nmp
= rep
->r_nmp
;
538 while ((error
= nfs_connect(nmp
, rep
))) {
539 if (error
== EINTR
|| error
== ERESTART
)
543 nfs_down(rep
, "can not connect", error
);
544 (void) tsleep((caddr_t
)&lbolt
, PSOCK
, "nfscon", 0);
547 NFS_DPF(DUP
, ("nfs_reconnect RESEND\n"));
549 * Loop through outstanding request list and fix up all requests
552 TAILQ_FOREACH(rp
, &nfs_reqq
, r_chain
) {
553 if (rp
->r_nmp
== nmp
)
554 rp
->r_flags
|= R_MUSTRESEND
;
560 * NFS disconnect. Clean up and unlink.
564 register struct nfsmount
*nmp
;
566 register struct socket
*so
;
568 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
571 nmp
->nm_so
= (struct socket
*)0;
575 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
579 * This is the nfs send routine. For connection based socket types, it
580 * must be called with an nfs_sndlock() on the socket.
581 * "rep == NULL" indicates that it has been called from a server.
582 * For the client side:
583 * - return EINTR if the RPC is terminated, 0 otherwise
584 * - set R_MUSTRESEND if the send fails for any reason
585 * - do any cleanup required by recoverable socket errors (???)
586 * For the server side:
587 * - return EINTR or ERESTART if interrupted by a signal
588 * - return EPIPE if a connection is lost for connection based sockets (TCP...)
589 * - do any cleanup required by recoverable socket errors (???)
592 nfs_send(so
, nam
, top
, rep
)
593 register struct socket
*so
;
595 register struct mbuf
*top
;
598 struct sockaddr
*sendnam
;
599 int error
, error2
, soflags
, flags
;
602 char savenametolog
[MNAMELEN
];
605 error
= nfs_sigintr(rep
->r_nmp
, rep
, rep
->r_procp
);
610 if ((so
= rep
->r_nmp
->nm_so
) == NULL
) {
611 rep
->r_flags
|= R_MUSTRESEND
;
615 rep
->r_flags
&= ~R_MUSTRESEND
;
616 soflags
= rep
->r_nmp
->nm_soflags
;
617 TAILQ_FOREACH(rp
, &nfs_reqq
, r_chain
)
621 xidqueued
= rp
->r_xid
;
623 soflags
= so
->so_proto
->pr_flags
;
624 if ((soflags
& PR_CONNREQUIRED
) || (so
->so_state
& SS_ISCONNECTED
) ||
626 sendnam
= (struct sockaddr
*)0;
628 sendnam
= mtod(nam
, struct sockaddr
*);
630 if (so
->so_type
== SOCK_SEQPACKET
)
640 * Save the name here in case mount point goes away when we switch
641 * funnels. The name is using local stack and is large, but don't
642 * want to block if we malloc.
645 strncpy(savenametolog
,
646 rep
->r_nmp
->nm_mountp
->mnt_stat
.f_mntfromname
,
648 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
649 error
= sosend(so
, sendnam
, (struct uio
*)0, top
,
650 (struct mbuf
*)0, flags
);
651 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
656 TAILQ_FOREACH(rp
, &nfs_reqq
, r_chain
)
657 if (rp
== rep
&& rp
->r_xid
== xidqueued
)
660 panic("nfs_send: error %d xid %x gone",
663 log(LOG_INFO
, "nfs send error %d for server %s\n",
664 error
, savenametolog
);
666 * Deal with errors for the client side.
668 error2
= nfs_sigintr(rep
->r_nmp
, rep
, rep
->r_procp
);
672 rep
->r_flags
|= R_MUSTRESEND
;
674 ("nfs_send RESEND error=%d\n", error
));
677 log(LOG_INFO
, "nfsd send error %d\n", error
);
680 * Handle any recoverable (soft) socket errors here. (???)
682 if (error
!= EINTR
&& error
!= ERESTART
&& error
!= EIO
&&
683 error
!= EWOULDBLOCK
&& error
!= EPIPE
) {
691 * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all
692 * done by soreceive(), but for SOCK_STREAM we must deal with the Record
693 * Mark and consolidate the data into a new mbuf list.
694 * nb: Sometimes TCP passes the data up to soreceive() in long lists of
696 * For SOCK_STREAM we must be very careful to read an entire record once
697 * we have read any of it, even if the system call has been interrupted.
700 nfs_receive(rep
, aname
, mp
)
701 register struct nfsreq
*rep
;
705 register struct socket
*so
;
708 register struct mbuf
*m
;
709 struct mbuf
*control
;
711 struct sockaddr
**getnam
;
712 struct sockaddr
*tmp_nam
;
714 struct sockaddr_in
*sin
;
715 int error
, error2
, sotype
, rcvflg
;
716 struct proc
*p
= current_proc(); /* XXX */
719 * Set up arguments for soreceive()
721 *mp
= (struct mbuf
*)0;
722 *aname
= (struct mbuf
*)0;
723 sotype
= rep
->r_nmp
->nm_sotype
;
726 * For reliable protocols, lock against other senders/receivers
727 * in case a reconnect is necessary.
728 * For SOCK_STREAM, first get the Record Mark to find out how much
729 * more there is to get.
730 * We must lock the socket against other receivers
731 * until we have an entire rpc request/reply.
733 if (sotype
!= SOCK_DGRAM
) {
734 error
= nfs_sndlock(rep
);
739 * Check for fatal errors and resending request.
742 * Ugh: If a reconnect attempt just happened, nm_so
743 * would have changed. NULL indicates a failed
744 * attempt that has essentially shut down this
747 if ((error
= nfs_sigintr(rep
->r_nmp
, rep
, p
)) || rep
->r_mrep
) {
753 so
= rep
->r_nmp
->nm_so
;
755 error
= nfs_reconnect(rep
);
762 while (rep
->r_flags
& R_MUSTRESEND
) {
763 m
= m_copym(rep
->r_mreq
, 0, M_COPYALL
, M_WAIT
);
764 nfsstats
.rpcretries
++;
766 ("nfs_receive RESEND %s\n",
767 rep
->r_nmp
->nm_mountp
->mnt_stat
.f_mntfromname
));
768 error
= nfs_send(so
, rep
->r_nmp
->nm_nam
, m
, rep
);
770 * we also hold rcv lock so rep is still
774 if (error
== EINTR
|| error
== ERESTART
||
775 (error
= nfs_reconnect(rep
))) {
783 if (sotype
== SOCK_STREAM
) {
784 aio
.iov_base
= (caddr_t
) &len
;
785 aio
.iov_len
= sizeof(u_long
);
788 auio
.uio_segflg
= UIO_SYSSPACE
;
789 auio
.uio_rw
= UIO_READ
;
791 auio
.uio_resid
= sizeof(u_long
);
794 rcvflg
= MSG_WAITALL
;
795 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
796 error
= soreceive(so
, (struct sockaddr
**)0, &auio
,
797 (struct mbuf
**)0, (struct mbuf
**)0, &rcvflg
);
798 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
799 if (!rep
->r_nmp
) /* if unmounted then bailout */
801 if (error
== EWOULDBLOCK
&& rep
) {
802 error2
= nfs_sigintr(rep
->r_nmp
, rep
, p
);
806 } while (error
== EWOULDBLOCK
);
807 if (!error
&& auio
.uio_resid
> 0) {
809 "short receive (%d/%d) from nfs server %s\n",
810 sizeof(u_long
) - auio
.uio_resid
,
812 rep
->r_nmp
->nm_mountp
->mnt_stat
.f_mntfromname
);
817 len
= ntohl(len
) & ~0x80000000;
819 * This is SERIOUS! We are out of sync with the sender
820 * and forcing a disconnect/reconnect is all I can do.
822 if (len
> NFS_MAXPACKET
) {
823 log(LOG_ERR
, "%s (%d) from nfs server %s\n",
824 "impossible packet length",
826 rep
->r_nmp
->nm_mountp
->mnt_stat
.f_mntfromname
);
830 auio
.uio_resid
= len
;
832 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
834 rcvflg
= MSG_WAITALL
;
835 error
= soreceive(so
, (struct sockaddr
**)0,
836 &auio
, mp
, (struct mbuf
**)0, &rcvflg
);
837 if (!rep
->r_nmp
) /* if unmounted then bailout */ {
838 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
841 } while (error
== EWOULDBLOCK
|| error
== EINTR
||
844 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
846 if (!error
&& auio
.uio_resid
> 0) {
848 "short receive (%d/%d) from nfs server %s\n",
849 len
- auio
.uio_resid
, len
,
850 rep
->r_nmp
->nm_mountp
->mnt_stat
.f_mntfromname
);
855 * NB: Since uio_resid is big, MSG_WAITALL is ignored
856 * and soreceive() will return when it has either a
857 * control msg or a data msg.
858 * We have no use for control msg., but must grab them
859 * and then throw them away so we know what is going
862 auio
.uio_resid
= len
= 100000000; /* Anything Big */
865 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
868 error
= soreceive(so
, (struct sockaddr
**)0,
869 &auio
, mp
, &control
, &rcvflg
);
872 if (!rep
->r_nmp
) /* if unmounted then bailout */ {
873 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
876 if (error
== EWOULDBLOCK
&& rep
) {
877 error2
= nfs_sigintr(rep
->r_nmp
, rep
, p
);
879 thread_funnel_switch(NETWORK_FUNNEL
,
884 } while (error
== EWOULDBLOCK
||
885 (!error
&& *mp
== NULL
&& control
));
887 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
889 if ((rcvflg
& MSG_EOR
) == 0)
891 if (!error
&& *mp
== NULL
)
893 len
-= auio
.uio_resid
;
896 if (error
&& error
!= EINTR
&& error
!= ERESTART
) {
898 *mp
= (struct mbuf
*)0;
901 "receive error %d from nfs server %s\n",
903 rep
->r_nmp
->nm_mountp
->mnt_stat
.f_mntfromname
);
904 error
= nfs_sndlock(rep
);
906 error
= nfs_reconnect(rep
);
912 * We could have failed while rebinding the datagram socket
913 * so we need to attempt to rebind here.
915 if ((so
= rep
->r_nmp
->nm_so
) == NULL
) {
916 error
= nfs_sndlock(rep
);
918 error
= nfs_reconnect(rep
);
923 if (!rep
->r_nmp
) /* if unmounted then bailout */
925 so
= rep
->r_nmp
->nm_so
;
927 if (so
->so_state
& SS_ISCONNECTED
)
928 getnam
= (struct sockaddr
**)0;
931 auio
.uio_resid
= len
= 1000000;
934 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
937 error
= soreceive(so
, getnam
, &auio
, mp
,
938 (struct mbuf
**)0, &rcvflg
);
940 if ((getnam
) && (*getnam
)) {
941 MGET(mhck
, M_WAIT
, MT_SONAME
);
942 mhck
->m_len
= (*getnam
)->sa_len
;
943 sin
= mtod(mhck
, struct sockaddr_in
*);
944 bcopy(*getnam
, sin
, sizeof(struct sockaddr_in
));
945 mhck
->m_hdr
.mh_len
= sizeof(struct sockaddr_in
);
946 FREE(*getnam
, M_SONAME
);
949 if (!rep
->r_nmp
) /* if unmounted then bailout */
952 error2
= nfs_sigintr(rep
->r_nmp
, rep
, p
);
958 /* Reconnect for all errors. We may be receiving
959 * soft/hard/blocking errors because of a network
961 * XXX: we should rate limit or delay this
962 * to once every N attempts or something.
963 * although TCP doesn't seem to.
966 thread_funnel_switch(NETWORK_FUNNEL
,
968 error2
= nfs_sndlock(rep
);
970 error2
= nfs_reconnect(rep
);
973 else if (!rep
->r_nmp
) /* if unmounted then bailout */
976 so
= rep
->r_nmp
->nm_so
;
981 thread_funnel_switch(KERNEL_FUNNEL
,
984 } while (error
== EWOULDBLOCK
);
987 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
988 len
-= auio
.uio_resid
;
993 *mp
= (struct mbuf
*)0;
999 * Implement receipt of reply on a socket.
1000 * We must search through the list of received datagrams matching them
1001 * with outstanding requests using the xid, until ours is found.
1006 struct nfsreq
*myrep
;
1008 register struct nfsreq
*rep
;
1009 register struct nfsmount
*nmp
= myrep
->r_nmp
;
1011 struct mbuf
*mrep
, *md
;
1018 * Loop around until we get our own reply
1022 * Lock against other receivers so that I don't get stuck in
1023 * sbwait() after someone else has received my reply for me.
1024 * Also necessary for connection based protocols to avoid
1025 * race conditions during a reconnect.
1026 * If nfs_rcvlock() returns EALREADY, that means that
1027 * the reply has already been recieved by another
1028 * process and we can return immediately. In this
1029 * case, the lock is not taken to avoid races with
1032 error
= nfs_rcvlock(myrep
);
1033 if (error
== EALREADY
)
1039 * If we slept after putting bits otw, then reply may have
1040 * arrived. In which case returning is required, or we
1041 * would hang trying to nfs_receive an already received reply.
1043 if (myrep
->r_mrep
!= NULL
) {
1044 nfs_rcvunlock(myrep
);
1045 FSDBG(530, myrep
->r_xid
, myrep
, myrep
->r_nmp
, -1);
1049 * Get the next Rpc reply off the socket. Assume myrep->r_nmp
1050 * is still intact by checks done in nfs_rcvlock.
1052 error
= nfs_receive(myrep
, &nam
, &mrep
);
1056 * Bailout asap if nfsmount struct gone (unmounted).
1058 if (!myrep
->r_nmp
|| !nmp
->nm_so
) {
1059 FSDBG(530, myrep
->r_xid
, myrep
, nmp
, -2);
1063 FSDBG(530, myrep
->r_xid
, myrep
, nmp
, error
);
1064 nfs_rcvunlock(myrep
);
1066 /* Bailout asap if nfsmount struct gone (unmounted). */
1067 if (!myrep
->r_nmp
|| !nmp
->nm_so
)
1071 * Ignore routing errors on connectionless protocols??
1073 if (NFSIGNORE_SOERROR(nmp
->nm_soflags
, error
)) {
1074 nmp
->nm_so
->so_error
= 0;
1075 if (myrep
->r_flags
& R_GETONEREP
)
1083 * We assume all is fine, but if we did not have an error
1084 * and mrep is 0, better not dereference it. nfs_receieve
1085 * calls soreceive which carefully sets error=0 when it got
1086 * errors on sbwait (tsleep). In most cases, I assume that's
1087 * so we could go back again. In tcp case, EPIPE is returned.
1088 * In udp, case nfs_receive gets back here with no error and no
1089 * mrep. Is the right fix to have soreceive check for process
1090 * aborted after sbwait and return something non-zero? Should
1091 * nfs_receive give an EPIPE? Too risky to play with those
1092 * two this late in game for a shutdown problem. Instead,
1093 * just check here and get out. (ekn)
1096 FSDBG(530, myrep
->r_xid
, myrep
, nmp
, -3);
1097 return (ENXIO
); /* sounds good */
1101 * Get the xid and check that it is an rpc reply
1104 dpos
= mtod(md
, caddr_t
);
1105 nfsm_dissect(tl
, u_long
*, 2*NFSX_UNSIGNED
);
1107 if (*tl
!= rpc_reply
) {
1108 #ifndef NFS_NOSERVER
1109 if (nmp
->nm_flag
& NFSMNT_NQNFS
) {
1110 if (nqnfs_callback(nmp
, mrep
, md
, dpos
))
1111 nfsstats
.rpcinvalid
++;
1113 nfsstats
.rpcinvalid
++;
1117 nfsstats
.rpcinvalid
++;
1121 if (nmp
->nm_state
& NFSSTA_RCVLOCK
)
1122 nfs_rcvunlock(myrep
);
1123 if (myrep
->r_flags
& R_GETONEREP
)
1124 return (0); /* this path used by NQNFS */
1129 * Loop through the request list to match up the reply
1130 * Iff no match, just drop the datagram
1132 TAILQ_FOREACH(rep
, &nfs_reqq
, r_chain
) {
1133 if (rep
->r_mrep
== NULL
&& rxid
== rep
->r_xid
) {
1139 * If we're tracking the round trip time
1140 * then we update the circular log here
1141 * with the stats from our current request.
1146 rt
= &nfsrtt
.rttl
[nfsrtt
.pos
];
1147 rt
->proc
= rep
->r_procnum
;
1148 rt
->rto
= NFS_RTO(nmp
, proct
[rep
->r_procnum
]);
1149 rt
->sent
= nmp
->nm_sent
;
1150 rt
->cwnd
= nmp
->nm_cwnd
;
1151 if (proct
[rep
->r_procnum
] == 0)
1152 panic("nfs_reply: proct[%d] is zero", rep
->r_procnum
);
1153 rt
->srtt
= nmp
->nm_srtt
[proct
[rep
->r_procnum
] - 1];
1154 rt
->sdrtt
= nmp
->nm_sdrtt
[proct
[rep
->r_procnum
] - 1];
1155 rt
->fsid
= nmp
->nm_mountp
->mnt_stat
.f_fsid
;
1156 microtime(&rt
->tstamp
); // XXX unused
1157 if (rep
->r_flags
& R_TIMING
)
1158 rt
->rtt
= rep
->r_rtt
;
1161 nfsrtt
.pos
= (nfsrtt
.pos
+ 1) % NFSRTTLOGSIZ
;
1164 * Update congestion window.
1165 * Do the additive increase of
1168 FSDBG(530, rep
->r_xid
, rep
, nmp
->nm_sent
,
1170 if (nmp
->nm_cwnd
<= nmp
->nm_sent
) {
1172 (NFS_CWNDSCALE
* NFS_CWNDSCALE
+
1173 (nmp
->nm_cwnd
>> 1)) / nmp
->nm_cwnd
;
1174 if (nmp
->nm_cwnd
> NFS_MAXCWND
)
1175 nmp
->nm_cwnd
= NFS_MAXCWND
;
1177 if (rep
->r_flags
& R_SENT
) {
1178 rep
->r_flags
&= ~R_SENT
;
1179 nmp
->nm_sent
-= NFS_CWNDSCALE
;
1182 * Update rtt using a gain of 0.125 on the mean
1183 * and a gain of 0.25 on the deviation.
1185 if (rep
->r_flags
& R_TIMING
) {
1187 * Since the timer resolution of
1188 * NFS_HZ is so course, it can often
1189 * result in r_rtt == 0. Since
1190 * r_rtt == N means that the actual
1191 * rtt is between N+dt and N+2-dt ticks,
1194 if (proct
[rep
->r_procnum
] == 0)
1195 panic("nfs_reply: proct[%d] is zero", rep
->r_procnum
);
1196 t1
= rep
->r_rtt
+ 1;
1197 t1
-= (NFS_SRTT(rep
) >> 3);
1198 NFS_SRTT(rep
) += t1
;
1201 t1
-= (NFS_SDRTT(rep
) >> 2);
1202 NFS_SDRTT(rep
) += t1
;
1204 nmp
->nm_timeouts
= 0;
1208 nfs_rcvunlock(myrep
);
1210 * If not matched to a request, drop it.
1211 * If it's mine, get out.
1214 nfsstats
.rpcunexpected
++;
1216 } else if (rep
== myrep
) {
1217 if (rep
->r_mrep
== NULL
)
1218 panic("nfs_reply: nil r_mrep");
1221 FSDBG(530, myrep
->r_xid
, myrep
, rep
,
1222 rep
? rep
->r_xid
: myrep
->r_flags
);
1223 if (myrep
->r_flags
& R_GETONEREP
)
1224 return (0); /* this path used by NQNFS */
1229 * nfs_request - goes something like this
1230 * - fill in request struct
1231 * - links it into list
1232 * - calls nfs_send() for first transmit
1233 * - calls nfs_receive() to get reply
1234 * - break down rpc header and return with nfs reply pointed to
1236 * nb: always frees up mreq mbuf list
1239 nfs_request(vp
, mrest
, procnum
, procp
, cred
, mrp
, mdp
, dposp
, xidp
)
1250 register struct mbuf
*m
, *mrep
, *m2
;
1251 register struct nfsreq
*rep
, *rp
;
1252 register u_long
*tl
;
1254 struct nfsmount
*nmp
;
1255 struct mbuf
*md
, *mheadend
;
1257 char nickv
[RPCX_NICKVERF
];
1258 time_t reqtime
, waituntil
;
1260 int t1
, nqlflag
, cachable
, s
, error
= 0, mrest_len
, auth_len
, auth_type
;
1261 int trylater_delay
= NQ_TRYLATERDEL
, trylater_cnt
= 0, failed_auth
= 0;
1262 int verf_len
, verf_type
;
1265 char *auth_str
, *verf_str
;
1266 NFSKERBKEY_T key
; /* save session key */
1273 MALLOC_ZONE(rep
, struct nfsreq
*,
1274 sizeof(struct nfsreq
), M_NFSREQ
, M_WAITOK
);
1276 nmp
= VFSTONFS(vp
->v_mount
);
1278 (nmp
->nm_state
& (NFSSTA_FORCE
|NFSSTA_TIMEO
)) ==
1279 (NFSSTA_FORCE
|NFSSTA_TIMEO
)) {
1280 FREE_ZONE((caddr_t
)rep
, sizeof (struct nfsreq
), M_NFSREQ
);
1283 nmsotype
= nmp
->nm_sotype
;
1285 FSDBG_TOP(531, vp
, procnum
, nmp
, rep
);
1289 rep
->r_procp
= procp
;
1290 rep
->r_procnum
= procnum
;
1292 rep
->r_lastmsg
= now
.tv_sec
-
1293 ((nmp
->nm_tprintf_delay
) - (nmp
->nm_tprintf_initial_delay
));
1303 * Get the RPC header with authorization.
1306 nmp
= VFSTONFS(vp
->v_mount
);
1308 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1309 FREE_ZONE((caddr_t
)rep
, sizeof (struct nfsreq
), M_NFSREQ
);
1312 verf_str
= auth_str
= (char *)0;
1313 if (nmp
->nm_flag
& NFSMNT_KERB
) {
1315 verf_len
= sizeof (nickv
);
1316 auth_type
= RPCAUTH_KERB4
;
1317 bzero((caddr_t
)key
, sizeof (key
));
1318 if (failed_auth
|| nfs_getnickauth(nmp
, cred
, &auth_str
,
1319 &auth_len
, verf_str
, verf_len
)) {
1320 nmp
= VFSTONFS(vp
->v_mount
);
1322 FSDBG_BOT(531, 2, vp
, error
, rep
);
1323 FREE_ZONE((caddr_t
)rep
,
1324 sizeof (struct nfsreq
), M_NFSREQ
);
1328 error
= nfs_getauth(nmp
, rep
, cred
, &auth_str
,
1329 &auth_len
, verf_str
, &verf_len
, key
);
1330 nmp
= VFSTONFS(vp
->v_mount
);
1334 FSDBG_BOT(531, 2, vp
, error
, rep
);
1335 FREE_ZONE((caddr_t
)rep
,
1336 sizeof (struct nfsreq
), M_NFSREQ
);
1342 auth_type
= RPCAUTH_UNIX
;
1343 if (cred
->cr_ngroups
< 1)
1344 panic("nfsreq nogrps");
1345 auth_len
= ((((cred
->cr_ngroups
- 1) > nmp
->nm_numgrps
) ?
1346 nmp
->nm_numgrps
: (cred
->cr_ngroups
- 1)) << 2) +
1349 m
= nfsm_rpchead(cred
, nmp
->nm_flag
, procnum
, auth_type
, auth_len
,
1350 auth_str
, verf_len
, verf_str
, mrest
, mrest_len
, &mheadend
, &xid
);
1352 *xidp
= ntohl(xid
) + ((u_int64_t
)nfs_xidwrap
<< 32);
1354 _FREE(auth_str
, M_TEMP
);
1357 * For stream protocols, insert a Sun RPC Record Mark.
1359 if (nmsotype
== SOCK_STREAM
) {
1360 M_PREPEND(m
, NFSX_UNSIGNED
, M_WAIT
);
1361 *mtod(m
, u_long
*) = htonl(0x80000000 |
1362 (m
->m_pkthdr
.len
- NFSX_UNSIGNED
));
1367 nmp
= VFSTONFS(vp
->v_mount
);
1368 if (nmp
&& (nmp
->nm_flag
& NFSMNT_SOFT
))
1369 rep
->r_retry
= nmp
->nm_retry
;
1371 rep
->r_retry
= NFS_MAXREXMIT
+ 1; /* past clip limit */
1372 rep
->r_rtt
= rep
->r_rexmit
= 0;
1373 if (proct
[procnum
] > 0)
1374 rep
->r_flags
= R_TIMING
;
1380 * Do the client side RPC.
1382 nfsstats
.rpcrequests
++;
1384 * Chain request into list of outstanding requests. Be sure
1385 * to put it LAST so timer finds oldest requests first.
1388 TAILQ_INSERT_TAIL(&nfs_reqq
, rep
, r_chain
);
1390 /* Get send time for nqnfs */
1392 reqtime
= now
.tv_sec
;
1395 * If backing off another request or avoiding congestion, don't
1396 * send this one now but let timer do it. If not timing a request,
1399 if (nmp
&& nmp
->nm_so
&& (nmp
->nm_sotype
!= SOCK_DGRAM
||
1400 (nmp
->nm_flag
& NFSMNT_DUMBTIMR
) ||
1401 nmp
->nm_sent
< nmp
->nm_cwnd
)) {
1402 int connrequired
= (nmp
->nm_soflags
& PR_CONNREQUIRED
);
1406 error
= nfs_sndlock(rep
);
1409 * Set the R_SENT before doing the send in case another thread
1410 * processes the reply before the nfs_send returns here
1413 if ((rep
->r_flags
& R_MUSTRESEND
) == 0) {
1414 FSDBG(531, rep
->r_xid
, rep
, nmp
->nm_sent
,
1416 nmp
->nm_sent
+= NFS_CWNDSCALE
;
1417 rep
->r_flags
|= R_SENT
;
1420 m2
= m_copym(m
, 0, M_COPYALL
, M_WAIT
);
1421 error
= nfs_send(nmp
->nm_so
, nmp
->nm_nam
, m2
, rep
);
1425 nmp
= VFSTONFS(vp
->v_mount
);
1428 nmp
->nm_sent
-= NFS_CWNDSCALE
;
1429 rep
->r_flags
&= ~R_SENT
;
1437 * Wait for the reply from our send or the timer's.
1439 if (!error
|| error
== EPIPE
)
1440 error
= nfs_reply(rep
);
1443 * RPC done, unlink the request.
1445 nfs_repdequeue(rep
);
1447 nmp
= VFSTONFS(vp
->v_mount
);
1450 * Decrement the outstanding request count.
1452 if (rep
->r_flags
& R_SENT
) {
1453 rep
->r_flags
&= ~R_SENT
; /* paranoia */
1455 FSDBG(531, rep
->r_xid
, rep
, nmp
->nm_sent
, nmp
->nm_cwnd
);
1456 nmp
->nm_sent
-= NFS_CWNDSCALE
;
1461 * If there was a successful reply and a tprintf msg.
1462 * tprintf a response.
1464 nfs_up(rep
, "is alive again", error
);
1471 m_freem(rep
->r_mreq
);
1472 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1473 FREE_ZONE((caddr_t
)rep
, sizeof (struct nfsreq
), M_NFSREQ
);
1478 * break down the rpc header and check if ok
1480 nfsm_dissect(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
1481 if (*tl
++ == rpc_msgdenied
) {
1482 if (*tl
== rpc_mismatch
)
1484 else if ((nmp
->nm_flag
& NFSMNT_KERB
) && *tl
++ == rpc_autherr
) {
1487 mheadend
->m_next
= (struct mbuf
*)0;
1489 m_freem(rep
->r_mreq
);
1496 m_freem(rep
->r_mreq
);
1497 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1498 FREE_ZONE((caddr_t
)rep
, sizeof (struct nfsreq
), M_NFSREQ
);
1503 * Grab any Kerberos verifier, otherwise just throw it away.
1505 verf_type
= fxdr_unsigned(int, *tl
++);
1506 i
= fxdr_unsigned(int, *tl
);
1507 if ((nmp
->nm_flag
& NFSMNT_KERB
) && verf_type
== RPCAUTH_KERB4
) {
1508 error
= nfs_savenickauth(nmp
, cred
, i
, key
, &md
, &dpos
, mrep
);
1512 nfsm_adv(nfsm_rndup(i
));
1513 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
1516 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
1518 error
= fxdr_unsigned(int, *tl
);
1519 if ((nmp
->nm_flag
& NFSMNT_NFSV3
) &&
1520 error
== NFSERR_TRYLATER
) {
1524 waituntil
= now
.tv_sec
+ trylater_delay
;
1526 ("nfs_request %s flag=%x trylater_cnt=%x waituntil=%lx trylater_delay=%x\n",
1527 nmp
->nm_mountp
->mnt_stat
.f_mntfromname
,
1528 nmp
->nm_flag
, trylater_cnt
, waituntil
,
1530 while (now
.tv_sec
< waituntil
) {
1531 (void)tsleep((caddr_t
)&lbolt
,
1532 PSOCK
, "nqnfstry", 0);
1535 trylater_delay
*= 2;
1536 if (trylater_delay
> 60)
1537 trylater_delay
= 60;
1538 if (trylater_cnt
< 7)
1544 * If the File Handle was stale, invalidate the
1545 * lookup cache, just in case.
1547 if (error
== ESTALE
)
1549 if (nmp
->nm_flag
& NFSMNT_NFSV3
) {
1553 error
|= NFSERR_RETERR
;
1556 m_freem(rep
->r_mreq
);
1557 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1558 FREE_ZONE((caddr_t
)rep
,
1559 sizeof (struct nfsreq
), M_NFSREQ
);
1564 * For nqnfs, get any lease in reply
1566 if (nmp
->nm_flag
& NFSMNT_NQNFS
) {
1567 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
1570 nqlflag
= fxdr_unsigned(int, *tl
);
1571 nfsm_dissect(tl
, u_long
*, 4*NFSX_UNSIGNED
);
1572 cachable
= fxdr_unsigned(int, *tl
++);
1573 reqtime
+= fxdr_unsigned(int, *tl
++);
1575 if (reqtime
> now
.tv_sec
) {
1576 fxdr_hyper(tl
, &frev
);
1577 nqnfs_clientlease(nmp
, np
, nqlflag
,
1578 cachable
, reqtime
, frev
);
1585 m_freem(rep
->r_mreq
);
1586 FSDBG_BOT(531, 0xf0f0f0f0, rep
->r_xid
, nmp
, rep
);
1587 FREE_ZONE((caddr_t
)rep
, sizeof (struct nfsreq
), M_NFSREQ
);
1591 error
= EPROTONOSUPPORT
;
1593 m_freem(rep
->r_mreq
);
1594 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1595 FREE_ZONE((caddr_t
)rep
, sizeof (struct nfsreq
), M_NFSREQ
);
1599 #ifndef NFS_NOSERVER
1601 * Generate the rpc reply header
1602 * siz arg. is used to decide if adding a cluster is worthwhile
1605 nfs_rephead(siz
, nd
, slp
, err
, cache
, frev
, mrq
, mbp
, bposp
)
1607 struct nfsrv_descript
*nd
;
1608 struct nfssvc_sock
*slp
;
1616 register u_long
*tl
;
1617 register struct mbuf
*mreq
;
1619 struct mbuf
*mb
, *mb2
;
1621 MGETHDR(mreq
, M_WAIT
, MT_DATA
);
1624 * If this is a big reply, use a cluster else
1625 * try and leave leading space for the lower level headers.
1627 siz
+= RPC_REPLYSIZ
;
1628 if (siz
>= MINCLSIZE
) {
1629 MCLGET(mreq
, M_WAIT
);
1631 mreq
->m_data
+= max_hdr
;
1632 tl
= mtod(mreq
, u_long
*);
1633 mreq
->m_len
= 6 * NFSX_UNSIGNED
;
1634 bpos
= ((caddr_t
)tl
) + mreq
->m_len
;
1635 *tl
++ = txdr_unsigned(nd
->nd_retxid
);
1637 if (err
== ERPCMISMATCH
|| (err
& NFSERR_AUTHERR
)) {
1638 *tl
++ = rpc_msgdenied
;
1639 if (err
& NFSERR_AUTHERR
) {
1640 *tl
++ = rpc_autherr
;
1641 *tl
= txdr_unsigned(err
& ~NFSERR_AUTHERR
);
1642 mreq
->m_len
-= NFSX_UNSIGNED
;
1643 bpos
-= NFSX_UNSIGNED
;
1645 *tl
++ = rpc_mismatch
;
1646 *tl
++ = txdr_unsigned(RPC_VER2
);
1647 *tl
= txdr_unsigned(RPC_VER2
);
1650 *tl
++ = rpc_msgaccepted
;
1653 * For Kerberos authentication, we must send the nickname
1654 * verifier back, otherwise just RPCAUTH_NULL.
1656 if (nd
->nd_flag
& ND_KERBFULL
) {
1657 register struct nfsuid
*nuidp
;
1658 struct timeval ktvin
, ktvout
;
1660 for (nuidp
= NUIDHASH(slp
, nd
->nd_cr
.cr_uid
)->lh_first
;
1661 nuidp
!= 0; nuidp
= nuidp
->nu_hash
.le_next
) {
1662 if (nuidp
->nu_cr
.cr_uid
== nd
->nd_cr
.cr_uid
&&
1663 (!nd
->nd_nam2
|| netaddr_match(NU_NETFAM(nuidp
),
1664 &nuidp
->nu_haddr
, nd
->nd_nam2
)))
1669 txdr_unsigned(nuidp
->nu_timestamp
.tv_sec
- 1);
1671 txdr_unsigned(nuidp
->nu_timestamp
.tv_usec
);
1674 * Encrypt the timestamp in ecb mode using the
1681 *tl
++ = rpc_auth_kerb
;
1682 *tl
++ = txdr_unsigned(3 * NFSX_UNSIGNED
);
1683 *tl
= ktvout
.tv_sec
;
1684 nfsm_build(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
1685 *tl
++ = ktvout
.tv_usec
;
1686 *tl
++ = txdr_unsigned(nuidp
->nu_cr
.cr_uid
);
1697 *tl
= txdr_unsigned(RPC_PROGUNAVAIL
);
1700 *tl
= txdr_unsigned(RPC_PROGMISMATCH
);
1701 nfsm_build(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
1702 if (nd
->nd_flag
& ND_NQNFS
) {
1703 *tl
++ = txdr_unsigned(3);
1704 *tl
= txdr_unsigned(3);
1706 *tl
++ = txdr_unsigned(2);
1707 *tl
= txdr_unsigned(3);
1711 *tl
= txdr_unsigned(RPC_PROCUNAVAIL
);
1714 *tl
= txdr_unsigned(RPC_GARBAGE
);
1718 if (err
!= NFSERR_RETVOID
) {
1719 nfsm_build(tl
, u_long
*, NFSX_UNSIGNED
);
1721 *tl
= txdr_unsigned(nfsrv_errmap(nd
, err
));
1730 * For nqnfs, piggyback lease as requested.
1732 if ((nd
->nd_flag
& ND_NQNFS
) && err
== 0) {
1733 if (nd
->nd_flag
& ND_LEASE
) {
1734 nfsm_build(tl
, u_long
*, 5 * NFSX_UNSIGNED
);
1735 *tl
++ = txdr_unsigned(nd
->nd_flag
& ND_LEASE
);
1736 *tl
++ = txdr_unsigned(cache
);
1737 *tl
++ = txdr_unsigned(nd
->nd_duration
);
1738 txdr_hyper(frev
, tl
);
1740 nfsm_build(tl
, u_long
*, NFSX_UNSIGNED
);
1748 if (err
!= 0 && err
!= NFSERR_RETVOID
)
1749 nfsstats
.srvrpc_errs
++;
1754 #endif /* NFS_NOSERVER */
1758 * From FreeBSD 1.58, a Matt Dillon fix...
1759 * Flag a request as being about to terminate.
1760 * The nm_sent count is decremented now to avoid deadlocks when the process
1761 * in soreceive() hasn't yet managed to send its own request.
1764 nfs_softterm(struct nfsreq
*rep
)
1767 rep
->r_flags
|= R_SOFTTERM
;
1768 if (rep
->r_flags
& R_SENT
) {
1769 FSDBG(532, rep
->r_xid
, rep
, rep
->r_nmp
->nm_sent
,
1770 rep
->r_nmp
->nm_cwnd
);
1771 rep
->r_nmp
->nm_sent
-= NFS_CWNDSCALE
;
1772 rep
->r_flags
&= ~R_SENT
;
1777 nfs_timer_funnel(arg
)
1780 (void) thread_funnel_set(kernel_flock
, TRUE
);
1782 (void) thread_funnel_set(kernel_flock
, FALSE
);
1787 * Ensure rep isn't in use by the timer, then dequeue it.
1790 nfs_repdequeue(struct nfsreq
*rep
)
1794 while ((rep
->r_flags
& R_BUSY
)) {
1795 rep
->r_flags
|= R_WAITING
;
1796 tsleep(rep
, PSOCK
, "repdeq", 0);
1799 TAILQ_REMOVE(&nfs_reqq
, rep
, r_chain
);
1804 * Busy (lock) a nfsreq, used by the nfs timer to make sure it's not
1805 * free()'d out from under it.
1808 nfs_repbusy(struct nfsreq
*rep
)
1811 if ((rep
->r_flags
& R_BUSY
))
1812 panic("rep locked");
1813 rep
->r_flags
|= R_BUSY
;
1817 * Unbusy the nfsreq passed in, return the next nfsreq in the chain busied.
1820 nfs_repnext(struct nfsreq
*rep
)
1822 struct nfsreq
* nextrep
;
1827 * We need to get and busy the next req before signalling the
1828 * current one, otherwise wakeup() may block us and we'll race to
1829 * grab the next req.
1831 nextrep
= TAILQ_NEXT(rep
, r_chain
);
1832 if (nextrep
!= NULL
)
1833 nfs_repbusy(nextrep
);
1834 /* unbusy and signal. */
1835 rep
->r_flags
&= ~R_BUSY
;
1836 if ((rep
->r_flags
& R_WAITING
)) {
1837 rep
->r_flags
&= ~R_WAITING
;
1845 * Scan the nfsreq list and retranmit any requests that have timed out
1846 * To avoid retransmission attempts on STREAM sockets (in the future) make
1847 * sure to set the r_retry field to 0 (implies nm_retry == 0).
1851 void *arg
; /* never used */
1853 register struct nfsreq
*rep
;
1854 register struct mbuf
*m
;
1855 register struct socket
*so
;
1856 register struct nfsmount
*nmp
;
1859 #ifndef NFS_NOSERVER
1860 static long lasttime
= 0;
1861 register struct nfssvc_sock
*slp
;
1863 #endif /* NFS_NOSERVER */
1867 int flags
, rexmit
, cwnd
, sent
;
1873 * XXX If preemptable threads are implemented the spls used for the
1874 * outstanding request queue must be replaced with mutexes.
1876 #ifdef NFSTRACESUSPENDERS
1877 if (NFSTRACE_SUSPENDING
) {
1878 TAILQ_FOREACH(rep
, &nfs_reqq
, r_chain
)
1879 if (rep
->r_xid
== nfstracexid
)
1883 } else if (NFSTRACE_SUSPENSEOVER
) {
1888 rep
= TAILQ_FIRST(&nfs_reqq
);
1892 for ( ; rep
!= NULL
; rep
= nfs_repnext(rep
)) {
1893 #ifdef NFSTRACESUSPENDERS
1894 if (rep
->r_mrep
&& !NFSTRACE_SUSPENDING
) {
1895 nfstracexid
= rep
->r_xid
;
1896 NFSTRACE_STARTSUSPENDCOUNTDOWN
;
1900 if (!nmp
) /* unmounted */
1902 if (rep
->r_mrep
|| (rep
->r_flags
& R_SOFTTERM
))
1904 if (nfs_sigintr(nmp
, rep
, rep
->r_procp
))
1906 if (nmp
->nm_tprintf_initial_delay
!= 0 &&
1907 (rep
->r_rexmit
> 2 || (rep
->r_flags
& R_RESENDERR
)) &&
1908 rep
->r_lastmsg
+ nmp
->nm_tprintf_delay
< now
.tv_sec
) {
1909 rep
->r_lastmsg
= now
.tv_sec
;
1910 nfs_down(rep
, "not responding", 0);
1912 if (rep
->r_rtt
>= 0) {
1914 if (nmp
->nm_flag
& NFSMNT_DUMBTIMR
)
1915 timeo
= nmp
->nm_timeo
;
1917 timeo
= NFS_RTO(nmp
, proct
[rep
->r_procnum
]);
1918 /* ensure 62.5 ms floor */
1919 while (16 * timeo
< hz
)
1921 if (nmp
->nm_timeouts
> 0)
1922 timeo
*= nfs_backoff
[nmp
->nm_timeouts
- 1];
1923 if (rep
->r_rtt
<= timeo
)
1925 if (nmp
->nm_timeouts
< 8)
1929 * Check for too many retransmits. This is never true for
1930 * 'hard' mounts because we set r_retry to NFS_MAXREXMIT + 1
1931 * and never allow r_rexmit to be more than NFS_MAXREXMIT.
1933 if (rep
->r_rexmit
>= rep
->r_retry
) { /* too many */
1934 nfsstats
.rpctimeouts
++;
1938 if (nmp
->nm_sotype
!= SOCK_DGRAM
) {
1939 if (++rep
->r_rexmit
> NFS_MAXREXMIT
)
1940 rep
->r_rexmit
= NFS_MAXREXMIT
;
1943 if ((so
= nmp
->nm_so
) == NULL
)
1947 * If there is enough space and the window allows..
1949 * Set r_rtt to -1 in case we fail to send it now.
1952 rttdiag
= rep
->r_rtt
;
1955 if (sbspace(&so
->so_snd
) >= rep
->r_mreq
->m_pkthdr
.len
&&
1956 ((nmp
->nm_flag
& NFSMNT_DUMBTIMR
) ||
1957 (rep
->r_flags
& R_SENT
) ||
1958 nmp
->nm_sent
< nmp
->nm_cwnd
) &&
1959 (m
= m_copym(rep
->r_mreq
, 0, M_COPYALL
, M_DONTWAIT
))){
1961 struct proc
*p
= current_proc();
1964 if (rep
->r_flags
& R_SENT
&& nfsprnttimo
&&
1965 nmp
->nm_timeouts
>= nfsprnttimo
) {
1966 int t
= proct
[rep
->r_procnum
];
1968 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]));
1970 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
));
1973 #endif /* NFSDIAG */
1975 * Iff first send, start timing
1976 * else turn timing off, backoff timer
1977 * and divide congestion window by 2.
1978 * We update these *before* the send to avoid
1979 * racing against receiving the reply.
1980 * We save them so we can restore them on send error.
1982 flags
= rep
->r_flags
;
1983 rexmit
= rep
->r_rexmit
;
1984 cwnd
= nmp
->nm_cwnd
;
1985 sent
= nmp
->nm_sent
;
1987 if (rep
->r_flags
& R_SENT
) {
1988 rep
->r_flags
&= ~R_TIMING
;
1989 if (++rep
->r_rexmit
> NFS_MAXREXMIT
)
1990 rep
->r_rexmit
= NFS_MAXREXMIT
;
1992 if (nmp
->nm_cwnd
< NFS_CWNDSCALE
)
1993 nmp
->nm_cwnd
= NFS_CWNDSCALE
;
1994 nfsstats
.rpcretries
++;
1996 rep
->r_flags
|= R_SENT
;
1997 nmp
->nm_sent
+= NFS_CWNDSCALE
;
1999 FSDBG(535, xid
, rep
, nmp
->nm_sent
, nmp
->nm_cwnd
);
2001 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
2003 if ((nmp
->nm_flag
& NFSMNT_NOCONN
) == 0)
2004 error
= (*so
->so_proto
->pr_usrreqs
->pru_send
)
2005 (so
, 0, m
, 0, 0, p
);
2007 error
= (*so
->so_proto
->pr_usrreqs
->pru_send
)
2008 (so
, 0, m
, mtod(nmp
->nm_nam
, struct sockaddr
*), 0, p
);
2010 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
2012 FSDBG(535, xid
, error
, sent
, cwnd
);
2015 if (NFSIGNORE_SOERROR(nmp
->nm_soflags
, error
))
2017 rep
->r_flags
= flags
| R_RESENDERR
;
2018 rep
->r_rexmit
= rexmit
;
2019 nmp
->nm_cwnd
= cwnd
;
2020 nmp
->nm_sent
= sent
;
2022 nfsstats
.rpcretries
--;
2027 #ifndef NFS_NOSERVER
2029 * Call the nqnfs server timer once a second to handle leases.
2032 if (lasttime
!= now
.tv_sec
) {
2033 lasttime
= now
.tv_sec
;
2038 * Scan the write gathering queues for writes that need to be
2041 cur_usec
= (u_quad_t
)now
.tv_sec
* 1000000 + (u_quad_t
)now
.tv_usec
;
2042 TAILQ_FOREACH(slp
, &nfssvc_sockhead
, ns_chain
) {
2043 if (LIST_FIRST(&slp
->ns_tq
) &&
2044 LIST_FIRST(&slp
->ns_tq
)->nd_time
<= cur_usec
)
2045 nfsrv_wakenfsd(slp
);
2047 #endif /* NFS_NOSERVER */
2049 timeout(nfs_timer_funnel
, (void *)0, nfs_ticks
);
2055 * Test for a termination condition pending on the process.
2056 * This is used to determine if we need to bail on a mount.
2057 * EIO is returned if there has been a soft timeout.
2058 * EINTR is returned if there is a signal pending that is not being ignored
2059 * and the mount is interruptable, or if we are a thread that is in the process
2060 * of cancellation (also SIGKILL posted).
2063 nfs_sigintr(nmp
, rep
, p
)
2064 struct nfsmount
*nmp
;
2068 struct uthread
*curr_td
;
2069 sigset_t pending_sigs
;
2070 int context_good
= 0;
2071 struct nfsmount
*repnmp
;
2076 repnmp
= rep
->r_nmp
;
2077 /* we've had a forced unmount. */
2080 /* request has timed out on a 'soft' mount. */
2081 if (rep
->r_flags
& R_SOFTTERM
)
2084 * We're in the progress of a force unmount and there's
2085 * been a timeout we're dead and fail IO.
2087 if ((repnmp
->nm_state
& (NFSSTA_FORCE
|NFSSTA_TIMEO
)) ==
2088 (NFSSTA_FORCE
|NFSSTA_TIMEO
))
2090 /* Someone is unmounting us, go soft and mark it. */
2091 if ((repnmp
->nm_mountp
->mnt_kern_flag
& MNTK_FRCUNMOUNT
)) {
2092 repnmp
->nm_flag
|= NFSMNT_SOFT
;
2093 nmp
->nm_state
|= NFSSTA_FORCE
;
2096 * If the mount is hung and we've requested not to hang
2097 * on remote filesystems, then bail now.
2099 if (p
!= NULL
&& (p
->p_flag
& P_NOREMOTEHANG
) != 0 &&
2100 (repnmp
->nm_state
& NFSSTA_TIMEO
) != 0)
2103 /* XXX: is this valid? this probably should be an assertion. */
2108 * XXX: Since nfs doesn't have a good shot at getting the current
2109 * thread we take a guess. (only struct proc * are passed to VOPs)
2110 * What we do is look at the current thread, if it belongs to the
2111 * passed in proc pointer then we have a "good/accurate" context
2112 * and can make an accurate guess as to what to do.
2113 * However if we have a bad context we have to make due with what
2114 * is in the proc struct which may not be as up to date as we'd
2116 * This is ok because the process will call us with the correct
2117 * context after a short timeout while waiting for a response.
2119 curr_td
= (struct uthread
*)get_bsdthread_info(current_act());
2120 if (curr_td
->uu_proc
== p
)
2122 if (context_good
&& current_thread_aborted())
2124 /* mask off thread and process blocked signals. */
2126 pending_sigs
= curr_td
->uu_siglist
& ~curr_td
->uu_sigmask
;
2128 pending_sigs
= p
->p_siglist
;
2129 /* mask off process level and NFS ignored signals. */
2130 pending_sigs
&= ~p
->p_sigignore
& NFSINT_SIGMASK
;
2131 if (pending_sigs
&& (nmp
->nm_flag
& NFSMNT_INT
) != 0)
2137 * Lock a socket against others.
2138 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
2139 * and also to avoid race conditions between the processes with nfs requests
2140 * in progress when a reconnect is necessary.
2146 register int *statep
;
2148 int error
, slpflag
= 0, slptimeo
= 0;
2150 if (rep
->r_nmp
== NULL
)
2152 statep
= &rep
->r_nmp
->nm_state
;
2155 if (rep
->r_nmp
->nm_flag
& NFSMNT_INT
)
2157 while (*statep
& NFSSTA_SNDLOCK
) {
2158 error
= nfs_sigintr(rep
->r_nmp
, rep
, p
);
2161 *statep
|= NFSSTA_WANTSND
;
2162 if (p
!= NULL
&& (p
->p_flag
& P_NOREMOTEHANG
) != 0)
2164 (void) tsleep((caddr_t
)statep
, slpflag
| (PZERO
- 1),
2165 "nfsndlck", slptimeo
);
2166 if (slpflag
== PCATCH
) {
2171 * Make sure while we slept that the mountpoint didn't go away.
2172 * nfs_sigintr and callers expect it in tact.
2175 return (ENXIO
); /* don't have lock until out of loop */
2177 *statep
|= NFSSTA_SNDLOCK
;
2182 * Unlock the stream socket for others.
2188 register int *statep
;
2190 if (rep
->r_nmp
== NULL
)
2192 statep
= &rep
->r_nmp
->nm_state
;
2193 if ((*statep
& NFSSTA_SNDLOCK
) == 0)
2194 panic("nfs sndunlock");
2195 *statep
&= ~NFSSTA_SNDLOCK
;
2196 if (*statep
& NFSSTA_WANTSND
) {
2197 *statep
&= ~NFSSTA_WANTSND
;
2198 wakeup((caddr_t
)statep
);
2204 register struct nfsreq
*rep
;
2206 register int *statep
;
2207 int error
, slpflag
, slptimeo
= 0;
2209 /* make sure we still have our mountpoint */
2211 if (rep
->r_mrep
!= NULL
)
2216 statep
= &rep
->r_nmp
->nm_state
;
2217 FSDBG_TOP(534, rep
->r_xid
, rep
, rep
->r_nmp
, *statep
);
2218 if (rep
->r_nmp
->nm_flag
& NFSMNT_INT
)
2222 while (*statep
& NFSSTA_RCVLOCK
) {
2223 if ((error
= nfs_sigintr(rep
->r_nmp
, rep
, rep
->r_procp
))) {
2224 FSDBG_BOT(534, rep
->r_xid
, rep
, rep
->r_nmp
, 0x100);
2226 } else if (rep
->r_mrep
!= NULL
) {
2228 * Don't bother sleeping if reply already arrived
2230 FSDBG_BOT(534, rep
->r_xid
, rep
, rep
->r_nmp
, 0x101);
2233 FSDBG(534, rep
->r_xid
, rep
, rep
->r_nmp
, 0x102);
2234 *statep
|= NFSSTA_WANTRCV
;
2236 * We need to poll if we're P_NOREMOTEHANG so that we
2237 * call nfs_sigintr periodically above.
2239 if (rep
->r_procp
!= NULL
&&
2240 (rep
->r_procp
->p_flag
& P_NOREMOTEHANG
) != 0)
2242 (void) tsleep((caddr_t
)statep
, slpflag
| (PZERO
- 1),
2243 "nfsrcvlk", slptimeo
);
2244 if (slpflag
== PCATCH
) {
2249 * Make sure while we slept that the mountpoint didn't go away.
2250 * nfs_sigintr and caller nfs_reply expect it intact.
2253 FSDBG_BOT(534, rep
->r_xid
, rep
, rep
->r_nmp
, 0x103);
2254 return (ENXIO
); /* don't have lock until out of loop */
2258 * nfs_reply will handle it if reply already arrived.
2259 * (We may have slept or been preempted while on network funnel).
2261 FSDBG_BOT(534, rep
->r_xid
, rep
, rep
->r_nmp
, *statep
);
2262 *statep
|= NFSSTA_RCVLOCK
;
2267 * Unlock the stream socket for others.
2271 register struct nfsreq
*rep
;
2273 register int *statep
;
2275 if (rep
->r_nmp
== NULL
)
2277 statep
= &rep
->r_nmp
->nm_state
;
2279 FSDBG(533, statep
, *statep
, 0, 0);
2280 if ((*statep
& NFSSTA_RCVLOCK
) == 0)
2281 panic("nfs rcvunlock");
2282 *statep
&= ~NFSSTA_RCVLOCK
;
2283 if (*statep
& NFSSTA_WANTRCV
) {
2284 *statep
&= ~NFSSTA_WANTRCV
;
2285 wakeup((caddr_t
)statep
);
2290 #ifndef NFS_NOSERVER
2292 * Socket upcall routine for the nfsd sockets.
2293 * The caddr_t arg is a pointer to the "struct nfssvc_sock".
2294 * Essentially do as much as possible non-blocking, else punt and it will
2295 * be called with M_WAIT from an nfsd.
2298 * Needs to run under network funnel
2301 nfsrv_rcv(so
, arg
, waitflag
)
2306 register struct nfssvc_sock
*slp
= (struct nfssvc_sock
*)arg
;
2307 register struct mbuf
*m
;
2308 struct mbuf
*mp
, *mhck
;
2309 struct sockaddr
*nam
=0;
2311 int flags
, ns_nflag
=0, error
;
2312 struct sockaddr_in
*sin
;
2314 if ((slp
->ns_flag
& SLP_VALID
) == 0)
2318 * Define this to test for nfsds handling this under heavy load.
2320 if (waitflag
== M_DONTWAIT
) {
2321 ns_nflag
= SLPN_NEEDQ
;
2325 auio
.uio_procp
= NULL
;
2326 if (so
->so_type
== SOCK_STREAM
) {
2328 * If there are already records on the queue, defer soreceive()
2329 * to an nfsd so that there is feedback to the TCP layer that
2330 * the nfs servers are heavily loaded.
2332 if (slp
->ns_rec
&& waitflag
== M_DONTWAIT
) {
2333 ns_nflag
= SLPN_NEEDQ
;
2340 auio
.uio_resid
= 1000000000;
2341 flags
= MSG_DONTWAIT
;
2342 error
= soreceive(so
, (struct sockaddr
**) 0, &auio
, &mp
, (struct mbuf
**)0, &flags
);
2343 if (error
|| mp
== (struct mbuf
*)0) {
2344 if (error
== EWOULDBLOCK
)
2345 ns_nflag
= SLPN_NEEDQ
;
2347 ns_nflag
= SLPN_DISCONN
;
2351 if (slp
->ns_rawend
) {
2352 slp
->ns_rawend
->m_next
= m
;
2353 slp
->ns_cc
+= 1000000000 - auio
.uio_resid
;
2356 slp
->ns_cc
= 1000000000 - auio
.uio_resid
;
2363 * Now try and parse record(s) out of the raw stream data.
2365 error
= nfsrv_getstream(slp
, waitflag
);
2368 ns_nflag
= SLPN_DISCONN
;
2370 ns_nflag
= SLPN_NEEDQ
;
2374 auio
.uio_resid
= 1000000000;
2375 flags
= MSG_DONTWAIT
;
2377 error
= soreceive(so
, &nam
, &auio
, &mp
,
2378 (struct mbuf
**)0, &flags
);
2382 MGET(mhck
, M_WAIT
, MT_SONAME
);
2383 mhck
->m_len
= nam
->sa_len
;
2384 sin
= mtod(mhck
, struct sockaddr_in
*);
2385 bcopy(nam
, sin
, sizeof(struct sockaddr_in
));
2386 mhck
->m_hdr
.mh_len
= sizeof(struct sockaddr_in
);
2387 FREE(nam
, M_SONAME
);
2394 slp
->ns_recend
->m_nextpkt
= m
;
2398 m
->m_nextpkt
= (struct mbuf
*)0;
2401 if ((so
->so_proto
->pr_flags
& PR_CONNREQUIRED
)
2402 && error
!= EWOULDBLOCK
) {
2403 ns_nflag
= SLPN_DISCONN
;
2411 * Now try and process the request records, non-blocking.
2415 slp
->ns_nflag
|= ns_nflag
;
2416 if (waitflag
== M_DONTWAIT
&&
2417 (slp
->ns_rec
|| (slp
->ns_nflag
& (SLPN_NEEDQ
| SLPN_DISCONN
)))) {
2418 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
2419 nfsrv_wakenfsd(slp
);
2420 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
2425 * Try and extract an RPC request from the mbuf data list received on a
2426 * stream socket. The "waitflag" argument indicates whether or not it
2430 nfsrv_getstream(slp
, waitflag
)
2431 register struct nfssvc_sock
*slp
;
2434 register struct mbuf
*m
, **mpp
;
2435 register char *cp1
, *cp2
;
2437 struct mbuf
*om
, *m2
, *recm
;
2440 if (slp
->ns_nflag
& SLPN_GETSTREAM
)
2441 panic("nfs getstream");
2442 slp
->ns_nflag
|= SLPN_GETSTREAM
;
2444 if (slp
->ns_reclen
== 0) {
2445 if (slp
->ns_cc
< NFSX_UNSIGNED
) {
2446 slp
->ns_nflag
&= ~SLPN_GETSTREAM
;
2450 if (m
->m_len
>= NFSX_UNSIGNED
) {
2451 bcopy(mtod(m
, caddr_t
), (caddr_t
)&recmark
, NFSX_UNSIGNED
);
2452 m
->m_data
+= NFSX_UNSIGNED
;
2453 m
->m_len
-= NFSX_UNSIGNED
;
2455 cp1
= (caddr_t
)&recmark
;
2456 cp2
= mtod(m
, caddr_t
);
2457 while (cp1
< ((caddr_t
)&recmark
) + NFSX_UNSIGNED
) {
2458 while (m
->m_len
== 0) {
2460 cp2
= mtod(m
, caddr_t
);
2467 slp
->ns_cc
-= NFSX_UNSIGNED
;
2468 recmark
= ntohl(recmark
);
2469 slp
->ns_reclen
= recmark
& ~0x80000000;
2470 if (recmark
& 0x80000000)
2471 slp
->ns_nflag
|= SLPN_LASTFRAG
;
2473 slp
->ns_nflag
&= ~SLPN_LASTFRAG
;
2474 if (slp
->ns_reclen
< NFS_MINPACKET
|| slp
->ns_reclen
> NFS_MAXPACKET
) {
2475 slp
->ns_nflag
&= ~SLPN_GETSTREAM
;
2481 * Now get the record part.
2483 * Note that slp->ns_reclen may be 0. Linux sometimes
2484 * generates 0-length RPCs
2487 if (slp
->ns_cc
== slp
->ns_reclen
) {
2489 slp
->ns_raw
= slp
->ns_rawend
= (struct mbuf
*)0;
2490 slp
->ns_cc
= slp
->ns_reclen
= 0;
2491 } else if (slp
->ns_cc
> slp
->ns_reclen
) {
2494 om
= (struct mbuf
*)0;
2495 while (len
< slp
->ns_reclen
) {
2496 if ((len
+ m
->m_len
) > slp
->ns_reclen
) {
2497 m2
= m_copym(m
, 0, slp
->ns_reclen
- len
,
2505 m
->m_data
+= slp
->ns_reclen
- len
;
2506 m
->m_len
-= slp
->ns_reclen
- len
;
2507 len
= slp
->ns_reclen
;
2509 slp
->ns_nflag
&= ~SLPN_GETSTREAM
;
2510 return (EWOULDBLOCK
);
2512 } else if ((len
+ m
->m_len
) == slp
->ns_reclen
) {
2517 om
->m_next
= (struct mbuf
*)0;
2528 slp
->ns_nflag
&= ~SLPN_GETSTREAM
;
2533 * Accumulate the fragments into a record.
2535 mpp
= &slp
->ns_frag
;
2537 mpp
= &((*mpp
)->m_next
);
2539 if (slp
->ns_nflag
& SLPN_LASTFRAG
) {
2541 slp
->ns_recend
->m_nextpkt
= slp
->ns_frag
;
2543 slp
->ns_rec
= slp
->ns_frag
;
2544 slp
->ns_recend
= slp
->ns_frag
;
2545 slp
->ns_frag
= (struct mbuf
*)0;
2551 * Parse an RPC header.
2554 nfsrv_dorec(slp
, nfsd
, ndp
)
2555 register struct nfssvc_sock
*slp
;
2557 struct nfsrv_descript
**ndp
;
2559 register struct mbuf
*m
;
2560 register struct mbuf
*nam
;
2561 register struct nfsrv_descript
*nd
;
2565 if ((slp
->ns_flag
& SLP_VALID
) == 0 ||
2566 (m
= slp
->ns_rec
) == (struct mbuf
*)0)
2568 slp
->ns_rec
= m
->m_nextpkt
;
2570 m
->m_nextpkt
= (struct mbuf
*)0;
2572 slp
->ns_recend
= (struct mbuf
*)0;
2573 if (m
->m_type
== MT_SONAME
) {
2579 MALLOC_ZONE(nd
, struct nfsrv_descript
*,
2580 sizeof (struct nfsrv_descript
), M_NFSRVDESC
, M_WAITOK
);
2581 nd
->nd_md
= nd
->nd_mrep
= m
;
2583 nd
->nd_dpos
= mtod(m
, caddr_t
);
2584 error
= nfs_getreq(nd
, nfsd
, TRUE
);
2588 FREE_ZONE((caddr_t
)nd
, sizeof *nd
, M_NFSRVDESC
);
2597 * Parse an RPC request
2599 * - fill in the cred struct.
2602 nfs_getreq(nd
, nfsd
, has_header
)
2603 register struct nfsrv_descript
*nd
;
2607 register int len
, i
;
2608 register u_long
*tl
;
2612 caddr_t dpos
, cp2
, cp
;
2613 u_long nfsvers
, auth_type
;
2615 int error
= 0, nqnfs
= 0, ticklen
;
2616 struct mbuf
*mrep
, *md
;
2617 register struct nfsuid
*nuidp
;
2618 struct timeval tvin
, tvout
, now
;
2619 #if 0 /* until encrypted keys are implemented */
2620 NFSKERBKEYSCHED_T keys
; /* stores key schedule */
2627 nfsm_dissect(tl
, u_long
*, 10 * NFSX_UNSIGNED
);
2628 nd
->nd_retxid
= fxdr_unsigned(u_long
, *tl
++);
2629 if (*tl
++ != rpc_call
) {
2634 nfsm_dissect(tl
, u_long
*, 8 * NFSX_UNSIGNED
);
2637 if (*tl
++ != rpc_vers
) {
2638 nd
->nd_repstat
= ERPCMISMATCH
;
2639 nd
->nd_procnum
= NFSPROC_NOOP
;
2642 if (*tl
!= nfs_prog
) {
2643 if (*tl
== nqnfs_prog
)
2646 nd
->nd_repstat
= EPROGUNAVAIL
;
2647 nd
->nd_procnum
= NFSPROC_NOOP
;
2652 nfsvers
= fxdr_unsigned(u_long
, *tl
++);
2653 if (((nfsvers
< NFS_VER2
|| nfsvers
> NFS_VER3
) && !nqnfs
) ||
2654 (nfsvers
!= NQNFS_VER3
&& nqnfs
)) {
2655 nd
->nd_repstat
= EPROGMISMATCH
;
2656 nd
->nd_procnum
= NFSPROC_NOOP
;
2660 nd
->nd_flag
= (ND_NFSV3
| ND_NQNFS
);
2661 else if (nfsvers
== NFS_VER3
)
2662 nd
->nd_flag
= ND_NFSV3
;
2663 nd
->nd_procnum
= fxdr_unsigned(u_long
, *tl
++);
2664 if (nd
->nd_procnum
== NFSPROC_NULL
)
2666 if (nd
->nd_procnum
>= NFS_NPROCS
||
2667 (!nqnfs
&& nd
->nd_procnum
>= NQNFSPROC_GETLEASE
) ||
2668 (!nd
->nd_flag
&& nd
->nd_procnum
> NFSV2PROC_STATFS
)) {
2669 nd
->nd_repstat
= EPROCUNAVAIL
;
2670 nd
->nd_procnum
= NFSPROC_NOOP
;
2673 if ((nd
->nd_flag
& ND_NFSV3
) == 0)
2674 nd
->nd_procnum
= nfsv3_procid
[nd
->nd_procnum
];
2676 len
= fxdr_unsigned(int, *tl
++);
2677 if (len
< 0 || len
> RPCAUTH_MAXSIZ
) {
2682 nd
->nd_flag
&= ~ND_KERBAUTH
;
2684 * Handle auth_unix or auth_kerb.
2686 if (auth_type
== rpc_auth_unix
) {
2687 len
= fxdr_unsigned(int, *++tl
);
2688 if (len
< 0 || len
> NFS_MAXNAMLEN
) {
2692 nfsm_adv(nfsm_rndup(len
));
2693 nfsm_dissect(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
2694 bzero((caddr_t
)&nd
->nd_cr
, sizeof (struct ucred
));
2695 nd
->nd_cr
.cr_ref
= 1;
2696 nd
->nd_cr
.cr_uid
= fxdr_unsigned(uid_t
, *tl
++);
2697 nd
->nd_cr
.cr_gid
= fxdr_unsigned(gid_t
, *tl
++);
2698 len
= fxdr_unsigned(int, *tl
);
2699 if (len
< 0 || len
> RPCAUTH_UNIXGIDS
) {
2703 nfsm_dissect(tl
, u_long
*, (len
+ 2) * NFSX_UNSIGNED
);
2704 for (i
= 1; i
<= len
; i
++)
2706 nd
->nd_cr
.cr_groups
[i
] = fxdr_unsigned(gid_t
, *tl
++);
2709 nd
->nd_cr
.cr_ngroups
= (len
>= NGROUPS
) ? NGROUPS
: (len
+ 1);
2710 if (nd
->nd_cr
.cr_ngroups
> 1)
2711 nfsrvw_sort(nd
->nd_cr
.cr_groups
, nd
->nd_cr
.cr_ngroups
);
2712 len
= fxdr_unsigned(int, *++tl
);
2713 if (len
< 0 || len
> RPCAUTH_MAXSIZ
) {
2718 nfsm_adv(nfsm_rndup(len
));
2719 } else if (auth_type
== rpc_auth_kerb
) {
2720 switch (fxdr_unsigned(int, *tl
++)) {
2721 case RPCAKN_FULLNAME
:
2722 ticklen
= fxdr_unsigned(int, *tl
);
2723 *((u_long
*)nfsd
->nfsd_authstr
) = *tl
;
2724 uio
.uio_resid
= nfsm_rndup(ticklen
) + NFSX_UNSIGNED
;
2725 nfsd
->nfsd_authlen
= uio
.uio_resid
+ NFSX_UNSIGNED
;
2726 if (uio
.uio_resid
> (len
- 2 * NFSX_UNSIGNED
)) {
2733 uio
.uio_segflg
= UIO_SYSSPACE
;
2734 iov
.iov_base
= (caddr_t
)&nfsd
->nfsd_authstr
[4];
2735 iov
.iov_len
= RPCAUTH_MAXSIZ
- 4;
2736 nfsm_mtouio(&uio
, uio
.uio_resid
);
2737 nfsm_dissect(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
2738 if (*tl
++ != rpc_auth_kerb
||
2739 fxdr_unsigned(int, *tl
) != 4 * NFSX_UNSIGNED
) {
2740 printf("Bad kerb verifier\n");
2741 nd
->nd_repstat
= (NFSERR_AUTHERR
|AUTH_BADVERF
);
2742 nd
->nd_procnum
= NFSPROC_NOOP
;
2745 nfsm_dissect(cp
, caddr_t
, 4 * NFSX_UNSIGNED
);
2747 if (fxdr_unsigned(int, *tl
) != RPCAKN_FULLNAME
) {
2748 printf("Not fullname kerb verifier\n");
2749 nd
->nd_repstat
= (NFSERR_AUTHERR
|AUTH_BADVERF
);
2750 nd
->nd_procnum
= NFSPROC_NOOP
;
2753 cp
+= NFSX_UNSIGNED
;
2754 bcopy(cp
, nfsd
->nfsd_verfstr
, 3 * NFSX_UNSIGNED
);
2755 nfsd
->nfsd_verflen
= 3 * NFSX_UNSIGNED
;
2756 nd
->nd_flag
|= ND_KERBFULL
;
2757 nfsd
->nfsd_flag
|= NFSD_NEEDAUTH
;
2759 case RPCAKN_NICKNAME
:
2760 if (len
!= 2 * NFSX_UNSIGNED
) {
2761 printf("Kerb nickname short\n");
2762 nd
->nd_repstat
= (NFSERR_AUTHERR
|AUTH_BADCRED
);
2763 nd
->nd_procnum
= NFSPROC_NOOP
;
2766 nickuid
= fxdr_unsigned(uid_t
, *tl
);
2767 nfsm_dissect(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
2768 if (*tl
++ != rpc_auth_kerb
||
2769 fxdr_unsigned(int, *tl
) != 3 * NFSX_UNSIGNED
) {
2770 printf("Kerb nick verifier bad\n");
2771 nd
->nd_repstat
= (NFSERR_AUTHERR
|AUTH_BADVERF
);
2772 nd
->nd_procnum
= NFSPROC_NOOP
;
2775 nfsm_dissect(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
2776 tvin
.tv_sec
= *tl
++;
2779 for (nuidp
= NUIDHASH(nfsd
->nfsd_slp
,nickuid
)->lh_first
;
2780 nuidp
!= 0; nuidp
= nuidp
->nu_hash
.le_next
) {
2781 if (nuidp
->nu_cr
.cr_uid
== nickuid
&&
2783 netaddr_match(NU_NETFAM(nuidp
),
2784 &nuidp
->nu_haddr
, nd
->nd_nam2
)))
2789 (NFSERR_AUTHERR
|AUTH_REJECTCRED
);
2790 nd
->nd_procnum
= NFSPROC_NOOP
;
2795 * Now, decrypt the timestamp using the session key
2802 tvout
.tv_sec
= fxdr_unsigned(long, tvout
.tv_sec
);
2803 tvout
.tv_usec
= fxdr_unsigned(long, tvout
.tv_usec
);
2805 if (nuidp
->nu_expire
< now
.tv_sec
||
2806 nuidp
->nu_timestamp
.tv_sec
> tvout
.tv_sec
||
2807 (nuidp
->nu_timestamp
.tv_sec
== tvout
.tv_sec
&&
2808 nuidp
->nu_timestamp
.tv_usec
> tvout
.tv_usec
)) {
2809 nuidp
->nu_expire
= 0;
2811 (NFSERR_AUTHERR
|AUTH_REJECTVERF
);
2812 nd
->nd_procnum
= NFSPROC_NOOP
;
2815 nfsrv_setcred(&nuidp
->nu_cr
, &nd
->nd_cr
);
2816 nd
->nd_flag
|= ND_KERBNICK
;
2819 nd
->nd_repstat
= (NFSERR_AUTHERR
| AUTH_REJECTCRED
);
2820 nd
->nd_procnum
= NFSPROC_NOOP
;
2825 * For nqnfs, get piggybacked lease request.
2827 if (nqnfs
&& nd
->nd_procnum
!= NQNFSPROC_EVICTED
) {
2828 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
2829 nd
->nd_flag
|= fxdr_unsigned(int, *tl
);
2830 if (nd
->nd_flag
& ND_LEASE
) {
2831 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
2832 nd
->nd_duration
= fxdr_unsigned(int, *tl
);
2834 nd
->nd_duration
= NQ_MINLEASE
;
2836 nd
->nd_duration
= NQ_MINLEASE
;
2845 * Search for a sleeping nfsd and wake it up.
2846 * SIDE EFFECT: If none found, set NFSD_CHECKSLP flag, so that one of the
2847 * running nfsds will go look for the work in the nfssvc_sock list.
2851 struct nfssvc_sock
*slp
;
2853 register struct nfsd
*nd
;
2855 if ((slp
->ns_flag
& SLP_VALID
) == 0)
2857 TAILQ_FOREACH(nd
, &nfsd_head
, nfsd_chain
) {
2858 if (nd
->nfsd_flag
& NFSD_WAITING
) {
2859 nd
->nfsd_flag
&= ~NFSD_WAITING
;
2861 panic("nfsd wakeup");
2864 wakeup((caddr_t
)nd
);
2868 slp
->ns_flag
|= SLP_DOREC
;
2869 nfsd_head_flag
|= NFSD_CHECKSLP
;
2871 #endif /* NFS_NOSERVER */
2874 nfs_msg(p
, server
, msg
, error
)
2876 const char *server
, *msg
;
2882 tpr
= tprintf_open(p
);
2886 tprintf(tpr
, "nfs server %s: %s, error %d\n", server
, msg
,
2889 tprintf(tpr
, "nfs server %s: %s\n", server
, msg
);
2895 nfs_down(rep
, msg
, error
)
2902 if (rep
== NULL
|| rep
->r_nmp
== NULL
)
2904 if (!(rep
->r_nmp
->nm_state
& NFSSTA_TIMEO
)) {
2905 vfs_event_signal(&rep
->r_nmp
->nm_mountp
->mnt_stat
.f_fsid
,
2907 rep
->r_nmp
->nm_state
|= NFSSTA_TIMEO
;
2909 rep
->r_flags
|= R_TPRINTFMSG
;
2910 nfs_msg(rep
->r_procp
, rep
->r_nmp
->nm_mountp
->mnt_stat
.f_mntfromname
,
2915 nfs_up(rep
, msg
, error
)
2921 if (error
!= 0 || rep
== NULL
|| rep
->r_nmp
== NULL
)
2923 if ((rep
->r_flags
& R_TPRINTFMSG
) != 0)
2924 nfs_msg(rep
->r_procp
,
2925 rep
->r_nmp
->nm_mountp
->mnt_stat
.f_mntfromname
, msg
, 0);
2926 if ((rep
->r_nmp
->nm_state
& NFSSTA_TIMEO
)) {
2927 rep
->r_nmp
->nm_state
&= ~NFSSTA_TIMEO
;
2928 vfs_event_signal(&rep
->r_nmp
->nm_mountp
->mnt_stat
.f_fsid
,