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