2 * Copyright (c) 1989, 1991, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)nfs_socket.c 8.5 (Berkeley) 3/30/95
37 * $FreeBSD: src/sys/nfs/nfs_socket.c,v 1.60.2.6 2003/03/26 01:44:46 alfred Exp $
38 * $DragonFly: src/sys/vfs/nfs/nfs_socket.c,v 1.45 2007/05/18 17:05:13 dillon Exp $
42 * Socket operations for use by nfs
45 #include <sys/param.h>
46 #include <sys/systm.h>
48 #include <sys/malloc.h>
49 #include <sys/mount.h>
50 #include <sys/kernel.h>
52 #include <sys/vnode.h>
53 #include <sys/fcntl.h>
54 #include <sys/protosw.h>
55 #include <sys/resourcevar.h>
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
58 #include <sys/socketops.h>
59 #include <sys/syslog.h>
60 #include <sys/thread.h>
61 #include <sys/tprintf.h>
62 #include <sys/sysctl.h>
63 #include <sys/signalvar.h>
65 #include <sys/signal2.h>
66 #include <sys/mutex2.h>
67 #include <sys/socketvar2.h>
69 #include <netinet/in.h>
70 #include <netinet/tcp.h>
71 #include <sys/thread2.h>
77 #include "nfsm_subs.h"
86 * RTT calculations are scaled by 256 (8 bits). A proper fractional
87 * RTT will still be calculated even with a slow NFS timer.
89 #define NFS_SRTT(r) (r)->r_nmp->nm_srtt[proct[(r)->r_procnum]]
90 #define NFS_SDRTT(r) (r)->r_nmp->nm_sdrtt[proct[(r)->r_procnum]]
91 #define NFS_RTT_SCALE_BITS 8 /* bits */
92 #define NFS_RTT_SCALE 256 /* value */
95 * Defines which timer to use for the procnum.
102 static int proct
[NFS_NPROCS
] = {
103 0, 1, 0, 2, 1, 3, 3, 4, 0, 0, /* 00-09 */
104 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, /* 10-19 */
105 0, 5, 0, 0, 0, 0, /* 20-29 */
108 static int multt
[NFS_NPROCS
] = {
109 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-09 */
110 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10-19 */
111 1, 2, 1, 1, 1, 1, /* 20-29 */
114 static int nfs_backoff
[8] = { 2, 3, 5, 8, 13, 21, 34, 55 };
115 static int nfs_realign_test
;
116 static int nfs_realign_count
;
117 static int nfs_showrtt
;
118 static int nfs_showrexmit
;
119 int nfs_maxasyncbio
= NFS_MAXASYNCBIO
;
121 SYSCTL_DECL(_vfs_nfs
);
123 SYSCTL_INT(_vfs_nfs
, OID_AUTO
, realign_test
, CTLFLAG_RW
, &nfs_realign_test
, 0, "");
124 SYSCTL_INT(_vfs_nfs
, OID_AUTO
, realign_count
, CTLFLAG_RW
, &nfs_realign_count
, 0, "");
125 SYSCTL_INT(_vfs_nfs
, OID_AUTO
, showrtt
, CTLFLAG_RW
, &nfs_showrtt
, 0, "");
126 SYSCTL_INT(_vfs_nfs
, OID_AUTO
, showrexmit
, CTLFLAG_RW
, &nfs_showrexmit
, 0, "");
127 SYSCTL_INT(_vfs_nfs
, OID_AUTO
, maxasyncbio
, CTLFLAG_RW
, &nfs_maxasyncbio
, 0, "");
129 static int nfs_request_setup(nfsm_info_t info
);
130 static int nfs_request_auth(struct nfsreq
*rep
);
131 static int nfs_request_try(struct nfsreq
*rep
);
132 static int nfs_request_waitreply(struct nfsreq
*rep
);
133 static int nfs_request_processreply(nfsm_info_t info
, int);
136 struct nfsrtt nfsrtt
;
137 struct callout nfs_timer_handle
;
139 static int nfs_msg (struct thread
*,char *,char *);
140 static int nfs_rcvlock (struct nfsmount
*nmp
, struct nfsreq
*myreq
);
141 static void nfs_rcvunlock (struct nfsmount
*nmp
);
142 static void nfs_realign (struct mbuf
**pm
, int hsiz
);
143 static int nfs_receive (struct nfsmount
*nmp
, struct nfsreq
*rep
,
144 struct sockaddr
**aname
, struct mbuf
**mp
);
145 static void nfs_softterm (struct nfsreq
*rep
, int islocked
);
146 static void nfs_hardterm (struct nfsreq
*rep
, int islocked
);
147 static int nfs_reconnect (struct nfsmount
*nmp
, struct nfsreq
*rep
);
149 static int nfsrv_getstream (struct nfssvc_sock
*, int, int *);
150 static void nfs_timer_req(struct nfsreq
*req
);
151 static void nfs_checkpkt(struct mbuf
*m
, int len
);
153 int (*nfsrv3_procs
[NFS_NPROCS
]) (struct nfsrv_descript
*nd
,
154 struct nfssvc_sock
*slp
,
156 struct mbuf
**mreqp
) = {
184 #endif /* NFS_NOSERVER */
187 * Initialize sockets and congestion for a new NFS connection.
188 * We do not free the sockaddr if error.
191 nfs_connect(struct nfsmount
*nmp
, struct nfsreq
*rep
)
195 struct sockaddr
*saddr
;
196 struct sockaddr_in
*sin
;
197 struct thread
*td
= &thread0
; /* only used for socreate and sobind */
199 nmp
->nm_so
= so
= NULL
;
200 if (nmp
->nm_flag
& NFSMNT_FORCE
)
203 error
= socreate(saddr
->sa_family
, &so
, nmp
->nm_sotype
,
204 nmp
->nm_soproto
, td
);
207 nmp
->nm_soflags
= so
->so_proto
->pr_flags
;
210 * Some servers require that the client port be a reserved port number.
212 if (saddr
->sa_family
== AF_INET
&& (nmp
->nm_flag
& NFSMNT_RESVPORT
)) {
215 struct sockaddr_in ssin
;
217 bzero(&sopt
, sizeof sopt
);
218 ip
= IP_PORTRANGE_LOW
;
219 sopt
.sopt_level
= IPPROTO_IP
;
220 sopt
.sopt_name
= IP_PORTRANGE
;
221 sopt
.sopt_val
= (void *)&ip
;
222 sopt
.sopt_valsize
= sizeof(ip
);
224 error
= sosetopt(so
, &sopt
);
227 bzero(&ssin
, sizeof ssin
);
229 sin
->sin_len
= sizeof (struct sockaddr_in
);
230 sin
->sin_family
= AF_INET
;
231 sin
->sin_addr
.s_addr
= INADDR_ANY
;
232 sin
->sin_port
= htons(0);
233 error
= sobind(so
, (struct sockaddr
*)sin
, td
);
236 bzero(&sopt
, sizeof sopt
);
237 ip
= IP_PORTRANGE_DEFAULT
;
238 sopt
.sopt_level
= IPPROTO_IP
;
239 sopt
.sopt_name
= IP_PORTRANGE
;
240 sopt
.sopt_val
= (void *)&ip
;
241 sopt
.sopt_valsize
= sizeof(ip
);
243 error
= sosetopt(so
, &sopt
);
249 * Protocols that do not require connections may be optionally left
250 * unconnected for servers that reply from a port other than NFS_PORT.
252 if (nmp
->nm_flag
& NFSMNT_NOCONN
) {
253 if (nmp
->nm_soflags
& PR_CONNREQUIRED
) {
258 error
= soconnect(so
, nmp
->nm_nam
, td
);
263 * Wait for the connection to complete. Cribbed from the
264 * connect system call but with the wait timing out so
265 * that interruptible mounts don't hang here for a long time.
268 while ((so
->so_state
& SS_ISCONNECTING
) && so
->so_error
== 0) {
269 (void) tsleep((caddr_t
)&so
->so_timeo
, 0,
271 if ((so
->so_state
& SS_ISCONNECTING
) &&
272 so
->so_error
== 0 && rep
&&
273 (error
= nfs_sigintr(nmp
, rep
, rep
->r_td
)) != 0){
274 soclrstate(so
, SS_ISCONNECTING
);
280 error
= so
->so_error
;
287 so
->so_rcv
.ssb_timeo
= (5 * hz
);
288 so
->so_snd
.ssb_timeo
= (5 * hz
);
291 * Get buffer reservation size from sysctl, but impose reasonable
294 if (nmp
->nm_sotype
== SOCK_STREAM
) {
295 if (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) {
299 bzero(&sopt
, sizeof sopt
);
300 sopt
.sopt_level
= SOL_SOCKET
;
301 sopt
.sopt_name
= SO_KEEPALIVE
;
302 sopt
.sopt_val
= &val
;
303 sopt
.sopt_valsize
= sizeof val
;
307 if (so
->so_proto
->pr_protocol
== IPPROTO_TCP
) {
311 bzero(&sopt
, sizeof sopt
);
312 sopt
.sopt_level
= IPPROTO_TCP
;
313 sopt
.sopt_name
= TCP_NODELAY
;
314 sopt
.sopt_val
= &val
;
315 sopt
.sopt_valsize
= sizeof val
;
319 bzero(&sopt
, sizeof sopt
);
320 sopt
.sopt_level
= IPPROTO_TCP
;
321 sopt
.sopt_name
= TCP_FASTKEEP
;
322 sopt
.sopt_val
= &val
;
323 sopt
.sopt_valsize
= sizeof val
;
328 error
= soreserve(so
, nfs_soreserve
, nfs_soreserve
, NULL
);
331 atomic_set_int(&so
->so_rcv
.ssb_flags
, SSB_NOINTR
);
332 atomic_set_int(&so
->so_snd
.ssb_flags
, SSB_NOINTR
);
334 /* Initialize other non-zero congestion variables */
335 nmp
->nm_srtt
[0] = nmp
->nm_srtt
[1] = nmp
->nm_srtt
[2] =
336 nmp
->nm_srtt
[3] = (NFS_TIMEO
<< NFS_RTT_SCALE_BITS
);
337 nmp
->nm_sdrtt
[0] = nmp
->nm_sdrtt
[1] = nmp
->nm_sdrtt
[2] =
338 nmp
->nm_sdrtt
[3] = 0;
339 nmp
->nm_maxasync_scaled
= NFS_MINASYNC_SCALED
;
340 nmp
->nm_timeouts
= 0;
343 * Assign nm_so last. The moment nm_so is assigned the nfs_timer()
344 * can mess with the socket.
351 soshutdown(so
, SHUT_RDWR
);
352 soclose(so
, FNONBLOCK
);
359 * Called when a connection is broken on a reliable protocol.
360 * - clean up the old socket
361 * - nfs_connect() again
362 * - set R_NEEDSXMIT for all outstanding requests on mount point
363 * If this fails the mount point is DEAD!
364 * nb: Must be called with the nfs_sndlock() set on the mount point.
367 nfs_reconnect(struct nfsmount
*nmp
, struct nfsreq
*rep
)
373 if (nmp
->nm_rxstate
>= NFSSVC_STOPPING
)
375 while ((error
= nfs_connect(nmp
, rep
)) != 0) {
376 if (error
== EINTR
|| error
== ERESTART
)
380 if (nmp
->nm_rxstate
>= NFSSVC_STOPPING
)
382 (void) tsleep((caddr_t
)&lbolt
, 0, "nfscon", 0);
386 * Loop through outstanding request list and fix up all requests
390 TAILQ_FOREACH(req
, &nmp
->nm_reqq
, r_chain
) {
391 KKASSERT(req
->r_nmp
== nmp
);
392 req
->r_flags
|= R_NEEDSXMIT
;
399 * NFS disconnect. Clean up and unlink.
402 nfs_disconnect(struct nfsmount
*nmp
)
409 soshutdown(so
, SHUT_RDWR
);
410 soclose(so
, FNONBLOCK
);
415 nfs_safedisconnect(struct nfsmount
*nmp
)
417 nfs_rcvlock(nmp
, NULL
);
423 * This is the nfs send routine. For connection based socket types, it
424 * must be called with an nfs_sndlock() on the socket.
425 * "rep == NULL" indicates that it has been called from a server.
426 * For the client side:
427 * - return EINTR if the RPC is terminated, 0 otherwise
428 * - set R_NEEDSXMIT if the send fails for any reason
429 * - do any cleanup required by recoverable socket errors (?)
430 * For the server side:
431 * - return EINTR or ERESTART if interrupted by a signal
432 * - return EPIPE if a connection is lost for connection based sockets (TCP...)
433 * - do any cleanup required by recoverable socket errors (?)
436 nfs_send(struct socket
*so
, struct sockaddr
*nam
, struct mbuf
*top
,
439 struct sockaddr
*sendnam
;
440 int error
, soflags
, flags
;
443 if (rep
->r_flags
& R_SOFTTERM
) {
447 if ((so
= rep
->r_nmp
->nm_so
) == NULL
) {
448 rep
->r_flags
|= R_NEEDSXMIT
;
452 rep
->r_flags
&= ~R_NEEDSXMIT
;
453 soflags
= rep
->r_nmp
->nm_soflags
;
455 soflags
= so
->so_proto
->pr_flags
;
457 if ((soflags
& PR_CONNREQUIRED
) || (so
->so_state
& SS_ISCONNECTED
))
461 if (so
->so_type
== SOCK_SEQPACKET
)
467 * calls pru_sosend -> sosend -> so_pru_send -> netrpc
469 error
= so_pru_sosend(so
, sendnam
, NULL
, top
, NULL
, flags
,
473 * ENOBUFS for dgram sockets is transient and non fatal.
474 * No need to log, and no need to break a soft mount.
476 if (error
== ENOBUFS
&& so
->so_type
== SOCK_DGRAM
) {
479 * do backoff retransmit on client
482 if ((rep
->r_nmp
->nm_state
& NFSSTA_SENDSPACE
) == 0) {
483 rep
->r_nmp
->nm_state
|= NFSSTA_SENDSPACE
;
484 kprintf("Warning: NFS: Insufficient sendspace "
486 "\t You must increase vfs.nfs.soreserve"
487 "or decrease vfs.nfs.maxasyncbio\n",
488 so
->so_snd
.ssb_hiwat
);
490 rep
->r_flags
|= R_NEEDSXMIT
;
496 log(LOG_INFO
, "nfs send error %d for server %s\n",error
,
497 rep
->r_nmp
->nm_mountp
->mnt_stat
.f_mntfromname
);
499 * Deal with errors for the client side.
501 if (rep
->r_flags
& R_SOFTTERM
)
504 rep
->r_flags
|= R_NEEDSXMIT
;
506 log(LOG_INFO
, "nfsd send error %d\n", error
);
510 * Handle any recoverable (soft) socket errors here. (?)
512 if (error
!= EINTR
&& error
!= ERESTART
&&
513 error
!= EWOULDBLOCK
&& error
!= EPIPE
)
520 * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all
521 * done by soreceive(), but for SOCK_STREAM we must deal with the Record
522 * Mark and consolidate the data into a new mbuf list.
523 * nb: Sometimes TCP passes the data up to soreceive() in long lists of
525 * For SOCK_STREAM we must be very careful to read an entire record once
526 * we have read any of it, even if the system call has been interrupted.
529 nfs_receive(struct nfsmount
*nmp
, struct nfsreq
*rep
,
530 struct sockaddr
**aname
, struct mbuf
**mp
)
537 struct mbuf
*control
;
539 struct sockaddr
**getnam
;
540 int error
, sotype
, rcvflg
;
541 struct thread
*td
= curthread
; /* XXX */
544 * Set up arguments for soreceive()
548 sotype
= nmp
->nm_sotype
;
551 * For reliable protocols, lock against other senders/receivers
552 * in case a reconnect is necessary.
553 * For SOCK_STREAM, first get the Record Mark to find out how much
554 * more there is to get.
555 * We must lock the socket against other receivers
556 * until we have an entire rpc request/reply.
558 if (sotype
!= SOCK_DGRAM
) {
559 error
= nfs_sndlock(nmp
, rep
);
564 * Check for fatal errors and resending request.
567 * Ugh: If a reconnect attempt just happened, nm_so
568 * would have changed. NULL indicates a failed
569 * attempt that has essentially shut down this
572 if (rep
&& (rep
->r_mrep
|| (rep
->r_flags
& R_SOFTTERM
))) {
578 error
= nfs_reconnect(nmp
, rep
);
585 while (rep
&& (rep
->r_flags
& R_NEEDSXMIT
)) {
586 m
= m_copym(rep
->r_mreq
, 0, M_COPYALL
, MB_WAIT
);
587 nfsstats
.rpcretries
++;
588 error
= nfs_send(so
, rep
->r_nmp
->nm_nam
, m
, rep
);
590 if (error
== EINTR
|| error
== ERESTART
||
591 (error
= nfs_reconnect(nmp
, rep
)) != 0) {
599 if (sotype
== SOCK_STREAM
) {
601 * Get the length marker from the stream
603 aio
.iov_base
= (caddr_t
)&len
;
604 aio
.iov_len
= sizeof(u_int32_t
);
607 auio
.uio_segflg
= UIO_SYSSPACE
;
608 auio
.uio_rw
= UIO_READ
;
610 auio
.uio_resid
= sizeof(u_int32_t
);
613 rcvflg
= MSG_WAITALL
;
614 error
= so_pru_soreceive(so
, NULL
, &auio
, NULL
,
616 if (error
== EWOULDBLOCK
&& rep
) {
617 if (rep
->r_flags
& R_SOFTTERM
)
620 } while (error
== EWOULDBLOCK
);
622 if (error
== 0 && auio
.uio_resid
> 0) {
624 * Only log short packets if not EOF
626 if (auio
.uio_resid
!= sizeof(u_int32_t
))
628 "short receive (%d/%d) from nfs server %s\n",
629 (int)(sizeof(u_int32_t
) - auio
.uio_resid
),
630 (int)sizeof(u_int32_t
),
631 nmp
->nm_mountp
->mnt_stat
.f_mntfromname
);
636 len
= ntohl(len
) & ~0x80000000;
638 * This is SERIOUS! We are out of sync with the sender
639 * and forcing a disconnect/reconnect is all I can do.
641 if (len
> NFS_MAXPACKET
) {
642 log(LOG_ERR
, "%s (%d) from nfs server %s\n",
643 "impossible packet length",
645 nmp
->nm_mountp
->mnt_stat
.f_mntfromname
);
651 * Get the rest of the packet as an mbuf chain
655 rcvflg
= MSG_WAITALL
;
656 error
= so_pru_soreceive(so
, NULL
, NULL
, &sio
,
658 } while (error
== EWOULDBLOCK
|| error
== EINTR
||
660 if (error
== 0 && sio
.sb_cc
!= len
) {
663 "short receive (%zu/%d) from nfs server %s\n",
664 (size_t)len
- auio
.uio_resid
, len
,
665 nmp
->nm_mountp
->mnt_stat
.f_mntfromname
);
671 * Non-stream, so get the whole packet by not
672 * specifying MSG_WAITALL and by specifying a large
675 * We have no use for control msg., but must grab them
676 * and then throw them away so we know what is going
679 sbinit(&sio
, 100000000);
682 error
= so_pru_soreceive(so
, NULL
, NULL
, &sio
,
686 if (error
== EWOULDBLOCK
&& rep
) {
687 if (rep
->r_flags
& R_SOFTTERM
) {
692 } while (error
== EWOULDBLOCK
||
693 (error
== 0 && sio
.sb_mb
== NULL
&& control
));
694 if ((rcvflg
& MSG_EOR
) == 0)
696 if (error
== 0 && sio
.sb_mb
== NULL
)
702 if (error
&& error
!= EINTR
&& error
!= ERESTART
) {
705 if (error
!= EPIPE
) {
707 "receive error %d from nfs server %s\n",
709 nmp
->nm_mountp
->mnt_stat
.f_mntfromname
);
711 error
= nfs_sndlock(nmp
, rep
);
713 error
= nfs_reconnect(nmp
, rep
);
721 if ((so
= nmp
->nm_so
) == NULL
)
723 if (so
->so_state
& SS_ISCONNECTED
)
727 sbinit(&sio
, 100000000);
730 error
= so_pru_soreceive(so
, getnam
, NULL
, &sio
,
732 if (error
== EWOULDBLOCK
&& rep
&&
733 (rep
->r_flags
& R_SOFTTERM
)) {
737 } while (error
== EWOULDBLOCK
);
743 * A shutdown may result in no error and no mbuf.
746 if (*mp
== NULL
&& error
== 0)
755 * Search for any mbufs that are not a multiple of 4 bytes long
756 * or with m_data not longword aligned.
757 * These could cause pointer alignment problems, so copy them to
758 * well aligned mbufs.
760 nfs_realign(mp
, 5 * NFSX_UNSIGNED
);
765 * Implement receipt of reply on a socket.
767 * We must search through the list of received datagrams matching them
768 * with outstanding requests using the xid, until ours is found.
770 * If myrep is NULL we process packets on the socket until
771 * interrupted or until nm_reqrxq is non-empty.
775 nfs_reply(struct nfsmount
*nmp
, struct nfsreq
*myrep
)
778 struct sockaddr
*nam
;
782 struct nfsm_info info
;
785 * Loop around until we get our own reply
789 * Lock against other receivers so that I don't get stuck in
790 * sbwait() after someone else has received my reply for me.
791 * Also necessary for connection based protocols to avoid
792 * race conditions during a reconnect.
794 * If nfs_rcvlock() returns EALREADY, that means that
795 * the reply has already been recieved by another
796 * process and we can return immediately. In this
797 * case, the lock is not taken to avoid races with
802 error
= nfs_rcvlock(nmp
, myrep
);
803 if (error
== EALREADY
)
809 * If myrep is NULL we are the receiver helper thread.
810 * Stop waiting for incoming replies if there are
811 * messages sitting on reqrxq that we need to process,
812 * or if a shutdown request is pending.
814 if (myrep
== NULL
&& (TAILQ_FIRST(&nmp
->nm_reqrxq
) ||
815 nmp
->nm_rxstate
> NFSSVC_PENDING
)) {
821 * Get the next Rpc reply off the socket
823 * We cannot release the receive lock until we've
824 * filled in rep->r_mrep, otherwise a waiting
825 * thread may deadlock in soreceive with no incoming
828 error
= nfs_receive(nmp
, myrep
, &nam
, &info
.mrep
);
831 * Ignore routing errors on connectionless protocols??
834 if (NFSIGNORE_SOERROR(nmp
->nm_soflags
, error
)) {
835 if (nmp
->nm_so
== NULL
)
837 nmp
->nm_so
->so_error
= 0;
846 * Get the xid and check that it is an rpc reply
849 info
.dpos
= mtod(info
.md
, caddr_t
);
850 NULLOUT(tl
= nfsm_dissect(&info
, 2*NFSX_UNSIGNED
));
852 if (*tl
!= rpc_reply
) {
853 nfsstats
.rpcinvalid
++;
862 * Loop through the request list to match up the reply
863 * Iff no match, just drop the datagram. On match, set
864 * r_mrep atomically to prevent the timer from messing
865 * around with the request after we have exited the critical
869 TAILQ_FOREACH(rep
, &nmp
->nm_reqq
, r_chain
) {
870 if (rep
->r_mrep
== NULL
&& rxid
== rep
->r_xid
)
875 * Fill in the rest of the reply if we found a match.
877 * Deal with duplicate responses if there was no match.
881 rep
->r_dpos
= info
.dpos
;
885 rt
= &nfsrtt
.rttl
[nfsrtt
.pos
];
886 rt
->proc
= rep
->r_procnum
;
889 rt
->cwnd
= nmp
->nm_maxasync_scaled
;
890 rt
->srtt
= nmp
->nm_srtt
[proct
[rep
->r_procnum
] - 1];
891 rt
->sdrtt
= nmp
->nm_sdrtt
[proct
[rep
->r_procnum
] - 1];
892 rt
->fsid
= nmp
->nm_mountp
->mnt_stat
.f_fsid
;
893 getmicrotime(&rt
->tstamp
);
894 if (rep
->r_flags
& R_TIMING
)
895 rt
->rtt
= rep
->r_rtt
;
898 nfsrtt
.pos
= (nfsrtt
.pos
+ 1) % NFSRTTLOGSIZ
;
902 * New congestion control is based only on async
905 if (nmp
->nm_maxasync_scaled
< NFS_MAXASYNC_SCALED
)
906 ++nmp
->nm_maxasync_scaled
;
907 if (rep
->r_flags
& R_SENT
) {
908 rep
->r_flags
&= ~R_SENT
;
911 * Update rtt using a gain of 0.125 on the mean
912 * and a gain of 0.25 on the deviation.
914 * NOTE SRTT/SDRTT are only good if R_TIMING is set.
916 if ((rep
->r_flags
& R_TIMING
) && rep
->r_rexmit
== 0) {
918 * Since the timer resolution of
919 * NFS_HZ is so course, it can often
920 * result in r_rtt == 0. Since
921 * r_rtt == N means that the actual
922 * rtt is between N+dt and N+2-dt ticks,
928 #define NFSRSB NFS_RTT_SCALE_BITS
929 n
= ((NFS_SRTT(rep
) * 7) +
930 (rep
->r_rtt
<< NFSRSB
)) >> 3;
931 d
= n
- NFS_SRTT(rep
);
935 * Don't let the jitter calculation decay
936 * too quickly, but we want a fast rampup.
941 if (d
< NFS_SDRTT(rep
))
942 n
= ((NFS_SDRTT(rep
) * 15) + d
) >> 4;
944 n
= ((NFS_SDRTT(rep
) * 3) + d
) >> 2;
948 nmp
->nm_timeouts
= 0;
949 rep
->r_mrep
= info
.mrep
;
950 nfs_hardterm(rep
, 0);
953 * Extract vers, prog, nfsver, procnum. A duplicate
954 * response means we didn't wait long enough so
955 * we increase the SRTT to avoid future spurious
958 u_int procnum
= nmp
->nm_lastreprocnum
;
961 if (procnum
< NFS_NPROCS
&& proct
[procnum
]) {
964 n
= nmp
->nm_srtt
[proct
[procnum
]];
965 n
+= NFS_ASYSCALE
* NFS_HZ
;
966 if (n
< NFS_ASYSCALE
* NFS_HZ
* 10)
967 n
= NFS_ASYSCALE
* NFS_HZ
* 10;
968 nmp
->nm_srtt
[proct
[procnum
]] = n
;
975 * If not matched to a request, drop it.
976 * If it's mine, get out.
979 nfsstats
.rpcunexpected
++;
982 } else if (rep
== myrep
) {
983 if (rep
->r_mrep
== NULL
)
984 panic("nfsreply nil");
991 * Run the request state machine until the target state is reached
992 * or a fatal error occurs. The target state is not run. Specifying
993 * a target of NFSM_STATE_DONE runs the state machine until the rpc
996 * EINPROGRESS is returned for all states other then the DONE state,
997 * indicating that the rpc is still in progress.
1000 nfs_request(struct nfsm_info
*info
, nfsm_state_t bstate
, nfsm_state_t estate
)
1004 while (info
->state
>= bstate
&& info
->state
< estate
) {
1005 switch(info
->state
) {
1006 case NFSM_STATE_SETUP
:
1008 * Setup the nfsreq. Any error which occurs during
1009 * this state is fatal.
1011 info
->error
= nfs_request_setup(info
);
1013 info
->state
= NFSM_STATE_DONE
;
1014 return (info
->error
);
1017 req
->r_mrp
= &info
->mrep
;
1018 req
->r_mdp
= &info
->md
;
1019 req
->r_dposp
= &info
->dpos
;
1020 info
->state
= NFSM_STATE_AUTH
;
1023 case NFSM_STATE_AUTH
:
1025 * Authenticate the nfsreq. Any error which occurs
1026 * during this state is fatal.
1028 info
->error
= nfs_request_auth(info
->req
);
1030 info
->state
= NFSM_STATE_DONE
;
1031 return (info
->error
);
1033 info
->state
= NFSM_STATE_TRY
;
1036 case NFSM_STATE_TRY
:
1038 * Transmit or retransmit attempt. An error in this
1039 * state is ignored and we always move on to the
1042 * This can trivially race the receiver if the
1043 * request is asynchronous. nfs_request_try()
1044 * will thus set the state for us and we
1045 * must also return immediately if we are
1046 * running an async state machine, because
1047 * info can become invalid due to races after
1050 if (info
->req
->r_flags
& R_ASYNC
) {
1051 nfs_request_try(info
->req
);
1052 if (estate
== NFSM_STATE_WAITREPLY
)
1053 return (EINPROGRESS
);
1055 nfs_request_try(info
->req
);
1056 info
->state
= NFSM_STATE_WAITREPLY
;
1059 case NFSM_STATE_WAITREPLY
:
1061 * Wait for a reply or timeout and move on to the
1062 * next state. The error returned by this state
1063 * is passed to the processing code in the next
1066 info
->error
= nfs_request_waitreply(info
->req
);
1067 info
->state
= NFSM_STATE_PROCESSREPLY
;
1069 case NFSM_STATE_PROCESSREPLY
:
1071 * Process the reply or timeout. Errors which occur
1072 * in this state may cause the state machine to
1073 * go back to an earlier state, and are fatal
1076 info
->error
= nfs_request_processreply(info
,
1078 switch(info
->error
) {
1080 info
->state
= NFSM_STATE_AUTH
;
1083 info
->state
= NFSM_STATE_TRY
;
1087 * Operation complete, with or without an
1088 * error. We are done.
1091 info
->state
= NFSM_STATE_DONE
;
1092 return (info
->error
);
1095 case NFSM_STATE_DONE
:
1097 * Shouldn't be reached
1099 return (info
->error
);
1105 * If we are done return the error code (if any).
1106 * Otherwise return EINPROGRESS.
1108 if (info
->state
== NFSM_STATE_DONE
)
1109 return (info
->error
);
1110 return (EINPROGRESS
);
1114 * nfs_request - goes something like this
1115 * - fill in request struct
1116 * - links it into list
1117 * - calls nfs_send() for first transmit
1118 * - calls nfs_receive() to get reply
1119 * - break down rpc header and return with nfs reply pointed to
1121 * nb: always frees up mreq mbuf list
1124 nfs_request_setup(nfsm_info_t info
)
1127 struct nfsmount
*nmp
;
1132 * Reject requests while attempting a forced unmount.
1134 if (info
->vp
->v_mount
->mnt_kern_flag
& MNTK_UNMOUNTF
) {
1135 m_freem(info
->mreq
);
1139 nmp
= VFSTONFS(info
->vp
->v_mount
);
1140 req
= kmalloc(sizeof(struct nfsreq
), M_NFSREQ
, M_WAITOK
);
1142 req
->r_vp
= info
->vp
;
1143 req
->r_td
= info
->td
;
1144 req
->r_procnum
= info
->procnum
;
1146 req
->r_cred
= info
->cred
;
1154 req
->r_mrest
= info
->mreq
;
1155 req
->r_mrest_len
= i
;
1158 * The presence of a non-NULL r_info in req indicates
1159 * async completion via our helper threads. See the receiver
1164 req
->r_flags
= R_ASYNC
;
1174 nfs_request_auth(struct nfsreq
*rep
)
1176 struct nfsmount
*nmp
= rep
->r_nmp
;
1178 char nickv
[RPCX_NICKVERF
];
1179 int error
= 0, auth_len
, auth_type
;
1182 char *auth_str
, *verf_str
;
1186 rep
->r_failed_auth
= 0;
1189 * Get the RPC header with authorization.
1191 verf_str
= auth_str
= NULL
;
1192 if (nmp
->nm_flag
& NFSMNT_KERB
) {
1194 verf_len
= sizeof (nickv
);
1195 auth_type
= RPCAUTH_KERB4
;
1196 bzero((caddr_t
)rep
->r_key
, sizeof(rep
->r_key
));
1197 if (rep
->r_failed_auth
||
1198 nfs_getnickauth(nmp
, cred
, &auth_str
, &auth_len
,
1199 verf_str
, verf_len
)) {
1200 error
= nfs_getauth(nmp
, rep
, cred
, &auth_str
,
1201 &auth_len
, verf_str
, &verf_len
, rep
->r_key
);
1203 m_freem(rep
->r_mrest
);
1204 rep
->r_mrest
= NULL
;
1205 kfree((caddr_t
)rep
, M_NFSREQ
);
1210 auth_type
= RPCAUTH_UNIX
;
1211 if (cred
->cr_ngroups
< 1)
1212 panic("nfsreq nogrps");
1213 auth_len
= ((((cred
->cr_ngroups
- 1) > nmp
->nm_numgrps
) ?
1214 nmp
->nm_numgrps
: (cred
->cr_ngroups
- 1)) << 2) +
1218 nfs_checkpkt(rep
->r_mrest
, rep
->r_mrest_len
);
1219 m
= nfsm_rpchead(cred
, nmp
->nm_flag
, rep
->r_procnum
, auth_type
,
1220 auth_len
, auth_str
, verf_len
, verf_str
,
1221 rep
->r_mrest
, rep
->r_mrest_len
, &rep
->r_mheadend
, &xid
);
1222 rep
->r_mrest
= NULL
;
1224 kfree(auth_str
, M_TEMP
);
1227 * For stream protocols, insert a Sun RPC Record Mark.
1229 if (nmp
->nm_sotype
== SOCK_STREAM
) {
1230 M_PREPEND(m
, NFSX_UNSIGNED
, MB_WAIT
);
1232 kfree(rep
, M_NFSREQ
);
1235 *mtod(m
, u_int32_t
*) = htonl(0x80000000 |
1236 (m
->m_pkthdr
.len
- NFSX_UNSIGNED
));
1239 nfs_checkpkt(m
, m
->m_pkthdr
.len
);
1247 nfs_request_try(struct nfsreq
*rep
)
1249 struct nfsmount
*nmp
= rep
->r_nmp
;
1254 * Request is not on any queue, only the owner has access to it
1255 * so it should not be locked by anyone atm.
1257 * Interlock to prevent races. While locked the only remote
1258 * action possible is for r_mrep to be set (once we enqueue it).
1260 if (rep
->r_flags
== 0xdeadc0de) {
1261 print_backtrace(-1);
1262 panic("flags nbad\n");
1264 KKASSERT((rep
->r_flags
& (R_LOCKED
| R_ONREQQ
)) == 0);
1265 if (nmp
->nm_flag
& NFSMNT_SOFT
)
1266 rep
->r_retry
= nmp
->nm_retry
;
1268 rep
->r_retry
= NFS_MAXREXMIT
+ 1; /* past clip limit */
1269 rep
->r_rtt
= rep
->r_rexmit
= 0;
1270 if (proct
[rep
->r_procnum
] > 0)
1271 rep
->r_flags
|= R_TIMING
| R_LOCKED
;
1273 rep
->r_flags
|= R_LOCKED
;
1276 nfsstats
.rpcrequests
++;
1278 if (nmp
->nm_flag
& NFSMNT_FORCE
) {
1279 rep
->r_flags
|= R_SOFTTERM
;
1280 rep
->r_flags
&= ~R_LOCKED
;
1283 rep
->r_flags
|= R_NEEDSXMIT
; /* in case send lock races us */
1286 * Do the client side RPC.
1288 * Chain request into list of outstanding requests. Be sure
1289 * to put it LAST so timer finds oldest requests first. Note
1290 * that our control of R_LOCKED prevents the request from
1291 * getting ripped out from under us or transmitted by the
1294 * For requests with info structures we must atomically set the
1295 * info's state because the structure could become invalid upon
1296 * return due to races (i.e., if async)
1299 mtx_link_init(&rep
->r_link
);
1300 KKASSERT((rep
->r_flags
& R_ONREQQ
) == 0);
1301 TAILQ_INSERT_TAIL(&nmp
->nm_reqq
, rep
, r_chain
);
1302 rep
->r_flags
|= R_ONREQQ
;
1304 if (rep
->r_flags
& R_ASYNC
)
1305 rep
->r_info
->state
= NFSM_STATE_WAITREPLY
;
1311 * Send if we can. Congestion control is not handled here any more
1312 * becausing trying to defer the initial send based on the nfs_timer
1313 * requires having a very fast nfs_timer, which is silly.
1316 if (nmp
->nm_soflags
& PR_CONNREQUIRED
)
1317 error
= nfs_sndlock(nmp
, rep
);
1318 if (error
== 0 && (rep
->r_flags
& R_NEEDSXMIT
)) {
1319 m2
= m_copym(rep
->r_mreq
, 0, M_COPYALL
, MB_WAIT
);
1320 error
= nfs_send(nmp
->nm_so
, nmp
->nm_nam
, m2
, rep
);
1321 rep
->r_flags
&= ~R_NEEDSXMIT
;
1322 if ((rep
->r_flags
& R_SENT
) == 0) {
1323 rep
->r_flags
|= R_SENT
;
1325 if (nmp
->nm_soflags
& PR_CONNREQUIRED
)
1335 * Release the lock. The only remote action that may have occurred
1336 * would have been the setting of rep->r_mrep. If this occured
1337 * and the request was async we have to move it to the reader
1338 * thread's queue for action.
1340 * For async requests also make sure the reader is woken up so
1341 * it gets on the socket to read responses.
1344 if (rep
->r_flags
& R_ASYNC
) {
1346 nfs_hardterm(rep
, 1);
1347 rep
->r_flags
&= ~R_LOCKED
;
1348 nfssvc_iod_reader_wakeup(nmp
);
1350 rep
->r_flags
&= ~R_LOCKED
;
1352 if (rep
->r_flags
& R_WANTED
) {
1353 rep
->r_flags
&= ~R_WANTED
;
1361 * This code is only called for synchronous requests. Completed synchronous
1362 * requests are left on reqq and we remove them before moving on to the
1366 nfs_request_waitreply(struct nfsreq
*rep
)
1368 struct nfsmount
*nmp
= rep
->r_nmp
;
1371 KKASSERT((rep
->r_flags
& R_ASYNC
) == 0);
1374 * Wait until the request is finished.
1376 error
= nfs_reply(nmp
, rep
);
1379 * RPC done, unlink the request, but don't rip it out from under
1380 * the callout timer.
1382 * Once unlinked no other receiver or the timer will have
1383 * visibility, so we do not have to set R_LOCKED.
1386 while (rep
->r_flags
& R_LOCKED
) {
1387 rep
->r_flags
|= R_WANTED
;
1388 tsleep(rep
, 0, "nfstrac", 0);
1390 KKASSERT(rep
->r_flags
& R_ONREQQ
);
1391 TAILQ_REMOVE(&nmp
->nm_reqq
, rep
, r_chain
);
1392 rep
->r_flags
&= ~R_ONREQQ
;
1394 if (TAILQ_FIRST(&nmp
->nm_bioq
) &&
1395 nmp
->nm_reqqlen
<= nfs_maxasyncbio
* 2 / 3) {
1396 nfssvc_iod_writer_wakeup(nmp
);
1401 * Decrement the outstanding request count.
1403 if (rep
->r_flags
& R_SENT
) {
1404 rep
->r_flags
&= ~R_SENT
;
1410 * Process reply with error returned from nfs_requet_waitreply().
1412 * Returns EAGAIN if it wants us to loop up to nfs_request_try() again.
1413 * Returns ENEEDAUTH if it wants us to loop up to nfs_request_auth() again.
1416 nfs_request_processreply(nfsm_info_t info
, int error
)
1418 struct nfsreq
*req
= info
->req
;
1419 struct nfsmount
*nmp
= req
->r_nmp
;
1425 * If there was a successful reply and a tprintf msg.
1426 * tprintf a response.
1428 if (error
== 0 && (req
->r_flags
& R_TPRINTFMSG
)) {
1429 nfs_msg(req
->r_td
, nmp
->nm_mountp
->mnt_stat
.f_mntfromname
,
1432 info
->mrep
= req
->r_mrep
;
1433 info
->md
= req
->r_md
;
1434 info
->dpos
= req
->r_dpos
;
1436 m_freem(req
->r_mreq
);
1438 kfree(req
, M_NFSREQ
);
1444 * break down the rpc header and check if ok
1446 NULLOUT(tl
= nfsm_dissect(info
, 3 * NFSX_UNSIGNED
));
1447 if (*tl
++ == rpc_msgdenied
) {
1448 if (*tl
== rpc_mismatch
) {
1450 } else if ((nmp
->nm_flag
& NFSMNT_KERB
) &&
1451 *tl
++ == rpc_autherr
) {
1452 if (req
->r_failed_auth
== 0) {
1453 req
->r_failed_auth
++;
1454 req
->r_mheadend
->m_next
= NULL
;
1455 m_freem(info
->mrep
);
1457 m_freem(req
->r_mreq
);
1466 m_freem(info
->mrep
);
1468 m_freem(req
->r_mreq
);
1470 kfree(req
, M_NFSREQ
);
1476 * Grab any Kerberos verifier, otherwise just throw it away.
1478 verf_type
= fxdr_unsigned(int, *tl
++);
1479 i
= fxdr_unsigned(int32_t, *tl
);
1480 if ((nmp
->nm_flag
& NFSMNT_KERB
) && verf_type
== RPCAUTH_KERB4
) {
1481 error
= nfs_savenickauth(nmp
, req
->r_cred
, i
, req
->r_key
,
1482 &info
->md
, &info
->dpos
, info
->mrep
);
1486 ERROROUT(nfsm_adv(info
, nfsm_rndup(i
)));
1488 NULLOUT(tl
= nfsm_dissect(info
, NFSX_UNSIGNED
));
1491 NULLOUT(tl
= nfsm_dissect(info
, NFSX_UNSIGNED
));
1493 error
= fxdr_unsigned(int, *tl
);
1496 * Does anyone even implement this? Just impose
1499 if ((nmp
->nm_flag
& NFSMNT_NFSV3
) &&
1500 error
== NFSERR_TRYLATER
) {
1501 m_freem(info
->mrep
);
1505 tsleep((caddr_t
)&lbolt
, 0, "nqnfstry", 0);
1506 return (EAGAIN
); /* goto tryagain */
1510 * If the File Handle was stale, invalidate the
1511 * lookup cache, just in case.
1513 * To avoid namecache<->vnode deadlocks we must
1514 * release the vnode lock if we hold it.
1516 if (error
== ESTALE
) {
1517 struct vnode
*vp
= req
->r_vp
;
1520 ltype
= lockstatus(&vp
->v_lock
, curthread
);
1521 if (ltype
== LK_EXCLUSIVE
|| ltype
== LK_SHARED
)
1522 lockmgr(&vp
->v_lock
, LK_RELEASE
);
1523 cache_inval_vp(vp
, CINV_CHILDREN
);
1524 if (ltype
== LK_EXCLUSIVE
|| ltype
== LK_SHARED
)
1525 lockmgr(&vp
->v_lock
, ltype
);
1527 if (nmp
->nm_flag
& NFSMNT_NFSV3
) {
1528 KKASSERT(*req
->r_mrp
== info
->mrep
);
1529 KKASSERT(*req
->r_mdp
== info
->md
);
1530 KKASSERT(*req
->r_dposp
== info
->dpos
);
1531 error
|= NFSERR_RETERR
;
1533 m_freem(info
->mrep
);
1536 m_freem(req
->r_mreq
);
1538 kfree(req
, M_NFSREQ
);
1543 KKASSERT(*req
->r_mrp
== info
->mrep
);
1544 KKASSERT(*req
->r_mdp
== info
->md
);
1545 KKASSERT(*req
->r_dposp
== info
->dpos
);
1546 m_freem(req
->r_mreq
);
1548 FREE(req
, M_NFSREQ
);
1551 m_freem(info
->mrep
);
1553 error
= EPROTONOSUPPORT
;
1555 m_freem(req
->r_mreq
);
1557 kfree(req
, M_NFSREQ
);
1562 #ifndef NFS_NOSERVER
1564 * Generate the rpc reply header
1565 * siz arg. is used to decide if adding a cluster is worthwhile
1568 nfs_rephead(int siz
, struct nfsrv_descript
*nd
, struct nfssvc_sock
*slp
,
1569 int err
, struct mbuf
**mrq
, struct mbuf
**mbp
, caddr_t
*bposp
)
1572 struct nfsm_info info
;
1574 siz
+= RPC_REPLYSIZ
;
1575 info
.mb
= m_getl(max_hdr
+ siz
, MB_WAIT
, MT_DATA
, M_PKTHDR
, NULL
);
1576 info
.mreq
= info
.mb
;
1577 info
.mreq
->m_pkthdr
.len
= 0;
1579 * If this is not a cluster, try and leave leading space
1580 * for the lower level headers.
1582 if ((max_hdr
+ siz
) < MINCLSIZE
)
1583 info
.mreq
->m_data
+= max_hdr
;
1584 tl
= mtod(info
.mreq
, u_int32_t
*);
1585 info
.mreq
->m_len
= 6 * NFSX_UNSIGNED
;
1586 info
.bpos
= ((caddr_t
)tl
) + info
.mreq
->m_len
;
1587 *tl
++ = txdr_unsigned(nd
->nd_retxid
);
1589 if (err
== ERPCMISMATCH
|| (err
& NFSERR_AUTHERR
)) {
1590 *tl
++ = rpc_msgdenied
;
1591 if (err
& NFSERR_AUTHERR
) {
1592 *tl
++ = rpc_autherr
;
1593 *tl
= txdr_unsigned(err
& ~NFSERR_AUTHERR
);
1594 info
.mreq
->m_len
-= NFSX_UNSIGNED
;
1595 info
.bpos
-= NFSX_UNSIGNED
;
1597 *tl
++ = rpc_mismatch
;
1598 *tl
++ = txdr_unsigned(RPC_VER2
);
1599 *tl
= txdr_unsigned(RPC_VER2
);
1602 *tl
++ = rpc_msgaccepted
;
1605 * For Kerberos authentication, we must send the nickname
1606 * verifier back, otherwise just RPCAUTH_NULL.
1608 if (nd
->nd_flag
& ND_KERBFULL
) {
1609 struct nfsuid
*nuidp
;
1610 struct timeval ktvin
, ktvout
;
1612 for (nuidp
= NUIDHASH(slp
, nd
->nd_cr
.cr_uid
)->lh_first
;
1613 nuidp
!= 0; nuidp
= nuidp
->nu_hash
.le_next
) {
1614 if (nuidp
->nu_cr
.cr_uid
== nd
->nd_cr
.cr_uid
&&
1615 (!nd
->nd_nam2
|| netaddr_match(NU_NETFAM(nuidp
),
1616 &nuidp
->nu_haddr
, nd
->nd_nam2
)))
1621 txdr_unsigned(nuidp
->nu_timestamp
.tv_sec
- 1);
1623 txdr_unsigned(nuidp
->nu_timestamp
.tv_usec
);
1626 * Encrypt the timestamp in ecb mode using the
1636 *tl
++ = rpc_auth_kerb
;
1637 *tl
++ = txdr_unsigned(3 * NFSX_UNSIGNED
);
1638 *tl
= ktvout
.tv_sec
;
1639 tl
= nfsm_build(&info
, 3 * NFSX_UNSIGNED
);
1640 *tl
++ = ktvout
.tv_usec
;
1641 *tl
++ = txdr_unsigned(nuidp
->nu_cr
.cr_uid
);
1652 *tl
= txdr_unsigned(RPC_PROGUNAVAIL
);
1655 *tl
= txdr_unsigned(RPC_PROGMISMATCH
);
1656 tl
= nfsm_build(&info
, 2 * NFSX_UNSIGNED
);
1657 *tl
++ = txdr_unsigned(2);
1658 *tl
= txdr_unsigned(3);
1661 *tl
= txdr_unsigned(RPC_PROCUNAVAIL
);
1664 *tl
= txdr_unsigned(RPC_GARBAGE
);
1668 if (err
!= NFSERR_RETVOID
) {
1669 tl
= nfsm_build(&info
, NFSX_UNSIGNED
);
1671 *tl
= txdr_unsigned(nfsrv_errmap(nd
, err
));
1683 if (err
!= 0 && err
!= NFSERR_RETVOID
)
1684 nfsstats
.srvrpc_errs
++;
1689 #endif /* NFS_NOSERVER */
1692 * Nfs timer routine.
1694 * Scan the nfsreq list and retranmit any requests that have timed out
1695 * To avoid retransmission attempts on STREAM sockets (in the future) make
1696 * sure to set the r_retry field to 0 (implies nm_retry == 0).
1698 * Requests with attached responses, terminated requests, and
1699 * locked requests are ignored. Locked requests will be picked up
1700 * in a later timer call.
1703 nfs_timer_callout(void *arg
/* never used */)
1705 struct nfsmount
*nmp
;
1707 #ifndef NFS_NOSERVER
1708 struct nfssvc_sock
*slp
;
1710 #endif /* NFS_NOSERVER */
1712 lwkt_gettoken(&nfs_token
);
1713 TAILQ_FOREACH(nmp
, &nfs_mountq
, nm_entry
) {
1714 lwkt_gettoken(&nmp
->nm_token
);
1715 TAILQ_FOREACH(req
, &nmp
->nm_reqq
, r_chain
) {
1716 KKASSERT(nmp
== req
->r_nmp
);
1719 if (req
->r_flags
& (R_SOFTTERM
| R_LOCKED
))
1723 * Handle timeout/retry. Be sure to process r_mrep
1724 * for async requests that completed while we had
1725 * the request locked or they will hang in the reqq
1728 req
->r_flags
|= R_LOCKED
;
1729 if (nfs_sigintr(nmp
, req
, req
->r_td
)) {
1730 nfs_softterm(req
, 1);
1731 req
->r_flags
&= ~R_LOCKED
;
1734 if (req
->r_flags
& R_ASYNC
) {
1736 nfs_hardterm(req
, 1);
1737 req
->r_flags
&= ~R_LOCKED
;
1738 nfssvc_iod_reader_wakeup(nmp
);
1740 req
->r_flags
&= ~R_LOCKED
;
1743 if (req
->r_flags
& R_WANTED
) {
1744 req
->r_flags
&= ~R_WANTED
;
1748 lwkt_reltoken(&nmp
->nm_token
);
1750 #ifndef NFS_NOSERVER
1753 * Scan the write gathering queues for writes that need to be
1756 cur_usec
= nfs_curusec();
1758 TAILQ_FOREACH(slp
, &nfssvc_sockhead
, ns_chain
) {
1759 /* XXX race against removal */
1760 if (lwkt_trytoken(&slp
->ns_token
)) {
1761 if (slp
->ns_tq
.lh_first
&&
1762 (slp
->ns_tq
.lh_first
->nd_time
<= cur_usec
)) {
1763 nfsrv_wakenfsd(slp
, 1);
1765 lwkt_reltoken(&slp
->ns_token
);
1768 #endif /* NFS_NOSERVER */
1770 callout_reset(&nfs_timer_handle
, nfs_ticks
, nfs_timer_callout
, NULL
);
1771 lwkt_reltoken(&nfs_token
);
1776 nfs_timer_req(struct nfsreq
*req
)
1778 struct thread
*td
= &thread0
; /* XXX for creds, will break if sleep */
1779 struct nfsmount
*nmp
= req
->r_nmp
;
1786 * rtt ticks and timeout calculation. Return if the timeout
1787 * has not been reached yet, unless the packet is flagged
1788 * for an immediate send.
1790 * The mean rtt doesn't help when we get random I/Os, we have
1791 * to multiply by fairly large numbers.
1793 if (req
->r_rtt
>= 0) {
1795 * Calculate the timeout to test against.
1798 if (nmp
->nm_flag
& NFSMNT_DUMBTIMR
) {
1799 timeo
= nmp
->nm_timeo
<< NFS_RTT_SCALE_BITS
;
1800 } else if (req
->r_flags
& R_TIMING
) {
1801 timeo
= NFS_SRTT(req
) + NFS_SDRTT(req
);
1803 timeo
= nmp
->nm_timeo
<< NFS_RTT_SCALE_BITS
;
1805 timeo
*= multt
[req
->r_procnum
];
1806 /* timeo is still scaled by SCALE_BITS */
1808 #define NFSFS (NFS_RTT_SCALE * NFS_HZ)
1809 if (req
->r_flags
& R_TIMING
) {
1810 static long last_time
;
1811 if (nfs_showrtt
&& last_time
!= time_second
) {
1812 kprintf("rpccmd %d NFS SRTT %d SDRTT %d "
1814 proct
[req
->r_procnum
],
1815 NFS_SRTT(req
), NFS_SDRTT(req
),
1817 timeo
% NFSFS
* 1000 / NFSFS
);
1818 last_time
= time_second
;
1824 * deal with nfs_timer jitter.
1826 timeo
= (timeo
>> NFS_RTT_SCALE_BITS
) + 1;
1830 if (nmp
->nm_timeouts
> 0)
1831 timeo
*= nfs_backoff
[nmp
->nm_timeouts
- 1];
1832 if (timeo
> NFS_MAXTIMEO
)
1833 timeo
= NFS_MAXTIMEO
;
1834 if (req
->r_rtt
<= timeo
) {
1835 if ((req
->r_flags
& R_NEEDSXMIT
) == 0)
1837 } else if (nmp
->nm_timeouts
< 8) {
1843 * Check for server not responding
1845 if ((req
->r_flags
& R_TPRINTFMSG
) == 0 &&
1846 req
->r_rexmit
> nmp
->nm_deadthresh
) {
1847 nfs_msg(req
->r_td
, nmp
->nm_mountp
->mnt_stat
.f_mntfromname
,
1849 req
->r_flags
|= R_TPRINTFMSG
;
1851 if (req
->r_rexmit
>= req
->r_retry
) { /* too many */
1852 nfsstats
.rpctimeouts
++;
1853 nfs_softterm(req
, 1);
1858 * Generally disable retransmission on reliable sockets,
1859 * unless the request is flagged for immediate send.
1861 if (nmp
->nm_sotype
!= SOCK_DGRAM
) {
1862 if (++req
->r_rexmit
> NFS_MAXREXMIT
)
1863 req
->r_rexmit
= NFS_MAXREXMIT
;
1864 if ((req
->r_flags
& R_NEEDSXMIT
) == 0)
1869 * Stop here if we do not have a socket!
1871 if ((so
= nmp
->nm_so
) == NULL
)
1875 * If there is enough space and the window allows.. resend it.
1877 * r_rtt is left intact in case we get an answer after the
1878 * retry that was a reply to the original packet.
1880 * NOTE: so_pru_send()
1882 if (ssb_space(&so
->so_snd
) >= req
->r_mreq
->m_pkthdr
.len
&&
1883 (req
->r_flags
& (R_SENT
| R_NEEDSXMIT
)) &&
1884 (m
= m_copym(req
->r_mreq
, 0, M_COPYALL
, MB_DONTWAIT
))){
1885 if ((nmp
->nm_flag
& NFSMNT_NOCONN
) == 0)
1886 error
= so_pru_send(so
, 0, m
, NULL
, NULL
, td
);
1888 error
= so_pru_send(so
, 0, m
, nmp
->nm_nam
, NULL
, td
);
1890 if (NFSIGNORE_SOERROR(nmp
->nm_soflags
, error
))
1892 req
->r_flags
|= R_NEEDSXMIT
;
1893 } else if (req
->r_mrep
== NULL
) {
1895 * Iff first send, start timing
1896 * else turn timing off, backoff timer
1897 * and divide congestion window by 2.
1899 * It is possible for the so_pru_send() to
1900 * block and for us to race a reply so we
1901 * only do this if the reply field has not
1902 * been filled in. R_LOCKED will prevent
1903 * the request from being ripped out from under
1906 * Record the last resent procnum to aid us
1907 * in duplicate detection on receive.
1909 if ((req
->r_flags
& R_NEEDSXMIT
) == 0) {
1912 if (++req
->r_rexmit
> NFS_MAXREXMIT
)
1913 req
->r_rexmit
= NFS_MAXREXMIT
;
1914 nmp
->nm_maxasync_scaled
>>= 1;
1915 if (nmp
->nm_maxasync_scaled
< NFS_MINASYNC_SCALED
)
1916 nmp
->nm_maxasync_scaled
= NFS_MINASYNC_SCALED
;
1917 nfsstats
.rpcretries
++;
1918 nmp
->nm_lastreprocnum
= req
->r_procnum
;
1920 req
->r_flags
|= R_SENT
;
1921 req
->r_flags
&= ~R_NEEDSXMIT
;
1928 * Mark all of an nfs mount's outstanding requests with R_SOFTTERM and
1929 * wait for all requests to complete. This is used by forced unmounts
1930 * to terminate any outstanding RPCs.
1932 * Locked requests cannot be canceled but will be marked for
1936 nfs_nmcancelreqs(struct nfsmount
*nmp
)
1942 TAILQ_FOREACH(req
, &nmp
->nm_reqq
, r_chain
) {
1943 if (req
->r_mrep
!= NULL
|| (req
->r_flags
& R_SOFTTERM
))
1945 nfs_softterm(req
, 0);
1947 /* XXX the other two queues as well */
1950 for (i
= 0; i
< 30; i
++) {
1952 TAILQ_FOREACH(req
, &nmp
->nm_reqq
, r_chain
) {
1953 if (nmp
== req
->r_nmp
)
1959 tsleep(&lbolt
, 0, "nfscancel", 0);
1965 * Soft-terminate a request, effectively marking it as failed.
1967 * Must be called from within a critical section.
1970 nfs_softterm(struct nfsreq
*rep
, int islocked
)
1972 rep
->r_flags
|= R_SOFTTERM
;
1973 nfs_hardterm(rep
, islocked
);
1977 * Hard-terminate a request, typically after getting a response.
1979 * The state machine can still decide to re-issue it later if necessary.
1981 * Must be called from within a critical section.
1984 nfs_hardterm(struct nfsreq
*rep
, int islocked
)
1986 struct nfsmount
*nmp
= rep
->r_nmp
;
1989 * The nm_send count is decremented now to avoid deadlocks
1990 * when the process in soreceive() hasn't yet managed to send
1993 if (rep
->r_flags
& R_SENT
) {
1994 rep
->r_flags
&= ~R_SENT
;
1998 * If we locked the request or nobody else has locked the request,
1999 * and the request is async, we can move it to the reader thread's
2000 * queue now and fix up the state.
2002 * If we locked the request or nobody else has locked the request,
2003 * we can wake up anyone blocked waiting for a response on the
2006 if (islocked
|| (rep
->r_flags
& R_LOCKED
) == 0) {
2007 if ((rep
->r_flags
& (R_ONREQQ
| R_ASYNC
)) ==
2008 (R_ONREQQ
| R_ASYNC
)) {
2009 rep
->r_flags
&= ~R_ONREQQ
;
2010 TAILQ_REMOVE(&nmp
->nm_reqq
, rep
, r_chain
);
2012 TAILQ_INSERT_TAIL(&nmp
->nm_reqrxq
, rep
, r_chain
);
2013 KKASSERT(rep
->r_info
->state
== NFSM_STATE_TRY
||
2014 rep
->r_info
->state
== NFSM_STATE_WAITREPLY
);
2015 rep
->r_info
->state
= NFSM_STATE_PROCESSREPLY
;
2016 nfssvc_iod_reader_wakeup(nmp
);
2017 if (TAILQ_FIRST(&nmp
->nm_bioq
) &&
2018 nmp
->nm_reqqlen
<= nfs_maxasyncbio
* 2 / 3) {
2019 nfssvc_iod_writer_wakeup(nmp
);
2022 mtx_abort_ex_link(&nmp
->nm_rxlock
, &rep
->r_link
);
2027 * Test for a termination condition pending on the process.
2028 * This is used for NFSMNT_INT mounts.
2031 nfs_sigintr(struct nfsmount
*nmp
, struct nfsreq
*rep
, struct thread
*td
)
2037 if (rep
&& (rep
->r_flags
& R_SOFTTERM
))
2039 /* Terminate all requests while attempting a forced unmount. */
2040 if (nmp
->nm_mountp
->mnt_kern_flag
& MNTK_UNMOUNTF
)
2042 if (!(nmp
->nm_flag
& NFSMNT_INT
))
2044 /* td might be NULL YYY */
2045 if (td
== NULL
|| (p
= td
->td_proc
) == NULL
)
2049 tmpset
= lwp_sigpend(lp
);
2050 SIGSETNAND(tmpset
, lp
->lwp_sigmask
);
2051 SIGSETNAND(tmpset
, p
->p_sigignore
);
2052 if (SIGNOTEMPTY(tmpset
) && NFSINT_SIGMASK(tmpset
))
2059 * Lock a socket against others.
2060 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
2061 * and also to avoid race conditions between the processes with nfs requests
2062 * in progress when a reconnect is necessary.
2065 nfs_sndlock(struct nfsmount
*nmp
, struct nfsreq
*rep
)
2067 mtx_t mtx
= &nmp
->nm_txlock
;
2075 td
= rep
? rep
->r_td
: NULL
;
2076 if (nmp
->nm_flag
& NFSMNT_INT
)
2079 while ((error
= mtx_lock_ex_try(mtx
)) != 0) {
2080 if (nfs_sigintr(nmp
, rep
, td
)) {
2084 error
= mtx_lock_ex(mtx
, "nfsndlck", slpflag
, slptimeo
);
2087 if (slpflag
== PCATCH
) {
2092 /* Always fail if our request has been cancelled. */
2093 if (rep
&& (rep
->r_flags
& R_SOFTTERM
)) {
2102 * Unlock the stream socket for others.
2105 nfs_sndunlock(struct nfsmount
*nmp
)
2107 mtx_unlock(&nmp
->nm_txlock
);
2111 * Lock the receiver side of the socket.
2116 nfs_rcvlock(struct nfsmount
*nmp
, struct nfsreq
*rep
)
2118 mtx_t mtx
= &nmp
->nm_rxlock
;
2124 * Unconditionally check for completion in case another nfsiod
2125 * get the packet while the caller was blocked, before the caller
2126 * called us. Packet reception is handled by mainline code which
2127 * is protected by the BGL at the moment.
2129 * We do not strictly need the second check just before the
2130 * tsleep(), but it's good defensive programming.
2132 if (rep
&& rep
->r_mrep
!= NULL
)
2135 if (nmp
->nm_flag
& NFSMNT_INT
)
2141 while ((error
= mtx_lock_ex_try(mtx
)) != 0) {
2142 if (nfs_sigintr(nmp
, rep
, (rep
? rep
->r_td
: NULL
))) {
2146 if (rep
&& rep
->r_mrep
!= NULL
) {
2152 * NOTE: can return ENOLCK, but in that case rep->r_mrep
2153 * will already be set.
2156 error
= mtx_lock_ex_link(mtx
, &rep
->r_link
,
2160 error
= mtx_lock_ex(mtx
, "nfsrcvlk", slpflag
, slptimeo
);
2166 * If our reply was recieved while we were sleeping,
2167 * then just return without taking the lock to avoid a
2168 * situation where a single iod could 'capture' the
2171 if (rep
&& rep
->r_mrep
!= NULL
) {
2175 if (slpflag
== PCATCH
) {
2181 if (rep
&& rep
->r_mrep
!= NULL
) {
2190 * Unlock the stream socket for others.
2193 nfs_rcvunlock(struct nfsmount
*nmp
)
2195 mtx_unlock(&nmp
->nm_rxlock
);
2201 * Check for badly aligned mbuf data and realign by copying the unaligned
2202 * portion of the data into a new mbuf chain and freeing the portions
2203 * of the old chain that were replaced.
2205 * We cannot simply realign the data within the existing mbuf chain
2206 * because the underlying buffers may contain other rpc commands and
2207 * we cannot afford to overwrite them.
2209 * We would prefer to avoid this situation entirely. The situation does
2210 * not occur with NFS/UDP and is supposed to only occassionally occur
2211 * with TCP. Use vfs.nfs.realign_count and realign_test to check this.
2213 * NOTE! MB_DONTWAIT cannot be used here. The mbufs must be acquired
2214 * because the rpc request OR reply cannot be thrown away. TCP NFS
2215 * mounts do not retry their RPCs unless the TCP connection itself
2216 * is dropped so throwing away a RPC will basically cause the NFS
2217 * operation to lockup indefinitely.
2220 nfs_realign(struct mbuf
**pm
, int hsiz
)
2223 struct mbuf
*n
= NULL
;
2226 * Check for misalignemnt
2229 while ((m
= *pm
) != NULL
) {
2230 if ((m
->m_len
& 0x3) || (mtod(m
, intptr_t) & 0x3))
2236 * If misalignment found make a completely new copy.
2239 ++nfs_realign_count
;
2240 n
= m_dup_data(m
, MB_WAIT
);
2246 #ifndef NFS_NOSERVER
2249 * Parse an RPC request
2251 * - fill in the cred struct.
2254 nfs_getreq(struct nfsrv_descript
*nd
, struct nfsd
*nfsd
, int has_header
)
2261 u_int32_t nfsvers
, auth_type
;
2263 int error
= 0, ticklen
;
2264 struct nfsuid
*nuidp
;
2265 struct timeval tvin
, tvout
;
2266 struct nfsm_info info
;
2267 #if 0 /* until encrypted keys are implemented */
2268 NFSKERBKEYSCHED_T keys
; /* stores key schedule */
2271 info
.mrep
= nd
->nd_mrep
;
2272 info
.md
= nd
->nd_md
;
2273 info
.dpos
= nd
->nd_dpos
;
2276 NULLOUT(tl
= nfsm_dissect(&info
, 10 * NFSX_UNSIGNED
));
2277 nd
->nd_retxid
= fxdr_unsigned(u_int32_t
, *tl
++);
2278 if (*tl
++ != rpc_call
) {
2283 NULLOUT(tl
= nfsm_dissect(&info
, 8 * NFSX_UNSIGNED
));
2287 if (*tl
++ != rpc_vers
) {
2288 nd
->nd_repstat
= ERPCMISMATCH
;
2289 nd
->nd_procnum
= NFSPROC_NOOP
;
2292 if (*tl
!= nfs_prog
) {
2293 nd
->nd_repstat
= EPROGUNAVAIL
;
2294 nd
->nd_procnum
= NFSPROC_NOOP
;
2298 nfsvers
= fxdr_unsigned(u_int32_t
, *tl
++);
2299 if (nfsvers
< NFS_VER2
|| nfsvers
> NFS_VER3
) {
2300 nd
->nd_repstat
= EPROGMISMATCH
;
2301 nd
->nd_procnum
= NFSPROC_NOOP
;
2304 if (nfsvers
== NFS_VER3
)
2305 nd
->nd_flag
= ND_NFSV3
;
2306 nd
->nd_procnum
= fxdr_unsigned(u_int32_t
, *tl
++);
2307 if (nd
->nd_procnum
== NFSPROC_NULL
)
2309 if (nd
->nd_procnum
>= NFS_NPROCS
||
2310 (nd
->nd_procnum
>= NQNFSPROC_GETLEASE
) ||
2311 (!nd
->nd_flag
&& nd
->nd_procnum
> NFSV2PROC_STATFS
)) {
2312 nd
->nd_repstat
= EPROCUNAVAIL
;
2313 nd
->nd_procnum
= NFSPROC_NOOP
;
2316 if ((nd
->nd_flag
& ND_NFSV3
) == 0)
2317 nd
->nd_procnum
= nfsv3_procid
[nd
->nd_procnum
];
2319 len
= fxdr_unsigned(int, *tl
++);
2320 if (len
< 0 || len
> RPCAUTH_MAXSIZ
) {
2325 nd
->nd_flag
&= ~ND_KERBAUTH
;
2327 * Handle auth_unix or auth_kerb.
2329 if (auth_type
== rpc_auth_unix
) {
2330 len
= fxdr_unsigned(int, *++tl
);
2331 if (len
< 0 || len
> NFS_MAXNAMLEN
) {
2335 ERROROUT(nfsm_adv(&info
, nfsm_rndup(len
)));
2336 NULLOUT(tl
= nfsm_dissect(&info
, 3 * NFSX_UNSIGNED
));
2337 bzero((caddr_t
)&nd
->nd_cr
, sizeof (struct ucred
));
2338 nd
->nd_cr
.cr_ref
= 1;
2339 nd
->nd_cr
.cr_uid
= fxdr_unsigned(uid_t
, *tl
++);
2340 nd
->nd_cr
.cr_ruid
= nd
->nd_cr
.cr_svuid
= nd
->nd_cr
.cr_uid
;
2341 nd
->nd_cr
.cr_gid
= fxdr_unsigned(gid_t
, *tl
++);
2342 nd
->nd_cr
.cr_rgid
= nd
->nd_cr
.cr_svgid
= nd
->nd_cr
.cr_gid
;
2343 len
= fxdr_unsigned(int, *tl
);
2344 if (len
< 0 || len
> RPCAUTH_UNIXGIDS
) {
2348 NULLOUT(tl
= nfsm_dissect(&info
, (len
+ 2) * NFSX_UNSIGNED
));
2349 for (i
= 1; i
<= len
; i
++)
2351 nd
->nd_cr
.cr_groups
[i
] = fxdr_unsigned(gid_t
, *tl
++);
2354 nd
->nd_cr
.cr_ngroups
= (len
>= NGROUPS
) ? NGROUPS
: (len
+ 1);
2355 if (nd
->nd_cr
.cr_ngroups
> 1)
2356 nfsrvw_sort(nd
->nd_cr
.cr_groups
, nd
->nd_cr
.cr_ngroups
);
2357 len
= fxdr_unsigned(int, *++tl
);
2358 if (len
< 0 || len
> RPCAUTH_MAXSIZ
) {
2363 ERROROUT(nfsm_adv(&info
, nfsm_rndup(len
)));
2365 } else if (auth_type
== rpc_auth_kerb
) {
2366 switch (fxdr_unsigned(int, *tl
++)) {
2367 case RPCAKN_FULLNAME
:
2368 ticklen
= fxdr_unsigned(int, *tl
);
2369 *((u_int32_t
*)nfsd
->nfsd_authstr
) = *tl
;
2370 uio
.uio_resid
= nfsm_rndup(ticklen
) + NFSX_UNSIGNED
;
2371 nfsd
->nfsd_authlen
= uio
.uio_resid
+ NFSX_UNSIGNED
;
2372 if (uio
.uio_resid
> (len
- 2 * NFSX_UNSIGNED
)) {
2379 uio
.uio_segflg
= UIO_SYSSPACE
;
2380 iov
.iov_base
= (caddr_t
)&nfsd
->nfsd_authstr
[4];
2381 iov
.iov_len
= RPCAUTH_MAXSIZ
- 4;
2382 ERROROUT(nfsm_mtouio(&info
, &uio
, uio
.uio_resid
));
2383 NULLOUT(tl
= nfsm_dissect(&info
, 2 * NFSX_UNSIGNED
));
2384 if (*tl
++ != rpc_auth_kerb
||
2385 fxdr_unsigned(int, *tl
) != 4 * NFSX_UNSIGNED
) {
2386 kprintf("Bad kerb verifier\n");
2387 nd
->nd_repstat
= (NFSERR_AUTHERR
|AUTH_BADVERF
);
2388 nd
->nd_procnum
= NFSPROC_NOOP
;
2391 NULLOUT(cp
= nfsm_dissect(&info
, 4 * NFSX_UNSIGNED
));
2392 tl
= (u_int32_t
*)cp
;
2393 if (fxdr_unsigned(int, *tl
) != RPCAKN_FULLNAME
) {
2394 kprintf("Not fullname kerb verifier\n");
2395 nd
->nd_repstat
= (NFSERR_AUTHERR
|AUTH_BADVERF
);
2396 nd
->nd_procnum
= NFSPROC_NOOP
;
2399 cp
+= NFSX_UNSIGNED
;
2400 bcopy(cp
, nfsd
->nfsd_verfstr
, 3 * NFSX_UNSIGNED
);
2401 nfsd
->nfsd_verflen
= 3 * NFSX_UNSIGNED
;
2402 nd
->nd_flag
|= ND_KERBFULL
;
2403 nfsd
->nfsd_flag
|= NFSD_NEEDAUTH
;
2405 case RPCAKN_NICKNAME
:
2406 if (len
!= 2 * NFSX_UNSIGNED
) {
2407 kprintf("Kerb nickname short\n");
2408 nd
->nd_repstat
= (NFSERR_AUTHERR
|AUTH_BADCRED
);
2409 nd
->nd_procnum
= NFSPROC_NOOP
;
2412 nickuid
= fxdr_unsigned(uid_t
, *tl
);
2413 NULLOUT(tl
= nfsm_dissect(&info
, 2 * NFSX_UNSIGNED
));
2414 if (*tl
++ != rpc_auth_kerb
||
2415 fxdr_unsigned(int, *tl
) != 3 * NFSX_UNSIGNED
) {
2416 kprintf("Kerb nick verifier bad\n");
2417 nd
->nd_repstat
= (NFSERR_AUTHERR
|AUTH_BADVERF
);
2418 nd
->nd_procnum
= NFSPROC_NOOP
;
2421 NULLOUT(tl
= nfsm_dissect(&info
, 3 * NFSX_UNSIGNED
));
2422 tvin
.tv_sec
= *tl
++;
2425 for (nuidp
= NUIDHASH(nfsd
->nfsd_slp
,nickuid
)->lh_first
;
2426 nuidp
!= 0; nuidp
= nuidp
->nu_hash
.le_next
) {
2427 if (nuidp
->nu_cr
.cr_uid
== nickuid
&&
2429 netaddr_match(NU_NETFAM(nuidp
),
2430 &nuidp
->nu_haddr
, nd
->nd_nam2
)))
2435 (NFSERR_AUTHERR
|AUTH_REJECTCRED
);
2436 nd
->nd_procnum
= NFSPROC_NOOP
;
2441 * Now, decrypt the timestamp using the session key
2451 tvout
.tv_sec
= fxdr_unsigned(long, tvout
.tv_sec
);
2452 tvout
.tv_usec
= fxdr_unsigned(long, tvout
.tv_usec
);
2453 if (nuidp
->nu_expire
< time_second
||
2454 nuidp
->nu_timestamp
.tv_sec
> tvout
.tv_sec
||
2455 (nuidp
->nu_timestamp
.tv_sec
== tvout
.tv_sec
&&
2456 nuidp
->nu_timestamp
.tv_usec
> tvout
.tv_usec
)) {
2457 nuidp
->nu_expire
= 0;
2459 (NFSERR_AUTHERR
|AUTH_REJECTVERF
);
2460 nd
->nd_procnum
= NFSPROC_NOOP
;
2463 nfsrv_setcred(&nuidp
->nu_cr
, &nd
->nd_cr
);
2464 nd
->nd_flag
|= ND_KERBNICK
;
2467 nd
->nd_repstat
= (NFSERR_AUTHERR
| AUTH_REJECTCRED
);
2468 nd
->nd_procnum
= NFSPROC_NOOP
;
2472 nd
->nd_md
= info
.md
;
2473 nd
->nd_dpos
= info
.dpos
;
2482 * Send a message to the originating process's terminal. The thread and/or
2483 * process may be NULL. YYY the thread should not be NULL but there may
2484 * still be some uio_td's that are still being passed as NULL through to
2488 nfs_msg(struct thread
*td
, char *server
, char *msg
)
2492 if (td
&& td
->td_proc
)
2493 tpr
= tprintf_open(td
->td_proc
);
2496 tprintf(tpr
, "nfs server %s: %s\n", server
, msg
);
2501 #ifndef NFS_NOSERVER
2504 * Socket upcall routine for nfsd sockets. This runs in the protocol
2505 * thread and passes waitflag == MB_DONTWAIT.
2508 nfsrv_rcv_upcall(struct socket
*so
, void *arg
, int waitflag
)
2510 struct nfssvc_sock
*slp
= (struct nfssvc_sock
*)arg
;
2512 if (slp
->ns_needq_upcall
== 0) {
2513 slp
->ns_needq_upcall
= 1; /* ok to race */
2514 lwkt_gettoken(&nfs_token
);
2515 nfsrv_wakenfsd(slp
, 1);
2516 lwkt_reltoken(&nfs_token
);
2519 lwkt_gettoken(&slp
->ns_token
);
2520 slp
->ns_flag
|= SLP_NEEDQ
;
2521 nfsrv_rcv(so
, arg
, waitflag
);
2522 lwkt_reltoken(&slp
->ns_token
);
2527 * Process new data on a receive socket. Essentially do as much as we can
2528 * non-blocking, else punt and it will be called with MB_WAIT from an nfsd.
2530 * slp->ns_token is held on call
2533 nfsrv_rcv(struct socket
*so
, void *arg
, int waitflag
)
2535 struct nfssvc_sock
*slp
= (struct nfssvc_sock
*)arg
;
2537 struct sockaddr
*nam
;
2540 int nparallel_wakeup
= 0;
2542 ASSERT_LWKT_TOKEN_HELD(&slp
->ns_token
);
2544 if ((slp
->ns_flag
& SLP_VALID
) == 0)
2548 * Do not allow an infinite number of completed RPC records to build
2549 * up before we stop reading data from the socket. Otherwise we could
2550 * end up holding onto an unreasonable number of mbufs for requests
2551 * waiting for service.
2553 * This should give pretty good feedback to the TCP layer and
2554 * prevents a memory crunch for other protocols.
2556 * Note that the same service socket can be dispatched to several
2557 * nfs servers simultaniously. The tcp protocol callback calls us
2558 * with MB_DONTWAIT. nfsd calls us with MB_WAIT (typically).
2560 if (NFSRV_RECLIMIT(slp
))
2564 * Handle protocol specifics to parse an RPC request. We always
2565 * pull from the socket using non-blocking I/O.
2567 if (so
->so_type
== SOCK_STREAM
) {
2569 * The data has to be read in an orderly fashion from a TCP
2570 * stream, unlike a UDP socket. It is possible for soreceive
2571 * and/or nfsrv_getstream() to block, so make sure only one
2572 * entity is messing around with the TCP stream at any given
2573 * moment. The receive sockbuf's lock in soreceive is not
2576 if (slp
->ns_flag
& SLP_GETSTREAM
)
2578 slp
->ns_flag
|= SLP_GETSTREAM
;
2581 * Do soreceive(). Pull out as much data as possible without
2584 sbinit(&sio
, 1000000000);
2585 flags
= MSG_DONTWAIT
;
2586 error
= so_pru_soreceive(so
, &nam
, NULL
, &sio
, NULL
, &flags
);
2587 if (error
|| sio
.sb_mb
== NULL
) {
2588 if (error
!= EWOULDBLOCK
)
2589 slp
->ns_flag
|= SLP_DISCONN
;
2590 slp
->ns_flag
&= ~(SLP_GETSTREAM
| SLP_NEEDQ
);
2594 if (slp
->ns_rawend
) {
2595 slp
->ns_rawend
->m_next
= m
;
2596 slp
->ns_cc
+= sio
.sb_cc
;
2599 slp
->ns_cc
= sio
.sb_cc
;
2606 * Now try and parse as many record(s) as we can out of the
2607 * raw stream data. This will set SLP_DOREC.
2609 error
= nfsrv_getstream(slp
, waitflag
, &nparallel_wakeup
);
2610 if (error
&& error
!= EWOULDBLOCK
)
2611 slp
->ns_flag
|= SLP_DISCONN
;
2612 slp
->ns_flag
&= ~SLP_GETSTREAM
;
2615 * For UDP soreceive typically pulls just one packet, loop
2616 * to get the whole batch.
2619 sbinit(&sio
, 1000000000);
2620 flags
= MSG_DONTWAIT
;
2621 error
= so_pru_soreceive(so
, &nam
, NULL
, &sio
,
2624 struct nfsrv_rec
*rec
;
2625 int mf
= (waitflag
& MB_DONTWAIT
) ?
2626 M_NOWAIT
: M_WAITOK
;
2627 rec
= kmalloc(sizeof(struct nfsrv_rec
),
2631 FREE(nam
, M_SONAME
);
2635 nfs_realign(&sio
.sb_mb
, 10 * NFSX_UNSIGNED
);
2636 rec
->nr_address
= nam
;
2637 rec
->nr_packet
= sio
.sb_mb
;
2638 STAILQ_INSERT_TAIL(&slp
->ns_rec
, rec
, nr_link
);
2640 slp
->ns_flag
|= SLP_DOREC
;
2643 slp
->ns_flag
&= ~SLP_NEEDQ
;
2646 if ((so
->so_proto
->pr_flags
& PR_CONNREQUIRED
)
2647 && error
!= EWOULDBLOCK
) {
2648 slp
->ns_flag
|= SLP_DISCONN
;
2652 if (NFSRV_RECLIMIT(slp
))
2654 } while (sio
.sb_mb
);
2658 * If we were upcalled from the tcp protocol layer and we have
2659 * fully parsed records ready to go, or there is new data pending,
2660 * or something went wrong, try to wake up a nfsd thread to deal
2664 /* XXX this code is currently not executed (nfsrv_rcv_upcall) */
2665 if (waitflag
== MB_DONTWAIT
&& (slp
->ns_flag
& SLP_ACTION_MASK
)) {
2666 lwkt_gettoken(&nfs_token
);
2667 nfsrv_wakenfsd(slp
, nparallel_wakeup
);
2668 lwkt_reltoken(&nfs_token
);
2673 * Try and extract an RPC request from the mbuf data list received on a
2674 * stream socket. The "waitflag" argument indicates whether or not it
2678 nfsrv_getstream(struct nfssvc_sock
*slp
, int waitflag
, int *countp
)
2680 struct mbuf
*m
, **mpp
;
2683 struct mbuf
*om
, *m2
, *recm
;
2687 if (slp
->ns_reclen
== 0) {
2688 if (slp
->ns_cc
< NFSX_UNSIGNED
)
2691 if (m
->m_len
>= NFSX_UNSIGNED
) {
2692 bcopy(mtod(m
, caddr_t
), (caddr_t
)&recmark
, NFSX_UNSIGNED
);
2693 m
->m_data
+= NFSX_UNSIGNED
;
2694 m
->m_len
-= NFSX_UNSIGNED
;
2696 cp1
= (caddr_t
)&recmark
;
2697 cp2
= mtod(m
, caddr_t
);
2698 while (cp1
< ((caddr_t
)&recmark
) + NFSX_UNSIGNED
) {
2699 while (m
->m_len
== 0) {
2701 cp2
= mtod(m
, caddr_t
);
2708 slp
->ns_cc
-= NFSX_UNSIGNED
;
2709 recmark
= ntohl(recmark
);
2710 slp
->ns_reclen
= recmark
& ~0x80000000;
2711 if (recmark
& 0x80000000)
2712 slp
->ns_flag
|= SLP_LASTFRAG
;
2714 slp
->ns_flag
&= ~SLP_LASTFRAG
;
2715 if (slp
->ns_reclen
> NFS_MAXPACKET
|| slp
->ns_reclen
<= 0) {
2716 log(LOG_ERR
, "%s (%d) from nfs client\n",
2717 "impossible packet length",
2724 * Now get the record part.
2726 * Note that slp->ns_reclen may be 0. Linux sometimes
2727 * generates 0-length RPCs
2730 if (slp
->ns_cc
== slp
->ns_reclen
) {
2732 slp
->ns_raw
= slp
->ns_rawend
= NULL
;
2733 slp
->ns_cc
= slp
->ns_reclen
= 0;
2734 } else if (slp
->ns_cc
> slp
->ns_reclen
) {
2739 while (len
< slp
->ns_reclen
) {
2740 if ((len
+ m
->m_len
) > slp
->ns_reclen
) {
2741 m2
= m_copym(m
, 0, slp
->ns_reclen
- len
,
2749 m
->m_data
+= slp
->ns_reclen
- len
;
2750 m
->m_len
-= slp
->ns_reclen
- len
;
2751 len
= slp
->ns_reclen
;
2753 return (EWOULDBLOCK
);
2755 } else if ((len
+ m
->m_len
) == slp
->ns_reclen
) {
2775 * Accumulate the fragments into a record.
2777 mpp
= &slp
->ns_frag
;
2779 mpp
= &((*mpp
)->m_next
);
2781 if (slp
->ns_flag
& SLP_LASTFRAG
) {
2782 struct nfsrv_rec
*rec
;
2783 int mf
= (waitflag
& MB_DONTWAIT
) ? M_NOWAIT
: M_WAITOK
;
2784 rec
= kmalloc(sizeof(struct nfsrv_rec
), M_NFSRVDESC
, mf
);
2786 m_freem(slp
->ns_frag
);
2788 nfs_realign(&slp
->ns_frag
, 10 * NFSX_UNSIGNED
);
2789 rec
->nr_address
= NULL
;
2790 rec
->nr_packet
= slp
->ns_frag
;
2791 STAILQ_INSERT_TAIL(&slp
->ns_rec
, rec
, nr_link
);
2793 slp
->ns_flag
|= SLP_DOREC
;
2796 slp
->ns_frag
= NULL
;
2804 * Sanity check our mbuf chain.
2807 nfs_checkpkt(struct mbuf
*m
, int len
)
2815 panic("nfs_checkpkt: len mismatch %d/%d mbuf %p\n",
2823 nfs_checkpkt(struct mbuf
*m __unused
, int len __unused
)
2830 * Parse an RPC header.
2832 * If the socket is invalid or no records are pending we return ENOBUFS.
2833 * The caller must deal with NEEDQ races.
2836 nfsrv_dorec(struct nfssvc_sock
*slp
, struct nfsd
*nfsd
,
2837 struct nfsrv_descript
**ndp
)
2839 struct nfsrv_rec
*rec
;
2841 struct sockaddr
*nam
;
2842 struct nfsrv_descript
*nd
;
2846 if ((slp
->ns_flag
& SLP_VALID
) == 0 || !STAILQ_FIRST(&slp
->ns_rec
))
2848 rec
= STAILQ_FIRST(&slp
->ns_rec
);
2849 STAILQ_REMOVE_HEAD(&slp
->ns_rec
, nr_link
);
2850 KKASSERT(slp
->ns_numrec
> 0);
2851 if (--slp
->ns_numrec
== 0)
2852 slp
->ns_flag
&= ~SLP_DOREC
;
2853 nam
= rec
->nr_address
;
2855 kfree(rec
, M_NFSRVDESC
);
2856 MALLOC(nd
, struct nfsrv_descript
*, sizeof (struct nfsrv_descript
),
2857 M_NFSRVDESC
, M_WAITOK
);
2858 nd
->nd_md
= nd
->nd_mrep
= m
;
2860 nd
->nd_dpos
= mtod(m
, caddr_t
);
2861 error
= nfs_getreq(nd
, nfsd
, TRUE
);
2864 FREE(nam
, M_SONAME
);
2866 kfree((caddr_t
)nd
, M_NFSRVDESC
);
2875 * Try to assign service sockets to nfsd threads based on the number
2876 * of new rpc requests that have been queued on the service socket.
2878 * If no nfsd's are available or additonal requests are pending, set the
2879 * NFSD_CHECKSLP flag so that one of the running nfsds will go look for
2880 * the work in the nfssvc_sock list when it is finished processing its
2881 * current work. This flag is only cleared when an nfsd can not find
2882 * any new work to perform.
2885 nfsrv_wakenfsd(struct nfssvc_sock
*slp
, int nparallel
)
2889 if ((slp
->ns_flag
& SLP_VALID
) == 0)
2893 TAILQ_FOREACH(nd
, &nfsd_head
, nfsd_chain
) {
2894 if (nd
->nfsd_flag
& NFSD_WAITING
) {
2895 nd
->nfsd_flag
&= ~NFSD_WAITING
;
2897 panic("nfsd wakeup");
2900 wakeup((caddr_t
)nd
);
2901 if (--nparallel
== 0)
2907 * If we couldn't assign slp then the NFSDs are all busy and
2908 * we set a flag indicating that there is pending work.
2911 nfsd_head_flag
|= NFSD_CHECKSLP
;
2913 #endif /* NFS_NOSERVER */