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