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