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