]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/nfsclient/nfs_socket.c
This commit was generated by cvs2svn to compensate for changes in r168993,
[FreeBSD/FreeBSD.git] / sys / nfsclient / nfs_socket.c
1 /*-
2  * Copyright (c) 1989, 1991, 1993, 1995
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Rick Macklem at The University of Guelph.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
19  *
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
30  * SUCH DAMAGE.
31  *
32  *      @(#)nfs_socket.c        8.5 (Berkeley) 3/30/95
33  */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 /*
39  * Socket operations for use by nfs
40  */
41
42 #include "opt_inet6.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/lock.h>
48 #include <sys/malloc.h>
49 #include <sys/mbuf.h>
50 #include <sys/mount.h>
51 #include <sys/mutex.h>
52 #include <sys/proc.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>
61
62 #include <netinet/in.h>
63 #include <netinet/tcp.h>
64
65 #include <rpc/rpcclnt.h>
66
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>
74
75 #include <nfs4client/nfs4.h>
76
77 #define TRUE    1
78 #define FALSE   0
79
80 extern u_int32_t nfs_xid;
81
82 static int      nfs_realign_test;
83 static int      nfs_realign_count;
84 static int      nfs_bufpackets = 4;
85 static int      nfs_reconnects;
86 static int     nfs3_jukebox_delay = 10;
87 static int     nfs_skip_wcc_data_onerr = 1;
88
89 SYSCTL_DECL(_vfs_nfs);
90
91 SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_test, CTLFLAG_RW, &nfs_realign_test, 0, "");
92 SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_count, CTLFLAG_RW, &nfs_realign_count, 0, "");
93 SYSCTL_INT(_vfs_nfs, OID_AUTO, bufpackets, CTLFLAG_RW, &nfs_bufpackets, 0, "");
94 SYSCTL_INT(_vfs_nfs, OID_AUTO, reconnects, CTLFLAG_RD, &nfs_reconnects, 0,
95     "number of times the nfs client has had to reconnect");
96 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs3_jukebox_delay, CTLFLAG_RW, &nfs3_jukebox_delay, 0,
97            "number of seconds to delay a retry after receiving EJUKEBOX");
98 SYSCTL_INT(_vfs_nfs, OID_AUTO, skip_wcc_data_onerr, CTLFLAG_RW, &nfs_skip_wcc_data_onerr, 0, "");
99
100 /*
101  * There is a congestion window for outstanding rpcs maintained per mount
102  * point. The cwnd size is adjusted in roughly the way that:
103  * Van Jacobson, Congestion avoidance and Control, In "Proceedings of
104  * SIGCOMM '88". ACM, August 1988.
105  * describes for TCP. The cwnd size is chopped in half on a retransmit timeout
106  * and incremented by 1/cwnd when each rpc reply is received and a full cwnd
107  * of rpcs is in progress.
108  * (The sent count and cwnd are scaled for integer arith.)
109  * Variants of "slow start" were tried and were found to be too much of a
110  * performance hit (ave. rtt 3 times larger),
111  * I suspect due to the large rtt that nfs rpcs have.
112  */
113 #define NFS_CWNDSCALE   256
114 #define NFS_MAXCWND     (NFS_CWNDSCALE * 32)
115 #define NFS_NBACKOFF    8
116 static int nfs_backoff[NFS_NBACKOFF] = { 2, 4, 8, 16, 32, 64, 128, 256, };
117 struct callout  nfs_callout;
118
119 static int      nfs_msg(struct thread *, const char *, const char *, int);
120 static int      nfs_realign(struct mbuf **pm, int hsiz);
121 static int      nfs_reply(struct nfsreq *);
122 static void     nfs_softterm(struct nfsreq *rep);
123 static int      nfs_reconnect(struct nfsreq *rep);
124 static void nfs_clnt_tcp_soupcall(struct socket *so, void *arg, int waitflag);
125 static void nfs_clnt_udp_soupcall(struct socket *so, void *arg, int waitflag);
126
127 extern struct mtx nfs_reqq_mtx;
128
129 /*
130  * RTT estimator
131  */
132
133 static enum nfs_rto_timer_t nfs_proct[NFS_NPROCS] = {
134         NFS_DEFAULT_TIMER,      /* NULL */
135         NFS_GETATTR_TIMER,      /* GETATTR */
136         NFS_DEFAULT_TIMER,      /* SETATTR */
137         NFS_LOOKUP_TIMER,       /* LOOKUP */
138         NFS_GETATTR_TIMER,      /* ACCESS */
139         NFS_READ_TIMER,         /* READLINK */
140         NFS_READ_TIMER,         /* READ */
141         NFS_WRITE_TIMER,        /* WRITE */
142         NFS_DEFAULT_TIMER,      /* CREATE */
143         NFS_DEFAULT_TIMER,      /* MKDIR */
144         NFS_DEFAULT_TIMER,      /* SYMLINK */
145         NFS_DEFAULT_TIMER,      /* MKNOD */
146         NFS_DEFAULT_TIMER,      /* REMOVE */
147         NFS_DEFAULT_TIMER,      /* RMDIR */
148         NFS_DEFAULT_TIMER,      /* RENAME */
149         NFS_DEFAULT_TIMER,      /* LINK */
150         NFS_READ_TIMER,         /* READDIR */
151         NFS_READ_TIMER,         /* READDIRPLUS */
152         NFS_DEFAULT_TIMER,      /* FSSTAT */
153         NFS_DEFAULT_TIMER,      /* FSINFO */
154         NFS_DEFAULT_TIMER,      /* PATHCONF */
155         NFS_DEFAULT_TIMER,      /* COMMIT */
156         NFS_DEFAULT_TIMER,      /* NOOP */
157 };
158
159 /*
160  * Choose the correct RTT timer for this NFS procedure.
161  */
162 static inline enum nfs_rto_timer_t
163 nfs_rto_timer(u_int32_t procnum)
164 {
165         return nfs_proct[procnum];
166 }
167
168 /*
169  * Initialize the RTT estimator state for a new mount point.
170  */
171 static void
172 nfs_init_rtt(struct nfsmount *nmp)
173 {
174         int i;
175
176         for (i = 0; i < NFS_MAX_TIMER; i++)
177                 nmp->nm_srtt[i] = NFS_INITRTT;
178         for (i = 0; i < NFS_MAX_TIMER; i++)
179                 nmp->nm_sdrtt[i] = 0;
180 }
181
182 /*
183  * Update a mount point's RTT estimator state using data from the
184  * passed-in request.
185  * 
186  * Use a gain of 0.125 on the mean and a gain of 0.25 on the deviation.
187  *
188  * NB: Since the timer resolution of NFS_HZ is so course, it can often
189  * result in r_rtt == 0. Since r_rtt == N means that the actual RTT is
190  * between N + dt and N + 2 - dt ticks, add 1 before calculating the
191  * update values.
192  */
193 static void
194 nfs_update_rtt(struct nfsreq *rep)
195 {
196         int t1 = rep->r_rtt + 1;
197         int index = nfs_rto_timer(rep->r_procnum) - 1;
198         int *srtt = &rep->r_nmp->nm_srtt[index];
199         int *sdrtt = &rep->r_nmp->nm_sdrtt[index];
200
201         t1 -= *srtt >> 3;
202         *srtt += t1;
203         if (t1 < 0)
204                 t1 = -t1;
205         t1 -= *sdrtt >> 2;
206         *sdrtt += t1;
207 }
208
209 /*
210  * Estimate RTO for an NFS RPC sent via an unreliable datagram.
211  *
212  * Use the mean and mean deviation of RTT for the appropriate type
213  * of RPC for the frequent RPCs and a default for the others.
214  * The justification for doing "other" this way is that these RPCs
215  * happen so infrequently that timer est. would probably be stale.
216  * Also, since many of these RPCs are non-idempotent, a conservative
217  * timeout is desired.
218  *
219  * getattr, lookup - A+2D
220  * read, write     - A+4D
221  * other           - nm_timeo
222  */
223 static int
224 nfs_estimate_rto(struct nfsmount *nmp, u_int32_t procnum)
225 {
226         enum nfs_rto_timer_t timer = nfs_rto_timer(procnum);
227         int index = timer - 1;
228         int rto;
229
230         switch (timer) {
231         case NFS_GETATTR_TIMER:
232         case NFS_LOOKUP_TIMER:
233                 rto = ((nmp->nm_srtt[index] + 3) >> 2) +
234                                 ((nmp->nm_sdrtt[index] + 1) >> 1);
235                 break;
236         case NFS_READ_TIMER:
237         case NFS_WRITE_TIMER:
238                 rto = ((nmp->nm_srtt[index] + 7) >> 3) +
239                                 (nmp->nm_sdrtt[index] + 1);
240                 break;
241         default:
242                 rto = nmp->nm_timeo;
243                 return (rto);
244         }
245
246         if (rto < NFS_MINRTO)
247                 rto = NFS_MINRTO;
248         else if (rto > NFS_MAXRTO)
249                 rto = NFS_MAXRTO;
250
251         return (rto);
252 }
253
254
255 /*
256  * Initialize sockets and congestion for a new NFS connection.
257  * We do not free the sockaddr if error.
258  */
259 int
260 nfs_connect(struct nfsmount *nmp, struct nfsreq *rep)
261 {
262         struct socket *so;
263         int error, rcvreserve, sndreserve;
264         int pktscale;
265         struct sockaddr *saddr;
266         struct thread *td = &thread0; /* only used for socreate and sobind */
267
268         NET_LOCK_GIANT();
269
270         if (nmp->nm_sotype == SOCK_STREAM) {
271                 mtx_lock(&nmp->nm_mtx);
272                 nmp->nm_nfstcpstate.flags |= NFS_TCP_EXPECT_RPCMARKER;
273                 nmp->nm_nfstcpstate.rpcresid = 0;
274                 mtx_unlock(&nmp->nm_mtx);
275         }       
276         nmp->nm_so = NULL;
277         saddr = nmp->nm_nam;
278         error = socreate(saddr->sa_family, &nmp->nm_so, nmp->nm_sotype,
279                 nmp->nm_soproto, nmp->nm_mountp->mnt_cred, td);
280         if (error)
281                 goto bad;
282         so = nmp->nm_so;
283         nmp->nm_soflags = so->so_proto->pr_flags;
284
285         /*
286          * Some servers require that the client port be a reserved port number.
287          */
288         if (nmp->nm_flag & NFSMNT_RESVPORT) {
289                 struct sockopt sopt;
290                 int ip, ip2, len;
291                 struct sockaddr_in6 ssin;
292                 struct sockaddr *sa;
293
294                 bzero(&sopt, sizeof sopt);
295                 switch(saddr->sa_family) {
296                 case AF_INET:
297                         sopt.sopt_level = IPPROTO_IP;
298                         sopt.sopt_name = IP_PORTRANGE;
299                         ip = IP_PORTRANGE_LOW;
300                         ip2 = IP_PORTRANGE_DEFAULT;
301                         len = sizeof (struct sockaddr_in);
302                         break;
303 #ifdef INET6
304                 case AF_INET6:
305                         sopt.sopt_level = IPPROTO_IPV6;
306                         sopt.sopt_name = IPV6_PORTRANGE;
307                         ip = IPV6_PORTRANGE_LOW;
308                         ip2 = IPV6_PORTRANGE_DEFAULT;
309                         len = sizeof (struct sockaddr_in6);
310                         break;
311 #endif
312                 default:
313                         goto noresvport;
314                 }
315                 sa = (struct sockaddr *)&ssin;
316                 bzero(sa, len);
317                 sa->sa_len = len;
318                 sa->sa_family = saddr->sa_family;
319                 sopt.sopt_dir = SOPT_SET;
320                 sopt.sopt_val = (void *)&ip;
321                 sopt.sopt_valsize = sizeof(ip);
322                 error = sosetopt(so, &sopt);
323                 if (error)
324                         goto bad;
325                 error = sobind(so, sa, td);
326                 if (error)
327                         goto bad;
328                 ip = ip2;
329                 error = sosetopt(so, &sopt);
330                 if (error)
331                         goto bad;
332         noresvport: ;
333         }
334
335         /*
336          * Protocols that do not require connections may be optionally left
337          * unconnected for servers that reply from a port other than NFS_PORT.
338          */
339         mtx_lock(&nmp->nm_mtx);
340         if (nmp->nm_flag & NFSMNT_NOCONN) {
341                 if (nmp->nm_soflags & PR_CONNREQUIRED) {
342                         error = ENOTCONN;
343                         mtx_unlock(&nmp->nm_mtx);
344                         goto bad;
345                 } else
346                         mtx_unlock(&nmp->nm_mtx);
347         } else {
348                 mtx_unlock(&nmp->nm_mtx);
349                 error = soconnect(so, nmp->nm_nam, td);
350                 if (error)
351                         goto bad;
352
353                 /*
354                  * Wait for the connection to complete. Cribbed from the
355                  * connect system call but with the wait timing out so
356                  * that interruptible mounts don't hang here for a long time.
357                  */
358                 SOCK_LOCK(so);
359                 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
360                         (void) msleep(&so->so_timeo, SOCK_MTX(so),
361                             PSOCK, "nfscon", 2 * hz);
362                         if ((so->so_state & SS_ISCONNECTING) &&
363                             so->so_error == 0 && rep &&
364                             (error = nfs_sigintr(nmp, rep, rep->r_td)) != 0) {
365                                 so->so_state &= ~SS_ISCONNECTING;
366                                 SOCK_UNLOCK(so);
367                                 goto bad;
368                         }
369                 }
370                 if (so->so_error) {
371                         error = so->so_error;
372                         so->so_error = 0;
373                         SOCK_UNLOCK(so);
374                         goto bad;
375                 }
376                 SOCK_UNLOCK(so);
377         }
378         so->so_rcv.sb_timeo = 12 * hz;
379         so->so_snd.sb_timeo = 5 * hz;
380
381         /*
382          * Get buffer reservation size from sysctl, but impose reasonable
383          * limits.
384          */
385         pktscale = nfs_bufpackets;
386         if (pktscale < 2)
387                 pktscale = 2;
388         if (pktscale > 64)
389                 pktscale = 64;
390         mtx_lock(&nmp->nm_mtx);
391         if (nmp->nm_sotype == SOCK_DGRAM) {
392                 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * pktscale;
393                 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
394                     NFS_MAXPKTHDR) * pktscale;
395         } else if (nmp->nm_sotype == SOCK_SEQPACKET) {
396                 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * pktscale;
397                 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
398                     NFS_MAXPKTHDR) * pktscale;
399         } else {
400                 if (nmp->nm_sotype != SOCK_STREAM)
401                         panic("nfscon sotype");
402                 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
403                         struct sockopt sopt;
404                         int val;
405
406                         bzero(&sopt, sizeof sopt);
407                         sopt.sopt_dir = SOPT_SET;
408                         sopt.sopt_level = SOL_SOCKET;
409                         sopt.sopt_name = SO_KEEPALIVE;
410                         sopt.sopt_val = &val;
411                         sopt.sopt_valsize = sizeof val;
412                         val = 1;
413                         mtx_unlock(&nmp->nm_mtx);
414                         sosetopt(so, &sopt);
415                         mtx_lock(&nmp->nm_mtx);
416                 }
417                 if (so->so_proto->pr_protocol == IPPROTO_TCP) {
418                         struct sockopt sopt;
419                         int val;
420
421                         bzero(&sopt, sizeof sopt);
422                         sopt.sopt_dir = SOPT_SET;
423                         sopt.sopt_level = IPPROTO_TCP;
424                         sopt.sopt_name = TCP_NODELAY;
425                         sopt.sopt_val = &val;
426                         sopt.sopt_valsize = sizeof val;
427                         val = 1;
428                         mtx_unlock(&nmp->nm_mtx);
429                         sosetopt(so, &sopt);
430                         mtx_lock(&nmp->nm_mtx);
431                 }
432                 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR +
433                     sizeof (u_int32_t)) * pktscale;
434                 rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR +
435                     sizeof (u_int32_t)) * pktscale;
436         }
437         mtx_unlock(&nmp->nm_mtx);
438         error = soreserve(so, sndreserve, rcvreserve);
439         if (error)
440                 goto bad;
441         SOCKBUF_LOCK(&so->so_rcv);
442         so->so_rcv.sb_flags |= SB_NOINTR;
443         so->so_upcallarg = (caddr_t)nmp;
444         if (so->so_type == SOCK_STREAM)
445                 so->so_upcall = nfs_clnt_tcp_soupcall;
446         else    
447                 so->so_upcall = nfs_clnt_udp_soupcall;
448         so->so_rcv.sb_flags |= SB_UPCALL;
449         SOCKBUF_UNLOCK(&so->so_rcv);
450         SOCKBUF_LOCK(&so->so_snd);
451         so->so_snd.sb_flags |= SB_NOINTR;
452         SOCKBUF_UNLOCK(&so->so_snd);
453
454         mtx_lock(&nmp->nm_mtx);
455         /* Initialize other non-zero congestion variables */
456         nfs_init_rtt(nmp);
457         nmp->nm_cwnd = NFS_MAXCWND / 2;     /* Initial send window */
458         nmp->nm_sent = 0;
459         nmp->nm_timeouts = 0;
460         mtx_unlock(&nmp->nm_mtx);
461         NET_UNLOCK_GIANT();
462         return (0);
463
464 bad:
465         nfs_disconnect(nmp);
466         NET_UNLOCK_GIANT();
467         return (error);
468 }
469
470 /*
471  * Reconnect routine:
472  * Called when a connection is broken on a reliable protocol.
473  * - clean up the old socket
474  * - nfs_connect() again
475  * - set R_MUSTRESEND for all outstanding requests on mount point
476  * If this fails the mount point is DEAD!
477  * nb: Must be called with the nfs_sndlock() set on the mount point.
478  */
479 static int
480 nfs_reconnect(struct nfsreq *rep)
481 {
482         struct nfsreq *rp;
483         struct nfsmount *nmp = rep->r_nmp;
484         int error;
485
486         nfs_reconnects++;
487         nfs_disconnect(nmp);
488         while ((error = nfs_connect(nmp, rep)) != 0) {
489                 if (error == ERESTART)
490                         error = EINTR;
491                 if (error == EIO || error == EINTR)
492                         return (error);
493                 (void) tsleep(&lbolt, PSOCK, "nfscon", 0);
494         }
495
496         /*
497          * Clear the FORCE_RECONNECT flag only after the connect 
498          * succeeds. To prevent races between multiple processes 
499          * waiting on the mountpoint where the connection is being
500          * torn down. The first one to acquire the sndlock will 
501          * retry the connection. The others block on the sndlock
502          * until the connection is established successfully, and 
503          * then re-transmit the request.
504          */
505         mtx_lock(&nmp->nm_mtx);
506         nmp->nm_nfstcpstate.flags &= ~NFS_TCP_FORCE_RECONNECT;
507         nmp->nm_nfstcpstate.rpcresid = 0;
508         mtx_unlock(&nmp->nm_mtx);       
509
510         /*
511          * Loop through outstanding request list and fix up all requests
512          * on old socket.
513          */
514         mtx_lock(&nfs_reqq_mtx);
515         TAILQ_FOREACH(rp, &nfs_reqq, r_chain) {
516                 if (rp->r_nmp == nmp) {
517                         mtx_lock(&rp->r_mtx);                   
518                         rp->r_flags |= R_MUSTRESEND;
519                         mtx_unlock(&rp->r_mtx);
520                 }
521         }
522         mtx_unlock(&nfs_reqq_mtx);
523         return (0);
524 }
525
526 /*
527  * NFS disconnect. Clean up and unlink.
528  */
529 void
530 nfs_disconnect(struct nfsmount *nmp)
531 {
532         struct socket *so;
533
534         NET_ASSERT_GIANT();
535
536         mtx_lock(&nmp->nm_mtx);
537         if (nmp->nm_so) {
538                 so = nmp->nm_so;
539                 nmp->nm_so = NULL;
540                 mtx_unlock(&nmp->nm_mtx);
541                 SOCKBUF_LOCK(&so->so_rcv);
542                 so->so_upcallarg = NULL;
543                 so->so_upcall = NULL;
544                 so->so_rcv.sb_flags &= ~SB_UPCALL;
545                 SOCKBUF_UNLOCK(&so->so_rcv);
546                 soshutdown(so, SHUT_WR);
547                 soclose(so);
548         } else
549                 mtx_unlock(&nmp->nm_mtx);
550 }
551
552 void
553 nfs_safedisconnect(struct nfsmount *nmp)
554 {
555         struct nfsreq dummyreq;
556
557         bzero(&dummyreq, sizeof(dummyreq));
558         dummyreq.r_nmp = nmp;
559         nfs_disconnect(nmp);
560 }
561
562 /*
563  * This is the nfs send routine. For connection based socket types, it
564  * must be called with an nfs_sndlock() on the socket.
565  * - return EINTR if the RPC is terminated, 0 otherwise
566  * - set R_MUSTRESEND if the send fails for any reason
567  * - do any cleanup required by recoverable socket errors (?)
568  */
569 int
570 nfs_send(struct socket *so, struct sockaddr *nam, struct mbuf *top,
571     struct nfsreq *rep)
572 {
573         struct sockaddr *sendnam;
574         int error, error2, soflags, flags;
575
576         NET_LOCK_GIANT();
577
578         KASSERT(rep, ("nfs_send: called with rep == NULL"));
579
580         error = nfs_sigintr(rep->r_nmp, rep, rep->r_td);
581         if (error) {
582                 m_freem(top);
583                 goto out;
584         }
585         mtx_lock(&rep->r_nmp->nm_mtx);
586         mtx_lock(&rep->r_mtx);
587         if ((so = rep->r_nmp->nm_so) == NULL) {
588                 rep->r_flags |= R_MUSTRESEND;
589                 mtx_unlock(&rep->r_mtx);
590                 mtx_unlock(&rep->r_nmp->nm_mtx);
591                 m_freem(top);
592                 error = 0;
593                 goto out;
594         }
595         rep->r_flags &= ~R_MUSTRESEND;
596         soflags = rep->r_nmp->nm_soflags;
597         mtx_unlock(&rep->r_mtx);
598         mtx_unlock(&rep->r_nmp->nm_mtx);
599
600         if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED))
601                 sendnam = NULL;
602         else
603                 sendnam = nam;
604         if (so->so_type == SOCK_SEQPACKET)
605                 flags = MSG_EOR;
606         else
607                 flags = 0;
608
609         error = sosend(so, sendnam, 0, top, 0, flags, curthread /*XXX*/);
610         if (error == ENOBUFS && so->so_type == SOCK_DGRAM) {
611                 error = 0;
612                 mtx_lock(&rep->r_mtx);
613                 rep->r_flags |= R_MUSTRESEND;
614                 mtx_unlock(&rep->r_mtx);
615         }
616
617         if (error) {
618                 /*
619                  * Don't report EPIPE errors on nfs sockets.
620                  * These can be due to idle tcp mounts which will be closed by
621                  * netapp, solaris, etc. if left idle too long.
622                  */
623                 if (error != EPIPE) {
624                         log(LOG_INFO, "nfs send error %d for server %s\n",
625                             error,
626                             rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
627                 }
628                 /*
629                  * Deal with errors for the client side.
630                  */
631                 error2 = NFS_SIGREP(rep);
632                 if (error2)
633                         error = error2;
634                 else {
635                         mtx_lock(&rep->r_mtx);
636                         rep->r_flags |= R_MUSTRESEND;
637                         mtx_unlock(&rep->r_mtx);
638                 }
639
640                 /*
641                  * Handle any recoverable (soft) socket errors here. (?)
642                  * Make EWOULDBLOCK a recoverable error, we'll rexmit from nfs_timer().
643                  */
644                 if (error != EINTR && error != ERESTART && error != EIO && error != EPIPE)
645                         error = 0;
646         }
647 out:
648         NET_UNLOCK_GIANT();
649         return (error);
650 }
651
652 int
653 nfs_reply(struct nfsreq *rep)
654 {
655         register struct socket *so;
656         register struct mbuf *m;
657         int error = 0, sotype, slpflag;
658
659         NET_LOCK_GIANT();
660
661         sotype = rep->r_nmp->nm_sotype;
662         /*
663          * For reliable protocols, lock against other senders/receivers
664          * in case a reconnect is necessary.
665          */
666         if (sotype != SOCK_DGRAM) {
667                 error = nfs_sndlock(rep);
668                 if (error)
669                         goto out;
670 tryagain:
671                 mtx_lock(&rep->r_nmp->nm_mtx);
672                 mtx_lock(&rep->r_mtx);
673                 if (rep->r_mrep) {
674                         mtx_unlock(&rep->r_mtx);
675                         mtx_unlock(&rep->r_nmp->nm_mtx);
676                         nfs_sndunlock(rep);
677                         error = 0;
678                         goto out;
679                 }
680                 if (rep->r_flags & R_SOFTTERM) {
681                         mtx_unlock(&rep->r_mtx);
682                         mtx_unlock(&rep->r_nmp->nm_mtx);
683                         nfs_sndunlock(rep);
684                         error = EINTR;
685                         goto out;
686                 }
687                 so = rep->r_nmp->nm_so;
688                 if (!so || 
689                     (rep->r_nmp->nm_nfstcpstate.flags & NFS_TCP_FORCE_RECONNECT)) {
690                         mtx_unlock(&rep->r_mtx);
691                         mtx_unlock(&rep->r_nmp->nm_mtx);
692                         error = nfs_reconnect(rep);
693                         if (error) {
694                                 nfs_sndunlock(rep);
695                                 goto out;
696                         }
697                         goto tryagain;
698                 }
699                 while (rep->r_flags & R_MUSTRESEND) {
700                         mtx_unlock(&rep->r_mtx);
701                         mtx_unlock(&rep->r_nmp->nm_mtx);
702                         m = m_copym(rep->r_mreq, 0, M_COPYALL, M_WAIT);
703                         nfsstats.rpcretries++;
704                         error = nfs_send(so, rep->r_nmp->nm_nam, m, rep);
705                         if (error) {
706                                 if (error == EINTR || error == ERESTART ||
707                                     (error = nfs_reconnect(rep)) != 0) {
708                                         nfs_sndunlock(rep);
709                                         goto out;
710                                 }
711                                 goto tryagain;
712                         }
713                         mtx_lock(&rep->r_nmp->nm_mtx);
714                         mtx_lock(&rep->r_mtx);
715                 }
716                 mtx_unlock(&rep->r_nmp->nm_mtx);
717                 mtx_unlock(&rep->r_mtx);
718                 nfs_sndunlock(rep);
719         }
720         slpflag = 0;
721         mtx_lock(&rep->r_nmp->nm_mtx);
722         if (rep->r_nmp->nm_flag & NFSMNT_INT)
723                 slpflag = PCATCH;
724         mtx_unlock(&rep->r_nmp->nm_mtx);
725         mtx_lock(&rep->r_mtx);
726         while ((rep->r_mrep == NULL) && (error == 0) && 
727                ((rep->r_flags & R_SOFTTERM) == 0) &&
728                ((sotype == SOCK_DGRAM) || ((rep->r_flags & R_MUSTRESEND) == 0)))
729                 error = msleep((caddr_t)rep, &rep->r_mtx, 
730                                slpflag | (PZERO - 1), "nfsreq", 0);
731         if (error == EINTR || error == ERESTART) {
732                 /* NFS operations aren't restartable. Map ERESTART to EINTR */
733                 error = EINTR;
734                 mtx_unlock(&rep->r_mtx);
735                 goto out;
736         }
737         if (rep->r_flags & R_SOFTTERM) {
738                 /* Request was terminated because we exceeded the retries (soft mount) */
739                 error = ETIMEDOUT;
740                 mtx_unlock(&rep->r_mtx);
741                 goto out;
742         }
743         mtx_unlock(&rep->r_mtx);
744         if (sotype == SOCK_STREAM) {
745                 mtx_lock(&rep->r_nmp->nm_mtx);
746                 mtx_lock(&rep->r_mtx);
747                 if (((rep->r_nmp->nm_nfstcpstate.flags & NFS_TCP_FORCE_RECONNECT) || 
748                      (rep->r_flags & R_MUSTRESEND))) {
749                         mtx_unlock(&rep->r_mtx);
750                         mtx_unlock(&rep->r_nmp->nm_mtx);        
751                         error = nfs_sndlock(rep);
752                         if (error)
753                                 goto out;
754                         goto tryagain;
755                 } else {
756                         mtx_unlock(&rep->r_mtx);
757                         mtx_unlock(&rep->r_nmp->nm_mtx);        
758                 }
759         }
760 out:
761         NET_UNLOCK_GIANT();
762         return (error);
763 }
764
765 /*
766  * XXX TO DO
767  * Make nfs_realign() non-blocking. Also make nfsm_dissect() nonblocking.
768  */
769 static void
770 nfs_clnt_match_xid(struct socket *so, 
771                    struct nfsmount *nmp, 
772                    struct mbuf *mrep)
773 {
774         struct mbuf *md;
775         caddr_t dpos;
776         u_int32_t rxid, *tl;
777         struct nfsreq *rep;
778         int error;
779         
780         /*
781          * Search for any mbufs that are not a multiple of 4 bytes long
782          * or with m_data not longword aligned.
783          * These could cause pointer alignment problems, so copy them to
784          * well aligned mbufs.
785          */
786         if (nfs_realign(&mrep, 5 * NFSX_UNSIGNED) == ENOMEM) {
787                 m_freem(mrep);
788                 nfsstats.rpcinvalid++;
789                 return;
790         }
791         
792         /*
793          * Get the xid and check that it is an rpc reply
794          */
795         md = mrep;
796         dpos = mtod(md, caddr_t);
797         tl = nfsm_dissect_nonblock(u_int32_t *, 2*NFSX_UNSIGNED);
798         rxid = *tl++;
799         if (*tl != rpc_reply) {
800                 m_freem(mrep);
801 nfsmout:
802                 nfsstats.rpcinvalid++;
803                 return;
804         }
805
806         mtx_lock(&nfs_reqq_mtx);
807         /*
808          * Loop through the request list to match up the reply
809          * Iff no match, just drop the datagram
810          */
811         TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
812                 mtx_lock(&nmp->nm_mtx);
813                 mtx_lock(&rep->r_mtx);
814                 if (rep->r_mrep == NULL && rxid == rep->r_xid) {
815                         /* Found it.. */
816                         rep->r_mrep = mrep;
817                         rep->r_md = md;
818                         rep->r_dpos = dpos;
819                         /*
820                          * Update congestion window.
821                          * Do the additive increase of
822                          * one rpc/rtt.
823                          */
824                         if (nmp->nm_cwnd <= nmp->nm_sent) {
825                                 nmp->nm_cwnd +=
826                                         (NFS_CWNDSCALE * NFS_CWNDSCALE +
827                                          (nmp->nm_cwnd >> 1)) / nmp->nm_cwnd;
828                                 if (nmp->nm_cwnd > NFS_MAXCWND)
829                                         nmp->nm_cwnd = NFS_MAXCWND;
830                         }       
831                         if (rep->r_flags & R_SENT) {
832                                 rep->r_flags &= ~R_SENT;
833                                 nmp->nm_sent -= NFS_CWNDSCALE;
834                         }
835                         if (rep->r_flags & R_TIMING)
836                                 nfs_update_rtt(rep);
837                         nmp->nm_timeouts = 0;
838                         wakeup((caddr_t)rep);
839                         mtx_unlock(&rep->r_mtx);
840                         mtx_unlock(&nmp->nm_mtx);
841                         break;
842                 }
843                 mtx_unlock(&rep->r_mtx);
844                 mtx_unlock(&nmp->nm_mtx);
845         }
846         /*
847          * If not matched to a request, drop it.
848          * If it's mine, wake up requestor.
849          */
850         if (rep == 0) {
851                 nfsstats.rpcunexpected++;
852                 m_freem(mrep);
853         }
854         mtx_unlock(&nfs_reqq_mtx);
855 }
856
857 static void
858 nfs_mark_for_reconnect(struct nfsmount *nmp)
859 {
860         struct nfsreq *rp;
861
862         mtx_lock(&nmp->nm_mtx);
863         nmp->nm_nfstcpstate.flags |= NFS_TCP_FORCE_RECONNECT;
864         mtx_unlock(&nmp->nm_mtx);
865         /* 
866          * Wakeup all processes that are waiting for replies 
867          * on this mount point. One of them does the reconnect.
868          */
869         mtx_lock(&nfs_reqq_mtx);
870         TAILQ_FOREACH(rp, &nfs_reqq, r_chain) {
871                 if (rp->r_nmp == nmp) {
872                         mtx_lock(&rp->r_mtx);
873                         rp->r_flags |= R_MUSTRESEND;
874                         wakeup((caddr_t)rp);
875                         mtx_unlock(&rp->r_mtx);
876                 }
877         }
878         mtx_unlock(&nfs_reqq_mtx);
879 }
880
881 static int
882 nfstcp_readable(struct socket *so, int bytes)
883 {
884         int retval;
885         
886         SOCKBUF_LOCK(&so->so_rcv);
887         retval = (so->so_rcv.sb_cc >= (bytes) ||
888                   (so->so_rcv.sb_state & SBS_CANTRCVMORE) ||
889                   so->so_error);
890         SOCKBUF_UNLOCK(&so->so_rcv);
891         return (retval);
892 }
893
894 #define nfstcp_marker_readable(so)      nfstcp_readable(so, sizeof(u_int32_t))
895
896 static int
897 nfs_copy_len(struct mbuf *mp, char *buf, int len)
898 {
899         while (len > 0 && mp != NULL) {
900                 int copylen = min(len, mp->m_len);
901                 
902                 bcopy(mp->m_data, buf, copylen);
903                 buf += copylen;
904                 len -= copylen;
905                 mp = mp->m_next;
906         }
907         return (len);
908 }
909
910 static void
911 nfs_clnt_tcp_soupcall(struct socket *so, void *arg, int waitflag)
912 {
913         struct nfsmount *nmp = (struct nfsmount *)arg;
914         struct mbuf *mp = NULL;
915         struct uio auio;
916         int error;
917         u_int32_t len;
918         int rcvflg;
919
920         /*
921          * Don't pick any more data from the socket if we've marked the 
922          * mountpoint for reconnect.
923          */
924         mtx_lock(&nmp->nm_mtx);
925         if (nmp->nm_nfstcpstate.flags & NFS_TCP_FORCE_RECONNECT) {
926                 mtx_unlock(&nmp->nm_mtx);               
927                 return;
928         } else                  
929                 mtx_unlock(&nmp->nm_mtx);
930         auio.uio_td = curthread;
931         auio.uio_segflg = UIO_SYSSPACE;
932         auio.uio_rw = UIO_READ;
933         for ( ; ; ) {
934                 mtx_lock(&nmp->nm_mtx);
935                 if (nmp->nm_nfstcpstate.flags & NFS_TCP_EXPECT_RPCMARKER) {
936                         int resid;
937
938                         mtx_unlock(&nmp->nm_mtx);
939                         if (!nfstcp_marker_readable(so)) {
940                                 /* Marker is not readable */
941                                 return;
942                         }
943                         auio.uio_resid = sizeof(u_int32_t);
944                         auio.uio_iov = NULL;
945                         auio.uio_iovcnt = 0;
946                         mp = NULL;
947                         rcvflg = (MSG_DONTWAIT | MSG_SOCALLBCK);
948                         error =  soreceive(so, (struct sockaddr **)0, &auio,
949                             &mp, (struct mbuf **)0, &rcvflg);
950                         /*
951                          * We've already tested that the socket is readable. 2 cases 
952                          * here, we either read 0 bytes (client closed connection), 
953                          * or got some other error. In both cases, we tear down the 
954                          * connection.
955                          */
956                         if (error || auio.uio_resid > 0) {
957                                 if (error && error != ECONNRESET) {
958                                         log(LOG_ERR, 
959                                             "nfs/tcp clnt: Error %d reading socket, tearing down TCP connection\n",
960                                             error);
961                                 }
962                                 goto mark_reconnect;
963                         }
964                         if (mp == NULL)
965                                 panic("nfs_clnt_tcp_soupcall: Got empty mbuf chain from sorecv\n");
966                         /*
967                          * Sigh. We can't do the obvious thing here (which would
968                          * be to have soreceive copy the length from mbufs for us).
969                          * Calling uiomove() from the context of a socket callback
970                          * (even for kernel-kernel copies) leads to LORs (since
971                          * we hold network locks at this point).
972                          */
973                         if ((resid = nfs_copy_len(mp, (char *)&len, 
974                                                   sizeof(u_int32_t)))) {
975                                 log(LOG_ERR, "%s (%d) from nfs server %s\n",
976                                     "Bad RPC HDR length",
977                                     (int)(sizeof(u_int32_t) - resid),
978                                     nmp->nm_mountp->mnt_stat.f_mntfromname);
979                                 goto mark_reconnect;
980                         }                               
981                         len = ntohl(len) & ~0x80000000;
982                         m_freem(mp);
983                         /*
984                          * This is SERIOUS! We are out of sync with the sender
985                          * and forcing a disconnect/reconnect is all I can do.
986                          */
987                         if (len > NFS_MAXPACKET || len == 0) {
988                                 log(LOG_ERR, "%s (%d) from nfs server %s\n",
989                                     "impossible packet length",
990                                     len,
991                                     nmp->nm_mountp->mnt_stat.f_mntfromname);
992                                 goto mark_reconnect;
993                         }
994                         mtx_lock(&nmp->nm_mtx);
995                         nmp->nm_nfstcpstate.rpcresid = len;
996                         nmp->nm_nfstcpstate.flags &= ~(NFS_TCP_EXPECT_RPCMARKER);
997                         mtx_unlock(&nmp->nm_mtx);
998                 } else
999                         mtx_unlock(&nmp->nm_mtx);
1000
1001                 /* 
1002                  * Processed RPC marker or no RPC marker to process. 
1003                  * Pull in and process data.
1004                  */
1005                 mtx_lock(&nmp->nm_mtx);
1006                 if (nmp->nm_nfstcpstate.rpcresid > 0) {
1007                         mtx_unlock(&nmp->nm_mtx);
1008                         if (!nfstcp_readable(so, nmp->nm_nfstcpstate.rpcresid)) {
1009                                 /* All data not readable */
1010                                 return;
1011                         }
1012                         auio.uio_resid = nmp->nm_nfstcpstate.rpcresid;
1013                         auio.uio_iov = NULL;
1014                         auio.uio_iovcnt = 0;
1015                         mp = NULL;
1016                         rcvflg = (MSG_DONTWAIT | MSG_SOCALLBCK);
1017                         error =  soreceive(so, (struct sockaddr **)0, &auio,
1018                             &mp, (struct mbuf **)0, &rcvflg);
1019                         if (error || auio.uio_resid > 0) {
1020                                 if (error && error != ECONNRESET) {
1021                                         log(LOG_ERR, 
1022                                             "nfs/tcp clnt: Error %d reading socket, tearing down TCP connection\n",
1023                                             error);
1024                                 }
1025                                 goto mark_reconnect;                            
1026                         }
1027                         if (mp == NULL)
1028                                 panic("nfs_clnt_tcp_soupcall: Got empty mbuf chain from sorecv\n");
1029                         mtx_lock(&nmp->nm_mtx);
1030                         nmp->nm_nfstcpstate.rpcresid = 0;
1031                         nmp->nm_nfstcpstate.flags |= NFS_TCP_EXPECT_RPCMARKER;
1032                         mtx_unlock(&nmp->nm_mtx);
1033                         /* We got the entire RPC reply. Match XIDs and wake up requestor */
1034                         nfs_clnt_match_xid(so, nmp, mp);
1035                 } else
1036                         mtx_unlock(&nmp->nm_mtx);
1037         }
1038
1039 mark_reconnect:
1040         nfs_mark_for_reconnect(nmp);
1041 }
1042
1043 static void
1044 nfs_clnt_udp_soupcall(struct socket *so, void *arg, int waitflag)
1045 {
1046         struct nfsmount *nmp = (struct nfsmount *)arg;
1047         struct uio auio;
1048         struct mbuf *mp = NULL;
1049         struct mbuf *control = NULL;
1050         int error, rcvflag;
1051
1052         auio.uio_resid = 1000000;
1053         auio.uio_td = curthread;
1054         rcvflag = MSG_DONTWAIT;
1055         auio.uio_resid = 1000000000;
1056         do {
1057                 mp = control = NULL;
1058                 error = soreceive(so, NULL, &auio, &mp, &control, &rcvflag);
1059                 if (control)
1060                         m_freem(control);
1061                 if (mp)
1062                         nfs_clnt_match_xid(so, nmp, mp);
1063         } while (mp && !error);
1064 }
1065
1066 /*
1067  * nfs_request - goes something like this
1068  *      - fill in request struct
1069  *      - links it into list
1070  *      - calls nfs_send() for first transmit
1071  *      - calls nfs_receive() to get reply
1072  *      - break down rpc header and return with nfs reply pointed to
1073  *        by mrep or error
1074  * nb: always frees up mreq mbuf list
1075  */
1076 int
1077 nfs_request(struct vnode *vp, struct mbuf *mrest, int procnum,
1078     struct thread *td, struct ucred *cred, struct mbuf **mrp,
1079     struct mbuf **mdp, caddr_t *dposp)
1080 {
1081         struct mbuf *mrep, *m2;
1082         struct nfsreq *rep;
1083         u_int32_t *tl;
1084         int i;
1085         struct nfsmount *nmp;
1086         struct mbuf *m, *md, *mheadend;
1087         time_t waituntil;
1088         caddr_t dpos;
1089         int error = 0, mrest_len, auth_len, auth_type;
1090         struct timeval now;
1091         u_int32_t *xidp;
1092
1093         /* Reject requests while attempting a forced unmount. */
1094         if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF) {
1095                 m_freem(mrest);
1096                 return (ESTALE);
1097         }
1098         nmp = VFSTONFS(vp->v_mount);
1099         if ((nmp->nm_flag & NFSMNT_NFSV4) != 0)
1100                 return nfs4_request(vp, mrest, procnum, td, cred, mrp, mdp, dposp);
1101         MALLOC(rep, struct nfsreq *, sizeof(struct nfsreq), M_NFSREQ, M_WAITOK);
1102         bzero(rep, sizeof(struct nfsreq));
1103         rep->r_nmp = nmp;
1104         rep->r_vp = vp;
1105         rep->r_td = td;
1106         rep->r_procnum = procnum;
1107         mtx_init(&rep->r_mtx, "NFSrep lock", NULL, MTX_DEF);
1108
1109         getmicrouptime(&now);
1110         rep->r_lastmsg = now.tv_sec -
1111             ((nmp->nm_tprintf_delay) - (nmp->nm_tprintf_initial_delay));
1112         mrest_len = m_length(mrest, NULL);
1113
1114         /*
1115          * Get the RPC header with authorization.
1116          */
1117         auth_type = RPCAUTH_UNIX;
1118         if (cred->cr_ngroups < 1)
1119                 panic("nfsreq nogrps");
1120         auth_len = ((((cred->cr_ngroups - 1) > nmp->nm_numgrps) ?
1121                 nmp->nm_numgrps : (cred->cr_ngroups - 1)) << 2) +
1122                 5 * NFSX_UNSIGNED;
1123         m = nfsm_rpchead(cred, nmp->nm_flag, procnum, auth_type, auth_len,
1124              mrest, mrest_len, &mheadend, &xidp);
1125
1126         /*
1127          * For stream protocols, insert a Sun RPC Record Mark.
1128          */
1129         if (nmp->nm_sotype == SOCK_STREAM) {
1130                 M_PREPEND(m, NFSX_UNSIGNED, M_TRYWAIT);
1131                 *mtod(m, u_int32_t *) = htonl(0x80000000 |
1132                          (m->m_pkthdr.len - NFSX_UNSIGNED));
1133         }
1134         rep->r_mreq = m;
1135         rep->r_xid = *xidp;
1136 tryagain:
1137         if (nmp->nm_flag & NFSMNT_SOFT)
1138                 rep->r_retry = nmp->nm_retry;
1139         else
1140                 rep->r_retry = NFS_MAXREXMIT + 1;       /* past clip limit */
1141         rep->r_rtt = rep->r_rexmit = 0;
1142         if (nfs_rto_timer(procnum) != NFS_DEFAULT_TIMER)
1143                 rep->r_flags = R_TIMING;
1144         else
1145                 rep->r_flags = 0;
1146         rep->r_mrep = NULL;
1147
1148         /*
1149          * Do the client side RPC.
1150          */
1151         nfsstats.rpcrequests++;
1152         /*
1153          * Chain request into list of outstanding requests. Be sure
1154          * to put it LAST so timer finds oldest requests first.
1155          */
1156         mtx_lock(&nfs_reqq_mtx);
1157         if (TAILQ_EMPTY(&nfs_reqq))
1158                 callout_reset(&nfs_callout, nfs_ticks, nfs_timer, NULL);
1159         TAILQ_INSERT_TAIL(&nfs_reqq, rep, r_chain);
1160         mtx_unlock(&nfs_reqq_mtx);
1161
1162         /*
1163          * If backing off another request or avoiding congestion, don't
1164          * send this one now but let timer do it. If not timing a request,
1165          * do it now.
1166          */
1167         mtx_lock(&nmp->nm_mtx);
1168         if (nmp->nm_so && (nmp->nm_sotype != SOCK_DGRAM ||
1169                 (nmp->nm_flag & NFSMNT_DUMBTIMR) ||
1170                 nmp->nm_sent < nmp->nm_cwnd)) {
1171                 mtx_unlock(&nmp->nm_mtx);
1172                 error = nfs_sndlock(rep);
1173                 if (!error) {
1174                         m2 = m_copym(m, 0, M_COPYALL, M_TRYWAIT);
1175                         error = nfs_send(nmp->nm_so, nmp->nm_nam, m2, rep);
1176                         nfs_sndunlock(rep);
1177                 }
1178                 mtx_lock(&nfs_reqq_mtx);
1179                 /* 
1180                  * nfs_timer() could've re-transmitted the request if we ended up
1181                  * blocking on nfs_send() too long, so check for R_SENT here.
1182                  */
1183                 if (!error && (rep->r_flags & (R_SENT | R_MUSTRESEND)) == 0) {
1184                         mtx_lock(&nmp->nm_mtx);
1185                         nmp->nm_sent += NFS_CWNDSCALE;
1186                         mtx_unlock(&nmp->nm_mtx);
1187                         rep->r_flags |= R_SENT;
1188                 }
1189                 mtx_unlock(&nfs_reqq_mtx);
1190         } else {
1191                 mtx_unlock(&nmp->nm_mtx);
1192                 rep->r_rtt = -1;
1193         }
1194
1195         /*
1196          * Wait for the reply from our send or the timer's.
1197          */
1198         if (!error || error == EPIPE)
1199                 error = nfs_reply(rep);
1200
1201         /*
1202          * RPC done, unlink the request.
1203          */
1204         mtx_lock(&nfs_reqq_mtx);
1205         /*
1206          * nfs_timer() may be in the process of re-transmitting this request.
1207          * nfs_timer() drops the nfs_reqq_mtx before the pru_send() (to avoid LORs).
1208          * Wait till nfs_timer() completes the re-transmission. When the reply 
1209          * comes back, it will be discarded (since the req struct for it no longer 
1210          * exists).
1211          */
1212         while (rep->r_flags & R_PIN_REQ) {
1213                 msleep((caddr_t)&rep->r_flags, &nfs_reqq_mtx, 
1214                        (PZERO - 1), "nfsrxmt", 0);
1215         }
1216         TAILQ_REMOVE(&nfs_reqq, rep, r_chain);
1217         if (TAILQ_EMPTY(&nfs_reqq))
1218                 callout_stop(&nfs_callout);
1219         /*
1220          * Decrement the outstanding request count.
1221          */
1222         if (rep->r_flags & R_SENT) {
1223                 rep->r_flags &= ~R_SENT;        /* paranoia */
1224                 mtx_lock(&nmp->nm_mtx);
1225                 nmp->nm_sent -= NFS_CWNDSCALE;
1226                 mtx_unlock(&nmp->nm_mtx);
1227         }
1228         mtx_unlock(&nfs_reqq_mtx);
1229
1230         /*
1231          * If there was a successful reply and a tprintf msg.
1232          * tprintf a response.
1233          */
1234         if (!error) {
1235                 nfs_up(rep, nmp, rep->r_td, "is alive again", NFSSTA_TIMEO);
1236         }
1237         mrep = rep->r_mrep;
1238         md = rep->r_md;
1239         dpos = rep->r_dpos;
1240         if (error) {
1241                 /*
1242                  * If we got interrupted by a signal in nfs_reply(), there's
1243                  * a very small window where the reply could've come in before
1244                  * this process got scheduled in. To handle that case, we need 
1245                  * to free the reply if it was delivered.
1246                  */
1247                 if (rep->r_mrep != NULL)
1248                         m_freem(rep->r_mrep);
1249                 m_freem(rep->r_mreq);
1250                 mtx_destroy(&rep->r_mtx);
1251                 free((caddr_t)rep, M_NFSREQ);
1252                 return (error);
1253         }
1254
1255         if (rep->r_mrep == NULL)
1256                 panic("nfs_request: rep->r_mrep shouldn't be NULL if no error\n");
1257
1258         /*
1259          * break down the rpc header and check if ok
1260          */
1261         tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
1262         if (*tl++ == rpc_msgdenied) {
1263                 if (*tl == rpc_mismatch)
1264                         error = EOPNOTSUPP;
1265                 else
1266                         error = EACCES;
1267                 m_freem(mrep);
1268                 m_freem(rep->r_mreq);
1269                 mtx_destroy(&rep->r_mtx);
1270                 free((caddr_t)rep, M_NFSREQ);
1271                 return (error);
1272         }
1273
1274         /*
1275          * Just throw away any verifyer (ie: kerberos etc).
1276          */
1277         i = fxdr_unsigned(int, *tl++);          /* verf type */
1278         i = fxdr_unsigned(int32_t, *tl);        /* len */
1279         if (i > 0)
1280                 nfsm_adv(nfsm_rndup(i));
1281         tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
1282         /* 0 == ok */
1283         if (*tl == 0) {
1284                 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
1285                 if (*tl != 0) {
1286                         error = fxdr_unsigned(int, *tl);
1287                         if ((nmp->nm_flag & NFSMNT_NFSV3) &&
1288                                 error == NFSERR_TRYLATER) {
1289                                 m_freem(mrep);
1290                                 error = 0;
1291                                 waituntil = time_second + nfs3_jukebox_delay;
1292                                 while (time_second < waituntil) {
1293                                         (void) tsleep(&lbolt, PSOCK, "nqnfstry", 0);
1294                                 }
1295                                 mtx_lock(&nfs_reqq_mtx);
1296                                 if (++nfs_xid == 0)
1297                                         nfs_xid++;
1298                                 rep->r_xid = *xidp = txdr_unsigned(nfs_xid);
1299                                 mtx_unlock(&nfs_reqq_mtx);
1300                                 goto tryagain;
1301                         }
1302
1303                         /*
1304                          * If the File Handle was stale, invalidate the
1305                          * lookup cache, just in case.
1306                          */
1307                         if (error == ESTALE)
1308                                 cache_purge(vp);
1309                         /*
1310                          * Skip wcc data on NFS errors for now. NetApp filers return corrupt
1311                          * postop attrs in the wcc data for NFS err EROFS. Not sure if they 
1312                          * could return corrupt postop attrs for others errors.
1313                          */
1314                         if ((nmp->nm_flag & NFSMNT_NFSV3) && !nfs_skip_wcc_data_onerr) {
1315                                 *mrp = mrep;
1316                                 *mdp = md;
1317                                 *dposp = dpos;
1318                                 error |= NFSERR_RETERR;
1319                         } else
1320                                 m_freem(mrep);
1321                         m_freem(rep->r_mreq);
1322                         mtx_destroy(&rep->r_mtx);
1323                         free((caddr_t)rep, M_NFSREQ);
1324                         return (error);
1325                 }
1326
1327                 *mrp = mrep;
1328                 *mdp = md;
1329                 *dposp = dpos;
1330                 m_freem(rep->r_mreq);
1331                 mtx_destroy(&rep->r_mtx);
1332                 FREE((caddr_t)rep, M_NFSREQ);
1333                 return (0);
1334         }
1335         m_freem(mrep);
1336         error = EPROTONOSUPPORT;
1337 nfsmout:
1338         m_freem(rep->r_mreq);
1339         mtx_destroy(&rep->r_mtx);
1340         free((caddr_t)rep, M_NFSREQ);
1341         return (error);
1342 }
1343
1344 /*
1345  * Nfs timer routine
1346  * Scan the nfsreq list and retranmit any requests that have timed out
1347  * To avoid retransmission attempts on STREAM sockets (in the future) make
1348  * sure to set the r_retry field to 0 (implies nm_retry == 0).
1349  * 
1350  * The nfs reqq lock cannot be held while we do the pru_send() because of a
1351  * lock ordering violation. The NFS client socket callback acquires 
1352  * inp_lock->nfsreq mutex and pru_send acquires inp_lock. So we drop the 
1353  * reqq mutex (and reacquire it after the pru_send()). The req structure
1354  * (for the rexmit) is prevented from being removed by the R_PIN_REQ flag.
1355  */
1356 void
1357 nfs_timer(void *arg)
1358 {
1359         struct nfsreq *rep;
1360         struct mbuf *m;
1361         struct socket *so;
1362         struct nfsmount *nmp;
1363         int timeo;
1364         int error;
1365         struct timeval now;
1366
1367         getmicrouptime(&now);
1368         mtx_lock(&nfs_reqq_mtx);
1369         TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
1370                 nmp = rep->r_nmp;
1371                 mtx_lock(&rep->r_mtx);
1372                 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM)) {
1373                         mtx_unlock(&rep->r_mtx);                        
1374                         continue;
1375                 } else {
1376                         /*
1377                          * Terminate request if force-unmount in progress.
1378                          * Note that NFS could have vfs_busy'ed the mount,
1379                          * causing the unmount to wait for the mnt_lock, making
1380                          * this bit of logic necessary.
1381                          */
1382                         if (rep->r_nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF) {
1383                                 nfs_softterm(rep);
1384                                 mtx_unlock(&rep->r_mtx);
1385                                 continue;
1386                         }                               
1387                         mtx_unlock(&rep->r_mtx);                        
1388                 }
1389                 if (nfs_sigintr(nmp, rep, rep->r_td))
1390                         continue;
1391                 mtx_lock(&nmp->nm_mtx);
1392                 mtx_lock(&rep->r_mtx);
1393                 if (nmp->nm_tprintf_initial_delay != 0 &&
1394                     (rep->r_rexmit > 2 || (rep->r_flags & R_RESENDERR)) &&
1395                     rep->r_lastmsg + nmp->nm_tprintf_delay < now.tv_sec) {
1396                         rep->r_lastmsg = now.tv_sec;
1397                         /*
1398                          * Pin down the request and drop locks for the acquisition
1399                          * of Giant from tprintf() in nfs_down().
1400                          */
1401                         rep->r_flags |= R_PIN_REQ;
1402                         mtx_unlock(&rep->r_mtx);
1403                         mtx_unlock(&nmp->nm_mtx);
1404                         mtx_unlock(&nfs_reqq_mtx);
1405                         nfs_down(rep, nmp, rep->r_td, "not responding",
1406                                  0, NFSSTA_TIMEO);
1407                         mtx_lock(&nfs_reqq_mtx);
1408                         mtx_lock(&nmp->nm_mtx);
1409                         mtx_lock(&rep->r_mtx);
1410                         rep->r_flags &= ~R_PIN_REQ;
1411                         wakeup((caddr_t)&rep->r_flags);
1412                 }
1413                 if (rep->r_rtt >= 0) {
1414                         rep->r_rtt++;
1415                         if (nmp->nm_flag & NFSMNT_DUMBTIMR)
1416                                 timeo = nmp->nm_timeo;
1417                         else
1418                                 timeo = nfs_estimate_rto(nmp, rep->r_procnum);
1419                         if (nmp->nm_timeouts > 0)
1420                                 timeo *= nfs_backoff[nmp->nm_timeouts - 1];
1421                         if (rep->r_rtt <= timeo) {
1422                                 mtx_unlock(&rep->r_mtx);
1423                                 mtx_unlock(&nmp->nm_mtx);
1424                                 continue;
1425                         }
1426                         if (nmp->nm_timeouts < NFS_NBACKOFF)
1427                                 nmp->nm_timeouts++;
1428                 }
1429                 if (rep->r_rexmit >= rep->r_retry) {    /* too many */
1430                         nfsstats.rpctimeouts++;
1431                         nfs_softterm(rep);
1432                         mtx_unlock(&rep->r_mtx);
1433                         mtx_unlock(&nmp->nm_mtx);
1434                         continue;
1435                 }
1436                 if (nmp->nm_sotype != SOCK_DGRAM) {
1437                         if (++rep->r_rexmit > NFS_MAXREXMIT)
1438                                 rep->r_rexmit = NFS_MAXREXMIT;
1439                         /*
1440                          * For NFS/TCP, setting R_MUSTRESEND and waking up 
1441                          * the requester will cause the request to be   
1442                          * retransmitted (in nfs_reply()), re-connecting
1443                          * if necessary.
1444                          */
1445                         rep->r_flags |= R_MUSTRESEND;
1446                         wakeup((caddr_t)rep);
1447                         rep->r_rtt = 0;
1448                         mtx_unlock(&rep->r_mtx);
1449                         mtx_unlock(&nmp->nm_mtx);
1450                         continue;
1451                 }
1452                 if ((so = nmp->nm_so) == NULL) {
1453                         mtx_unlock(&rep->r_mtx);
1454                         mtx_unlock(&nmp->nm_mtx);
1455                         continue;
1456                 }
1457                 /*
1458                  * If there is enough space and the window allows..
1459                  *      Resend it
1460                  * Set r_rtt to -1 in case we fail to send it now.
1461                  */
1462                 rep->r_rtt = -1;
1463                 if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len &&
1464                     ((nmp->nm_flag & NFSMNT_DUMBTIMR) || (rep->r_flags & R_SENT) ||
1465                      nmp->nm_sent < nmp->nm_cwnd)) {
1466                         mtx_unlock(&rep->r_mtx);
1467                         mtx_unlock(&nmp->nm_mtx);
1468                         if ((m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))) {
1469                                 /*
1470                                  * Mark the request to indicate that a XMIT is in 
1471                                  * progress to prevent the req structure being 
1472                                  * removed in nfs_request().
1473                                  */
1474                                 mtx_lock(&rep->r_mtx);
1475                                 rep->r_flags |= R_PIN_REQ;
1476                                 mtx_unlock(&rep->r_mtx);
1477                                 mtx_unlock(&nfs_reqq_mtx);
1478                                 NET_LOCK_GIANT();
1479                                 if ((nmp->nm_flag & NFSMNT_NOCONN) == 0)
1480                                         error = (*so->so_proto->pr_usrreqs->pru_send)
1481                                                 (so, 0, m, NULL, NULL, curthread);
1482                                 else    
1483                                         error = (*so->so_proto->pr_usrreqs->pru_send)
1484                                                 (so, 0, m, nmp->nm_nam, NULL, 
1485                                                  curthread);
1486                                 NET_UNLOCK_GIANT();
1487                                 mtx_lock(&nfs_reqq_mtx);
1488                                 mtx_lock(&nmp->nm_mtx);
1489                                 mtx_lock(&rep->r_mtx);
1490                                 rep->r_flags &= ~R_PIN_REQ;
1491                                 wakeup((caddr_t)&rep->r_flags);
1492                                 if (error) {
1493                                         if (NFSIGNORE_SOERROR(nmp->nm_soflags, error))
1494                                                 so->so_error = 0;
1495                                         rep->r_flags |= R_RESENDERR;
1496                                 } else {
1497                                         /*
1498                                          * Iff first send, start timing
1499                                          * else turn timing off, backoff timer
1500                                          * and divide congestion window by 2.
1501                                          */
1502                                         rep->r_flags &= ~R_RESENDERR;
1503                                         if (rep->r_flags & R_SENT) {
1504                                                 rep->r_flags &= ~R_TIMING;
1505                                                 if (++rep->r_rexmit > NFS_MAXREXMIT)
1506                                                         rep->r_rexmit = NFS_MAXREXMIT;
1507                                                 nmp->nm_cwnd >>= 1;
1508                                                 if (nmp->nm_cwnd < NFS_CWNDSCALE)
1509                                                         nmp->nm_cwnd = NFS_CWNDSCALE;
1510                                                 nfsstats.rpcretries++;
1511                                         } else {
1512                                                 rep->r_flags |= R_SENT;
1513                                                 nmp->nm_sent += NFS_CWNDSCALE;
1514                                         }
1515                                         rep->r_rtt = 0;
1516                                 }
1517                                 mtx_unlock(&rep->r_mtx);
1518                                 mtx_unlock(&nmp->nm_mtx);
1519                         }
1520                 } else {
1521                         mtx_unlock(&rep->r_mtx);
1522                         mtx_unlock(&nmp->nm_mtx);
1523                 }
1524         }
1525         mtx_unlock(&nfs_reqq_mtx);
1526         callout_reset(&nfs_callout, nfs_ticks, nfs_timer, NULL);
1527 }
1528
1529 /*
1530  * Mark all of an nfs mount's outstanding requests with R_SOFTTERM and
1531  * wait for all requests to complete. This is used by forced unmounts
1532  * to terminate any outstanding RPCs.
1533  */
1534 int
1535 nfs_nmcancelreqs(nmp)
1536         struct nfsmount *nmp;
1537 {
1538         struct nfsreq *req;
1539         int i;
1540
1541         mtx_lock(&nfs_reqq_mtx);
1542         TAILQ_FOREACH(req, &nfs_reqq, r_chain) {
1543                 mtx_lock(&req->r_mtx);
1544                 if (nmp != req->r_nmp || req->r_mrep != NULL ||
1545                     (req->r_flags & R_SOFTTERM)) {
1546                         mtx_unlock(&req->r_mtx);                        
1547                         continue;
1548                 }
1549                 nfs_softterm(req);
1550                 mtx_unlock(&req->r_mtx);
1551         }
1552         mtx_unlock(&nfs_reqq_mtx);
1553
1554         for (i = 0; i < 30; i++) {
1555                 mtx_lock(&nfs_reqq_mtx);
1556                 TAILQ_FOREACH(req, &nfs_reqq, r_chain) {
1557                         if (nmp == req->r_nmp)
1558                                 break;
1559                 }
1560                 mtx_unlock(&nfs_reqq_mtx);
1561                 if (req == NULL)
1562                         return (0);
1563                 tsleep(&lbolt, PSOCK, "nfscancel", 0);
1564         }
1565         return (EBUSY);
1566 }
1567
1568 /*
1569  * Flag a request as being about to terminate (due to NFSMNT_INT/NFSMNT_SOFT).
1570  * The nm_send count is decremented now to avoid deadlocks when the process in
1571  * soreceive() hasn't yet managed to send its own request.
1572  */
1573
1574 static void
1575 nfs_softterm(struct nfsreq *rep)
1576 {
1577         KASSERT(mtx_owned(&rep->r_mtx), ("NFS req lock not owned !"));
1578         rep->r_flags |= R_SOFTTERM;
1579         if (rep->r_flags & R_SENT) {
1580                 rep->r_nmp->nm_sent -= NFS_CWNDSCALE;
1581                 rep->r_flags &= ~R_SENT;
1582         }
1583         /* 
1584          * Request terminated, wakeup the blocked process, so that we
1585          * can return EINTR back.
1586          */
1587         wakeup((caddr_t)rep);
1588 }
1589
1590 /*
1591  * Any signal that can interrupt an NFS operation in an intr mount
1592  * should be added to this set. SIGSTOP and SIGKILL cannot be masked.
1593  */
1594 int nfs_sig_set[] = {
1595         SIGINT,
1596         SIGTERM,
1597         SIGHUP,
1598         SIGKILL,
1599         SIGSTOP,
1600         SIGQUIT
1601 };
1602
1603 /*
1604  * Check to see if one of the signals in our subset is pending on
1605  * the process (in an intr mount).
1606  */
1607 static int
1608 nfs_sig_pending(sigset_t set)
1609 {
1610         int i;
1611         
1612         for (i = 0 ; i < sizeof(nfs_sig_set)/sizeof(int) ; i++)
1613                 if (SIGISMEMBER(set, nfs_sig_set[i]))
1614                         return (1);
1615         return (0);
1616 }
1617  
1618 /*
1619  * The set/restore sigmask functions are used to (temporarily) overwrite
1620  * the process p_sigmask during an RPC call (for example). These are also
1621  * used in other places in the NFS client that might tsleep().
1622  */
1623 void
1624 nfs_set_sigmask(struct thread *td, sigset_t *oldset)
1625 {
1626         sigset_t newset;
1627         int i;
1628         struct proc *p;
1629         
1630         SIGFILLSET(newset);
1631         if (td == NULL)
1632                 td = curthread; /* XXX */
1633         p = td->td_proc;
1634         /* Remove the NFS set of signals from newset */
1635         PROC_LOCK(p);
1636         mtx_lock(&p->p_sigacts->ps_mtx);
1637         for (i = 0 ; i < sizeof(nfs_sig_set)/sizeof(int) ; i++) {
1638                 /*
1639                  * But make sure we leave the ones already masked
1640                  * by the process, ie. remove the signal from the
1641                  * temporary signalmask only if it wasn't already
1642                  * in p_sigmask.
1643                  */
1644                 if (!SIGISMEMBER(td->td_sigmask, nfs_sig_set[i]) &&
1645                     !SIGISMEMBER(p->p_sigacts->ps_sigignore, nfs_sig_set[i]))
1646                         SIGDELSET(newset, nfs_sig_set[i]);
1647         }
1648         mtx_unlock(&p->p_sigacts->ps_mtx);
1649         PROC_UNLOCK(p);
1650         kern_sigprocmask(td, SIG_SETMASK, &newset, oldset, 0);
1651 }
1652
1653 void
1654 nfs_restore_sigmask(struct thread *td, sigset_t *set)
1655 {
1656         if (td == NULL)
1657                 td = curthread; /* XXX */
1658         kern_sigprocmask(td, SIG_SETMASK, set, NULL, 0);
1659 }
1660
1661 /*
1662  * NFS wrapper to msleep(), that shoves a new p_sigmask and restores the
1663  * old one after msleep() returns.
1664  */
1665 int
1666 nfs_msleep(struct thread *td, void *ident, struct mtx *mtx, int priority, char *wmesg, int timo)
1667 {
1668         sigset_t oldset;
1669         int error;
1670         struct proc *p;
1671         
1672         if ((priority & PCATCH) == 0)
1673                 return msleep(ident, mtx, priority, wmesg, timo);
1674         if (td == NULL)
1675                 td = curthread; /* XXX */
1676         nfs_set_sigmask(td, &oldset);
1677         error = msleep(ident, mtx, priority, wmesg, timo);
1678         nfs_restore_sigmask(td, &oldset);
1679         p = td->td_proc;
1680         return (error);
1681 }
1682
1683 /*
1684  * Test for a termination condition pending on the process.
1685  * This is used for NFSMNT_INT mounts.
1686  */
1687 int
1688 nfs_sigintr(struct nfsmount *nmp, struct nfsreq *rep, struct thread *td)
1689 {
1690         struct proc *p;
1691         sigset_t tmpset;
1692         int error = 0;
1693         
1694         if ((nmp->nm_flag & NFSMNT_NFSV4) != 0)
1695                 return nfs4_sigintr(nmp, rep, td);
1696         if (rep) {
1697                 mtx_lock(&rep->r_mtx);
1698                 if (rep->r_flags & R_SOFTTERM) {
1699                         mtx_unlock(&rep->r_mtx);
1700                         error = EIO;
1701                         goto out;
1702                 } else
1703                         mtx_unlock(&rep->r_mtx);
1704         }
1705         /* Terminate all requests while attempting a forced unmount. */
1706         if (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF) {
1707                 error = EIO;
1708                 goto out;
1709         }
1710         if (!(nmp->nm_flag & NFSMNT_INT))
1711                 goto out;
1712         if (td == NULL)
1713                 return (0);
1714         p = td->td_proc;
1715         PROC_LOCK(p);
1716         tmpset = p->p_siglist;
1717         SIGSETOR(tmpset, td->td_siglist);
1718         SIGSETNAND(tmpset, td->td_sigmask);
1719         mtx_lock(&p->p_sigacts->ps_mtx);
1720         SIGSETNAND(tmpset, p->p_sigacts->ps_sigignore);
1721         mtx_unlock(&p->p_sigacts->ps_mtx);
1722         if ((SIGNOTEMPTY(p->p_siglist) || SIGNOTEMPTY(td->td_siglist))
1723             && nfs_sig_pending(tmpset)) {
1724                 PROC_UNLOCK(p);
1725                 return (EINTR);
1726         }
1727         PROC_UNLOCK(p);
1728
1729         return (0);
1730 out:
1731         return(error);
1732 }
1733
1734 /*
1735  * Lock a socket against others.
1736  * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
1737  * and also to avoid race conditions between the processes with nfs requests
1738  * in progress when a reconnect is necessary.
1739  */
1740 int
1741 nfs_sndlock(struct nfsreq *rep)
1742 {
1743         int *statep = &rep->r_nmp->nm_state;
1744         struct thread *td;
1745         int error, slpflag = 0, slptimeo = 0;
1746
1747         td = rep->r_td;
1748         mtx_lock(&rep->r_nmp->nm_mtx);
1749         if (rep->r_nmp->nm_flag & NFSMNT_INT)
1750                 slpflag = PCATCH;
1751         while (*statep & NFSSTA_SNDLOCK) {
1752                 error = nfs_sigintr(rep->r_nmp, rep, td);
1753                 if (error) {
1754                         mtx_unlock(&rep->r_nmp->nm_mtx);
1755                         return (error);
1756                 }
1757                 *statep |= NFSSTA_WANTSND;
1758                 (void) msleep(statep, &rep->r_nmp->nm_mtx,
1759                               slpflag | (PZERO - 1), "nfsndlck", slptimeo);
1760                 if (slpflag == PCATCH) {
1761                         slpflag = 0;
1762                         slptimeo = 2 * hz;
1763                 }
1764         }
1765         *statep |= NFSSTA_SNDLOCK;
1766         mtx_unlock(&rep->r_nmp->nm_mtx);
1767         return (0);
1768 }
1769
1770 /*
1771  * Unlock the stream socket for others.
1772  */
1773 void
1774 nfs_sndunlock(struct nfsreq *rep)
1775 {
1776         int *statep = &rep->r_nmp->nm_state;
1777
1778         mtx_lock(&rep->r_nmp->nm_mtx);
1779         if ((*statep & NFSSTA_SNDLOCK) == 0)
1780                 panic("nfs sndunlock");
1781         *statep &= ~NFSSTA_SNDLOCK;
1782         if (*statep & NFSSTA_WANTSND) {
1783                 *statep &= ~NFSSTA_WANTSND;
1784                 wakeup(statep);
1785         }
1786         mtx_unlock(&rep->r_nmp->nm_mtx);
1787 }
1788
1789 /*
1790  *      nfs_realign:
1791  *
1792  *      Check for badly aligned mbuf data and realign by copying the unaligned
1793  *      portion of the data into a new mbuf chain and freeing the portions
1794  *      of the old chain that were replaced.
1795  *
1796  *      We cannot simply realign the data within the existing mbuf chain
1797  *      because the underlying buffers may contain other rpc commands and
1798  *      we cannot afford to overwrite them.
1799  *
1800  *      We would prefer to avoid this situation entirely.  The situation does
1801  *      not occur with NFS/UDP and is supposed to only occassionally occur
1802  *      with TCP.  Use vfs.nfs.realign_count and realign_test to check this.
1803  *
1804  */
1805 static int
1806 nfs_realign(struct mbuf **pm, int hsiz)
1807 {
1808         struct mbuf *m;
1809         struct mbuf *n = NULL;
1810         int off = 0;
1811
1812         ++nfs_realign_test;
1813         while ((m = *pm) != NULL) {
1814                 if ((m->m_len & 0x3) || (mtod(m, intptr_t) & 0x3)) {
1815                         MGET(n, M_DONTWAIT, MT_DATA);
1816                         if (n == NULL)
1817                                 return (ENOMEM);
1818                         if (m->m_len >= MINCLSIZE) {
1819                                 MCLGET(n, M_DONTWAIT);
1820                                 if (n->m_ext.ext_buf == NULL) {
1821                                         m_freem(n);
1822                                         return (ENOMEM);
1823                                 }
1824                         }
1825                         n->m_len = 0;
1826                         break;
1827                 }
1828                 pm = &m->m_next;
1829         }
1830         /*
1831          * If n is non-NULL, loop on m copying data, then replace the
1832          * portion of the chain that had to be realigned.
1833          */
1834         if (n != NULL) {
1835                 ++nfs_realign_count;
1836                 while (m) {
1837                         m_copyback(n, off, m->m_len, mtod(m, caddr_t));
1838                         off += m->m_len;
1839                         m = m->m_next;
1840                 }
1841                 m_freem(*pm);
1842                 *pm = n;
1843         }
1844         return (0);
1845 }
1846
1847
1848 static int
1849 nfs_msg(struct thread *td, const char *server, const char *msg, int error)
1850 {
1851         struct proc *p;
1852
1853         p = td ? td->td_proc : NULL;
1854         if (error) {
1855                 tprintf(p, LOG_INFO, "nfs server %s: %s, error %d\n", server,
1856                     msg, error);
1857         } else {
1858                 tprintf(p, LOG_INFO, "nfs server %s: %s\n", server, msg);
1859         }
1860         return (0);
1861 }
1862
1863 void
1864 nfs_down(rep, nmp, td, msg, error, flags)
1865         struct nfsreq *rep;
1866         struct nfsmount *nmp;
1867         struct thread *td;
1868         const char *msg;
1869         int error, flags;
1870 {
1871         if (nmp == NULL)
1872                 return;
1873         mtx_lock(&nmp->nm_mtx);
1874         if ((flags & NFSSTA_TIMEO) && !(nmp->nm_state & NFSSTA_TIMEO)) {
1875                 nmp->nm_state |= NFSSTA_TIMEO;
1876                 mtx_unlock(&nmp->nm_mtx);
1877                 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1878                     VQ_NOTRESP, 0);
1879         } else
1880                 mtx_unlock(&nmp->nm_mtx);
1881 #ifdef NFSSTA_LOCKTIMEO
1882         mtx_lock(&nmp->nm_mtx);
1883         if ((flags & NFSSTA_LOCKTIMEO) && !(nmp->nm_state & NFSSTA_LOCKTIMEO)) {
1884                 nmp->nm_state |= NFSSTA_LOCKTIMEO;
1885                 mtx_unlock(&nmp->nm_mtx);
1886                 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1887                     VQ_NOTRESPLOCK, 0);
1888         } else
1889                 mtx_unlock(&nmp->nm_mtx);
1890 #endif
1891         mtx_lock(&rep->r_mtx);
1892         if (rep)
1893                 rep->r_flags |= R_TPRINTFMSG;
1894         mtx_unlock(&rep->r_mtx);
1895         nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, error);
1896 }
1897
1898 void
1899 nfs_up(rep, nmp, td, msg, flags)
1900         struct nfsreq *rep;
1901         struct nfsmount *nmp;
1902         struct thread *td;
1903         const char *msg;
1904         int flags;
1905 {
1906         if (nmp == NULL || rep == NULL)
1907                 return;
1908         mtx_lock(&rep->r_mtx);
1909         if ((rep->r_flags & R_TPRINTFMSG) != 0) {
1910                 mtx_unlock(&rep->r_mtx);
1911                 nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, 0);
1912         } else
1913                 mtx_unlock(&rep->r_mtx);
1914
1915         mtx_lock(&nmp->nm_mtx);
1916         if ((flags & NFSSTA_TIMEO) && (nmp->nm_state & NFSSTA_TIMEO)) {
1917                 nmp->nm_state &= ~NFSSTA_TIMEO;
1918                 mtx_unlock(&nmp->nm_mtx);
1919                 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1920                     VQ_NOTRESP, 1);
1921         } else
1922                 mtx_unlock(&nmp->nm_mtx);
1923         
1924 #ifdef NFSSTA_LOCKTIMEO
1925         mtx_lock(&nmp->nm_mtx);
1926         if ((flags & NFSSTA_LOCKTIMEO) && (nmp->nm_state & NFSSTA_LOCKTIMEO)) {
1927                 nmp->nm_state &= ~NFSSTA_LOCKTIMEO;
1928                 mtx_unlock(&nmp->nm_mtx);
1929                 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1930                     VQ_NOTRESPLOCK, 1);
1931         } else
1932                 mtx_unlock(&nmp->nm_mtx);
1933 #endif
1934 }