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