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>
64 #include <sys/mutex.h>
66 #include <sys/signal2.h>
67 #include <sys/mutex2.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);
152 int (*nfsrv3_procs[NFS_NPROCS]) (struct nfsrv_descript *nd,
153 struct nfssvc_sock *slp,
155 struct mbuf **mreqp) = {
183 #endif /* NFS_NOSERVER */
186 * Initialize sockets and congestion for a new NFS connection.
187 * We do not free the sockaddr if error.
190 nfs_connect(struct nfsmount *nmp, struct nfsreq *rep)
194 struct sockaddr *saddr;
195 struct sockaddr_in *sin;
196 struct thread *td = &thread0; /* only used for socreate and sobind */
198 nmp->nm_so = so = NULL;
199 if (nmp->nm_flag & NFSMNT_FORCE)
202 error = socreate(saddr->sa_family, &so, nmp->nm_sotype,
203 nmp->nm_soproto, td);
206 nmp->nm_soflags = so->so_proto->pr_flags;
209 * Some servers require that the client port be a reserved port number.
211 if (saddr->sa_family == AF_INET && (nmp->nm_flag & NFSMNT_RESVPORT)) {
214 struct sockaddr_in ssin;
216 bzero(&sopt, sizeof sopt);
217 ip = IP_PORTRANGE_LOW;
218 sopt.sopt_level = IPPROTO_IP;
219 sopt.sopt_name = IP_PORTRANGE;
220 sopt.sopt_val = (void *)&ip;
221 sopt.sopt_valsize = sizeof(ip);
223 error = sosetopt(so, &sopt);
226 bzero(&ssin, sizeof ssin);
228 sin->sin_len = sizeof (struct sockaddr_in);
229 sin->sin_family = AF_INET;
230 sin->sin_addr.s_addr = INADDR_ANY;
231 sin->sin_port = htons(0);
232 error = sobind(so, (struct sockaddr *)sin, td);
235 bzero(&sopt, sizeof sopt);
236 ip = IP_PORTRANGE_DEFAULT;
237 sopt.sopt_level = IPPROTO_IP;
238 sopt.sopt_name = IP_PORTRANGE;
239 sopt.sopt_val = (void *)&ip;
240 sopt.sopt_valsize = sizeof(ip);
242 error = sosetopt(so, &sopt);
248 * Protocols that do not require connections may be optionally left
249 * unconnected for servers that reply from a port other than NFS_PORT.
251 if (nmp->nm_flag & NFSMNT_NOCONN) {
252 if (nmp->nm_soflags & PR_CONNREQUIRED) {
257 error = soconnect(so, nmp->nm_nam, td);
262 * Wait for the connection to complete. Cribbed from the
263 * connect system call but with the wait timing out so
264 * that interruptible mounts don't hang here for a long time.
267 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
268 (void) tsleep((caddr_t)&so->so_timeo, 0,
270 if ((so->so_state & SS_ISCONNECTING) &&
271 so->so_error == 0 && rep &&
272 (error = nfs_sigintr(nmp, rep, rep->r_td)) != 0){
273 so->so_state &= ~SS_ISCONNECTING;
279 error = so->so_error;
286 so->so_rcv.ssb_timeo = (5 * hz);
287 so->so_snd.ssb_timeo = (5 * hz);
290 * Get buffer reservation size from sysctl, but impose reasonable
293 if (nmp->nm_sotype == SOCK_STREAM) {
294 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
298 bzero(&sopt, sizeof sopt);
299 sopt.sopt_level = SOL_SOCKET;
300 sopt.sopt_name = SO_KEEPALIVE;
301 sopt.sopt_val = &val;
302 sopt.sopt_valsize = sizeof val;
306 if (so->so_proto->pr_protocol == IPPROTO_TCP) {
310 bzero(&sopt, sizeof sopt);
311 sopt.sopt_level = IPPROTO_TCP;
312 sopt.sopt_name = TCP_NODELAY;
313 sopt.sopt_val = &val;
314 sopt.sopt_valsize = sizeof val;
319 error = soreserve(so, nfs_soreserve, nfs_soreserve, NULL);
322 so->so_rcv.ssb_flags |= SSB_NOINTR;
323 so->so_snd.ssb_flags |= SSB_NOINTR;
325 /* Initialize other non-zero congestion variables */
326 nmp->nm_srtt[0] = nmp->nm_srtt[1] = nmp->nm_srtt[2] =
327 nmp->nm_srtt[3] = (NFS_TIMEO << NFS_RTT_SCALE_BITS);
328 nmp->nm_sdrtt[0] = nmp->nm_sdrtt[1] = nmp->nm_sdrtt[2] =
329 nmp->nm_sdrtt[3] = 0;
330 nmp->nm_maxasync_scaled = NFS_MINASYNC_SCALED;
331 nmp->nm_timeouts = 0;
334 * Assign nm_so last. The moment nm_so is assigned the nfs_timer()
335 * can mess with the socket.
342 soshutdown(so, SHUT_RDWR);
343 soclose(so, FNONBLOCK);
350 * Called when a connection is broken on a reliable protocol.
351 * - clean up the old socket
352 * - nfs_connect() again
353 * - set R_NEEDSXMIT for all outstanding requests on mount point
354 * If this fails the mount point is DEAD!
355 * nb: Must be called with the nfs_sndlock() set on the mount point.
358 nfs_reconnect(struct nfsmount *nmp, struct nfsreq *rep)
364 if (nmp->nm_rxstate >= NFSSVC_STOPPING)
366 while ((error = nfs_connect(nmp, rep)) != 0) {
367 if (error == EINTR || error == ERESTART)
371 if (nmp->nm_rxstate >= NFSSVC_STOPPING)
373 (void) tsleep((caddr_t)&lbolt, 0, "nfscon", 0);
377 * Loop through outstanding request list and fix up all requests
381 TAILQ_FOREACH(req, &nmp->nm_reqq, r_chain) {
382 KKASSERT(req->r_nmp == nmp);
383 req->r_flags |= R_NEEDSXMIT;
390 * NFS disconnect. Clean up and unlink.
393 nfs_disconnect(struct nfsmount *nmp)
400 soshutdown(so, SHUT_RDWR);
401 soclose(so, FNONBLOCK);
406 nfs_safedisconnect(struct nfsmount *nmp)
408 nfs_rcvlock(nmp, NULL);
414 * This is the nfs send routine. For connection based socket types, it
415 * must be called with an nfs_sndlock() on the socket.
416 * "rep == NULL" indicates that it has been called from a server.
417 * For the client side:
418 * - return EINTR if the RPC is terminated, 0 otherwise
419 * - set R_NEEDSXMIT if the send fails for any reason
420 * - do any cleanup required by recoverable socket errors (?)
421 * For the server side:
422 * - return EINTR or ERESTART if interrupted by a signal
423 * - return EPIPE if a connection is lost for connection based sockets (TCP...)
424 * - do any cleanup required by recoverable socket errors (?)
427 nfs_send(struct socket *so, struct sockaddr *nam, struct mbuf *top,
430 struct sockaddr *sendnam;
431 int error, soflags, flags;
434 if (rep->r_flags & R_SOFTTERM) {
438 if ((so = rep->r_nmp->nm_so) == NULL) {
439 rep->r_flags |= R_NEEDSXMIT;
443 rep->r_flags &= ~R_NEEDSXMIT;
444 soflags = rep->r_nmp->nm_soflags;
446 soflags = so->so_proto->pr_flags;
448 if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED))
452 if (so->so_type == SOCK_SEQPACKET)
457 error = so_pru_sosend(so, sendnam, NULL, top, NULL, flags,
460 * ENOBUFS for dgram sockets is transient and non fatal.
461 * No need to log, and no need to break a soft mount.
463 if (error == ENOBUFS && so->so_type == SOCK_DGRAM) {
466 * do backoff retransmit on client
469 if ((rep->r_nmp->nm_state & NFSSTA_SENDSPACE) == 0) {
470 rep->r_nmp->nm_state |= NFSSTA_SENDSPACE;
471 kprintf("Warning: NFS: Insufficient sendspace "
473 "\t You must increase vfs.nfs.soreserve"
474 "or decrease vfs.nfs.maxasyncbio\n",
475 so->so_snd.ssb_hiwat);
477 rep->r_flags |= R_NEEDSXMIT;
483 log(LOG_INFO, "nfs send error %d for server %s\n",error,
484 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
486 * Deal with errors for the client side.
488 if (rep->r_flags & R_SOFTTERM)
491 rep->r_flags |= R_NEEDSXMIT;
493 log(LOG_INFO, "nfsd send error %d\n", error);
497 * Handle any recoverable (soft) socket errors here. (?)
499 if (error != EINTR && error != ERESTART &&
500 error != EWOULDBLOCK && error != EPIPE)
507 * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all
508 * done by soreceive(), but for SOCK_STREAM we must deal with the Record
509 * Mark and consolidate the data into a new mbuf list.
510 * nb: Sometimes TCP passes the data up to soreceive() in long lists of
512 * For SOCK_STREAM we must be very careful to read an entire record once
513 * we have read any of it, even if the system call has been interrupted.
516 nfs_receive(struct nfsmount *nmp, struct nfsreq *rep,
517 struct sockaddr **aname, struct mbuf **mp)
524 struct mbuf *control;
526 struct sockaddr **getnam;
527 int error, sotype, rcvflg;
528 struct thread *td = curthread; /* XXX */
531 * Set up arguments for soreceive()
535 sotype = nmp->nm_sotype;
538 * For reliable protocols, lock against other senders/receivers
539 * in case a reconnect is necessary.
540 * For SOCK_STREAM, first get the Record Mark to find out how much
541 * more there is to get.
542 * We must lock the socket against other receivers
543 * until we have an entire rpc request/reply.
545 if (sotype != SOCK_DGRAM) {
546 error = nfs_sndlock(nmp, rep);
551 * Check for fatal errors and resending request.
554 * Ugh: If a reconnect attempt just happened, nm_so
555 * would have changed. NULL indicates a failed
556 * attempt that has essentially shut down this
559 if (rep && (rep->r_mrep || (rep->r_flags & R_SOFTTERM))) {
565 error = nfs_reconnect(nmp, rep);
572 while (rep && (rep->r_flags & R_NEEDSXMIT)) {
573 m = m_copym(rep->r_mreq, 0, M_COPYALL, MB_WAIT);
574 nfsstats.rpcretries++;
575 error = nfs_send(so, rep->r_nmp->nm_nam, m, rep);
577 if (error == EINTR || error == ERESTART ||
578 (error = nfs_reconnect(nmp, rep)) != 0) {
586 if (sotype == SOCK_STREAM) {
588 * Get the length marker from the stream
590 aio.iov_base = (caddr_t)&len;
591 aio.iov_len = sizeof(u_int32_t);
594 auio.uio_segflg = UIO_SYSSPACE;
595 auio.uio_rw = UIO_READ;
597 auio.uio_resid = sizeof(u_int32_t);
600 rcvflg = MSG_WAITALL;
601 error = so_pru_soreceive(so, NULL, &auio, NULL,
603 if (error == EWOULDBLOCK && rep) {
604 if (rep->r_flags & R_SOFTTERM)
607 } while (error == EWOULDBLOCK);
609 if (error == 0 && auio.uio_resid > 0) {
611 * Only log short packets if not EOF
613 if (auio.uio_resid != sizeof(u_int32_t))
615 "short receive (%d/%d) from nfs server %s\n",
616 (int)(sizeof(u_int32_t) - auio.uio_resid),
617 (int)sizeof(u_int32_t),
618 nmp->nm_mountp->mnt_stat.f_mntfromname);
623 len = ntohl(len) & ~0x80000000;
625 * This is SERIOUS! We are out of sync with the sender
626 * and forcing a disconnect/reconnect is all I can do.
628 if (len > NFS_MAXPACKET) {
629 log(LOG_ERR, "%s (%d) from nfs server %s\n",
630 "impossible packet length",
632 nmp->nm_mountp->mnt_stat.f_mntfromname);
638 * Get the rest of the packet as an mbuf chain
642 rcvflg = MSG_WAITALL;
643 error = so_pru_soreceive(so, NULL, NULL, &sio,
645 } while (error == EWOULDBLOCK || error == EINTR ||
647 if (error == 0 && sio.sb_cc != len) {
650 "short receive (%zu/%d) from nfs server %s\n",
651 (size_t)len - auio.uio_resid, len,
652 nmp->nm_mountp->mnt_stat.f_mntfromname);
658 * Non-stream, so get the whole packet by not
659 * specifying MSG_WAITALL and by specifying a large
662 * We have no use for control msg., but must grab them
663 * and then throw them away so we know what is going
666 sbinit(&sio, 100000000);
669 error = so_pru_soreceive(so, NULL, NULL, &sio,
673 if (error == EWOULDBLOCK && rep) {
674 if (rep->r_flags & R_SOFTTERM) {
679 } while (error == EWOULDBLOCK ||
680 (error == 0 && sio.sb_mb == NULL && control));
681 if ((rcvflg & MSG_EOR) == 0)
683 if (error == 0 && sio.sb_mb == NULL)
689 if (error && error != EINTR && error != ERESTART) {
692 if (error != EPIPE) {
694 "receive error %d from nfs server %s\n",
696 nmp->nm_mountp->mnt_stat.f_mntfromname);
698 error = nfs_sndlock(nmp, rep);
700 error = nfs_reconnect(nmp, rep);
708 if ((so = nmp->nm_so) == NULL)
710 if (so->so_state & SS_ISCONNECTED)
714 sbinit(&sio, 100000000);
717 error = so_pru_soreceive(so, getnam, NULL, &sio,
719 if (error == EWOULDBLOCK && rep &&
720 (rep->r_flags & R_SOFTTERM)) {
724 } while (error == EWOULDBLOCK);
730 * A shutdown may result in no error and no mbuf.
733 if (*mp == NULL && error == 0)
742 * Search for any mbufs that are not a multiple of 4 bytes long
743 * or with m_data not longword aligned.
744 * These could cause pointer alignment problems, so copy them to
745 * well aligned mbufs.
747 nfs_realign(mp, 5 * NFSX_UNSIGNED);
752 * Implement receipt of reply on a socket.
754 * We must search through the list of received datagrams matching them
755 * with outstanding requests using the xid, until ours is found.
757 * If myrep is NULL we process packets on the socket until
758 * interrupted or until nm_reqrxq is non-empty.
762 nfs_reply(struct nfsmount *nmp, struct nfsreq *myrep)
765 struct sockaddr *nam;
769 struct nfsm_info info;
772 * Loop around until we get our own reply
776 * Lock against other receivers so that I don't get stuck in
777 * sbwait() after someone else has received my reply for me.
778 * Also necessary for connection based protocols to avoid
779 * race conditions during a reconnect.
781 * If nfs_rcvlock() returns EALREADY, that means that
782 * the reply has already been recieved by another
783 * process and we can return immediately. In this
784 * case, the lock is not taken to avoid races with
789 error = nfs_rcvlock(nmp, myrep);
790 if (error == EALREADY)
796 * If myrep is NULL we are the receiver helper thread.
797 * Stop waiting for incoming replies if there are
798 * messages sitting on reqrxq that we need to process,
799 * or if a shutdown request is pending.
801 if (myrep == NULL && (TAILQ_FIRST(&nmp->nm_reqrxq) ||
802 nmp->nm_rxstate > NFSSVC_PENDING)) {
808 * Get the next Rpc reply off the socket
810 * We cannot release the receive lock until we've
811 * filled in rep->r_mrep, otherwise a waiting
812 * thread may deadlock in soreceive with no incoming
815 error = nfs_receive(nmp, myrep, &nam, &info.mrep);
818 * Ignore routing errors on connectionless protocols??
821 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
822 if (nmp->nm_so == NULL)
824 nmp->nm_so->so_error = 0;
833 * Get the xid and check that it is an rpc reply
836 info.dpos = mtod(info.md, caddr_t);
837 NULLOUT(tl = nfsm_dissect(&info, 2*NFSX_UNSIGNED));
839 if (*tl != rpc_reply) {
840 nfsstats.rpcinvalid++;
849 * Loop through the request list to match up the reply
850 * Iff no match, just drop the datagram. On match, set
851 * r_mrep atomically to prevent the timer from messing
852 * around with the request after we have exited the critical
856 TAILQ_FOREACH(rep, &nmp->nm_reqq, r_chain) {
857 if (rep->r_mrep == NULL && rxid == rep->r_xid)
862 * Fill in the rest of the reply if we found a match.
864 * Deal with duplicate responses if there was no match.
868 rep->r_dpos = info.dpos;
872 rt = &nfsrtt.rttl[nfsrtt.pos];
873 rt->proc = rep->r_procnum;
876 rt->cwnd = nmp->nm_maxasync_scaled;
877 rt->srtt = nmp->nm_srtt[proct[rep->r_procnum] - 1];
878 rt->sdrtt = nmp->nm_sdrtt[proct[rep->r_procnum] - 1];
879 rt->fsid = nmp->nm_mountp->mnt_stat.f_fsid;
880 getmicrotime(&rt->tstamp);
881 if (rep->r_flags & R_TIMING)
882 rt->rtt = rep->r_rtt;
885 nfsrtt.pos = (nfsrtt.pos + 1) % NFSRTTLOGSIZ;
889 * New congestion control is based only on async
892 if (nmp->nm_maxasync_scaled < NFS_MAXASYNC_SCALED)
893 ++nmp->nm_maxasync_scaled;
894 if (rep->r_flags & R_SENT) {
895 rep->r_flags &= ~R_SENT;
898 * Update rtt using a gain of 0.125 on the mean
899 * and a gain of 0.25 on the deviation.
901 * NOTE SRTT/SDRTT are only good if R_TIMING is set.
903 if ((rep->r_flags & R_TIMING) && rep->r_rexmit == 0) {
905 * Since the timer resolution of
906 * NFS_HZ is so course, it can often
907 * result in r_rtt == 0. Since
908 * r_rtt == N means that the actual
909 * rtt is between N+dt and N+2-dt ticks,
915 #define NFSRSB NFS_RTT_SCALE_BITS
916 n = ((NFS_SRTT(rep) * 7) +
917 (rep->r_rtt << NFSRSB)) >> 3;
918 d = n - NFS_SRTT(rep);
922 * Don't let the jitter calculation decay
923 * too quickly, but we want a fast rampup.
928 if (d < NFS_SDRTT(rep))
929 n = ((NFS_SDRTT(rep) * 15) + d) >> 4;
931 n = ((NFS_SDRTT(rep) * 3) + d) >> 2;
935 nmp->nm_timeouts = 0;
936 rep->r_mrep = info.mrep;
937 nfs_hardterm(rep, 0);
940 * Extract vers, prog, nfsver, procnum. A duplicate
941 * response means we didn't wait long enough so
942 * we increase the SRTT to avoid future spurious
945 u_int procnum = nmp->nm_lastreprocnum;
948 if (procnum < NFS_NPROCS && proct[procnum]) {
951 n = nmp->nm_srtt[proct[procnum]];
952 n += NFS_ASYSCALE * NFS_HZ;
953 if (n < NFS_ASYSCALE * NFS_HZ * 10)
954 n = NFS_ASYSCALE * NFS_HZ * 10;
955 nmp->nm_srtt[proct[procnum]] = n;
962 * If not matched to a request, drop it.
963 * If it's mine, get out.
966 nfsstats.rpcunexpected++;
969 } else if (rep == myrep) {
970 if (rep->r_mrep == NULL)
971 panic("nfsreply nil");
978 * Run the request state machine until the target state is reached
979 * or a fatal error occurs. The target state is not run. Specifying
980 * a target of NFSM_STATE_DONE runs the state machine until the rpc
983 * EINPROGRESS is returned for all states other then the DONE state,
984 * indicating that the rpc is still in progress.
987 nfs_request(struct nfsm_info *info, nfsm_state_t bstate, nfsm_state_t estate)
991 while (info->state >= bstate && info->state < estate) {
992 switch(info->state) {
993 case NFSM_STATE_SETUP:
995 * Setup the nfsreq. Any error which occurs during
996 * this state is fatal.
998 info->error = nfs_request_setup(info);
1000 info->state = NFSM_STATE_DONE;
1001 return (info->error);
1004 req->r_mrp = &info->mrep;
1005 req->r_mdp = &info->md;
1006 req->r_dposp = &info->dpos;
1007 info->state = NFSM_STATE_AUTH;
1010 case NFSM_STATE_AUTH:
1012 * Authenticate the nfsreq. Any error which occurs
1013 * during this state is fatal.
1015 info->error = nfs_request_auth(info->req);
1017 info->state = NFSM_STATE_DONE;
1018 return (info->error);
1020 info->state = NFSM_STATE_TRY;
1023 case NFSM_STATE_TRY:
1025 * Transmit or retransmit attempt. An error in this
1026 * state is ignored and we always move on to the
1029 * This can trivially race the receiver if the
1030 * request is asynchronous. nfs_request_try()
1031 * will thus set the state for us and we
1032 * must also return immediately if we are
1033 * running an async state machine, because
1034 * info can become invalid due to races after
1037 if (info->req->r_flags & R_ASYNC) {
1038 nfs_request_try(info->req);
1039 if (estate == NFSM_STATE_WAITREPLY)
1040 return (EINPROGRESS);
1042 nfs_request_try(info->req);
1043 info->state = NFSM_STATE_WAITREPLY;
1046 case NFSM_STATE_WAITREPLY:
1048 * Wait for a reply or timeout and move on to the
1049 * next state. The error returned by this state
1050 * is passed to the processing code in the next
1053 info->error = nfs_request_waitreply(info->req);
1054 info->state = NFSM_STATE_PROCESSREPLY;
1056 case NFSM_STATE_PROCESSREPLY:
1058 * Process the reply or timeout. Errors which occur
1059 * in this state may cause the state machine to
1060 * go back to an earlier state, and are fatal
1063 info->error = nfs_request_processreply(info,
1065 switch(info->error) {
1067 info->state = NFSM_STATE_AUTH;
1070 info->state = NFSM_STATE_TRY;
1074 * Operation complete, with or without an
1075 * error. We are done.
1078 info->state = NFSM_STATE_DONE;
1079 return (info->error);
1082 case NFSM_STATE_DONE:
1084 * Shouldn't be reached
1086 return (info->error);
1092 * If we are done return the error code (if any).
1093 * Otherwise return EINPROGRESS.
1095 if (info->state == NFSM_STATE_DONE)
1096 return (info->error);
1097 return (EINPROGRESS);
1101 * nfs_request - goes something like this
1102 * - fill in request struct
1103 * - links it into list
1104 * - calls nfs_send() for first transmit
1105 * - calls nfs_receive() to get reply
1106 * - break down rpc header and return with nfs reply pointed to
1108 * nb: always frees up mreq mbuf list
1111 nfs_request_setup(nfsm_info_t info)
1114 struct nfsmount *nmp;
1119 * Reject requests while attempting a forced unmount.
1121 if (info->vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF) {
1122 m_freem(info->mreq);
1126 nmp = VFSTONFS(info->vp->v_mount);
1127 req = kmalloc(sizeof(struct nfsreq), M_NFSREQ, M_WAITOK);
1129 req->r_vp = info->vp;
1130 req->r_td = info->td;
1131 req->r_procnum = info->procnum;
1133 req->r_cred = info->cred;
1141 req->r_mrest = info->mreq;
1142 req->r_mrest_len = i;
1145 * The presence of a non-NULL r_info in req indicates
1146 * async completion via our helper threads. See the receiver
1151 req->r_flags = R_ASYNC;
1161 nfs_request_auth(struct nfsreq *rep)
1163 struct nfsmount *nmp = rep->r_nmp;
1165 char nickv[RPCX_NICKVERF];
1166 int error = 0, auth_len, auth_type;
1169 char *auth_str, *verf_str;
1173 rep->r_failed_auth = 0;
1176 * Get the RPC header with authorization.
1178 verf_str = auth_str = NULL;
1179 if (nmp->nm_flag & NFSMNT_KERB) {
1181 verf_len = sizeof (nickv);
1182 auth_type = RPCAUTH_KERB4;
1183 bzero((caddr_t)rep->r_key, sizeof(rep->r_key));
1184 if (rep->r_failed_auth ||
1185 nfs_getnickauth(nmp, cred, &auth_str, &auth_len,
1186 verf_str, verf_len)) {
1187 error = nfs_getauth(nmp, rep, cred, &auth_str,
1188 &auth_len, verf_str, &verf_len, rep->r_key);
1190 m_freem(rep->r_mrest);
1191 rep->r_mrest = NULL;
1192 kfree((caddr_t)rep, M_NFSREQ);
1197 auth_type = RPCAUTH_UNIX;
1198 if (cred->cr_ngroups < 1)
1199 panic("nfsreq nogrps");
1200 auth_len = ((((cred->cr_ngroups - 1) > nmp->nm_numgrps) ?
1201 nmp->nm_numgrps : (cred->cr_ngroups - 1)) << 2) +
1204 m = nfsm_rpchead(cred, nmp->nm_flag, rep->r_procnum, auth_type,
1205 auth_len, auth_str, verf_len, verf_str,
1206 rep->r_mrest, rep->r_mrest_len, &rep->r_mheadend, &xid);
1207 rep->r_mrest = NULL;
1209 kfree(auth_str, M_TEMP);
1212 * For stream protocols, insert a Sun RPC Record Mark.
1214 if (nmp->nm_sotype == SOCK_STREAM) {
1215 M_PREPEND(m, NFSX_UNSIGNED, MB_WAIT);
1217 kfree(rep, M_NFSREQ);
1220 *mtod(m, u_int32_t *) = htonl(0x80000000 |
1221 (m->m_pkthdr.len - NFSX_UNSIGNED));
1229 nfs_request_try(struct nfsreq *rep)
1231 struct nfsmount *nmp = rep->r_nmp;
1236 * Request is not on any queue, only the owner has access to it
1237 * so it should not be locked by anyone atm.
1239 * Interlock to prevent races. While locked the only remote
1240 * action possible is for r_mrep to be set (once we enqueue it).
1242 if (rep->r_flags == 0xdeadc0de) {
1244 panic("flags nbad\n");
1246 KKASSERT((rep->r_flags & (R_LOCKED | R_ONREQQ)) == 0);
1247 if (nmp->nm_flag & NFSMNT_SOFT)
1248 rep->r_retry = nmp->nm_retry;
1250 rep->r_retry = NFS_MAXREXMIT + 1; /* past clip limit */
1251 rep->r_rtt = rep->r_rexmit = 0;
1252 if (proct[rep->r_procnum] > 0)
1253 rep->r_flags |= R_TIMING | R_LOCKED;
1255 rep->r_flags |= R_LOCKED;
1259 * Do the client side RPC.
1261 nfsstats.rpcrequests++;
1263 if (nmp->nm_flag & NFSMNT_FORCE) {
1264 rep->r_flags |= R_SOFTTERM;
1265 rep->r_flags &= ~R_LOCKED;
1270 * Chain request into list of outstanding requests. Be sure
1271 * to put it LAST so timer finds oldest requests first. Note
1272 * that our control of R_LOCKED prevents the request from
1273 * getting ripped out from under us or transmitted by the
1276 * For requests with info structures we must atomically set the
1277 * info's state because the structure could become invalid upon
1278 * return due to races (i.e., if async)
1281 mtx_link_init(&rep->r_link);
1282 TAILQ_INSERT_TAIL(&nmp->nm_reqq, rep, r_chain);
1283 rep->r_flags |= R_ONREQQ;
1285 if (rep->r_flags & R_ASYNC)
1286 rep->r_info->state = NFSM_STATE_WAITREPLY;
1292 * Send if we can. Congestion control is not handled here any more
1293 * becausing trying to defer the initial send based on the nfs_timer
1294 * requires having a very fast nfs_timer, which is silly.
1297 if (nmp->nm_soflags & PR_CONNREQUIRED)
1298 error = nfs_sndlock(nmp, rep);
1300 m2 = m_copym(rep->r_mreq, 0, M_COPYALL, MB_WAIT);
1301 error = nfs_send(nmp->nm_so, nmp->nm_nam, m2, rep);
1302 if (nmp->nm_soflags & PR_CONNREQUIRED)
1304 rep->r_flags &= ~R_NEEDSXMIT;
1305 if ((rep->r_flags & R_SENT) == 0) {
1306 rep->r_flags |= R_SENT;
1309 rep->r_flags |= R_NEEDSXMIT;
1312 rep->r_flags |= R_NEEDSXMIT;
1319 * Release the lock. The only remote action that may have occurred
1320 * would have been the setting of rep->r_mrep. If this occured
1321 * and the request was async we have to move it to the reader
1322 * thread's queue for action.
1324 * For async requests also make sure the reader is woken up so
1325 * it gets on the socket to read responses.
1328 if (rep->r_flags & R_ASYNC) {
1330 nfs_hardterm(rep, 1);
1331 rep->r_flags &= ~R_LOCKED;
1332 nfssvc_iod_reader_wakeup(nmp);
1334 rep->r_flags &= ~R_LOCKED;
1336 if (rep->r_flags & R_WANTED) {
1337 rep->r_flags &= ~R_WANTED;
1345 * This code is only called for synchronous requests. Completed synchronous
1346 * requests are left on reqq and we remove them before moving on to the
1350 nfs_request_waitreply(struct nfsreq *rep)
1352 struct nfsmount *nmp = rep->r_nmp;
1355 KKASSERT((rep->r_flags & R_ASYNC) == 0);
1358 * Wait until the request is finished.
1360 error = nfs_reply(nmp, rep);
1363 * RPC done, unlink the request, but don't rip it out from under
1364 * the callout timer.
1366 * Once unlinked no other receiver or the timer will have
1367 * visibility, so we do not have to set R_LOCKED.
1370 while (rep->r_flags & R_LOCKED) {
1371 rep->r_flags |= R_WANTED;
1372 tsleep(rep, 0, "nfstrac", 0);
1374 KKASSERT(rep->r_flags & R_ONREQQ);
1375 TAILQ_REMOVE(&nmp->nm_reqq, rep, r_chain);
1376 rep->r_flags &= ~R_ONREQQ;
1378 if (TAILQ_FIRST(&nmp->nm_bioq) &&
1379 nmp->nm_reqqlen <= nfs_maxasyncbio * 2 / 3) {
1380 nfssvc_iod_writer_wakeup(nmp);
1385 * Decrement the outstanding request count.
1387 if (rep->r_flags & R_SENT) {
1388 rep->r_flags &= ~R_SENT;
1394 * Process reply with error returned from nfs_requet_waitreply().
1396 * Returns EAGAIN if it wants us to loop up to nfs_request_try() again.
1397 * Returns ENEEDAUTH if it wants us to loop up to nfs_request_auth() again.
1400 nfs_request_processreply(nfsm_info_t info, int error)
1402 struct nfsreq *req = info->req;
1403 struct nfsmount *nmp = req->r_nmp;
1409 * If there was a successful reply and a tprintf msg.
1410 * tprintf a response.
1412 if (error == 0 && (req->r_flags & R_TPRINTFMSG)) {
1413 nfs_msg(req->r_td, nmp->nm_mountp->mnt_stat.f_mntfromname,
1416 info->mrep = req->r_mrep;
1417 info->md = req->r_md;
1418 info->dpos = req->r_dpos;
1420 m_freem(req->r_mreq);
1422 kfree(req, M_NFSREQ);
1428 * break down the rpc header and check if ok
1430 NULLOUT(tl = nfsm_dissect(info, 3 * NFSX_UNSIGNED));
1431 if (*tl++ == rpc_msgdenied) {
1432 if (*tl == rpc_mismatch) {
1434 } else if ((nmp->nm_flag & NFSMNT_KERB) &&
1435 *tl++ == rpc_autherr) {
1436 if (req->r_failed_auth == 0) {
1437 req->r_failed_auth++;
1438 req->r_mheadend->m_next = NULL;
1439 m_freem(info->mrep);
1441 m_freem(req->r_mreq);
1449 m_freem(info->mrep);
1451 m_freem(req->r_mreq);
1453 kfree(req, M_NFSREQ);
1459 * Grab any Kerberos verifier, otherwise just throw it away.
1461 verf_type = fxdr_unsigned(int, *tl++);
1462 i = fxdr_unsigned(int32_t, *tl);
1463 if ((nmp->nm_flag & NFSMNT_KERB) && verf_type == RPCAUTH_KERB4) {
1464 error = nfs_savenickauth(nmp, req->r_cred, i, req->r_key,
1465 &info->md, &info->dpos, info->mrep);
1469 ERROROUT(nfsm_adv(info, nfsm_rndup(i)));
1471 NULLOUT(tl = nfsm_dissect(info, NFSX_UNSIGNED));
1474 NULLOUT(tl = nfsm_dissect(info, NFSX_UNSIGNED));
1476 error = fxdr_unsigned(int, *tl);
1479 * Does anyone even implement this? Just impose
1482 if ((nmp->nm_flag & NFSMNT_NFSV3) &&
1483 error == NFSERR_TRYLATER) {
1484 m_freem(info->mrep);
1488 tsleep((caddr_t)&lbolt, 0, "nqnfstry", 0);
1489 return (EAGAIN); /* goto tryagain */
1493 * If the File Handle was stale, invalidate the
1494 * lookup cache, just in case.
1496 * To avoid namecache<->vnode deadlocks we must
1497 * release the vnode lock if we hold it.
1499 if (error == ESTALE) {
1500 struct vnode *vp = req->r_vp;
1503 ltype = lockstatus(&vp->v_lock, curthread);
1504 if (ltype == LK_EXCLUSIVE || ltype == LK_SHARED)
1505 lockmgr(&vp->v_lock, LK_RELEASE);
1506 cache_inval_vp(vp, CINV_CHILDREN);
1507 if (ltype == LK_EXCLUSIVE || ltype == LK_SHARED)
1508 lockmgr(&vp->v_lock, ltype);
1510 if (nmp->nm_flag & NFSMNT_NFSV3) {
1511 KKASSERT(*req->r_mrp == info->mrep);
1512 KKASSERT(*req->r_mdp == info->md);
1513 KKASSERT(*req->r_dposp == info->dpos);
1514 error |= NFSERR_RETERR;
1516 m_freem(info->mrep);
1519 m_freem(req->r_mreq);
1521 kfree(req, M_NFSREQ);
1526 KKASSERT(*req->r_mrp == info->mrep);
1527 KKASSERT(*req->r_mdp == info->md);
1528 KKASSERT(*req->r_dposp == info->dpos);
1529 m_freem(req->r_mreq);
1531 FREE(req, M_NFSREQ);
1534 m_freem(info->mrep);
1536 error = EPROTONOSUPPORT;
1538 m_freem(req->r_mreq);
1540 kfree(req, M_NFSREQ);
1545 #ifndef NFS_NOSERVER
1547 * Generate the rpc reply header
1548 * siz arg. is used to decide if adding a cluster is worthwhile
1551 nfs_rephead(int siz, struct nfsrv_descript *nd, struct nfssvc_sock *slp,
1552 int err, struct mbuf **mrq, struct mbuf **mbp, caddr_t *bposp)
1555 struct nfsm_info info;
1557 siz += RPC_REPLYSIZ;
1558 info.mb = m_getl(max_hdr + siz, MB_WAIT, MT_DATA, M_PKTHDR, NULL);
1559 info.mreq = info.mb;
1560 info.mreq->m_pkthdr.len = 0;
1562 * If this is not a cluster, try and leave leading space
1563 * for the lower level headers.
1565 if ((max_hdr + siz) < MINCLSIZE)
1566 info.mreq->m_data += max_hdr;
1567 tl = mtod(info.mreq, u_int32_t *);
1568 info.mreq->m_len = 6 * NFSX_UNSIGNED;
1569 info.bpos = ((caddr_t)tl) + info.mreq->m_len;
1570 *tl++ = txdr_unsigned(nd->nd_retxid);
1572 if (err == ERPCMISMATCH || (err & NFSERR_AUTHERR)) {
1573 *tl++ = rpc_msgdenied;
1574 if (err & NFSERR_AUTHERR) {
1575 *tl++ = rpc_autherr;
1576 *tl = txdr_unsigned(err & ~NFSERR_AUTHERR);
1577 info.mreq->m_len -= NFSX_UNSIGNED;
1578 info.bpos -= NFSX_UNSIGNED;
1580 *tl++ = rpc_mismatch;
1581 *tl++ = txdr_unsigned(RPC_VER2);
1582 *tl = txdr_unsigned(RPC_VER2);
1585 *tl++ = rpc_msgaccepted;
1588 * For Kerberos authentication, we must send the nickname
1589 * verifier back, otherwise just RPCAUTH_NULL.
1591 if (nd->nd_flag & ND_KERBFULL) {
1592 struct nfsuid *nuidp;
1593 struct timeval ktvin, ktvout;
1595 for (nuidp = NUIDHASH(slp, nd->nd_cr.cr_uid)->lh_first;
1596 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
1597 if (nuidp->nu_cr.cr_uid == nd->nd_cr.cr_uid &&
1598 (!nd->nd_nam2 || netaddr_match(NU_NETFAM(nuidp),
1599 &nuidp->nu_haddr, nd->nd_nam2)))
1604 txdr_unsigned(nuidp->nu_timestamp.tv_sec - 1);
1606 txdr_unsigned(nuidp->nu_timestamp.tv_usec);
1609 * Encrypt the timestamp in ecb mode using the
1619 *tl++ = rpc_auth_kerb;
1620 *tl++ = txdr_unsigned(3 * NFSX_UNSIGNED);
1621 *tl = ktvout.tv_sec;
1622 tl = nfsm_build(&info, 3 * NFSX_UNSIGNED);
1623 *tl++ = ktvout.tv_usec;
1624 *tl++ = txdr_unsigned(nuidp->nu_cr.cr_uid);
1635 *tl = txdr_unsigned(RPC_PROGUNAVAIL);
1638 *tl = txdr_unsigned(RPC_PROGMISMATCH);
1639 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
1640 *tl++ = txdr_unsigned(2);
1641 *tl = txdr_unsigned(3);
1644 *tl = txdr_unsigned(RPC_PROCUNAVAIL);
1647 *tl = txdr_unsigned(RPC_GARBAGE);
1651 if (err != NFSERR_RETVOID) {
1652 tl = nfsm_build(&info, NFSX_UNSIGNED);
1654 *tl = txdr_unsigned(nfsrv_errmap(nd, err));
1666 if (err != 0 && err != NFSERR_RETVOID)
1667 nfsstats.srvrpc_errs++;
1672 #endif /* NFS_NOSERVER */
1675 * Nfs timer routine.
1677 * Scan the nfsreq list and retranmit any requests that have timed out
1678 * To avoid retransmission attempts on STREAM sockets (in the future) make
1679 * sure to set the r_retry field to 0 (implies nm_retry == 0).
1681 * Requests with attached responses, terminated requests, and
1682 * locked requests are ignored. Locked requests will be picked up
1683 * in a later timer call.
1686 nfs_timer(void *arg /* never used */)
1688 struct nfsmount *nmp;
1690 #ifndef NFS_NOSERVER
1691 struct nfssvc_sock *slp;
1693 #endif /* NFS_NOSERVER */
1696 TAILQ_FOREACH(nmp, &nfs_mountq, nm_entry) {
1697 TAILQ_FOREACH(req, &nmp->nm_reqq, r_chain) {
1698 KKASSERT(nmp == req->r_nmp);
1701 if (req->r_flags & (R_SOFTTERM | R_LOCKED))
1703 req->r_flags |= R_LOCKED;
1704 if (nfs_sigintr(nmp, req, req->r_td)) {
1705 nfs_softterm(req, 1);
1709 req->r_flags &= ~R_LOCKED;
1710 if (req->r_flags & R_WANTED) {
1711 req->r_flags &= ~R_WANTED;
1716 #ifndef NFS_NOSERVER
1719 * Scan the write gathering queues for writes that need to be
1722 cur_usec = nfs_curusec();
1723 TAILQ_FOREACH(slp, &nfssvc_sockhead, ns_chain) {
1724 if (slp->ns_tq.lh_first && slp->ns_tq.lh_first->nd_time<=cur_usec)
1725 nfsrv_wakenfsd(slp, 1);
1727 #endif /* NFS_NOSERVER */
1729 callout_reset(&nfs_timer_handle, nfs_ticks, nfs_timer, NULL);
1734 nfs_timer_req(struct nfsreq *req)
1736 struct thread *td = &thread0; /* XXX for creds, will break if sleep */
1737 struct nfsmount *nmp = req->r_nmp;
1744 * rtt ticks and timeout calculation. Return if the timeout
1745 * has not been reached yet, unless the packet is flagged
1746 * for an immediate send.
1748 * The mean rtt doesn't help when we get random I/Os, we have
1749 * to multiply by fairly large numbers.
1751 if (req->r_rtt >= 0) {
1753 * Calculate the timeout to test against.
1756 if (nmp->nm_flag & NFSMNT_DUMBTIMR) {
1757 timeo = nmp->nm_timeo << NFS_RTT_SCALE_BITS;
1758 } else if (req->r_flags & R_TIMING) {
1759 timeo = NFS_SRTT(req) + NFS_SDRTT(req);
1761 timeo = nmp->nm_timeo << NFS_RTT_SCALE_BITS;
1763 timeo *= multt[req->r_procnum];
1764 /* timeo is still scaled by SCALE_BITS */
1766 #define NFSFS (NFS_RTT_SCALE * NFS_HZ)
1767 if (req->r_flags & R_TIMING) {
1768 static long last_time;
1769 if (nfs_showrtt && last_time != time_second) {
1770 kprintf("rpccmd %d NFS SRTT %d SDRTT %d "
1772 proct[req->r_procnum],
1773 NFS_SRTT(req), NFS_SDRTT(req),
1775 timeo % NFSFS * 1000 / NFSFS);
1776 last_time = time_second;
1782 * deal with nfs_timer jitter.
1784 timeo = (timeo >> NFS_RTT_SCALE_BITS) + 1;
1788 if (nmp->nm_timeouts > 0)
1789 timeo *= nfs_backoff[nmp->nm_timeouts - 1];
1790 if (timeo > NFS_MAXTIMEO)
1791 timeo = NFS_MAXTIMEO;
1792 if (req->r_rtt <= timeo) {
1793 if ((req->r_flags & R_NEEDSXMIT) == 0)
1795 } else if (nmp->nm_timeouts < 8) {
1801 * Check for server not responding
1803 if ((req->r_flags & R_TPRINTFMSG) == 0 &&
1804 req->r_rexmit > nmp->nm_deadthresh) {
1805 nfs_msg(req->r_td, nmp->nm_mountp->mnt_stat.f_mntfromname,
1807 req->r_flags |= R_TPRINTFMSG;
1809 if (req->r_rexmit >= req->r_retry) { /* too many */
1810 nfsstats.rpctimeouts++;
1811 nfs_softterm(req, 1);
1816 * Generally disable retransmission on reliable sockets,
1817 * unless the request is flagged for immediate send.
1819 if (nmp->nm_sotype != SOCK_DGRAM) {
1820 if (++req->r_rexmit > NFS_MAXREXMIT)
1821 req->r_rexmit = NFS_MAXREXMIT;
1822 if ((req->r_flags & R_NEEDSXMIT) == 0)
1827 * Stop here if we do not have a socket!
1829 if ((so = nmp->nm_so) == NULL)
1833 * If there is enough space and the window allows.. resend it.
1835 * r_rtt is left intact in case we get an answer after the
1836 * retry that was a reply to the original packet.
1838 if (ssb_space(&so->so_snd) >= req->r_mreq->m_pkthdr.len &&
1839 (req->r_flags & (R_SENT | R_NEEDSXMIT)) &&
1840 (m = m_copym(req->r_mreq, 0, M_COPYALL, MB_DONTWAIT))){
1841 if ((nmp->nm_flag & NFSMNT_NOCONN) == 0)
1842 error = so_pru_send(so, 0, m, NULL, NULL, td);
1844 error = so_pru_send(so, 0, m, nmp->nm_nam,
1847 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error))
1849 req->r_flags |= R_NEEDSXMIT;
1850 } else if (req->r_mrep == NULL) {
1852 * Iff first send, start timing
1853 * else turn timing off, backoff timer
1854 * and divide congestion window by 2.
1856 * It is possible for the so_pru_send() to
1857 * block and for us to race a reply so we
1858 * only do this if the reply field has not
1859 * been filled in. R_LOCKED will prevent
1860 * the request from being ripped out from under
1863 * Record the last resent procnum to aid us
1864 * in duplicate detection on receive.
1866 if ((req->r_flags & R_NEEDSXMIT) == 0) {
1869 if (++req->r_rexmit > NFS_MAXREXMIT)
1870 req->r_rexmit = NFS_MAXREXMIT;
1871 nmp->nm_maxasync_scaled >>= 1;
1872 if (nmp->nm_maxasync_scaled < NFS_MINASYNC_SCALED)
1873 nmp->nm_maxasync_scaled = NFS_MINASYNC_SCALED;
1874 nfsstats.rpcretries++;
1875 nmp->nm_lastreprocnum = req->r_procnum;
1877 req->r_flags |= R_SENT;
1878 req->r_flags &= ~R_NEEDSXMIT;
1885 * Mark all of an nfs mount's outstanding requests with R_SOFTTERM and
1886 * wait for all requests to complete. This is used by forced unmounts
1887 * to terminate any outstanding RPCs.
1889 * Locked requests cannot be canceled but will be marked for
1893 nfs_nmcancelreqs(struct nfsmount *nmp)
1899 TAILQ_FOREACH(req, &nmp->nm_reqq, r_chain) {
1900 if (req->r_mrep != NULL || (req->r_flags & R_SOFTTERM))
1902 nfs_softterm(req, 0);
1904 /* XXX the other two queues as well */
1907 for (i = 0; i < 30; i++) {
1909 TAILQ_FOREACH(req, &nmp->nm_reqq, r_chain) {
1910 if (nmp == req->r_nmp)
1916 tsleep(&lbolt, 0, "nfscancel", 0);
1922 * Soft-terminate a request, effectively marking it as failed.
1924 * Must be called from within a critical section.
1927 nfs_softterm(struct nfsreq *rep, int islocked)
1929 rep->r_flags |= R_SOFTTERM;
1930 nfs_hardterm(rep, islocked);
1934 * Hard-terminate a request, typically after getting a response.
1936 * The state machine can still decide to re-issue it later if necessary.
1938 * Must be called from within a critical section.
1941 nfs_hardterm(struct nfsreq *rep, int islocked)
1943 struct nfsmount *nmp = rep->r_nmp;
1946 * The nm_send count is decremented now to avoid deadlocks
1947 * when the process in soreceive() hasn't yet managed to send
1950 if (rep->r_flags & R_SENT) {
1951 rep->r_flags &= ~R_SENT;
1955 * If we locked the request or nobody else has locked the request,
1956 * and the request is async, we can move it to the reader thread's
1957 * queue now and fix up the state.
1959 * If we locked the request or nobody else has locked the request,
1960 * we can wake up anyone blocked waiting for a response on the
1963 if (islocked || (rep->r_flags & R_LOCKED) == 0) {
1964 if ((rep->r_flags & (R_ONREQQ | R_ASYNC)) ==
1965 (R_ONREQQ | R_ASYNC)) {
1966 rep->r_flags &= ~R_ONREQQ;
1967 TAILQ_REMOVE(&nmp->nm_reqq, rep, r_chain);
1969 TAILQ_INSERT_TAIL(&nmp->nm_reqrxq, rep, r_chain);
1970 KKASSERT(rep->r_info->state == NFSM_STATE_TRY ||
1971 rep->r_info->state == NFSM_STATE_WAITREPLY);
1972 rep->r_info->state = NFSM_STATE_PROCESSREPLY;
1973 nfssvc_iod_reader_wakeup(nmp);
1974 if (TAILQ_FIRST(&nmp->nm_bioq) &&
1975 nmp->nm_reqqlen <= nfs_maxasyncbio * 2 / 3) {
1976 nfssvc_iod_writer_wakeup(nmp);
1979 mtx_abort_ex_link(&nmp->nm_rxlock, &rep->r_link);
1984 * Test for a termination condition pending on the process.
1985 * This is used for NFSMNT_INT mounts.
1988 nfs_sigintr(struct nfsmount *nmp, struct nfsreq *rep, struct thread *td)
1994 if (rep && (rep->r_flags & R_SOFTTERM))
1996 /* Terminate all requests while attempting a forced unmount. */
1997 if (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF)
1999 if (!(nmp->nm_flag & NFSMNT_INT))
2001 /* td might be NULL YYY */
2002 if (td == NULL || (p = td->td_proc) == NULL)
2006 tmpset = lwp_sigpend(lp);
2007 SIGSETNAND(tmpset, lp->lwp_sigmask);
2008 SIGSETNAND(tmpset, p->p_sigignore);
2009 if (SIGNOTEMPTY(tmpset) && NFSINT_SIGMASK(tmpset))
2016 * Lock a socket against others.
2017 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
2018 * and also to avoid race conditions between the processes with nfs requests
2019 * in progress when a reconnect is necessary.
2022 nfs_sndlock(struct nfsmount *nmp, struct nfsreq *rep)
2024 mtx_t mtx = &nmp->nm_txlock;
2032 td = rep ? rep->r_td : NULL;
2033 if (nmp->nm_flag & NFSMNT_INT)
2036 while ((error = mtx_lock_ex_try(mtx)) != 0) {
2037 if (nfs_sigintr(nmp, rep, td)) {
2041 error = mtx_lock_ex(mtx, "nfsndlck", slpflag, slptimeo);
2044 if (slpflag == PCATCH) {
2049 /* Always fail if our request has been cancelled. */
2050 if (rep && (rep->r_flags & R_SOFTTERM)) {
2059 * Unlock the stream socket for others.
2062 nfs_sndunlock(struct nfsmount *nmp)
2064 mtx_unlock(&nmp->nm_txlock);
2068 * Lock the receiver side of the socket.
2073 nfs_rcvlock(struct nfsmount *nmp, struct nfsreq *rep)
2075 mtx_t mtx = &nmp->nm_rxlock;
2081 * Unconditionally check for completion in case another nfsiod
2082 * get the packet while the caller was blocked, before the caller
2083 * called us. Packet reception is handled by mainline code which
2084 * is protected by the BGL at the moment.
2086 * We do not strictly need the second check just before the
2087 * tsleep(), but it's good defensive programming.
2089 if (rep && rep->r_mrep != NULL)
2092 if (nmp->nm_flag & NFSMNT_INT)
2098 while ((error = mtx_lock_ex_try(mtx)) != 0) {
2099 if (nfs_sigintr(nmp, rep, (rep ? rep->r_td : NULL))) {
2103 if (rep && rep->r_mrep != NULL) {
2109 * NOTE: can return ENOLCK, but in that case rep->r_mrep
2110 * will already be set.
2113 error = mtx_lock_ex_link(mtx, &rep->r_link,
2117 error = mtx_lock_ex(mtx, "nfsrcvlk", slpflag, slptimeo);
2123 * If our reply was recieved while we were sleeping,
2124 * then just return without taking the lock to avoid a
2125 * situation where a single iod could 'capture' the
2128 if (rep && rep->r_mrep != NULL) {
2132 if (slpflag == PCATCH) {
2138 if (rep && rep->r_mrep != NULL) {
2147 * Unlock the stream socket for others.
2150 nfs_rcvunlock(struct nfsmount *nmp)
2152 mtx_unlock(&nmp->nm_rxlock);
2158 * Check for badly aligned mbuf data and realign by copying the unaligned
2159 * portion of the data into a new mbuf chain and freeing the portions
2160 * of the old chain that were replaced.
2162 * We cannot simply realign the data within the existing mbuf chain
2163 * because the underlying buffers may contain other rpc commands and
2164 * we cannot afford to overwrite them.
2166 * We would prefer to avoid this situation entirely. The situation does
2167 * not occur with NFS/UDP and is supposed to only occassionally occur
2168 * with TCP. Use vfs.nfs.realign_count and realign_test to check this.
2170 * NOTE! MB_DONTWAIT cannot be used here. The mbufs must be acquired
2171 * because the rpc request OR reply cannot be thrown away. TCP NFS
2172 * mounts do not retry their RPCs unless the TCP connection itself
2173 * is dropped so throwing away a RPC will basically cause the NFS
2174 * operation to lockup indefinitely.
2177 nfs_realign(struct mbuf **pm, int hsiz)
2180 struct mbuf *n = NULL;
2185 while ((m = *pm) != NULL) {
2186 if ((m->m_len & 0x3) || (mtod(m, intptr_t) & 0x3)) {
2187 n = m_getl(m->m_len, MB_WAIT, MT_DATA, 0, NULL);
2195 * If n is non-NULL, loop on m copying data, then replace the
2196 * portion of the chain that had to be realigned.
2199 ++nfs_realign_count;
2201 m_copyback(n, off, m->m_len, mtod(m, caddr_t));
2210 #ifndef NFS_NOSERVER
2213 * Parse an RPC request
2215 * - fill in the cred struct.
2218 nfs_getreq(struct nfsrv_descript *nd, struct nfsd *nfsd, int has_header)
2225 u_int32_t nfsvers, auth_type;
2227 int error = 0, ticklen;
2228 struct nfsuid *nuidp;
2229 struct timeval tvin, tvout;
2230 struct nfsm_info info;
2231 #if 0 /* until encrypted keys are implemented */
2232 NFSKERBKEYSCHED_T keys; /* stores key schedule */
2235 info.mrep = nd->nd_mrep;
2236 info.md = nd->nd_md;
2237 info.dpos = nd->nd_dpos;
2240 NULLOUT(tl = nfsm_dissect(&info, 10 * NFSX_UNSIGNED));
2241 nd->nd_retxid = fxdr_unsigned(u_int32_t, *tl++);
2242 if (*tl++ != rpc_call) {
2247 NULLOUT(tl = nfsm_dissect(&info, 8 * NFSX_UNSIGNED));
2251 if (*tl++ != rpc_vers) {
2252 nd->nd_repstat = ERPCMISMATCH;
2253 nd->nd_procnum = NFSPROC_NOOP;
2256 if (*tl != nfs_prog) {
2257 nd->nd_repstat = EPROGUNAVAIL;
2258 nd->nd_procnum = NFSPROC_NOOP;
2262 nfsvers = fxdr_unsigned(u_int32_t, *tl++);
2263 if (nfsvers < NFS_VER2 || nfsvers > NFS_VER3) {
2264 nd->nd_repstat = EPROGMISMATCH;
2265 nd->nd_procnum = NFSPROC_NOOP;
2268 if (nfsvers == NFS_VER3)
2269 nd->nd_flag = ND_NFSV3;
2270 nd->nd_procnum = fxdr_unsigned(u_int32_t, *tl++);
2271 if (nd->nd_procnum == NFSPROC_NULL)
2273 if (nd->nd_procnum >= NFS_NPROCS ||
2274 (nd->nd_procnum >= NQNFSPROC_GETLEASE) ||
2275 (!nd->nd_flag && nd->nd_procnum > NFSV2PROC_STATFS)) {
2276 nd->nd_repstat = EPROCUNAVAIL;
2277 nd->nd_procnum = NFSPROC_NOOP;
2280 if ((nd->nd_flag & ND_NFSV3) == 0)
2281 nd->nd_procnum = nfsv3_procid[nd->nd_procnum];
2283 len = fxdr_unsigned(int, *tl++);
2284 if (len < 0 || len > RPCAUTH_MAXSIZ) {
2289 nd->nd_flag &= ~ND_KERBAUTH;
2291 * Handle auth_unix or auth_kerb.
2293 if (auth_type == rpc_auth_unix) {
2294 len = fxdr_unsigned(int, *++tl);
2295 if (len < 0 || len > NFS_MAXNAMLEN) {
2299 ERROROUT(nfsm_adv(&info, nfsm_rndup(len)));
2300 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2301 bzero((caddr_t)&nd->nd_cr, sizeof (struct ucred));
2302 nd->nd_cr.cr_ref = 1;
2303 nd->nd_cr.cr_uid = fxdr_unsigned(uid_t, *tl++);
2304 nd->nd_cr.cr_ruid = nd->nd_cr.cr_svuid = nd->nd_cr.cr_uid;
2305 nd->nd_cr.cr_gid = fxdr_unsigned(gid_t, *tl++);
2306 nd->nd_cr.cr_rgid = nd->nd_cr.cr_svgid = nd->nd_cr.cr_gid;
2307 len = fxdr_unsigned(int, *tl);
2308 if (len < 0 || len > RPCAUTH_UNIXGIDS) {
2312 NULLOUT(tl = nfsm_dissect(&info, (len + 2) * NFSX_UNSIGNED));
2313 for (i = 1; i <= len; i++)
2315 nd->nd_cr.cr_groups[i] = fxdr_unsigned(gid_t, *tl++);
2318 nd->nd_cr.cr_ngroups = (len >= NGROUPS) ? NGROUPS : (len + 1);
2319 if (nd->nd_cr.cr_ngroups > 1)
2320 nfsrvw_sort(nd->nd_cr.cr_groups, nd->nd_cr.cr_ngroups);
2321 len = fxdr_unsigned(int, *++tl);
2322 if (len < 0 || len > RPCAUTH_MAXSIZ) {
2327 ERROROUT(nfsm_adv(&info, nfsm_rndup(len)));
2329 } else if (auth_type == rpc_auth_kerb) {
2330 switch (fxdr_unsigned(int, *tl++)) {
2331 case RPCAKN_FULLNAME:
2332 ticklen = fxdr_unsigned(int, *tl);
2333 *((u_int32_t *)nfsd->nfsd_authstr) = *tl;
2334 uio.uio_resid = nfsm_rndup(ticklen) + NFSX_UNSIGNED;
2335 nfsd->nfsd_authlen = uio.uio_resid + NFSX_UNSIGNED;
2336 if (uio.uio_resid > (len - 2 * NFSX_UNSIGNED)) {
2343 uio.uio_segflg = UIO_SYSSPACE;
2344 iov.iov_base = (caddr_t)&nfsd->nfsd_authstr[4];
2345 iov.iov_len = RPCAUTH_MAXSIZ - 4;
2346 ERROROUT(nfsm_mtouio(&info, &uio, uio.uio_resid));
2347 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2348 if (*tl++ != rpc_auth_kerb ||
2349 fxdr_unsigned(int, *tl) != 4 * NFSX_UNSIGNED) {
2350 kprintf("Bad kerb verifier\n");
2351 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
2352 nd->nd_procnum = NFSPROC_NOOP;
2355 NULLOUT(cp = nfsm_dissect(&info, 4 * NFSX_UNSIGNED));
2356 tl = (u_int32_t *)cp;
2357 if (fxdr_unsigned(int, *tl) != RPCAKN_FULLNAME) {
2358 kprintf("Not fullname kerb verifier\n");
2359 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
2360 nd->nd_procnum = NFSPROC_NOOP;
2363 cp += NFSX_UNSIGNED;
2364 bcopy(cp, nfsd->nfsd_verfstr, 3 * NFSX_UNSIGNED);
2365 nfsd->nfsd_verflen = 3 * NFSX_UNSIGNED;
2366 nd->nd_flag |= ND_KERBFULL;
2367 nfsd->nfsd_flag |= NFSD_NEEDAUTH;
2369 case RPCAKN_NICKNAME:
2370 if (len != 2 * NFSX_UNSIGNED) {
2371 kprintf("Kerb nickname short\n");
2372 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADCRED);
2373 nd->nd_procnum = NFSPROC_NOOP;
2376 nickuid = fxdr_unsigned(uid_t, *tl);
2377 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2378 if (*tl++ != rpc_auth_kerb ||
2379 fxdr_unsigned(int, *tl) != 3 * NFSX_UNSIGNED) {
2380 kprintf("Kerb nick verifier bad\n");
2381 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
2382 nd->nd_procnum = NFSPROC_NOOP;
2385 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2386 tvin.tv_sec = *tl++;
2389 for (nuidp = NUIDHASH(nfsd->nfsd_slp,nickuid)->lh_first;
2390 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
2391 if (nuidp->nu_cr.cr_uid == nickuid &&
2393 netaddr_match(NU_NETFAM(nuidp),
2394 &nuidp->nu_haddr, nd->nd_nam2)))
2399 (NFSERR_AUTHERR|AUTH_REJECTCRED);
2400 nd->nd_procnum = NFSPROC_NOOP;
2405 * Now, decrypt the timestamp using the session key
2415 tvout.tv_sec = fxdr_unsigned(long, tvout.tv_sec);
2416 tvout.tv_usec = fxdr_unsigned(long, tvout.tv_usec);
2417 if (nuidp->nu_expire < time_second ||
2418 nuidp->nu_timestamp.tv_sec > tvout.tv_sec ||
2419 (nuidp->nu_timestamp.tv_sec == tvout.tv_sec &&
2420 nuidp->nu_timestamp.tv_usec > tvout.tv_usec)) {
2421 nuidp->nu_expire = 0;
2423 (NFSERR_AUTHERR|AUTH_REJECTVERF);
2424 nd->nd_procnum = NFSPROC_NOOP;
2427 nfsrv_setcred(&nuidp->nu_cr, &nd->nd_cr);
2428 nd->nd_flag |= ND_KERBNICK;
2431 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_REJECTCRED);
2432 nd->nd_procnum = NFSPROC_NOOP;
2436 nd->nd_md = info.md;
2437 nd->nd_dpos = info.dpos;
2446 * Send a message to the originating process's terminal. The thread and/or
2447 * process may be NULL. YYY the thread should not be NULL but there may
2448 * still be some uio_td's that are still being passed as NULL through to
2452 nfs_msg(struct thread *td, char *server, char *msg)
2456 if (td && td->td_proc)
2457 tpr = tprintf_open(td->td_proc);
2460 tprintf(tpr, "nfs server %s: %s\n", server, msg);
2465 #ifndef NFS_NOSERVER
2467 * Socket upcall routine for the nfsd sockets.
2468 * The caddr_t arg is a pointer to the "struct nfssvc_sock".
2469 * Essentially do as much as possible non-blocking, else punt and it will
2470 * be called with MB_WAIT from an nfsd.
2473 nfsrv_rcv(struct socket *so, void *arg, int waitflag)
2475 struct nfssvc_sock *slp = (struct nfssvc_sock *)arg;
2477 struct sockaddr *nam;
2480 int nparallel_wakeup = 0;
2482 if ((slp->ns_flag & SLP_VALID) == 0)
2486 * Do not allow an infinite number of completed RPC records to build
2487 * up before we stop reading data from the socket. Otherwise we could
2488 * end up holding onto an unreasonable number of mbufs for requests
2489 * waiting for service.
2491 * This should give pretty good feedback to the TCP
2492 * layer and prevents a memory crunch for other protocols.
2494 * Note that the same service socket can be dispatched to several
2495 * nfs servers simultaniously.
2497 * the tcp protocol callback calls us with MB_DONTWAIT.
2498 * nfsd calls us with MB_WAIT (typically).
2500 if (waitflag == MB_DONTWAIT && slp->ns_numrec >= nfsd_waiting / 2 + 1) {
2501 slp->ns_flag |= SLP_NEEDQ;
2506 * Handle protocol specifics to parse an RPC request. We always
2507 * pull from the socket using non-blocking I/O.
2509 if (so->so_type == SOCK_STREAM) {
2511 * The data has to be read in an orderly fashion from a TCP
2512 * stream, unlike a UDP socket. It is possible for soreceive
2513 * and/or nfsrv_getstream() to block, so make sure only one
2514 * entity is messing around with the TCP stream at any given
2515 * moment. The receive sockbuf's lock in soreceive is not
2518 * Note that this procedure can be called from any number of
2519 * NFS severs *OR* can be upcalled directly from a TCP
2522 if (slp->ns_flag & SLP_GETSTREAM) {
2523 slp->ns_flag |= SLP_NEEDQ;
2526 slp->ns_flag |= SLP_GETSTREAM;
2529 * Do soreceive(). Pull out as much data as possible without
2532 sbinit(&sio, 1000000000);
2533 flags = MSG_DONTWAIT;
2534 error = so_pru_soreceive(so, &nam, NULL, &sio, NULL, &flags);
2535 if (error || sio.sb_mb == NULL) {
2536 if (error == EWOULDBLOCK)
2537 slp->ns_flag |= SLP_NEEDQ;
2539 slp->ns_flag |= SLP_DISCONN;
2540 slp->ns_flag &= ~SLP_GETSTREAM;
2544 if (slp->ns_rawend) {
2545 slp->ns_rawend->m_next = m;
2546 slp->ns_cc += sio.sb_cc;
2549 slp->ns_cc = sio.sb_cc;
2556 * Now try and parse as many record(s) as we can out of the
2559 error = nfsrv_getstream(slp, waitflag, &nparallel_wakeup);
2562 slp->ns_flag |= SLP_DISCONN;
2564 slp->ns_flag |= SLP_NEEDQ;
2566 slp->ns_flag &= ~SLP_GETSTREAM;
2569 * For UDP soreceive typically pulls just one packet, loop
2570 * to get the whole batch.
2573 sbinit(&sio, 1000000000);
2574 flags = MSG_DONTWAIT;
2575 error = so_pru_soreceive(so, &nam, NULL, &sio,
2578 struct nfsrv_rec *rec;
2579 int mf = (waitflag & MB_DONTWAIT) ?
2580 M_NOWAIT : M_WAITOK;
2581 rec = kmalloc(sizeof(struct nfsrv_rec),
2585 FREE(nam, M_SONAME);
2589 nfs_realign(&sio.sb_mb, 10 * NFSX_UNSIGNED);
2590 rec->nr_address = nam;
2591 rec->nr_packet = sio.sb_mb;
2592 STAILQ_INSERT_TAIL(&slp->ns_rec, rec, nr_link);
2597 if ((so->so_proto->pr_flags & PR_CONNREQUIRED)
2598 && error != EWOULDBLOCK) {
2599 slp->ns_flag |= SLP_DISCONN;
2603 } while (sio.sb_mb);
2607 * If we were upcalled from the tcp protocol layer and we have
2608 * fully parsed records ready to go, or there is new data pending,
2609 * or something went wrong, try to wake up an nfsd thread to deal
2613 if (waitflag == MB_DONTWAIT && (slp->ns_numrec > 0
2614 || (slp->ns_flag & (SLP_NEEDQ | SLP_DISCONN)))) {
2615 nfsrv_wakenfsd(slp, nparallel_wakeup);
2620 * Try and extract an RPC request from the mbuf data list received on a
2621 * stream socket. The "waitflag" argument indicates whether or not it
2625 nfsrv_getstream(struct nfssvc_sock *slp, int waitflag, int *countp)
2627 struct mbuf *m, **mpp;
2630 struct mbuf *om, *m2, *recm;
2634 if (slp->ns_reclen == 0) {
2635 if (slp->ns_cc < NFSX_UNSIGNED)
2638 if (m->m_len >= NFSX_UNSIGNED) {
2639 bcopy(mtod(m, caddr_t), (caddr_t)&recmark, NFSX_UNSIGNED);
2640 m->m_data += NFSX_UNSIGNED;
2641 m->m_len -= NFSX_UNSIGNED;
2643 cp1 = (caddr_t)&recmark;
2644 cp2 = mtod(m, caddr_t);
2645 while (cp1 < ((caddr_t)&recmark) + NFSX_UNSIGNED) {
2646 while (m->m_len == 0) {
2648 cp2 = mtod(m, caddr_t);
2655 slp->ns_cc -= NFSX_UNSIGNED;
2656 recmark = ntohl(recmark);
2657 slp->ns_reclen = recmark & ~0x80000000;
2658 if (recmark & 0x80000000)
2659 slp->ns_flag |= SLP_LASTFRAG;
2661 slp->ns_flag &= ~SLP_LASTFRAG;
2662 if (slp->ns_reclen > NFS_MAXPACKET || slp->ns_reclen <= 0) {
2663 log(LOG_ERR, "%s (%d) from nfs client\n",
2664 "impossible packet length",
2671 * Now get the record part.
2673 * Note that slp->ns_reclen may be 0. Linux sometimes
2674 * generates 0-length RPCs
2677 if (slp->ns_cc == slp->ns_reclen) {
2679 slp->ns_raw = slp->ns_rawend = NULL;
2680 slp->ns_cc = slp->ns_reclen = 0;
2681 } else if (slp->ns_cc > slp->ns_reclen) {
2686 while (len < slp->ns_reclen) {
2687 if ((len + m->m_len) > slp->ns_reclen) {
2688 m2 = m_copym(m, 0, slp->ns_reclen - len,
2696 m->m_data += slp->ns_reclen - len;
2697 m->m_len -= slp->ns_reclen - len;
2698 len = slp->ns_reclen;
2700 return (EWOULDBLOCK);
2702 } else if ((len + m->m_len) == slp->ns_reclen) {
2722 * Accumulate the fragments into a record.
2724 mpp = &slp->ns_frag;
2726 mpp = &((*mpp)->m_next);
2728 if (slp->ns_flag & SLP_LASTFRAG) {
2729 struct nfsrv_rec *rec;
2730 int mf = (waitflag & MB_DONTWAIT) ? M_NOWAIT : M_WAITOK;
2731 rec = kmalloc(sizeof(struct nfsrv_rec), M_NFSRVDESC, mf);
2733 m_freem(slp->ns_frag);
2735 nfs_realign(&slp->ns_frag, 10 * NFSX_UNSIGNED);
2736 rec->nr_address = NULL;
2737 rec->nr_packet = slp->ns_frag;
2738 STAILQ_INSERT_TAIL(&slp->ns_rec, rec, nr_link);
2742 slp->ns_frag = NULL;
2748 * Parse an RPC header.
2751 nfsrv_dorec(struct nfssvc_sock *slp, struct nfsd *nfsd,
2752 struct nfsrv_descript **ndp)
2754 struct nfsrv_rec *rec;
2756 struct sockaddr *nam;
2757 struct nfsrv_descript *nd;
2761 if ((slp->ns_flag & SLP_VALID) == 0 || !STAILQ_FIRST(&slp->ns_rec))
2763 rec = STAILQ_FIRST(&slp->ns_rec);
2764 STAILQ_REMOVE_HEAD(&slp->ns_rec, nr_link);
2765 KKASSERT(slp->ns_numrec > 0);
2767 nam = rec->nr_address;
2769 kfree(rec, M_NFSRVDESC);
2770 MALLOC(nd, struct nfsrv_descript *, sizeof (struct nfsrv_descript),
2771 M_NFSRVDESC, M_WAITOK);
2772 nd->nd_md = nd->nd_mrep = m;
2774 nd->nd_dpos = mtod(m, caddr_t);
2775 error = nfs_getreq(nd, nfsd, TRUE);
2778 FREE(nam, M_SONAME);
2780 kfree((caddr_t)nd, M_NFSRVDESC);
2789 * Try to assign service sockets to nfsd threads based on the number
2790 * of new rpc requests that have been queued on the service socket.
2792 * If no nfsd's are available or additonal requests are pending, set the
2793 * NFSD_CHECKSLP flag so that one of the running nfsds will go look for
2794 * the work in the nfssvc_sock list when it is finished processing its
2795 * current work. This flag is only cleared when an nfsd can not find
2796 * any new work to perform.
2799 nfsrv_wakenfsd(struct nfssvc_sock *slp, int nparallel)
2803 if ((slp->ns_flag & SLP_VALID) == 0)
2807 TAILQ_FOREACH(nd, &nfsd_head, nfsd_chain) {
2808 if (nd->nfsd_flag & NFSD_WAITING) {
2809 nd->nfsd_flag &= ~NFSD_WAITING;
2811 panic("nfsd wakeup");
2814 wakeup((caddr_t)nd);
2815 if (--nparallel == 0)
2820 slp->ns_flag |= SLP_DOREC;
2821 nfsd_head_flag |= NFSD_CHECKSLP;
2824 #endif /* NFS_NOSERVER */