]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/nfsclient/nfs_socket.c
This commit was generated by cvs2svn to compensate for changes in r100513,
[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  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *      @(#)nfs_socket.c        8.5 (Berkeley) 3/30/95
37  */
38
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41
42 /*
43  * Socket operations for use by nfs
44  */
45
46 #include "opt_inet6.h"
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/lock.h>
52 #include <sys/malloc.h>
53 #include <sys/mbuf.h>
54 #include <sys/mount.h>
55 #include <sys/mutex.h>
56 #include <sys/proc.h>
57 #include <sys/protosw.h>
58 #include <sys/signalvar.h>
59 #include <sys/socket.h>
60 #include <sys/socketvar.h>
61 #include <sys/sysctl.h>
62 #include <sys/syslog.h>
63 #include <sys/vnode.h>
64
65 #include <netinet/in.h>
66 #include <netinet/tcp.h>
67
68 #include <nfs/rpcv2.h>
69 #include <nfs/nfsproto.h>
70 #include <nfsclient/nfs.h>
71 #include <nfs/xdr_subs.h>
72 #include <nfsclient/nfsm_subs.h>
73 #include <nfsclient/nfsmount.h>
74 #include <nfsclient/nfsnode.h>
75
76 #define TRUE    1
77 #define FALSE   0
78
79 /*
80  * Estimate rto for an nfs rpc sent via. an unreliable datagram.
81  * Use the mean and mean deviation of rtt for the appropriate type of rpc
82  * for the frequent rpcs and a default for the others.
83  * The justification for doing "other" this way is that these rpcs
84  * happen so infrequently that timer est. would probably be stale.
85  * Also, since many of these rpcs are
86  * non-idempotent, a conservative timeout is desired.
87  * getattr, lookup - A+2D
88  * read, write     - A+4D
89  * other           - nm_timeo
90  */
91 #define NFS_RTO(n, t) \
92         ((t) == 0 ? (n)->nm_timeo : \
93          ((t) < 3 ? \
94           (((((n)->nm_srtt[t-1] + 3) >> 2) + (n)->nm_sdrtt[t-1] + 1) >> 1) : \
95           ((((n)->nm_srtt[t-1] + 7) >> 3) + (n)->nm_sdrtt[t-1] + 1)))
96 #define NFS_SRTT(r)     (r)->r_nmp->nm_srtt[proct[(r)->r_procnum] - 1]
97 #define NFS_SDRTT(r)    (r)->r_nmp->nm_sdrtt[proct[(r)->r_procnum] - 1]
98
99 /*
100  * Defines which timer to use for the procnum.
101  * 0 - default
102  * 1 - getattr
103  * 2 - lookup
104  * 3 - read
105  * 4 - write
106  */
107 static int proct[NFS_NPROCS] = {
108         0, 1, 0, 2, 1, 3, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0,
109 };
110
111 static int      nfs_realign_test;
112 static int      nfs_realign_count;
113 static int      nfs_bufpackets = 4;
114
115 SYSCTL_DECL(_vfs_nfs);
116
117 SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_test, CTLFLAG_RW, &nfs_realign_test, 0, "");
118 SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_count, CTLFLAG_RW, &nfs_realign_count, 0, "");
119 SYSCTL_INT(_vfs_nfs, OID_AUTO, bufpackets, CTLFLAG_RW, &nfs_bufpackets, 0, "");
120
121
122 /*
123  * There is a congestion window for outstanding rpcs maintained per mount
124  * point. The cwnd size is adjusted in roughly the way that:
125  * Van Jacobson, Congestion avoidance and Control, In "Proceedings of
126  * SIGCOMM '88". ACM, August 1988.
127  * describes for TCP. The cwnd size is chopped in half on a retransmit timeout
128  * and incremented by 1/cwnd when each rpc reply is received and a full cwnd
129  * of rpcs is in progress.
130  * (The sent count and cwnd are scaled for integer arith.)
131  * Variants of "slow start" were tried and were found to be too much of a
132  * performance hit (ave. rtt 3 times larger),
133  * I suspect due to the large rtt that nfs rpcs have.
134  */
135 #define NFS_CWNDSCALE   256
136 #define NFS_MAXCWND     (NFS_CWNDSCALE * 32)
137 #define NFS_NBACKOFF    8
138 static int nfs_backoff[NFS_NBACKOFF] = { 2, 4, 8, 16, 32, 64, 128, 256, };
139 struct callout_handle   nfs_timer_handle;
140
141 static int      nfs_msg(struct thread *, char *, char *);
142 static int      nfs_rcvlock(struct nfsreq *);
143 static void     nfs_rcvunlock(struct nfsreq *);
144 static void     nfs_realign(struct mbuf **pm, int hsiz);
145 static int      nfs_receive(struct nfsreq *rep, struct sockaddr **aname,
146                     struct mbuf **mp);
147 static int      nfs_reply(struct nfsreq *);
148 static void     nfs_softterm(struct nfsreq *rep);
149 static int      nfs_reconnect(struct nfsreq *rep);
150
151 /*
152  * Initialize sockets and congestion for a new NFS connection.
153  * We do not free the sockaddr if error.
154  */
155 int
156 nfs_connect(struct nfsmount *nmp, struct nfsreq *rep)
157 {
158         struct socket *so;
159         int s, error, rcvreserve, sndreserve;
160         int pktscale;
161         struct sockaddr *saddr;
162         struct thread *td = &thread0; /* only used for socreate and sobind */
163
164         nmp->nm_so = NULL;
165         saddr = nmp->nm_nam;
166         error = socreate(saddr->sa_family, &nmp->nm_so, nmp->nm_sotype,
167                 nmp->nm_soproto, nmp->nm_cred, td);
168         if (error)
169                 goto bad;
170         so = nmp->nm_so;
171         nmp->nm_soflags = so->so_proto->pr_flags;
172
173         /*
174          * Some servers require that the client port be a reserved port number.
175          */
176         if (nmp->nm_flag & NFSMNT_RESVPORT) {
177                 struct sockopt sopt;
178                 int ip, ip2, len;
179                 struct sockaddr_in6 ssin;
180                 struct sockaddr *sa;
181
182                 bzero(&sopt, sizeof sopt);
183                 switch(saddr->sa_family) {
184                 case AF_INET:
185                         sopt.sopt_level = IPPROTO_IP;
186                         sopt.sopt_name = IP_PORTRANGE;
187                         ip = IP_PORTRANGE_LOW;
188                         ip2 = IP_PORTRANGE_DEFAULT;
189                         len = sizeof (struct sockaddr_in);
190                         break;
191 #ifdef INET6
192                 case AF_INET6:
193                         sopt.sopt_level = IPPROTO_IPV6;
194                         sopt.sopt_name = IPV6_PORTRANGE;
195                         ip = IPV6_PORTRANGE_LOW;
196                         ip2 = IPV6_PORTRANGE_DEFAULT;
197                         len = sizeof (struct sockaddr_in6);
198                         break;
199 #endif
200                 default:
201                         goto noresvport;
202                 }
203                 sa = (struct sockaddr *)&ssin;
204                 bzero(sa, len);
205                 sa->sa_len = len;
206                 sa->sa_family = saddr->sa_family;
207                 sopt.sopt_dir = SOPT_SET;
208                 sopt.sopt_val = (void *)&ip;
209                 sopt.sopt_valsize = sizeof(ip);
210                 error = sosetopt(so, &sopt);
211                 if (error)
212                         goto bad;
213                 error = sobind(so, sa, td);
214                 if (error)
215                         goto bad;
216                 ip = ip2;
217                 error = sosetopt(so, &sopt);
218                 if (error)
219                         goto bad;
220         noresvport: ;
221         }
222
223         /*
224          * Protocols that do not require connections may be optionally left
225          * unconnected for servers that reply from a port other than NFS_PORT.
226          */
227         if (nmp->nm_flag & NFSMNT_NOCONN) {
228                 if (nmp->nm_soflags & PR_CONNREQUIRED) {
229                         error = ENOTCONN;
230                         goto bad;
231                 }
232         } else {
233                 error = soconnect(so, nmp->nm_nam, td);
234                 if (error)
235                         goto bad;
236
237                 /*
238                  * Wait for the connection to complete. Cribbed from the
239                  * connect system call but with the wait timing out so
240                  * that interruptible mounts don't hang here for a long time.
241                  */
242                 s = splnet();
243                 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
244                         (void) tsleep((caddr_t)&so->so_timeo,
245                             PSOCK, "nfscon", 2 * hz);
246                         if ((so->so_state & SS_ISCONNECTING) &&
247                             so->so_error == 0 && rep &&
248                             (error = nfs_sigintr(nmp, rep, rep->r_td)) != 0) {
249                                 so->so_state &= ~SS_ISCONNECTING;
250                                 splx(s);
251                                 goto bad;
252                         }
253                 }
254                 if (so->so_error) {
255                         error = so->so_error;
256                         so->so_error = 0;
257                         splx(s);
258                         goto bad;
259                 }
260                 splx(s);
261         }
262         so->so_rcv.sb_timeo = 5 * hz;
263         so->so_snd.sb_timeo = 5 * hz;
264
265         /*
266          * Get buffer reservation size from sysctl, but impose reasonable
267          * limits.
268          */
269         pktscale = nfs_bufpackets;
270         if (pktscale < 2)
271                 pktscale = 2;
272         if (pktscale > 64)
273                 pktscale = 64;
274
275         if (nmp->nm_sotype == SOCK_DGRAM) {
276                 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * pktscale;
277                 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
278                     NFS_MAXPKTHDR) * pktscale;
279         } else if (nmp->nm_sotype == SOCK_SEQPACKET) {
280                 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * pktscale;
281                 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
282                     NFS_MAXPKTHDR) * pktscale;
283         } else {
284                 if (nmp->nm_sotype != SOCK_STREAM)
285                         panic("nfscon sotype");
286                 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
287                         struct sockopt sopt;
288                         int val;
289
290                         bzero(&sopt, sizeof sopt);
291                         sopt.sopt_level = SOL_SOCKET;
292                         sopt.sopt_name = SO_KEEPALIVE;
293                         sopt.sopt_val = &val;
294                         sopt.sopt_valsize = sizeof val;
295                         val = 1;
296                         sosetopt(so, &sopt);
297                 }
298                 if (so->so_proto->pr_protocol == IPPROTO_TCP) {
299                         struct sockopt sopt;
300                         int val;
301
302                         bzero(&sopt, sizeof sopt);
303                         sopt.sopt_level = IPPROTO_TCP;
304                         sopt.sopt_name = TCP_NODELAY;
305                         sopt.sopt_val = &val;
306                         sopt.sopt_valsize = sizeof val;
307                         val = 1;
308                         sosetopt(so, &sopt);
309                 }
310                 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR +
311                     sizeof (u_int32_t)) * pktscale;
312                 rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR +
313                     sizeof (u_int32_t)) * pktscale;
314         }
315         error = soreserve(so, sndreserve, rcvreserve);
316         if (error)
317                 goto bad;
318         so->so_rcv.sb_flags |= SB_NOINTR;
319         so->so_snd.sb_flags |= SB_NOINTR;
320
321         /* Initialize other non-zero congestion variables */
322         nmp->nm_srtt[0] = nmp->nm_srtt[1] = nmp->nm_srtt[2] =
323                 nmp->nm_srtt[3] = (NFS_TIMEO << 3);
324         nmp->nm_sdrtt[0] = nmp->nm_sdrtt[1] = nmp->nm_sdrtt[2] =
325                 nmp->nm_sdrtt[3] = 0;
326         nmp->nm_cwnd = NFS_MAXCWND / 2;     /* Initial send window */
327         nmp->nm_sent = 0;
328         nmp->nm_timeouts = 0;
329         return (0);
330
331 bad:
332         nfs_disconnect(nmp);
333         return (error);
334 }
335
336 /*
337  * Reconnect routine:
338  * Called when a connection is broken on a reliable protocol.
339  * - clean up the old socket
340  * - nfs_connect() again
341  * - set R_MUSTRESEND for all outstanding requests on mount point
342  * If this fails the mount point is DEAD!
343  * nb: Must be called with the nfs_sndlock() set on the mount point.
344  */
345 static int
346 nfs_reconnect(struct nfsreq *rep)
347 {
348         struct nfsreq *rp;
349         struct nfsmount *nmp = rep->r_nmp;
350         int error;
351
352         nfs_disconnect(nmp);
353         while ((error = nfs_connect(nmp, rep)) != 0) {
354                 if (error == EINTR || error == ERESTART)
355                         return (EINTR);
356                 (void) tsleep((caddr_t)&lbolt, PSOCK, "nfscon", 0);
357         }
358
359         /*
360          * Loop through outstanding request list and fix up all requests
361          * on old socket.
362          */
363         TAILQ_FOREACH(rp, &nfs_reqq, r_chain) {
364                 if (rp->r_nmp == nmp)
365                         rp->r_flags |= R_MUSTRESEND;
366         }
367         return (0);
368 }
369
370 /*
371  * NFS disconnect. Clean up and unlink.
372  */
373 void
374 nfs_disconnect(struct nfsmount *nmp)
375 {
376         struct socket *so;
377
378         if (nmp->nm_so) {
379                 so = nmp->nm_so;
380                 nmp->nm_so = NULL;
381                 soshutdown(so, 2);
382                 soclose(so);
383         }
384 }
385
386 void
387 nfs_safedisconnect(struct nfsmount *nmp)
388 {
389         struct nfsreq dummyreq;
390
391         bzero(&dummyreq, sizeof(dummyreq));
392         dummyreq.r_nmp = nmp;
393         nfs_rcvlock(&dummyreq);
394         nfs_disconnect(nmp);
395         nfs_rcvunlock(&dummyreq);
396 }
397
398 /*
399  * This is the nfs send routine. For connection based socket types, it
400  * must be called with an nfs_sndlock() on the socket.
401  * - return EINTR if the RPC is terminated, 0 otherwise
402  * - set R_MUSTRESEND if the send fails for any reason
403  * - do any cleanup required by recoverable socket errors (?)
404  */
405 int
406 nfs_send(struct socket *so, struct sockaddr *nam, struct mbuf *top,
407     struct nfsreq *rep)
408 {
409         struct sockaddr *sendnam;
410         int error, soflags, flags;
411
412         KASSERT(rep, ("nfs_send: called with rep == NULL"));
413
414         if (rep->r_flags & R_SOFTTERM) {
415                 m_freem(top);
416                 return (EINTR);
417         }
418         if ((so = rep->r_nmp->nm_so) == NULL) {
419                 rep->r_flags |= R_MUSTRESEND;
420                 m_freem(top);
421                 return (0);
422         }
423         rep->r_flags &= ~R_MUSTRESEND;
424         soflags = rep->r_nmp->nm_soflags;
425
426         if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED))
427                 sendnam = NULL;
428         else
429                 sendnam = nam;
430         if (so->so_type == SOCK_SEQPACKET)
431                 flags = MSG_EOR;
432         else
433                 flags = 0;
434
435         error = so->so_proto->pr_usrreqs->pru_sosend(so, sendnam, 0, top, 0,
436                                                      flags, curthread /*XXX*/);
437         if (error == ENOBUFS && so->so_type == SOCK_DGRAM) {
438                 error = 0;
439                 rep->r_flags |= R_MUSTRESEND;
440         }
441
442         if (error) {
443                 log(LOG_INFO, "nfs send error %d for server %s\n", error,
444                     rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
445                 /*
446                  * Deal with errors for the client side.
447                  */
448                 if (rep->r_flags & R_SOFTTERM)
449                         error = EINTR;
450                 else
451                         rep->r_flags |= R_MUSTRESEND;
452
453                 /*
454                  * Handle any recoverable (soft) socket errors here. (?)
455                  */
456                 if (error != EINTR && error != ERESTART &&
457                         error != EWOULDBLOCK && error != EPIPE)
458                         error = 0;
459         }
460         return (error);
461 }
462
463 /*
464  * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all
465  * done by soreceive(), but for SOCK_STREAM we must deal with the Record
466  * Mark and consolidate the data into a new mbuf list.
467  * nb: Sometimes TCP passes the data up to soreceive() in long lists of
468  *     small mbufs.
469  * For SOCK_STREAM we must be very careful to read an entire record once
470  * we have read any of it, even if the system call has been interrupted.
471  */
472 static int
473 nfs_receive(struct nfsreq *rep, struct sockaddr **aname, struct mbuf **mp)
474 {
475         struct socket *so;
476         struct uio auio;
477         struct iovec aio;
478         struct mbuf *m;
479         struct mbuf *control;
480         u_int32_t len;
481         struct sockaddr **getnam;
482         int error, sotype, rcvflg;
483         struct thread *td = curthread;  /* XXX */
484
485         /*
486          * Set up arguments for soreceive()
487          */
488         *mp = NULL;
489         *aname = NULL;
490         sotype = rep->r_nmp->nm_sotype;
491
492         /*
493          * For reliable protocols, lock against other senders/receivers
494          * in case a reconnect is necessary.
495          * For SOCK_STREAM, first get the Record Mark to find out how much
496          * more there is to get.
497          * We must lock the socket against other receivers
498          * until we have an entire rpc request/reply.
499          */
500         if (sotype != SOCK_DGRAM) {
501                 error = nfs_sndlock(rep);
502                 if (error)
503                         return (error);
504 tryagain:
505                 /*
506                  * Check for fatal errors and resending request.
507                  */
508                 /*
509                  * Ugh: If a reconnect attempt just happened, nm_so
510                  * would have changed. NULL indicates a failed
511                  * attempt that has essentially shut down this
512                  * mount point.
513                  */
514                 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM)) {
515                         nfs_sndunlock(rep);
516                         return (EINTR);
517                 }
518                 so = rep->r_nmp->nm_so;
519                 if (!so) {
520                         error = nfs_reconnect(rep);
521                         if (error) {
522                                 nfs_sndunlock(rep);
523                                 return (error);
524                         }
525                         goto tryagain;
526                 }
527                 while (rep->r_flags & R_MUSTRESEND) {
528                         m = m_copym(rep->r_mreq, 0, M_COPYALL, M_TRYWAIT);
529                         nfsstats.rpcretries++;
530                         error = nfs_send(so, rep->r_nmp->nm_nam, m, rep);
531                         if (error) {
532                                 if (error == EINTR || error == ERESTART ||
533                                     (error = nfs_reconnect(rep)) != 0) {
534                                         nfs_sndunlock(rep);
535                                         return (error);
536                                 }
537                                 goto tryagain;
538                         }
539                 }
540                 nfs_sndunlock(rep);
541                 if (sotype == SOCK_STREAM) {
542                         aio.iov_base = (caddr_t) &len;
543                         aio.iov_len = sizeof(u_int32_t);
544                         auio.uio_iov = &aio;
545                         auio.uio_iovcnt = 1;
546                         auio.uio_segflg = UIO_SYSSPACE;
547                         auio.uio_rw = UIO_READ;
548                         auio.uio_offset = 0;
549                         auio.uio_resid = sizeof(u_int32_t);
550                         auio.uio_td = td;
551                         do {
552                            rcvflg = MSG_WAITALL;
553                            error = so->so_proto->pr_usrreqs->pru_soreceive
554                                    (so, NULL, &auio, NULL, NULL, &rcvflg);
555                            if (error == EWOULDBLOCK && rep) {
556                                 if (rep->r_flags & R_SOFTTERM)
557                                         return (EINTR);
558                            }
559                         } while (error == EWOULDBLOCK);
560                         if (!error && auio.uio_resid > 0) {
561                             /*
562                              * Don't log a 0 byte receive; it means
563                              * that the socket has been closed, and
564                              * can happen during normal operation
565                              * (forcible unmount or Solaris server).
566                              */
567                             if (auio.uio_resid != sizeof (u_int32_t))
568                             log(LOG_INFO,
569                                  "short receive (%d/%d) from nfs server %s\n",
570                                  (int)(sizeof(u_int32_t) - auio.uio_resid),
571                                  (int)sizeof(u_int32_t),
572                                  rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
573                             error = EPIPE;
574                         }
575                         if (error)
576                                 goto errout;
577                         len = ntohl(len) & ~0x80000000;
578                         /*
579                          * This is SERIOUS! We are out of sync with the sender
580                          * and forcing a disconnect/reconnect is all I can do.
581                          */
582                         if (len > NFS_MAXPACKET) {
583                             log(LOG_ERR, "%s (%d) from nfs server %s\n",
584                                 "impossible packet length",
585                                 len,
586                                 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
587                             error = EFBIG;
588                             goto errout;
589                         }
590                         auio.uio_resid = len;
591                         do {
592                             rcvflg = MSG_WAITALL;
593                             error =  so->so_proto->pr_usrreqs->pru_soreceive
594                                     (so, NULL,
595                                      &auio, mp, NULL, &rcvflg);
596                         } while (error == EWOULDBLOCK || error == EINTR ||
597                                  error == ERESTART);
598                         if (!error && auio.uio_resid > 0) {
599                             if (len != auio.uio_resid)
600                             log(LOG_INFO,
601                                 "short receive (%d/%d) from nfs server %s\n",
602                                 len - auio.uio_resid, len,
603                                 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
604                             error = EPIPE;
605                         }
606                 } else {
607                         /*
608                          * NB: Since uio_resid is big, MSG_WAITALL is ignored
609                          * and soreceive() will return when it has either a
610                          * control msg or a data msg.
611                          * We have no use for control msg., but must grab them
612                          * and then throw them away so we know what is going
613                          * on.
614                          */
615                         auio.uio_resid = len = 100000000; /* Anything Big */
616                         auio.uio_td = td;
617                         do {
618                             rcvflg = 0;
619                             error =  so->so_proto->pr_usrreqs->pru_soreceive
620                                     (so, NULL,
621                                 &auio, mp, &control, &rcvflg);
622                             if (control)
623                                 m_freem(control);
624                             if (error == EWOULDBLOCK && rep) {
625                                 if (rep->r_flags & R_SOFTTERM)
626                                         return (EINTR);
627                             }
628                         } while (error == EWOULDBLOCK ||
629                                  (!error && *mp == NULL && control));
630                         if ((rcvflg & MSG_EOR) == 0)
631                                 printf("Egad!!\n");
632                         if (!error && *mp == NULL)
633                                 error = EPIPE;
634                         len -= auio.uio_resid;
635                 }
636 errout:
637                 if (error && error != EINTR && error != ERESTART) {
638                         m_freem(*mp);
639                         *mp = NULL;
640                         if (error != EPIPE)
641                                 log(LOG_INFO,
642                                     "receive error %d from nfs server %s\n",
643                                     error,
644                                  rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
645                         error = nfs_sndlock(rep);
646                         if (!error) {
647                                 error = nfs_reconnect(rep);
648                                 if (!error)
649                                         goto tryagain;
650                                 else
651                                         nfs_sndunlock(rep);
652                         }
653                 }
654         } else {
655                 if ((so = rep->r_nmp->nm_so) == NULL)
656                         return (EACCES);
657                 if (so->so_state & SS_ISCONNECTED)
658                         getnam = NULL;
659                 else
660                         getnam = aname;
661                 auio.uio_resid = len = 1000000;
662                 auio.uio_td = td;
663                 do {
664                         rcvflg = 0;
665                         error =  so->so_proto->pr_usrreqs->pru_soreceive
666                                 (so, getnam, &auio, mp,
667                                 NULL, &rcvflg);
668                         if (error == EWOULDBLOCK &&
669                             (rep->r_flags & R_SOFTTERM))
670                                 return (EINTR);
671                 } while (error == EWOULDBLOCK);
672                 len -= auio.uio_resid;
673         }
674         if (error) {
675                 m_freem(*mp);
676                 *mp = NULL;
677         }
678         /*
679          * Search for any mbufs that are not a multiple of 4 bytes long
680          * or with m_data not longword aligned.
681          * These could cause pointer alignment problems, so copy them to
682          * well aligned mbufs.
683          */
684         nfs_realign(mp, 5 * NFSX_UNSIGNED);
685         return (error);
686 }
687
688 /*
689  * Implement receipt of reply on a socket.
690  * We must search through the list of received datagrams matching them
691  * with outstanding requests using the xid, until ours is found.
692  */
693 /* ARGSUSED */
694 static int
695 nfs_reply(struct nfsreq *myrep)
696 {
697         struct nfsreq *rep;
698         struct nfsmount *nmp = myrep->r_nmp;
699         int32_t t1;
700         struct mbuf *mrep, *md;
701         struct sockaddr *nam;
702         u_int32_t rxid, *tl;
703         caddr_t dpos;
704         int error;
705
706         /*
707          * Loop around until we get our own reply
708          */
709         for (;;) {
710                 /*
711                  * Lock against other receivers so that I don't get stuck in
712                  * sbwait() after someone else has received my reply for me.
713                  * Also necessary for connection based protocols to avoid
714                  * race conditions during a reconnect.
715                  * If nfs_rcvlock() returns EALREADY, that means that
716                  * the reply has already been recieved by another
717                  * process and we can return immediately.  In this
718                  * case, the lock is not taken to avoid races with
719                  * other processes.
720                  */
721                 error = nfs_rcvlock(myrep);
722                 if (error == EALREADY)
723                         return (0);
724                 if (error)
725                         return (error);
726                 /*
727                  * Get the next Rpc reply off the socket
728                  */
729                 error = nfs_receive(myrep, &nam, &mrep);
730                 nfs_rcvunlock(myrep);
731                 if (error) {
732
733                         /*
734                          * Ignore routing errors on connectionless protocols??
735                          */
736                         if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
737                                 nmp->nm_so->so_error = 0;
738                                 if (myrep->r_flags & R_GETONEREP)
739                                         return (0);
740                                 continue;
741                         }
742                         return (error);
743                 }
744                 if (nam)
745                         FREE(nam, M_SONAME);
746
747                 /*
748                  * Get the xid and check that it is an rpc reply
749                  */
750                 md = mrep;
751                 dpos = mtod(md, caddr_t);
752                 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED);
753                 rxid = *tl++;
754                 if (*tl != rpc_reply) {
755                         nfsstats.rpcinvalid++;
756                         m_freem(mrep);
757 nfsmout:
758                         if (myrep->r_flags & R_GETONEREP)
759                                 return (0);
760                         continue;
761                 }
762
763                 /*
764                  * Loop through the request list to match up the reply
765                  * Iff no match, just drop the datagram
766                  */
767                 TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
768                         if (rep->r_mrep == NULL && rxid == rep->r_xid) {
769                                 /* Found it.. */
770                                 rep->r_mrep = mrep;
771                                 rep->r_md = md;
772                                 rep->r_dpos = dpos;
773                                 /*
774                                  * Update congestion window.
775                                  * Do the additive increase of
776                                  * one rpc/rtt.
777                                  */
778                                 if (nmp->nm_cwnd <= nmp->nm_sent) {
779                                         nmp->nm_cwnd +=
780                                            (NFS_CWNDSCALE * NFS_CWNDSCALE +
781                                            (nmp->nm_cwnd >> 1)) / nmp->nm_cwnd;
782                                         if (nmp->nm_cwnd > NFS_MAXCWND)
783                                                 nmp->nm_cwnd = NFS_MAXCWND;
784                                 }
785                                 if (rep->r_flags & R_SENT) {
786                                         rep->r_flags &= ~R_SENT;
787                                         nmp->nm_sent -= NFS_CWNDSCALE;
788                                 }
789                                 /*
790                                  * Update rtt using a gain of 0.125 on the mean
791                                  * and a gain of 0.25 on the deviation.
792                                  */
793                                 if (rep->r_flags & R_TIMING) {
794                                         /*
795                                          * Since the timer resolution of
796                                          * NFS_HZ is so course, it can often
797                                          * result in r_rtt == 0. Since
798                                          * r_rtt == N means that the actual
799                                          * rtt is between N+dt and N+2-dt ticks,
800                                          * add 1.
801                                          */
802                                         t1 = rep->r_rtt + 1;
803                                         t1 -= (NFS_SRTT(rep) >> 3);
804                                         NFS_SRTT(rep) += t1;
805                                         if (t1 < 0)
806                                                 t1 = -t1;
807                                         t1 -= (NFS_SDRTT(rep) >> 2);
808                                         NFS_SDRTT(rep) += t1;
809                                 }
810                                 nmp->nm_timeouts = 0;
811                                 break;
812                         }
813                 }
814                 /*
815                  * If not matched to a request, drop it.
816                  * If it's mine, get out.
817                  */
818                 if (rep == 0) {
819                         nfsstats.rpcunexpected++;
820                         m_freem(mrep);
821                 } else if (rep == myrep) {
822                         if (rep->r_mrep == NULL)
823                                 panic("nfsreply nil");
824                         return (0);
825                 }
826                 if (myrep->r_flags & R_GETONEREP)
827                         return (0);
828         }
829 }
830
831 /*
832  * nfs_request - goes something like this
833  *      - fill in request struct
834  *      - links it into list
835  *      - calls nfs_send() for first transmit
836  *      - calls nfs_receive() to get reply
837  *      - break down rpc header and return with nfs reply pointed to
838  *        by mrep or error
839  * nb: always frees up mreq mbuf list
840  */
841 /* XXX overloaded before */
842 #define NQ_TRYLATERDEL  15      /* Initial try later delay (sec) */
843
844 int
845 nfs_request(struct vnode *vp, struct mbuf *mrest, int procnum,
846     struct thread *td, struct ucred *cred, struct mbuf **mrp,
847     struct mbuf **mdp, caddr_t *dposp)
848 {
849         struct mbuf *mrep, *m2;
850         struct nfsreq *rep;
851         u_int32_t *tl;
852         int i;
853         struct nfsmount *nmp;
854         struct mbuf *m, *md, *mheadend;
855         time_t waituntil;
856         caddr_t dpos;
857         int s, error = 0, mrest_len, auth_len, auth_type;
858         int trylater_delay = NQ_TRYLATERDEL, trylater_cnt = 0;
859         u_int32_t xid;
860
861         /* Reject requests while attempting a forced unmount. */
862         if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF) {
863                 m_freem(mrest);
864                 return (ESTALE);
865         }
866         nmp = VFSTONFS(vp->v_mount);
867         MALLOC(rep, struct nfsreq *, sizeof(struct nfsreq), M_NFSREQ, M_WAITOK);
868         rep->r_nmp = nmp;
869         rep->r_vp = vp;
870         rep->r_td = td;
871         rep->r_procnum = procnum;
872         i = 0;
873         m = mrest;
874         while (m) {
875                 i += m->m_len;
876                 m = m->m_next;
877         }
878         mrest_len = i;
879
880         /*
881          * Get the RPC header with authorization.
882          */
883         auth_type = RPCAUTH_UNIX;
884         if (cred->cr_ngroups < 1)
885                 panic("nfsreq nogrps");
886         auth_len = ((((cred->cr_ngroups - 1) > nmp->nm_numgrps) ?
887                 nmp->nm_numgrps : (cred->cr_ngroups - 1)) << 2) +
888                 5 * NFSX_UNSIGNED;
889         m = nfsm_rpchead(cred, nmp->nm_flag, procnum, auth_type, auth_len,
890              mrest, mrest_len, &mheadend, &xid);
891
892         /*
893          * For stream protocols, insert a Sun RPC Record Mark.
894          */
895         if (nmp->nm_sotype == SOCK_STREAM) {
896                 M_PREPEND(m, NFSX_UNSIGNED, M_TRYWAIT);
897                 *mtod(m, u_int32_t *) = htonl(0x80000000 |
898                          (m->m_pkthdr.len - NFSX_UNSIGNED));
899         }
900         rep->r_mreq = m;
901         rep->r_xid = xid;
902 tryagain:
903         if (nmp->nm_flag & NFSMNT_SOFT)
904                 rep->r_retry = nmp->nm_retry;
905         else
906                 rep->r_retry = NFS_MAXREXMIT + 1;       /* past clip limit */
907         rep->r_rtt = rep->r_rexmit = 0;
908         if (proct[procnum] > 0)
909                 rep->r_flags = R_TIMING;
910         else
911                 rep->r_flags = 0;
912         rep->r_mrep = NULL;
913
914         /*
915          * Do the client side RPC.
916          */
917         nfsstats.rpcrequests++;
918         /*
919          * Chain request into list of outstanding requests. Be sure
920          * to put it LAST so timer finds oldest requests first.
921          */
922         s = splsoftclock();
923         TAILQ_INSERT_TAIL(&nfs_reqq, rep, r_chain);
924
925         /*
926          * If backing off another request or avoiding congestion, don't
927          * send this one now but let timer do it. If not timing a request,
928          * do it now.
929          */
930         if (nmp->nm_so && (nmp->nm_sotype != SOCK_DGRAM ||
931                 (nmp->nm_flag & NFSMNT_DUMBTIMR) ||
932                 nmp->nm_sent < nmp->nm_cwnd)) {
933                 splx(s);
934                 if (nmp->nm_soflags & PR_CONNREQUIRED)
935                         error = nfs_sndlock(rep);
936                 if (!error) {
937                         m2 = m_copym(m, 0, M_COPYALL, M_TRYWAIT);
938                         error = nfs_send(nmp->nm_so, nmp->nm_nam, m2, rep);
939                         if (nmp->nm_soflags & PR_CONNREQUIRED)
940                                 nfs_sndunlock(rep);
941                 }
942                 if (!error && (rep->r_flags & R_MUSTRESEND) == 0) {
943                         nmp->nm_sent += NFS_CWNDSCALE;
944                         rep->r_flags |= R_SENT;
945                 }
946         } else {
947                 splx(s);
948                 rep->r_rtt = -1;
949         }
950
951         /*
952          * Wait for the reply from our send or the timer's.
953          */
954         if (!error || error == EPIPE)
955                 error = nfs_reply(rep);
956
957         /*
958          * RPC done, unlink the request.
959          */
960         s = splsoftclock();
961         TAILQ_REMOVE(&nfs_reqq, rep, r_chain);
962         splx(s);
963
964         /*
965          * Decrement the outstanding request count.
966          */
967         if (rep->r_flags & R_SENT) {
968                 rep->r_flags &= ~R_SENT;        /* paranoia */
969                 nmp->nm_sent -= NFS_CWNDSCALE;
970         }
971
972         /*
973          * If there was a successful reply and a tprintf msg.
974          * tprintf a response.
975          */
976         if (!error && (rep->r_flags & R_TPRINTFMSG))
977                 nfs_msg(rep->r_td, nmp->nm_mountp->mnt_stat.f_mntfromname,
978                     "is alive again");
979         mrep = rep->r_mrep;
980         md = rep->r_md;
981         dpos = rep->r_dpos;
982         if (error) {
983                 m_freem(rep->r_mreq);
984                 free((caddr_t)rep, M_NFSREQ);
985                 return (error);
986         }
987
988         /*
989          * break down the rpc header and check if ok
990          */
991         tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
992         if (*tl++ == rpc_msgdenied) {
993                 if (*tl == rpc_mismatch)
994                         error = EOPNOTSUPP;
995                 else
996                         error = EACCES;
997                 m_freem(mrep);
998                 m_freem(rep->r_mreq);
999                 free((caddr_t)rep, M_NFSREQ);
1000                 return (error);
1001         }
1002
1003         /*
1004          * Just throw away any verifyer (ie: kerberos etc).
1005          */
1006         i = fxdr_unsigned(int, *tl++);          /* verf type */
1007         i = fxdr_unsigned(int32_t, *tl);        /* len */
1008         if (i > 0)
1009                 nfsm_adv(nfsm_rndup(i));
1010         tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
1011         /* 0 == ok */
1012         if (*tl == 0) {
1013                 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
1014                 if (*tl != 0) {
1015                         error = fxdr_unsigned(int, *tl);
1016                         if ((nmp->nm_flag & NFSMNT_NFSV3) &&
1017                                 error == NFSERR_TRYLATER) {
1018                                 m_freem(mrep);
1019                                 error = 0;
1020                                 waituntil = time_second + trylater_delay;
1021                                 while (time_second < waituntil)
1022                                         (void) tsleep((caddr_t)&lbolt,
1023                                                 PSOCK, "nqnfstry", 0);
1024                                 trylater_delay *= nfs_backoff[trylater_cnt];
1025                                 if (trylater_cnt < NFS_NBACKOFF - 1)
1026                                         trylater_cnt++;
1027                                 goto tryagain;
1028                         }
1029
1030                         /*
1031                          * If the File Handle was stale, invalidate the
1032                          * lookup cache, just in case.
1033                          */
1034                         if (error == ESTALE)
1035                                 cache_purge(vp);
1036                         if (nmp->nm_flag & NFSMNT_NFSV3) {
1037                                 *mrp = mrep;
1038                                 *mdp = md;
1039                                 *dposp = dpos;
1040                                 error |= NFSERR_RETERR;
1041                         } else
1042                                 m_freem(mrep);
1043                         m_freem(rep->r_mreq);
1044                         free((caddr_t)rep, M_NFSREQ);
1045                         return (error);
1046                 }
1047
1048                 *mrp = mrep;
1049                 *mdp = md;
1050                 *dposp = dpos;
1051                 m_freem(rep->r_mreq);
1052                 FREE((caddr_t)rep, M_NFSREQ);
1053                 return (0);
1054         }
1055         m_freem(mrep);
1056         error = EPROTONOSUPPORT;
1057 nfsmout:
1058         m_freem(rep->r_mreq);
1059         free((caddr_t)rep, M_NFSREQ);
1060         return (error);
1061 }
1062
1063 /*
1064  * Nfs timer routine
1065  * Scan the nfsreq list and retranmit any requests that have timed out
1066  * To avoid retransmission attempts on STREAM sockets (in the future) make
1067  * sure to set the r_retry field to 0 (implies nm_retry == 0).
1068  */
1069 void
1070 nfs_timer(void *arg)
1071 {
1072         struct nfsreq *rep;
1073         struct mbuf *m;
1074         struct socket *so;
1075         struct nfsmount *nmp;
1076         int timeo;
1077         int s, error;
1078         struct thread *td;
1079
1080         td = &thread0; /* XXX for credentials, may break if sleep */
1081         s = splnet();
1082         TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
1083                 nmp = rep->r_nmp;
1084                 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM))
1085                         continue;
1086                 if (nfs_sigintr(nmp, rep, rep->r_td)) {
1087                         nfs_softterm(rep);
1088                         continue;
1089                 }
1090                 if (rep->r_rtt >= 0) {
1091                         rep->r_rtt++;
1092                         if (nmp->nm_flag & NFSMNT_DUMBTIMR)
1093                                 timeo = nmp->nm_timeo;
1094                         else
1095                                 timeo = NFS_RTO(nmp, proct[rep->r_procnum]);
1096                         if (nmp->nm_timeouts > 0)
1097                                 timeo *= nfs_backoff[nmp->nm_timeouts - 1];
1098                         if (rep->r_rtt <= timeo)
1099                                 continue;
1100                         if (nmp->nm_timeouts < NFS_NBACKOFF)
1101                                 nmp->nm_timeouts++;
1102                 }
1103                 /*
1104                  * Check for server not responding
1105                  */
1106                 if ((rep->r_flags & R_TPRINTFMSG) == 0 &&
1107                      rep->r_rexmit > nmp->nm_deadthresh) {
1108                         char buf[40];
1109                         sprintf(buf, "not responding %d > %d",
1110                         rep->r_rexmit, nmp->nm_deadthresh);
1111                         nfs_msg(rep->r_td,
1112                             nmp->nm_mountp->mnt_stat.f_mntfromname,
1113                             buf /* "not responding" */);
1114                         rep->r_flags |= R_TPRINTFMSG;
1115                 }
1116                 if (rep->r_rexmit >= rep->r_retry) {    /* too many */
1117                         nfsstats.rpctimeouts++;
1118                         nfs_softterm(rep);
1119                         continue;
1120                 }
1121                 if (nmp->nm_sotype != SOCK_DGRAM) {
1122                         if (++rep->r_rexmit > NFS_MAXREXMIT)
1123                                 rep->r_rexmit = NFS_MAXREXMIT;
1124                         continue;
1125                 }
1126                 if ((so = nmp->nm_so) == NULL)
1127                         continue;
1128
1129                 /*
1130                  * If there is enough space and the window allows..
1131                  *      Resend it
1132                  * Set r_rtt to -1 in case we fail to send it now.
1133                  */
1134                 rep->r_rtt = -1;
1135                 if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len &&
1136                    ((nmp->nm_flag & NFSMNT_DUMBTIMR) ||
1137                     (rep->r_flags & R_SENT) ||
1138                     nmp->nm_sent < nmp->nm_cwnd) &&
1139                    (m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))){
1140                         if ((nmp->nm_flag & NFSMNT_NOCONN) == 0)
1141                             error = (*so->so_proto->pr_usrreqs->pru_send)
1142                                     (so, 0, m, NULL, NULL, td);
1143                         else
1144                             error = (*so->so_proto->pr_usrreqs->pru_send)
1145                                     (so, 0, m, nmp->nm_nam, NULL, td);
1146                         if (error) {
1147                                 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error))
1148                                         so->so_error = 0;
1149                         } else {
1150                                 /*
1151                                  * Iff first send, start timing
1152                                  * else turn timing off, backoff timer
1153                                  * and divide congestion window by 2.
1154                                  */
1155                                 if (rep->r_flags & R_SENT) {
1156                                         rep->r_flags &= ~R_TIMING;
1157                                         if (++rep->r_rexmit > NFS_MAXREXMIT)
1158                                                 rep->r_rexmit = NFS_MAXREXMIT;
1159                                         nmp->nm_cwnd >>= 1;
1160                                         if (nmp->nm_cwnd < NFS_CWNDSCALE)
1161                                                 nmp->nm_cwnd = NFS_CWNDSCALE;
1162                                         nfsstats.rpcretries++;
1163                                 } else {
1164                                         rep->r_flags |= R_SENT;
1165                                         nmp->nm_sent += NFS_CWNDSCALE;
1166                                 }
1167                                 rep->r_rtt = 0;
1168                         }
1169                 }
1170         }
1171         splx(s);
1172         nfs_timer_handle = timeout(nfs_timer, NULL, nfs_ticks);
1173 }
1174
1175 /*
1176  * Mark all of an nfs mount's outstanding requests with R_SOFTTERM and
1177  * wait for all requests to complete. This is used by forced unmounts
1178  * to terminate any outstanding RPCs.
1179  */
1180 int
1181 nfs_nmcancelreqs(nmp)
1182         struct nfsmount *nmp;
1183 {
1184         struct nfsreq *req;
1185         int i, s;
1186
1187         s = splnet();
1188         TAILQ_FOREACH(req, &nfs_reqq, r_chain) {
1189                 if (nmp != req->r_nmp || req->r_mrep != NULL ||
1190                     (req->r_flags & R_SOFTTERM))
1191                         continue;
1192                 nfs_softterm(req);
1193         }
1194         splx(s);
1195
1196         for (i = 0; i < 30; i++) {
1197                 s = splnet();
1198                 TAILQ_FOREACH(req, &nfs_reqq, r_chain) {
1199                         if (nmp == req->r_nmp)
1200                                 break;
1201                 }
1202                 splx(s);
1203                 if (req == NULL)
1204                         return (0);
1205                 tsleep(&lbolt, PSOCK, "nfscancel", 0);
1206         }
1207         return (EBUSY);
1208 }
1209
1210 /*
1211  * Flag a request as being about to terminate (due to NFSMNT_INT/NFSMNT_SOFT).
1212  * The nm_send count is decremented now to avoid deadlocks when the process in
1213  * soreceive() hasn't yet managed to send its own request.
1214  */
1215
1216 static void
1217 nfs_softterm(struct nfsreq *rep)
1218 {
1219
1220         rep->r_flags |= R_SOFTTERM;
1221         if (rep->r_flags & R_SENT) {
1222                 rep->r_nmp->nm_sent -= NFS_CWNDSCALE;
1223                 rep->r_flags &= ~R_SENT;
1224         }
1225 }
1226
1227 /*
1228  * Test for a termination condition pending on the process.
1229  * This is used for NFSMNT_INT mounts.
1230  */
1231 int
1232 nfs_sigintr(struct nfsmount *nmp, struct nfsreq *rep, struct thread *td)
1233 {
1234         struct proc *p;
1235         sigset_t tmpset;
1236
1237         if (rep && (rep->r_flags & R_SOFTTERM))
1238                 return (EINTR);
1239         /* Terminate all requests while attempting a forced unmount. */
1240         if (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF)
1241                 return (EINTR);
1242         if (!(nmp->nm_flag & NFSMNT_INT))
1243                 return (0);
1244         if (td == NULL)
1245                 return (0);
1246
1247         p = td->td_proc;
1248         tmpset = p->p_siglist;
1249         SIGSETNAND(tmpset, p->p_sigmask);
1250         SIGSETNAND(tmpset, p->p_sigignore);
1251         if (SIGNOTEMPTY(p->p_siglist) && NFSINT_SIGMASK(tmpset))
1252                 return (EINTR);
1253
1254         return (0);
1255 }
1256
1257 /*
1258  * Lock a socket against others.
1259  * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
1260  * and also to avoid race conditions between the processes with nfs requests
1261  * in progress when a reconnect is necessary.
1262  */
1263 int
1264 nfs_sndlock(struct nfsreq *rep)
1265 {
1266         int *statep = &rep->r_nmp->nm_state;
1267         struct thread *td;
1268         int slpflag = 0, slptimeo = 0;
1269
1270         if (rep) {
1271                 td = rep->r_td;
1272                 if (rep->r_nmp->nm_flag & NFSMNT_INT)
1273                         slpflag = PCATCH;
1274         } else
1275                 td = NULL;
1276         while (*statep & NFSSTA_SNDLOCK) {
1277                 if (nfs_sigintr(rep->r_nmp, rep, td))
1278                         return (EINTR);
1279                 *statep |= NFSSTA_WANTSND;
1280                 (void) tsleep((caddr_t)statep, slpflag | (PZERO - 1),
1281                         "nfsndlck", slptimeo);
1282                 if (slpflag == PCATCH) {
1283                         slpflag = 0;
1284                         slptimeo = 2 * hz;
1285                 }
1286         }
1287         *statep |= NFSSTA_SNDLOCK;
1288         return (0);
1289 }
1290
1291 /*
1292  * Unlock the stream socket for others.
1293  */
1294 void
1295 nfs_sndunlock(struct nfsreq *rep)
1296 {
1297         int *statep = &rep->r_nmp->nm_state;
1298
1299         if ((*statep & NFSSTA_SNDLOCK) == 0)
1300                 panic("nfs sndunlock");
1301         *statep &= ~NFSSTA_SNDLOCK;
1302         if (*statep & NFSSTA_WANTSND) {
1303                 *statep &= ~NFSSTA_WANTSND;
1304                 wakeup((caddr_t)statep);
1305         }
1306 }
1307
1308 static int
1309 nfs_rcvlock(struct nfsreq *rep)
1310 {
1311         int *statep = &rep->r_nmp->nm_state;
1312         int slpflag, slptimeo = 0;
1313
1314         if (rep->r_nmp->nm_flag & NFSMNT_INT)
1315                 slpflag = PCATCH;
1316         else
1317                 slpflag = 0;
1318         while (*statep & NFSSTA_RCVLOCK) {
1319                 if (nfs_sigintr(rep->r_nmp, rep, rep->r_td))
1320                         return (EINTR);
1321                 *statep |= NFSSTA_WANTRCV;
1322                 (void) tsleep((caddr_t)statep, slpflag | (PZERO - 1), "nfsrcvlk",
1323                         slptimeo);
1324                 /*
1325                  * If our reply was recieved while we were sleeping,
1326                  * then just return without taking the lock to avoid a
1327                  * situation where a single iod could 'capture' the
1328                  * recieve lock.
1329                  */
1330                 if (rep->r_mrep != NULL)
1331                         return (EALREADY);
1332                 if (slpflag == PCATCH) {
1333                         slpflag = 0;
1334                         slptimeo = 2 * hz;
1335                 }
1336         }
1337         /* Always fail if our request has been cancelled. */
1338         if (rep != NULL && (rep->r_flags & R_SOFTTERM))
1339                 return (EINTR);
1340         *statep |= NFSSTA_RCVLOCK;
1341         return (0);
1342 }
1343
1344 /*
1345  * Unlock the stream socket for others.
1346  */
1347 static void
1348 nfs_rcvunlock(struct nfsreq *rep)
1349 {
1350         int *statep = &rep->r_nmp->nm_state;
1351
1352         if ((*statep & NFSSTA_RCVLOCK) == 0)
1353                 panic("nfs rcvunlock");
1354         *statep &= ~NFSSTA_RCVLOCK;
1355         if (*statep & NFSSTA_WANTRCV) {
1356                 *statep &= ~NFSSTA_WANTRCV;
1357                 wakeup((caddr_t)statep);
1358         }
1359 }
1360
1361 /*
1362  *      nfs_realign:
1363  *
1364  *      Check for badly aligned mbuf data and realign by copying the unaligned
1365  *      portion of the data into a new mbuf chain and freeing the portions
1366  *      of the old chain that were replaced.
1367  *
1368  *      We cannot simply realign the data within the existing mbuf chain
1369  *      because the underlying buffers may contain other rpc commands and
1370  *      we cannot afford to overwrite them.
1371  *
1372  *      We would prefer to avoid this situation entirely.  The situation does
1373  *      not occur with NFS/UDP and is supposed to only occassionally occur
1374  *      with TCP.  Use vfs.nfs.realign_count and realign_test to check this.
1375  */
1376 static void
1377 nfs_realign(struct mbuf **pm, int hsiz)
1378 {
1379         struct mbuf *m;
1380         struct mbuf *n = NULL;
1381         int off = 0;
1382
1383         ++nfs_realign_test;
1384         while ((m = *pm) != NULL) {
1385                 if ((m->m_len & 0x3) || (mtod(m, intptr_t) & 0x3)) {
1386                         MGET(n, M_TRYWAIT, MT_DATA);
1387                         if (m->m_len >= MINCLSIZE) {
1388                                 MCLGET(n, M_TRYWAIT);
1389                         }
1390                         n->m_len = 0;
1391                         break;
1392                 }
1393                 pm = &m->m_next;
1394         }
1395         /*
1396          * If n is non-NULL, loop on m copying data, then replace the
1397          * portion of the chain that had to be realigned.
1398          */
1399         if (n != NULL) {
1400                 ++nfs_realign_count;
1401                 while (m) {
1402                         m_copyback(n, off, m->m_len, mtod(m, caddr_t));
1403                         off += m->m_len;
1404                         m = m->m_next;
1405                 }
1406                 m_freem(*pm);
1407                 *pm = n;
1408         }
1409 }
1410
1411
1412 static int
1413 nfs_msg(struct thread *td, char *server, char *msg)
1414 {
1415
1416         tprintf(td ? td->td_proc : NULL, LOG_INFO,
1417             "nfs server %s: %s\n", server, msg);
1418         return (0);
1419 }