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