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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)nfs_socket.c 8.5 (Berkeley) 3/30/95
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 * Socket operations for use by nfs
42 #include "opt_inet6.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
48 #include <sys/malloc.h>
50 #include <sys/mount.h>
51 #include <sys/mutex.h>
53 #include <sys/protosw.h>
54 #include <sys/signalvar.h>
55 #include <sys/syscallsubr.h>
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
58 #include <sys/sysctl.h>
59 #include <sys/syslog.h>
60 #include <sys/vnode.h>
62 #include <netinet/in.h>
63 #include <netinet/tcp.h>
65 #include <rpc/rpcclnt.h>
67 #include <nfs/rpcv2.h>
68 #include <nfs/nfsproto.h>
69 #include <nfsclient/nfs.h>
70 #include <nfs/xdr_subs.h>
71 #include <nfsclient/nfsm_subs.h>
72 #include <nfsclient/nfsmount.h>
73 #include <nfsclient/nfsnode.h>
75 #include <nfs4client/nfs4.h>
80 extern u_int32_t nfs_xid;
81 extern struct mtx nfs_xid_mtx;
83 static int nfs_realign_test;
84 static int nfs_realign_count;
85 static int nfs_bufpackets = 4;
86 static int nfs_reconnects;
87 static int nfs3_jukebox_delay = 10;
88 static int nfs_skip_wcc_data_onerr = 1;
90 SYSCTL_DECL(_vfs_nfs);
92 SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_test, CTLFLAG_RW, &nfs_realign_test, 0, "");
93 SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_count, CTLFLAG_RW, &nfs_realign_count, 0, "");
94 SYSCTL_INT(_vfs_nfs, OID_AUTO, bufpackets, CTLFLAG_RW, &nfs_bufpackets, 0, "");
95 SYSCTL_INT(_vfs_nfs, OID_AUTO, reconnects, CTLFLAG_RD, &nfs_reconnects, 0,
96 "number of times the nfs client has had to reconnect");
97 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs3_jukebox_delay, CTLFLAG_RW, &nfs3_jukebox_delay, 0,
98 "number of seconds to delay a retry after receiving EJUKEBOX");
99 SYSCTL_INT(_vfs_nfs, OID_AUTO, skip_wcc_data_onerr, CTLFLAG_RW, &nfs_skip_wcc_data_onerr, 0, "");
102 * There is a congestion window for outstanding rpcs maintained per mount
103 * point. The cwnd size is adjusted in roughly the way that:
104 * Van Jacobson, Congestion avoidance and Control, In "Proceedings of
105 * SIGCOMM '88". ACM, August 1988.
106 * describes for TCP. The cwnd size is chopped in half on a retransmit timeout
107 * and incremented by 1/cwnd when each rpc reply is received and a full cwnd
108 * of rpcs is in progress.
109 * (The sent count and cwnd are scaled for integer arith.)
110 * Variants of "slow start" were tried and were found to be too much of a
111 * performance hit (ave. rtt 3 times larger),
112 * I suspect due to the large rtt that nfs rpcs have.
114 #define NFS_CWNDSCALE 256
115 #define NFS_MAXCWND (NFS_CWNDSCALE * 32)
116 #define NFS_NBACKOFF 8
117 static int nfs_backoff[NFS_NBACKOFF] = { 2, 4, 8, 16, 32, 64, 128, 256, };
118 struct callout nfs_callout;
120 static int nfs_msg(struct thread *, const char *, const char *, int);
121 static int nfs_realign(struct mbuf **pm, int hsiz);
122 static int nfs_reply(struct nfsreq *);
123 static void nfs_softterm(struct nfsreq *rep);
124 static int nfs_reconnect(struct nfsreq *rep);
125 static void nfs_clnt_tcp_soupcall(struct socket *so, void *arg, int waitflag);
126 static void nfs_clnt_udp_soupcall(struct socket *so, void *arg, int waitflag);
128 extern struct mtx nfs_reqq_mtx;
134 static enum nfs_rto_timer_t nfs_proct[NFS_NPROCS] = {
135 NFS_DEFAULT_TIMER, /* NULL */
136 NFS_GETATTR_TIMER, /* GETATTR */
137 NFS_DEFAULT_TIMER, /* SETATTR */
138 NFS_LOOKUP_TIMER, /* LOOKUP */
139 NFS_GETATTR_TIMER, /* ACCESS */
140 NFS_READ_TIMER, /* READLINK */
141 NFS_READ_TIMER, /* READ */
142 NFS_WRITE_TIMER, /* WRITE */
143 NFS_DEFAULT_TIMER, /* CREATE */
144 NFS_DEFAULT_TIMER, /* MKDIR */
145 NFS_DEFAULT_TIMER, /* SYMLINK */
146 NFS_DEFAULT_TIMER, /* MKNOD */
147 NFS_DEFAULT_TIMER, /* REMOVE */
148 NFS_DEFAULT_TIMER, /* RMDIR */
149 NFS_DEFAULT_TIMER, /* RENAME */
150 NFS_DEFAULT_TIMER, /* LINK */
151 NFS_READ_TIMER, /* READDIR */
152 NFS_READ_TIMER, /* READDIRPLUS */
153 NFS_DEFAULT_TIMER, /* FSSTAT */
154 NFS_DEFAULT_TIMER, /* FSINFO */
155 NFS_DEFAULT_TIMER, /* PATHCONF */
156 NFS_DEFAULT_TIMER, /* COMMIT */
157 NFS_DEFAULT_TIMER, /* NOOP */
161 * Choose the correct RTT timer for this NFS procedure.
163 static inline enum nfs_rto_timer_t
164 nfs_rto_timer(u_int32_t procnum)
166 return nfs_proct[procnum];
170 * Initialize the RTT estimator state for a new mount point.
173 nfs_init_rtt(struct nfsmount *nmp)
177 for (i = 0; i < NFS_MAX_TIMER; i++)
178 nmp->nm_srtt[i] = NFS_INITRTT;
179 for (i = 0; i < NFS_MAX_TIMER; i++)
180 nmp->nm_sdrtt[i] = 0;
184 * Update a mount point's RTT estimator state using data from the
187 * Use a gain of 0.125 on the mean and a gain of 0.25 on the deviation.
189 * NB: Since the timer resolution of NFS_HZ is so course, it can often
190 * result in r_rtt == 0. Since r_rtt == N means that the actual RTT is
191 * between N + dt and N + 2 - dt ticks, add 1 before calculating the
195 nfs_update_rtt(struct nfsreq *rep)
197 int t1 = rep->r_rtt + 1;
198 int index = nfs_rto_timer(rep->r_procnum) - 1;
199 int *srtt = &rep->r_nmp->nm_srtt[index];
200 int *sdrtt = &rep->r_nmp->nm_sdrtt[index];
211 * Estimate RTO for an NFS RPC sent via an unreliable datagram.
213 * Use the mean and mean deviation of RTT for the appropriate type
214 * of RPC for the frequent RPCs and a default for the others.
215 * The justification for doing "other" this way is that these RPCs
216 * happen so infrequently that timer est. would probably be stale.
217 * Also, since many of these RPCs are non-idempotent, a conservative
218 * timeout is desired.
220 * getattr, lookup - A+2D
225 nfs_estimate_rto(struct nfsmount *nmp, u_int32_t procnum)
227 enum nfs_rto_timer_t timer = nfs_rto_timer(procnum);
228 int index = timer - 1;
232 case NFS_GETATTR_TIMER:
233 case NFS_LOOKUP_TIMER:
234 rto = ((nmp->nm_srtt[index] + 3) >> 2) +
235 ((nmp->nm_sdrtt[index] + 1) >> 1);
238 case NFS_WRITE_TIMER:
239 rto = ((nmp->nm_srtt[index] + 7) >> 3) +
240 (nmp->nm_sdrtt[index] + 1);
247 if (rto < NFS_MINRTO)
249 else if (rto > NFS_MAXRTO)
257 * Initialize sockets and congestion for a new NFS connection.
258 * We do not free the sockaddr if error.
261 nfs_connect(struct nfsmount *nmp, struct nfsreq *rep)
264 int error, rcvreserve, sndreserve;
266 struct sockaddr *saddr;
267 struct ucred *origcred;
268 struct thread *td = curthread;
271 * We need to establish the socket using the credentials of
272 * the mountpoint. Some parts of this process (such as
273 * sobind() and soconnect()) will use the curent thread's
274 * credential instead of the socket credential. To work
275 * around this, temporarily change the current thread's
276 * credential to that of the mountpoint.
278 * XXX: It would be better to explicitly pass the correct
279 * credential to sobind() and soconnect().
281 origcred = td->td_ucred;
282 td->td_ucred = nmp->nm_mountp->mnt_cred;
284 if (nmp->nm_sotype == SOCK_STREAM) {
285 mtx_lock(&nmp->nm_mtx);
286 nmp->nm_nfstcpstate.flags |= NFS_TCP_EXPECT_RPCMARKER;
287 nmp->nm_nfstcpstate.rpcresid = 0;
288 mtx_unlock(&nmp->nm_mtx);
292 error = socreate(saddr->sa_family, &nmp->nm_so, nmp->nm_sotype,
293 nmp->nm_soproto, nmp->nm_mountp->mnt_cred, td);
297 nmp->nm_soflags = so->so_proto->pr_flags;
300 * Some servers require that the client port be a reserved port number.
302 if (nmp->nm_flag & NFSMNT_RESVPORT) {
305 struct sockaddr_in6 ssin;
308 bzero(&sopt, sizeof sopt);
309 switch(saddr->sa_family) {
311 sopt.sopt_level = IPPROTO_IP;
312 sopt.sopt_name = IP_PORTRANGE;
313 ip = IP_PORTRANGE_LOW;
314 ip2 = IP_PORTRANGE_DEFAULT;
315 len = sizeof (struct sockaddr_in);
319 sopt.sopt_level = IPPROTO_IPV6;
320 sopt.sopt_name = IPV6_PORTRANGE;
321 ip = IPV6_PORTRANGE_LOW;
322 ip2 = IPV6_PORTRANGE_DEFAULT;
323 len = sizeof (struct sockaddr_in6);
329 sa = (struct sockaddr *)&ssin;
332 sa->sa_family = saddr->sa_family;
333 sopt.sopt_dir = SOPT_SET;
334 sopt.sopt_val = (void *)&ip;
335 sopt.sopt_valsize = sizeof(ip);
336 error = sosetopt(so, &sopt);
339 error = sobind(so, sa, td);
343 error = sosetopt(so, &sopt);
350 * Protocols that do not require connections may be optionally left
351 * unconnected for servers that reply from a port other than NFS_PORT.
353 mtx_lock(&nmp->nm_mtx);
354 if (nmp->nm_flag & NFSMNT_NOCONN) {
355 if (nmp->nm_soflags & PR_CONNREQUIRED) {
357 mtx_unlock(&nmp->nm_mtx);
360 mtx_unlock(&nmp->nm_mtx);
362 mtx_unlock(&nmp->nm_mtx);
363 error = soconnect(so, nmp->nm_nam, td);
368 * Wait for the connection to complete. Cribbed from the
369 * connect system call but with the wait timing out so
370 * that interruptible mounts don't hang here for a long time.
373 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
374 (void) msleep(&so->so_timeo, SOCK_MTX(so),
375 PSOCK, "nfscon", 2 * hz);
376 if ((so->so_state & SS_ISCONNECTING) &&
377 so->so_error == 0 && rep &&
378 (error = nfs_sigintr(nmp, rep, rep->r_td)) != 0) {
379 so->so_state &= ~SS_ISCONNECTING;
385 error = so->so_error;
392 so->so_rcv.sb_timeo = 12 * hz;
393 if (nmp->nm_sotype == SOCK_STREAM)
394 so->so_snd.sb_timeo = 1 * hz; /* 1s snd timeout for NFS/TCP */
396 so->so_snd.sb_timeo = 5 * hz;
399 * Get buffer reservation size from sysctl, but impose reasonable
402 pktscale = nfs_bufpackets;
407 mtx_lock(&nmp->nm_mtx);
408 if (nmp->nm_sotype == SOCK_DGRAM) {
409 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * pktscale;
410 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
411 NFS_MAXPKTHDR) * pktscale;
412 } else if (nmp->nm_sotype == SOCK_SEQPACKET) {
413 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * pktscale;
414 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
415 NFS_MAXPKTHDR) * pktscale;
417 if (nmp->nm_sotype != SOCK_STREAM)
418 panic("nfscon sotype");
419 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
423 bzero(&sopt, sizeof sopt);
424 sopt.sopt_dir = SOPT_SET;
425 sopt.sopt_level = SOL_SOCKET;
426 sopt.sopt_name = SO_KEEPALIVE;
427 sopt.sopt_val = &val;
428 sopt.sopt_valsize = sizeof val;
430 mtx_unlock(&nmp->nm_mtx);
432 mtx_lock(&nmp->nm_mtx);
434 if (so->so_proto->pr_protocol == IPPROTO_TCP) {
438 bzero(&sopt, sizeof sopt);
439 sopt.sopt_dir = SOPT_SET;
440 sopt.sopt_level = IPPROTO_TCP;
441 sopt.sopt_name = TCP_NODELAY;
442 sopt.sopt_val = &val;
443 sopt.sopt_valsize = sizeof val;
445 mtx_unlock(&nmp->nm_mtx);
447 mtx_lock(&nmp->nm_mtx);
449 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR +
450 sizeof (u_int32_t)) * pktscale;
451 rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR +
452 sizeof (u_int32_t)) * pktscale;
454 mtx_unlock(&nmp->nm_mtx);
455 error = soreserve(so, sndreserve, rcvreserve);
458 SOCKBUF_LOCK(&so->so_rcv);
459 so->so_rcv.sb_flags |= SB_NOINTR;
460 so->so_upcallarg = (caddr_t)nmp;
461 if (so->so_type == SOCK_STREAM)
462 so->so_upcall = nfs_clnt_tcp_soupcall;
464 so->so_upcall = nfs_clnt_udp_soupcall;
465 so->so_rcv.sb_flags |= SB_UPCALL;
466 SOCKBUF_UNLOCK(&so->so_rcv);
467 SOCKBUF_LOCK(&so->so_snd);
468 so->so_snd.sb_flags |= SB_NOINTR;
469 SOCKBUF_UNLOCK(&so->so_snd);
471 /* Restore current thread's credentials. */
472 td->td_ucred = origcred;
474 mtx_lock(&nmp->nm_mtx);
475 /* Initialize other non-zero congestion variables */
477 nmp->nm_cwnd = NFS_MAXCWND / 2; /* Initial send window */
479 nmp->nm_timeouts = 0;
480 mtx_unlock(&nmp->nm_mtx);
484 /* Restore current thread's credentials. */
485 td->td_ucred = origcred;
492 nfs_wakup_reconnectors(struct nfsmount *nmp)
494 KASSERT(mtx_owned(&nmp->nm_mtx), ("NFS mnt lock not owned !"));
495 if (--nmp->nm_nfstcpstate.sock_send_inprog == 0 &&
496 (nmp->nm_nfstcpstate.flags & NFS_TCP_WAIT_WRITE_DRAIN)) {
497 nmp->nm_nfstcpstate.flags &= ~NFS_TCP_WAIT_WRITE_DRAIN;
498 wakeup((caddr_t)&nmp->nm_nfstcpstate.sock_send_inprog);
504 * Called when a connection is broken on a reliable protocol.
505 * - clean up the old socket
506 * - nfs_connect() again
507 * - set R_MUSTRESEND for all outstanding requests on mount point
508 * If this fails the mount point is DEAD!
509 * nb: Must be called with the nfs_sndlock() set on the mount point.
512 nfs_reconnect(struct nfsreq *rep)
515 struct nfsmount *nmp = rep->r_nmp;
519 KASSERT(mtx_owned(&nmp->nm_mtx), ("NFS mnt lock not owned !"));
520 if (nmp->nm_flag & NFSMNT_INT)
523 * Wait for any pending writes to this socket to drain (or timeout).
525 while (nmp->nm_nfstcpstate.sock_send_inprog > 0) {
526 nmp->nm_nfstcpstate.flags |= NFS_TCP_WAIT_WRITE_DRAIN;
527 error = msleep((caddr_t)&nmp->nm_nfstcpstate.sock_send_inprog,
528 &nmp->nm_mtx, slpflag | (PZERO - 1), "nfscon", 0);
531 * Grab the nfs_connect_lock to serialize connects.
532 * After grabbing the nfs_connect_lock, check if a reconnect is necessary or
533 * if someone else beat us to the connect !
535 error = nfs_connect_lock(rep);
538 if ((nmp->nm_nfstcpstate.flags & NFS_TCP_FORCE_RECONNECT) == 0)
541 mtx_unlock(&nmp->nm_mtx);
545 while ((error = nfs_connect(nmp, rep)) != 0) {
546 if (error == ERESTART)
548 if (error == EIO || error == EINTR) {
549 mtx_lock(&nmp->nm_mtx);
552 (void) tsleep(&lbolt, PSOCK, "nfscon", 0);
556 * Clear the FORCE_RECONNECT flag only after the connect
557 * succeeds. To prevent races between multiple processes
558 * waiting on the mountpoint where the connection is being
559 * torn down. The first one to acquire the sndlock will
560 * retry the connection. The others block on the sndlock
561 * until the connection is established successfully, and
562 * then re-transmit the request.
564 mtx_lock(&nmp->nm_mtx);
565 nmp->nm_nfstcpstate.flags &= ~NFS_TCP_FORCE_RECONNECT;
566 nmp->nm_nfstcpstate.rpcresid = 0;
567 mtx_unlock(&nmp->nm_mtx);
570 * Loop through outstanding request list and fix up all requests
573 mtx_lock(&nfs_reqq_mtx);
574 TAILQ_FOREACH(rp, &nfs_reqq, r_chain) {
575 if (rp->r_nmp == nmp) {
576 mtx_lock(&rp->r_mtx);
577 rp->r_flags |= R_MUSTRESEND;
578 mtx_unlock(&rp->r_mtx);
581 mtx_unlock(&nfs_reqq_mtx);
582 mtx_lock(&nmp->nm_mtx);
584 nfs_connect_unlock(rep);
585 mtx_unlock(&nmp->nm_mtx);
590 * NFS disconnect. Clean up and unlink.
593 nfs_disconnect(struct nfsmount *nmp)
597 mtx_lock(&nmp->nm_mtx);
601 mtx_unlock(&nmp->nm_mtx);
602 SOCKBUF_LOCK(&so->so_rcv);
603 so->so_upcallarg = NULL;
604 so->so_upcall = NULL;
605 so->so_rcv.sb_flags &= ~SB_UPCALL;
606 SOCKBUF_UNLOCK(&so->so_rcv);
607 soshutdown(so, SHUT_WR);
610 mtx_unlock(&nmp->nm_mtx);
614 nfs_safedisconnect(struct nfsmount *nmp)
616 struct nfsreq dummyreq;
618 bzero(&dummyreq, sizeof(dummyreq));
619 dummyreq.r_nmp = nmp;
624 * This is the nfs send routine. For connection based socket types, it
625 * must be called with an nfs_sndlock() on the socket.
626 * - return EINTR if the RPC is terminated, 0 otherwise
627 * - set R_MUSTRESEND if the send fails for any reason
628 * - do any cleanup required by recoverable socket errors (?)
631 nfs_send(struct socket *so, struct sockaddr *nam, struct mbuf *top,
634 struct sockaddr *sendnam;
635 int error, error2, soflags, flags;
637 KASSERT(rep, ("nfs_send: called with rep == NULL"));
639 error = nfs_sigintr(rep->r_nmp, rep, rep->r_td);
644 mtx_lock(&rep->r_nmp->nm_mtx);
645 mtx_lock(&rep->r_mtx);
646 if ((so = rep->r_nmp->nm_so) == NULL) {
647 rep->r_flags |= R_MUSTRESEND;
648 mtx_unlock(&rep->r_mtx);
649 mtx_unlock(&rep->r_nmp->nm_mtx);
653 rep->r_flags &= ~R_MUSTRESEND;
654 soflags = rep->r_nmp->nm_soflags;
655 mtx_unlock(&rep->r_mtx);
656 mtx_unlock(&rep->r_nmp->nm_mtx);
658 if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED))
662 if (so->so_type == SOCK_SEQPACKET)
667 error = sosend(so, sendnam, 0, top, 0, flags, curthread /*XXX*/);
668 if (error == ENOBUFS && so->so_type == SOCK_DGRAM) {
670 mtx_lock(&rep->r_mtx);
671 rep->r_flags |= R_MUSTRESEND;
672 mtx_unlock(&rep->r_mtx);
677 * Don't report EPIPE errors on nfs sockets.
678 * These can be due to idle tcp mounts which will be closed by
679 * netapp, solaris, etc. if left idle too long.
681 if (error != EPIPE) {
682 log(LOG_INFO, "nfs send error %d for server %s\n",
684 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
687 * Deal with errors for the client side.
689 error2 = NFS_SIGREP(rep);
693 mtx_lock(&rep->r_mtx);
694 rep->r_flags |= R_MUSTRESEND;
695 mtx_unlock(&rep->r_mtx);
699 * Handle any recoverable (soft) socket errors here. (?)
700 * Make EWOULDBLOCK a recoverable error, we'll rexmit from nfs_timer().
702 if (error != EINTR && error != ERESTART && error != EIO && error != EPIPE)
709 nfs_reply(struct nfsreq *rep)
711 register struct socket *so;
712 register struct mbuf *m;
713 int error = 0, sotype, slpflag;
714 struct nfsmount *nmp = rep->r_nmp;
716 sotype = nmp->nm_sotype;
718 * For reliable protocols, lock against other senders/receivers
719 * in case a reconnect is necessary.
721 if (sotype != SOCK_DGRAM) {
723 mtx_lock(&nmp->nm_mtx);
724 mtx_lock(&rep->r_mtx);
726 mtx_unlock(&rep->r_mtx);
727 mtx_unlock(&nmp->nm_mtx);
730 if (rep->r_flags & R_SOFTTERM) {
731 mtx_unlock(&rep->r_mtx);
732 mtx_unlock(&nmp->nm_mtx);
737 (nmp->nm_nfstcpstate.flags & NFS_TCP_FORCE_RECONNECT)) {
738 mtx_unlock(&rep->r_mtx);
739 nmp->nm_nfstcpstate.flags |= NFS_TCP_FORCE_RECONNECT;
740 error = nfs_reconnect(rep);
745 while (rep->r_flags & R_MUSTRESEND) {
746 mtx_unlock(&rep->r_mtx);
747 nmp->nm_nfstcpstate.sock_send_inprog++;
748 mtx_unlock(&nmp->nm_mtx);
749 m = m_copym(rep->r_mreq, 0, M_COPYALL, M_WAIT);
750 nfsstats.rpcretries++;
751 error = nfs_send(so, nmp->nm_nam, m, rep);
753 mtx_lock(&nmp->nm_mtx);
754 nfs_wakup_reconnectors(nmp);
755 if (!(error == EINTR || error == ERESTART)) {
756 nmp->nm_nfstcpstate.flags |= NFS_TCP_FORCE_RECONNECT;
757 error = nfs_reconnect(rep);
759 mtx_unlock(&nmp->nm_mtx);
764 mtx_lock(&nmp->nm_mtx);
765 nfs_wakup_reconnectors(nmp);
766 mtx_lock(&rep->r_mtx);
769 mtx_unlock(&rep->r_mtx);
770 mtx_unlock(&nmp->nm_mtx);
773 mtx_lock(&nmp->nm_mtx);
774 if (nmp->nm_flag & NFSMNT_INT)
776 mtx_unlock(&nmp->nm_mtx);
777 mtx_lock(&rep->r_mtx);
778 while ((rep->r_mrep == NULL) && (error == 0) &&
779 ((rep->r_flags & R_SOFTTERM) == 0) &&
780 ((sotype == SOCK_DGRAM) || ((rep->r_flags & R_MUSTRESEND) == 0)))
781 error = msleep((caddr_t)rep, &rep->r_mtx,
782 slpflag | (PZERO - 1), "nfsreq", 0);
783 if (error == EINTR || error == ERESTART) {
784 /* NFS operations aren't restartable. Map ERESTART to EINTR */
785 mtx_unlock(&rep->r_mtx);
788 if (rep->r_flags & R_SOFTTERM) {
789 /* Request was terminated because we exceeded the retries (soft mount) */
790 mtx_unlock(&rep->r_mtx);
793 mtx_unlock(&rep->r_mtx);
794 if (sotype == SOCK_STREAM) {
795 mtx_lock(&nmp->nm_mtx);
796 mtx_lock(&rep->r_mtx);
797 if (((nmp->nm_nfstcpstate.flags & NFS_TCP_FORCE_RECONNECT) ||
798 (rep->r_flags & R_MUSTRESEND))) {
799 mtx_unlock(&rep->r_mtx);
800 mtx_unlock(&nmp->nm_mtx);
803 mtx_unlock(&rep->r_mtx);
804 mtx_unlock(&nmp->nm_mtx);
812 * Make nfs_realign() non-blocking. Also make nfsm_dissect() nonblocking.
815 nfs_clnt_match_xid(struct socket *so,
816 struct nfsmount *nmp,
826 * Search for any mbufs that are not a multiple of 4 bytes long
827 * or with m_data not longword aligned.
828 * These could cause pointer alignment problems, so copy them to
829 * well aligned mbufs.
831 if (nfs_realign(&mrep, 5 * NFSX_UNSIGNED) == ENOMEM) {
833 nfsstats.rpcinvalid++;
838 * Get the xid and check that it is an rpc reply
841 dpos = mtod(md, caddr_t);
842 tl = nfsm_dissect_nonblock(u_int32_t *, 2*NFSX_UNSIGNED);
844 if (*tl != rpc_reply) {
847 nfsstats.rpcinvalid++;
851 mtx_lock(&nfs_reqq_mtx);
853 * Loop through the request list to match up the reply
854 * Iff no match, just drop the datagram
856 TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
857 mtx_lock(&nmp->nm_mtx);
858 mtx_lock(&rep->r_mtx);
859 if (rep->r_mrep == NULL && rxid == rep->r_xid) {
865 * Update congestion window.
866 * Do the additive increase of
869 if (nmp->nm_cwnd <= nmp->nm_sent) {
871 (NFS_CWNDSCALE * NFS_CWNDSCALE +
872 (nmp->nm_cwnd >> 1)) / nmp->nm_cwnd;
873 if (nmp->nm_cwnd > NFS_MAXCWND)
874 nmp->nm_cwnd = NFS_MAXCWND;
876 if (rep->r_flags & R_SENT) {
877 rep->r_flags &= ~R_SENT;
878 nmp->nm_sent -= NFS_CWNDSCALE;
880 if (rep->r_flags & R_TIMING)
882 nmp->nm_timeouts = 0;
883 wakeup((caddr_t)rep);
884 mtx_unlock(&rep->r_mtx);
885 mtx_unlock(&nmp->nm_mtx);
888 mtx_unlock(&rep->r_mtx);
889 mtx_unlock(&nmp->nm_mtx);
892 * If not matched to a request, drop it.
893 * If it's mine, wake up requestor.
896 nfsstats.rpcunexpected++;
899 mtx_unlock(&nfs_reqq_mtx);
903 nfs_mark_for_reconnect(struct nfsmount *nmp)
907 mtx_lock(&nmp->nm_mtx);
908 nmp->nm_nfstcpstate.flags |= NFS_TCP_FORCE_RECONNECT;
909 mtx_unlock(&nmp->nm_mtx);
911 * Wakeup all processes that are waiting for replies
912 * on this mount point. One of them does the reconnect.
914 mtx_lock(&nfs_reqq_mtx);
915 TAILQ_FOREACH(rp, &nfs_reqq, r_chain) {
916 if (rp->r_nmp == nmp) {
917 mtx_lock(&rp->r_mtx);
918 rp->r_flags |= R_MUSTRESEND;
920 mtx_unlock(&rp->r_mtx);
923 mtx_unlock(&nfs_reqq_mtx);
927 nfstcp_readable(struct socket *so, int bytes)
931 SOCKBUF_LOCK(&so->so_rcv);
932 retval = (so->so_rcv.sb_cc >= (bytes) ||
933 (so->so_rcv.sb_state & SBS_CANTRCVMORE) ||
935 SOCKBUF_UNLOCK(&so->so_rcv);
939 #define nfstcp_marker_readable(so) nfstcp_readable(so, sizeof(u_int32_t))
942 nfs_copy_len(struct mbuf *mp, char *buf, int len)
944 while (len > 0 && mp != NULL) {
945 int copylen = min(len, mp->m_len);
947 bcopy(mp->m_data, buf, copylen);
956 nfs_clnt_tcp_soupcall(struct socket *so, void *arg, int waitflag)
958 struct nfsmount *nmp = (struct nfsmount *)arg;
959 struct mbuf *mp = NULL;
966 * Don't pick any more data from the socket if we've marked the
967 * mountpoint for reconnect.
969 mtx_lock(&nmp->nm_mtx);
970 if (nmp->nm_nfstcpstate.flags & NFS_TCP_FORCE_RECONNECT) {
971 mtx_unlock(&nmp->nm_mtx);
974 mtx_unlock(&nmp->nm_mtx);
975 auio.uio_td = curthread;
976 auio.uio_segflg = UIO_SYSSPACE;
977 auio.uio_rw = UIO_READ;
979 mtx_lock(&nmp->nm_mtx);
980 if (nmp->nm_nfstcpstate.flags & NFS_TCP_EXPECT_RPCMARKER) {
983 mtx_unlock(&nmp->nm_mtx);
984 if (!nfstcp_marker_readable(so)) {
985 /* Marker is not readable */
988 auio.uio_resid = sizeof(u_int32_t);
992 rcvflg = (MSG_DONTWAIT | MSG_SOCALLBCK);
993 error = soreceive(so, (struct sockaddr **)0, &auio,
994 &mp, (struct mbuf **)0, &rcvflg);
996 * We've already tested that the socket is readable. 2 cases
997 * here, we either read 0 bytes (client closed connection),
998 * or got some other error. In both cases, we tear down the
1001 if (error || auio.uio_resid > 0) {
1002 if (error && error != ECONNRESET) {
1004 "nfs/tcp clnt: Error %d reading socket, tearing down TCP connection\n",
1007 goto mark_reconnect;
1010 panic("nfs_clnt_tcp_soupcall: Got empty mbuf chain from sorecv\n");
1012 * Sigh. We can't do the obvious thing here (which would
1013 * be to have soreceive copy the length from mbufs for us).
1014 * Calling uiomove() from the context of a socket callback
1015 * (even for kernel-kernel copies) leads to LORs (since
1016 * we hold network locks at this point).
1018 if ((resid = nfs_copy_len(mp, (char *)&len,
1019 sizeof(u_int32_t)))) {
1020 log(LOG_ERR, "%s (%d) from nfs server %s\n",
1021 "Bad RPC HDR length",
1022 (int)(sizeof(u_int32_t) - resid),
1023 nmp->nm_mountp->mnt_stat.f_mntfromname);
1024 goto mark_reconnect;
1026 len = ntohl(len) & ~0x80000000;
1029 * This is SERIOUS! We are out of sync with the sender
1030 * and forcing a disconnect/reconnect is all I can do.
1032 if (len > NFS_MAXPACKET || len == 0) {
1033 log(LOG_ERR, "%s (%d) from nfs server %s\n",
1034 "impossible packet length",
1036 nmp->nm_mountp->mnt_stat.f_mntfromname);
1037 goto mark_reconnect;
1039 mtx_lock(&nmp->nm_mtx);
1040 nmp->nm_nfstcpstate.rpcresid = len;
1041 nmp->nm_nfstcpstate.flags &= ~(NFS_TCP_EXPECT_RPCMARKER);
1042 mtx_unlock(&nmp->nm_mtx);
1044 mtx_unlock(&nmp->nm_mtx);
1047 * Processed RPC marker or no RPC marker to process.
1048 * Pull in and process data.
1050 mtx_lock(&nmp->nm_mtx);
1051 if (nmp->nm_nfstcpstate.rpcresid > 0) {
1052 mtx_unlock(&nmp->nm_mtx);
1053 if (!nfstcp_readable(so, nmp->nm_nfstcpstate.rpcresid)) {
1054 /* All data not readable */
1057 auio.uio_resid = nmp->nm_nfstcpstate.rpcresid;
1058 auio.uio_iov = NULL;
1059 auio.uio_iovcnt = 0;
1061 rcvflg = (MSG_DONTWAIT | MSG_SOCALLBCK);
1062 error = soreceive(so, (struct sockaddr **)0, &auio,
1063 &mp, (struct mbuf **)0, &rcvflg);
1064 if (error || auio.uio_resid > 0) {
1065 if (error && error != ECONNRESET) {
1067 "nfs/tcp clnt: Error %d reading socket, tearing down TCP connection\n",
1070 goto mark_reconnect;
1073 panic("nfs_clnt_tcp_soupcall: Got empty mbuf chain from sorecv\n");
1074 mtx_lock(&nmp->nm_mtx);
1075 nmp->nm_nfstcpstate.rpcresid = 0;
1076 nmp->nm_nfstcpstate.flags |= NFS_TCP_EXPECT_RPCMARKER;
1077 mtx_unlock(&nmp->nm_mtx);
1078 /* We got the entire RPC reply. Match XIDs and wake up requestor */
1079 nfs_clnt_match_xid(so, nmp, mp);
1081 mtx_unlock(&nmp->nm_mtx);
1085 nfs_mark_for_reconnect(nmp);
1089 nfs_clnt_udp_soupcall(struct socket *so, void *arg, int waitflag)
1091 struct nfsmount *nmp = (struct nfsmount *)arg;
1093 struct mbuf *mp = NULL;
1094 struct mbuf *control = NULL;
1097 auio.uio_resid = 1000000;
1098 auio.uio_td = curthread;
1099 rcvflag = MSG_DONTWAIT;
1100 auio.uio_resid = 1000000000;
1102 mp = control = NULL;
1103 error = soreceive(so, NULL, &auio, &mp, &control, &rcvflag);
1107 nfs_clnt_match_xid(so, nmp, mp);
1108 } while (mp && !error);
1112 * nfs_request - goes something like this
1113 * - fill in request struct
1114 * - links it into list
1115 * - calls nfs_send() for first transmit
1116 * - calls nfs_receive() to get reply
1117 * - break down rpc header and return with nfs reply pointed to
1119 * nb: always frees up mreq mbuf list
1122 nfs_request(struct vnode *vp, struct mbuf *mrest, int procnum,
1123 struct thread *td, struct ucred *cred, struct mbuf **mrp,
1124 struct mbuf **mdp, caddr_t *dposp)
1126 struct mbuf *mrep, *m2;
1130 struct nfsmount *nmp;
1131 struct mbuf *m, *md, *mheadend;
1134 int error = 0, mrest_len, auth_len, auth_type;
1138 /* Reject requests while attempting a forced unmount. */
1139 if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF) {
1143 nmp = VFSTONFS(vp->v_mount);
1144 if ((nmp->nm_flag & NFSMNT_NFSV4) != 0)
1145 return nfs4_request(vp, mrest, procnum, td, cred, mrp, mdp, dposp);
1146 MALLOC(rep, struct nfsreq *, sizeof(struct nfsreq), M_NFSREQ, M_WAITOK);
1147 bzero(rep, sizeof(struct nfsreq));
1151 rep->r_procnum = procnum;
1152 mtx_init(&rep->r_mtx, "NFSrep lock", NULL, MTX_DEF);
1154 getmicrouptime(&now);
1155 rep->r_lastmsg = now.tv_sec -
1156 ((nmp->nm_tprintf_delay) - (nmp->nm_tprintf_initial_delay));
1157 mrest_len = m_length(mrest, NULL);
1160 * Get the RPC header with authorization.
1162 auth_type = RPCAUTH_UNIX;
1163 if (cred->cr_ngroups < 1)
1164 panic("nfsreq nogrps");
1165 auth_len = ((((cred->cr_ngroups - 1) > nmp->nm_numgrps) ?
1166 nmp->nm_numgrps : (cred->cr_ngroups - 1)) << 2) +
1168 m = nfsm_rpchead(cred, nmp->nm_flag, procnum, auth_type, auth_len,
1169 mrest, mrest_len, &mheadend, &xidp);
1172 * For stream protocols, insert a Sun RPC Record Mark.
1174 if (nmp->nm_sotype == SOCK_STREAM) {
1175 M_PREPEND(m, NFSX_UNSIGNED, M_TRYWAIT);
1176 *mtod(m, u_int32_t *) = htonl(0x80000000 |
1177 (m->m_pkthdr.len - NFSX_UNSIGNED));
1182 if (nmp->nm_flag & NFSMNT_SOFT)
1183 rep->r_retry = nmp->nm_retry;
1185 rep->r_retry = NFS_MAXREXMIT + 1; /* past clip limit */
1186 rep->r_rtt = rep->r_rexmit = 0;
1187 if (nfs_rto_timer(procnum) != NFS_DEFAULT_TIMER)
1188 rep->r_flags = R_TIMING;
1194 * Do the client side RPC.
1196 nfsstats.rpcrequests++;
1198 * Chain request into list of outstanding requests. Be sure
1199 * to put it LAST so timer finds oldest requests first.
1201 mtx_lock(&nfs_reqq_mtx);
1202 if (TAILQ_EMPTY(&nfs_reqq))
1203 callout_reset(&nfs_callout, nfs_ticks, nfs_timer, NULL);
1204 TAILQ_INSERT_TAIL(&nfs_reqq, rep, r_chain);
1205 mtx_unlock(&nfs_reqq_mtx);
1208 * If backing off another request or avoiding congestion, don't
1209 * send this one now but let timer do it. If not timing a request,
1212 mtx_lock(&nmp->nm_mtx);
1214 (((nmp->nm_sotype == SOCK_STREAM) && !(nmp->nm_nfstcpstate.flags & NFS_TCP_FORCE_RECONNECT)) ||
1215 (nmp->nm_flag & NFSMNT_DUMBTIMR) || nmp->nm_sent < nmp->nm_cwnd)) {
1216 if (nmp->nm_sotype == SOCK_STREAM)
1217 nmp->nm_nfstcpstate.sock_send_inprog++;
1218 mtx_unlock(&nmp->nm_mtx);
1219 m2 = m_copym(m, 0, M_COPYALL, M_TRYWAIT);
1220 error = nfs_send(nmp->nm_so, nmp->nm_nam, m2, rep);
1221 mtx_lock(&nmp->nm_mtx);
1222 mtx_lock(&rep->r_mtx);
1224 * nfs_timer() could've re-transmitted the request if we ended up
1225 * blocking on nfs_send() too long, so check for R_SENT here.
1227 if (!error && (rep->r_flags & (R_SENT | R_MUSTRESEND)) == 0) {
1228 nmp->nm_sent += NFS_CWNDSCALE;
1229 rep->r_flags |= R_SENT;
1231 mtx_unlock(&rep->r_mtx);
1232 if (nmp->nm_sotype == SOCK_STREAM)
1233 nfs_wakup_reconnectors(rep->r_nmp);
1234 mtx_unlock(&nmp->nm_mtx);
1236 mtx_unlock(&nmp->nm_mtx);
1241 * Wait for the reply from our send or the timer's.
1243 if (!error || error == EPIPE)
1244 error = nfs_reply(rep);
1247 * nfs_timer() may be in the process of re-transmitting this request.
1248 * nfs_timer() drops the nfs_reqq_mtx before the pru_send() (to avoid LORs).
1249 * Wait till nfs_timer() completes the re-transmission. When the reply
1250 * comes back, it will be discarded (since the req struct for it no longer
1253 wait_for_pinned_req:
1254 mtx_lock(&rep->r_mtx);
1255 while (rep->r_flags & R_PIN_REQ) {
1256 msleep((caddr_t)&rep->r_flags, &rep->r_mtx,
1257 (PZERO - 1), "nfsrxmt", 0);
1259 mtx_unlock(&rep->r_mtx);
1261 mtx_lock(&nfs_reqq_mtx);
1262 /* Have to check for R_PIN_REQ after grabbing wlock again */
1263 mtx_lock(&rep->r_mtx);
1264 if (rep->r_flags & R_PIN_REQ) {
1265 mtx_unlock(&rep->r_mtx);
1266 mtx_unlock(&nfs_reqq_mtx);
1267 goto wait_for_pinned_req;
1269 mtx_unlock(&rep->r_mtx);
1270 /* RPC done (timer not active, request not pinned), unlink the request */
1271 TAILQ_REMOVE(&nfs_reqq, rep, r_chain);
1272 if (TAILQ_EMPTY(&nfs_reqq))
1273 callout_stop(&nfs_callout);
1274 mtx_unlock(&nfs_reqq_mtx);
1277 * Decrement the outstanding request count.
1279 mtx_lock(&rep->r_mtx);
1280 if (rep->r_flags & R_SENT) {
1281 rep->r_flags &= ~R_SENT; /* paranoia */
1282 mtx_unlock(&rep->r_mtx);
1283 mtx_lock(&nmp->nm_mtx);
1284 nmp->nm_sent -= NFS_CWNDSCALE;
1285 mtx_unlock(&nmp->nm_mtx);
1287 mtx_unlock(&rep->r_mtx);
1290 * If there was a successful reply and a tprintf msg.
1291 * tprintf a response.
1294 nfs_up(rep, nmp, rep->r_td, "is alive again", NFSSTA_TIMEO);
1301 * If we got interrupted by a signal in nfs_reply(), there's
1302 * a very small window where the reply could've come in before
1303 * this process got scheduled in. To handle that case, we need
1304 * to free the reply if it was delivered.
1306 if (rep->r_mrep != NULL)
1307 m_freem(rep->r_mrep);
1308 m_freem(rep->r_mreq);
1309 mtx_destroy(&rep->r_mtx);
1310 free((caddr_t)rep, M_NFSREQ);
1314 if (rep->r_mrep == NULL)
1315 panic("nfs_request: rep->r_mrep shouldn't be NULL if no error\n");
1318 * break down the rpc header and check if ok
1320 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
1321 if (*tl++ == rpc_msgdenied) {
1322 if (*tl == rpc_mismatch)
1327 m_freem(rep->r_mreq);
1328 mtx_destroy(&rep->r_mtx);
1329 free((caddr_t)rep, M_NFSREQ);
1334 * Just throw away any verifyer (ie: kerberos etc).
1336 i = fxdr_unsigned(int, *tl++); /* verf type */
1337 i = fxdr_unsigned(int32_t, *tl); /* len */
1339 nfsm_adv(nfsm_rndup(i));
1340 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
1343 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
1345 error = fxdr_unsigned(int, *tl);
1346 if ((nmp->nm_flag & NFSMNT_NFSV3) &&
1347 error == NFSERR_TRYLATER) {
1350 waituntil = time_second + nfs3_jukebox_delay;
1351 while (time_second < waituntil) {
1352 (void) tsleep(&lbolt, PSOCK, "nqnfstry", 0);
1354 mtx_lock(&nfs_xid_mtx);
1357 rep->r_xid = *xidp = txdr_unsigned(nfs_xid);
1358 mtx_unlock(&nfs_xid_mtx);
1363 * If the File Handle was stale, invalidate the
1364 * lookup cache, just in case.
1366 if (error == ESTALE)
1369 * Skip wcc data on NFS errors for now. NetApp filers return corrupt
1370 * postop attrs in the wcc data for NFS err EROFS. Not sure if they
1371 * could return corrupt postop attrs for others errors.
1373 if ((nmp->nm_flag & NFSMNT_NFSV3) && !nfs_skip_wcc_data_onerr) {
1377 error |= NFSERR_RETERR;
1380 m_freem(rep->r_mreq);
1381 mtx_destroy(&rep->r_mtx);
1382 free((caddr_t)rep, M_NFSREQ);
1389 m_freem(rep->r_mreq);
1390 mtx_destroy(&rep->r_mtx);
1391 FREE((caddr_t)rep, M_NFSREQ);
1395 error = EPROTONOSUPPORT;
1397 m_freem(rep->r_mreq);
1398 mtx_destroy(&rep->r_mtx);
1399 free((caddr_t)rep, M_NFSREQ);
1405 * Scan the nfsreq list and retranmit any requests that have timed out
1406 * To avoid retransmission attempts on STREAM sockets (in the future) make
1407 * sure to set the r_retry field to 0 (implies nm_retry == 0).
1409 * The nfs reqq lock cannot be held while we do the pru_send() because of a
1410 * lock ordering violation. The NFS client socket callback acquires
1411 * inp_lock->nfsreq mutex and pru_send acquires inp_lock. So we drop the
1412 * reqq mutex (and reacquire it after the pru_send()). The req structure
1413 * (for the rexmit) is prevented from being removed by the R_PIN_REQ flag.
1416 nfs_timer(void *arg)
1421 struct nfsmount *nmp;
1426 getmicrouptime(&now);
1427 mtx_lock(&nfs_reqq_mtx);
1428 TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
1430 mtx_lock(&rep->r_mtx);
1431 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM)) {
1432 mtx_unlock(&rep->r_mtx);
1436 * Terminate request if force-unmount in progress.
1437 * Note that NFS could have vfs_busy'ed the mount,
1438 * causing the unmount to wait for the mnt_lock, making
1439 * this bit of logic necessary.
1441 if (rep->r_nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF) {
1443 mtx_unlock(&rep->r_mtx);
1446 mtx_unlock(&rep->r_mtx);
1448 if (nfs_sigintr(nmp, rep, rep->r_td))
1450 mtx_lock(&nmp->nm_mtx);
1451 mtx_lock(&rep->r_mtx);
1452 if (nmp->nm_tprintf_initial_delay != 0 &&
1453 (rep->r_rexmit > 2 || (rep->r_flags & R_RESENDERR)) &&
1454 rep->r_lastmsg + nmp->nm_tprintf_delay < now.tv_sec) {
1455 rep->r_lastmsg = now.tv_sec;
1457 * Pin down the request and drop locks for the acquisition
1458 * of Giant from tprintf() in nfs_down().
1460 rep->r_flags |= R_PIN_REQ;
1461 mtx_unlock(&rep->r_mtx);
1462 mtx_unlock(&nmp->nm_mtx);
1463 mtx_unlock(&nfs_reqq_mtx);
1464 nfs_down(rep, nmp, rep->r_td, "not responding",
1466 mtx_lock(&nfs_reqq_mtx);
1467 mtx_lock(&nmp->nm_mtx);
1468 mtx_lock(&rep->r_mtx);
1469 rep->r_flags &= ~R_PIN_REQ;
1470 wakeup((caddr_t)&rep->r_flags);
1472 if (rep->r_rtt >= 0) {
1474 if (nmp->nm_flag & NFSMNT_DUMBTIMR)
1475 timeo = nmp->nm_timeo;
1477 timeo = nfs_estimate_rto(nmp, rep->r_procnum);
1478 if (nmp->nm_timeouts > 0)
1479 timeo *= nfs_backoff[nmp->nm_timeouts - 1];
1480 if (rep->r_rtt <= timeo) {
1481 mtx_unlock(&rep->r_mtx);
1482 mtx_unlock(&nmp->nm_mtx);
1485 if (nmp->nm_timeouts < NFS_NBACKOFF)
1488 if (rep->r_rexmit >= rep->r_retry) { /* too many */
1489 nfsstats.rpctimeouts++;
1491 mtx_unlock(&rep->r_mtx);
1492 mtx_unlock(&nmp->nm_mtx);
1495 if (nmp->nm_sotype != SOCK_DGRAM) {
1496 if (++rep->r_rexmit > NFS_MAXREXMIT)
1497 rep->r_rexmit = NFS_MAXREXMIT;
1499 * For NFS/TCP, setting R_MUSTRESEND and waking up
1500 * the requester will cause the request to be
1501 * retransmitted (in nfs_reply()), re-connecting
1504 rep->r_flags |= R_MUSTRESEND;
1505 wakeup((caddr_t)rep);
1507 mtx_unlock(&rep->r_mtx);
1508 mtx_unlock(&nmp->nm_mtx);
1511 if ((so = nmp->nm_so) == NULL) {
1512 mtx_unlock(&rep->r_mtx);
1513 mtx_unlock(&nmp->nm_mtx);
1517 * If there is enough space and the window allows..
1519 * Set r_rtt to -1 in case we fail to send it now.
1522 if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len &&
1523 ((nmp->nm_flag & NFSMNT_DUMBTIMR) || (rep->r_flags & R_SENT) ||
1524 nmp->nm_sent < nmp->nm_cwnd)) {
1525 mtx_unlock(&rep->r_mtx);
1526 mtx_unlock(&nmp->nm_mtx);
1527 if ((m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))) {
1529 * Mark the request to indicate that a XMIT is in
1530 * progress to prevent the req structure being
1531 * removed in nfs_request().
1533 mtx_lock(&rep->r_mtx);
1534 rep->r_flags |= R_PIN_REQ;
1535 mtx_unlock(&rep->r_mtx);
1536 mtx_unlock(&nfs_reqq_mtx);
1537 if ((nmp->nm_flag & NFSMNT_NOCONN) == 0)
1538 error = (*so->so_proto->pr_usrreqs->pru_send)
1539 (so, 0, m, NULL, NULL, curthread);
1541 error = (*so->so_proto->pr_usrreqs->pru_send)
1542 (so, 0, m, nmp->nm_nam, NULL,
1544 mtx_lock(&nfs_reqq_mtx);
1545 mtx_lock(&nmp->nm_mtx);
1546 mtx_lock(&rep->r_mtx);
1547 rep->r_flags &= ~R_PIN_REQ;
1548 wakeup((caddr_t)&rep->r_flags);
1550 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error))
1552 rep->r_flags |= R_RESENDERR;
1555 * Iff first send, start timing
1556 * else turn timing off, backoff timer
1557 * and divide congestion window by 2.
1559 rep->r_flags &= ~R_RESENDERR;
1560 if (rep->r_flags & R_SENT) {
1561 rep->r_flags &= ~R_TIMING;
1562 if (++rep->r_rexmit > NFS_MAXREXMIT)
1563 rep->r_rexmit = NFS_MAXREXMIT;
1565 if (nmp->nm_cwnd < NFS_CWNDSCALE)
1566 nmp->nm_cwnd = NFS_CWNDSCALE;
1567 nfsstats.rpcretries++;
1569 rep->r_flags |= R_SENT;
1570 nmp->nm_sent += NFS_CWNDSCALE;
1574 mtx_unlock(&rep->r_mtx);
1575 mtx_unlock(&nmp->nm_mtx);
1578 mtx_unlock(&rep->r_mtx);
1579 mtx_unlock(&nmp->nm_mtx);
1582 mtx_unlock(&nfs_reqq_mtx);
1583 callout_reset(&nfs_callout, nfs_ticks, nfs_timer, NULL);
1587 * Mark all of an nfs mount's outstanding requests with R_SOFTTERM and
1588 * wait for all requests to complete. This is used by forced unmounts
1589 * to terminate any outstanding RPCs.
1592 nfs_nmcancelreqs(nmp)
1593 struct nfsmount *nmp;
1598 mtx_lock(&nfs_reqq_mtx);
1599 TAILQ_FOREACH(req, &nfs_reqq, r_chain) {
1600 mtx_lock(&req->r_mtx);
1601 if (nmp != req->r_nmp || req->r_mrep != NULL ||
1602 (req->r_flags & R_SOFTTERM)) {
1603 mtx_unlock(&req->r_mtx);
1607 mtx_unlock(&req->r_mtx);
1609 mtx_unlock(&nfs_reqq_mtx);
1611 for (i = 0; i < 30; i++) {
1612 mtx_lock(&nfs_reqq_mtx);
1613 TAILQ_FOREACH(req, &nfs_reqq, r_chain) {
1614 if (nmp == req->r_nmp)
1617 mtx_unlock(&nfs_reqq_mtx);
1620 tsleep(&lbolt, PSOCK, "nfscancel", 0);
1626 * Flag a request as being about to terminate (due to NFSMNT_INT/NFSMNT_SOFT).
1627 * The nm_send count is decremented now to avoid deadlocks when the process in
1628 * soreceive() hasn't yet managed to send its own request.
1632 nfs_softterm(struct nfsreq *rep)
1634 KASSERT(mtx_owned(&rep->r_mtx), ("NFS req lock not owned !"));
1635 rep->r_flags |= R_SOFTTERM;
1636 if (rep->r_flags & R_SENT) {
1637 rep->r_nmp->nm_sent -= NFS_CWNDSCALE;
1638 rep->r_flags &= ~R_SENT;
1641 * Request terminated, wakeup the blocked process, so that we
1642 * can return EINTR back.
1644 wakeup((caddr_t)rep);
1648 * Any signal that can interrupt an NFS operation in an intr mount
1649 * should be added to this set. SIGSTOP and SIGKILL cannot be masked.
1651 int nfs_sig_set[] = {
1661 * Check to see if one of the signals in our subset is pending on
1662 * the process (in an intr mount).
1665 nfs_sig_pending(sigset_t set)
1669 for (i = 0 ; i < sizeof(nfs_sig_set)/sizeof(int) ; i++)
1670 if (SIGISMEMBER(set, nfs_sig_set[i]))
1676 * The set/restore sigmask functions are used to (temporarily) overwrite
1677 * the process p_sigmask during an RPC call (for example). These are also
1678 * used in other places in the NFS client that might tsleep().
1681 nfs_set_sigmask(struct thread *td, sigset_t *oldset)
1689 td = curthread; /* XXX */
1691 /* Remove the NFS set of signals from newset */
1693 mtx_lock(&p->p_sigacts->ps_mtx);
1694 for (i = 0 ; i < sizeof(nfs_sig_set)/sizeof(int) ; i++) {
1696 * But make sure we leave the ones already masked
1697 * by the process, ie. remove the signal from the
1698 * temporary signalmask only if it wasn't already
1701 if (!SIGISMEMBER(td->td_sigmask, nfs_sig_set[i]) &&
1702 !SIGISMEMBER(p->p_sigacts->ps_sigignore, nfs_sig_set[i]))
1703 SIGDELSET(newset, nfs_sig_set[i]);
1705 mtx_unlock(&p->p_sigacts->ps_mtx);
1707 kern_sigprocmask(td, SIG_SETMASK, &newset, oldset, 0);
1711 nfs_restore_sigmask(struct thread *td, sigset_t *set)
1714 td = curthread; /* XXX */
1715 kern_sigprocmask(td, SIG_SETMASK, set, NULL, 0);
1719 * NFS wrapper to msleep(), that shoves a new p_sigmask and restores the
1720 * old one after msleep() returns.
1723 nfs_msleep(struct thread *td, void *ident, struct mtx *mtx, int priority, char *wmesg, int timo)
1729 if ((priority & PCATCH) == 0)
1730 return msleep(ident, mtx, priority, wmesg, timo);
1732 td = curthread; /* XXX */
1733 nfs_set_sigmask(td, &oldset);
1734 error = msleep(ident, mtx, priority, wmesg, timo);
1735 nfs_restore_sigmask(td, &oldset);
1741 * Test for a termination condition pending on the process.
1742 * This is used for NFSMNT_INT mounts.
1745 nfs_sigintr(struct nfsmount *nmp, struct nfsreq *rep, struct thread *td)
1750 if ((nmp->nm_flag & NFSMNT_NFSV4) != 0)
1751 return nfs4_sigintr(nmp, rep, td);
1753 mtx_lock(&rep->r_mtx);
1754 if (rep->r_flags & R_SOFTTERM) {
1755 mtx_unlock(&rep->r_mtx);
1758 mtx_unlock(&rep->r_mtx);
1760 /* Terminate all requests while attempting a forced unmount. */
1761 if (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF)
1763 if (!(nmp->nm_flag & NFSMNT_INT))
1769 tmpset = p->p_siglist;
1770 SIGSETOR(tmpset, td->td_siglist);
1771 SIGSETNAND(tmpset, td->td_sigmask);
1772 mtx_lock(&p->p_sigacts->ps_mtx);
1773 SIGSETNAND(tmpset, p->p_sigacts->ps_sigignore);
1774 mtx_unlock(&p->p_sigacts->ps_mtx);
1775 if ((SIGNOTEMPTY(p->p_siglist) || SIGNOTEMPTY(td->td_siglist))
1776 && nfs_sig_pending(tmpset)) {
1785 * Lock a socket against others.
1786 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
1787 * and also to avoid race conditions between the processes with nfs requests
1788 * in progress when a reconnect is necessary.
1791 nfs_connect_lock(struct nfsreq *rep)
1793 int *statep = &rep->r_nmp->nm_state;
1795 int error, slpflag = 0, slptimeo = 0;
1798 if (rep->r_nmp->nm_flag & NFSMNT_INT)
1800 while (*statep & NFSSTA_SNDLOCK) {
1801 error = nfs_sigintr(rep->r_nmp, rep, td);
1805 *statep |= NFSSTA_WANTSND;
1806 (void) msleep(statep, &rep->r_nmp->nm_mtx,
1807 slpflag | (PZERO - 1), "nfsndlck", slptimeo);
1808 if (slpflag == PCATCH) {
1813 *statep |= NFSSTA_SNDLOCK;
1818 * Unlock the stream socket for others.
1821 nfs_connect_unlock(struct nfsreq *rep)
1823 int *statep = &rep->r_nmp->nm_state;
1825 if ((*statep & NFSSTA_SNDLOCK) == 0)
1826 panic("nfs sndunlock");
1827 *statep &= ~NFSSTA_SNDLOCK;
1828 if (*statep & NFSSTA_WANTSND) {
1829 *statep &= ~NFSSTA_WANTSND;
1837 * Check for badly aligned mbuf data and realign by copying the unaligned
1838 * portion of the data into a new mbuf chain and freeing the portions
1839 * of the old chain that were replaced.
1841 * We cannot simply realign the data within the existing mbuf chain
1842 * because the underlying buffers may contain other rpc commands and
1843 * we cannot afford to overwrite them.
1845 * We would prefer to avoid this situation entirely. The situation does
1846 * not occur with NFS/UDP and is supposed to only occassionally occur
1847 * with TCP. Use vfs.nfs.realign_count and realign_test to check this.
1851 nfs_realign(struct mbuf **pm, int hsiz)
1854 struct mbuf *n = NULL;
1858 while ((m = *pm) != NULL) {
1859 if ((m->m_len & 0x3) || (mtod(m, intptr_t) & 0x3)) {
1860 MGET(n, M_DONTWAIT, MT_DATA);
1863 if (m->m_len >= MINCLSIZE) {
1864 MCLGET(n, M_DONTWAIT);
1865 if (n->m_ext.ext_buf == NULL) {
1876 * If n is non-NULL, loop on m copying data, then replace the
1877 * portion of the chain that had to be realigned.
1880 ++nfs_realign_count;
1882 m_copyback(n, off, m->m_len, mtod(m, caddr_t));
1894 nfs_msg(struct thread *td, const char *server, const char *msg, int error)
1898 p = td ? td->td_proc : NULL;
1900 tprintf(p, LOG_INFO, "nfs server %s: %s, error %d\n", server,
1903 tprintf(p, LOG_INFO, "nfs server %s: %s\n", server, msg);
1909 nfs_down(rep, nmp, td, msg, error, flags)
1911 struct nfsmount *nmp;
1918 mtx_lock(&nmp->nm_mtx);
1919 if ((flags & NFSSTA_TIMEO) && !(nmp->nm_state & NFSSTA_TIMEO)) {
1920 nmp->nm_state |= NFSSTA_TIMEO;
1921 mtx_unlock(&nmp->nm_mtx);
1922 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1925 mtx_unlock(&nmp->nm_mtx);
1926 #ifdef NFSSTA_LOCKTIMEO
1927 mtx_lock(&nmp->nm_mtx);
1928 if ((flags & NFSSTA_LOCKTIMEO) && !(nmp->nm_state & NFSSTA_LOCKTIMEO)) {
1929 nmp->nm_state |= NFSSTA_LOCKTIMEO;
1930 mtx_unlock(&nmp->nm_mtx);
1931 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1934 mtx_unlock(&nmp->nm_mtx);
1937 mtx_lock(&rep->r_mtx);
1938 rep->r_flags |= R_TPRINTFMSG;
1939 mtx_unlock(&rep->r_mtx);
1941 nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, error);
1945 nfs_up(rep, nmp, td, msg, flags)
1947 struct nfsmount *nmp;
1952 if (nmp == NULL || rep == NULL)
1954 mtx_lock(&rep->r_mtx);
1955 if ((rep->r_flags & R_TPRINTFMSG) != 0) {
1956 mtx_unlock(&rep->r_mtx);
1957 nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, 0);
1959 mtx_unlock(&rep->r_mtx);
1961 mtx_lock(&nmp->nm_mtx);
1962 if ((flags & NFSSTA_TIMEO) && (nmp->nm_state & NFSSTA_TIMEO)) {
1963 nmp->nm_state &= ~NFSSTA_TIMEO;
1964 mtx_unlock(&nmp->nm_mtx);
1965 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1968 mtx_unlock(&nmp->nm_mtx);
1970 #ifdef NFSSTA_LOCKTIMEO
1971 mtx_lock(&nmp->nm_mtx);
1972 if ((flags & NFSSTA_LOCKTIMEO) && (nmp->nm_state & NFSSTA_LOCKTIMEO)) {
1973 nmp->nm_state &= ~NFSSTA_LOCKTIMEO;
1974 mtx_unlock(&nmp->nm_mtx);
1975 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1978 mtx_unlock(&nmp->nm_mtx);