nfs_socket.c revision 1.24 1 /* $NetBSD: nfs_socket.c,v 1.24 1996/02/18 11:53:48 fvdl Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1991, 1993, 1995
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)nfs_socket.c 8.5 (Berkeley) 3/30/95
39 */
40
41 /*
42 * Socket operations for use by nfs
43 */
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/proc.h>
48 #include <sys/mount.h>
49 #include <sys/kernel.h>
50 #include <sys/mbuf.h>
51 #include <sys/vnode.h>
52 #include <sys/domain.h>
53 #include <sys/protosw.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56 #include <sys/syslog.h>
57 #include <sys/tprintf.h>
58 #include <sys/namei.h>
59
60 #include <netinet/in.h>
61 #include <netinet/tcp.h>
62
63 #include <nfs/rpcv2.h>
64 #include <nfs/nfsproto.h>
65 #include <nfs/nfs.h>
66 #include <nfs/xdr_subs.h>
67 #include <nfs/nfsm_subs.h>
68 #include <nfs/nfsmount.h>
69 #include <nfs/nfsnode.h>
70 #include <nfs/nfsrtt.h>
71 #include <nfs/nqnfs.h>
72 #include <nfs/nfs_var.h>
73
74 #define TRUE 1
75 #define FALSE 0
76
77 /*
78 * Estimate rto for an nfs rpc sent via. an unreliable datagram.
79 * Use the mean and mean deviation of rtt for the appropriate type of rpc
80 * for the frequent rpcs and a default for the others.
81 * The justification for doing "other" this way is that these rpcs
82 * happen so infrequently that timer est. would probably be stale.
83 * Also, since many of these rpcs are
84 * non-idempotent, a conservative timeout is desired.
85 * getattr, lookup - A+2D
86 * read, write - A+4D
87 * other - nm_timeo
88 */
89 #define NFS_RTO(n, t) \
90 ((t) == 0 ? (n)->nm_timeo : \
91 ((t) < 3 ? \
92 (((((n)->nm_srtt[t-1] + 3) >> 2) + (n)->nm_sdrtt[t-1] + 1) >> 1) : \
93 ((((n)->nm_srtt[t-1] + 7) >> 3) + (n)->nm_sdrtt[t-1] + 1)))
94 #define NFS_SRTT(r) (r)->r_nmp->nm_srtt[proct[(r)->r_procnum] - 1]
95 #define NFS_SDRTT(r) (r)->r_nmp->nm_sdrtt[proct[(r)->r_procnum] - 1]
96 /*
97 * External data, mostly RPC constants in XDR form
98 */
99 extern u_int32_t rpc_reply, rpc_msgdenied, rpc_mismatch, rpc_vers,
100 rpc_auth_unix, rpc_msgaccepted, rpc_call, rpc_autherr,
101 rpc_auth_kerb;
102 extern u_int32_t nfs_prog, nqnfs_prog;
103 extern time_t nqnfsstarttime;
104 extern struct nfsstats nfsstats;
105 extern int nfsv3_procid[NFS_NPROCS];
106 extern int nfs_ticks;
107
108 /*
109 * Defines which timer to use for the procnum.
110 * 0 - default
111 * 1 - getattr
112 * 2 - lookup
113 * 3 - read
114 * 4 - write
115 */
116 static int proct[NFS_NPROCS] = {
117 0, 1, 0, 2, 1, 3, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0,
118 0, 0, 0,
119 };
120
121 /*
122 * There is a congestion window for outstanding rpcs maintained per mount
123 * point. The cwnd size is adjusted in roughly the way that:
124 * Van Jacobson, Congestion avoidance and Control, In "Proceedings of
125 * SIGCOMM '88". ACM, August 1988.
126 * describes for TCP. The cwnd size is chopped in half on a retransmit timeout
127 * and incremented by 1/cwnd when each rpc reply is received and a full cwnd
128 * of rpcs is in progress.
129 * (The sent count and cwnd are scaled for integer arith.)
130 * Variants of "slow start" were tried and were found to be too much of a
131 * performance hit (ave. rtt 3 times larger),
132 * I suspect due to the large rtt that nfs rpcs have.
133 */
134 #define NFS_CWNDSCALE 256
135 #define NFS_MAXCWND (NFS_CWNDSCALE * 32)
136 static int nfs_backoff[8] = { 2, 4, 8, 16, 32, 64, 128, 256, };
137 int nfsrtton = 0;
138 struct nfsrtt nfsrtt;
139
140 /*
141 * Initialize sockets and congestion for a new NFS connection.
142 * We do not free the sockaddr if error.
143 */
144 int
145 nfs_connect(nmp, rep)
146 register struct nfsmount *nmp;
147 struct nfsreq *rep;
148 {
149 register struct socket *so;
150 int s, error, rcvreserve, sndreserve;
151 struct sockaddr *saddr;
152 struct sockaddr_in *sin;
153 struct mbuf *m;
154 u_int16_t tport;
155
156 nmp->nm_so = (struct socket *)0;
157 saddr = mtod(nmp->nm_nam, struct sockaddr *);
158 error = socreate(saddr->sa_family, &nmp->nm_so, nmp->nm_sotype,
159 nmp->nm_soproto);
160 if (error)
161 goto bad;
162 so = nmp->nm_so;
163 nmp->nm_soflags = so->so_proto->pr_flags;
164
165 /*
166 * Some servers require that the client port be a reserved port number.
167 */
168 if (saddr->sa_family == AF_INET && (nmp->nm_flag & NFSMNT_RESVPORT)) {
169 MGET(m, M_WAIT, MT_SONAME);
170 sin = mtod(m, struct sockaddr_in *);
171 sin->sin_len = m->m_len = sizeof (struct sockaddr_in);
172 sin->sin_family = AF_INET;
173 sin->sin_addr.s_addr = INADDR_ANY;
174 tport = IPPORT_RESERVED - 1;
175 sin->sin_port = htons(tport);
176 while ((error = sobind(so, m)) == EADDRINUSE &&
177 --tport > IPPORT_RESERVED / 2)
178 sin->sin_port = htons(tport);
179 m_freem(m);
180 if (error)
181 goto bad;
182 }
183
184 /*
185 * Protocols that do not require connections may be optionally left
186 * unconnected for servers that reply from a port other than NFS_PORT.
187 */
188 if (nmp->nm_flag & NFSMNT_NOCONN) {
189 if (nmp->nm_soflags & PR_CONNREQUIRED) {
190 error = ENOTCONN;
191 goto bad;
192 }
193 } else {
194 error = soconnect(so, nmp->nm_nam);
195 if (error)
196 goto bad;
197
198 /*
199 * Wait for the connection to complete. Cribbed from the
200 * connect system call but with the wait timing out so
201 * that interruptible mounts don't hang here for a long time.
202 */
203 s = splsoftnet();
204 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
205 (void) tsleep((caddr_t)&so->so_timeo, PSOCK,
206 "nfscon", 2 * hz);
207 if ((so->so_state & SS_ISCONNECTING) &&
208 so->so_error == 0 && rep &&
209 (error = nfs_sigintr(nmp, rep, rep->r_procp)) != 0){
210 so->so_state &= ~SS_ISCONNECTING;
211 splx(s);
212 goto bad;
213 }
214 }
215 if (so->so_error) {
216 error = so->so_error;
217 so->so_error = 0;
218 splx(s);
219 goto bad;
220 }
221 splx(s);
222 }
223 if (nmp->nm_flag & (NFSMNT_SOFT | NFSMNT_INT)) {
224 so->so_rcv.sb_timeo = (5 * hz);
225 so->so_snd.sb_timeo = (5 * hz);
226 } else {
227 so->so_rcv.sb_timeo = 0;
228 so->so_snd.sb_timeo = 0;
229 }
230 if (nmp->nm_sotype == SOCK_DGRAM) {
231 sndreserve = nmp->nm_wsize + NFS_MAXPKTHDR;
232 rcvreserve = nmp->nm_rsize + NFS_MAXPKTHDR;
233 } else if (nmp->nm_sotype == SOCK_SEQPACKET) {
234 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * 2;
235 rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR) * 2;
236 } else {
237 if (nmp->nm_sotype != SOCK_STREAM)
238 panic("nfscon sotype");
239 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
240 MGET(m, M_WAIT, MT_SOOPTS);
241 *mtod(m, int32_t *) = 1;
242 m->m_len = sizeof(int32_t);
243 sosetopt(so, SOL_SOCKET, SO_KEEPALIVE, m);
244 }
245 if (so->so_proto->pr_protocol == IPPROTO_TCP) {
246 MGET(m, M_WAIT, MT_SOOPTS);
247 *mtod(m, int32_t *) = 1;
248 m->m_len = sizeof(int32_t);
249 sosetopt(so, IPPROTO_TCP, TCP_NODELAY, m);
250 }
251 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR +
252 sizeof (u_int32_t)) * 2;
253 rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR +
254 sizeof (u_int32_t)) * 2;
255 }
256 error = soreserve(so, sndreserve, rcvreserve);
257 if (error)
258 goto bad;
259 so->so_rcv.sb_flags |= SB_NOINTR;
260 so->so_snd.sb_flags |= SB_NOINTR;
261
262 /* Initialize other non-zero congestion variables */
263 nmp->nm_srtt[0] = nmp->nm_srtt[1] = nmp->nm_srtt[2] = nmp->nm_srtt[3] =
264 nmp->nm_srtt[4] = (NFS_TIMEO << 3);
265 nmp->nm_sdrtt[0] = nmp->nm_sdrtt[1] = nmp->nm_sdrtt[2] =
266 nmp->nm_sdrtt[3] = nmp->nm_sdrtt[4] = 0;
267 nmp->nm_cwnd = NFS_MAXCWND / 2; /* Initial send window */
268 nmp->nm_sent = 0;
269 nmp->nm_timeouts = 0;
270 return (0);
271
272 bad:
273 nfs_disconnect(nmp);
274 return (error);
275 }
276
277 /*
278 * Reconnect routine:
279 * Called when a connection is broken on a reliable protocol.
280 * - clean up the old socket
281 * - nfs_connect() again
282 * - set R_MUSTRESEND for all outstanding requests on mount point
283 * If this fails the mount point is DEAD!
284 * nb: Must be called with the nfs_sndlock() set on the mount point.
285 */
286 int
287 nfs_reconnect(rep)
288 register struct nfsreq *rep;
289 {
290 register struct nfsreq *rp;
291 register struct nfsmount *nmp = rep->r_nmp;
292 int error;
293
294 nfs_disconnect(nmp);
295 while ((error = nfs_connect(nmp, rep)) != 0) {
296 if (error == EINTR || error == ERESTART)
297 return (EINTR);
298 (void) tsleep((caddr_t)&lbolt, PSOCK, "nfscon", 0);
299 }
300
301 /*
302 * Loop through outstanding request list and fix up all requests
303 * on old socket.
304 */
305 for (rp = nfs_reqq.tqh_first; rp != 0; rp = rp->r_chain.tqe_next) {
306 if (rp->r_nmp == nmp)
307 rp->r_flags |= R_MUSTRESEND;
308 }
309 return (0);
310 }
311
312 /*
313 * NFS disconnect. Clean up and unlink.
314 */
315 void
316 nfs_disconnect(nmp)
317 register struct nfsmount *nmp;
318 {
319 register struct socket *so;
320
321 if (nmp->nm_so) {
322 so = nmp->nm_so;
323 nmp->nm_so = (struct socket *)0;
324 soshutdown(so, 2);
325 soclose(so);
326 }
327 }
328
329 /*
330 * This is the nfs send routine. For connection based socket types, it
331 * must be called with an nfs_sndlock() on the socket.
332 * "rep == NULL" indicates that it has been called from a server.
333 * For the client side:
334 * - return EINTR if the RPC is terminated, 0 otherwise
335 * - set R_MUSTRESEND if the send fails for any reason
336 * - do any cleanup required by recoverable socket errors (???)
337 * For the server side:
338 * - return EINTR or ERESTART if interrupted by a signal
339 * - return EPIPE if a connection is lost for connection based sockets (TCP...)
340 * - do any cleanup required by recoverable socket errors (???)
341 */
342 int
343 nfs_send(so, nam, top, rep)
344 register struct socket *so;
345 struct mbuf *nam;
346 register struct mbuf *top;
347 struct nfsreq *rep;
348 {
349 struct mbuf *sendnam;
350 int error, soflags, flags;
351
352 if (rep) {
353 if (rep->r_flags & R_SOFTTERM) {
354 m_freem(top);
355 return (EINTR);
356 }
357 if ((so = rep->r_nmp->nm_so) == NULL) {
358 rep->r_flags |= R_MUSTRESEND;
359 m_freem(top);
360 return (0);
361 }
362 rep->r_flags &= ~R_MUSTRESEND;
363 soflags = rep->r_nmp->nm_soflags;
364 } else
365 soflags = so->so_proto->pr_flags;
366 if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED))
367 sendnam = (struct mbuf *)0;
368 else
369 sendnam = nam;
370 if (so->so_type == SOCK_SEQPACKET)
371 flags = MSG_EOR;
372 else
373 flags = 0;
374
375 error = sosend(so, sendnam, (struct uio *)0, top,
376 (struct mbuf *)0, flags);
377 if (error) {
378 if (rep) {
379 log(LOG_INFO, "nfs send error %d for server %s\n",error,
380 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
381 /*
382 * Deal with errors for the client side.
383 */
384 if (rep->r_flags & R_SOFTTERM)
385 error = EINTR;
386 else
387 rep->r_flags |= R_MUSTRESEND;
388 } else
389 log(LOG_INFO, "nfsd send error %d\n", error);
390
391 /*
392 * Handle any recoverable (soft) socket errors here. (???)
393 */
394 if (error != EINTR && error != ERESTART &&
395 error != EWOULDBLOCK && error != EPIPE)
396 error = 0;
397 }
398 return (error);
399 }
400
401 #ifdef NFSCLIENT
402 /*
403 * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all
404 * done by soreceive(), but for SOCK_STREAM we must deal with the Record
405 * Mark and consolidate the data into a new mbuf list.
406 * nb: Sometimes TCP passes the data up to soreceive() in long lists of
407 * small mbufs.
408 * For SOCK_STREAM we must be very careful to read an entire record once
409 * we have read any of it, even if the system call has been interrupted.
410 */
411 int
412 nfs_receive(rep, aname, mp)
413 register struct nfsreq *rep;
414 struct mbuf **aname;
415 struct mbuf **mp;
416 {
417 register struct socket *so;
418 struct uio auio;
419 struct iovec aio;
420 register struct mbuf *m;
421 struct mbuf *control;
422 u_int32_t len;
423 struct mbuf **getnam;
424 int error, sotype, rcvflg;
425 struct proc *p = curproc; /* XXX */
426
427 /*
428 * Set up arguments for soreceive()
429 */
430 *mp = (struct mbuf *)0;
431 *aname = (struct mbuf *)0;
432 sotype = rep->r_nmp->nm_sotype;
433
434 /*
435 * For reliable protocols, lock against other senders/receivers
436 * in case a reconnect is necessary.
437 * For SOCK_STREAM, first get the Record Mark to find out how much
438 * more there is to get.
439 * We must lock the socket against other receivers
440 * until we have an entire rpc request/reply.
441 */
442 if (sotype != SOCK_DGRAM) {
443 error = nfs_sndlock(&rep->r_nmp->nm_flag, rep);
444 if (error)
445 return (error);
446 tryagain:
447 /*
448 * Check for fatal errors and resending request.
449 */
450 /*
451 * Ugh: If a reconnect attempt just happened, nm_so
452 * would have changed. NULL indicates a failed
453 * attempt that has essentially shut down this
454 * mount point.
455 */
456 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM)) {
457 nfs_sndunlock(&rep->r_nmp->nm_flag);
458 return (EINTR);
459 }
460 so = rep->r_nmp->nm_so;
461 if (!so) {
462 error = nfs_reconnect(rep);
463 if (error) {
464 nfs_sndunlock(&rep->r_nmp->nm_flag);
465 return (error);
466 }
467 goto tryagain;
468 }
469 while (rep->r_flags & R_MUSTRESEND) {
470 m = m_copym(rep->r_mreq, 0, M_COPYALL, M_WAIT);
471 nfsstats.rpcretries++;
472 error = nfs_send(so, rep->r_nmp->nm_nam, m, rep);
473 if (error) {
474 if (error == EINTR || error == ERESTART ||
475 (error = nfs_reconnect(rep)) != 0) {
476 nfs_sndunlock(&rep->r_nmp->nm_flag);
477 return (error);
478 }
479 goto tryagain;
480 }
481 }
482 nfs_sndunlock(&rep->r_nmp->nm_flag);
483 if (sotype == SOCK_STREAM) {
484 aio.iov_base = (caddr_t) &len;
485 aio.iov_len = sizeof(u_int32_t);
486 auio.uio_iov = &aio;
487 auio.uio_iovcnt = 1;
488 auio.uio_segflg = UIO_SYSSPACE;
489 auio.uio_rw = UIO_READ;
490 auio.uio_offset = 0;
491 auio.uio_resid = sizeof(u_int32_t);
492 auio.uio_procp = p;
493 do {
494 rcvflg = MSG_WAITALL;
495 error = soreceive(so, (struct mbuf **)0, &auio,
496 (struct mbuf **)0, (struct mbuf **)0, &rcvflg);
497 if (error == EWOULDBLOCK && rep) {
498 if (rep->r_flags & R_SOFTTERM)
499 return (EINTR);
500 }
501 } while (error == EWOULDBLOCK);
502 if (!error && auio.uio_resid > 0) {
503 log(LOG_INFO,
504 "short receive (%d/%d) from nfs server %s\n",
505 sizeof(u_int32_t) - auio.uio_resid,
506 sizeof(u_int32_t),
507 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
508 error = EPIPE;
509 }
510 if (error)
511 goto errout;
512 len = ntohl(len) & ~0x80000000;
513 /*
514 * This is SERIOUS! We are out of sync with the sender
515 * and forcing a disconnect/reconnect is all I can do.
516 */
517 if (len > NFS_MAXPACKET) {
518 log(LOG_ERR, "%s (%d) from nfs server %s\n",
519 "impossible packet length",
520 len,
521 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
522 error = EFBIG;
523 goto errout;
524 }
525 auio.uio_resid = len;
526 do {
527 rcvflg = MSG_WAITALL;
528 error = soreceive(so, (struct mbuf **)0,
529 &auio, mp, (struct mbuf **)0, &rcvflg);
530 } while (error == EWOULDBLOCK || error == EINTR ||
531 error == ERESTART);
532 if (!error && auio.uio_resid > 0) {
533 log(LOG_INFO,
534 "short receive (%d/%d) from nfs server %s\n",
535 len - auio.uio_resid, len,
536 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
537 error = EPIPE;
538 }
539 } else {
540 /*
541 * NB: Since uio_resid is big, MSG_WAITALL is ignored
542 * and soreceive() will return when it has either a
543 * control msg or a data msg.
544 * We have no use for control msg., but must grab them
545 * and then throw them away so we know what is going
546 * on.
547 */
548 auio.uio_resid = len = 100000000; /* Anything Big */
549 auio.uio_procp = p;
550 do {
551 rcvflg = 0;
552 error = soreceive(so, (struct mbuf **)0,
553 &auio, mp, &control, &rcvflg);
554 if (control)
555 m_freem(control);
556 if (error == EWOULDBLOCK && rep) {
557 if (rep->r_flags & R_SOFTTERM)
558 return (EINTR);
559 }
560 } while (error == EWOULDBLOCK ||
561 (!error && *mp == NULL && control));
562 if ((rcvflg & MSG_EOR) == 0)
563 printf("Egad!!\n");
564 if (!error && *mp == NULL)
565 error = EPIPE;
566 len -= auio.uio_resid;
567 }
568 errout:
569 if (error && error != EINTR && error != ERESTART) {
570 m_freem(*mp);
571 *mp = (struct mbuf *)0;
572 if (error != EPIPE)
573 log(LOG_INFO,
574 "receive error %d from nfs server %s\n",
575 error,
576 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
577 error = nfs_sndlock(&rep->r_nmp->nm_flag, rep);
578 if (!error)
579 error = nfs_reconnect(rep);
580 if (!error)
581 goto tryagain;
582 }
583 } else {
584 if ((so = rep->r_nmp->nm_so) == NULL)
585 return (EACCES);
586 if (so->so_state & SS_ISCONNECTED)
587 getnam = (struct mbuf **)0;
588 else
589 getnam = aname;
590 auio.uio_resid = len = 1000000;
591 auio.uio_procp = p;
592 do {
593 rcvflg = 0;
594 error = soreceive(so, getnam, &auio, mp,
595 (struct mbuf **)0, &rcvflg);
596 if (error == EWOULDBLOCK &&
597 (rep->r_flags & R_SOFTTERM))
598 return (EINTR);
599 } while (error == EWOULDBLOCK);
600 len -= auio.uio_resid;
601 }
602 if (error) {
603 m_freem(*mp);
604 *mp = (struct mbuf *)0;
605 }
606 /*
607 * Search for any mbufs that are not a multiple of 4 bytes long
608 * or with m_data not longword aligned.
609 * These could cause pointer alignment problems, so copy them to
610 * well aligned mbufs.
611 */
612 nfs_realign(*mp, 5 * NFSX_UNSIGNED);
613 return (error);
614 }
615
616 /*
617 * Implement receipt of reply on a socket.
618 * We must search through the list of received datagrams matching them
619 * with outstanding requests using the xid, until ours is found.
620 */
621 /* ARGSUSED */
622 int
623 nfs_reply(myrep)
624 struct nfsreq *myrep;
625 {
626 register struct nfsreq *rep;
627 register struct nfsmount *nmp = myrep->r_nmp;
628 register int32_t t1;
629 struct mbuf *mrep, *nam, *md;
630 u_int32_t rxid, *tl;
631 caddr_t dpos, cp2;
632 int error;
633
634 /*
635 * Loop around until we get our own reply
636 */
637 for (;;) {
638 /*
639 * Lock against other receivers so that I don't get stuck in
640 * sbwait() after someone else has received my reply for me.
641 * Also necessary for connection based protocols to avoid
642 * race conditions during a reconnect.
643 */
644 error = nfs_rcvlock(myrep);
645 if (error)
646 return (error);
647 /* Already received, bye bye */
648 if (myrep->r_mrep != NULL) {
649 nfs_rcvunlock(&nmp->nm_flag);
650 return (0);
651 }
652 /*
653 * Get the next Rpc reply off the socket
654 */
655 error = nfs_receive(myrep, &nam, &mrep);
656 nfs_rcvunlock(&nmp->nm_flag);
657 if (error) {
658
659 /*
660 * Ignore routing errors on connectionless protocols??
661 */
662 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
663 nmp->nm_so->so_error = 0;
664 if (myrep->r_flags & R_GETONEREP)
665 return (0);
666 continue;
667 }
668 return (error);
669 }
670 if (nam)
671 m_freem(nam);
672
673 /*
674 * Get the xid and check that it is an rpc reply
675 */
676 md = mrep;
677 dpos = mtod(md, caddr_t);
678 nfsm_dissect(tl, u_int32_t *, 2*NFSX_UNSIGNED);
679 rxid = *tl++;
680 if (*tl != rpc_reply) {
681 if (nmp->nm_flag & NFSMNT_NQNFS) {
682 if (nqnfs_callback(nmp, mrep, md, dpos))
683 nfsstats.rpcinvalid++;
684 } else {
685 nfsstats.rpcinvalid++;
686 m_freem(mrep);
687 }
688 nfsmout:
689 if (myrep->r_flags & R_GETONEREP)
690 return (0);
691 continue;
692 }
693
694 /*
695 * Loop through the request list to match up the reply
696 * Iff no match, just drop the datagram
697 */
698 for (rep = nfs_reqq.tqh_first; rep != 0;
699 rep = rep->r_chain.tqe_next) {
700 if (rep->r_mrep == NULL && rxid == rep->r_xid) {
701 /* Found it.. */
702 rep->r_mrep = mrep;
703 rep->r_md = md;
704 rep->r_dpos = dpos;
705 if (nfsrtton) {
706 struct rttl *rt;
707
708 rt = &nfsrtt.rttl[nfsrtt.pos];
709 rt->proc = rep->r_procnum;
710 rt->rto = NFS_RTO(nmp, proct[rep->r_procnum]);
711 rt->sent = nmp->nm_sent;
712 rt->cwnd = nmp->nm_cwnd;
713 rt->srtt = nmp->nm_srtt[proct[rep->r_procnum] - 1];
714 rt->sdrtt = nmp->nm_sdrtt[proct[rep->r_procnum] - 1];
715 rt->fsid = nmp->nm_mountp->mnt_stat.f_fsid;
716 rt->tstamp = time;
717 if (rep->r_flags & R_TIMING)
718 rt->rtt = rep->r_rtt;
719 else
720 rt->rtt = 1000000;
721 nfsrtt.pos = (nfsrtt.pos + 1) % NFSRTTLOGSIZ;
722 }
723 /*
724 * Update congestion window.
725 * Do the additive increase of
726 * one rpc/rtt.
727 */
728 if (nmp->nm_cwnd <= nmp->nm_sent) {
729 nmp->nm_cwnd +=
730 (NFS_CWNDSCALE * NFS_CWNDSCALE +
731 (nmp->nm_cwnd >> 1)) / nmp->nm_cwnd;
732 if (nmp->nm_cwnd > NFS_MAXCWND)
733 nmp->nm_cwnd = NFS_MAXCWND;
734 }
735 rep->r_flags &= ~R_SENT;
736 nmp->nm_sent -= NFS_CWNDSCALE;
737 /*
738 * Update rtt using a gain of 0.125 on the mean
739 * and a gain of 0.25 on the deviation.
740 */
741 if (rep->r_flags & R_TIMING) {
742 /*
743 * Since the timer resolution of
744 * NFS_HZ is so course, it can often
745 * result in r_rtt == 0. Since
746 * r_rtt == N means that the actual
747 * rtt is between N+dt and N+2-dt ticks,
748 * add 1.
749 */
750 t1 = rep->r_rtt + 1;
751 t1 -= (NFS_SRTT(rep) >> 3);
752 NFS_SRTT(rep) += t1;
753 if (t1 < 0)
754 t1 = -t1;
755 t1 -= (NFS_SDRTT(rep) >> 2);
756 NFS_SDRTT(rep) += t1;
757 }
758 nmp->nm_timeouts = 0;
759 break;
760 }
761 }
762 /*
763 * If not matched to a request, drop it.
764 * If it's mine, get out.
765 */
766 if (rep == 0) {
767 nfsstats.rpcunexpected++;
768 m_freem(mrep);
769 } else if (rep == myrep) {
770 if (rep->r_mrep == NULL)
771 panic("nfsreply nil");
772 return (0);
773 }
774 if (myrep->r_flags & R_GETONEREP)
775 return (0);
776 }
777 }
778
779 /*
780 * nfs_request - goes something like this
781 * - fill in request struct
782 * - links it into list
783 * - calls nfs_send() for first transmit
784 * - calls nfs_receive() to get reply
785 * - break down rpc header and return with nfs reply pointed to
786 * by mrep or error
787 * nb: always frees up mreq mbuf list
788 */
789 int
790 nfs_request(vp, mrest, procnum, procp, cred, mrp, mdp, dposp)
791 struct vnode *vp;
792 struct mbuf *mrest;
793 int procnum;
794 struct proc *procp;
795 struct ucred *cred;
796 struct mbuf **mrp;
797 struct mbuf **mdp;
798 caddr_t *dposp;
799 {
800 register struct mbuf *m, *mrep;
801 register struct nfsreq *rep;
802 register u_int32_t *tl;
803 register int i;
804 struct nfsmount *nmp;
805 struct mbuf *md, *mheadend;
806 struct nfsnode *np;
807 char nickv[RPCX_NICKVERF];
808 time_t reqtime, waituntil;
809 caddr_t dpos, cp2;
810 int t1, nqlflag, cachable, s, error = 0, mrest_len, auth_len, auth_type;
811 int trylater_delay = NQ_TRYLATERDEL, trylater_cnt = 0, failed_auth = 0;
812 int verf_len, verf_type;
813 u_int32_t xid;
814 u_quad_t frev;
815 char *auth_str, *verf_str;
816 NFSKERBKEY_T key; /* save session key */
817
818 nmp = VFSTONFS(vp->v_mount);
819 MALLOC(rep, struct nfsreq *, sizeof(struct nfsreq), M_NFSREQ, M_WAITOK);
820 rep->r_nmp = nmp;
821 rep->r_vp = vp;
822 rep->r_procp = procp;
823 rep->r_procnum = procnum;
824 i = 0;
825 m = mrest;
826 while (m) {
827 i += m->m_len;
828 m = m->m_next;
829 }
830 mrest_len = i;
831
832 /*
833 * Get the RPC header with authorization.
834 */
835 kerbauth:
836 verf_str = auth_str = (char *)0;
837 if (nmp->nm_flag & NFSMNT_KERB) {
838 verf_str = nickv;
839 verf_len = sizeof (nickv);
840 auth_type = RPCAUTH_KERB4;
841 bzero((caddr_t)key, sizeof (key));
842 if (failed_auth || nfs_getnickauth(nmp, cred, &auth_str,
843 &auth_len, verf_str, verf_len)) {
844 error = nfs_getauth(nmp, rep, cred, &auth_str,
845 &auth_len, verf_str, &verf_len, key);
846 if (error) {
847 free((caddr_t)rep, M_NFSREQ);
848 m_freem(mrest);
849 return (error);
850 }
851 }
852 } else {
853 auth_type = RPCAUTH_UNIX;
854 auth_len = (((cred->cr_ngroups > nmp->nm_numgrps) ?
855 nmp->nm_numgrps : cred->cr_ngroups) << 2) +
856 5 * NFSX_UNSIGNED;
857 }
858 m = nfsm_rpchead(cred, nmp->nm_flag, procnum, auth_type, auth_len,
859 auth_str, verf_len, verf_str, mrest, mrest_len, &mheadend, &xid);
860 if (auth_str)
861 free(auth_str, M_TEMP);
862
863 /*
864 * For stream protocols, insert a Sun RPC Record Mark.
865 */
866 if (nmp->nm_sotype == SOCK_STREAM) {
867 M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
868 *mtod(m, u_int32_t *) = htonl(0x80000000 |
869 (m->m_pkthdr.len - NFSX_UNSIGNED));
870 }
871 rep->r_mreq = m;
872 rep->r_xid = xid;
873 tryagain:
874 if (nmp->nm_flag & NFSMNT_SOFT)
875 rep->r_retry = nmp->nm_retry;
876 else
877 rep->r_retry = NFS_MAXREXMIT + 1; /* past clip limit */
878 rep->r_rtt = rep->r_rexmit = 0;
879 if (proct[procnum] > 0)
880 rep->r_flags = R_TIMING;
881 else
882 rep->r_flags = 0;
883 rep->r_mrep = NULL;
884
885 /*
886 * Do the client side RPC.
887 */
888 nfsstats.rpcrequests++;
889 /*
890 * Chain request into list of outstanding requests. Be sure
891 * to put it LAST so timer finds oldest requests first.
892 */
893 s = splsoftclock();
894 TAILQ_INSERT_TAIL(&nfs_reqq, rep, r_chain);
895
896 /* Get send time for nqnfs */
897 reqtime = time.tv_sec;
898
899 /*
900 * If backing off another request or avoiding congestion, don't
901 * send this one now but let timer do it. If not timing a request,
902 * do it now.
903 */
904 if (nmp->nm_so && (nmp->nm_sotype != SOCK_DGRAM ||
905 (nmp->nm_flag & NFSMNT_DUMBTIMR) ||
906 nmp->nm_sent < nmp->nm_cwnd)) {
907 splx(s);
908 if (nmp->nm_soflags & PR_CONNREQUIRED)
909 error = nfs_sndlock(&nmp->nm_flag, rep);
910 if (!error) {
911 m = m_copym(m, 0, M_COPYALL, M_WAIT);
912 error = nfs_send(nmp->nm_so, nmp->nm_nam, m, rep);
913 if (nmp->nm_soflags & PR_CONNREQUIRED)
914 nfs_sndunlock(&nmp->nm_flag);
915 }
916 if (!error && (rep->r_flags & R_MUSTRESEND) == 0) {
917 nmp->nm_sent += NFS_CWNDSCALE;
918 rep->r_flags |= R_SENT;
919 }
920 } else {
921 splx(s);
922 rep->r_rtt = -1;
923 }
924
925 /*
926 * Wait for the reply from our send or the timer's.
927 */
928 if (!error || error == EPIPE)
929 error = nfs_reply(rep);
930
931 /*
932 * RPC done, unlink the request.
933 */
934 s = splsoftclock();
935 TAILQ_REMOVE(&nfs_reqq, rep, r_chain);
936 splx(s);
937
938 /*
939 * Decrement the outstanding request count.
940 */
941 if (rep->r_flags & R_SENT) {
942 rep->r_flags &= ~R_SENT; /* paranoia */
943 nmp->nm_sent -= NFS_CWNDSCALE;
944 }
945
946 /*
947 * If there was a successful reply and a tprintf msg.
948 * tprintf a response.
949 */
950 if (!error && (rep->r_flags & R_TPRINTFMSG))
951 nfs_msg(rep->r_procp, nmp->nm_mountp->mnt_stat.f_mntfromname,
952 "is alive again");
953 mrep = rep->r_mrep;
954 md = rep->r_md;
955 dpos = rep->r_dpos;
956 if (error) {
957 m_freem(rep->r_mreq);
958 free((caddr_t)rep, M_NFSREQ);
959 return (error);
960 }
961
962 /*
963 * break down the rpc header and check if ok
964 */
965 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
966 if (*tl++ == rpc_msgdenied) {
967 if (*tl == rpc_mismatch)
968 error = EOPNOTSUPP;
969 else if ((nmp->nm_flag & NFSMNT_KERB) && *tl++ == rpc_autherr) {
970 if (!failed_auth) {
971 failed_auth++;
972 mheadend->m_next = (struct mbuf *)0;
973 m_freem(mrep);
974 m_freem(rep->r_mreq);
975 goto kerbauth;
976 } else
977 error = EAUTH;
978 } else
979 error = EACCES;
980 m_freem(mrep);
981 m_freem(rep->r_mreq);
982 free((caddr_t)rep, M_NFSREQ);
983 return (error);
984 }
985
986 /*
987 * Grab any Kerberos verifier, otherwise just throw it away.
988 */
989 verf_type = fxdr_unsigned(int, *tl++);
990 i = fxdr_unsigned(int32_t, *tl);
991 if ((nmp->nm_flag & NFSMNT_KERB) && verf_type == RPCAUTH_KERB4) {
992 error = nfs_savenickauth(nmp, cred, i, key, &md, &dpos, mrep);
993 if (error)
994 goto nfsmout;
995 } else if (i > 0)
996 nfsm_adv(nfsm_rndup(i));
997 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
998 /* 0 == ok */
999 if (*tl == 0) {
1000 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1001 if (*tl != 0) {
1002 error = fxdr_unsigned(int, *tl);
1003 if ((nmp->nm_flag & NFSMNT_NFSV3) &&
1004 error == NFSERR_TRYLATER) {
1005 m_freem(mrep);
1006 error = 0;
1007 waituntil = time.tv_sec + trylater_delay;
1008 while (time.tv_sec < waituntil)
1009 (void) tsleep((caddr_t)&lbolt,
1010 PSOCK, "nqnfstry", 0);
1011 trylater_delay *= nfs_backoff[trylater_cnt];
1012 if (trylater_cnt < 7)
1013 trylater_cnt++;
1014 goto tryagain;
1015 }
1016
1017 /*
1018 * If the File Handle was stale, invalidate the
1019 * lookup cache, just in case.
1020 */
1021 if (error == ESTALE)
1022 cache_purge(vp);
1023 if (nmp->nm_flag & NFSMNT_NFSV3) {
1024 *mrp = mrep;
1025 *mdp = md;
1026 *dposp = dpos;
1027 error |= NFSERR_RETERR;
1028 } else
1029 m_freem(mrep);
1030 m_freem(rep->r_mreq);
1031 free((caddr_t)rep, M_NFSREQ);
1032 return (error);
1033 }
1034
1035 /*
1036 * For nqnfs, get any lease in reply
1037 */
1038 if (nmp->nm_flag & NFSMNT_NQNFS) {
1039 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1040 if (*tl) {
1041 np = VTONFS(vp);
1042 nqlflag = fxdr_unsigned(int, *tl);
1043 nfsm_dissect(tl, u_int32_t *, 4*NFSX_UNSIGNED);
1044 cachable = fxdr_unsigned(int, *tl++);
1045 reqtime += fxdr_unsigned(int, *tl++);
1046 if (reqtime > time.tv_sec) {
1047 fxdr_hyper(tl, &frev);
1048 nqnfs_clientlease(nmp, np, nqlflag,
1049 cachable, reqtime, frev);
1050 }
1051 }
1052 }
1053 *mrp = mrep;
1054 *mdp = md;
1055 *dposp = dpos;
1056 m_freem(rep->r_mreq);
1057 FREE((caddr_t)rep, M_NFSREQ);
1058 return (0);
1059 }
1060 m_freem(mrep);
1061 error = EPROTONOSUPPORT;
1062 nfsmout:
1063 m_freem(rep->r_mreq);
1064 free((caddr_t)rep, M_NFSREQ);
1065 return (error);
1066 }
1067 #endif /* NFSCLIENT */
1068
1069 /*
1070 * Generate the rpc reply header
1071 * siz arg. is used to decide if adding a cluster is worthwhile
1072 */
1073 int
1074 nfs_rephead(siz, nd, slp, err, cache, frev, mrq, mbp, bposp)
1075 int siz;
1076 struct nfsrv_descript *nd;
1077 struct nfssvc_sock *slp;
1078 int err;
1079 int cache;
1080 u_quad_t *frev;
1081 struct mbuf **mrq;
1082 struct mbuf **mbp;
1083 caddr_t *bposp;
1084 {
1085 register u_int32_t *tl;
1086 register struct mbuf *mreq;
1087 caddr_t bpos;
1088 struct mbuf *mb, *mb2;
1089
1090 MGETHDR(mreq, M_WAIT, MT_DATA);
1091 mb = mreq;
1092 /*
1093 * If this is a big reply, use a cluster else
1094 * try and leave leading space for the lower level headers.
1095 */
1096 siz += RPC_REPLYSIZ;
1097 if (siz >= MINCLSIZE) {
1098 MCLGET(mreq, M_WAIT);
1099 } else
1100 mreq->m_data += max_hdr;
1101 tl = mtod(mreq, u_int32_t *);
1102 mreq->m_len = 6 * NFSX_UNSIGNED;
1103 bpos = ((caddr_t)tl) + mreq->m_len;
1104 *tl++ = txdr_unsigned(nd->nd_retxid);
1105 *tl++ = rpc_reply;
1106 if (err == ERPCMISMATCH || (err & NFSERR_AUTHERR)) {
1107 *tl++ = rpc_msgdenied;
1108 if (err & NFSERR_AUTHERR) {
1109 *tl++ = rpc_autherr;
1110 *tl = txdr_unsigned(err & ~NFSERR_AUTHERR);
1111 mreq->m_len -= NFSX_UNSIGNED;
1112 bpos -= NFSX_UNSIGNED;
1113 } else {
1114 *tl++ = rpc_mismatch;
1115 *tl++ = txdr_unsigned(RPC_VER2);
1116 *tl = txdr_unsigned(RPC_VER2);
1117 }
1118 } else {
1119 *tl++ = rpc_msgaccepted;
1120
1121 /*
1122 * For Kerberos authentication, we must send the nickname
1123 * verifier back, otherwise just RPCAUTH_NULL.
1124 */
1125 if (nd->nd_flag & ND_KERBFULL) {
1126 register struct nfsuid *nuidp;
1127 struct timeval ktvin, ktvout;
1128
1129 for (nuidp = NUIDHASH(slp, nd->nd_cr.cr_uid)->lh_first;
1130 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
1131 if (nuidp->nu_cr.cr_uid == nd->nd_cr.cr_uid &&
1132 (!nd->nd_nam2 || netaddr_match(NU_NETFAM(nuidp),
1133 &nuidp->nu_haddr, nd->nd_nam2)))
1134 break;
1135 }
1136 if (nuidp) {
1137 ktvin.tv_sec =
1138 txdr_unsigned(nuidp->nu_timestamp.tv_sec - 1);
1139 ktvin.tv_usec =
1140 txdr_unsigned(nuidp->nu_timestamp.tv_usec);
1141
1142 /*
1143 * Encrypt the timestamp in ecb mode using the
1144 * session key.
1145 */
1146 #ifdef NFSKERB
1147 XXX
1148 #endif
1149
1150 *tl++ = rpc_auth_kerb;
1151 *tl++ = txdr_unsigned(3 * NFSX_UNSIGNED);
1152 *tl = ktvout.tv_sec;
1153 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1154 *tl++ = ktvout.tv_usec;
1155 *tl++ = txdr_unsigned(nuidp->nu_cr.cr_uid);
1156 } else {
1157 *tl++ = 0;
1158 *tl++ = 0;
1159 }
1160 } else {
1161 *tl++ = 0;
1162 *tl++ = 0;
1163 }
1164 switch (err) {
1165 case EPROGUNAVAIL:
1166 *tl = txdr_unsigned(RPC_PROGUNAVAIL);
1167 break;
1168 case EPROGMISMATCH:
1169 *tl = txdr_unsigned(RPC_PROGMISMATCH);
1170 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1171 if (nd->nd_flag & ND_NQNFS) {
1172 *tl++ = txdr_unsigned(3);
1173 *tl = txdr_unsigned(3);
1174 } else {
1175 *tl++ = txdr_unsigned(2);
1176 *tl = txdr_unsigned(3);
1177 }
1178 break;
1179 case EPROCUNAVAIL:
1180 *tl = txdr_unsigned(RPC_PROCUNAVAIL);
1181 break;
1182 case EBADRPC:
1183 *tl = txdr_unsigned(RPC_GARBAGE);
1184 break;
1185 default:
1186 *tl = 0;
1187 if (err != NFSERR_RETVOID) {
1188 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1189 if (err)
1190 *tl = txdr_unsigned(nfsrv_errmap(nd, err));
1191 else
1192 *tl = 0;
1193 }
1194 break;
1195 };
1196 }
1197
1198 /*
1199 * For nqnfs, piggyback lease as requested.
1200 */
1201 if ((nd->nd_flag & ND_NQNFS) && err == 0) {
1202 if (nd->nd_flag & ND_LEASE) {
1203 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1204 *tl++ = txdr_unsigned(nd->nd_flag & ND_LEASE);
1205 *tl++ = txdr_unsigned(cache);
1206 *tl++ = txdr_unsigned(nd->nd_duration);
1207 txdr_hyper(frev, tl);
1208 } else {
1209 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1210 *tl = 0;
1211 }
1212 }
1213 *mrq = mreq;
1214 *mbp = mb;
1215 *bposp = bpos;
1216 if (err != 0 && err != NFSERR_RETVOID)
1217 nfsstats.srvrpc_errs++;
1218 return (0);
1219 }
1220
1221 /*
1222 * Nfs timer routine
1223 * Scan the nfsreq list and retranmit any requests that have timed out
1224 * To avoid retransmission attempts on STREAM sockets (in the future) make
1225 * sure to set the r_retry field to 0 (implies nm_retry == 0).
1226 */
1227 void
1228 nfs_timer(arg)
1229 void *arg; /* never used */
1230 {
1231 register struct nfsreq *rep;
1232 register struct mbuf *m;
1233 register struct socket *so;
1234 register struct nfsmount *nmp;
1235 register int timeo;
1236 register struct nfssvc_sock *slp;
1237 #ifdef NFSSERVER
1238 static long lasttime = 0;
1239 #endif
1240 int s, error;
1241 u_quad_t cur_usec;
1242
1243 s = splsoftnet();
1244 for (rep = nfs_reqq.tqh_first; rep != 0; rep = rep->r_chain.tqe_next) {
1245 nmp = rep->r_nmp;
1246 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM))
1247 continue;
1248 if (nfs_sigintr(nmp, rep, rep->r_procp)) {
1249 rep->r_flags |= R_SOFTTERM;
1250 continue;
1251 }
1252 if (rep->r_rtt >= 0) {
1253 rep->r_rtt++;
1254 if (nmp->nm_flag & NFSMNT_DUMBTIMR)
1255 timeo = nmp->nm_timeo;
1256 else
1257 timeo = NFS_RTO(nmp, proct[rep->r_procnum]);
1258 if (nmp->nm_timeouts > 0)
1259 timeo *= nfs_backoff[nmp->nm_timeouts - 1];
1260 if (rep->r_rtt <= timeo)
1261 continue;
1262 if (nmp->nm_timeouts < 8)
1263 nmp->nm_timeouts++;
1264 }
1265 /*
1266 * Check for server not responding
1267 */
1268 if ((rep->r_flags & R_TPRINTFMSG) == 0 &&
1269 rep->r_rexmit > nmp->nm_deadthresh) {
1270 nfs_msg(rep->r_procp,
1271 nmp->nm_mountp->mnt_stat.f_mntfromname,
1272 "not responding");
1273 rep->r_flags |= R_TPRINTFMSG;
1274 }
1275 if (rep->r_rexmit >= rep->r_retry) { /* too many */
1276 nfsstats.rpctimeouts++;
1277 rep->r_flags |= R_SOFTTERM;
1278 continue;
1279 }
1280 if (nmp->nm_sotype != SOCK_DGRAM) {
1281 if (++rep->r_rexmit > NFS_MAXREXMIT)
1282 rep->r_rexmit = NFS_MAXREXMIT;
1283 continue;
1284 }
1285 if ((so = nmp->nm_so) == NULL)
1286 continue;
1287
1288 /*
1289 * If there is enough space and the window allows..
1290 * Resend it
1291 * Set r_rtt to -1 in case we fail to send it now.
1292 */
1293 rep->r_rtt = -1;
1294 if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len &&
1295 ((nmp->nm_flag & NFSMNT_DUMBTIMR) ||
1296 (rep->r_flags & R_SENT) ||
1297 nmp->nm_sent < nmp->nm_cwnd) &&
1298 (m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))){
1299 if ((nmp->nm_flag & NFSMNT_NOCONN) == 0)
1300 error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m,
1301 (struct mbuf *)0, (struct mbuf *)0);
1302 else
1303 error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m,
1304 nmp->nm_nam, (struct mbuf *)0);
1305 if (error) {
1306 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error))
1307 so->so_error = 0;
1308 } else {
1309 /*
1310 * Iff first send, start timing
1311 * else turn timing off, backoff timer
1312 * and divide congestion window by 2.
1313 */
1314 if (rep->r_flags & R_SENT) {
1315 rep->r_flags &= ~R_TIMING;
1316 if (++rep->r_rexmit > NFS_MAXREXMIT)
1317 rep->r_rexmit = NFS_MAXREXMIT;
1318 nmp->nm_cwnd >>= 1;
1319 if (nmp->nm_cwnd < NFS_CWNDSCALE)
1320 nmp->nm_cwnd = NFS_CWNDSCALE;
1321 nfsstats.rpcretries++;
1322 } else {
1323 rep->r_flags |= R_SENT;
1324 nmp->nm_sent += NFS_CWNDSCALE;
1325 }
1326 rep->r_rtt = 0;
1327 }
1328 }
1329 }
1330
1331 #ifdef NFSSERVER
1332 /*
1333 * Call the nqnfs server timer once a second to handle leases.
1334 */
1335 if (lasttime != time.tv_sec) {
1336 lasttime = time.tv_sec;
1337 nqnfs_serverd();
1338 }
1339
1340 /*
1341 * Scan the write gathering queues for writes that need to be
1342 * completed now.
1343 */
1344 cur_usec = (u_quad_t)time.tv_sec * 1000000 + (u_quad_t)time.tv_usec;
1345 for (slp = nfssvc_sockhead.tqh_first; slp != 0;
1346 slp = slp->ns_chain.tqe_next) {
1347 if (slp->ns_tq.lh_first && slp->ns_tq.lh_first->nd_time<=cur_usec)
1348 nfsrv_wakenfsd(slp);
1349 }
1350 #endif /* NFSSERVER */
1351 splx(s);
1352 timeout(nfs_timer, (void *)0, nfs_ticks);
1353 }
1354
1355 /*
1356 * Test for a termination condition pending on the process.
1357 * This is used for NFSMNT_INT mounts.
1358 */
1359 int
1360 nfs_sigintr(nmp, rep, p)
1361 struct nfsmount *nmp;
1362 struct nfsreq *rep;
1363 register struct proc *p;
1364 {
1365
1366 if (rep && (rep->r_flags & R_SOFTTERM))
1367 return (EINTR);
1368 if (!(nmp->nm_flag & NFSMNT_INT))
1369 return (0);
1370 if (p && p->p_siglist &&
1371 (((p->p_siglist & ~p->p_sigmask) & ~p->p_sigignore) &
1372 NFSINT_SIGMASK))
1373 return (EINTR);
1374 return (0);
1375 }
1376
1377 /*
1378 * Lock a socket against others.
1379 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
1380 * and also to avoid race conditions between the processes with nfs requests
1381 * in progress when a reconnect is necessary.
1382 */
1383 int
1384 nfs_sndlock(flagp, rep)
1385 register int *flagp;
1386 struct nfsreq *rep;
1387 {
1388 struct proc *p;
1389 int slpflag = 0, slptimeo = 0;
1390
1391 if (rep) {
1392 p = rep->r_procp;
1393 if (rep->r_nmp->nm_flag & NFSMNT_INT)
1394 slpflag = PCATCH;
1395 } else
1396 p = (struct proc *)0;
1397 while (*flagp & NFSMNT_SNDLOCK) {
1398 if (nfs_sigintr(rep->r_nmp, rep, p))
1399 return (EINTR);
1400 *flagp |= NFSMNT_WANTSND;
1401 (void) tsleep((caddr_t)flagp, slpflag | (PZERO - 1), "nfsndlck",
1402 slptimeo);
1403 if (slpflag == PCATCH) {
1404 slpflag = 0;
1405 slptimeo = 2 * hz;
1406 }
1407 }
1408 *flagp |= NFSMNT_SNDLOCK;
1409 return (0);
1410 }
1411
1412 /*
1413 * Unlock the stream socket for others.
1414 */
1415 void
1416 nfs_sndunlock(flagp)
1417 register int *flagp;
1418 {
1419
1420 if ((*flagp & NFSMNT_SNDLOCK) == 0)
1421 panic("nfs sndunlock");
1422 *flagp &= ~NFSMNT_SNDLOCK;
1423 if (*flagp & NFSMNT_WANTSND) {
1424 *flagp &= ~NFSMNT_WANTSND;
1425 wakeup((caddr_t)flagp);
1426 }
1427 }
1428
1429 int
1430 nfs_rcvlock(rep)
1431 register struct nfsreq *rep;
1432 {
1433 register int *flagp = &rep->r_nmp->nm_flag;
1434 int slpflag, slptimeo = 0;
1435
1436 if (*flagp & NFSMNT_INT)
1437 slpflag = PCATCH;
1438 else
1439 slpflag = 0;
1440 while (*flagp & NFSMNT_RCVLOCK) {
1441 if (nfs_sigintr(rep->r_nmp, rep, rep->r_procp))
1442 return (EINTR);
1443 *flagp |= NFSMNT_WANTRCV;
1444 (void) tsleep((caddr_t)flagp, slpflag | (PZERO - 1), "nfsrcvlk",
1445 slptimeo);
1446 if (slpflag == PCATCH) {
1447 slpflag = 0;
1448 slptimeo = 2 * hz;
1449 }
1450 }
1451 *flagp |= NFSMNT_RCVLOCK;
1452 return (0);
1453 }
1454
1455 /*
1456 * Unlock the stream socket for others.
1457 */
1458 void
1459 nfs_rcvunlock(flagp)
1460 register int *flagp;
1461 {
1462
1463 if ((*flagp & NFSMNT_RCVLOCK) == 0)
1464 panic("nfs rcvunlock");
1465 *flagp &= ~NFSMNT_RCVLOCK;
1466 if (*flagp & NFSMNT_WANTRCV) {
1467 *flagp &= ~NFSMNT_WANTRCV;
1468 wakeup((caddr_t)flagp);
1469 }
1470 }
1471
1472 /*
1473 * Check for badly aligned mbuf data areas and
1474 * realign data in an mbuf list by copying the data areas up, as required.
1475 */
1476 void
1477 nfs_realign(m, hsiz)
1478 register struct mbuf *m;
1479 int hsiz;
1480 {
1481 register struct mbuf *m2;
1482 register int siz, mlen, olen;
1483 register caddr_t tcp, fcp;
1484 struct mbuf *mnew;
1485
1486 while (m) {
1487 /*
1488 * This never happens for UDP, rarely happens for TCP
1489 * but frequently happens for iso transport.
1490 */
1491 if ((m->m_len & 0x3) || (mtod(m, long) & 0x3)) {
1492 olen = m->m_len;
1493 fcp = mtod(m, caddr_t);
1494 if ((long)fcp & 0x3) {
1495 m->m_flags &= ~M_PKTHDR;
1496 if (m->m_flags & M_EXT)
1497 m->m_data = m->m_ext.ext_buf +
1498 ((m->m_ext.ext_size - olen) & ~0x3);
1499 else
1500 m->m_data = m->m_dat;
1501 }
1502 m->m_len = 0;
1503 tcp = mtod(m, caddr_t);
1504 mnew = m;
1505 m2 = m->m_next;
1506
1507 /*
1508 * If possible, only put the first invariant part
1509 * of the RPC header in the first mbuf.
1510 */
1511 mlen = M_TRAILINGSPACE(m);
1512 if (olen <= hsiz && mlen > hsiz)
1513 mlen = hsiz;
1514
1515 /*
1516 * Loop through the mbuf list consolidating data.
1517 */
1518 while (m) {
1519 while (olen > 0) {
1520 if (mlen == 0) {
1521 m2->m_flags &= ~M_PKTHDR;
1522 if (m2->m_flags & M_EXT)
1523 m2->m_data = m2->m_ext.ext_buf;
1524 else
1525 m2->m_data = m2->m_dat;
1526 m2->m_len = 0;
1527 mlen = M_TRAILINGSPACE(m2);
1528 tcp = mtod(m2, caddr_t);
1529 mnew = m2;
1530 m2 = m2->m_next;
1531 }
1532 siz = min(mlen, olen);
1533 if (tcp != fcp)
1534 bcopy(fcp, tcp, siz);
1535 mnew->m_len += siz;
1536 mlen -= siz;
1537 olen -= siz;
1538 tcp += siz;
1539 fcp += siz;
1540 }
1541 m = m->m_next;
1542 if (m) {
1543 olen = m->m_len;
1544 fcp = mtod(m, caddr_t);
1545 }
1546 }
1547
1548 /*
1549 * Finally, set m_len == 0 for any trailing mbufs that have
1550 * been copied out of.
1551 */
1552 while (m2) {
1553 m2->m_len = 0;
1554 m2 = m2->m_next;
1555 }
1556 return;
1557 }
1558 m = m->m_next;
1559 }
1560 }
1561
1562 /*
1563 * Parse an RPC request
1564 * - verify it
1565 * - fill in the cred struct.
1566 */
1567 int
1568 nfs_getreq(nd, nfsd, has_header)
1569 register struct nfsrv_descript *nd;
1570 struct nfsd *nfsd;
1571 int has_header;
1572 {
1573 register int len, i;
1574 register u_int32_t *tl;
1575 register int32_t t1;
1576 struct uio uio;
1577 struct iovec iov;
1578 caddr_t dpos, cp2, cp;
1579 u_int32_t nfsvers, auth_type;
1580 uid_t nickuid;
1581 int error = 0, nqnfs = 0, ticklen;
1582 struct mbuf *mrep, *md;
1583 register struct nfsuid *nuidp;
1584 struct timeval tvin, tvout;
1585
1586 mrep = nd->nd_mrep;
1587 md = nd->nd_md;
1588 dpos = nd->nd_dpos;
1589 if (has_header) {
1590 nfsm_dissect(tl, u_int32_t *, 10 * NFSX_UNSIGNED);
1591 nd->nd_retxid = fxdr_unsigned(u_int32_t, *tl++);
1592 if (*tl++ != rpc_call) {
1593 m_freem(mrep);
1594 return (EBADRPC);
1595 }
1596 } else
1597 nfsm_dissect(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
1598 nd->nd_repstat = 0;
1599 nd->nd_flag = 0;
1600 if (*tl++ != rpc_vers) {
1601 nd->nd_repstat = ERPCMISMATCH;
1602 nd->nd_procnum = NFSPROC_NOOP;
1603 return (0);
1604 }
1605 if (*tl != nfs_prog) {
1606 if (*tl == nqnfs_prog)
1607 nqnfs++;
1608 else {
1609 nd->nd_repstat = EPROGUNAVAIL;
1610 nd->nd_procnum = NFSPROC_NOOP;
1611 return (0);
1612 }
1613 }
1614 tl++;
1615 nfsvers = fxdr_unsigned(u_int32_t, *tl++);
1616 if (((nfsvers < NFS_VER2 || nfsvers > NFS_VER3) && !nqnfs) ||
1617 (nfsvers != NQNFS_VER3 && nqnfs)) {
1618 nd->nd_repstat = EPROGMISMATCH;
1619 nd->nd_procnum = NFSPROC_NOOP;
1620 return (0);
1621 }
1622 if (nqnfs)
1623 nd->nd_flag = (ND_NFSV3 | ND_NQNFS);
1624 else if (nfsvers == NFS_VER3)
1625 nd->nd_flag = ND_NFSV3;
1626 nd->nd_procnum = fxdr_unsigned(u_int32_t, *tl++);
1627 if (nd->nd_procnum == NFSPROC_NULL)
1628 return (0);
1629 if (nd->nd_procnum >= NFS_NPROCS ||
1630 (!nqnfs && nd->nd_procnum >= NQNFSPROC_GETLEASE) ||
1631 (!nd->nd_flag && nd->nd_procnum > NFSV2PROC_STATFS)) {
1632 nd->nd_repstat = EPROCUNAVAIL;
1633 nd->nd_procnum = NFSPROC_NOOP;
1634 return (0);
1635 }
1636 if ((nd->nd_flag & ND_NFSV3) == 0)
1637 nd->nd_procnum = nfsv3_procid[nd->nd_procnum];
1638 auth_type = *tl++;
1639 len = fxdr_unsigned(int, *tl++);
1640 if (len < 0 || len > RPCAUTH_MAXSIZ) {
1641 m_freem(mrep);
1642 return (EBADRPC);
1643 }
1644
1645 nd->nd_flag &= ~ND_KERBAUTH;
1646 /*
1647 * Handle auth_unix or auth_kerb.
1648 */
1649 if (auth_type == rpc_auth_unix) {
1650 len = fxdr_unsigned(int, *++tl);
1651 if (len < 0 || len > NFS_MAXNAMLEN) {
1652 m_freem(mrep);
1653 return (EBADRPC);
1654 }
1655 nfsm_adv(nfsm_rndup(len));
1656 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1657 bzero((caddr_t)&nd->nd_cr, sizeof (struct ucred));
1658 nd->nd_cr.cr_ref = 1;
1659 nd->nd_cr.cr_uid = fxdr_unsigned(uid_t, *tl++);
1660 nd->nd_cr.cr_gid = fxdr_unsigned(gid_t, *tl++);
1661 len = fxdr_unsigned(int, *tl);
1662 if (len < 0 || len > RPCAUTH_UNIXGIDS) {
1663 m_freem(mrep);
1664 return (EBADRPC);
1665 }
1666 nfsm_dissect(tl, u_int32_t *, (len + 2) * NFSX_UNSIGNED);
1667 for (i = 0; i < len; i++)
1668 if (i < NGROUPS)
1669 nd->nd_cr.cr_groups[i] = fxdr_unsigned(gid_t, *tl++);
1670 else
1671 tl++;
1672 nd->nd_cr.cr_ngroups = (len > NGROUPS) ? NGROUPS : len;
1673 if (nd->nd_cr.cr_ngroups > 1)
1674 nfsrvw_sort(nd->nd_cr.cr_groups, nd->nd_cr.cr_ngroups);
1675 len = fxdr_unsigned(int, *++tl);
1676 if (len < 0 || len > RPCAUTH_MAXSIZ) {
1677 m_freem(mrep);
1678 return (EBADRPC);
1679 }
1680 if (len > 0)
1681 nfsm_adv(nfsm_rndup(len));
1682 } else if (auth_type == rpc_auth_kerb) {
1683 switch (fxdr_unsigned(int, *tl++)) {
1684 case RPCAKN_FULLNAME:
1685 ticklen = fxdr_unsigned(int, *tl);
1686 *((u_int32_t *)nfsd->nfsd_authstr) = *tl;
1687 uio.uio_resid = nfsm_rndup(ticklen) + NFSX_UNSIGNED;
1688 nfsd->nfsd_authlen = uio.uio_resid + NFSX_UNSIGNED;
1689 if (uio.uio_resid > (len - 2 * NFSX_UNSIGNED)) {
1690 m_freem(mrep);
1691 return (EBADRPC);
1692 }
1693 uio.uio_offset = 0;
1694 uio.uio_iov = &iov;
1695 uio.uio_iovcnt = 1;
1696 uio.uio_segflg = UIO_SYSSPACE;
1697 iov.iov_base = (caddr_t)&nfsd->nfsd_authstr[4];
1698 iov.iov_len = RPCAUTH_MAXSIZ - 4;
1699 nfsm_mtouio(&uio, uio.uio_resid);
1700 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1701 if (*tl++ != rpc_auth_kerb ||
1702 fxdr_unsigned(int, *tl) != 4 * NFSX_UNSIGNED) {
1703 printf("Bad kerb verifier\n");
1704 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
1705 nd->nd_procnum = NFSPROC_NOOP;
1706 return (0);
1707 }
1708 nfsm_dissect(cp, caddr_t, 4 * NFSX_UNSIGNED);
1709 tl = (u_int32_t *)cp;
1710 if (fxdr_unsigned(int, *tl) != RPCAKN_FULLNAME) {
1711 printf("Not fullname kerb verifier\n");
1712 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
1713 nd->nd_procnum = NFSPROC_NOOP;
1714 return (0);
1715 }
1716 cp += NFSX_UNSIGNED;
1717 bcopy(cp, nfsd->nfsd_verfstr, 3 * NFSX_UNSIGNED);
1718 nfsd->nfsd_verflen = 3 * NFSX_UNSIGNED;
1719 nd->nd_flag |= ND_KERBFULL;
1720 nfsd->nfsd_flag |= NFSD_NEEDAUTH;
1721 break;
1722 case RPCAKN_NICKNAME:
1723 if (len != 2 * NFSX_UNSIGNED) {
1724 printf("Kerb nickname short\n");
1725 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADCRED);
1726 nd->nd_procnum = NFSPROC_NOOP;
1727 return (0);
1728 }
1729 nickuid = fxdr_unsigned(uid_t, *tl);
1730 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1731 if (*tl++ != rpc_auth_kerb ||
1732 fxdr_unsigned(int, *tl) != 3 * NFSX_UNSIGNED) {
1733 printf("Kerb nick verifier bad\n");
1734 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
1735 nd->nd_procnum = NFSPROC_NOOP;
1736 return (0);
1737 }
1738 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1739 tvin.tv_sec = *tl++;
1740 tvin.tv_usec = *tl;
1741
1742 for (nuidp = NUIDHASH(nfsd->nfsd_slp,nickuid)->lh_first;
1743 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
1744 if (nuidp->nu_cr.cr_uid == nickuid &&
1745 (!nd->nd_nam2 ||
1746 netaddr_match(NU_NETFAM(nuidp),
1747 &nuidp->nu_haddr, nd->nd_nam2)))
1748 break;
1749 }
1750 if (!nuidp) {
1751 nd->nd_repstat =
1752 (NFSERR_AUTHERR|AUTH_REJECTCRED);
1753 nd->nd_procnum = NFSPROC_NOOP;
1754 return (0);
1755 }
1756
1757 /*
1758 * Now, decrypt the timestamp using the session key
1759 * and validate it.
1760 */
1761 #ifdef NFSKERB
1762 XXX
1763 #endif
1764
1765 tvout.tv_sec = fxdr_unsigned(long, tvout.tv_sec);
1766 tvout.tv_usec = fxdr_unsigned(long, tvout.tv_usec);
1767 if (nuidp->nu_expire < time.tv_sec ||
1768 nuidp->nu_timestamp.tv_sec > tvout.tv_sec ||
1769 (nuidp->nu_timestamp.tv_sec == tvout.tv_sec &&
1770 nuidp->nu_timestamp.tv_usec > tvout.tv_usec)) {
1771 nuidp->nu_expire = 0;
1772 nd->nd_repstat =
1773 (NFSERR_AUTHERR|AUTH_REJECTVERF);
1774 nd->nd_procnum = NFSPROC_NOOP;
1775 return (0);
1776 }
1777 nfsrv_setcred(&nuidp->nu_cr, &nd->nd_cr);
1778 nd->nd_flag |= ND_KERBNICK;
1779 };
1780 } else {
1781 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_REJECTCRED);
1782 nd->nd_procnum = NFSPROC_NOOP;
1783 return (0);
1784 }
1785
1786 /*
1787 * For nqnfs, get piggybacked lease request.
1788 */
1789 if (nqnfs && nd->nd_procnum != NQNFSPROC_EVICTED) {
1790 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1791 nd->nd_flag |= fxdr_unsigned(int, *tl);
1792 if (nd->nd_flag & ND_LEASE) {
1793 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1794 nd->nd_duration = fxdr_unsigned(u_int32_t, *tl);
1795 } else
1796 nd->nd_duration = NQ_MINLEASE;
1797 } else
1798 nd->nd_duration = NQ_MINLEASE;
1799 nd->nd_md = md;
1800 nd->nd_dpos = dpos;
1801 return (0);
1802 nfsmout:
1803 return (error);
1804 }
1805
1806 int
1807 nfs_msg(p, server, msg)
1808 struct proc *p;
1809 char *server, *msg;
1810 {
1811 tpr_t tpr;
1812
1813 if (p)
1814 tpr = tprintf_open(p);
1815 else
1816 tpr = NULL;
1817 tprintf(tpr, "nfs server %s: %s\n", server, msg);
1818 tprintf_close(tpr);
1819 return (0);
1820 }
1821
1822 #ifdef NFSSERVER
1823 int (*nfsrv3_procs[NFS_NPROCS]) __P((struct nfsrv_descript *,
1824 struct nfssvc_sock *, struct proc *,
1825 struct mbuf **)) = {
1826 nfsrv_null,
1827 nfsrv_getattr,
1828 nfsrv_setattr,
1829 nfsrv_lookup,
1830 nfsrv3_access,
1831 nfsrv_readlink,
1832 nfsrv_read,
1833 nfsrv_write,
1834 nfsrv_create,
1835 nfsrv_mkdir,
1836 nfsrv_symlink,
1837 nfsrv_mknod,
1838 nfsrv_remove,
1839 nfsrv_rmdir,
1840 nfsrv_rename,
1841 nfsrv_link,
1842 nfsrv_readdir,
1843 nfsrv_readdirplus,
1844 nfsrv_statfs,
1845 nfsrv_fsinfo,
1846 nfsrv_pathconf,
1847 nfsrv_commit,
1848 nqnfsrv_getlease,
1849 nqnfsrv_vacated,
1850 nfsrv_noop,
1851 nfsrv_noop
1852 };
1853
1854 /*
1855 * Socket upcall routine for the nfsd sockets.
1856 * The caddr_t arg is a pointer to the "struct nfssvc_sock".
1857 * Essentially do as much as possible non-blocking, else punt and it will
1858 * be called with M_WAIT from an nfsd.
1859 */
1860 void
1861 nfsrv_rcv(so, arg, waitflag)
1862 struct socket *so;
1863 caddr_t arg;
1864 int waitflag;
1865 {
1866 register struct nfssvc_sock *slp = (struct nfssvc_sock *)arg;
1867 register struct mbuf *m;
1868 struct mbuf *mp, *nam;
1869 struct uio auio;
1870 int flags, error;
1871
1872 if ((slp->ns_flag & SLP_VALID) == 0)
1873 return;
1874 #ifdef notdef
1875 /*
1876 * Define this to test for nfsds handling this under heavy load.
1877 */
1878 if (waitflag == M_DONTWAIT) {
1879 slp->ns_flag |= SLP_NEEDQ; goto dorecs;
1880 }
1881 #endif
1882 auio.uio_procp = NULL;
1883 if (so->so_type == SOCK_STREAM) {
1884 /*
1885 * If there are already records on the queue, defer soreceive()
1886 * to an nfsd so that there is feedback to the TCP layer that
1887 * the nfs servers are heavily loaded.
1888 */
1889 if (slp->ns_rec && waitflag == M_DONTWAIT) {
1890 slp->ns_flag |= SLP_NEEDQ;
1891 goto dorecs;
1892 }
1893
1894 /*
1895 * Do soreceive().
1896 */
1897 auio.uio_resid = 1000000000;
1898 flags = MSG_DONTWAIT;
1899 error = soreceive(so, &nam, &auio, &mp, (struct mbuf **)0, &flags);
1900 if (error || mp == (struct mbuf *)0) {
1901 if (error == EWOULDBLOCK)
1902 slp->ns_flag |= SLP_NEEDQ;
1903 else
1904 slp->ns_flag |= SLP_DISCONN;
1905 goto dorecs;
1906 }
1907 m = mp;
1908 if (slp->ns_rawend) {
1909 slp->ns_rawend->m_next = m;
1910 slp->ns_cc += 1000000000 - auio.uio_resid;
1911 } else {
1912 slp->ns_raw = m;
1913 slp->ns_cc = 1000000000 - auio.uio_resid;
1914 }
1915 while (m->m_next)
1916 m = m->m_next;
1917 slp->ns_rawend = m;
1918
1919 /*
1920 * Now try and parse record(s) out of the raw stream data.
1921 */
1922 error = nfsrv_getstream(slp, waitflag);
1923 if (error) {
1924 if (error == EPERM)
1925 slp->ns_flag |= SLP_DISCONN;
1926 else
1927 slp->ns_flag |= SLP_NEEDQ;
1928 }
1929 } else {
1930 do {
1931 auio.uio_resid = 1000000000;
1932 flags = MSG_DONTWAIT;
1933 error = soreceive(so, &nam, &auio, &mp,
1934 (struct mbuf **)0, &flags);
1935 if (mp) {
1936 nfs_realign(mp, 10 * NFSX_UNSIGNED);
1937 if (nam) {
1938 m = nam;
1939 m->m_next = mp;
1940 } else
1941 m = mp;
1942 if (slp->ns_recend)
1943 slp->ns_recend->m_nextpkt = m;
1944 else
1945 slp->ns_rec = m;
1946 slp->ns_recend = m;
1947 m->m_nextpkt = (struct mbuf *)0;
1948 }
1949 if (error) {
1950 if ((so->so_proto->pr_flags & PR_CONNREQUIRED)
1951 && error != EWOULDBLOCK) {
1952 slp->ns_flag |= SLP_DISCONN;
1953 goto dorecs;
1954 }
1955 }
1956 } while (mp);
1957 }
1958
1959 /*
1960 * Now try and process the request records, non-blocking.
1961 */
1962 dorecs:
1963 if (waitflag == M_DONTWAIT &&
1964 (slp->ns_rec || (slp->ns_flag & (SLP_NEEDQ | SLP_DISCONN))))
1965 nfsrv_wakenfsd(slp);
1966 }
1967
1968 /*
1969 * Try and extract an RPC request from the mbuf data list received on a
1970 * stream socket. The "waitflag" argument indicates whether or not it
1971 * can sleep.
1972 */
1973 int
1974 nfsrv_getstream(slp, waitflag)
1975 register struct nfssvc_sock *slp;
1976 int waitflag;
1977 {
1978 register struct mbuf *m, **mpp;
1979 register char *cp1, *cp2;
1980 register int len;
1981 struct mbuf *om, *m2, *recm = NULL;
1982 u_int32_t recmark;
1983
1984 if (slp->ns_flag & SLP_GETSTREAM)
1985 panic("nfs getstream");
1986 slp->ns_flag |= SLP_GETSTREAM;
1987 for (;;) {
1988 if (slp->ns_reclen == 0) {
1989 if (slp->ns_cc < NFSX_UNSIGNED) {
1990 slp->ns_flag &= ~SLP_GETSTREAM;
1991 return (0);
1992 }
1993 m = slp->ns_raw;
1994 if (m->m_len >= NFSX_UNSIGNED) {
1995 bcopy(mtod(m, caddr_t), (caddr_t)&recmark, NFSX_UNSIGNED);
1996 m->m_data += NFSX_UNSIGNED;
1997 m->m_len -= NFSX_UNSIGNED;
1998 } else {
1999 cp1 = (caddr_t)&recmark;
2000 cp2 = mtod(m, caddr_t);
2001 while (cp1 < ((caddr_t)&recmark) + NFSX_UNSIGNED) {
2002 while (m->m_len == 0) {
2003 m = m->m_next;
2004 cp2 = mtod(m, caddr_t);
2005 }
2006 *cp1++ = *cp2++;
2007 m->m_data++;
2008 m->m_len--;
2009 }
2010 }
2011 slp->ns_cc -= NFSX_UNSIGNED;
2012 recmark = ntohl(recmark);
2013 slp->ns_reclen = recmark & ~0x80000000;
2014 if (recmark & 0x80000000)
2015 slp->ns_flag |= SLP_LASTFRAG;
2016 else
2017 slp->ns_flag &= ~SLP_LASTFRAG;
2018 if (slp->ns_reclen < NFS_MINPACKET || slp->ns_reclen > NFS_MAXPACKET) {
2019 slp->ns_flag &= ~SLP_GETSTREAM;
2020 return (EPERM);
2021 }
2022 }
2023
2024 /*
2025 * Now get the record part.
2026 */
2027 if (slp->ns_cc == slp->ns_reclen) {
2028 recm = slp->ns_raw;
2029 slp->ns_raw = slp->ns_rawend = (struct mbuf *)0;
2030 slp->ns_cc = slp->ns_reclen = 0;
2031 } else if (slp->ns_cc > slp->ns_reclen) {
2032 len = 0;
2033 m = slp->ns_raw;
2034 om = (struct mbuf *)0;
2035 while (len < slp->ns_reclen) {
2036 if ((len + m->m_len) > slp->ns_reclen) {
2037 m2 = m_copym(m, 0, slp->ns_reclen - len,
2038 waitflag);
2039 if (m2) {
2040 if (om) {
2041 om->m_next = m2;
2042 recm = slp->ns_raw;
2043 } else
2044 recm = m2;
2045 m->m_data += slp->ns_reclen - len;
2046 m->m_len -= slp->ns_reclen - len;
2047 len = slp->ns_reclen;
2048 } else {
2049 slp->ns_flag &= ~SLP_GETSTREAM;
2050 return (EWOULDBLOCK);
2051 }
2052 } else if ((len + m->m_len) == slp->ns_reclen) {
2053 om = m;
2054 len += m->m_len;
2055 m = m->m_next;
2056 recm = slp->ns_raw;
2057 om->m_next = (struct mbuf *)0;
2058 } else {
2059 om = m;
2060 len += m->m_len;
2061 m = m->m_next;
2062 }
2063 }
2064 slp->ns_raw = m;
2065 slp->ns_cc -= len;
2066 slp->ns_reclen = 0;
2067 } else {
2068 slp->ns_flag &= ~SLP_GETSTREAM;
2069 return (0);
2070 }
2071
2072 /*
2073 * Accumulate the fragments into a record.
2074 */
2075 mpp = &slp->ns_frag;
2076 while (*mpp)
2077 mpp = &((*mpp)->m_next);
2078 *mpp = recm;
2079 if (slp->ns_flag & SLP_LASTFRAG) {
2080 nfs_realign(slp->ns_frag, 10 * NFSX_UNSIGNED);
2081 if (slp->ns_recend)
2082 slp->ns_recend->m_nextpkt = slp->ns_frag;
2083 else
2084 slp->ns_rec = slp->ns_frag;
2085 slp->ns_recend = slp->ns_frag;
2086 slp->ns_frag = (struct mbuf *)0;
2087 }
2088 }
2089 }
2090
2091 /*
2092 * Parse an RPC header.
2093 */
2094 int
2095 nfsrv_dorec(slp, nfsd, ndp)
2096 register struct nfssvc_sock *slp;
2097 struct nfsd *nfsd;
2098 struct nfsrv_descript **ndp;
2099 {
2100 register struct mbuf *m, *nam;
2101 register struct nfsrv_descript *nd;
2102 int error;
2103
2104 *ndp = NULL;
2105 if ((slp->ns_flag & SLP_VALID) == 0 ||
2106 (m = slp->ns_rec) == (struct mbuf *)0)
2107 return (ENOBUFS);
2108 slp->ns_rec = m->m_nextpkt;
2109 if (slp->ns_rec)
2110 m->m_nextpkt = (struct mbuf *)0;
2111 else
2112 slp->ns_recend = (struct mbuf *)0;
2113 if (m->m_type == MT_SONAME) {
2114 nam = m;
2115 m = m->m_next;
2116 nam->m_next = NULL;
2117 } else
2118 nam = NULL;
2119 MALLOC(nd, struct nfsrv_descript *, sizeof (struct nfsrv_descript),
2120 M_NFSRVDESC, M_WAITOK);
2121 nd->nd_md = nd->nd_mrep = m;
2122 nd->nd_nam2 = nam;
2123 nd->nd_dpos = mtod(m, caddr_t);
2124 error = nfs_getreq(nd, nfsd, TRUE);
2125 if (error) {
2126 m_freem(nam);
2127 free((caddr_t)nd, M_NFSRVDESC);
2128 return (error);
2129 }
2130 *ndp = nd;
2131 nfsd->nfsd_nd = nd;
2132 return (0);
2133 }
2134
2135
2136 /*
2137 * Search for a sleeping nfsd and wake it up.
2138 * SIDE EFFECT: If none found, set NFSD_CHECKSLP flag, so that one of the
2139 * running nfsds will go look for the work in the nfssvc_sock list.
2140 */
2141 void
2142 nfsrv_wakenfsd(slp)
2143 struct nfssvc_sock *slp;
2144 {
2145 register struct nfsd *nd;
2146
2147 if ((slp->ns_flag & SLP_VALID) == 0)
2148 return;
2149 for (nd = nfsd_head.tqh_first; nd != 0; nd = nd->nfsd_chain.tqe_next) {
2150 if (nd->nfsd_flag & NFSD_WAITING) {
2151 nd->nfsd_flag &= ~NFSD_WAITING;
2152 if (nd->nfsd_slp)
2153 panic("nfsd wakeup");
2154 slp->ns_sref++;
2155 nd->nfsd_slp = slp;
2156 wakeup((caddr_t)nd);
2157 return;
2158 }
2159 }
2160 slp->ns_flag |= SLP_DOREC;
2161 nfsd_head_flag |= NFSD_CHECKSLP;
2162 }
2163 #endif /* NFSSERVER */
2164