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