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