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, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0,
107 static int nfs_backoff[8] = { 2, 3, 5, 8, 13, 21, 34, 55 };
108 static int nfs_realign_test;
109 static int nfs_realign_count;
110 static int nfs_bufpackets = 4;
111 static int nfs_showrtt;
112 static int nfs_showrexmit;
114 SYSCTL_DECL(_vfs_nfs);
116 SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_test, CTLFLAG_RW, &nfs_realign_test, 0, "");
117 SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_count, CTLFLAG_RW, &nfs_realign_count, 0, "");
118 SYSCTL_INT(_vfs_nfs, OID_AUTO, bufpackets, CTLFLAG_RW, &nfs_bufpackets, 0, "");
119 SYSCTL_INT(_vfs_nfs, OID_AUTO, showrtt, CTLFLAG_RW, &nfs_showrtt, 0, "");
120 SYSCTL_INT(_vfs_nfs, OID_AUTO, showrexmit, CTLFLAG_RW, &nfs_showrexmit, 0, "");
122 static int nfs_request_setup(nfsm_info_t info);
123 static int nfs_request_auth(struct nfsreq *rep);
124 static int nfs_request_try(struct nfsreq *rep);
125 static int nfs_request_waitreply(struct nfsreq *rep);
126 static int nfs_request_processreply(nfsm_info_t info, int);
129 struct nfsrtt nfsrtt;
130 struct callout nfs_timer_handle;
132 static int nfs_msg (struct thread *,char *,char *);
133 static int nfs_rcvlock (struct nfsmount *nmp, struct nfsreq *myreq);
134 static void nfs_rcvunlock (struct nfsmount *nmp);
135 static void nfs_realign (struct mbuf **pm, int hsiz);
136 static int nfs_receive (struct nfsmount *nmp, struct nfsreq *rep,
137 struct sockaddr **aname, struct mbuf **mp);
138 static void nfs_softterm (struct nfsreq *rep, int islocked);
139 static void nfs_hardterm (struct nfsreq *rep, int islocked);
140 static int nfs_reconnect (struct nfsmount *nmp, struct nfsreq *rep);
142 static int nfsrv_getstream (struct nfssvc_sock *, int, int *);
143 static void nfs_timer_req(struct nfsreq *req);
145 int (*nfsrv3_procs[NFS_NPROCS]) (struct nfsrv_descript *nd,
146 struct nfssvc_sock *slp,
148 struct mbuf **mreqp) = {
176 #endif /* NFS_NOSERVER */
179 * Initialize sockets and congestion for a new NFS connection.
180 * We do not free the sockaddr if error.
183 nfs_connect(struct nfsmount *nmp, struct nfsreq *rep)
186 int error, rcvreserve, sndreserve;
188 struct sockaddr *saddr;
189 struct sockaddr_in *sin;
190 struct thread *td = &thread0; /* only used for socreate and sobind */
194 error = socreate(saddr->sa_family, &nmp->nm_so, nmp->nm_sotype,
195 nmp->nm_soproto, td);
199 nmp->nm_soflags = so->so_proto->pr_flags;
202 * Some servers require that the client port be a reserved port number.
204 if (saddr->sa_family == AF_INET && (nmp->nm_flag & NFSMNT_RESVPORT)) {
207 struct sockaddr_in ssin;
209 bzero(&sopt, sizeof sopt);
210 ip = IP_PORTRANGE_LOW;
211 sopt.sopt_level = IPPROTO_IP;
212 sopt.sopt_name = IP_PORTRANGE;
213 sopt.sopt_val = (void *)&ip;
214 sopt.sopt_valsize = sizeof(ip);
216 error = sosetopt(so, &sopt);
219 bzero(&ssin, sizeof ssin);
221 sin->sin_len = sizeof (struct sockaddr_in);
222 sin->sin_family = AF_INET;
223 sin->sin_addr.s_addr = INADDR_ANY;
224 sin->sin_port = htons(0);
225 error = sobind(so, (struct sockaddr *)sin, td);
228 bzero(&sopt, sizeof sopt);
229 ip = IP_PORTRANGE_DEFAULT;
230 sopt.sopt_level = IPPROTO_IP;
231 sopt.sopt_name = IP_PORTRANGE;
232 sopt.sopt_val = (void *)&ip;
233 sopt.sopt_valsize = sizeof(ip);
235 error = sosetopt(so, &sopt);
241 * Protocols that do not require connections may be optionally left
242 * unconnected for servers that reply from a port other than NFS_PORT.
244 if (nmp->nm_flag & NFSMNT_NOCONN) {
245 if (nmp->nm_soflags & PR_CONNREQUIRED) {
250 error = soconnect(so, nmp->nm_nam, td);
255 * Wait for the connection to complete. Cribbed from the
256 * connect system call but with the wait timing out so
257 * that interruptible mounts don't hang here for a long time.
260 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
261 (void) tsleep((caddr_t)&so->so_timeo, 0,
263 if ((so->so_state & SS_ISCONNECTING) &&
264 so->so_error == 0 && rep &&
265 (error = nfs_sigintr(nmp, rep, rep->r_td)) != 0){
266 so->so_state &= ~SS_ISCONNECTING;
272 error = so->so_error;
279 so->so_rcv.ssb_timeo = (5 * hz);
280 so->so_snd.ssb_timeo = (5 * hz);
283 * Get buffer reservation size from sysctl, but impose reasonable
286 pktscale = nfs_bufpackets;
292 if (nmp->nm_sotype == SOCK_DGRAM) {
293 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * pktscale;
294 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
295 NFS_MAXPKTHDR) * pktscale;
296 } else if (nmp->nm_sotype == SOCK_SEQPACKET) {
297 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * pktscale;
298 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
299 NFS_MAXPKTHDR) * pktscale;
301 if (nmp->nm_sotype != SOCK_STREAM)
302 panic("nfscon sotype");
303 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
307 bzero(&sopt, sizeof sopt);
308 sopt.sopt_level = SOL_SOCKET;
309 sopt.sopt_name = SO_KEEPALIVE;
310 sopt.sopt_val = &val;
311 sopt.sopt_valsize = sizeof val;
315 if (so->so_proto->pr_protocol == IPPROTO_TCP) {
319 bzero(&sopt, sizeof sopt);
320 sopt.sopt_level = IPPROTO_TCP;
321 sopt.sopt_name = TCP_NODELAY;
322 sopt.sopt_val = &val;
323 sopt.sopt_valsize = sizeof val;
327 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR +
328 sizeof (u_int32_t)) * pktscale;
329 rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR +
330 sizeof (u_int32_t)) * pktscale;
332 error = soreserve(so, sndreserve, rcvreserve,
333 &td->td_proc->p_rlimit[RLIMIT_SBSIZE]);
336 so->so_rcv.ssb_flags |= SSB_NOINTR;
337 so->so_snd.ssb_flags |= SSB_NOINTR;
339 /* Initialize other non-zero congestion variables */
340 nmp->nm_srtt[0] = nmp->nm_srtt[1] = nmp->nm_srtt[2] =
341 nmp->nm_srtt[3] = (NFS_TIMEO << NFS_RTT_SCALE_BITS);
342 nmp->nm_sdrtt[0] = nmp->nm_sdrtt[1] = nmp->nm_sdrtt[2] =
343 nmp->nm_sdrtt[3] = 0;
344 nmp->nm_maxasync_scaled = NFS_MINASYNC_SCALED;
345 nmp->nm_timeouts = 0;
355 * Called when a connection is broken on a reliable protocol.
356 * - clean up the old socket
357 * - nfs_connect() again
358 * - set R_NEEDSXMIT for all outstanding requests on mount point
359 * If this fails the mount point is DEAD!
360 * nb: Must be called with the nfs_sndlock() set on the mount point.
363 nfs_reconnect(struct nfsmount *nmp, struct nfsreq *rep)
369 while ((error = nfs_connect(nmp, rep)) != 0) {
370 if (error == EINTR || error == ERESTART)
372 (void) tsleep((caddr_t)&lbolt, 0, "nfscon", 0);
376 * Loop through outstanding request list and fix up all requests
380 TAILQ_FOREACH(req, &nmp->nm_reqq, r_chain) {
381 KKASSERT(req->r_nmp == nmp);
382 req->r_flags |= R_NEEDSXMIT;
389 * NFS disconnect. Clean up and unlink.
392 nfs_disconnect(struct nfsmount *nmp)
399 soshutdown(so, SHUT_RDWR);
400 soclose(so, FNONBLOCK);
405 nfs_safedisconnect(struct nfsmount *nmp)
407 nfs_rcvlock(nmp, NULL);
413 * This is the nfs send routine. For connection based socket types, it
414 * must be called with an nfs_sndlock() on the socket.
415 * "rep == NULL" indicates that it has been called from a server.
416 * For the client side:
417 * - return EINTR if the RPC is terminated, 0 otherwise
418 * - set R_NEEDSXMIT if the send fails for any reason
419 * - do any cleanup required by recoverable socket errors (?)
420 * For the server side:
421 * - return EINTR or ERESTART if interrupted by a signal
422 * - return EPIPE if a connection is lost for connection based sockets (TCP...)
423 * - do any cleanup required by recoverable socket errors (?)
426 nfs_send(struct socket *so, struct sockaddr *nam, struct mbuf *top,
429 struct sockaddr *sendnam;
430 int error, soflags, flags;
433 if (rep->r_flags & R_SOFTTERM) {
437 if ((so = rep->r_nmp->nm_so) == NULL) {
438 rep->r_flags |= R_NEEDSXMIT;
442 rep->r_flags &= ~R_NEEDSXMIT;
443 soflags = rep->r_nmp->nm_soflags;
445 soflags = so->so_proto->pr_flags;
447 if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED))
451 if (so->so_type == SOCK_SEQPACKET)
456 error = so_pru_sosend(so, sendnam, NULL, top, NULL, flags,
459 * ENOBUFS for dgram sockets is transient and non fatal.
460 * No need to log, and no need to break a soft mount.
462 if (error == ENOBUFS && so->so_type == SOCK_DGRAM) {
465 * do backoff retransmit on client
468 rep->r_flags |= R_NEEDSXMIT;
473 log(LOG_INFO, "nfs send error %d for server %s\n",error,
474 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
476 * Deal with errors for the client side.
478 if (rep->r_flags & R_SOFTTERM)
481 rep->r_flags |= R_NEEDSXMIT;
483 log(LOG_INFO, "nfsd send error %d\n", error);
487 * Handle any recoverable (soft) socket errors here. (?)
489 if (error != EINTR && error != ERESTART &&
490 error != EWOULDBLOCK && error != EPIPE)
497 * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all
498 * done by soreceive(), but for SOCK_STREAM we must deal with the Record
499 * Mark and consolidate the data into a new mbuf list.
500 * nb: Sometimes TCP passes the data up to soreceive() in long lists of
502 * For SOCK_STREAM we must be very careful to read an entire record once
503 * we have read any of it, even if the system call has been interrupted.
506 nfs_receive(struct nfsmount *nmp, struct nfsreq *rep,
507 struct sockaddr **aname, struct mbuf **mp)
514 struct mbuf *control;
516 struct sockaddr **getnam;
517 int error, sotype, rcvflg;
518 struct thread *td = curthread; /* XXX */
521 * Set up arguments for soreceive()
525 sotype = nmp->nm_sotype;
528 * For reliable protocols, lock against other senders/receivers
529 * in case a reconnect is necessary.
530 * For SOCK_STREAM, first get the Record Mark to find out how much
531 * more there is to get.
532 * We must lock the socket against other receivers
533 * until we have an entire rpc request/reply.
535 if (sotype != SOCK_DGRAM) {
536 error = nfs_sndlock(nmp, rep);
541 * Check for fatal errors and resending request.
544 * Ugh: If a reconnect attempt just happened, nm_so
545 * would have changed. NULL indicates a failed
546 * attempt that has essentially shut down this
549 if (rep && (rep->r_mrep || (rep->r_flags & R_SOFTTERM))) {
555 error = nfs_reconnect(nmp, rep);
562 while (rep && (rep->r_flags & R_NEEDSXMIT)) {
563 m = m_copym(rep->r_mreq, 0, M_COPYALL, MB_WAIT);
564 nfsstats.rpcretries++;
565 error = nfs_send(so, rep->r_nmp->nm_nam, m, rep);
567 if (error == EINTR || error == ERESTART ||
568 (error = nfs_reconnect(nmp, rep)) != 0) {
576 if (sotype == SOCK_STREAM) {
578 * Get the length marker from the stream
580 aio.iov_base = (caddr_t)&len;
581 aio.iov_len = sizeof(u_int32_t);
584 auio.uio_segflg = UIO_SYSSPACE;
585 auio.uio_rw = UIO_READ;
587 auio.uio_resid = sizeof(u_int32_t);
590 rcvflg = MSG_WAITALL;
591 error = so_pru_soreceive(so, NULL, &auio, NULL,
593 if (error == EWOULDBLOCK && rep) {
594 if (rep->r_flags & R_SOFTTERM)
597 } while (error == EWOULDBLOCK);
599 if (error == 0 && auio.uio_resid > 0) {
601 * Only log short packets if not EOF
603 if (auio.uio_resid != sizeof(u_int32_t))
605 "short receive (%d/%d) from nfs server %s\n",
606 (int)(sizeof(u_int32_t) - auio.uio_resid),
607 (int)sizeof(u_int32_t),
608 nmp->nm_mountp->mnt_stat.f_mntfromname);
613 len = ntohl(len) & ~0x80000000;
615 * This is SERIOUS! We are out of sync with the sender
616 * and forcing a disconnect/reconnect is all I can do.
618 if (len > NFS_MAXPACKET) {
619 log(LOG_ERR, "%s (%d) from nfs server %s\n",
620 "impossible packet length",
622 nmp->nm_mountp->mnt_stat.f_mntfromname);
628 * Get the rest of the packet as an mbuf chain
632 rcvflg = MSG_WAITALL;
633 error = so_pru_soreceive(so, NULL, NULL, &sio,
635 } while (error == EWOULDBLOCK || error == EINTR ||
637 if (error == 0 && sio.sb_cc != len) {
640 "short receive (%d/%d) from nfs server %s\n",
641 len - auio.uio_resid, len,
642 nmp->nm_mountp->mnt_stat.f_mntfromname);
648 * Non-stream, so get the whole packet by not
649 * specifying MSG_WAITALL and by specifying a large
652 * We have no use for control msg., but must grab them
653 * and then throw them away so we know what is going
656 sbinit(&sio, 100000000);
659 error = so_pru_soreceive(so, NULL, NULL, &sio,
663 if (error == EWOULDBLOCK && rep) {
664 if (rep->r_flags & R_SOFTTERM) {
669 } while (error == EWOULDBLOCK ||
670 (error == 0 && sio.sb_mb == NULL && control));
671 if ((rcvflg & MSG_EOR) == 0)
673 if (error == 0 && sio.sb_mb == NULL)
679 if (error && error != EINTR && error != ERESTART) {
682 if (error != EPIPE) {
684 "receive error %d from nfs server %s\n",
686 nmp->nm_mountp->mnt_stat.f_mntfromname);
688 error = nfs_sndlock(nmp, rep);
690 error = nfs_reconnect(nmp, rep);
698 if ((so = nmp->nm_so) == NULL)
700 if (so->so_state & SS_ISCONNECTED)
704 sbinit(&sio, 100000000);
707 error = so_pru_soreceive(so, getnam, NULL, &sio,
709 if (error == EWOULDBLOCK && rep &&
710 (rep->r_flags & R_SOFTTERM)) {
714 } while (error == EWOULDBLOCK);
720 * A shutdown may result in no error and no mbuf.
723 if (*mp == NULL && error == 0)
732 * Search for any mbufs that are not a multiple of 4 bytes long
733 * or with m_data not longword aligned.
734 * These could cause pointer alignment problems, so copy them to
735 * well aligned mbufs.
737 nfs_realign(mp, 5 * NFSX_UNSIGNED);
742 * Implement receipt of reply on a socket.
744 * We must search through the list of received datagrams matching them
745 * with outstanding requests using the xid, until ours is found.
747 * If myrep is NULL we process packets on the socket until
748 * interrupted or until nm_reqrxq is non-empty.
752 nfs_reply(struct nfsmount *nmp, struct nfsreq *myrep)
755 struct sockaddr *nam;
759 struct nfsm_info info;
762 * Loop around until we get our own reply
766 * Lock against other receivers so that I don't get stuck in
767 * sbwait() after someone else has received my reply for me.
768 * Also necessary for connection based protocols to avoid
769 * race conditions during a reconnect.
771 * If nfs_rcvlock() returns EALREADY, that means that
772 * the reply has already been recieved by another
773 * process and we can return immediately. In this
774 * case, the lock is not taken to avoid races with
779 error = nfs_rcvlock(nmp, myrep);
780 if (error == EALREADY)
786 * If myrep is NULL we are the receiver helper thread.
787 * Stop waiting for incoming replies if there are
788 * messages sitting on reqrxq that we need to process,
789 * or if a shutdown request is pending.
791 if (myrep == NULL && (TAILQ_FIRST(&nmp->nm_reqrxq) ||
792 nmp->nm_rxstate > NFSSVC_PENDING)) {
798 * Get the next Rpc reply off the socket
800 * We cannot release the receive lock until we've
801 * filled in rep->r_mrep, otherwise a waiting
802 * thread may deadlock in soreceive with no incoming
805 error = nfs_receive(nmp, myrep, &nam, &info.mrep);
808 * Ignore routing errors on connectionless protocols??
811 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
812 if (nmp->nm_so == NULL)
814 nmp->nm_so->so_error = 0;
823 * Get the xid and check that it is an rpc reply
826 info.dpos = mtod(info.md, caddr_t);
827 NULLOUT(tl = nfsm_dissect(&info, 2*NFSX_UNSIGNED));
829 if (*tl != rpc_reply) {
830 nfsstats.rpcinvalid++;
839 * Loop through the request list to match up the reply
840 * Iff no match, just drop the datagram. On match, set
841 * r_mrep atomically to prevent the timer from messing
842 * around with the request after we have exited the critical
846 TAILQ_FOREACH(rep, &nmp->nm_reqq, r_chain) {
847 if (rep->r_mrep == NULL && rxid == rep->r_xid)
852 * Fill in the rest of the reply if we found a match.
856 rep->r_dpos = info.dpos;
860 rt = &nfsrtt.rttl[nfsrtt.pos];
861 rt->proc = rep->r_procnum;
864 rt->cwnd = nmp->nm_maxasync_scaled;
865 rt->srtt = nmp->nm_srtt[proct[rep->r_procnum] - 1];
866 rt->sdrtt = nmp->nm_sdrtt[proct[rep->r_procnum] - 1];
867 rt->fsid = nmp->nm_mountp->mnt_stat.f_fsid;
868 getmicrotime(&rt->tstamp);
869 if (rep->r_flags & R_TIMING)
870 rt->rtt = rep->r_rtt;
873 nfsrtt.pos = (nfsrtt.pos + 1) % NFSRTTLOGSIZ;
877 * New congestion control is based only on async
880 if (nmp->nm_maxasync_scaled < NFS_MAXASYNC_SCALED)
881 ++nmp->nm_maxasync_scaled;
882 if (rep->r_flags & R_SENT) {
883 rep->r_flags &= ~R_SENT;
886 * Update rtt using a gain of 0.125 on the mean
887 * and a gain of 0.25 on the deviation.
889 * NOTE SRTT/SDRTT are only good if R_TIMING is set.
891 if (rep->r_flags & R_TIMING) {
893 * Since the timer resolution of
894 * NFS_HZ is so course, it can often
895 * result in r_rtt == 0. Since
896 * r_rtt == N means that the actual
897 * rtt is between N+dt and N+2-dt ticks,
903 #define NFSRSB NFS_RTT_SCALE_BITS
904 n = ((NFS_SRTT(rep) * 7) +
905 (rep->r_rtt << NFSRSB)) >> 3;
906 d = n - NFS_SRTT(rep);
910 * Don't let the jitter calculation decay
911 * too quickly, but we want a fast rampup.
916 if (d < NFS_SDRTT(rep))
917 n = ((NFS_SDRTT(rep) * 15) + d) >> 4;
919 n = ((NFS_SDRTT(rep) * 3) + d) >> 2;
923 nmp->nm_timeouts = 0;
924 rep->r_mrep = info.mrep;
925 nfs_hardterm(rep, 0);
931 * If not matched to a request, drop it.
932 * If it's mine, get out.
935 nfsstats.rpcunexpected++;
938 } else if (rep == myrep) {
939 if (rep->r_mrep == NULL)
940 panic("nfsreply nil");
947 * Run the request state machine until the target state is reached
948 * or a fatal error occurs. The target state is not run. Specifying
949 * a target of NFSM_STATE_DONE runs the state machine until the rpc
952 * EINPROGRESS is returned for all states other then the DONE state,
953 * indicating that the rpc is still in progress.
956 nfs_request(struct nfsm_info *info, nfsm_state_t bstate, nfsm_state_t estate)
960 while (info->state >= bstate && info->state < estate) {
961 switch(info->state) {
962 case NFSM_STATE_SETUP:
964 * Setup the nfsreq. Any error which occurs during
965 * this state is fatal.
967 info->error = nfs_request_setup(info);
969 info->state = NFSM_STATE_DONE;
970 return (info->error);
973 req->r_mrp = &info->mrep;
974 req->r_mdp = &info->md;
975 req->r_dposp = &info->dpos;
976 info->state = NFSM_STATE_AUTH;
979 case NFSM_STATE_AUTH:
981 * Authenticate the nfsreq. Any error which occurs
982 * during this state is fatal.
984 info->error = nfs_request_auth(info->req);
986 info->state = NFSM_STATE_DONE;
987 return (info->error);
989 info->state = NFSM_STATE_TRY;
994 * Transmit or retransmit attempt. An error in this
995 * state is ignored and we always move on to the
998 * This can trivially race the receiver if the
999 * request is asynchronous. nfs_request_try()
1000 * will thus set the state for us and we
1001 * must also return immediately if we are
1002 * running an async state machine, because
1003 * info can become invalid due to races after
1006 if (info->req->r_flags & R_ASYNC) {
1007 nfs_request_try(info->req);
1008 if (estate == NFSM_STATE_WAITREPLY)
1009 return (EINPROGRESS);
1011 nfs_request_try(info->req);
1012 info->state = NFSM_STATE_WAITREPLY;
1015 case NFSM_STATE_WAITREPLY:
1017 * Wait for a reply or timeout and move on to the
1018 * next state. The error returned by this state
1019 * is passed to the processing code in the next
1022 info->error = nfs_request_waitreply(info->req);
1023 info->state = NFSM_STATE_PROCESSREPLY;
1025 case NFSM_STATE_PROCESSREPLY:
1027 * Process the reply or timeout. Errors which occur
1028 * in this state may cause the state machine to
1029 * go back to an earlier state, and are fatal
1032 info->error = nfs_request_processreply(info,
1034 switch(info->error) {
1036 info->state = NFSM_STATE_AUTH;
1039 info->state = NFSM_STATE_TRY;
1043 * Operation complete, with or without an
1044 * error. We are done.
1047 info->state = NFSM_STATE_DONE;
1048 return (info->error);
1051 case NFSM_STATE_DONE:
1053 * Shouldn't be reached
1055 return (info->error);
1061 * If we are done return the error code (if any).
1062 * Otherwise return EINPROGRESS.
1064 if (info->state == NFSM_STATE_DONE)
1065 return (info->error);
1066 return (EINPROGRESS);
1070 * nfs_request - goes something like this
1071 * - fill in request struct
1072 * - links it into list
1073 * - calls nfs_send() for first transmit
1074 * - calls nfs_receive() to get reply
1075 * - break down rpc header and return with nfs reply pointed to
1077 * nb: always frees up mreq mbuf list
1080 nfs_request_setup(nfsm_info_t info)
1083 struct nfsmount *nmp;
1088 * Reject requests while attempting a forced unmount.
1090 if (info->vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF) {
1091 m_freem(info->mreq);
1095 nmp = VFSTONFS(info->vp->v_mount);
1096 req = kmalloc(sizeof(struct nfsreq), M_NFSREQ, M_WAITOK);
1098 req->r_vp = info->vp;
1099 req->r_td = info->td;
1100 req->r_procnum = info->procnum;
1102 req->r_cred = info->cred;
1110 req->r_mrest = info->mreq;
1111 req->r_mrest_len = i;
1114 * The presence of a non-NULL r_info in req indicates
1115 * async completion via our helper threads. See the receiver
1120 req->r_flags = R_ASYNC;
1130 nfs_request_auth(struct nfsreq *rep)
1132 struct nfsmount *nmp = rep->r_nmp;
1134 char nickv[RPCX_NICKVERF];
1135 int error = 0, auth_len, auth_type;
1138 char *auth_str, *verf_str;
1142 rep->r_failed_auth = 0;
1145 * Get the RPC header with authorization.
1147 verf_str = auth_str = NULL;
1148 if (nmp->nm_flag & NFSMNT_KERB) {
1150 verf_len = sizeof (nickv);
1151 auth_type = RPCAUTH_KERB4;
1152 bzero((caddr_t)rep->r_key, sizeof(rep->r_key));
1153 if (rep->r_failed_auth ||
1154 nfs_getnickauth(nmp, cred, &auth_str, &auth_len,
1155 verf_str, verf_len)) {
1156 error = nfs_getauth(nmp, rep, cred, &auth_str,
1157 &auth_len, verf_str, &verf_len, rep->r_key);
1159 m_freem(rep->r_mrest);
1160 rep->r_mrest = NULL;
1161 kfree((caddr_t)rep, M_NFSREQ);
1166 auth_type = RPCAUTH_UNIX;
1167 if (cred->cr_ngroups < 1)
1168 panic("nfsreq nogrps");
1169 auth_len = ((((cred->cr_ngroups - 1) > nmp->nm_numgrps) ?
1170 nmp->nm_numgrps : (cred->cr_ngroups - 1)) << 2) +
1173 m = nfsm_rpchead(cred, nmp->nm_flag, rep->r_procnum, auth_type,
1174 auth_len, auth_str, verf_len, verf_str,
1175 rep->r_mrest, rep->r_mrest_len, &rep->r_mheadend, &xid);
1176 rep->r_mrest = NULL;
1178 kfree(auth_str, M_TEMP);
1181 * For stream protocols, insert a Sun RPC Record Mark.
1183 if (nmp->nm_sotype == SOCK_STREAM) {
1184 M_PREPEND(m, NFSX_UNSIGNED, MB_WAIT);
1186 kfree(rep, M_NFSREQ);
1189 *mtod(m, u_int32_t *) = htonl(0x80000000 |
1190 (m->m_pkthdr.len - NFSX_UNSIGNED));
1198 nfs_request_try(struct nfsreq *rep)
1200 struct nfsmount *nmp = rep->r_nmp;
1205 * Request is not on any queue, only the owner has access to it
1206 * so it should not be locked by anyone atm.
1208 * Interlock to prevent races. While locked the only remote
1209 * action possible is for r_mrep to be set (once we enqueue it).
1211 if (rep->r_flags == 0xdeadc0de) {
1213 panic("flags nbad\n");
1215 KKASSERT((rep->r_flags & (R_LOCKED | R_ONREQQ)) == 0);
1216 if (nmp->nm_flag & NFSMNT_SOFT)
1217 rep->r_retry = nmp->nm_retry;
1219 rep->r_retry = NFS_MAXREXMIT + 1; /* past clip limit */
1220 rep->r_rtt = rep->r_rexmit = 0;
1221 if (proct[rep->r_procnum] > 0)
1222 rep->r_flags |= R_TIMING | R_LOCKED;
1224 rep->r_flags |= R_LOCKED;
1228 * Do the client side RPC.
1230 nfsstats.rpcrequests++;
1233 * Chain request into list of outstanding requests. Be sure
1234 * to put it LAST so timer finds oldest requests first. Note
1235 * that our control of R_LOCKED prevents the request from
1236 * getting ripped out from under us or transmitted by the
1239 * For requests with info structures we must atomically set the
1240 * info's state because the structure could become invalid upon
1241 * return due to races (i.e., if async)
1244 mtx_link_init(&rep->r_link);
1245 TAILQ_INSERT_TAIL(&nmp->nm_reqq, rep, r_chain);
1246 rep->r_flags |= R_ONREQQ;
1248 if (rep->r_flags & R_ASYNC)
1249 rep->r_info->state = NFSM_STATE_WAITREPLY;
1255 * Send if we can. Congestion control is not handled here any more
1256 * becausing trying to defer the initial send based on the nfs_timer
1257 * requires having a very fast nfs_timer, which is silly.
1260 if (nmp->nm_soflags & PR_CONNREQUIRED)
1261 error = nfs_sndlock(nmp, rep);
1263 m2 = m_copym(rep->r_mreq, 0, M_COPYALL, MB_WAIT);
1264 error = nfs_send(nmp->nm_so, nmp->nm_nam, m2, rep);
1265 if (nmp->nm_soflags & PR_CONNREQUIRED)
1267 rep->r_flags &= ~R_NEEDSXMIT;
1268 if ((rep->r_flags & R_SENT) == 0) {
1269 rep->r_flags |= R_SENT;
1272 rep->r_flags |= R_NEEDSXMIT;
1275 rep->r_flags |= R_NEEDSXMIT;
1282 * Release the lock. The only remote action that may have occurred
1283 * would have been the setting of rep->r_mrep. If this occured
1284 * and the request was async we have to move it to the reader
1285 * thread's queue for action.
1287 * For async requests also make sure the reader is woken up so
1288 * it gets on the socket to read responses.
1291 if (rep->r_flags & R_ASYNC) {
1293 nfs_hardterm(rep, 1);
1294 rep->r_flags &= ~R_LOCKED;
1295 nfssvc_iod_reader_wakeup(nmp);
1297 rep->r_flags &= ~R_LOCKED;
1299 if (rep->r_flags & R_WANTED) {
1300 rep->r_flags &= ~R_WANTED;
1308 * This code is only called for synchronous requests. Completed synchronous
1309 * requests are left on reqq and we remove them before moving on to the
1313 nfs_request_waitreply(struct nfsreq *rep)
1315 struct nfsmount *nmp = rep->r_nmp;
1318 KKASSERT((rep->r_flags & R_ASYNC) == 0);
1321 * Wait until the request is finished.
1323 error = nfs_reply(nmp, rep);
1326 * RPC done, unlink the request, but don't rip it out from under
1327 * the callout timer.
1329 * Once unlinked no other receiver or the timer will have
1330 * visibility, so we do not have to set R_LOCKED.
1333 while (rep->r_flags & R_LOCKED) {
1334 rep->r_flags |= R_WANTED;
1335 tsleep(rep, 0, "nfstrac", 0);
1337 KKASSERT(rep->r_flags & R_ONREQQ);
1338 TAILQ_REMOVE(&nmp->nm_reqq, rep, r_chain);
1339 rep->r_flags &= ~R_ONREQQ;
1344 * Decrement the outstanding request count.
1346 if (rep->r_flags & R_SENT) {
1347 rep->r_flags &= ~R_SENT;
1353 * Process reply with error returned from nfs_requet_waitreply().
1355 * Returns EAGAIN if it wants us to loop up to nfs_request_try() again.
1356 * Returns ENEEDAUTH if it wants us to loop up to nfs_request_auth() again.
1359 nfs_request_processreply(nfsm_info_t info, int error)
1361 struct nfsreq *req = info->req;
1362 struct nfsmount *nmp = req->r_nmp;
1368 * If there was a successful reply and a tprintf msg.
1369 * tprintf a response.
1371 if (error == 0 && (req->r_flags & R_TPRINTFMSG)) {
1372 nfs_msg(req->r_td, nmp->nm_mountp->mnt_stat.f_mntfromname,
1375 info->mrep = req->r_mrep;
1376 info->md = req->r_md;
1377 info->dpos = req->r_dpos;
1379 m_freem(req->r_mreq);
1381 kfree(req, M_NFSREQ);
1387 * break down the rpc header and check if ok
1389 NULLOUT(tl = nfsm_dissect(info, 3 * NFSX_UNSIGNED));
1390 if (*tl++ == rpc_msgdenied) {
1391 if (*tl == rpc_mismatch) {
1393 } else if ((nmp->nm_flag & NFSMNT_KERB) &&
1394 *tl++ == rpc_autherr) {
1395 if (req->r_failed_auth == 0) {
1396 req->r_failed_auth++;
1397 req->r_mheadend->m_next = NULL;
1398 m_freem(info->mrep);
1400 m_freem(req->r_mreq);
1408 m_freem(info->mrep);
1410 m_freem(req->r_mreq);
1412 kfree(req, M_NFSREQ);
1418 * Grab any Kerberos verifier, otherwise just throw it away.
1420 verf_type = fxdr_unsigned(int, *tl++);
1421 i = fxdr_unsigned(int32_t, *tl);
1422 if ((nmp->nm_flag & NFSMNT_KERB) && verf_type == RPCAUTH_KERB4) {
1423 error = nfs_savenickauth(nmp, req->r_cred, i, req->r_key,
1424 &info->md, &info->dpos, info->mrep);
1428 ERROROUT(nfsm_adv(info, nfsm_rndup(i)));
1430 NULLOUT(tl = nfsm_dissect(info, NFSX_UNSIGNED));
1433 NULLOUT(tl = nfsm_dissect(info, NFSX_UNSIGNED));
1435 error = fxdr_unsigned(int, *tl);
1438 * Does anyone even implement this? Just impose
1441 if ((nmp->nm_flag & NFSMNT_NFSV3) &&
1442 error == NFSERR_TRYLATER) {
1443 m_freem(info->mrep);
1447 tsleep((caddr_t)&lbolt, 0, "nqnfstry", 0);
1448 return (EAGAIN); /* goto tryagain */
1452 * If the File Handle was stale, invalidate the
1453 * lookup cache, just in case.
1455 * To avoid namecache<->vnode deadlocks we must
1456 * release the vnode lock if we hold it.
1458 if (error == ESTALE) {
1459 struct vnode *vp = req->r_vp;
1462 ltype = lockstatus(&vp->v_lock, curthread);
1463 if (ltype == LK_EXCLUSIVE || ltype == LK_SHARED)
1464 lockmgr(&vp->v_lock, LK_RELEASE);
1465 cache_inval_vp(vp, CINV_CHILDREN);
1466 if (ltype == LK_EXCLUSIVE || ltype == LK_SHARED)
1467 lockmgr(&vp->v_lock, ltype);
1469 if (nmp->nm_flag & NFSMNT_NFSV3) {
1470 KKASSERT(*req->r_mrp == info->mrep);
1471 KKASSERT(*req->r_mdp == info->md);
1472 KKASSERT(*req->r_dposp == info->dpos);
1473 error |= NFSERR_RETERR;
1475 m_freem(info->mrep);
1478 m_freem(req->r_mreq);
1480 kfree(req, M_NFSREQ);
1485 KKASSERT(*req->r_mrp == info->mrep);
1486 KKASSERT(*req->r_mdp == info->md);
1487 KKASSERT(*req->r_dposp == info->dpos);
1488 m_freem(req->r_mreq);
1490 FREE(req, M_NFSREQ);
1493 m_freem(info->mrep);
1495 error = EPROTONOSUPPORT;
1497 m_freem(req->r_mreq);
1499 kfree(req, M_NFSREQ);
1504 #ifndef NFS_NOSERVER
1506 * Generate the rpc reply header
1507 * siz arg. is used to decide if adding a cluster is worthwhile
1510 nfs_rephead(int siz, struct nfsrv_descript *nd, struct nfssvc_sock *slp,
1511 int err, struct mbuf **mrq, struct mbuf **mbp, caddr_t *bposp)
1514 struct nfsm_info info;
1516 siz += RPC_REPLYSIZ;
1517 info.mb = m_getl(max_hdr + siz, MB_WAIT, MT_DATA, M_PKTHDR, NULL);
1518 info.mreq = info.mb;
1519 info.mreq->m_pkthdr.len = 0;
1521 * If this is not a cluster, try and leave leading space
1522 * for the lower level headers.
1524 if ((max_hdr + siz) < MINCLSIZE)
1525 info.mreq->m_data += max_hdr;
1526 tl = mtod(info.mreq, u_int32_t *);
1527 info.mreq->m_len = 6 * NFSX_UNSIGNED;
1528 info.bpos = ((caddr_t)tl) + info.mreq->m_len;
1529 *tl++ = txdr_unsigned(nd->nd_retxid);
1531 if (err == ERPCMISMATCH || (err & NFSERR_AUTHERR)) {
1532 *tl++ = rpc_msgdenied;
1533 if (err & NFSERR_AUTHERR) {
1534 *tl++ = rpc_autherr;
1535 *tl = txdr_unsigned(err & ~NFSERR_AUTHERR);
1536 info.mreq->m_len -= NFSX_UNSIGNED;
1537 info.bpos -= NFSX_UNSIGNED;
1539 *tl++ = rpc_mismatch;
1540 *tl++ = txdr_unsigned(RPC_VER2);
1541 *tl = txdr_unsigned(RPC_VER2);
1544 *tl++ = rpc_msgaccepted;
1547 * For Kerberos authentication, we must send the nickname
1548 * verifier back, otherwise just RPCAUTH_NULL.
1550 if (nd->nd_flag & ND_KERBFULL) {
1551 struct nfsuid *nuidp;
1552 struct timeval ktvin, ktvout;
1554 for (nuidp = NUIDHASH(slp, nd->nd_cr.cr_uid)->lh_first;
1555 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
1556 if (nuidp->nu_cr.cr_uid == nd->nd_cr.cr_uid &&
1557 (!nd->nd_nam2 || netaddr_match(NU_NETFAM(nuidp),
1558 &nuidp->nu_haddr, nd->nd_nam2)))
1563 txdr_unsigned(nuidp->nu_timestamp.tv_sec - 1);
1565 txdr_unsigned(nuidp->nu_timestamp.tv_usec);
1568 * Encrypt the timestamp in ecb mode using the
1575 *tl++ = rpc_auth_kerb;
1576 *tl++ = txdr_unsigned(3 * NFSX_UNSIGNED);
1577 *tl = ktvout.tv_sec;
1578 tl = nfsm_build(&info, 3 * NFSX_UNSIGNED);
1579 *tl++ = ktvout.tv_usec;
1580 *tl++ = txdr_unsigned(nuidp->nu_cr.cr_uid);
1591 *tl = txdr_unsigned(RPC_PROGUNAVAIL);
1594 *tl = txdr_unsigned(RPC_PROGMISMATCH);
1595 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
1596 *tl++ = txdr_unsigned(2);
1597 *tl = txdr_unsigned(3);
1600 *tl = txdr_unsigned(RPC_PROCUNAVAIL);
1603 *tl = txdr_unsigned(RPC_GARBAGE);
1607 if (err != NFSERR_RETVOID) {
1608 tl = nfsm_build(&info, NFSX_UNSIGNED);
1610 *tl = txdr_unsigned(nfsrv_errmap(nd, err));
1622 if (err != 0 && err != NFSERR_RETVOID)
1623 nfsstats.srvrpc_errs++;
1628 #endif /* NFS_NOSERVER */
1631 * Nfs timer routine.
1633 * Scan the nfsreq list and retranmit any requests that have timed out
1634 * To avoid retransmission attempts on STREAM sockets (in the future) make
1635 * sure to set the r_retry field to 0 (implies nm_retry == 0).
1637 * Requests with attached responses, terminated requests, and
1638 * locked requests are ignored. Locked requests will be picked up
1639 * in a later timer call.
1642 nfs_timer(void *arg /* never used */)
1644 struct nfsmount *nmp;
1646 #ifndef NFS_NOSERVER
1647 struct nfssvc_sock *slp;
1649 #endif /* NFS_NOSERVER */
1652 TAILQ_FOREACH(nmp, &nfs_mountq, nm_entry) {
1653 TAILQ_FOREACH(req, &nmp->nm_reqq, r_chain) {
1654 KKASSERT(nmp == req->r_nmp);
1657 if (req->r_flags & (R_SOFTTERM | R_LOCKED))
1659 req->r_flags |= R_LOCKED;
1660 if (nfs_sigintr(nmp, req, req->r_td)) {
1661 nfs_softterm(req, 1);
1665 req->r_flags &= ~R_LOCKED;
1666 if (req->r_flags & R_WANTED) {
1667 req->r_flags &= ~R_WANTED;
1672 #ifndef NFS_NOSERVER
1675 * Scan the write gathering queues for writes that need to be
1678 cur_usec = nfs_curusec();
1679 TAILQ_FOREACH(slp, &nfssvc_sockhead, ns_chain) {
1680 if (slp->ns_tq.lh_first && slp->ns_tq.lh_first->nd_time<=cur_usec)
1681 nfsrv_wakenfsd(slp, 1);
1683 #endif /* NFS_NOSERVER */
1685 callout_reset(&nfs_timer_handle, nfs_ticks, nfs_timer, NULL);
1690 nfs_timer_req(struct nfsreq *req)
1692 struct thread *td = &thread0; /* XXX for creds, will break if sleep */
1693 struct nfsmount *nmp = req->r_nmp;
1700 * rtt ticks and timeout calculation. Return if the timeout
1701 * has not been reached yet, unless the packet is flagged
1702 * for an immediate send.
1704 * The mean rtt doesn't help when we get random I/Os, we have
1705 * to multiply by fairly large numbers.
1707 if (req->r_rtt >= 0) {
1709 if (nmp->nm_flag & NFSMNT_DUMBTIMR) {
1710 timeo = nmp->nm_timeo << NFS_RTT_SCALE_BITS;
1711 } else if (req->r_flags & R_TIMING) {
1712 timeo = NFS_SRTT(req) + NFS_SDRTT(req);
1714 timeo = nmp->nm_timeo << NFS_RTT_SCALE_BITS;
1716 /* timeo is still scaled by SCALE_BITS */
1718 #define NFSFS (NFS_RTT_SCALE * NFS_HZ)
1719 if (req->r_flags & R_TIMING) {
1720 static long last_time;
1721 if (nfs_showrtt && last_time != time_second) {
1722 kprintf("rpccmd %d NFS SRTT %d SDRTT %d "
1724 proct[req->r_procnum],
1725 NFS_SRTT(req), NFS_SDRTT(req),
1727 timeo % NFSFS * 1000 / NFSFS);
1728 last_time = time_second;
1734 * deal with nfs_timer jitter.
1736 timeo = (timeo >> NFS_RTT_SCALE_BITS) + 1;
1740 if (nmp->nm_timeouts > 0)
1741 timeo *= nfs_backoff[nmp->nm_timeouts - 1];
1742 if (timeo > NFS_MAXTIMEO)
1743 timeo = NFS_MAXTIMEO;
1744 if (req->r_rtt <= timeo) {
1745 if ((req->r_flags & R_NEEDSXMIT) == 0)
1747 } else if (nmp->nm_timeouts < 8) {
1753 * Check for server not responding
1755 if ((req->r_flags & R_TPRINTFMSG) == 0 &&
1756 req->r_rexmit > nmp->nm_deadthresh) {
1757 nfs_msg(req->r_td, nmp->nm_mountp->mnt_stat.f_mntfromname,
1759 req->r_flags |= R_TPRINTFMSG;
1761 if (req->r_rexmit >= req->r_retry) { /* too many */
1762 nfsstats.rpctimeouts++;
1763 nfs_softterm(req, 1);
1768 * Generally disable retransmission on reliable sockets,
1769 * unless the request is flagged for immediate send.
1771 if (nmp->nm_sotype != SOCK_DGRAM) {
1772 if (++req->r_rexmit > NFS_MAXREXMIT)
1773 req->r_rexmit = NFS_MAXREXMIT;
1774 if ((req->r_flags & R_NEEDSXMIT) == 0)
1779 * Stop here if we do not have a socket!
1781 if ((so = nmp->nm_so) == NULL)
1785 * If there is enough space and the window allows.. resend it.
1787 * Set r_rtt to -1 in case we fail to send it now.
1790 if (ssb_space(&so->so_snd) >= req->r_mreq->m_pkthdr.len &&
1791 (req->r_flags & (R_SENT | R_NEEDSXMIT)) &&
1792 (m = m_copym(req->r_mreq, 0, M_COPYALL, MB_DONTWAIT))){
1793 req->r_flags &= ~R_NEEDSXMIT;
1794 if ((nmp->nm_flag & NFSMNT_NOCONN) == 0)
1795 error = so_pru_send(so, 0, m, NULL, NULL, td);
1797 error = so_pru_send(so, 0, m, nmp->nm_nam,
1800 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error))
1802 req->r_flags |= R_NEEDSXMIT;
1803 } else if (req->r_mrep == NULL) {
1805 * Iff first send, start timing
1806 * else turn timing off, backoff timer
1807 * and divide congestion window by 2.
1809 * It is possible for the so_pru_send() to
1810 * block and for us to race a reply so we
1811 * only do this if the reply field has not
1812 * been filled in. R_LOCKED will prevent
1813 * the request from being ripped out from under
1816 if (req->r_flags & R_SENT) {
1819 req->r_flags &= ~R_TIMING;
1820 if (++req->r_rexmit > NFS_MAXREXMIT)
1821 req->r_rexmit = NFS_MAXREXMIT;
1822 nmp->nm_maxasync_scaled >>= 1;
1823 if (nmp->nm_maxasync_scaled < NFS_MINASYNC_SCALED)
1824 nmp->nm_maxasync_scaled = NFS_MINASYNC_SCALED;
1825 nfsstats.rpcretries++;
1827 req->r_flags |= R_SENT;
1835 * Mark all of an nfs mount's outstanding requests with R_SOFTTERM and
1836 * wait for all requests to complete. This is used by forced unmounts
1837 * to terminate any outstanding RPCs.
1839 * Locked requests cannot be canceled but will be marked for
1843 nfs_nmcancelreqs(struct nfsmount *nmp)
1849 TAILQ_FOREACH(req, &nmp->nm_reqq, r_chain) {
1850 if (req->r_mrep != NULL || (req->r_flags & R_SOFTTERM))
1852 nfs_softterm(req, 0);
1854 /* XXX the other two queues as well */
1857 for (i = 0; i < 30; i++) {
1859 TAILQ_FOREACH(req, &nmp->nm_reqq, r_chain) {
1860 if (nmp == req->r_nmp)
1866 tsleep(&lbolt, 0, "nfscancel", 0);
1872 * Soft-terminate a request, effectively marking it as failed.
1874 * Must be called from within a critical section.
1877 nfs_softterm(struct nfsreq *rep, int islocked)
1879 rep->r_flags |= R_SOFTTERM;
1880 nfs_hardterm(rep, islocked);
1884 * Hard-terminate a request, typically after getting a response.
1886 * The state machine can still decide to re-issue it later if necessary.
1888 * Must be called from within a critical section.
1891 nfs_hardterm(struct nfsreq *rep, int islocked)
1893 struct nfsmount *nmp = rep->r_nmp;
1896 * The nm_send count is decremented now to avoid deadlocks
1897 * when the process in soreceive() hasn't yet managed to send
1900 if (rep->r_flags & R_SENT) {
1901 rep->r_flags &= ~R_SENT;
1905 * If we locked the request or nobody else has locked the request,
1906 * and the request is async, we can move it to the reader thread's
1907 * queue now and fix up the state.
1909 * If we locked the request or nobody else has locked the request,
1910 * we can wake up anyone blocked waiting for a response on the
1913 if (islocked || (rep->r_flags & R_LOCKED) == 0) {
1914 if ((rep->r_flags & (R_ONREQQ | R_ASYNC)) ==
1915 (R_ONREQQ | R_ASYNC)) {
1916 rep->r_flags &= ~R_ONREQQ;
1917 TAILQ_REMOVE(&nmp->nm_reqq, rep, r_chain);
1919 TAILQ_INSERT_TAIL(&nmp->nm_reqrxq, rep, r_chain);
1920 KKASSERT(rep->r_info->state == NFSM_STATE_TRY ||
1921 rep->r_info->state == NFSM_STATE_WAITREPLY);
1922 rep->r_info->state = NFSM_STATE_PROCESSREPLY;
1923 nfssvc_iod_reader_wakeup(nmp);
1925 mtx_abort_ex_link(&nmp->nm_rxlock, &rep->r_link);
1930 * Test for a termination condition pending on the process.
1931 * This is used for NFSMNT_INT mounts.
1934 nfs_sigintr(struct nfsmount *nmp, struct nfsreq *rep, struct thread *td)
1940 if (rep && (rep->r_flags & R_SOFTTERM))
1942 /* Terminate all requests while attempting a forced unmount. */
1943 if (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF)
1945 if (!(nmp->nm_flag & NFSMNT_INT))
1947 /* td might be NULL YYY */
1948 if (td == NULL || (p = td->td_proc) == NULL)
1952 tmpset = lwp_sigpend(lp);
1953 SIGSETNAND(tmpset, lp->lwp_sigmask);
1954 SIGSETNAND(tmpset, p->p_sigignore);
1955 if (SIGNOTEMPTY(tmpset) && NFSINT_SIGMASK(tmpset))
1962 * Lock a socket against others.
1963 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
1964 * and also to avoid race conditions between the processes with nfs requests
1965 * in progress when a reconnect is necessary.
1968 nfs_sndlock(struct nfsmount *nmp, struct nfsreq *rep)
1970 mtx_t mtx = &nmp->nm_txlock;
1978 td = rep ? rep->r_td : NULL;
1979 if (nmp->nm_flag & NFSMNT_INT)
1982 while ((error = mtx_lock_ex_try(mtx)) != 0) {
1983 if (nfs_sigintr(nmp, rep, td)) {
1987 error = mtx_lock_ex(mtx, "nfsndlck", slpflag, slptimeo);
1990 if (slpflag == PCATCH) {
1995 /* Always fail if our request has been cancelled. */
1996 if (rep && (rep->r_flags & R_SOFTTERM)) {
2005 * Unlock the stream socket for others.
2008 nfs_sndunlock(struct nfsmount *nmp)
2010 mtx_unlock(&nmp->nm_txlock);
2014 * Lock the receiver side of the socket.
2019 nfs_rcvlock(struct nfsmount *nmp, struct nfsreq *rep)
2021 mtx_t mtx = &nmp->nm_rxlock;
2027 * Unconditionally check for completion in case another nfsiod
2028 * get the packet while the caller was blocked, before the caller
2029 * called us. Packet reception is handled by mainline code which
2030 * is protected by the BGL at the moment.
2032 * We do not strictly need the second check just before the
2033 * tsleep(), but it's good defensive programming.
2035 if (rep && rep->r_mrep != NULL)
2038 if (nmp->nm_flag & NFSMNT_INT)
2044 while ((error = mtx_lock_ex_try(mtx)) != 0) {
2045 if (nfs_sigintr(nmp, rep, (rep ? rep->r_td : NULL))) {
2049 if (rep && rep->r_mrep != NULL) {
2055 * NOTE: can return ENOLCK, but in that case rep->r_mrep
2056 * will already be set.
2059 error = mtx_lock_ex_link(mtx, &rep->r_link,
2063 error = mtx_lock_ex(mtx, "nfsrcvlk", slpflag, slptimeo);
2069 * If our reply was recieved while we were sleeping,
2070 * then just return without taking the lock to avoid a
2071 * situation where a single iod could 'capture' the
2074 if (rep && rep->r_mrep != NULL) {
2078 if (slpflag == PCATCH) {
2084 if (rep && rep->r_mrep != NULL) {
2093 * Unlock the stream socket for others.
2096 nfs_rcvunlock(struct nfsmount *nmp)
2098 mtx_unlock(&nmp->nm_rxlock);
2104 * Check for badly aligned mbuf data and realign by copying the unaligned
2105 * portion of the data into a new mbuf chain and freeing the portions
2106 * of the old chain that were replaced.
2108 * We cannot simply realign the data within the existing mbuf chain
2109 * because the underlying buffers may contain other rpc commands and
2110 * we cannot afford to overwrite them.
2112 * We would prefer to avoid this situation entirely. The situation does
2113 * not occur with NFS/UDP and is supposed to only occassionally occur
2114 * with TCP. Use vfs.nfs.realign_count and realign_test to check this.
2117 nfs_realign(struct mbuf **pm, int hsiz)
2120 struct mbuf *n = NULL;
2125 while ((m = *pm) != NULL) {
2126 if ((m->m_len & 0x3) || (mtod(m, intptr_t) & 0x3)) {
2127 n = m_getl(m->m_len, MB_WAIT, MT_DATA, 0, NULL);
2135 * If n is non-NULL, loop on m copying data, then replace the
2136 * portion of the chain that had to be realigned.
2139 ++nfs_realign_count;
2141 m_copyback(n, off, m->m_len, mtod(m, caddr_t));
2150 #ifndef NFS_NOSERVER
2153 * Parse an RPC request
2155 * - fill in the cred struct.
2158 nfs_getreq(struct nfsrv_descript *nd, struct nfsd *nfsd, int has_header)
2165 u_int32_t nfsvers, auth_type;
2167 int error = 0, ticklen;
2168 struct nfsuid *nuidp;
2169 struct timeval tvin, tvout;
2170 struct nfsm_info info;
2171 #if 0 /* until encrypted keys are implemented */
2172 NFSKERBKEYSCHED_T keys; /* stores key schedule */
2175 info.mrep = nd->nd_mrep;
2176 info.md = nd->nd_md;
2177 info.dpos = nd->nd_dpos;
2180 NULLOUT(tl = nfsm_dissect(&info, 10 * NFSX_UNSIGNED));
2181 nd->nd_retxid = fxdr_unsigned(u_int32_t, *tl++);
2182 if (*tl++ != rpc_call) {
2187 NULLOUT(tl = nfsm_dissect(&info, 8 * NFSX_UNSIGNED));
2191 if (*tl++ != rpc_vers) {
2192 nd->nd_repstat = ERPCMISMATCH;
2193 nd->nd_procnum = NFSPROC_NOOP;
2196 if (*tl != nfs_prog) {
2197 nd->nd_repstat = EPROGUNAVAIL;
2198 nd->nd_procnum = NFSPROC_NOOP;
2202 nfsvers = fxdr_unsigned(u_int32_t, *tl++);
2203 if (nfsvers < NFS_VER2 || nfsvers > NFS_VER3) {
2204 nd->nd_repstat = EPROGMISMATCH;
2205 nd->nd_procnum = NFSPROC_NOOP;
2208 if (nfsvers == NFS_VER3)
2209 nd->nd_flag = ND_NFSV3;
2210 nd->nd_procnum = fxdr_unsigned(u_int32_t, *tl++);
2211 if (nd->nd_procnum == NFSPROC_NULL)
2213 if (nd->nd_procnum >= NFS_NPROCS ||
2214 (nd->nd_procnum >= NQNFSPROC_GETLEASE) ||
2215 (!nd->nd_flag && nd->nd_procnum > NFSV2PROC_STATFS)) {
2216 nd->nd_repstat = EPROCUNAVAIL;
2217 nd->nd_procnum = NFSPROC_NOOP;
2220 if ((nd->nd_flag & ND_NFSV3) == 0)
2221 nd->nd_procnum = nfsv3_procid[nd->nd_procnum];
2223 len = fxdr_unsigned(int, *tl++);
2224 if (len < 0 || len > RPCAUTH_MAXSIZ) {
2229 nd->nd_flag &= ~ND_KERBAUTH;
2231 * Handle auth_unix or auth_kerb.
2233 if (auth_type == rpc_auth_unix) {
2234 len = fxdr_unsigned(int, *++tl);
2235 if (len < 0 || len > NFS_MAXNAMLEN) {
2239 ERROROUT(nfsm_adv(&info, nfsm_rndup(len)));
2240 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2241 bzero((caddr_t)&nd->nd_cr, sizeof (struct ucred));
2242 nd->nd_cr.cr_ref = 1;
2243 nd->nd_cr.cr_uid = fxdr_unsigned(uid_t, *tl++);
2244 nd->nd_cr.cr_gid = fxdr_unsigned(gid_t, *tl++);
2245 len = fxdr_unsigned(int, *tl);
2246 if (len < 0 || len > RPCAUTH_UNIXGIDS) {
2250 NULLOUT(tl = nfsm_dissect(&info, (len + 2) * NFSX_UNSIGNED));
2251 for (i = 1; i <= len; i++)
2253 nd->nd_cr.cr_groups[i] = fxdr_unsigned(gid_t, *tl++);
2256 nd->nd_cr.cr_ngroups = (len >= NGROUPS) ? NGROUPS : (len + 1);
2257 if (nd->nd_cr.cr_ngroups > 1)
2258 nfsrvw_sort(nd->nd_cr.cr_groups, nd->nd_cr.cr_ngroups);
2259 len = fxdr_unsigned(int, *++tl);
2260 if (len < 0 || len > RPCAUTH_MAXSIZ) {
2265 ERROROUT(nfsm_adv(&info, nfsm_rndup(len)));
2267 } else if (auth_type == rpc_auth_kerb) {
2268 switch (fxdr_unsigned(int, *tl++)) {
2269 case RPCAKN_FULLNAME:
2270 ticklen = fxdr_unsigned(int, *tl);
2271 *((u_int32_t *)nfsd->nfsd_authstr) = *tl;
2272 uio.uio_resid = nfsm_rndup(ticklen) + NFSX_UNSIGNED;
2273 nfsd->nfsd_authlen = uio.uio_resid + NFSX_UNSIGNED;
2274 if (uio.uio_resid > (len - 2 * NFSX_UNSIGNED)) {
2281 uio.uio_segflg = UIO_SYSSPACE;
2282 iov.iov_base = (caddr_t)&nfsd->nfsd_authstr[4];
2283 iov.iov_len = RPCAUTH_MAXSIZ - 4;
2284 ERROROUT(nfsm_mtouio(&info, &uio, uio.uio_resid));
2285 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2286 if (*tl++ != rpc_auth_kerb ||
2287 fxdr_unsigned(int, *tl) != 4 * NFSX_UNSIGNED) {
2288 kprintf("Bad kerb verifier\n");
2289 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
2290 nd->nd_procnum = NFSPROC_NOOP;
2293 NULLOUT(cp = nfsm_dissect(&info, 4 * NFSX_UNSIGNED));
2294 tl = (u_int32_t *)cp;
2295 if (fxdr_unsigned(int, *tl) != RPCAKN_FULLNAME) {
2296 kprintf("Not fullname kerb verifier\n");
2297 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
2298 nd->nd_procnum = NFSPROC_NOOP;
2301 cp += NFSX_UNSIGNED;
2302 bcopy(cp, nfsd->nfsd_verfstr, 3 * NFSX_UNSIGNED);
2303 nfsd->nfsd_verflen = 3 * NFSX_UNSIGNED;
2304 nd->nd_flag |= ND_KERBFULL;
2305 nfsd->nfsd_flag |= NFSD_NEEDAUTH;
2307 case RPCAKN_NICKNAME:
2308 if (len != 2 * NFSX_UNSIGNED) {
2309 kprintf("Kerb nickname short\n");
2310 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADCRED);
2311 nd->nd_procnum = NFSPROC_NOOP;
2314 nickuid = fxdr_unsigned(uid_t, *tl);
2315 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2316 if (*tl++ != rpc_auth_kerb ||
2317 fxdr_unsigned(int, *tl) != 3 * NFSX_UNSIGNED) {
2318 kprintf("Kerb nick verifier bad\n");
2319 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
2320 nd->nd_procnum = NFSPROC_NOOP;
2323 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
2324 tvin.tv_sec = *tl++;
2327 for (nuidp = NUIDHASH(nfsd->nfsd_slp,nickuid)->lh_first;
2328 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
2329 if (nuidp->nu_cr.cr_uid == nickuid &&
2331 netaddr_match(NU_NETFAM(nuidp),
2332 &nuidp->nu_haddr, nd->nd_nam2)))
2337 (NFSERR_AUTHERR|AUTH_REJECTCRED);
2338 nd->nd_procnum = NFSPROC_NOOP;
2343 * Now, decrypt the timestamp using the session key
2350 tvout.tv_sec = fxdr_unsigned(long, tvout.tv_sec);
2351 tvout.tv_usec = fxdr_unsigned(long, tvout.tv_usec);
2352 if (nuidp->nu_expire < time_second ||
2353 nuidp->nu_timestamp.tv_sec > tvout.tv_sec ||
2354 (nuidp->nu_timestamp.tv_sec == tvout.tv_sec &&
2355 nuidp->nu_timestamp.tv_usec > tvout.tv_usec)) {
2356 nuidp->nu_expire = 0;
2358 (NFSERR_AUTHERR|AUTH_REJECTVERF);
2359 nd->nd_procnum = NFSPROC_NOOP;
2362 nfsrv_setcred(&nuidp->nu_cr, &nd->nd_cr);
2363 nd->nd_flag |= ND_KERBNICK;
2366 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_REJECTCRED);
2367 nd->nd_procnum = NFSPROC_NOOP;
2371 nd->nd_md = info.md;
2372 nd->nd_dpos = info.dpos;
2381 * Send a message to the originating process's terminal. The thread and/or
2382 * process may be NULL. YYY the thread should not be NULL but there may
2383 * still be some uio_td's that are still being passed as NULL through to
2387 nfs_msg(struct thread *td, char *server, char *msg)
2391 if (td && td->td_proc)
2392 tpr = tprintf_open(td->td_proc);
2395 tprintf(tpr, "nfs server %s: %s\n", server, msg);
2400 #ifndef NFS_NOSERVER
2402 * Socket upcall routine for the nfsd sockets.
2403 * The caddr_t arg is a pointer to the "struct nfssvc_sock".
2404 * Essentially do as much as possible non-blocking, else punt and it will
2405 * be called with MB_WAIT from an nfsd.
2408 nfsrv_rcv(struct socket *so, void *arg, int waitflag)
2410 struct nfssvc_sock *slp = (struct nfssvc_sock *)arg;
2412 struct sockaddr *nam;
2415 int nparallel_wakeup = 0;
2417 if ((slp->ns_flag & SLP_VALID) == 0)
2421 * Do not allow an infinite number of completed RPC records to build
2422 * up before we stop reading data from the socket. Otherwise we could
2423 * end up holding onto an unreasonable number of mbufs for requests
2424 * waiting for service.
2426 * This should give pretty good feedback to the TCP
2427 * layer and prevents a memory crunch for other protocols.
2429 * Note that the same service socket can be dispatched to several
2430 * nfs servers simultaniously.
2432 * the tcp protocol callback calls us with MB_DONTWAIT.
2433 * nfsd calls us with MB_WAIT (typically).
2435 if (waitflag == MB_DONTWAIT && slp->ns_numrec >= nfsd_waiting / 2 + 1) {
2436 slp->ns_flag |= SLP_NEEDQ;
2441 * Handle protocol specifics to parse an RPC request. We always
2442 * pull from the socket using non-blocking I/O.
2444 if (so->so_type == SOCK_STREAM) {
2446 * The data has to be read in an orderly fashion from a TCP
2447 * stream, unlike a UDP socket. It is possible for soreceive
2448 * and/or nfsrv_getstream() to block, so make sure only one
2449 * entity is messing around with the TCP stream at any given
2450 * moment. The receive sockbuf's lock in soreceive is not
2453 * Note that this procedure can be called from any number of
2454 * NFS severs *OR* can be upcalled directly from a TCP
2457 if (slp->ns_flag & SLP_GETSTREAM) {
2458 slp->ns_flag |= SLP_NEEDQ;
2461 slp->ns_flag |= SLP_GETSTREAM;
2464 * Do soreceive(). Pull out as much data as possible without
2467 sbinit(&sio, 1000000000);
2468 flags = MSG_DONTWAIT;
2469 error = so_pru_soreceive(so, &nam, NULL, &sio, NULL, &flags);
2470 if (error || sio.sb_mb == NULL) {
2471 if (error == EWOULDBLOCK)
2472 slp->ns_flag |= SLP_NEEDQ;
2474 slp->ns_flag |= SLP_DISCONN;
2475 slp->ns_flag &= ~SLP_GETSTREAM;
2479 if (slp->ns_rawend) {
2480 slp->ns_rawend->m_next = m;
2481 slp->ns_cc += sio.sb_cc;
2484 slp->ns_cc = sio.sb_cc;
2491 * Now try and parse as many record(s) as we can out of the
2494 error = nfsrv_getstream(slp, waitflag, &nparallel_wakeup);
2497 slp->ns_flag |= SLP_DISCONN;
2499 slp->ns_flag |= SLP_NEEDQ;
2501 slp->ns_flag &= ~SLP_GETSTREAM;
2504 * For UDP soreceive typically pulls just one packet, loop
2505 * to get the whole batch.
2508 sbinit(&sio, 1000000000);
2509 flags = MSG_DONTWAIT;
2510 error = so_pru_soreceive(so, &nam, NULL, &sio,
2513 struct nfsrv_rec *rec;
2514 int mf = (waitflag & MB_DONTWAIT) ?
2515 M_NOWAIT : M_WAITOK;
2516 rec = kmalloc(sizeof(struct nfsrv_rec),
2520 FREE(nam, M_SONAME);
2524 nfs_realign(&sio.sb_mb, 10 * NFSX_UNSIGNED);
2525 rec->nr_address = nam;
2526 rec->nr_packet = sio.sb_mb;
2527 STAILQ_INSERT_TAIL(&slp->ns_rec, rec, nr_link);
2532 if ((so->so_proto->pr_flags & PR_CONNREQUIRED)
2533 && error != EWOULDBLOCK) {
2534 slp->ns_flag |= SLP_DISCONN;
2538 } while (sio.sb_mb);
2542 * If we were upcalled from the tcp protocol layer and we have
2543 * fully parsed records ready to go, or there is new data pending,
2544 * or something went wrong, try to wake up an nfsd thread to deal
2548 if (waitflag == MB_DONTWAIT && (slp->ns_numrec > 0
2549 || (slp->ns_flag & (SLP_NEEDQ | SLP_DISCONN)))) {
2550 nfsrv_wakenfsd(slp, nparallel_wakeup);
2555 * Try and extract an RPC request from the mbuf data list received on a
2556 * stream socket. The "waitflag" argument indicates whether or not it
2560 nfsrv_getstream(struct nfssvc_sock *slp, int waitflag, int *countp)
2562 struct mbuf *m, **mpp;
2565 struct mbuf *om, *m2, *recm;
2569 if (slp->ns_reclen == 0) {
2570 if (slp->ns_cc < NFSX_UNSIGNED)
2573 if (m->m_len >= NFSX_UNSIGNED) {
2574 bcopy(mtod(m, caddr_t), (caddr_t)&recmark, NFSX_UNSIGNED);
2575 m->m_data += NFSX_UNSIGNED;
2576 m->m_len -= NFSX_UNSIGNED;
2578 cp1 = (caddr_t)&recmark;
2579 cp2 = mtod(m, caddr_t);
2580 while (cp1 < ((caddr_t)&recmark) + NFSX_UNSIGNED) {
2581 while (m->m_len == 0) {
2583 cp2 = mtod(m, caddr_t);
2590 slp->ns_cc -= NFSX_UNSIGNED;
2591 recmark = ntohl(recmark);
2592 slp->ns_reclen = recmark & ~0x80000000;
2593 if (recmark & 0x80000000)
2594 slp->ns_flag |= SLP_LASTFRAG;
2596 slp->ns_flag &= ~SLP_LASTFRAG;
2597 if (slp->ns_reclen > NFS_MAXPACKET || slp->ns_reclen <= 0) {
2598 log(LOG_ERR, "%s (%d) from nfs client\n",
2599 "impossible packet length",
2606 * Now get the record part.
2608 * Note that slp->ns_reclen may be 0. Linux sometimes
2609 * generates 0-length RPCs
2612 if (slp->ns_cc == slp->ns_reclen) {
2614 slp->ns_raw = slp->ns_rawend = NULL;
2615 slp->ns_cc = slp->ns_reclen = 0;
2616 } else if (slp->ns_cc > slp->ns_reclen) {
2621 while (len < slp->ns_reclen) {
2622 if ((len + m->m_len) > slp->ns_reclen) {
2623 m2 = m_copym(m, 0, slp->ns_reclen - len,
2631 m->m_data += slp->ns_reclen - len;
2632 m->m_len -= slp->ns_reclen - len;
2633 len = slp->ns_reclen;
2635 return (EWOULDBLOCK);
2637 } else if ((len + m->m_len) == slp->ns_reclen) {
2657 * Accumulate the fragments into a record.
2659 mpp = &slp->ns_frag;
2661 mpp = &((*mpp)->m_next);
2663 if (slp->ns_flag & SLP_LASTFRAG) {
2664 struct nfsrv_rec *rec;
2665 int mf = (waitflag & MB_DONTWAIT) ? M_NOWAIT : M_WAITOK;
2666 rec = kmalloc(sizeof(struct nfsrv_rec), M_NFSRVDESC, mf);
2668 m_freem(slp->ns_frag);
2670 nfs_realign(&slp->ns_frag, 10 * NFSX_UNSIGNED);
2671 rec->nr_address = NULL;
2672 rec->nr_packet = slp->ns_frag;
2673 STAILQ_INSERT_TAIL(&slp->ns_rec, rec, nr_link);
2677 slp->ns_frag = NULL;
2683 * Parse an RPC header.
2686 nfsrv_dorec(struct nfssvc_sock *slp, struct nfsd *nfsd,
2687 struct nfsrv_descript **ndp)
2689 struct nfsrv_rec *rec;
2691 struct sockaddr *nam;
2692 struct nfsrv_descript *nd;
2696 if ((slp->ns_flag & SLP_VALID) == 0 || !STAILQ_FIRST(&slp->ns_rec))
2698 rec = STAILQ_FIRST(&slp->ns_rec);
2699 STAILQ_REMOVE_HEAD(&slp->ns_rec, nr_link);
2700 KKASSERT(slp->ns_numrec > 0);
2702 nam = rec->nr_address;
2704 kfree(rec, M_NFSRVDESC);
2705 MALLOC(nd, struct nfsrv_descript *, sizeof (struct nfsrv_descript),
2706 M_NFSRVDESC, M_WAITOK);
2707 nd->nd_md = nd->nd_mrep = m;
2709 nd->nd_dpos = mtod(m, caddr_t);
2710 error = nfs_getreq(nd, nfsd, TRUE);
2713 FREE(nam, M_SONAME);
2715 kfree((caddr_t)nd, M_NFSRVDESC);
2724 * Try to assign service sockets to nfsd threads based on the number
2725 * of new rpc requests that have been queued on the service socket.
2727 * If no nfsd's are available or additonal requests are pending, set the
2728 * NFSD_CHECKSLP flag so that one of the running nfsds will go look for
2729 * the work in the nfssvc_sock list when it is finished processing its
2730 * current work. This flag is only cleared when an nfsd can not find
2731 * any new work to perform.
2734 nfsrv_wakenfsd(struct nfssvc_sock *slp, int nparallel)
2738 if ((slp->ns_flag & SLP_VALID) == 0)
2742 TAILQ_FOREACH(nd, &nfsd_head, nfsd_chain) {
2743 if (nd->nfsd_flag & NFSD_WAITING) {
2744 nd->nfsd_flag &= ~NFSD_WAITING;
2746 panic("nfsd wakeup");
2749 wakeup((caddr_t)nd);
2750 if (--nparallel == 0)
2755 slp->ns_flag |= SLP_DOREC;
2756 nfsd_head_flag |= NFSD_CHECKSLP;
2759 #endif /* NFS_NOSERVER */