Commit | Line | Data |
---|---|---|
15637ed4 RG |
1 | /* |
2 | * Copyright (c) 1989, 1991 The Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * This code is derived from software contributed to Berkeley by | |
6 | * Rick Macklem at The University of Guelph. | |
7 | * | |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
11 | * 1. Redistributions of source code must retain the above copyright | |
12 | * notice, this list of conditions and the following disclaimer. | |
13 | * 2. Redistributions in binary form must reproduce the above copyright | |
14 | * notice, this list of conditions and the following disclaimer in the | |
15 | * documentation and/or other materials provided with the distribution. | |
16 | * 3. All advertising materials mentioning features or use of this software | |
17 | * must display the following acknowledgement: | |
18 | * This product includes software developed by the University of | |
19 | * California, Berkeley and its contributors. | |
20 | * 4. Neither the name of the University nor the names of its contributors | |
21 | * may be used to endorse or promote products derived from this software | |
22 | * without specific prior written permission. | |
23 | * | |
24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
34 | * SUCH DAMAGE. | |
35 | * | |
a8599e88 | 36 | * From: @(#)nfs_socket.c 7.23 (Berkeley) 4/20/91 |
fde1aeb2 | 37 | * $Id: nfs_socket.c,v 1.3 1993/09/09 22:06:05 rgrimes Exp $ |
15637ed4 RG |
38 | */ |
39 | ||
40 | /* | |
41 | * Socket operations for use by nfs | |
42 | */ | |
43 | ||
44 | #include "param.h" | |
dd18dc33 | 45 | #include "systm.h" |
15637ed4 RG |
46 | #include "proc.h" |
47 | #include "mount.h" | |
48 | #include "kernel.h" | |
49 | #include "malloc.h" | |
50 | #include "mbuf.h" | |
51 | #include "namei.h" | |
52 | #include "vnode.h" | |
53 | #include "domain.h" | |
54 | #include "protosw.h" | |
55 | #include "socket.h" | |
56 | #include "socketvar.h" | |
57 | #include "syslog.h" | |
58 | #include "tprintf.h" | |
59 | #include "../netinet/in.h" | |
60 | #include "../netinet/tcp.h" | |
61 | ||
62 | #include "rpcv2.h" | |
63 | #include "nfsv2.h" | |
64 | #include "nfs.h" | |
65 | #include "xdr_subs.h" | |
66 | #include "nfsm_subs.h" | |
67 | #include "nfsmount.h" | |
68 | ||
69 | #define TRUE 1 | |
70 | #define FALSE 0 | |
71 | ||
72 | /* | |
73 | * External data, mostly RPC constants in XDR form | |
74 | */ | |
75 | extern u_long rpc_reply, rpc_msgdenied, rpc_mismatch, rpc_vers, rpc_auth_unix, | |
76 | rpc_msgaccepted, rpc_call; | |
77 | extern u_long nfs_prog, nfs_vers; | |
78 | /* Maybe these should be bits in a u_long ?? */ | |
79 | extern int nonidempotent[NFS_NPROCS]; | |
80 | static int compressrequest[NFS_NPROCS] = { | |
81 | FALSE, | |
82 | TRUE, | |
83 | TRUE, | |
84 | FALSE, | |
85 | TRUE, | |
86 | TRUE, | |
87 | TRUE, | |
88 | FALSE, | |
89 | FALSE, | |
90 | TRUE, | |
91 | TRUE, | |
92 | TRUE, | |
93 | TRUE, | |
94 | TRUE, | |
95 | TRUE, | |
96 | TRUE, | |
97 | TRUE, | |
98 | TRUE, | |
99 | }; | |
100 | int nfs_sbwait(); | |
101 | void nfs_disconnect(); | |
102 | struct mbuf *nfs_compress(), *nfs_uncompress(); | |
103 | ||
104 | int nfsrv_null(), | |
105 | nfsrv_getattr(), | |
106 | nfsrv_setattr(), | |
107 | nfsrv_lookup(), | |
108 | nfsrv_readlink(), | |
109 | nfsrv_read(), | |
110 | nfsrv_write(), | |
111 | nfsrv_create(), | |
112 | nfsrv_remove(), | |
113 | nfsrv_rename(), | |
114 | nfsrv_link(), | |
115 | nfsrv_symlink(), | |
116 | nfsrv_mkdir(), | |
117 | nfsrv_rmdir(), | |
118 | nfsrv_readdir(), | |
119 | nfsrv_statfs(), | |
120 | nfsrv_noop(); | |
121 | ||
122 | int (*nfsrv_procs[NFS_NPROCS])() = { | |
123 | nfsrv_null, | |
124 | nfsrv_getattr, | |
125 | nfsrv_setattr, | |
126 | nfsrv_noop, | |
127 | nfsrv_lookup, | |
128 | nfsrv_readlink, | |
129 | nfsrv_read, | |
130 | nfsrv_noop, | |
131 | nfsrv_write, | |
132 | nfsrv_create, | |
133 | nfsrv_remove, | |
134 | nfsrv_rename, | |
135 | nfsrv_link, | |
136 | nfsrv_symlink, | |
137 | nfsrv_mkdir, | |
138 | nfsrv_rmdir, | |
139 | nfsrv_readdir, | |
140 | nfsrv_statfs, | |
141 | }; | |
142 | ||
143 | struct nfsreq nfsreqh; | |
144 | int nfsrexmtthresh = NFS_FISHY; | |
145 | int nfs_tcpnodelay = 1; | |
146 | ||
147 | /* | |
148 | * Initialize sockets and congestion for a new NFS connection. | |
149 | * We do not free the sockaddr if error. | |
150 | */ | |
a8599e88 | 151 | int |
15637ed4 RG |
152 | nfs_connect(nmp) |
153 | register struct nfsmount *nmp; | |
154 | { | |
155 | register struct socket *so; | |
156 | struct sockaddr *saddr; /* 08 Sep 92*/ | |
157 | int s, error, bufsize; | |
158 | struct mbuf *m; | |
159 | struct sockaddr_in *sin; /* 08 Sep 92*/ | |
160 | u_short tport; /* 08 Sep 92*/ | |
161 | ||
162 | nmp->nm_so = (struct socket *)0; | |
163 | saddr = mtod(nmp->nm_nam, struct sockaddr *); /* 08 Sep 92*/ | |
164 | if (error = socreate(saddr->sa_family, /* 08 Sep 92*/ | |
165 | &nmp->nm_so, nmp->nm_sotype, nmp->nm_soproto)) | |
166 | goto bad; | |
167 | so = nmp->nm_so; | |
168 | nmp->nm_soflags = so->so_proto->pr_flags; | |
169 | ||
170 | /* | |
171 | * 08 Sep 92 | |
172 | * | |
173 | * Some servers require that the client port be a reserved port number. | |
174 | */ | |
175 | if (saddr->sa_family == AF_INET) { | |
176 | MGET(m, M_WAIT, MT_SONAME); | |
177 | sin = mtod(m, struct sockaddr_in *); | |
178 | sin->sin_len = m->m_len = sizeof (struct sockaddr_in); | |
179 | sin->sin_family = AF_INET; | |
180 | sin->sin_addr.s_addr = INADDR_ANY; | |
181 | tport = IPPORT_RESERVED - 1; | |
182 | sin->sin_port = htons(tport); | |
183 | while (sobind(so, m) == EADDRINUSE && | |
184 | --tport > IPPORT_RESERVED / 2) | |
185 | sin->sin_port = htons(tport); | |
186 | m_freem(m); | |
187 | } | |
188 | ||
189 | if (nmp->nm_sotype == SOCK_DGRAM) | |
190 | bufsize = min(4 * (nmp->nm_wsize + NFS_MAXPKTHDR), | |
191 | NFS_MAXPACKET); | |
192 | else | |
193 | bufsize = min(4 * (nmp->nm_wsize + NFS_MAXPKTHDR + sizeof(u_long)), | |
194 | NFS_MAXPACKET + sizeof(u_long)); | |
195 | if (error = soreserve(so, bufsize, bufsize)) | |
196 | goto bad; | |
197 | ||
198 | /* | |
199 | * Protocols that do not require connections may be optionally left | |
200 | * unconnected for servers that reply from a port other than NFS_PORT. | |
201 | */ | |
202 | if (nmp->nm_flag & NFSMNT_NOCONN) { | |
203 | if (nmp->nm_soflags & PR_CONNREQUIRED) { | |
204 | error = ENOTCONN; | |
205 | goto bad; | |
206 | } | |
207 | } else { | |
208 | if (error = soconnect(so, nmp->nm_nam)) | |
209 | goto bad; | |
210 | ||
211 | /* | |
212 | * Wait for the connection to complete. Cribbed from the | |
213 | * connect system call but with the wait at negative prio. | |
214 | */ | |
215 | s = splnet(); | |
216 | while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) | |
217 | (void) tsleep((caddr_t)&so->so_timeo, PSOCK, "nfscon", 0); | |
218 | splx(s); | |
219 | if (so->so_error) { | |
220 | error = so->so_error; | |
221 | goto bad; | |
222 | } | |
223 | } | |
224 | if (nmp->nm_sotype == SOCK_DGRAM) { | |
225 | if (nmp->nm_flag & (NFSMNT_SOFT | NFSMNT_SPONGY | NFSMNT_INT)) { | |
226 | so->so_rcv.sb_timeo = (5 * hz); | |
227 | so->so_snd.sb_timeo = (5 * hz); | |
228 | } else { | |
229 | so->so_rcv.sb_timeo = 0; | |
230 | so->so_snd.sb_timeo = 0; | |
231 | } | |
232 | nmp->nm_rto = NFS_TIMEO; | |
233 | } else { | |
234 | if (nmp->nm_flag & (NFSMNT_SOFT | NFSMNT_SPONGY | NFSMNT_INT)) { | |
235 | so->so_rcv.sb_timeo = (5 * hz); | |
236 | so->so_snd.sb_timeo = (5 * hz); | |
237 | } else { | |
238 | so->so_rcv.sb_timeo = 0; | |
239 | so->so_snd.sb_timeo = 0; | |
240 | } | |
241 | if (so->so_proto->pr_flags & PR_CONNREQUIRED) { | |
242 | MGET(m, M_WAIT, MT_SOOPTS); | |
243 | *mtod(m, int *) = 1; | |
244 | m->m_len = sizeof(int); | |
245 | sosetopt(so, SOL_SOCKET, SO_KEEPALIVE, m); | |
246 | } | |
247 | if (so->so_proto->pr_domain->dom_family == AF_INET && | |
248 | so->so_proto->pr_protocol == IPPROTO_TCP && | |
249 | nfs_tcpnodelay) { | |
250 | MGET(m, M_WAIT, MT_SOOPTS); | |
251 | *mtod(m, int *) = 1; | |
252 | m->m_len = sizeof(int); | |
253 | sosetopt(so, IPPROTO_TCP, TCP_NODELAY, m); | |
254 | } | |
255 | nmp->nm_rto = 10 * NFS_TIMEO; /* XXX */ | |
256 | } | |
257 | so->so_rcv.sb_flags |= SB_NOINTR; | |
258 | so->so_snd.sb_flags |= SB_NOINTR; | |
259 | ||
260 | /* Initialize other non-zero congestion variables */ | |
261 | nmp->nm_window = 2; /* Initial send window */ | |
262 | nmp->nm_ssthresh = NFS_MAXWINDOW; /* Slowstart threshold */ | |
263 | nmp->nm_rttvar = nmp->nm_rto << 1; | |
264 | nmp->nm_sent = 0; | |
265 | nmp->nm_currexmit = 0; | |
266 | return (0); | |
267 | ||
268 | bad: | |
269 | nfs_disconnect(nmp); | |
270 | return (error); | |
271 | } | |
272 | ||
273 | /* | |
274 | * Reconnect routine: | |
275 | * Called when a connection is broken on a reliable protocol. | |
276 | * - clean up the old socket | |
277 | * - nfs_connect() again | |
278 | * - set R_MUSTRESEND for all outstanding requests on mount point | |
279 | * If this fails the mount point is DEAD! | |
280 | * nb: Must be called with the nfs_solock() set on the mount point. | |
281 | */ | |
a8599e88 | 282 | int |
15637ed4 RG |
283 | nfs_reconnect(rep, nmp) |
284 | register struct nfsreq *rep; | |
285 | register struct nfsmount *nmp; | |
286 | { | |
287 | register struct nfsreq *rp; | |
288 | int error; | |
289 | ||
290 | nfs_msg(rep->r_procp, nmp->nm_mountp->mnt_stat.f_mntfromname, | |
291 | "trying reconnect"); | |
292 | while (error = nfs_connect(nmp)) { | |
293 | #ifdef lint | |
294 | error = error; | |
295 | #endif /* lint */ | |
296 | if ((nmp->nm_flag & NFSMNT_INT) && nfs_sigintr(rep->r_procp)) | |
297 | return (EINTR); | |
298 | (void) tsleep((caddr_t)&lbolt, PSOCK, "nfscon", 0); | |
299 | } | |
300 | nfs_msg(rep->r_procp, nmp->nm_mountp->mnt_stat.f_mntfromname, | |
301 | "reconnected"); | |
302 | ||
303 | /* | |
304 | * Loop through outstanding request list and fix up all requests | |
305 | * on old socket. | |
306 | */ | |
307 | rp = nfsreqh.r_next; | |
308 | while (rp != &nfsreqh) { | |
309 | if (rp->r_nmp == nmp) | |
310 | rp->r_flags |= R_MUSTRESEND; | |
311 | rp = rp->r_next; | |
312 | } | |
313 | return (0); | |
314 | } | |
315 | ||
316 | /* | |
317 | * NFS disconnect. Clean up and unlink. | |
318 | */ | |
319 | void | |
320 | nfs_disconnect(nmp) | |
321 | register struct nfsmount *nmp; | |
322 | { | |
323 | register struct socket *so; | |
324 | ||
325 | if (nmp->nm_so) { | |
326 | so = nmp->nm_so; | |
327 | nmp->nm_so = (struct socket *)0; | |
328 | soshutdown(so, 2); | |
329 | soclose(so); | |
330 | } | |
331 | } | |
332 | ||
333 | /* | |
334 | * This is the nfs send routine. For connection based socket types, it | |
335 | * must be called with an nfs_solock() on the socket. | |
336 | * "rep == NULL" indicates that it has been called from a server. | |
337 | */ | |
a8599e88 | 338 | int |
15637ed4 RG |
339 | nfs_send(so, nam, top, rep) |
340 | register struct socket *so; | |
341 | struct mbuf *nam; | |
342 | register struct mbuf *top; | |
343 | struct nfsreq *rep; | |
344 | { | |
345 | struct mbuf *sendnam; | |
346 | int error, soflags; | |
347 | ||
348 | if (rep) { | |
349 | if (rep->r_flags & R_SOFTTERM) { | |
350 | m_freem(top); | |
351 | return (EINTR); | |
352 | } | |
353 | if (rep->r_nmp->nm_so == NULL && | |
354 | (error = nfs_reconnect(rep, rep->r_nmp))) | |
355 | return (error); | |
356 | rep->r_flags &= ~R_MUSTRESEND; | |
357 | so = rep->r_nmp->nm_so; | |
358 | soflags = rep->r_nmp->nm_soflags; | |
359 | } else | |
360 | soflags = so->so_proto->pr_flags; | |
361 | if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED)) | |
362 | sendnam = (struct mbuf *)0; | |
363 | else | |
364 | sendnam = nam; | |
365 | ||
366 | error = sosend(so, sendnam, (struct uio *)0, top, | |
367 | (struct mbuf *)0, 0); | |
368 | if (error == EWOULDBLOCK && rep) { | |
369 | if (rep->r_flags & R_SOFTTERM) | |
370 | error = EINTR; | |
371 | else { | |
372 | rep->r_flags |= R_MUSTRESEND; | |
373 | error = 0; | |
374 | } | |
375 | } | |
376 | /* | |
377 | * Ignore socket errors?? | |
378 | */ | |
379 | if (error && error != EINTR && error != ERESTART) | |
380 | error = 0; | |
381 | return (error); | |
382 | } | |
383 | ||
384 | /* | |
385 | * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all | |
386 | * done by soreceive(), but for SOCK_STREAM we must deal with the Record | |
387 | * Mark and consolidate the data into a new mbuf list. | |
388 | * nb: Sometimes TCP passes the data up to soreceive() in long lists of | |
389 | * small mbufs. | |
390 | * For SOCK_STREAM we must be very careful to read an entire record once | |
391 | * we have read any of it, even if the system call has been interrupted. | |
392 | */ | |
a8599e88 | 393 | int |
15637ed4 RG |
394 | nfs_receive(so, aname, mp, rep) |
395 | register struct socket *so; | |
396 | struct mbuf **aname; | |
397 | struct mbuf **mp; | |
398 | register struct nfsreq *rep; | |
399 | { | |
400 | struct uio auio; | |
401 | struct iovec aio; | |
402 | register struct mbuf *m; | |
a8599e88 RG |
403 | struct mbuf *m2 = 0, *mnew, **mbp; |
404 | caddr_t fcp, tcp = 0; | |
15637ed4 RG |
405 | u_long len; |
406 | struct mbuf **getnam; | |
407 | int error, siz, mlen, soflags, rcvflg; | |
408 | ||
409 | /* | |
410 | * Set up arguments for soreceive() | |
411 | */ | |
412 | *mp = (struct mbuf *)0; | |
413 | *aname = (struct mbuf *)0; | |
414 | if (rep) | |
415 | soflags = rep->r_nmp->nm_soflags; | |
416 | else | |
417 | soflags = so->so_proto->pr_flags; | |
418 | ||
419 | /* | |
420 | * For reliable protocols, lock against other senders/receivers | |
421 | * in case a reconnect is necessary. | |
422 | * For SOCK_STREAM, first get the Record Mark to find out how much | |
423 | * more there is to get. | |
424 | * We must lock the socket against other receivers | |
425 | * until we have an entire rpc request/reply. | |
426 | */ | |
427 | if (soflags & PR_CONNREQUIRED) { | |
428 | tryagain: | |
429 | /* | |
430 | * Check for fatal errors and resending request. | |
431 | */ | |
432 | if (rep) { | |
433 | /* | |
434 | * Ugh: If a reconnect attempt just happened, nm_so | |
435 | * would have changed. NULL indicates a failed | |
436 | * attempt that has essentially shut down this | |
437 | * mount point. | |
438 | */ | |
439 | if (rep->r_mrep || (so = rep->r_nmp->nm_so) == NULL || | |
440 | (rep->r_flags & R_SOFTTERM)) | |
441 | return (EINTR); | |
442 | while (rep->r_flags & R_MUSTRESEND) { | |
443 | m = m_copym(rep->r_mreq, 0, M_COPYALL, M_WAIT); | |
444 | nfsstats.rpcretries++; | |
445 | if (error = nfs_send(so, rep->r_nmp->nm_nam, m, | |
446 | rep)) | |
447 | goto errout; | |
448 | } | |
449 | } | |
450 | if ((soflags & PR_ATOMIC) == 0) { | |
451 | aio.iov_base = (caddr_t) &len; | |
452 | aio.iov_len = sizeof(u_long); | |
453 | auio.uio_iov = &aio; | |
454 | auio.uio_iovcnt = 1; | |
455 | auio.uio_segflg = UIO_SYSSPACE; | |
456 | auio.uio_rw = UIO_READ; | |
457 | auio.uio_procp = (struct proc *)0; | |
458 | auio.uio_offset = 0; | |
459 | auio.uio_resid = sizeof(u_long); | |
460 | do { | |
461 | rcvflg = MSG_WAITALL; | |
462 | error = soreceive(so, (struct mbuf **)0, &auio, | |
463 | (struct mbuf **)0, (struct mbuf **)0, &rcvflg); | |
464 | if (error == EWOULDBLOCK && rep) { | |
465 | if (rep->r_flags & R_SOFTTERM) | |
466 | return (EINTR); | |
467 | if (rep->r_flags & R_MUSTRESEND) | |
468 | goto tryagain; | |
469 | } | |
470 | } while (error == EWOULDBLOCK); | |
471 | if (!error && auio.uio_resid > 0) { | |
472 | if (rep) | |
473 | log(LOG_INFO, | |
474 | "short receive (%d/%d) from nfs server %s\n", | |
475 | sizeof(u_long) - auio.uio_resid, | |
476 | sizeof(u_long), | |
477 | rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname); | |
478 | error = EPIPE; | |
479 | } | |
480 | if (error) | |
481 | goto errout; | |
fde1aeb2 | 482 | len = ntohl(len) & ~0x80000000ul; |
15637ed4 RG |
483 | /* |
484 | * This is SERIOUS! We are out of sync with the sender | |
485 | * and forcing a disconnect/reconnect is all I can do. | |
486 | */ | |
487 | if (len > NFS_MAXPACKET) { | |
488 | if (rep) | |
489 | log(LOG_ERR, "%s (%d) from nfs server %s\n", | |
490 | "impossible packet length", | |
491 | len, | |
492 | rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname); | |
493 | error = EFBIG; | |
494 | goto errout; | |
495 | } | |
496 | auio.uio_resid = len; | |
497 | do { | |
498 | rcvflg = MSG_WAITALL; | |
499 | error = soreceive(so, (struct mbuf **)0, | |
500 | &auio, mp, (struct mbuf **)0, &rcvflg); | |
501 | } while (error == EWOULDBLOCK || error == EINTR || | |
502 | error == ERESTART); | |
503 | if (!error && auio.uio_resid > 0) { | |
504 | if (rep) | |
505 | log(LOG_INFO, | |
506 | "short receive (%d/%d) from nfs server %s\n", | |
507 | len - auio.uio_resid, len, | |
508 | rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname); | |
509 | error = EPIPE; | |
510 | } | |
511 | } else { | |
512 | auio.uio_resid = len = 1000000; /* Anything Big */ | |
513 | do { | |
514 | rcvflg = 0; | |
515 | error = soreceive(so, (struct mbuf **)0, | |
516 | &auio, mp, (struct mbuf **)0, &rcvflg); | |
517 | if (error == EWOULDBLOCK && rep) { | |
518 | if (rep->r_flags & R_SOFTTERM) | |
519 | return (EINTR); | |
520 | if (rep->r_flags & R_MUSTRESEND) | |
521 | goto tryagain; | |
522 | } | |
523 | } while (error == EWOULDBLOCK); | |
524 | if (!error && *mp == NULL) | |
525 | error = EPIPE; | |
526 | len -= auio.uio_resid; | |
527 | } | |
528 | errout: | |
529 | if (error && rep && error != EINTR && error != ERESTART) { | |
530 | m_freem(*mp); | |
531 | *mp = (struct mbuf *)0; | |
532 | if (error != EPIPE && rep) | |
533 | log(LOG_INFO, | |
534 | "receive error %d from nfs server %s\n", | |
535 | error, | |
536 | rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname); | |
537 | nfs_disconnect(rep->r_nmp); | |
538 | error = nfs_reconnect(rep, rep->r_nmp); | |
539 | if (!error) | |
540 | goto tryagain; | |
541 | } | |
542 | } else { | |
543 | if (so->so_state & SS_ISCONNECTED) | |
544 | getnam = (struct mbuf **)0; | |
545 | else | |
546 | getnam = aname; | |
547 | auio.uio_resid = len = 1000000; | |
548 | do { | |
549 | rcvflg = 0; | |
550 | error = soreceive(so, getnam, &auio, mp, | |
551 | (struct mbuf **)0, &rcvflg); | |
552 | if (error == EWOULDBLOCK && rep && | |
553 | (rep->r_flags & R_SOFTTERM)) | |
554 | return (EINTR); | |
555 | } while (error == EWOULDBLOCK); | |
556 | len -= auio.uio_resid; | |
557 | } | |
558 | if (error) { | |
559 | m_freem(*mp); | |
560 | *mp = (struct mbuf *)0; | |
561 | } | |
562 | /* | |
563 | * Search for any mbufs that are not a multiple of 4 bytes long. | |
564 | * These could cause pointer alignment problems, so copy them to | |
565 | * well aligned mbufs. | |
566 | */ | |
567 | m = *mp; | |
568 | mbp = mp; | |
569 | while (m) { | |
570 | /* | |
571 | * All this for something that may never happen. | |
572 | */ | |
573 | if (m->m_next && (m->m_len & 0x3)) { | |
574 | printf("nfs_rcv odd length!\n"); | |
575 | mlen = 0; | |
576 | while (m) { | |
577 | fcp = mtod(m, caddr_t); | |
578 | while (m->m_len > 0) { | |
579 | if (mlen == 0) { | |
580 | MGET(m2, M_WAIT, MT_DATA); | |
581 | if (len >= MINCLSIZE) | |
582 | MCLGET(m2, M_WAIT); | |
583 | m2->m_len = 0; | |
584 | mlen = M_TRAILINGSPACE(m2); | |
585 | tcp = mtod(m2, caddr_t); | |
586 | *mbp = m2; | |
587 | mbp = &m2->m_next; | |
588 | } | |
589 | siz = MIN(mlen, m->m_len); | |
590 | bcopy(fcp, tcp, siz); | |
591 | m2->m_len += siz; | |
592 | mlen -= siz; | |
593 | len -= siz; | |
594 | tcp += siz; | |
595 | m->m_len -= siz; | |
596 | fcp += siz; | |
597 | } | |
598 | MFREE(m, mnew); | |
599 | m = mnew; | |
600 | } | |
601 | break; | |
602 | } | |
603 | len -= m->m_len; | |
604 | mbp = &m->m_next; | |
605 | m = m->m_next; | |
606 | } | |
607 | return (error); | |
608 | } | |
609 | ||
610 | /* | |
611 | * Implement receipt of reply on a socket. | |
612 | * We must search through the list of received datagrams matching them | |
613 | * with outstanding requests using the xid, until ours is found. | |
614 | */ | |
615 | /* ARGSUSED */ | |
a8599e88 | 616 | int |
15637ed4 RG |
617 | nfs_reply(nmp, myrep) |
618 | struct nfsmount *nmp; | |
619 | struct nfsreq *myrep; | |
620 | { | |
621 | register struct mbuf *m; | |
622 | register struct nfsreq *rep; | |
623 | register int error = 0; | |
624 | u_long rxid; | |
625 | struct mbuf *mp, *nam; | |
a8599e88 RG |
626 | /* char *cp; */ |
627 | /* int cnt, xfer; */ | |
15637ed4 RG |
628 | |
629 | /* | |
630 | * Loop around until we get our own reply | |
631 | */ | |
632 | for (;;) { | |
633 | /* | |
634 | * Lock against other receivers so that I don't get stuck in | |
635 | * sbwait() after someone else has received my reply for me. | |
636 | * Also necessary for connection based protocols to avoid | |
637 | * race conditions during a reconnect. | |
638 | */ | |
639 | nfs_solock(&nmp->nm_flag); | |
640 | /* Already received, bye bye */ | |
641 | if (myrep->r_mrep != NULL) { | |
642 | nfs_sounlock(&nmp->nm_flag); | |
643 | return (0); | |
644 | } | |
645 | /* | |
646 | * Get the next Rpc reply off the socket | |
647 | */ | |
648 | if (error = nfs_receive(nmp->nm_so, &nam, &mp, myrep)) { | |
649 | nfs_sounlock(&nmp->nm_flag); | |
650 | ||
651 | /* | |
652 | * Ignore routing errors on connectionless protocols?? | |
653 | */ | |
654 | if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) { | |
655 | nmp->nm_so->so_error = 0; | |
656 | continue; | |
657 | } | |
658 | ||
659 | /* | |
660 | * Otherwise cleanup and return a fatal error. | |
661 | */ | |
662 | if (myrep->r_flags & R_TIMING) { | |
663 | myrep->r_flags &= ~R_TIMING; | |
664 | nmp->nm_rtt = -1; | |
665 | } | |
666 | if (myrep->r_flags & R_SENT) { | |
667 | myrep->r_flags &= ~R_SENT; | |
668 | nmp->nm_sent--; | |
669 | } | |
670 | return (error); | |
671 | } | |
672 | ||
673 | /* | |
674 | * Get the xid and check that it is an rpc reply | |
675 | */ | |
676 | m = mp; | |
677 | while (m && m->m_len == 0) | |
678 | m = m->m_next; | |
679 | if (m == NULL) { | |
680 | nfsstats.rpcinvalid++; | |
681 | m_freem(mp); | |
682 | nfs_sounlock(&nmp->nm_flag); | |
683 | continue; | |
684 | } | |
685 | bcopy(mtod(m, caddr_t), (caddr_t)&rxid, NFSX_UNSIGNED); | |
686 | /* | |
687 | * Loop through the request list to match up the reply | |
688 | * Iff no match, just drop the datagram | |
689 | */ | |
690 | m = mp; | |
691 | rep = nfsreqh.r_next; | |
692 | while (rep != &nfsreqh) { | |
693 | if (rep->r_mrep == NULL && rxid == rep->r_xid) { | |
694 | /* Found it.. */ | |
695 | rep->r_mrep = m; | |
696 | /* | |
697 | * Update timing | |
698 | */ | |
699 | if (rep->r_flags & R_TIMING) { | |
700 | nfs_updatetimer(rep->r_nmp); | |
701 | rep->r_flags &= ~R_TIMING; | |
702 | rep->r_nmp->nm_rtt = -1; | |
703 | } | |
704 | if (rep->r_flags & R_SENT) { | |
705 | rep->r_flags &= ~R_SENT; | |
706 | rep->r_nmp->nm_sent--; | |
707 | } | |
708 | break; | |
709 | } | |
710 | rep = rep->r_next; | |
711 | } | |
712 | nfs_sounlock(&nmp->nm_flag); | |
713 | if (nam) | |
714 | m_freem(nam); | |
715 | /* | |
716 | * If not matched to a request, drop it. | |
717 | * If it's mine, get out. | |
718 | */ | |
719 | if (rep == &nfsreqh) { | |
720 | nfsstats.rpcunexpected++; | |
721 | m_freem(m); | |
722 | } else if (rep == myrep) | |
723 | return (0); | |
724 | } | |
725 | } | |
726 | ||
727 | /* | |
728 | * nfs_request - goes something like this | |
729 | * - fill in request struct | |
730 | * - links it into list | |
731 | * - calls nfs_send() for first transmit | |
732 | * - calls nfs_receive() to get reply | |
733 | * - break down rpc header and return with nfs reply pointed to | |
734 | * by mrep or error | |
735 | * nb: always frees up mreq mbuf list | |
736 | */ | |
a8599e88 | 737 | int |
15637ed4 RG |
738 | nfs_request(vp, mreq, xid, procnum, procp, tryhard, mp, mrp, mdp, dposp) |
739 | struct vnode *vp; | |
740 | struct mbuf *mreq; | |
741 | u_long xid; | |
742 | int procnum; | |
743 | struct proc *procp; | |
744 | int tryhard; | |
745 | struct mount *mp; | |
746 | struct mbuf **mrp; | |
747 | struct mbuf **mdp; | |
748 | caddr_t *dposp; | |
749 | { | |
750 | register struct mbuf *m, *mrep; | |
751 | register struct nfsreq *rep; | |
752 | register u_long *tl; | |
753 | register int len; | |
754 | struct nfsmount *nmp; | |
755 | struct mbuf *md; | |
756 | struct nfsreq *reph; | |
757 | caddr_t dpos; | |
758 | char *cp2; | |
759 | int t1; | |
760 | int s, compressed; | |
761 | int error = 0; | |
762 | ||
763 | nmp = VFSTONFS(mp); | |
764 | m = mreq; | |
765 | MALLOC(rep, struct nfsreq *, sizeof(struct nfsreq), M_NFSREQ, M_WAITOK); | |
766 | rep->r_xid = xid; | |
767 | rep->r_nmp = nmp; | |
768 | rep->r_vp = vp; | |
769 | rep->r_procp = procp; | |
770 | if ((nmp->nm_flag & NFSMNT_SOFT) || | |
771 | ((nmp->nm_flag & NFSMNT_SPONGY) && !tryhard)) | |
772 | rep->r_retry = nmp->nm_retry; | |
773 | else | |
774 | rep->r_retry = NFS_MAXREXMIT + 1; /* past clip limit */ | |
775 | rep->r_flags = rep->r_rexmit = 0; | |
776 | /* | |
777 | * Three cases: | |
778 | * - non-idempotent requests on SOCK_DGRAM use NFS_MINIDEMTIMEO | |
779 | * - idempotent requests on SOCK_DGRAM use 0 | |
780 | * - Reliable transports, NFS_RELIABLETIMEO | |
781 | * Timeouts are still done on reliable transports to ensure detection | |
782 | * of excessive connection delay. | |
783 | */ | |
784 | if (nmp->nm_sotype != SOCK_DGRAM) | |
785 | rep->r_timerinit = -NFS_RELIABLETIMEO; | |
786 | else if (nonidempotent[procnum]) | |
787 | rep->r_timerinit = -NFS_MINIDEMTIMEO; | |
788 | else | |
789 | rep->r_timerinit = 0; | |
790 | rep->r_timer = rep->r_timerinit; | |
791 | rep->r_mrep = NULL; | |
792 | len = 0; | |
793 | while (m) { | |
794 | len += m->m_len; | |
795 | m = m->m_next; | |
796 | } | |
797 | mreq->m_pkthdr.len = len; | |
798 | mreq->m_pkthdr.rcvif = (struct ifnet *)0; | |
799 | compressed = 0; | |
800 | m = mreq; | |
801 | if ((nmp->nm_flag & NFSMNT_COMPRESS) && compressrequest[procnum]) { | |
802 | mreq = nfs_compress(mreq); | |
803 | if (mreq != m) { | |
804 | len = mreq->m_pkthdr.len; | |
805 | compressed++; | |
806 | } | |
807 | } | |
808 | /* | |
809 | * For non-atomic protocols, insert a Sun RPC Record Mark. | |
810 | */ | |
811 | if ((nmp->nm_soflags & PR_ATOMIC) == 0) { | |
812 | M_PREPEND(mreq, sizeof(u_long), M_WAIT); | |
fde1aeb2 | 813 | *mtod(mreq, u_long *) = htonl(0x80000000ul | len); |
15637ed4 RG |
814 | } |
815 | rep->r_mreq = mreq; | |
816 | ||
817 | /* | |
818 | * Do the client side RPC. | |
819 | */ | |
820 | nfsstats.rpcrequests++; | |
821 | /* | |
822 | * Chain request into list of outstanding requests. Be sure | |
823 | * to put it LAST so timer finds oldest requests first. | |
824 | */ | |
825 | s = splnet(); | |
826 | reph = &nfsreqh; | |
827 | reph->r_prev->r_next = rep; | |
828 | rep->r_prev = reph->r_prev; | |
829 | reph->r_prev = rep; | |
830 | rep->r_next = reph; | |
831 | /* | |
832 | * If backing off another request or avoiding congestion, don't | |
833 | * send this one now but let timer do it. If not timing a request, | |
834 | * do it now. | |
835 | */ | |
836 | if (nmp->nm_sent <= 0 || nmp->nm_sotype != SOCK_DGRAM || | |
837 | (nmp->nm_currexmit == 0 && nmp->nm_sent < nmp->nm_window)) { | |
838 | nmp->nm_sent++; | |
839 | rep->r_flags |= R_SENT; | |
840 | if (nmp->nm_rtt == -1) { | |
841 | nmp->nm_rtt = 0; | |
842 | rep->r_flags |= R_TIMING; | |
843 | } | |
844 | splx(s); | |
845 | m = m_copym(mreq, 0, M_COPYALL, M_WAIT); | |
846 | if (nmp->nm_soflags & PR_CONNREQUIRED) | |
847 | nfs_solock(&nmp->nm_flag); | |
848 | error = nfs_send(nmp->nm_so, nmp->nm_nam, m, rep); | |
849 | if (nmp->nm_soflags & PR_CONNREQUIRED) | |
850 | nfs_sounlock(&nmp->nm_flag); | |
851 | if (error && NFSIGNORE_SOERROR(nmp->nm_soflags, error)) | |
852 | nmp->nm_so->so_error = error = 0; | |
853 | } else | |
854 | splx(s); | |
855 | ||
856 | /* | |
857 | * Wait for the reply from our send or the timer's. | |
858 | */ | |
859 | if (!error) | |
860 | error = nfs_reply(nmp, rep); | |
861 | ||
862 | /* | |
863 | * RPC done, unlink the request. | |
864 | */ | |
865 | s = splnet(); | |
866 | rep->r_prev->r_next = rep->r_next; | |
867 | rep->r_next->r_prev = rep->r_prev; | |
868 | splx(s); | |
869 | ||
870 | /* | |
871 | * If there was a successful reply and a tprintf msg. | |
872 | * tprintf a response. | |
873 | */ | |
874 | if (!error && (rep->r_flags & R_TPRINTFMSG)) | |
875 | nfs_msg(rep->r_procp, nmp->nm_mountp->mnt_stat.f_mntfromname, | |
876 | "is alive again"); | |
877 | m_freem(rep->r_mreq); | |
878 | mrep = rep->r_mrep; | |
879 | FREE((caddr_t)rep, M_NFSREQ); | |
880 | if (error) | |
881 | return (error); | |
882 | ||
883 | if (compressed) | |
884 | mrep = nfs_uncompress(mrep); | |
885 | md = mrep; | |
886 | /* | |
887 | * break down the rpc header and check if ok | |
888 | */ | |
889 | dpos = mtod(md, caddr_t); | |
890 | nfsm_disect(tl, u_long *, 5*NFSX_UNSIGNED); | |
891 | tl += 2; | |
892 | if (*tl++ == rpc_msgdenied) { | |
893 | if (*tl == rpc_mismatch) | |
894 | error = EOPNOTSUPP; | |
895 | else | |
896 | error = EACCES; | |
897 | m_freem(mrep); | |
898 | return (error); | |
899 | } | |
900 | /* | |
901 | * skip over the auth_verf, someday we may want to cache auth_short's | |
902 | * for nfs_reqhead(), but for now just dump it | |
903 | */ | |
904 | if (*++tl != 0) { | |
905 | len = nfsm_rndup(fxdr_unsigned(long, *tl)); | |
906 | nfsm_adv(len); | |
907 | } | |
908 | nfsm_disect(tl, u_long *, NFSX_UNSIGNED); | |
909 | /* 0 == ok */ | |
910 | if (*tl == 0) { | |
911 | nfsm_disect(tl, u_long *, NFSX_UNSIGNED); | |
912 | if (*tl != 0) { | |
913 | error = fxdr_unsigned(int, *tl); | |
914 | m_freem(mrep); | |
915 | return (error); | |
916 | } | |
917 | *mrp = mrep; | |
918 | *mdp = md; | |
919 | *dposp = dpos; | |
920 | return (0); | |
921 | } | |
922 | m_freem(mrep); | |
923 | return (EPROTONOSUPPORT); | |
924 | nfsmout: | |
925 | return (error); | |
926 | } | |
927 | ||
928 | /* | |
929 | * Get a request for the server main loop | |
930 | * - receive a request via. nfs_soreceive() | |
931 | * - verify it | |
932 | * - fill in the cred struct. | |
933 | */ | |
a8599e88 | 934 | int |
15637ed4 RG |
935 | nfs_getreq(so, prog, vers, maxproc, nam, mrp, mdp, dposp, retxid, procnum, cr, |
936 | msk, mtch, wascomp, repstat) /* 08 Aug 92*/ | |
937 | struct socket *so; | |
938 | u_long prog; | |
939 | u_long vers; | |
940 | int maxproc; | |
941 | struct mbuf **nam; | |
942 | struct mbuf **mrp; | |
943 | struct mbuf **mdp; | |
944 | caddr_t *dposp; | |
945 | u_long *retxid; | |
946 | u_long *procnum; | |
947 | register struct ucred *cr; | |
948 | struct mbuf *msk, *mtch; | |
949 | int *wascomp, *repstat; /* 08 Aug 92*/ | |
950 | { | |
951 | register int i; | |
952 | register u_long *tl; | |
953 | register long t1; | |
954 | caddr_t dpos, cp2; | |
955 | int error = 0; | |
956 | struct mbuf *mrep, *md; | |
957 | int len; | |
958 | ||
959 | *repstat = 0; /* 08 Aug 92*/ | |
960 | if (so->so_proto->pr_flags & PR_CONNREQUIRED) { | |
961 | error = nfs_receive(so, nam, &mrep, (struct nfsreq *)0); | |
962 | } else { | |
963 | mrep = (struct mbuf *)0; | |
964 | do { | |
965 | if (mrep) { | |
966 | m_freem(*nam); | |
967 | m_freem(mrep); | |
968 | } | |
969 | error = nfs_receive(so, nam, &mrep, (struct nfsreq *)0); | |
970 | } while (!error && nfs_badnam(*nam, msk, mtch)); | |
971 | } | |
972 | if (error) | |
973 | return (error); | |
974 | md = mrep; | |
975 | mrep = nfs_uncompress(mrep); | |
976 | if (mrep != md) { | |
977 | *wascomp = 1; | |
978 | md = mrep; | |
979 | } else | |
980 | *wascomp = 0; | |
981 | dpos = mtod(mrep, caddr_t); | |
982 | nfsm_disect(tl, u_long *, 10*NFSX_UNSIGNED); | |
983 | *retxid = *tl++; | |
984 | if (*tl++ != rpc_call || *tl++ != rpc_vers) { /* 08 Aug 92*/ | |
985 | *mrp = mrep; | |
986 | *procnum = NFSPROC_NOOP; | |
987 | *repstat = ERPCMISMATCH; | |
988 | return (0); | |
989 | } | |
990 | if (*tl++ != prog) { | |
991 | *mrp = mrep; /* 08 Aug 92*/ | |
992 | *procnum = NFSPROC_NOOP; | |
993 | *repstat = EPROGUNAVAIL; | |
994 | return (0); | |
995 | } | |
996 | if (*tl++ != vers) { | |
997 | *mrp = mrep; /* 08 Aug 92*/ | |
998 | *procnum = NFSPROC_NOOP; | |
999 | *repstat = EPROGMISMATCH; | |
1000 | return (0); | |
1001 | } | |
1002 | *procnum = fxdr_unsigned(u_long, *tl++); | |
1003 | if (*procnum == NFSPROC_NULL) { | |
1004 | *mrp = mrep; | |
1005 | return (0); | |
1006 | } | |
1007 | if (*procnum > maxproc || *tl++ != rpc_auth_unix) { | |
1008 | *mrp = mrep; /* 08 Aug 92*/ | |
1009 | *procnum = NFSPROC_NOOP; | |
1010 | *repstat = EPROCUNAVAIL; | |
1011 | return (0); | |
1012 | } | |
1013 | len = fxdr_unsigned(int, *tl++); | |
1014 | if (len < 0 || len > RPCAUTH_MAXSIZ) { | |
1015 | m_freem(mrep); | |
1016 | return (EBADRPC); | |
1017 | } | |
1018 | len = fxdr_unsigned(int, *++tl); | |
1019 | if (len < 0 || len > NFS_MAXNAMLEN) { | |
1020 | m_freem(mrep); | |
1021 | return (EBADRPC); | |
1022 | } | |
1023 | nfsm_adv(nfsm_rndup(len)); | |
1024 | nfsm_disect(tl, u_long *, 3*NFSX_UNSIGNED); | |
1025 | cr->cr_uid = fxdr_unsigned(uid_t, *tl++); | |
1026 | cr->cr_gid = fxdr_unsigned(gid_t, *tl++); | |
1027 | len = fxdr_unsigned(int, *tl); | |
1028 | if (len < 0 || len > RPCAUTH_UNIXGIDS) { | |
1029 | m_freem(mrep); | |
1030 | return (EBADRPC); | |
1031 | } | |
1032 | nfsm_disect(tl, u_long *, (len + 2)*NFSX_UNSIGNED); | |
1033 | for (i = 1; i <= len; i++) | |
1034 | if (i < NGROUPS) | |
1035 | cr->cr_groups[i] = fxdr_unsigned(gid_t, *tl++); | |
1036 | else | |
1037 | tl++; | |
1038 | cr->cr_ngroups = (len >= NGROUPS) ? NGROUPS : (len + 1); | |
1039 | /* | |
1040 | * Do we have any use for the verifier. | |
1041 | * According to the "Remote Procedure Call Protocol Spec." it | |
1042 | * should be AUTH_NULL, but some clients make it AUTH_UNIX? | |
1043 | * For now, just skip over it | |
1044 | */ | |
1045 | len = fxdr_unsigned(int, *++tl); | |
1046 | if (len < 0 || len > RPCAUTH_MAXSIZ) { | |
1047 | m_freem(mrep); | |
1048 | return (EBADRPC); | |
1049 | } | |
1050 | if (len > 0) | |
1051 | nfsm_adv(nfsm_rndup(len)); | |
1052 | *mrp = mrep; | |
1053 | *mdp = md; | |
1054 | *dposp = dpos; | |
1055 | return (0); | |
1056 | nfsmout: | |
1057 | return (error); | |
1058 | } | |
1059 | ||
1060 | /* | |
1061 | * Generate the rpc reply header | |
1062 | * siz arg. is used to decide if adding a cluster is worthwhile | |
1063 | */ | |
a8599e88 | 1064 | int |
15637ed4 RG |
1065 | nfs_rephead(siz, retxid, err, mrq, mbp, bposp) |
1066 | int siz; | |
1067 | u_long retxid; | |
1068 | int err; | |
1069 | struct mbuf **mrq; | |
1070 | struct mbuf **mbp; | |
1071 | caddr_t *bposp; | |
1072 | { | |
1073 | register u_long *tl; | |
1074 | register long t1; | |
1075 | caddr_t bpos; | |
1076 | struct mbuf *mreq, *mb, *mb2; | |
1077 | ||
1078 | NFSMGETHDR(mreq); | |
1079 | mb = mreq; | |
1080 | if ((siz+RPC_REPLYSIZ) > MHLEN) | |
1081 | MCLGET(mreq, M_WAIT); | |
1082 | tl = mtod(mreq, u_long *); | |
1083 | mreq->m_len = 6*NFSX_UNSIGNED; | |
1084 | bpos = ((caddr_t)tl)+mreq->m_len; | |
1085 | *tl++ = retxid; | |
1086 | *tl++ = rpc_reply; | |
1087 | if (err == ERPCMISMATCH) { | |
1088 | *tl++ = rpc_msgdenied; | |
1089 | *tl++ = rpc_mismatch; | |
1090 | *tl++ = txdr_unsigned(2); | |
1091 | *tl = txdr_unsigned(2); | |
1092 | } else { | |
1093 | *tl++ = rpc_msgaccepted; | |
1094 | *tl++ = 0; | |
1095 | *tl++ = 0; | |
1096 | switch (err) { | |
1097 | case EPROGUNAVAIL: | |
1098 | *tl = txdr_unsigned(RPC_PROGUNAVAIL); | |
1099 | break; | |
1100 | case EPROGMISMATCH: | |
1101 | *tl = txdr_unsigned(RPC_PROGMISMATCH); | |
1102 | nfsm_build(tl, u_long *, 2*NFSX_UNSIGNED); | |
1103 | *tl++ = txdr_unsigned(2); | |
1104 | *tl = txdr_unsigned(2); /* someday 3 */ | |
1105 | break; | |
1106 | case EPROCUNAVAIL: | |
1107 | *tl = txdr_unsigned(RPC_PROCUNAVAIL); | |
1108 | break; | |
1109 | default: | |
1110 | *tl = 0; | |
1111 | if (err != VNOVAL) { | |
1112 | nfsm_build(tl, u_long *, NFSX_UNSIGNED); | |
1113 | *tl = txdr_unsigned(err); | |
1114 | } | |
1115 | break; | |
1116 | }; | |
1117 | } | |
1118 | *mrq = mreq; | |
1119 | *mbp = mb; | |
1120 | *bposp = bpos; | |
1121 | if (err != 0 && err != VNOVAL) | |
1122 | nfsstats.srvrpc_errs++; | |
1123 | return (0); | |
1124 | } | |
1125 | ||
1126 | /* | |
1127 | * Nfs timer routine | |
1128 | * Scan the nfsreq list and retranmit any requests that have timed out | |
1129 | * To avoid retransmission attempts on STREAM sockets (in the future) make | |
1130 | * sure to set the r_retry field to 0 (implies nm_retry == 0). | |
1131 | */ | |
a8599e88 RG |
1132 | void |
1133 | nfs_timer(arg) | |
1134 | caddr_t arg; | |
15637ed4 RG |
1135 | { |
1136 | register struct nfsreq *rep; | |
1137 | register struct mbuf *m; | |
1138 | register struct socket *so; | |
1139 | register struct nfsmount *nmp; | |
1140 | int s, error; | |
1141 | ||
1142 | s = splnet(); | |
1143 | for (rep = nfsreqh.r_next; rep != &nfsreqh; rep = rep->r_next) { | |
1144 | nmp = rep->r_nmp; | |
1145 | if (rep->r_mrep || (rep->r_flags & R_SOFTTERM) || | |
1146 | (so = nmp->nm_so) == NULL) | |
1147 | continue; | |
1148 | if ((nmp->nm_flag & NFSMNT_INT) && nfs_sigintr(rep->r_procp)) { | |
1149 | rep->r_flags |= R_SOFTTERM; | |
1150 | continue; | |
1151 | } | |
1152 | if (rep->r_flags & R_TIMING) /* update rtt in mount */ | |
1153 | nmp->nm_rtt++; | |
1154 | /* If not timed out */ | |
1155 | if (++rep->r_timer < nmp->nm_rto) | |
1156 | continue; | |
1157 | /* Do backoff and save new timeout in mount */ | |
1158 | if (rep->r_flags & R_TIMING) { | |
1159 | nfs_backofftimer(nmp); | |
1160 | rep->r_flags &= ~R_TIMING; | |
1161 | nmp->nm_rtt = -1; | |
1162 | } | |
1163 | if (rep->r_flags & R_SENT) { | |
1164 | rep->r_flags &= ~R_SENT; | |
1165 | nmp->nm_sent--; | |
1166 | } | |
1167 | ||
1168 | /* | |
1169 | * Check for too many retries on soft mount. | |
1170 | * nb: For hard mounts, r_retry == NFS_MAXREXMIT+1 | |
1171 | */ | |
1172 | if (++rep->r_rexmit > NFS_MAXREXMIT) | |
1173 | rep->r_rexmit = NFS_MAXREXMIT; | |
1174 | ||
1175 | /* | |
1176 | * Check for server not responding | |
1177 | */ | |
1178 | if ((rep->r_flags & R_TPRINTFMSG) == 0 && | |
1179 | rep->r_rexmit > NFS_FISHY) { | |
1180 | nfs_msg(rep->r_procp, | |
1181 | nmp->nm_mountp->mnt_stat.f_mntfromname, | |
1182 | "not responding"); | |
1183 | rep->r_flags |= R_TPRINTFMSG; | |
1184 | } | |
1185 | if (rep->r_rexmit >= rep->r_retry) { /* too many */ | |
1186 | nfsstats.rpctimeouts++; | |
1187 | rep->r_flags |= R_SOFTTERM; | |
1188 | continue; | |
1189 | } | |
1190 | if (nmp->nm_sotype != SOCK_DGRAM) | |
1191 | continue; | |
1192 | ||
1193 | /* | |
1194 | * If there is enough space and the window allows.. | |
1195 | * Resend it | |
1196 | */ | |
1197 | if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len && | |
1198 | nmp->nm_sent < nmp->nm_window && | |
1199 | (m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))){ | |
1200 | nfsstats.rpcretries++; | |
1201 | if ((nmp->nm_flag & NFSMNT_NOCONN) == 0) | |
1202 | error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m, | |
a8599e88 RG |
1203 | (struct mbuf *)0, (struct mbuf *)0, |
1204 | (struct mbuf *)0); | |
15637ed4 RG |
1205 | else |
1206 | error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m, | |
1207 | nmp->nm_nam, (struct mbuf *)0, (struct mbuf *)0); | |
1208 | if (error) { | |
1209 | if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) | |
1210 | so->so_error = 0; | |
1211 | } else { | |
1212 | /* | |
1213 | * We need to time the request even though we | |
1214 | * are retransmitting. | |
1215 | */ | |
1216 | nmp->nm_rtt = 0; | |
1217 | nmp->nm_sent++; | |
1218 | rep->r_flags |= (R_SENT|R_TIMING); | |
1219 | rep->r_timer = rep->r_timerinit; | |
1220 | } | |
1221 | } | |
1222 | } | |
1223 | splx(s); | |
1224 | timeout(nfs_timer, (caddr_t)0, hz/NFS_HZ); | |
1225 | } | |
1226 | ||
1227 | /* | |
1228 | * NFS timer update and backoff. The "Jacobson/Karels/Karn" scheme is | |
1229 | * used here. The timer state is held in the nfsmount structure and | |
1230 | * a single request is used to clock the response. When successful | |
1231 | * the rtt smoothing in nfs_updatetimer is used, when failed the backoff | |
1232 | * is done by nfs_backofftimer. We also log failure messages in these | |
1233 | * routines. | |
1234 | * | |
1235 | * Congestion variables are held in the nfshost structure which | |
1236 | * is referenced by nfsmounts and shared per-server. This separation | |
1237 | * makes it possible to do per-mount timing which allows varying disk | |
1238 | * access times to be dealt with, while preserving a network oriented | |
1239 | * congestion control scheme. | |
1240 | * | |
1241 | * The windowing implements the Jacobson/Karels slowstart algorithm | |
1242 | * with adjusted scaling factors. We start with one request, then send | |
1243 | * 4 more after each success until the ssthresh limit is reached, then | |
1244 | * we increment at a rate proportional to the window. On failure, we | |
1245 | * remember 3/4 the current window and clamp the send limit to 1. Note | |
1246 | * ICMP source quench is not reflected in so->so_error so we ignore that | |
1247 | * for now. | |
1248 | * | |
1249 | * NFS behaves much more like a transport protocol with these changes, | |
1250 | * shedding the teenage pedal-to-the-metal tendencies of "other" | |
1251 | * implementations. | |
1252 | * | |
1253 | * Timers and congestion avoidance by Tom Talpey, Open Software Foundation. | |
1254 | */ | |
1255 | ||
1256 | /* | |
1257 | * The TCP algorithm was not forgiving enough. Because the NFS server | |
1258 | * responds only after performing lookups/diskio/etc, we have to be | |
1259 | * more prepared to accept a spiky variance. The TCP algorithm is: | |
1260 | * TCP_RTO(nmp) ((((nmp)->nm_srtt >> 2) + (nmp)->nm_rttvar) >> 1) | |
1261 | */ | |
1262 | #define NFS_RTO(nmp) (((nmp)->nm_srtt >> 3) + (nmp)->nm_rttvar) | |
1263 | ||
a8599e88 | 1264 | void |
15637ed4 RG |
1265 | nfs_updatetimer(nmp) |
1266 | register struct nfsmount *nmp; | |
1267 | { | |
1268 | ||
1269 | /* If retransmitted, clear and return */ | |
1270 | if (nmp->nm_rexmit || nmp->nm_currexmit) { | |
1271 | nmp->nm_rexmit = nmp->nm_currexmit = 0; | |
1272 | return; | |
1273 | } | |
1274 | /* If have a measurement, do smoothing */ | |
1275 | if (nmp->nm_srtt) { | |
1276 | register short delta; | |
1277 | delta = nmp->nm_rtt - (nmp->nm_srtt >> 3); | |
1278 | if ((nmp->nm_srtt += delta) <= 0) | |
1279 | nmp->nm_srtt = 1; | |
1280 | if (delta < 0) | |
1281 | delta = -delta; | |
1282 | delta -= (nmp->nm_rttvar >> 2); | |
1283 | if ((nmp->nm_rttvar += delta) <= 0) | |
1284 | nmp->nm_rttvar = 1; | |
1285 | /* Else initialize */ | |
1286 | } else { | |
1287 | nmp->nm_rttvar = nmp->nm_rtt << 1; | |
1288 | if (nmp->nm_rttvar == 0) nmp->nm_rttvar = 2; | |
1289 | nmp->nm_srtt = nmp->nm_rttvar << 2; | |
1290 | } | |
1291 | /* Compute new Retransmission TimeOut and clip */ | |
1292 | nmp->nm_rto = NFS_RTO(nmp); | |
1293 | if (nmp->nm_rto < NFS_MINTIMEO) | |
1294 | nmp->nm_rto = NFS_MINTIMEO; | |
1295 | else if (nmp->nm_rto > NFS_MAXTIMEO) | |
1296 | nmp->nm_rto = NFS_MAXTIMEO; | |
1297 | ||
1298 | /* Update window estimate */ | |
1299 | if (nmp->nm_window < nmp->nm_ssthresh) /* quickly */ | |
1300 | nmp->nm_window += 4; | |
1301 | else { /* slowly */ | |
1302 | register long incr = ++nmp->nm_winext; | |
1303 | incr = (incr * incr) / nmp->nm_window; | |
1304 | if (incr > 0) { | |
1305 | nmp->nm_winext = 0; | |
1306 | ++nmp->nm_window; | |
1307 | } | |
1308 | } | |
1309 | if (nmp->nm_window > NFS_MAXWINDOW) | |
1310 | nmp->nm_window = NFS_MAXWINDOW; | |
1311 | } | |
1312 | ||
a8599e88 | 1313 | void |
15637ed4 RG |
1314 | nfs_backofftimer(nmp) |
1315 | register struct nfsmount *nmp; | |
1316 | { | |
1317 | register unsigned long newrto; | |
1318 | ||
1319 | /* Clip shift count */ | |
1320 | if (++nmp->nm_rexmit > 8 * sizeof nmp->nm_rto) | |
1321 | nmp->nm_rexmit = 8 * sizeof nmp->nm_rto; | |
1322 | /* Back off RTO exponentially */ | |
1323 | newrto = NFS_RTO(nmp); | |
1324 | newrto <<= (nmp->nm_rexmit - 1); | |
1325 | if (newrto == 0 || newrto > NFS_MAXTIMEO) | |
1326 | newrto = NFS_MAXTIMEO; | |
1327 | nmp->nm_rto = newrto; | |
1328 | ||
1329 | /* If too many retries, message, assume a bogus RTT and re-measure */ | |
1330 | if (nmp->nm_currexmit < nmp->nm_rexmit) { | |
1331 | nmp->nm_currexmit = nmp->nm_rexmit; | |
1332 | if (nmp->nm_currexmit >= nfsrexmtthresh) { | |
1333 | if (nmp->nm_currexmit == nfsrexmtthresh) { | |
1334 | nmp->nm_rttvar += (nmp->nm_srtt >> 2); | |
1335 | nmp->nm_srtt = 0; | |
1336 | } | |
1337 | } | |
1338 | } | |
1339 | /* Close down window but remember this point (3/4 current) for later */ | |
1340 | nmp->nm_ssthresh = ((nmp->nm_window << 1) + nmp->nm_window) >> 2; | |
1341 | nmp->nm_window = 1; | |
1342 | nmp->nm_winext = 0; | |
1343 | } | |
1344 | ||
1345 | /* | |
1346 | * Test for a termination signal pending on procp. | |
1347 | * This is used for NFSMNT_INT mounts. | |
1348 | */ | |
a8599e88 | 1349 | int |
15637ed4 RG |
1350 | nfs_sigintr(p) |
1351 | register struct proc *p; | |
1352 | { | |
1353 | if (p && p->p_sig && (((p->p_sig &~ p->p_sigmask) &~ p->p_sigignore) & | |
1354 | NFSINT_SIGMASK)) | |
1355 | return (1); | |
1356 | else | |
1357 | return (0); | |
1358 | } | |
1359 | ||
a8599e88 | 1360 | void |
15637ed4 RG |
1361 | nfs_msg(p, server, msg) |
1362 | struct proc *p; | |
a8599e88 | 1363 | const char *server, *msg; |
15637ed4 RG |
1364 | { |
1365 | tpr_t tpr; | |
1366 | ||
1367 | if (p) | |
1368 | tpr = tprintf_open(p); | |
1369 | else | |
1370 | tpr = NULL; | |
1371 | tprintf(tpr, "nfs server %s: %s\n", server, msg); | |
1372 | tprintf_close(tpr); | |
1373 | } | |
1374 | ||
1375 | /* | |
1376 | * Lock a socket against others. | |
1377 | * Necessary for STREAM sockets to ensure you get an entire rpc request/reply | |
1378 | * and also to avoid race conditions between the processes with nfs requests | |
1379 | * in progress when a reconnect is necessary. | |
1380 | */ | |
a8599e88 | 1381 | void |
15637ed4 RG |
1382 | nfs_solock(flagp) |
1383 | register int *flagp; | |
1384 | { | |
1385 | ||
1386 | while (*flagp & NFSMNT_SCKLOCK) { | |
1387 | *flagp |= NFSMNT_WANTSCK; | |
1388 | (void) tsleep((caddr_t)flagp, PZERO-1, "nfsolck", 0); | |
1389 | } | |
1390 | *flagp |= NFSMNT_SCKLOCK; | |
1391 | } | |
1392 | ||
1393 | /* | |
1394 | * Unlock the stream socket for others. | |
1395 | */ | |
a8599e88 | 1396 | void |
15637ed4 RG |
1397 | nfs_sounlock(flagp) |
1398 | register int *flagp; | |
1399 | { | |
1400 | ||
1401 | if ((*flagp & NFSMNT_SCKLOCK) == 0) | |
1402 | panic("nfs sounlock"); | |
1403 | *flagp &= ~NFSMNT_SCKLOCK; | |
1404 | if (*flagp & NFSMNT_WANTSCK) { | |
1405 | *flagp &= ~NFSMNT_WANTSCK; | |
1406 | wakeup((caddr_t)flagp); | |
1407 | } | |
1408 | } | |
1409 | ||
1410 | /* | |
1411 | * This function compares two net addresses by family and returns TRUE | |
1412 | * if they are the same. | |
1413 | * If there is any doubt, return FALSE. | |
1414 | */ | |
a8599e88 | 1415 | int |
15637ed4 RG |
1416 | nfs_netaddr_match(nam1, nam2) |
1417 | struct mbuf *nam1, *nam2; | |
1418 | { | |
1419 | register struct sockaddr *saddr1, *saddr2; | |
1420 | ||
1421 | saddr1 = mtod(nam1, struct sockaddr *); | |
1422 | saddr2 = mtod(nam2, struct sockaddr *); | |
1423 | if (saddr1->sa_family != saddr2->sa_family) | |
1424 | return (0); | |
1425 | ||
1426 | /* | |
1427 | * Must do each address family separately since unused fields | |
1428 | * are undefined values and not always zeroed. | |
1429 | */ | |
1430 | switch (saddr1->sa_family) { | |
1431 | case AF_INET: | |
1432 | if (((struct sockaddr_in *)saddr1)->sin_addr.s_addr == | |
1433 | ((struct sockaddr_in *)saddr2)->sin_addr.s_addr) | |
1434 | return (1); | |
1435 | break; | |
1436 | default: | |
1437 | break; | |
1438 | }; | |
1439 | return (0); | |
1440 | } | |
1441 | ||
1442 | /* | |
1443 | * Check the hostname fields for nfsd's mask and match fields. | |
1444 | * By address family: | |
1445 | * - Bitwise AND the mask with the host address field | |
1446 | * - Compare for == with match | |
1447 | * return TRUE if not equal | |
1448 | */ | |
a8599e88 | 1449 | int |
15637ed4 RG |
1450 | nfs_badnam(nam, msk, mtch) |
1451 | register struct mbuf *nam, *msk, *mtch; | |
1452 | { | |
1453 | switch (mtod(nam, struct sockaddr *)->sa_family) { | |
1454 | case AF_INET: | |
1455 | return ((mtod(nam, struct sockaddr_in *)->sin_addr.s_addr & | |
1456 | mtod(msk, struct sockaddr_in *)->sin_addr.s_addr) != | |
1457 | mtod(mtch, struct sockaddr_in *)->sin_addr.s_addr); | |
1458 | default: | |
1459 | printf("nfs_badmatch, unknown sa_family\n"); | |
1460 | return (0); | |
1461 | }; | |
1462 | } |