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