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