2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1989, 1991, 1993, 1995
25 * The Regents of the University of California. All rights reserved.
27 * This code is derived from software contributed to Berkeley by
28 * Rick Macklem at The University of Guelph.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * @(#)nfs_socket.c 8.5 (Berkeley) 3/30/95
59 * FreeBSD-Id: nfs_socket.c,v 1.30 1997/10/28 15:59:07 bde Exp $
63 * Socket operations for use by nfs
66 #include <sys/param.h>
67 #include <sys/systm.h>
69 #include <sys/mount.h>
70 #include <sys/kernel.h>
72 #include <sys/malloc.h>
73 #include <sys/vnode.h>
74 #include <sys/domain.h>
75 #include <sys/protosw.h>
76 #include <sys/socket.h>
77 #include <sys/socketvar.h>
78 #include <sys/syslog.h>
79 #include <sys/tprintf.h>
80 #include <machine/spl.h>
83 #include <kern/clock.h>
85 #include <netinet/in.h>
86 #include <netinet/tcp.h>
88 #include <nfs/rpcv2.h>
89 #include <nfs/nfsproto.h>
91 #include <nfs/xdr_subs.h>
92 #include <nfs/nfsm_subs.h>
93 #include <nfs/nfsmount.h>
94 #include <nfs/nfsnode.h>
95 #include <nfs/nfsrtt.h>
96 #include <nfs/nqnfs.h>
102 * Estimate rto for an nfs rpc sent via. an unreliable datagram.
103 * Use the mean and mean deviation of rtt for the appropriate type of rpc
104 * for the frequent rpcs and a default for the others.
105 * The justification for doing "other" this way is that these rpcs
106 * happen so infrequently that timer est. would probably be stale.
107 * Also, since many of these rpcs are
108 * non-idempotent, a conservative timeout is desired.
109 * getattr, lookup - A+2D
113 #define NFS_RTO(n, t) \
114 ((t) == 0 ? (n)->nm_timeo : \
116 (((((n)->nm_srtt[t-1] + 3) >> 2) + (n)->nm_sdrtt[t-1] + 1) >> 1) : \
117 ((((n)->nm_srtt[t-1] + 7) >> 3) + (n)->nm_sdrtt[t-1] + 1)))
118 #define NFS_SRTT(r) (r)->r_nmp->nm_srtt[proct[(r)->r_procnum] - 1]
119 #define NFS_SDRTT(r) (r)->r_nmp->nm_sdrtt[proct[(r)->r_procnum] - 1]
121 * External data, mostly RPC constants in XDR form
123 extern u_long rpc_reply
, rpc_msgdenied
, rpc_mismatch
, rpc_vers
, rpc_auth_unix
,
124 rpc_msgaccepted
, rpc_call
, rpc_autherr
,
126 extern u_long nfs_prog
, nqnfs_prog
;
127 extern time_t nqnfsstarttime
;
128 extern struct nfsstats nfsstats
;
129 extern int nfsv3_procid
[NFS_NPROCS
];
130 extern int nfs_ticks
;
133 * Defines which timer to use for the procnum.
140 static int proct
[NFS_NPROCS
] = {
141 0, 1, 0, 2, 1, 3, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0,
146 * There is a congestion window for outstanding rpcs maintained per mount
147 * point. The cwnd size is adjusted in roughly the way that:
148 * Van Jacobson, Congestion avoidance and Control, In "Proceedings of
149 * SIGCOMM '88". ACM, August 1988.
150 * describes for TCP. The cwnd size is chopped in half on a retransmit timeout
151 * and incremented by 1/cwnd when each rpc reply is received and a full cwnd
152 * of rpcs is in progress.
153 * (The sent count and cwnd are scaled for integer arith.)
154 * Variants of "slow start" were tried and were found to be too much of a
155 * performance hit (ave. rtt 3 times larger),
156 * I suspect due to the large rtt that nfs rpcs have.
158 #define NFS_CWNDSCALE 256
159 #define NFS_MAXCWND (NFS_CWNDSCALE * 32)
160 static int nfs_backoff
[8] = { 2, 4, 8, 16, 32, 64, 128, 256, };
162 struct nfsrtt nfsrtt
;
164 static int nfs_msg
__P((struct proc
*,char *,char *));
165 static int nfs_rcvlock
__P((struct nfsreq
*));
166 static void nfs_rcvunlock
__P((int *flagp
));
167 static int nfs_receive
__P((struct nfsreq
*rep
, struct mbuf
**aname
,
169 static int nfs_reconnect
__P((struct nfsreq
*rep
));
171 static int nfsrv_getstream
__P((struct nfssvc_sock
*,int));
173 int (*nfsrv3_procs
[NFS_NPROCS
]) __P((struct nfsrv_descript
*nd
,
174 struct nfssvc_sock
*slp
,
176 struct mbuf
**mreqp
)) = {
204 #endif /* NFS_NOSERVER */
207 int nfstraceindx
= 0;
208 struct nfstracerec nfstracebuf
[NFSTBUFSIZ
] = {{0,0,0,0}};
210 #define NFSTRACESUSPENDERS
211 #ifdef NFSTRACESUSPENDERS
212 uint nfstracemask
= 0xfff00200;
213 int nfstracexid
= -1;
214 uint onfstracemask
= 0;
215 int nfstracesuspend
= -1;
216 #define NFSTRACE_SUSPEND \
218 if (nfstracemask) { \
219 onfstracemask = nfstracemask; \
223 #define NFSTRACE_RESUME \
225 nfstracesuspend = -1; \
227 nfstracemask = onfstracemask; \
229 #define NFSTRACE_STARTSUSPENDCOUNTDOWN \
231 nfstracesuspend = (nfstraceindx+100) % NFSTBUFSIZ; \
233 #define NFSTRACE_SUSPENDING (nfstracesuspend != -1)
234 #define NFSTRACE_SUSPENSEOVER \
235 (nfstracesuspend > 100 ? \
236 (nfstraceindx >= nfstracesuspend || \
237 nfstraceindx < nfstracesuspend - 100) : \
238 (nfstraceindx >= nfstracesuspend && \
239 nfstraceindx < nfstracesuspend + 8192 - 100))
241 uint nfstracemask
= 0;
242 #endif /* NFSTRACESUSPENDERS */
247 int nfsoprocnum
, nfsolen
;
248 int nfsbt
[32], nfsbtlen
;
252 backtrace(int *where
, int size
)
254 int register sp
, *fp
, numsaved
;
256 __asm__
volatile("mr %0,r1" : "=r" (sp
));
258 fp
= (int *)*((int *)sp
);
260 for (numsaved
= 0; numsaved
< size
; numsaved
++) {
268 #elif defined(__i386__)
272 return (0); /* Till someone implements a real routine */
275 #error architecture not implemented.
279 nfsdup(struct nfsreq
*rep
)
281 int *ip
, i
, first
= 1, end
;
285 if ((nfs_debug
& NFS_DEBUG_DUP
) == 0)
287 /* last mbuf in chain will be nfs content */
288 for (mb
= rep
->r_mreq
; mb
->m_next
; mb
= mb
->m_next
)
290 if (rep
->r_procnum
== nfsoprocnum
&& mb
->m_len
== nfsolen
&&
291 !bcmp((caddr_t
)nfsodata
, mb
->m_data
, nfsolen
)) {
292 s
= b
+ sprintf(b
, "nfsdup x=%x p=%d h=", rep
->r_xid
,
294 end
= (int)(VTONFS(rep
->r_vp
)->n_fhp
);
295 ip
= (int *)(end
& ~3);
296 end
+= VTONFS(rep
->r_vp
)->n_fhsize
;
297 while ((int)ip
< end
) {
299 if (first
) { /* avoid leading zeroes */
303 s
+= sprintf(s
, "%x", i
);
305 s
+= sprintf(s
, "%08x", i
);
309 else /* eliminate trailing zeroes */
313 * set a breakpoint here and you can view the
314 * current backtrace and the one saved in nfsbt
318 nfsoprocnum
= rep
->r_procnum
;
320 bcopy(mb
->m_data
, (caddr_t
)nfsodata
, mb
->m_len
);
321 nfsbtlen
= backtrace(&nfsbt
, sizeof(nfsbt
));
326 * Initialize sockets and congestion for a new NFS connection.
327 * We do not free the sockaddr if error.
330 nfs_connect(nmp
, rep
)
331 register struct nfsmount
*nmp
;
334 register struct socket
*so
;
335 int s
, error
, rcvreserve
, sndreserve
;
336 struct sockaddr
*saddr
;
337 struct sockaddr_in sin
;
340 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
341 nmp
->nm_so
= (struct socket
*)0;
342 saddr
= mtod(nmp
->nm_nam
, struct sockaddr
*);
343 error
= socreate(saddr
->sa_family
, &nmp
->nm_so
, nmp
->nm_sotype
,
349 nmp
->nm_soflags
= so
->so_proto
->pr_flags
;
352 * Some servers require that the client port be a reserved port number.
354 if (saddr
->sa_family
== AF_INET
&& (nmp
->nm_flag
& NFSMNT_RESVPORT
)) {
355 sin
.sin_len
= sizeof (struct sockaddr_in
);
356 sin
.sin_family
= AF_INET
;
357 sin
.sin_addr
.s_addr
= INADDR_ANY
;
358 tport
= IPPORT_RESERVED
- 1;
359 sin
.sin_port
= htons(tport
);
361 while ((error
= sobind(so
, (struct sockaddr
*) &sin
) == EADDRINUSE
) &&
362 (--tport
> IPPORT_RESERVED
/ 2))
363 sin
.sin_port
= htons(tport
);
370 * Protocols that do not require connections may be optionally left
371 * unconnected for servers that reply from a port other than NFS_PORT.
373 if (nmp
->nm_flag
& NFSMNT_NOCONN
) {
374 if (nmp
->nm_soflags
& PR_CONNREQUIRED
) {
379 error
= soconnect(so
, mtod(nmp
->nm_nam
, struct sockaddr
*));
385 * Wait for the connection to complete. Cribbed from the
386 * connect system call but with the wait timing out so
387 * that interruptible mounts don't hang here for a long time.
390 while ((so
->so_state
& SS_ISCONNECTING
) && so
->so_error
== 0) {
391 (void) tsleep((caddr_t
)&so
->so_timeo
, PSOCK
,
393 if ((so
->so_state
& SS_ISCONNECTING
) &&
394 so
->so_error
== 0 && rep
&&
395 (error
= nfs_sigintr(nmp
, rep
, rep
->r_procp
))) {
396 so
->so_state
&= ~SS_ISCONNECTING
;
402 error
= so
->so_error
;
409 if (nmp
->nm_flag
& (NFSMNT_SOFT
| NFSMNT_INT
)) {
410 so
->so_rcv
.sb_timeo
= (5 * hz
);
411 so
->so_snd
.sb_timeo
= (5 * hz
);
413 so
->so_rcv
.sb_timeo
= 0;
414 so
->so_snd
.sb_timeo
= 0;
416 if (nmp
->nm_sotype
== SOCK_DGRAM
) {
417 sndreserve
= (nmp
->nm_wsize
+ NFS_MAXPKTHDR
) * 2;
418 rcvreserve
= (nmp
->nm_rsize
+ NFS_MAXPKTHDR
) * 2;
419 } else if (nmp
->nm_sotype
== SOCK_SEQPACKET
) {
420 sndreserve
= (nmp
->nm_wsize
+ NFS_MAXPKTHDR
) * 2;
421 rcvreserve
= (nmp
->nm_rsize
+ NFS_MAXPKTHDR
) * 2;
423 if (nmp
->nm_sotype
!= SOCK_STREAM
)
424 panic("nfscon sotype");
426 if (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) {
430 bzero(&sopt
, sizeof sopt
);
431 sopt
.sopt_level
= SOL_SOCKET
;
432 sopt
.sopt_name
= SO_KEEPALIVE
;
433 sopt
.sopt_val
= &val
;
434 sopt
.sopt_valsize
= sizeof val
;
438 if (so
->so_proto
->pr_protocol
== IPPROTO_TCP
) {
442 bzero(&sopt
, sizeof sopt
);
443 sopt
.sopt_level
= IPPROTO_TCP
;
444 sopt
.sopt_name
= TCP_NODELAY
;
445 sopt
.sopt_val
= &val
;
446 sopt
.sopt_valsize
= sizeof val
;
451 sndreserve
= (nmp
->nm_wsize
+ NFS_MAXPKTHDR
+ sizeof (u_long
))
453 rcvreserve
= (nmp
->nm_rsize
+ NFS_MAXPKTHDR
+ sizeof (u_long
))
457 error
= soreserve(so
, sndreserve
, rcvreserve
);
461 so
->so_rcv
.sb_flags
|= SB_NOINTR
;
462 so
->so_snd
.sb_flags
|= SB_NOINTR
;
464 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
466 /* Initialize other non-zero congestion variables */
467 nmp
->nm_srtt
[0] = nmp
->nm_srtt
[1] = nmp
->nm_srtt
[2] =
468 nmp
->nm_srtt
[3] = (NFS_TIMEO
<< 3);
469 nmp
->nm_sdrtt
[0] = nmp
->nm_sdrtt
[1] = nmp
->nm_sdrtt
[2] =
470 nmp
->nm_sdrtt
[3] = 0;
471 nmp
->nm_cwnd
= NFS_MAXCWND
/ 2; /* Initial send window */
473 NFSTRACE4(NFSTRC_CWND_INIT
, nmp
, nmp
->nm_flag
, nmp
->nm_soflags
,
475 nmp
->nm_timeouts
= 0;
479 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
486 * Called when a connection is broken on a reliable protocol.
487 * - clean up the old socket
488 * - nfs_connect() again
489 * - set R_MUSTRESEND for all outstanding requests on mount point
490 * If this fails the mount point is DEAD!
491 * nb: Must be called with the nfs_sndlock() set on the mount point.
495 register struct nfsreq
*rep
;
497 register struct nfsreq
*rp
;
498 register struct nfsmount
*nmp
= rep
->r_nmp
;
502 while ((error
= nfs_connect(nmp
, rep
))) {
503 if (error
== EINTR
|| error
== ERESTART
)
505 (void) tsleep((caddr_t
)&lbolt
, PSOCK
, "nfscon", 0);
508 NFS_DPF(DUP
, ("nfs_reconnect RESEND\n"));
510 * Loop through outstanding request list and fix up all requests
513 for (rp
= nfs_reqq
.tqh_first
; rp
!= 0; rp
= rp
->r_chain
.tqe_next
) {
514 if (rp
->r_nmp
== nmp
)
515 rp
->r_flags
|= R_MUSTRESEND
;
521 * NFS disconnect. Clean up and unlink.
525 register struct nfsmount
*nmp
;
527 register struct socket
*so
;
529 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
532 nmp
->nm_so
= (struct socket
*)0;
536 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
540 * This is the nfs send routine. For connection based socket types, it
541 * must be called with an nfs_sndlock() on the socket.
542 * "rep == NULL" indicates that it has been called from a server.
543 * For the client side:
544 * - return EINTR if the RPC is terminated, 0 otherwise
545 * - set R_MUSTRESEND if the send fails for any reason
546 * - do any cleanup required by recoverable socket errors (???)
547 * For the server side:
548 * - return EINTR or ERESTART if interrupted by a signal
549 * - return EPIPE if a connection is lost for connection based sockets (TCP...)
550 * - do any cleanup required by recoverable socket errors (???)
553 nfs_send(so
, nam
, top
, rep
)
554 register struct socket
*so
;
556 register struct mbuf
*top
;
559 struct sockaddr
*sendnam
;
560 int error
, soflags
, flags
;
563 char savenametolog
[MNAMELEN
];
566 if (rep
->r_flags
& R_SOFTTERM
) {
570 if ((so
= rep
->r_nmp
->nm_so
) == NULL
) {
571 rep
->r_flags
|= R_MUSTRESEND
;
575 rep
->r_flags
&= ~R_MUSTRESEND
;
576 soflags
= rep
->r_nmp
->nm_soflags
;
577 for (rp
= nfs_reqq
.tqh_first
; rp
; rp
= rp
->r_chain
.tqe_next
)
581 xidqueued
= rp
->r_xid
;
583 soflags
= so
->so_proto
->pr_flags
;
584 if ((soflags
& PR_CONNREQUIRED
) || (so
->so_state
& SS_ISCONNECTED
) ||
586 sendnam
= (struct sockaddr
*)0;
588 sendnam
= mtod(nam
, struct sockaddr
*);
590 if (so
->so_type
== SOCK_SEQPACKET
)
600 * Save the name here in case mount point goes away when we switch
601 * funnels. The name is using local stack and is large, but don't
602 * want to block if we malloc.
605 strncpy(savenametolog
,
606 rep
->r_nmp
->nm_mountp
->mnt_stat
.f_mntfromname
,
608 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
609 error
= sosend(so
, sendnam
, (struct uio
*)0, top
,
610 (struct mbuf
*)0, flags
);
611 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
616 for (rp
= nfs_reqq
.tqh_first
; rp
;
617 rp
= rp
->r_chain
.tqe_next
)
618 if (rp
== rep
&& rp
->r_xid
== xidqueued
)
621 panic("nfs_send: error %d xid %x gone",
624 log(LOG_INFO
, "nfs send error %d for server %s\n",
625 error
, savenametolog
);
627 * Deal with errors for the client side.
629 if (rep
->r_flags
& R_SOFTTERM
)
632 rep
->r_flags
|= R_MUSTRESEND
;
634 ("nfs_send RESEND error=%d\n", error
));
637 log(LOG_INFO
, "nfsd send error %d\n", error
);
640 * Handle any recoverable (soft) socket errors here. (???)
642 if (error
!= EINTR
&& error
!= ERESTART
&&
643 error
!= EWOULDBLOCK
&& error
!= EPIPE
)
650 * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all
651 * done by soreceive(), but for SOCK_STREAM we must deal with the Record
652 * Mark and consolidate the data into a new mbuf list.
653 * nb: Sometimes TCP passes the data up to soreceive() in long lists of
655 * For SOCK_STREAM we must be very careful to read an entire record once
656 * we have read any of it, even if the system call has been interrupted.
659 nfs_receive(rep
, aname
, mp
)
660 register struct nfsreq
*rep
;
664 register struct socket
*so
;
667 register struct mbuf
*m
;
668 struct mbuf
*control
;
670 struct sockaddr
**getnam
;
671 struct sockaddr
*tmp_nam
;
673 struct sockaddr_in
*sin
;
674 int error
, sotype
, rcvflg
;
675 struct proc
*p
= current_proc(); /* XXX */
678 * Set up arguments for soreceive()
680 *mp
= (struct mbuf
*)0;
681 *aname
= (struct mbuf
*)0;
682 sotype
= rep
->r_nmp
->nm_sotype
;
685 * For reliable protocols, lock against other senders/receivers
686 * in case a reconnect is necessary.
687 * For SOCK_STREAM, first get the Record Mark to find out how much
688 * more there is to get.
689 * We must lock the socket against other receivers
690 * until we have an entire rpc request/reply.
692 if (sotype
!= SOCK_DGRAM
) {
693 error
= nfs_sndlock(&rep
->r_nmp
->nm_flag
, rep
);
698 * Check for fatal errors and resending request.
701 * Ugh: If a reconnect attempt just happened, nm_so
702 * would have changed. NULL indicates a failed
703 * attempt that has essentially shut down this
706 if (rep
->r_mrep
|| (rep
->r_flags
& R_SOFTTERM
)) {
707 nfs_sndunlock(&rep
->r_nmp
->nm_flag
);
710 so
= rep
->r_nmp
->nm_so
;
712 error
= nfs_reconnect(rep
);
714 nfs_sndunlock(&rep
->r_nmp
->nm_flag
);
719 while (rep
->r_flags
& R_MUSTRESEND
) {
720 m
= m_copym(rep
->r_mreq
, 0, M_COPYALL
, M_WAIT
);
721 nfsstats
.rpcretries
++;
723 ("nfs_receive RESEND %s\n",
724 rep
->r_nmp
->nm_mountp
->mnt_stat
.f_mntfromname
));
725 error
= nfs_send(so
, rep
->r_nmp
->nm_nam
, m
, rep
);
727 * we also hold rcv lock so rep is still
731 if (error
== EINTR
|| error
== ERESTART
||
732 (error
= nfs_reconnect(rep
))) {
733 nfs_sndunlock(&rep
->r_nmp
->nm_flag
);
739 nfs_sndunlock(&rep
->r_nmp
->nm_flag
);
740 if (sotype
== SOCK_STREAM
) {
741 aio
.iov_base
= (caddr_t
) &len
;
742 aio
.iov_len
= sizeof(u_long
);
745 auio
.uio_segflg
= UIO_SYSSPACE
;
746 auio
.uio_rw
= UIO_READ
;
748 auio
.uio_resid
= sizeof(u_long
);
751 rcvflg
= MSG_WAITALL
;
752 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
753 error
= soreceive(so
, (struct sockaddr
**)0, &auio
,
754 (struct mbuf
**)0, (struct mbuf
**)0, &rcvflg
);
755 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
756 if (!rep
->r_nmp
) /* if unmounted then bailout */
758 if (error
== EWOULDBLOCK
&& rep
) {
759 if (rep
->r_flags
& R_SOFTTERM
)
762 } while (error
== EWOULDBLOCK
);
763 if (!error
&& auio
.uio_resid
> 0) {
765 "short receive (%d/%d) from nfs server %s\n",
766 sizeof(u_long
) - auio
.uio_resid
,
768 rep
->r_nmp
->nm_mountp
->mnt_stat
.f_mntfromname
);
773 len
= ntohl(len
) & ~0x80000000;
775 * This is SERIOUS! We are out of sync with the sender
776 * and forcing a disconnect/reconnect is all I can do.
778 if (len
> NFS_MAXPACKET
) {
779 log(LOG_ERR
, "%s (%d) from nfs server %s\n",
780 "impossible packet length",
782 rep
->r_nmp
->nm_mountp
->mnt_stat
.f_mntfromname
);
786 auio
.uio_resid
= len
;
788 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
790 rcvflg
= MSG_WAITALL
;
791 error
= soreceive(so
, (struct sockaddr
**)0,
792 &auio
, mp
, (struct mbuf
**)0, &rcvflg
);
793 if (!rep
->r_nmp
) /* if unmounted then bailout */ {
794 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
797 } while (error
== EWOULDBLOCK
|| error
== EINTR
||
800 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
802 if (!error
&& auio
.uio_resid
> 0) {
804 "short receive (%d/%d) from nfs server %s\n",
805 len
- auio
.uio_resid
, len
,
806 rep
->r_nmp
->nm_mountp
->mnt_stat
.f_mntfromname
);
811 * NB: Since uio_resid is big, MSG_WAITALL is ignored
812 * and soreceive() will return when it has either a
813 * control msg or a data msg.
814 * We have no use for control msg., but must grab them
815 * and then throw them away so we know what is going
818 auio
.uio_resid
= len
= 100000000; /* Anything Big */
821 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
824 error
= soreceive(so
, (struct sockaddr
**)0,
825 &auio
, mp
, &control
, &rcvflg
);
826 if (!rep
->r_nmp
) /* if unmounted then bailout */ {
827 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
832 if (error
== EWOULDBLOCK
&& rep
) {
833 if (rep
->r_flags
& R_SOFTTERM
) {
834 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
838 } while (error
== EWOULDBLOCK
||
839 (!error
&& *mp
== NULL
&& control
));
841 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
843 if ((rcvflg
& MSG_EOR
) == 0)
845 if (!error
&& *mp
== NULL
)
847 len
-= auio
.uio_resid
;
850 if (error
&& error
!= EINTR
&& error
!= ERESTART
) {
852 *mp
= (struct mbuf
*)0;
855 "receive error %d from nfs server %s\n",
857 rep
->r_nmp
->nm_mountp
->mnt_stat
.f_mntfromname
);
858 error
= nfs_sndlock(&rep
->r_nmp
->nm_flag
, rep
);
860 error
= nfs_reconnect(rep
);
865 if ((so
= rep
->r_nmp
->nm_so
) == NULL
)
867 if (so
->so_state
& SS_ISCONNECTED
)
868 getnam
= (struct sockaddr
**)0;
871 auio
.uio_resid
= len
= 1000000;
874 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
877 error
= soreceive(so
, getnam
, &auio
, mp
,
878 (struct mbuf
**)0, &rcvflg
);
880 if ((getnam
) && (*getnam
)) {
881 MGET(mhck
, M_WAIT
, MT_SONAME
);
882 mhck
->m_len
= (*getnam
)->sa_len
;
883 sin
= mtod(mhck
, struct sockaddr_in
*);
884 bcopy(*getnam
, sin
, sizeof(struct sockaddr_in
));
885 mhck
->m_hdr
.mh_len
= sizeof(struct sockaddr_in
);
886 FREE(*getnam
, M_SONAME
);
889 if (!rep
->r_nmp
) /* if unmounted then bailout */ {
890 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
894 if (error
== EWOULDBLOCK
&&
895 (rep
->r_flags
& R_SOFTTERM
)) {
896 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
899 } while (error
== EWOULDBLOCK
);
901 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
902 len
-= auio
.uio_resid
;
907 *mp
= (struct mbuf
*)0;
913 * Implement receipt of reply on a socket.
914 * We must search through the list of received datagrams matching them
915 * with outstanding requests using the xid, until ours is found.
920 struct nfsreq
*myrep
;
922 register struct nfsreq
*rep
;
923 register struct nfsmount
*nmp
= myrep
->r_nmp
;
925 struct mbuf
*mrep
, *md
;
932 * Loop around until we get our own reply
936 * Lock against other receivers so that I don't get stuck in
937 * sbwait() after someone else has received my reply for me.
938 * Also necessary for connection based protocols to avoid
939 * race conditions during a reconnect.
940 * If nfs_rcvlock() returns EALREADY, that means that
941 * the reply has already been recieved by another
942 * process and we can return immediately. In this
943 * case, the lock is not taken to avoid races with
946 error
= nfs_rcvlock(myrep
);
947 if (error
== EALREADY
)
953 * If we slept after putting bits otw, then reply may have
954 * arrived. In which case returning is required, or we
955 * would hang trying to nfs_receive an already received reply.
957 if (myrep
->r_mrep
!= NULL
) {
958 nfs_rcvunlock(&nmp
->nm_flag
);
959 NFSTRACE4(NFSTRC_RCVALREADY
, myrep
->r_xid
, myrep
,
964 * Get the next Rpc reply off the socket. Assume myrep->r_nmp
965 * is still in tact by checks done in nfs_rcvlock.
967 error
= nfs_receive(myrep
, &nam
, &mrep
);
969 * Bailout asap if nfsmount struct gone (unmounted).
972 NFSTRACE4(NFSTRC_ECONN
, myrep
->r_xid
, myrep
, nmp
, 2);
973 return (ECONNABORTED
);
976 NFSTRACE4(NFSTRC_RCVERR
, myrep
->r_xid
, myrep
, nmp
,
978 nfs_rcvunlock(&nmp
->nm_flag
);
981 * Ignore routing errors on connectionless protocols??
983 if (NFSIGNORE_SOERROR(nmp
->nm_soflags
, error
)) {
984 nmp
->nm_so
->so_error
= 0;
985 if (myrep
->r_flags
& R_GETONEREP
)
995 * We assume all is fine, but if we did not have an error
996 * and mrep is 0, better not dereference it. nfs_receieve
997 * calls soreceive which carefully sets error=0 when it got
998 * errors on sbwait (tsleep). In most cases, I assume that's
999 * so we could go back again. In tcp case, EPIPE is returned.
1000 * In udp, case nfs_receive gets back here with no error and no
1001 * mrep. Is the right fix to have soreceive check for process
1002 * aborted after sbwait and return something non-zero? Should
1003 * nfs_receive give an EPIPE? Too risky to play with those
1004 * two this late in game for a shutdown problem. Instead,
1005 * just check here and get out. (ekn)
1008 NFSTRACE4(NFSTRC_ECONN
, myrep
->r_xid
, myrep
, nmp
, 3);
1009 return (ECONNABORTED
); /* sounds good */
1013 * Get the xid and check that it is an rpc reply
1016 dpos
= mtod(md
, caddr_t
);
1017 nfsm_dissect(tl
, u_long
*, 2*NFSX_UNSIGNED
);
1019 if (*tl
!= rpc_reply
) {
1020 #ifndef NFS_NOSERVER
1021 if (nmp
->nm_flag
& NFSMNT_NQNFS
) {
1022 if (nqnfs_callback(nmp
, mrep
, md
, dpos
))
1023 nfsstats
.rpcinvalid
++;
1025 nfsstats
.rpcinvalid
++;
1029 nfsstats
.rpcinvalid
++;
1033 if (nmp
->nm_flag
& NFSMNT_RCVLOCK
)
1034 nfs_rcvunlock(&nmp
->nm_flag
);
1035 if (myrep
->r_flags
& R_GETONEREP
)
1036 return (0); /* this path used by NQNFS */
1041 * Loop through the request list to match up the reply
1042 * Iff no match, just drop the datagram
1044 for (rep
= nfs_reqq
.tqh_first
; rep
!= 0;
1045 rep
= rep
->r_chain
.tqe_next
) {
1046 if (rep
->r_mrep
== NULL
&& rxid
== rep
->r_xid
) {
1054 rt
= &nfsrtt
.rttl
[nfsrtt
.pos
];
1055 rt
->proc
= rep
->r_procnum
;
1056 rt
->rto
= NFS_RTO(nmp
, proct
[rep
->r_procnum
]);
1057 rt
->sent
= nmp
->nm_sent
;
1058 rt
->cwnd
= nmp
->nm_cwnd
;
1059 if (proct
[rep
->r_procnum
] == 0)
1060 panic("nfs_reply: proct[%d] is zero", rep
->r_procnum
);
1061 rt
->srtt
= nmp
->nm_srtt
[proct
[rep
->r_procnum
] - 1];
1062 rt
->sdrtt
= nmp
->nm_sdrtt
[proct
[rep
->r_procnum
] - 1];
1063 rt
->fsid
= nmp
->nm_mountp
->mnt_stat
.f_fsid
;
1065 if (rep
->r_flags
& R_TIMING
)
1066 rt
->rtt
= rep
->r_rtt
;
1069 nfsrtt
.pos
= (nfsrtt
.pos
+ 1) % NFSRTTLOGSIZ
;
1072 * Update congestion window.
1073 * Do the additive increase of
1076 NFSTRACE4(NFSTRC_CWND_REPLY
, rep
->r_xid
, rep
,
1077 nmp
->nm_sent
, nmp
->nm_cwnd
);
1078 if (nmp
->nm_cwnd
<= nmp
->nm_sent
) {
1080 (NFS_CWNDSCALE
* NFS_CWNDSCALE
+
1081 (nmp
->nm_cwnd
>> 1)) / nmp
->nm_cwnd
;
1082 if (nmp
->nm_cwnd
> NFS_MAXCWND
)
1083 nmp
->nm_cwnd
= NFS_MAXCWND
;
1085 if (!(rep
->r_flags
& R_SENT
))
1086 printf("nfs_reply: unsent xid=%x",
1088 rep
->r_flags
&= ~R_SENT
;
1089 nmp
->nm_sent
-= NFS_CWNDSCALE
;
1091 * Update rtt using a gain of 0.125 on the mean
1092 * and a gain of 0.25 on the deviation.
1094 if (rep
->r_flags
& R_TIMING
) {
1096 * Since the timer resolution of
1097 * NFS_HZ is so course, it can often
1098 * result in r_rtt == 0. Since
1099 * r_rtt == N means that the actual
1100 * rtt is between N+dt and N+2-dt ticks,
1103 if (proct
[rep
->r_procnum
] == 0)
1104 panic("nfs_reply: proct[%d] is zero", rep
->r_procnum
);
1105 t1
= rep
->r_rtt
+ 1;
1106 t1
-= (NFS_SRTT(rep
) >> 3);
1107 NFS_SRTT(rep
) += t1
;
1110 t1
-= (NFS_SDRTT(rep
) >> 2);
1111 NFS_SDRTT(rep
) += t1
;
1113 nmp
->nm_timeouts
= 0;
1117 nfs_rcvunlock(&nmp
->nm_flag
);
1119 * If not matched to a request, drop it.
1120 * If it's mine, get out.
1123 nfsstats
.rpcunexpected
++;
1125 } else if (rep
== myrep
) {
1126 if (rep
->r_mrep
== NULL
)
1127 panic("nfs_reply: nil r_mrep");
1130 NFSTRACE4(NFSTRC_NOTMINE
, myrep
->r_xid
, myrep
, rep
,
1131 rep
? rep
->r_xid
: myrep
->r_flags
);
1132 if (myrep
->r_flags
& R_GETONEREP
)
1133 return (0); /* this path used by NQNFS */
1138 * nfs_request - goes something like this
1139 * - fill in request struct
1140 * - links it into list
1141 * - calls nfs_send() for first transmit
1142 * - calls nfs_receive() to get reply
1143 * - break down rpc header and return with nfs reply pointed to
1145 * nb: always frees up mreq mbuf list
1148 nfs_request(vp
, mrest
, procnum
, procp
, cred
, mrp
, mdp
, dposp
)
1158 register struct mbuf
*m
, *mrep
;
1159 register struct nfsreq
*rep
, *rp
;
1160 register u_long
*tl
;
1162 struct nfsmount
*nmp
;
1163 struct mbuf
*md
, *mheadend
;
1165 char nickv
[RPCX_NICKVERF
];
1166 time_t reqtime
, waituntil
;
1168 int t1
, nqlflag
, cachable
, s
, error
= 0, mrest_len
, auth_len
, auth_type
;
1169 int trylater_delay
= NQ_TRYLATERDEL
, trylater_cnt
= 0, failed_auth
= 0;
1170 int verf_len
, verf_type
;
1173 char *auth_str
, *verf_str
;
1174 NFSKERBKEY_T key
; /* save session key */
1176 nmp
= VFSTONFS(vp
->v_mount
);
1177 MALLOC_ZONE(rep
, struct nfsreq
*,
1178 sizeof(struct nfsreq
), M_NFSREQ
, M_WAITOK
);
1179 NFSTRACE4(NFSTRC_REQ
, vp
, procnum
, nmp
, rep
);
1182 * make sure if we blocked above, that the file system didn't get
1183 * unmounted leaving nmp bogus value to trip on later and crash.
1184 * Note nfs_unmount will set rep->r_nmp if unmounted volume, but we
1185 * aren't that far yet. SO this is best we can do. I wanted to check
1186 * for vp->v_mount = 0 also below, but that caused reboot crash.
1187 * Something must think it's okay for vp-v_mount=0 during booting.
1188 * Thus the best I can do here is see if we still have a vnode.
1191 if (vp
->v_type
== VBAD
) {
1192 NFSTRACE4(NFSTRC_VBAD
, 1, vp
, nmp
, rep
);
1193 _FREE_ZONE((caddr_t
)rep
, sizeof (struct nfsreq
), M_NFSREQ
);
1198 rep
->r_procp
= procp
;
1199 rep
->r_procnum
= procnum
;
1209 * Get the RPC header with authorization.
1212 verf_str
= auth_str
= (char *)0;
1213 if (nmp
->nm_flag
& NFSMNT_KERB
) {
1215 verf_len
= sizeof (nickv
);
1216 auth_type
= RPCAUTH_KERB4
;
1217 bzero((caddr_t
)key
, sizeof (key
));
1218 if (failed_auth
|| nfs_getnickauth(nmp
, cred
, &auth_str
,
1219 &auth_len
, verf_str
, verf_len
)) {
1220 error
= nfs_getauth(nmp
, rep
, cred
, &auth_str
,
1221 &auth_len
, verf_str
, &verf_len
, key
);
1223 _FREE_ZONE((caddr_t
)rep
,
1224 sizeof (struct nfsreq
), M_NFSREQ
);
1230 auth_type
= RPCAUTH_UNIX
;
1231 if (cred
->cr_ngroups
< 1)
1232 panic("nfsreq nogrps");
1233 auth_len
= ((((cred
->cr_ngroups
- 1) > nmp
->nm_numgrps
) ?
1234 nmp
->nm_numgrps
: (cred
->cr_ngroups
- 1)) << 2) +
1237 m
= nfsm_rpchead(cred
, nmp
->nm_flag
, procnum
, auth_type
, auth_len
,
1238 auth_str
, verf_len
, verf_str
, mrest
, mrest_len
, &mheadend
, &xid
);
1240 _FREE(auth_str
, M_TEMP
);
1243 * For stream protocols, insert a Sun RPC Record Mark.
1245 if (nmp
->nm_sotype
== SOCK_STREAM
) {
1246 M_PREPEND(m
, NFSX_UNSIGNED
, M_WAIT
);
1247 *mtod(m
, u_long
*) = htonl(0x80000000 |
1248 (m
->m_pkthdr
.len
- NFSX_UNSIGNED
));
1253 if (nmp
->nm_flag
& NFSMNT_SOFT
)
1254 rep
->r_retry
= nmp
->nm_retry
;
1256 rep
->r_retry
= NFS_MAXREXMIT
+ 1; /* past clip limit */
1257 rep
->r_rtt
= rep
->r_rexmit
= 0;
1258 if (proct
[procnum
] > 0)
1259 rep
->r_flags
= R_TIMING
;
1265 * Do the client side RPC.
1267 nfsstats
.rpcrequests
++;
1269 * Chain request into list of outstanding requests. Be sure
1270 * to put it LAST so timer finds oldest requests first.
1273 TAILQ_INSERT_TAIL(&nfs_reqq
, rep
, r_chain
);
1275 /* Get send time for nqnfs */
1276 reqtime
= time
.tv_sec
;
1279 * If backing off another request or avoiding congestion, don't
1280 * send this one now but let timer do it. If not timing a request,
1283 if (nmp
->nm_so
&& (nmp
->nm_sotype
!= SOCK_DGRAM
||
1284 (nmp
->nm_flag
& NFSMNT_DUMBTIMR
) ||
1285 nmp
->nm_sent
< nmp
->nm_cwnd
)) {
1287 if (nmp
->nm_soflags
& PR_CONNREQUIRED
)
1288 error
= nfs_sndlock(&nmp
->nm_flag
, rep
);
1291 * Set the R_SENT before doing the send in case another thread
1292 * processes the reply before the nfs_send returns here
1295 if ((rep
->r_flags
& R_MUSTRESEND
) == 0) {
1296 NFSTRACE4(NFSTRC_CWND_REQ1
, rep
->r_xid
, rep
,
1297 nmp
->nm_sent
, nmp
->nm_cwnd
);
1298 nmp
->nm_sent
+= NFS_CWNDSCALE
;
1299 rep
->r_flags
|= R_SENT
;
1302 m
= m_copym(m
, 0, M_COPYALL
, M_WAIT
);
1303 error
= nfs_send(nmp
->nm_so
, nmp
->nm_nam
, m
, rep
);
1304 if (nmp
->nm_soflags
& PR_CONNREQUIRED
)
1305 nfs_sndunlock(&nmp
->nm_flag
);
1308 nmp
->nm_sent
-= NFS_CWNDSCALE
;
1309 rep
->r_flags
&= ~R_SENT
;
1317 * Wait for the reply from our send or the timer's.
1319 if (!error
|| error
== EPIPE
)
1320 error
= nfs_reply(rep
);
1323 * RPC done, unlink the request.
1326 for (rp
= nfs_reqq
.tqh_first
; rp
;
1327 rp
= rp
->r_chain
.tqe_next
)
1328 if (rp
== rep
&& rp
->r_xid
== xid
)
1331 panic("nfs_request race, rep %x xid %x", rep
, xid
);
1332 TAILQ_REMOVE(&nfs_reqq
, rep
, r_chain
);
1336 * Decrement the outstanding request count.
1338 if (rep
->r_flags
& R_SENT
) {
1339 NFSTRACE4(NFSTRC_CWND_REQ2
, rep
->r_xid
, rep
, nmp
->nm_sent
,
1341 rep
->r_flags
&= ~R_SENT
; /* paranoia */
1342 nmp
->nm_sent
-= NFS_CWNDSCALE
;
1346 * If there was a successful reply and a tprintf msg.
1347 * tprintf a response.
1349 if (!error
&& (rep
->r_flags
& R_TPRINTFMSG
))
1350 nfs_msg(rep
->r_procp
, nmp
->nm_mountp
->mnt_stat
.f_mntfromname
,
1356 m_freem(rep
->r_mreq
);
1357 NFSTRACE4(NFSTRC_REQERR
, error
, rep
->r_xid
, nmp
, rep
);
1358 _FREE_ZONE((caddr_t
)rep
, sizeof (struct nfsreq
), M_NFSREQ
);
1363 * break down the rpc header and check if ok
1365 nfsm_dissect(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
1366 if (*tl
++ == rpc_msgdenied
) {
1367 if (*tl
== rpc_mismatch
)
1369 else if ((nmp
->nm_flag
& NFSMNT_KERB
) && *tl
++ == rpc_autherr
) {
1372 mheadend
->m_next
= (struct mbuf
*)0;
1374 m_freem(rep
->r_mreq
);
1381 m_freem(rep
->r_mreq
);
1382 NFSTRACE4(NFSTRC_RPCERR
, error
, rep
->r_xid
, nmp
, rep
);
1383 _FREE_ZONE((caddr_t
)rep
, sizeof (struct nfsreq
), M_NFSREQ
);
1388 * Grab any Kerberos verifier, otherwise just throw it away.
1390 verf_type
= fxdr_unsigned(int, *tl
++);
1391 i
= fxdr_unsigned(int, *tl
);
1392 if ((nmp
->nm_flag
& NFSMNT_KERB
) && verf_type
== RPCAUTH_KERB4
) {
1393 error
= nfs_savenickauth(nmp
, cred
, i
, key
, &md
, &dpos
, mrep
);
1397 nfsm_adv(nfsm_rndup(i
));
1398 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
1401 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
1403 error
= fxdr_unsigned(int, *tl
);
1404 if ((nmp
->nm_flag
& NFSMNT_NFSV3
) &&
1405 error
== NFSERR_TRYLATER
) {
1408 waituntil
= time
.tv_sec
+ trylater_delay
;
1410 ("nfs_request %s flag=%x trylater_cnt=%x waituntil=%lx trylater_delay=%x\n",
1411 nmp
->nm_mountp
->mnt_stat
.f_mntfromname
,
1412 nmp
->nm_flag
, trylater_cnt
, waituntil
,
1414 while (time
.tv_sec
< waituntil
)
1415 (void)tsleep((caddr_t
)&lbolt
,
1416 PSOCK
, "nqnfstry", 0);
1417 trylater_delay
*= nfs_backoff
[trylater_cnt
];
1418 if (trylater_cnt
< 7)
1424 * If the File Handle was stale, invalidate the
1425 * lookup cache, just in case.
1427 if (error
== ESTALE
)
1429 if (nmp
->nm_flag
& NFSMNT_NFSV3
) {
1433 error
|= NFSERR_RETERR
;
1436 m_freem(rep
->r_mreq
);
1437 NFSTRACE4(NFSTRC_DISSECTERR
, error
, rep
->r_xid
, nmp
,
1439 _FREE_ZONE((caddr_t
)rep
,
1440 sizeof (struct nfsreq
), M_NFSREQ
);
1445 * For nqnfs, get any lease in reply
1447 if (nmp
->nm_flag
& NFSMNT_NQNFS
) {
1448 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
1451 nqlflag
= fxdr_unsigned(int, *tl
);
1452 nfsm_dissect(tl
, u_long
*, 4*NFSX_UNSIGNED
);
1453 cachable
= fxdr_unsigned(int, *tl
++);
1454 reqtime
+= fxdr_unsigned(int, *tl
++);
1455 if (reqtime
> time
.tv_sec
) {
1456 fxdr_hyper(tl
, &frev
);
1457 nqnfs_clientlease(nmp
, np
, nqlflag
,
1458 cachable
, reqtime
, frev
);
1465 m_freem(rep
->r_mreq
);
1466 NFSTRACE4(NFSTRC_REQFREE
, 0xf0f0f0f0, rep
->r_xid
, nmp
, rep
);
1467 FREE_ZONE((caddr_t
)rep
, sizeof (struct nfsreq
), M_NFSREQ
);
1471 error
= EPROTONOSUPPORT
;
1473 m_freem(rep
->r_mreq
);
1474 NFSTRACE4(NFSTRC_REQFREE
, error
, rep
->r_xid
, nmp
, rep
);
1475 _FREE_ZONE((caddr_t
)rep
, sizeof (struct nfsreq
), M_NFSREQ
);
1479 #ifndef NFS_NOSERVER
1481 * Generate the rpc reply header
1482 * siz arg. is used to decide if adding a cluster is worthwhile
1485 nfs_rephead(siz
, nd
, slp
, err
, cache
, frev
, mrq
, mbp
, bposp
)
1487 struct nfsrv_descript
*nd
;
1488 struct nfssvc_sock
*slp
;
1496 register u_long
*tl
;
1497 register struct mbuf
*mreq
;
1499 struct mbuf
*mb
, *mb2
;
1501 MGETHDR(mreq
, M_WAIT
, MT_DATA
);
1504 * If this is a big reply, use a cluster else
1505 * try and leave leading space for the lower level headers.
1507 siz
+= RPC_REPLYSIZ
;
1508 if (siz
>= MINCLSIZE
) {
1509 MCLGET(mreq
, M_WAIT
);
1511 mreq
->m_data
+= max_hdr
;
1512 tl
= mtod(mreq
, u_long
*);
1513 mreq
->m_len
= 6 * NFSX_UNSIGNED
;
1514 bpos
= ((caddr_t
)tl
) + mreq
->m_len
;
1515 *tl
++ = txdr_unsigned(nd
->nd_retxid
);
1517 if (err
== ERPCMISMATCH
|| (err
& NFSERR_AUTHERR
)) {
1518 *tl
++ = rpc_msgdenied
;
1519 if (err
& NFSERR_AUTHERR
) {
1520 *tl
++ = rpc_autherr
;
1521 *tl
= txdr_unsigned(err
& ~NFSERR_AUTHERR
);
1522 mreq
->m_len
-= NFSX_UNSIGNED
;
1523 bpos
-= NFSX_UNSIGNED
;
1525 *tl
++ = rpc_mismatch
;
1526 *tl
++ = txdr_unsigned(RPC_VER2
);
1527 *tl
= txdr_unsigned(RPC_VER2
);
1530 *tl
++ = rpc_msgaccepted
;
1533 * For Kerberos authentication, we must send the nickname
1534 * verifier back, otherwise just RPCAUTH_NULL.
1536 if (nd
->nd_flag
& ND_KERBFULL
) {
1537 register struct nfsuid
*nuidp
;
1538 struct timeval ktvin
, ktvout
;
1540 for (nuidp
= NUIDHASH(slp
, nd
->nd_cr
.cr_uid
)->lh_first
;
1541 nuidp
!= 0; nuidp
= nuidp
->nu_hash
.le_next
) {
1542 if (nuidp
->nu_cr
.cr_uid
== nd
->nd_cr
.cr_uid
&&
1543 (!nd
->nd_nam2
|| netaddr_match(NU_NETFAM(nuidp
),
1544 &nuidp
->nu_haddr
, nd
->nd_nam2
)))
1549 txdr_unsigned(nuidp
->nu_timestamp
.tv_sec
- 1);
1551 txdr_unsigned(nuidp
->nu_timestamp
.tv_usec
);
1554 * Encrypt the timestamp in ecb mode using the
1561 *tl
++ = rpc_auth_kerb
;
1562 *tl
++ = txdr_unsigned(3 * NFSX_UNSIGNED
);
1563 *tl
= ktvout
.tv_sec
;
1564 nfsm_build(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
1565 *tl
++ = ktvout
.tv_usec
;
1566 *tl
++ = txdr_unsigned(nuidp
->nu_cr
.cr_uid
);
1577 *tl
= txdr_unsigned(RPC_PROGUNAVAIL
);
1580 *tl
= txdr_unsigned(RPC_PROGMISMATCH
);
1581 nfsm_build(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
1582 if (nd
->nd_flag
& ND_NQNFS
) {
1583 *tl
++ = txdr_unsigned(3);
1584 *tl
= txdr_unsigned(3);
1586 *tl
++ = txdr_unsigned(2);
1587 *tl
= txdr_unsigned(3);
1591 *tl
= txdr_unsigned(RPC_PROCUNAVAIL
);
1594 *tl
= txdr_unsigned(RPC_GARBAGE
);
1598 if (err
!= NFSERR_RETVOID
) {
1599 nfsm_build(tl
, u_long
*, NFSX_UNSIGNED
);
1601 *tl
= txdr_unsigned(nfsrv_errmap(nd
, err
));
1610 * For nqnfs, piggyback lease as requested.
1612 if ((nd
->nd_flag
& ND_NQNFS
) && err
== 0) {
1613 if (nd
->nd_flag
& ND_LEASE
) {
1614 nfsm_build(tl
, u_long
*, 5 * NFSX_UNSIGNED
);
1615 *tl
++ = txdr_unsigned(nd
->nd_flag
& ND_LEASE
);
1616 *tl
++ = txdr_unsigned(cache
);
1617 *tl
++ = txdr_unsigned(nd
->nd_duration
);
1618 txdr_hyper(frev
, tl
);
1620 nfsm_build(tl
, u_long
*, NFSX_UNSIGNED
);
1628 if (err
!= 0 && err
!= NFSERR_RETVOID
)
1629 nfsstats
.srvrpc_errs
++;
1634 #endif /* NFS_NOSERVER */
1638 * From FreeBSD 1.58, a Matt Dillon fix...
1639 * Flag a request as being about to terminate.
1640 * The nm_sent count is decremented now to avoid deadlocks when the process
1641 * in soreceive() hasn't yet managed to send its own request.
1644 nfs_softterm(struct nfsreq
*rep
)
1646 rep
->r_flags
|= R_SOFTTERM
;
1647 if (rep
->r_flags
& R_SENT
) {
1648 NFSTRACE4(NFSTRC_CWND_SOFT
, rep
->r_xid
, rep
,
1649 rep
->r_nmp
->nm_sent
, rep
->r_nmp
->nm_cwnd
);
1650 rep
->r_nmp
->nm_sent
-= NFS_CWNDSCALE
;
1651 rep
->r_flags
&= ~R_SENT
;
1656 nfs_timer_funnel(arg
)
1659 (void) thread_funnel_set(kernel_flock
, TRUE
);
1661 (void) thread_funnel_set(kernel_flock
, FALSE
);
1667 * Scan the nfsreq list and retranmit any requests that have timed out
1668 * To avoid retransmission attempts on STREAM sockets (in the future) make
1669 * sure to set the r_retry field to 0 (implies nm_retry == 0).
1673 void *arg
; /* never used */
1675 register struct nfsreq
*rep
, *rp
;
1676 register struct mbuf
*m
;
1677 register struct socket
*so
;
1678 register struct nfsmount
*nmp
;
1681 #ifndef NFS_NOSERVER
1682 static long lasttime
= 0;
1683 register struct nfssvc_sock
*slp
;
1685 #endif /* NFS_NOSERVER */
1689 int flags
, rexmit
, cwnd
, sent
;
1694 * XXX If preemptable threads are implemented the spls used for the
1695 * outstanding request queue must be replaced with mutexes.
1698 #ifdef NFSTRACESUSPENDERS
1699 if (NFSTRACE_SUSPENDING
) {
1700 for (rep
= nfs_reqq
.tqh_first
; rep
!= 0;
1701 rep
= rep
->r_chain
.tqe_next
)
1702 if (rep
->r_xid
== nfstracexid
)
1706 } else if (NFSTRACE_SUSPENSEOVER
) {
1711 for (rep
= nfs_reqq
.tqh_first
; rep
!= 0; rep
= rep
->r_chain
.tqe_next
) {
1712 #ifdef NFSTRACESUSPENDERS
1713 if (rep
->r_mrep
&& !NFSTRACE_SUSPENDING
) {
1714 nfstracexid
= rep
->r_xid
;
1715 NFSTRACE_STARTSUSPENDCOUNTDOWN
;
1719 if (!nmp
) /* unmounted */
1721 if (rep
->r_mrep
|| (rep
->r_flags
& R_SOFTTERM
))
1723 if (nfs_sigintr(nmp
, rep
, rep
->r_procp
)) {
1727 if (rep
->r_rtt
>= 0) {
1729 if (nmp
->nm_flag
& NFSMNT_DUMBTIMR
)
1730 timeo
= nmp
->nm_timeo
;
1732 timeo
= NFS_RTO(nmp
, proct
[rep
->r_procnum
]);
1733 /* ensure 62.5 ms floor */
1734 while (16 * timeo
< hz
)
1736 if (nmp
->nm_timeouts
> 0)
1737 timeo
*= nfs_backoff
[nmp
->nm_timeouts
- 1];
1738 if (rep
->r_rtt
<= timeo
)
1740 if (nmp
->nm_timeouts
< 8)
1744 * Check for server not responding
1746 if ((rep
->r_flags
& R_TPRINTFMSG
) == 0 &&
1747 rep
->r_rexmit
> nmp
->nm_deadthresh
) {
1748 nfs_msg(rep
->r_procp
,
1749 nmp
->nm_mountp
->mnt_stat
.f_mntfromname
,
1751 rep
->r_flags
|= R_TPRINTFMSG
;
1753 if (rep
->r_rexmit
>= rep
->r_retry
) { /* too many */
1754 nfsstats
.rpctimeouts
++;
1758 if (nmp
->nm_sotype
!= SOCK_DGRAM
) {
1759 if (++rep
->r_rexmit
> NFS_MAXREXMIT
)
1760 rep
->r_rexmit
= NFS_MAXREXMIT
;
1763 if ((so
= nmp
->nm_so
) == NULL
)
1767 * If there is enough space and the window allows..
1769 * Set r_rtt to -1 in case we fail to send it now.
1772 rttdiag
= rep
->r_rtt
;
1775 if (sbspace(&so
->so_snd
) >= rep
->r_mreq
->m_pkthdr
.len
&&
1776 ((nmp
->nm_flag
& NFSMNT_DUMBTIMR
) ||
1777 (rep
->r_flags
& R_SENT
) ||
1778 nmp
->nm_sent
< nmp
->nm_cwnd
) &&
1779 (m
= m_copym(rep
->r_mreq
, 0, M_COPYALL
, M_DONTWAIT
))){
1781 struct proc
*p
= current_proc();
1784 if (rep
->r_flags
& R_SENT
&& nfsprnttimo
&&
1785 nmp
->nm_timeouts
>= nfsprnttimo
) {
1786 int t
= proct
[rep
->r_procnum
];
1788 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]));
1790 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
));
1793 #endif /* NFSDIAG */
1795 * Iff first send, start timing
1796 * else turn timing off, backoff timer
1797 * and divide congestion window by 2.
1798 * We update these *before* the send to avoid
1799 * racing against receiving the reply.
1800 * We save them so we can restore them on send error.
1802 flags
= rep
->r_flags
;
1803 rexmit
= rep
->r_rexmit
;
1804 cwnd
= nmp
->nm_cwnd
;
1805 sent
= nmp
->nm_sent
;
1807 if (rep
->r_flags
& R_SENT
) {
1808 rep
->r_flags
&= ~R_TIMING
;
1809 if (++rep
->r_rexmit
> NFS_MAXREXMIT
)
1810 rep
->r_rexmit
= NFS_MAXREXMIT
;
1812 if (nmp
->nm_cwnd
< NFS_CWNDSCALE
)
1813 nmp
->nm_cwnd
= NFS_CWNDSCALE
;
1814 nfsstats
.rpcretries
++;
1816 rep
->r_flags
|= R_SENT
;
1817 nmp
->nm_sent
+= NFS_CWNDSCALE
;
1819 NFSTRACE4(NFSTRC_CWND_TIMER
, xid
, rep
,
1820 nmp
->nm_sent
, nmp
->nm_cwnd
);
1822 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1824 if ((nmp
->nm_flag
& NFSMNT_NOCONN
) == 0)
1825 error
= (*so
->so_proto
->pr_usrreqs
->pru_send
)
1826 (so
, 0, m
, 0, 0, p
);
1828 error
= (*so
->so_proto
->pr_usrreqs
->pru_send
)
1829 (so
, 0, m
, mtod(nmp
->nm_nam
, struct sockaddr
*), 0, p
);
1831 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1833 NFSTRACE4(NFSTRC_CWND_TIMER
, xid
, error
, sent
, cwnd
);
1835 * This is to fix "nfs_sigintr" DSI panics.
1836 * We may have slept during the send so the current
1837 * place in the request queue may have been released.
1838 * Due to zone_gc it may even be part of an
1839 * unrelated newly allocated data structure.
1840 * Restart the list scan from the top if needed...
1842 for (rp
= nfs_reqq
.tqh_first
; rp
;
1843 rp
= rp
->r_chain
.tqe_next
)
1844 if (rp
== rep
&& rp
->r_xid
== xid
)
1849 panic("nfs_timer: race error %d xid 0x%x\n",
1854 if (NFSIGNORE_SOERROR(nmp
->nm_soflags
, error
))
1856 rep
->r_flags
= flags
;
1857 rep
->r_rexmit
= rexmit
;
1858 nmp
->nm_cwnd
= cwnd
;
1859 nmp
->nm_sent
= sent
;
1861 nfsstats
.rpcretries
--;
1866 #ifndef NFS_NOSERVER
1868 * Call the nqnfs server timer once a second to handle leases.
1870 if (lasttime
!= time
.tv_sec
) {
1871 lasttime
= time
.tv_sec
;
1876 * Scan the write gathering queues for writes that need to be
1879 cur_usec
= (u_quad_t
)time
.tv_sec
* 1000000 + (u_quad_t
)time
.tv_usec
;
1880 for (slp
= nfssvc_sockhead
.tqh_first
; slp
!= 0;
1881 slp
= slp
->ns_chain
.tqe_next
) {
1882 if (slp
->ns_tq
.lh_first
&& slp
->ns_tq
.lh_first
->nd_time
<=cur_usec
)
1883 nfsrv_wakenfsd(slp
);
1885 #endif /* NFS_NOSERVER */
1887 timeout(nfs_timer_funnel
, (void *)0, nfs_ticks
);
1893 * Test for a termination condition pending on the process.
1894 * This is used for NFSMNT_INT mounts.
1897 nfs_sigintr(nmp
, rep
, p
)
1898 struct nfsmount
*nmp
;
1900 register struct proc
*p
;
1903 if (rep
&& (rep
->r_flags
& R_SOFTTERM
))
1905 if (!(nmp
->nm_flag
& NFSMNT_INT
))
1907 if (p
&& p
->p_siglist
&&
1908 (((p
->p_siglist
& ~p
->p_sigmask
) & ~p
->p_sigignore
) &
1915 * Lock a socket against others.
1916 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
1917 * and also to avoid race conditions between the processes with nfs requests
1918 * in progress when a reconnect is necessary.
1921 nfs_sndlock(flagp
, rep
)
1922 register int *flagp
;
1926 int slpflag
= 0, slptimeo
= 0;
1930 if (rep
->r_nmp
->nm_flag
& NFSMNT_INT
)
1933 p
= (struct proc
*)0;
1934 while (*flagp
& NFSMNT_SNDLOCK
) {
1935 if (nfs_sigintr(rep
->r_nmp
, rep
, p
))
1937 *flagp
|= NFSMNT_WANTSND
;
1938 (void) tsleep((caddr_t
)flagp
, slpflag
| (PZERO
- 1), "nfsndlck",
1940 if (slpflag
== PCATCH
) {
1945 * Make sure while we slept that the mountpoint didn't go away.
1946 * nfs_sigintr and callers expect it in tact.
1949 return (ECONNABORTED
); /* don't have lock until out of loop */
1951 *flagp
|= NFSMNT_SNDLOCK
;
1956 * Unlock the stream socket for others.
1959 nfs_sndunlock(flagp
)
1960 register int *flagp
;
1963 if ((*flagp
& NFSMNT_SNDLOCK
) == 0)
1964 panic("nfs sndunlock");
1965 *flagp
&= ~NFSMNT_SNDLOCK
;
1966 if (*flagp
& NFSMNT_WANTSND
) {
1967 *flagp
&= ~NFSMNT_WANTSND
;
1968 wakeup((caddr_t
)flagp
);
1974 register struct nfsreq
*rep
;
1976 register int *flagp
= &rep
->r_nmp
->nm_flag
;
1977 int slpflag
, slptimeo
= 0;
1979 if (*flagp
& NFSMNT_INT
)
1983 while (*flagp
& NFSMNT_RCVLOCK
) {
1984 if (nfs_sigintr(rep
->r_nmp
, rep
, rep
->r_procp
)) {
1985 NFSTRACE4(NFSTRC_RCVLCKINTR
, rep
->r_xid
, rep
,
1986 rep
->r_nmp
, *flagp
);
1988 } else if (rep
->r_mrep
!= NULL
) {
1990 * Don't bother sleeping if reply already arrived
1992 NFSTRACE4(NFSTRC_RCVALREADY
, rep
->r_xid
, rep
,
1996 NFSTRACE4(NFSTRC_RCVLCKW
, rep
->r_xid
, rep
, rep
->r_nmp
, *flagp
);
1997 *flagp
|= NFSMNT_WANTRCV
;
1998 (void) tsleep((caddr_t
)flagp
, slpflag
| (PZERO
- 1), "nfsrcvlk",
2000 if (slpflag
== PCATCH
) {
2005 * Make sure while we slept that the mountpoint didn't go away.
2006 * nfs_sigintr and caller nfs_reply expect it in tact.
2009 return (ECONNABORTED
); /* don't have lock until out of loop */
2012 * nfs_reply will handle it if reply already arrived.
2013 * (We may have slept or been preempted while on network funnel).
2015 NFSTRACE4(NFSTRC_RCVLCK
, rep
->r_xid
, rep
, rep
->r_nmp
, *flagp
);
2016 *flagp
|= NFSMNT_RCVLOCK
;
2021 * Unlock the stream socket for others.
2024 nfs_rcvunlock(flagp
)
2025 register int *flagp
;
2028 if ((*flagp
& NFSMNT_RCVLOCK
) == 0)
2029 panic("nfs rcvunlock");
2030 *flagp
&= ~NFSMNT_RCVLOCK
;
2031 if (*flagp
& NFSMNT_WANTRCV
) {
2032 NFSTRACE(NFSTRC_RCVUNLW
, flagp
);
2033 *flagp
&= ~NFSMNT_WANTRCV
;
2034 wakeup((caddr_t
)flagp
);
2036 NFSTRACE(NFSTRC_RCVUNL
, flagp
);
2041 #ifndef NFS_NOSERVER
2043 * Socket upcall routine for the nfsd sockets.
2044 * The caddr_t arg is a pointer to the "struct nfssvc_sock".
2045 * Essentially do as much as possible non-blocking, else punt and it will
2046 * be called with M_WAIT from an nfsd.
2049 * Needs to eun under network funnel
2052 nfsrv_rcv(so
, arg
, waitflag
)
2057 register struct nfssvc_sock
*slp
= (struct nfssvc_sock
*)arg
;
2058 register struct mbuf
*m
;
2059 struct mbuf
*mp
, *mhck
;
2060 struct sockaddr
*nam
=0;
2063 struct sockaddr_in
*sin
;
2065 if ((slp
->ns_flag
& SLP_VALID
) == 0)
2069 * Define this to test for nfsds handling this under heavy load.
2071 if (waitflag
== M_DONTWAIT
) {
2072 slp
->ns_flag
|= SLP_NEEDQ
; goto dorecs
;
2075 auio
.uio_procp
= NULL
;
2076 if (so
->so_type
== SOCK_STREAM
) {
2078 * If there are already records on the queue, defer soreceive()
2079 * to an nfsd so that there is feedback to the TCP layer that
2080 * the nfs servers are heavily loaded.
2082 if (slp
->ns_rec
&& waitflag
== M_DONTWAIT
) {
2083 slp
->ns_flag
|= SLP_NEEDQ
;
2090 auio
.uio_resid
= 1000000000;
2091 flags
= MSG_DONTWAIT
;
2092 error
= soreceive(so
, (struct sockaddr
**) 0, &auio
, &mp
, (struct mbuf
**)0, &flags
);
2093 if (error
|| mp
== (struct mbuf
*)0) {
2094 if (error
== EWOULDBLOCK
)
2095 slp
->ns_flag
|= SLP_NEEDQ
;
2097 slp
->ns_flag
|= SLP_DISCONN
;
2101 if (slp
->ns_rawend
) {
2102 slp
->ns_rawend
->m_next
= m
;
2103 slp
->ns_cc
+= 1000000000 - auio
.uio_resid
;
2106 slp
->ns_cc
= 1000000000 - auio
.uio_resid
;
2113 * Now try and parse record(s) out of the raw stream data.
2115 error
= nfsrv_getstream(slp
, waitflag
);
2118 slp
->ns_flag
|= SLP_DISCONN
;
2120 slp
->ns_flag
|= SLP_NEEDQ
;
2124 auio
.uio_resid
= 1000000000;
2125 flags
= MSG_DONTWAIT
;
2127 error
= soreceive(so
, &nam
, &auio
, &mp
,
2128 (struct mbuf
**)0, &flags
);
2132 MGET(mhck
, M_WAIT
, MT_SONAME
);
2133 mhck
->m_len
= nam
->sa_len
;
2134 sin
= mtod(mhck
, struct sockaddr_in
*);
2135 bcopy(nam
, sin
, sizeof(struct sockaddr_in
));
2136 mhck
->m_hdr
.mh_len
= sizeof(struct sockaddr_in
);
2137 FREE(nam
, M_SONAME
);
2144 slp
->ns_recend
->m_nextpkt
= m
;
2148 m
->m_nextpkt
= (struct mbuf
*)0;
2151 if ((so
->so_proto
->pr_flags
& PR_CONNREQUIRED
)
2152 && error
!= EWOULDBLOCK
) {
2153 slp
->ns_flag
|= SLP_DISCONN
;
2161 * Now try and process the request records, non-blocking.
2164 if (waitflag
== M_DONTWAIT
&&
2165 (slp
->ns_rec
|| (slp
->ns_flag
& (SLP_NEEDQ
| SLP_DISCONN
)))) {
2166 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
2167 nfsrv_wakenfsd(slp
);
2168 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
2173 * Try and extract an RPC request from the mbuf data list received on a
2174 * stream socket. The "waitflag" argument indicates whether or not it
2178 nfsrv_getstream(slp
, waitflag
)
2179 register struct nfssvc_sock
*slp
;
2182 register struct mbuf
*m
, **mpp
;
2183 register char *cp1
, *cp2
;
2185 struct mbuf
*om
, *m2
, *recm
= 0;
2188 if (slp
->ns_flag
& SLP_GETSTREAM
)
2189 panic("nfs getstream");
2190 slp
->ns_flag
|= SLP_GETSTREAM
;
2192 if (slp
->ns_reclen
== 0) {
2193 if (slp
->ns_cc
< NFSX_UNSIGNED
) {
2194 slp
->ns_flag
&= ~SLP_GETSTREAM
;
2198 if (m
->m_len
>= NFSX_UNSIGNED
) {
2199 bcopy(mtod(m
, caddr_t
), (caddr_t
)&recmark
, NFSX_UNSIGNED
);
2200 m
->m_data
+= NFSX_UNSIGNED
;
2201 m
->m_len
-= NFSX_UNSIGNED
;
2203 cp1
= (caddr_t
)&recmark
;
2204 cp2
= mtod(m
, caddr_t
);
2205 while (cp1
< ((caddr_t
)&recmark
) + NFSX_UNSIGNED
) {
2206 while (m
->m_len
== 0) {
2208 cp2
= mtod(m
, caddr_t
);
2215 slp
->ns_cc
-= NFSX_UNSIGNED
;
2216 recmark
= ntohl(recmark
);
2217 slp
->ns_reclen
= recmark
& ~0x80000000;
2218 if (recmark
& 0x80000000)
2219 slp
->ns_flag
|= SLP_LASTFRAG
;
2221 slp
->ns_flag
&= ~SLP_LASTFRAG
;
2222 if (slp
->ns_reclen
< NFS_MINPACKET
|| slp
->ns_reclen
> NFS_MAXPACKET
) {
2223 slp
->ns_flag
&= ~SLP_GETSTREAM
;
2229 * Now get the record part.
2231 if (slp
->ns_cc
== slp
->ns_reclen
) {
2233 slp
->ns_raw
= slp
->ns_rawend
= (struct mbuf
*)0;
2234 slp
->ns_cc
= slp
->ns_reclen
= 0;
2235 } else if (slp
->ns_cc
> slp
->ns_reclen
) {
2238 om
= (struct mbuf
*)0;
2239 while (len
< slp
->ns_reclen
) {
2240 if ((len
+ m
->m_len
) > slp
->ns_reclen
) {
2241 m2
= m_copym(m
, 0, slp
->ns_reclen
- len
,
2249 m
->m_data
+= slp
->ns_reclen
- len
;
2250 m
->m_len
-= slp
->ns_reclen
- len
;
2251 len
= slp
->ns_reclen
;
2253 slp
->ns_flag
&= ~SLP_GETSTREAM
;
2254 return (EWOULDBLOCK
);
2256 } else if ((len
+ m
->m_len
) == slp
->ns_reclen
) {
2261 om
->m_next
= (struct mbuf
*)0;
2272 slp
->ns_flag
&= ~SLP_GETSTREAM
;
2277 * Accumulate the fragments into a record.
2279 mpp
= &slp
->ns_frag
;
2281 mpp
= &((*mpp
)->m_next
);
2283 if (slp
->ns_flag
& SLP_LASTFRAG
) {
2285 slp
->ns_recend
->m_nextpkt
= slp
->ns_frag
;
2287 slp
->ns_rec
= slp
->ns_frag
;
2288 slp
->ns_recend
= slp
->ns_frag
;
2289 slp
->ns_frag
= (struct mbuf
*)0;
2295 * Parse an RPC header.
2298 nfsrv_dorec(slp
, nfsd
, ndp
)
2299 register struct nfssvc_sock
*slp
;
2301 struct nfsrv_descript
**ndp
;
2303 register struct mbuf
*m
;
2304 register struct mbuf
*nam
;
2305 register struct nfsrv_descript
*nd
;
2309 if ((slp
->ns_flag
& SLP_VALID
) == 0 ||
2310 (m
= slp
->ns_rec
) == (struct mbuf
*)0)
2312 slp
->ns_rec
= m
->m_nextpkt
;
2314 m
->m_nextpkt
= (struct mbuf
*)0;
2316 slp
->ns_recend
= (struct mbuf
*)0;
2317 if (m
->m_type
== MT_SONAME
) {
2323 MALLOC_ZONE(nd
, struct nfsrv_descript
*,
2324 sizeof (struct nfsrv_descript
), M_NFSRVDESC
, M_WAITOK
);
2325 nd
->nd_md
= nd
->nd_mrep
= m
;
2327 nd
->nd_dpos
= mtod(m
, caddr_t
);
2328 error
= nfs_getreq(nd
, nfsd
, TRUE
);
2331 _FREE_ZONE((caddr_t
)nd
, sizeof *nd
, M_NFSRVDESC
);
2340 * Parse an RPC request
2342 * - fill in the cred struct.
2345 nfs_getreq(nd
, nfsd
, has_header
)
2346 register struct nfsrv_descript
*nd
;
2350 register int len
, i
;
2351 register u_long
*tl
;
2355 caddr_t dpos
, cp2
, cp
;
2356 u_long nfsvers
, auth_type
;
2358 int error
= 0, nqnfs
= 0, ticklen
;
2359 struct mbuf
*mrep
, *md
;
2360 register struct nfsuid
*nuidp
;
2361 struct timeval tvin
, tvout
;
2362 #if 0 /* until encrypted keys are implemented */
2363 NFSKERBKEYSCHED_T keys
; /* stores key schedule */
2370 nfsm_dissect(tl
, u_long
*, 10 * NFSX_UNSIGNED
);
2371 nd
->nd_retxid
= fxdr_unsigned(u_long
, *tl
++);
2372 if (*tl
++ != rpc_call
) {
2377 nfsm_dissect(tl
, u_long
*, 8 * NFSX_UNSIGNED
);
2380 if (*tl
++ != rpc_vers
) {
2381 nd
->nd_repstat
= ERPCMISMATCH
;
2382 nd
->nd_procnum
= NFSPROC_NOOP
;
2385 if (*tl
!= nfs_prog
) {
2386 if (*tl
== nqnfs_prog
)
2389 nd
->nd_repstat
= EPROGUNAVAIL
;
2390 nd
->nd_procnum
= NFSPROC_NOOP
;
2395 nfsvers
= fxdr_unsigned(u_long
, *tl
++);
2396 if (((nfsvers
< NFS_VER2
|| nfsvers
> NFS_VER3
) && !nqnfs
) ||
2397 (nfsvers
!= NQNFS_VER3
&& nqnfs
)) {
2398 nd
->nd_repstat
= EPROGMISMATCH
;
2399 nd
->nd_procnum
= NFSPROC_NOOP
;
2403 nd
->nd_flag
= (ND_NFSV3
| ND_NQNFS
);
2404 else if (nfsvers
== NFS_VER3
)
2405 nd
->nd_flag
= ND_NFSV3
;
2406 nd
->nd_procnum
= fxdr_unsigned(u_long
, *tl
++);
2407 if (nd
->nd_procnum
== NFSPROC_NULL
)
2409 if (nd
->nd_procnum
>= NFS_NPROCS
||
2410 (!nqnfs
&& nd
->nd_procnum
>= NQNFSPROC_GETLEASE
) ||
2411 (!nd
->nd_flag
&& nd
->nd_procnum
> NFSV2PROC_STATFS
)) {
2412 nd
->nd_repstat
= EPROCUNAVAIL
;
2413 nd
->nd_procnum
= NFSPROC_NOOP
;
2416 if ((nd
->nd_flag
& ND_NFSV3
) == 0)
2417 nd
->nd_procnum
= nfsv3_procid
[nd
->nd_procnum
];
2419 len
= fxdr_unsigned(int, *tl
++);
2420 if (len
< 0 || len
> RPCAUTH_MAXSIZ
) {
2425 nd
->nd_flag
&= ~ND_KERBAUTH
;
2427 * Handle auth_unix or auth_kerb.
2429 if (auth_type
== rpc_auth_unix
) {
2430 len
= fxdr_unsigned(int, *++tl
);
2431 if (len
< 0 || len
> NFS_MAXNAMLEN
) {
2435 nfsm_adv(nfsm_rndup(len
));
2436 nfsm_dissect(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
2437 bzero((caddr_t
)&nd
->nd_cr
, sizeof (struct ucred
));
2438 nd
->nd_cr
.cr_ref
= 1;
2439 nd
->nd_cr
.cr_uid
= fxdr_unsigned(uid_t
, *tl
++);
2440 nd
->nd_cr
.cr_gid
= fxdr_unsigned(gid_t
, *tl
++);
2441 len
= fxdr_unsigned(int, *tl
);
2442 if (len
< 0 || len
> RPCAUTH_UNIXGIDS
) {
2446 nfsm_dissect(tl
, u_long
*, (len
+ 2) * NFSX_UNSIGNED
);
2447 for (i
= 1; i
<= len
; i
++)
2449 nd
->nd_cr
.cr_groups
[i
] = fxdr_unsigned(gid_t
, *tl
++);
2452 nd
->nd_cr
.cr_ngroups
= (len
>= NGROUPS
) ? NGROUPS
: (len
+ 1);
2453 if (nd
->nd_cr
.cr_ngroups
> 1)
2454 nfsrvw_sort(nd
->nd_cr
.cr_groups
, nd
->nd_cr
.cr_ngroups
);
2455 len
= fxdr_unsigned(int, *++tl
);
2456 if (len
< 0 || len
> RPCAUTH_MAXSIZ
) {
2461 nfsm_adv(nfsm_rndup(len
));
2462 } else if (auth_type
== rpc_auth_kerb
) {
2463 switch (fxdr_unsigned(int, *tl
++)) {
2464 case RPCAKN_FULLNAME
:
2465 ticklen
= fxdr_unsigned(int, *tl
);
2466 *((u_long
*)nfsd
->nfsd_authstr
) = *tl
;
2467 uio
.uio_resid
= nfsm_rndup(ticklen
) + NFSX_UNSIGNED
;
2468 nfsd
->nfsd_authlen
= uio
.uio_resid
+ NFSX_UNSIGNED
;
2469 if (uio
.uio_resid
> (len
- 2 * NFSX_UNSIGNED
)) {
2476 uio
.uio_segflg
= UIO_SYSSPACE
;
2477 iov
.iov_base
= (caddr_t
)&nfsd
->nfsd_authstr
[4];
2478 iov
.iov_len
= RPCAUTH_MAXSIZ
- 4;
2479 nfsm_mtouio(&uio
, uio
.uio_resid
);
2480 nfsm_dissect(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
2481 if (*tl
++ != rpc_auth_kerb
||
2482 fxdr_unsigned(int, *tl
) != 4 * NFSX_UNSIGNED
) {
2483 printf("Bad kerb verifier\n");
2484 nd
->nd_repstat
= (NFSERR_AUTHERR
|AUTH_BADVERF
);
2485 nd
->nd_procnum
= NFSPROC_NOOP
;
2488 nfsm_dissect(cp
, caddr_t
, 4 * NFSX_UNSIGNED
);
2490 if (fxdr_unsigned(int, *tl
) != RPCAKN_FULLNAME
) {
2491 printf("Not fullname kerb verifier\n");
2492 nd
->nd_repstat
= (NFSERR_AUTHERR
|AUTH_BADVERF
);
2493 nd
->nd_procnum
= NFSPROC_NOOP
;
2496 cp
+= NFSX_UNSIGNED
;
2497 bcopy(cp
, nfsd
->nfsd_verfstr
, 3 * NFSX_UNSIGNED
);
2498 nfsd
->nfsd_verflen
= 3 * NFSX_UNSIGNED
;
2499 nd
->nd_flag
|= ND_KERBFULL
;
2500 nfsd
->nfsd_flag
|= NFSD_NEEDAUTH
;
2502 case RPCAKN_NICKNAME
:
2503 if (len
!= 2 * NFSX_UNSIGNED
) {
2504 printf("Kerb nickname short\n");
2505 nd
->nd_repstat
= (NFSERR_AUTHERR
|AUTH_BADCRED
);
2506 nd
->nd_procnum
= NFSPROC_NOOP
;
2509 nickuid
= fxdr_unsigned(uid_t
, *tl
);
2510 nfsm_dissect(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
2511 if (*tl
++ != rpc_auth_kerb
||
2512 fxdr_unsigned(int, *tl
) != 3 * NFSX_UNSIGNED
) {
2513 printf("Kerb nick verifier bad\n");
2514 nd
->nd_repstat
= (NFSERR_AUTHERR
|AUTH_BADVERF
);
2515 nd
->nd_procnum
= NFSPROC_NOOP
;
2518 nfsm_dissect(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
2519 tvin
.tv_sec
= *tl
++;
2522 for (nuidp
= NUIDHASH(nfsd
->nfsd_slp
,nickuid
)->lh_first
;
2523 nuidp
!= 0; nuidp
= nuidp
->nu_hash
.le_next
) {
2524 if (nuidp
->nu_cr
.cr_uid
== nickuid
&&
2526 netaddr_match(NU_NETFAM(nuidp
),
2527 &nuidp
->nu_haddr
, nd
->nd_nam2
)))
2532 (NFSERR_AUTHERR
|AUTH_REJECTCRED
);
2533 nd
->nd_procnum
= NFSPROC_NOOP
;
2538 * Now, decrypt the timestamp using the session key
2545 tvout
.tv_sec
= fxdr_unsigned(long, tvout
.tv_sec
);
2546 tvout
.tv_usec
= fxdr_unsigned(long, tvout
.tv_usec
);
2547 if (nuidp
->nu_expire
< time
.tv_sec
||
2548 nuidp
->nu_timestamp
.tv_sec
> tvout
.tv_sec
||
2549 (nuidp
->nu_timestamp
.tv_sec
== tvout
.tv_sec
&&
2550 nuidp
->nu_timestamp
.tv_usec
> tvout
.tv_usec
)) {
2551 nuidp
->nu_expire
= 0;
2553 (NFSERR_AUTHERR
|AUTH_REJECTVERF
);
2554 nd
->nd_procnum
= NFSPROC_NOOP
;
2557 nfsrv_setcred(&nuidp
->nu_cr
, &nd
->nd_cr
);
2558 nd
->nd_flag
|= ND_KERBNICK
;
2561 nd
->nd_repstat
= (NFSERR_AUTHERR
| AUTH_REJECTCRED
);
2562 nd
->nd_procnum
= NFSPROC_NOOP
;
2567 * For nqnfs, get piggybacked lease request.
2569 if (nqnfs
&& nd
->nd_procnum
!= NQNFSPROC_EVICTED
) {
2570 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
2571 nd
->nd_flag
|= fxdr_unsigned(int, *tl
);
2572 if (nd
->nd_flag
& ND_LEASE
) {
2573 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
2574 nd
->nd_duration
= fxdr_unsigned(int, *tl
);
2576 nd
->nd_duration
= NQ_MINLEASE
;
2578 nd
->nd_duration
= NQ_MINLEASE
;
2587 * Search for a sleeping nfsd and wake it up.
2588 * SIDE EFFECT: If none found, set NFSD_CHECKSLP flag, so that one of the
2589 * running nfsds will go look for the work in the nfssvc_sock list.
2593 struct nfssvc_sock
*slp
;
2595 register struct nfsd
*nd
;
2597 if ((slp
->ns_flag
& SLP_VALID
) == 0)
2599 for (nd
= nfsd_head
.tqh_first
; nd
!= 0; nd
= nd
->nfsd_chain
.tqe_next
) {
2600 if (nd
->nfsd_flag
& NFSD_WAITING
) {
2601 nd
->nfsd_flag
&= ~NFSD_WAITING
;
2603 panic("nfsd wakeup");
2606 wakeup((caddr_t
)nd
);
2610 slp
->ns_flag
|= SLP_DOREC
;
2611 nfsd_head_flag
|= NFSD_CHECKSLP
;
2613 #endif /* NFS_NOSERVER */
2616 nfs_msg(p
, server
, msg
)
2623 tpr
= tprintf_open(p
);
2626 tprintf(tpr
, "nfs server %s: %s\n", server
, msg
);