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