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