]> git.saurik.com Git - apple/xnu.git/blame - bsd/nfs/nfs_socket.c
xnu-344.21.73.tar.gz
[apple/xnu.git] / bsd / nfs / nfs_socket.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
d7e50217 6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
1c79356b 7 *
d7e50217
A
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1c79356b
A
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
d7e50217
A
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
1c79356b
A
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
26/*
27 * Copyright (c) 1989, 1991, 1993, 1995
28 * The Regents of the University of California. All rights reserved.
29 *
30 * This code is derived from software contributed to Berkeley by
31 * Rick Macklem at The University of Guelph.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * @(#)nfs_socket.c 8.5 (Berkeley) 3/30/95
62 * FreeBSD-Id: nfs_socket.c,v 1.30 1997/10/28 15:59:07 bde Exp $
63 */
64
65/*
66 * Socket operations for use by nfs
67 */
68
69#include <sys/param.h>
70#include <sys/systm.h>
71#include <sys/proc.h>
72#include <sys/mount.h>
73#include <sys/kernel.h>
74#include <sys/mbuf.h>
75#include <sys/malloc.h>
76#include <sys/vnode.h>
77#include <sys/domain.h>
78#include <sys/protosw.h>
79#include <sys/socket.h>
80#include <sys/socketvar.h>
81#include <sys/syslog.h>
82#include <sys/tprintf.h>
83#include <machine/spl.h>
84
85#include <sys/time.h>
86#include <kern/clock.h>
9bccf70c 87#include <sys/user.h>
1c79356b
A
88
89#include <netinet/in.h>
90#include <netinet/tcp.h>
91
92#include <nfs/rpcv2.h>
93#include <nfs/nfsproto.h>
94#include <nfs/nfs.h>
95#include <nfs/xdr_subs.h>
96#include <nfs/nfsm_subs.h>
97#include <nfs/nfsmount.h>
98#include <nfs/nfsnode.h>
99#include <nfs/nfsrtt.h>
100#include <nfs/nqnfs.h>
101
fa4905b1
A
102#include <sys/kdebug.h>
103
104#define FSDBG(A, B, C, D, E) \
105 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_NONE, \
106 (int)(B), (int)(C), (int)(D), (int)(E), 0)
107#define FSDBG_TOP(A, B, C, D, E) \
108 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_START, \
109 (int)(B), (int)(C), (int)(D), (int)(E), 0)
110#define FSDBG_BOT(A, B, C, D, E) \
111 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_END, \
112 (int)(B), (int)(C), (int)(D), (int)(E), 0)
113
1c79356b
A
114#define TRUE 1
115#define FALSE 0
116
117/*
118 * Estimate rto for an nfs rpc sent via. an unreliable datagram.
119 * Use the mean and mean deviation of rtt for the appropriate type of rpc
120 * for the frequent rpcs and a default for the others.
121 * The justification for doing "other" this way is that these rpcs
122 * happen so infrequently that timer est. would probably be stale.
123 * Also, since many of these rpcs are
124 * non-idempotent, a conservative timeout is desired.
125 * getattr, lookup - A+2D
126 * read, write - A+4D
127 * other - nm_timeo
128 */
129#define NFS_RTO(n, t) \
130 ((t) == 0 ? (n)->nm_timeo : \
131 ((t) < 3 ? \
132 (((((n)->nm_srtt[t-1] + 3) >> 2) + (n)->nm_sdrtt[t-1] + 1) >> 1) : \
133 ((((n)->nm_srtt[t-1] + 7) >> 3) + (n)->nm_sdrtt[t-1] + 1)))
134#define NFS_SRTT(r) (r)->r_nmp->nm_srtt[proct[(r)->r_procnum] - 1]
135#define NFS_SDRTT(r) (r)->r_nmp->nm_sdrtt[proct[(r)->r_procnum] - 1]
136/*
137 * External data, mostly RPC constants in XDR form
138 */
139extern u_long rpc_reply, rpc_msgdenied, rpc_mismatch, rpc_vers, rpc_auth_unix,
140 rpc_msgaccepted, rpc_call, rpc_autherr,
141 rpc_auth_kerb;
142extern u_long nfs_prog, nqnfs_prog;
143extern time_t nqnfsstarttime;
144extern struct nfsstats nfsstats;
145extern int nfsv3_procid[NFS_NPROCS];
146extern int nfs_ticks;
fa4905b1 147extern u_long nfs_xidwrap;
1c79356b
A
148
149/*
150 * Defines which timer to use for the procnum.
151 * 0 - default
152 * 1 - getattr
153 * 2 - lookup
154 * 3 - read
155 * 4 - write
156 */
157static int proct[NFS_NPROCS] = {
158 0, 1, 0, 2, 1, 3, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0,
159 0, 0, 0,
160};
161
162/*
163 * There is a congestion window for outstanding rpcs maintained per mount
164 * point. The cwnd size is adjusted in roughly the way that:
165 * Van Jacobson, Congestion avoidance and Control, In "Proceedings of
166 * SIGCOMM '88". ACM, August 1988.
167 * describes for TCP. The cwnd size is chopped in half on a retransmit timeout
168 * and incremented by 1/cwnd when each rpc reply is received and a full cwnd
169 * of rpcs is in progress.
170 * (The sent count and cwnd are scaled for integer arith.)
171 * Variants of "slow start" were tried and were found to be too much of a
172 * performance hit (ave. rtt 3 times larger),
173 * I suspect due to the large rtt that nfs rpcs have.
174 */
175#define NFS_CWNDSCALE 256
176#define NFS_MAXCWND (NFS_CWNDSCALE * 32)
177static int nfs_backoff[8] = { 2, 4, 8, 16, 32, 64, 128, 256, };
178int nfsrtton = 0;
179struct nfsrtt nfsrtt;
180
181static int nfs_msg __P((struct proc *,char *,char *));
182static int nfs_rcvlock __P((struct nfsreq *));
183static void nfs_rcvunlock __P((int *flagp));
184static int nfs_receive __P((struct nfsreq *rep, struct mbuf **aname,
185 struct mbuf **mp));
186static int nfs_reconnect __P((struct nfsreq *rep));
187#ifndef NFS_NOSERVER
188static int nfsrv_getstream __P((struct nfssvc_sock *,int));
189
190int (*nfsrv3_procs[NFS_NPROCS]) __P((struct nfsrv_descript *nd,
191 struct nfssvc_sock *slp,
192 struct proc *procp,
193 struct mbuf **mreqp)) = {
194 nfsrv_null,
195 nfsrv_getattr,
196 nfsrv_setattr,
197 nfsrv_lookup,
198 nfsrv3_access,
199 nfsrv_readlink,
200 nfsrv_read,
201 nfsrv_write,
202 nfsrv_create,
203 nfsrv_mkdir,
204 nfsrv_symlink,
205 nfsrv_mknod,
206 nfsrv_remove,
207 nfsrv_rmdir,
208 nfsrv_rename,
209 nfsrv_link,
210 nfsrv_readdir,
211 nfsrv_readdirplus,
212 nfsrv_statfs,
213 nfsrv_fsinfo,
214 nfsrv_pathconf,
215 nfsrv_commit,
216 nqnfsrv_getlease,
217 nqnfsrv_vacated,
218 nfsrv_noop,
219 nfsrv_noop
220};
221#endif /* NFS_NOSERVER */
222
fa4905b1
A
223/*
224 * NFSTRACE points were changed to FSDBG (KERNEL_DEBUG)
225 * But some of this code may prove useful someday...
226 */
227#undef NFSDIAG
1c79356b
A
228#if NFSDIAG
229int nfstraceindx = 0;
230struct nfstracerec nfstracebuf[NFSTBUFSIZ] = {{0,0,0,0}};
231
232#define NFSTRACESUSPENDERS
233#ifdef NFSTRACESUSPENDERS
234uint nfstracemask = 0xfff00200;
235int nfstracexid = -1;
236uint onfstracemask = 0;
237int nfstracesuspend = -1;
238#define NFSTRACE_SUSPEND \
239 { \
240 if (nfstracemask) { \
241 onfstracemask = nfstracemask; \
242 nfstracemask = 0; \
243 } \
244 }
245#define NFSTRACE_RESUME \
246 { \
247 nfstracesuspend = -1; \
248 if (!nfstracemask) \
249 nfstracemask = onfstracemask; \
250 }
251#define NFSTRACE_STARTSUSPENDCOUNTDOWN \
252 { \
253 nfstracesuspend = (nfstraceindx+100) % NFSTBUFSIZ; \
254 }
255#define NFSTRACE_SUSPENDING (nfstracesuspend != -1)
256#define NFSTRACE_SUSPENSEOVER \
257 (nfstracesuspend > 100 ? \
258 (nfstraceindx >= nfstracesuspend || \
259 nfstraceindx < nfstracesuspend - 100) : \
260 (nfstraceindx >= nfstracesuspend && \
261 nfstraceindx < nfstracesuspend + 8192 - 100))
262#else
263uint nfstracemask = 0;
264#endif /* NFSTRACESUSPENDERS */
265
266int nfsprnttimo = 1;
267
268int nfsodata[1024];
269int nfsoprocnum, nfsolen;
270int nfsbt[32], nfsbtlen;
271
272#if defined(__ppc__)
273int
274backtrace(int *where, int size)
275{
276 int register sp, *fp, numsaved;
277
278 __asm__ volatile("mr %0,r1" : "=r" (sp));
279
280 fp = (int *)*((int *)sp);
281 size /= sizeof(int);
282 for (numsaved = 0; numsaved < size; numsaved++) {
283 *where++ = fp[2];
284 if ((int)fp <= 0)
285 break;
286 fp = (int *)*fp;
287 }
288 return (numsaved);
289}
290#elif defined(__i386__)
291int
292backtrace()
293{
294 return (0); /* Till someone implements a real routine */
295}
296#else
297#error architecture not implemented.
298#endif
299
300void
301nfsdup(struct nfsreq *rep)
302{
303 int *ip, i, first = 1, end;
304 char *s, b[240];
305 struct mbuf *mb;
306
307 if ((nfs_debug & NFS_DEBUG_DUP) == 0)
308 return;
309 /* last mbuf in chain will be nfs content */
310 for (mb = rep->r_mreq; mb->m_next; mb = mb->m_next)
311 ;
312 if (rep->r_procnum == nfsoprocnum && mb->m_len == nfsolen &&
313 !bcmp((caddr_t)nfsodata, mb->m_data, nfsolen)) {
314 s = b + sprintf(b, "nfsdup x=%x p=%d h=", rep->r_xid,
315 rep->r_procnum);
316 end = (int)(VTONFS(rep->r_vp)->n_fhp);
317 ip = (int *)(end & ~3);
318 end += VTONFS(rep->r_vp)->n_fhsize;
319 while ((int)ip < end) {
320 i = *ip++;
321 if (first) { /* avoid leading zeroes */
322 if (i == 0)
323 continue;
324 first = 0;
325 s += sprintf(s, "%x", i);
326 } else
327 s += sprintf(s, "%08x", i);
328 }
329 if (first)
330 sprintf(s, "%x", 0);
331 else /* eliminate trailing zeroes */
332 while (*--s == '0')
333 *s = 0;
334 /*
335 * set a breakpoint here and you can view the
336 * current backtrace and the one saved in nfsbt
337 */
338 kprintf("%s\n", b);
339 }
340 nfsoprocnum = rep->r_procnum;
341 nfsolen = mb->m_len;
342 bcopy(mb->m_data, (caddr_t)nfsodata, mb->m_len);
343 nfsbtlen = backtrace(&nfsbt, sizeof(nfsbt));
344}
345#endif /* NFSDIAG */
346
347/*
348 * Initialize sockets and congestion for a new NFS connection.
349 * We do not free the sockaddr if error.
350 */
351int
352nfs_connect(nmp, rep)
353 register struct nfsmount *nmp;
354 struct nfsreq *rep;
355{
356 register struct socket *so;
357 int s, error, rcvreserve, sndreserve;
358 struct sockaddr *saddr;
359 struct sockaddr_in sin;
360 u_short tport;
361
362 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
363 nmp->nm_so = (struct socket *)0;
364 saddr = mtod(nmp->nm_nam, struct sockaddr *);
365 error = socreate(saddr->sa_family, &nmp->nm_so, nmp->nm_sotype,
366 nmp->nm_soproto);
367 if (error) {
368 goto bad;
369 }
370 so = nmp->nm_so;
371 nmp->nm_soflags = so->so_proto->pr_flags;
372
373 /*
374 * Some servers require that the client port be a reserved port number.
375 */
376 if (saddr->sa_family == AF_INET && (nmp->nm_flag & NFSMNT_RESVPORT)) {
377 sin.sin_len = sizeof (struct sockaddr_in);
378 sin.sin_family = AF_INET;
379 sin.sin_addr.s_addr = INADDR_ANY;
380 tport = IPPORT_RESERVED - 1;
381 sin.sin_port = htons(tport);
382
383 while ((error = sobind(so, (struct sockaddr *) &sin) == EADDRINUSE) &&
384 (--tport > IPPORT_RESERVED / 2))
385 sin.sin_port = htons(tport);
386 if (error) {
387 goto bad;
388 }
389 }
390
391 /*
392 * Protocols that do not require connections may be optionally left
393 * unconnected for servers that reply from a port other than NFS_PORT.
394 */
395 if (nmp->nm_flag & NFSMNT_NOCONN) {
396 if (nmp->nm_soflags & PR_CONNREQUIRED) {
397 error = ENOTCONN;
398 goto bad;
399 }
400 } else {
401 error = soconnect(so, mtod(nmp->nm_nam, struct sockaddr *));
402 if (error) {
403 goto bad;
404 }
405
406 /*
407 * Wait for the connection to complete. Cribbed from the
408 * connect system call but with the wait timing out so
409 * that interruptible mounts don't hang here for a long time.
410 */
411 s = splnet();
412 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
413 (void) tsleep((caddr_t)&so->so_timeo, PSOCK,
414 "nfscon", 2 * hz);
415 if ((so->so_state & SS_ISCONNECTING) &&
416 so->so_error == 0 && rep &&
417 (error = nfs_sigintr(nmp, rep, rep->r_procp))) {
418 so->so_state &= ~SS_ISCONNECTING;
419 splx(s);
420 goto bad;
421 }
422 }
423 if (so->so_error) {
424 error = so->so_error;
425 so->so_error = 0;
426 splx(s);
427 goto bad;
428 }
429 splx(s);
430 }
431 if (nmp->nm_flag & (NFSMNT_SOFT | NFSMNT_INT)) {
432 so->so_rcv.sb_timeo = (5 * hz);
433 so->so_snd.sb_timeo = (5 * hz);
434 } else {
435 so->so_rcv.sb_timeo = 0;
436 so->so_snd.sb_timeo = 0;
437 }
438 if (nmp->nm_sotype == SOCK_DGRAM) {
439 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * 2;
440 rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR) * 2;
441 } else if (nmp->nm_sotype == SOCK_SEQPACKET) {
442 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * 2;
443 rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR) * 2;
444 } else {
445 if (nmp->nm_sotype != SOCK_STREAM)
446 panic("nfscon sotype");
447
448 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
449 struct sockopt sopt;
450 int val;
451
452 bzero(&sopt, sizeof sopt);
453 sopt.sopt_level = SOL_SOCKET;
454 sopt.sopt_name = SO_KEEPALIVE;
455 sopt.sopt_val = &val;
456 sopt.sopt_valsize = sizeof val;
457 val = 1;
458 sosetopt(so, &sopt);
459 }
460 if (so->so_proto->pr_protocol == IPPROTO_TCP) {
461 struct sockopt sopt;
462 int val;
463
464 bzero(&sopt, sizeof sopt);
465 sopt.sopt_level = IPPROTO_TCP;
466 sopt.sopt_name = TCP_NODELAY;
467 sopt.sopt_val = &val;
468 sopt.sopt_valsize = sizeof val;
469 val = 1;
470 sosetopt(so, &sopt);
471 }
472
473 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR + sizeof (u_long))
474 * 2;
475 rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR + sizeof (u_long))
476 * 2;
477 }
478
479 error = soreserve(so, sndreserve, rcvreserve);
480 if (error) {
481 goto bad;
482 }
483 so->so_rcv.sb_flags |= SB_NOINTR;
484 so->so_snd.sb_flags |= SB_NOINTR;
485
486 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
487
488 /* Initialize other non-zero congestion variables */
489 nmp->nm_srtt[0] = nmp->nm_srtt[1] = nmp->nm_srtt[2] =
490 nmp->nm_srtt[3] = (NFS_TIMEO << 3);
491 nmp->nm_sdrtt[0] = nmp->nm_sdrtt[1] = nmp->nm_sdrtt[2] =
492 nmp->nm_sdrtt[3] = 0;
493 nmp->nm_cwnd = NFS_MAXCWND / 2; /* Initial send window */
494 nmp->nm_sent = 0;
fa4905b1 495 FSDBG(529, nmp, nmp->nm_flag, nmp->nm_soflags, nmp->nm_cwnd);
1c79356b
A
496 nmp->nm_timeouts = 0;
497 return (0);
498
499bad:
500 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
501 nfs_disconnect(nmp);
502 return (error);
503}
504
505/*
506 * Reconnect routine:
507 * Called when a connection is broken on a reliable protocol.
508 * - clean up the old socket
509 * - nfs_connect() again
510 * - set R_MUSTRESEND for all outstanding requests on mount point
511 * If this fails the mount point is DEAD!
512 * nb: Must be called with the nfs_sndlock() set on the mount point.
513 */
514static int
515nfs_reconnect(rep)
516 register struct nfsreq *rep;
517{
518 register struct nfsreq *rp;
519 register struct nfsmount *nmp = rep->r_nmp;
520 int error;
521
522 nfs_disconnect(nmp);
523 while ((error = nfs_connect(nmp, rep))) {
524 if (error == EINTR || error == ERESTART)
525 return (EINTR);
526 (void) tsleep((caddr_t)&lbolt, PSOCK, "nfscon", 0);
527 }
528
529 NFS_DPF(DUP, ("nfs_reconnect RESEND\n"));
530 /*
531 * Loop through outstanding request list and fix up all requests
532 * on old socket.
533 */
534 for (rp = nfs_reqq.tqh_first; rp != 0; rp = rp->r_chain.tqe_next) {
535 if (rp->r_nmp == nmp)
536 rp->r_flags |= R_MUSTRESEND;
537 }
538 return (0);
539}
540
541/*
542 * NFS disconnect. Clean up and unlink.
543 */
544void
545nfs_disconnect(nmp)
546 register struct nfsmount *nmp;
547{
548 register struct socket *so;
549
550 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
551 if (nmp->nm_so) {
552 so = nmp->nm_so;
553 nmp->nm_so = (struct socket *)0;
554 soshutdown(so, 2);
555 soclose(so);
556 }
557 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
558}
559
560/*
561 * This is the nfs send routine. For connection based socket types, it
562 * must be called with an nfs_sndlock() on the socket.
563 * "rep == NULL" indicates that it has been called from a server.
564 * For the client side:
565 * - return EINTR if the RPC is terminated, 0 otherwise
566 * - set R_MUSTRESEND if the send fails for any reason
567 * - do any cleanup required by recoverable socket errors (???)
568 * For the server side:
569 * - return EINTR or ERESTART if interrupted by a signal
570 * - return EPIPE if a connection is lost for connection based sockets (TCP...)
571 * - do any cleanup required by recoverable socket errors (???)
572 */
573int
574nfs_send(so, nam, top, rep)
575 register struct socket *so;
576 struct mbuf *nam;
577 register struct mbuf *top;
578 struct nfsreq *rep;
579{
580 struct sockaddr *sendnam;
581 int error, soflags, flags;
582 int xidqueued = 0;
583 struct nfsreq *rp;
584 char savenametolog[MNAMELEN];
585
586 if (rep) {
587 if (rep->r_flags & R_SOFTTERM) {
588 m_freem(top);
589 return (EINTR);
590 }
591 if ((so = rep->r_nmp->nm_so) == NULL) {
592 rep->r_flags |= R_MUSTRESEND;
593 m_freem(top);
594 return (0);
595 }
596 rep->r_flags &= ~R_MUSTRESEND;
597 soflags = rep->r_nmp->nm_soflags;
598 for (rp = nfs_reqq.tqh_first; rp; rp = rp->r_chain.tqe_next)
599 if (rp == rep)
600 break;
601 if (rp)
602 xidqueued = rp->r_xid;
603 } else
604 soflags = so->so_proto->pr_flags;
605 if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED) ||
606 (nam == 0))
607 sendnam = (struct sockaddr *)0;
608 else
609 sendnam = mtod(nam, struct sockaddr *);
610
611 if (so->so_type == SOCK_SEQPACKET)
612 flags = MSG_EOR;
613 else
614 flags = 0;
615
616#if NFSDIAG
617 if (rep)
618 nfsdup(rep);
619#endif
620 /*
621 * Save the name here in case mount point goes away when we switch
622 * funnels. The name is using local stack and is large, but don't
623 * want to block if we malloc.
624 */
625 if (rep)
626 strncpy(savenametolog,
627 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname,
628 MNAMELEN);
629 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
630 error = sosend(so, sendnam, (struct uio *)0, top,
631 (struct mbuf *)0, flags);
632 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
633
634 if (error) {
635 if (rep) {
636 if (xidqueued) {
637 for (rp = nfs_reqq.tqh_first; rp;
638 rp = rp->r_chain.tqe_next)
639 if (rp == rep && rp->r_xid == xidqueued)
640 break;
641 if (!rp)
642 panic("nfs_send: error %d xid %x gone",
643 error, xidqueued);
644 }
645 log(LOG_INFO, "nfs send error %d for server %s\n",
646 error, savenametolog);
647 /*
648 * Deal with errors for the client side.
649 */
650 if (rep->r_flags & R_SOFTTERM)
651 error = EINTR;
652 else {
653 rep->r_flags |= R_MUSTRESEND;
654 NFS_DPF(DUP,
655 ("nfs_send RESEND error=%d\n", error));
656 }
657 } else
658 log(LOG_INFO, "nfsd send error %d\n", error);
659
660 /*
661 * Handle any recoverable (soft) socket errors here. (???)
662 */
663 if (error != EINTR && error != ERESTART &&
664 error != EWOULDBLOCK && error != EPIPE)
665 error = 0;
666 }
667 return (error);
668}
669
670/*
671 * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all
672 * done by soreceive(), but for SOCK_STREAM we must deal with the Record
673 * Mark and consolidate the data into a new mbuf list.
674 * nb: Sometimes TCP passes the data up to soreceive() in long lists of
675 * small mbufs.
676 * For SOCK_STREAM we must be very careful to read an entire record once
677 * we have read any of it, even if the system call has been interrupted.
678 */
679static int
680nfs_receive(rep, aname, mp)
681 register struct nfsreq *rep;
682 struct mbuf **aname;
683 struct mbuf **mp;
684{
685 register struct socket *so;
686 struct uio auio;
687 struct iovec aio;
688 register struct mbuf *m;
689 struct mbuf *control;
690 u_long len;
691 struct sockaddr **getnam;
692 struct sockaddr *tmp_nam;
693 struct mbuf *mhck;
694 struct sockaddr_in *sin;
695 int error, sotype, rcvflg;
696 struct proc *p = current_proc(); /* XXX */
697
698 /*
699 * Set up arguments for soreceive()
700 */
701 *mp = (struct mbuf *)0;
702 *aname = (struct mbuf *)0;
703 sotype = rep->r_nmp->nm_sotype;
704
705 /*
706 * For reliable protocols, lock against other senders/receivers
707 * in case a reconnect is necessary.
708 * For SOCK_STREAM, first get the Record Mark to find out how much
709 * more there is to get.
710 * We must lock the socket against other receivers
711 * until we have an entire rpc request/reply.
712 */
713 if (sotype != SOCK_DGRAM) {
714 error = nfs_sndlock(&rep->r_nmp->nm_flag, rep);
715 if (error)
716 return (error);
717tryagain:
718 /*
719 * Check for fatal errors and resending request.
720 */
721 /*
722 * Ugh: If a reconnect attempt just happened, nm_so
723 * would have changed. NULL indicates a failed
724 * attempt that has essentially shut down this
725 * mount point.
726 */
727 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM)) {
728 nfs_sndunlock(&rep->r_nmp->nm_flag);
729 return (EINTR);
730 }
731 so = rep->r_nmp->nm_so;
732 if (!so) {
733 error = nfs_reconnect(rep);
734 if (error) {
735 nfs_sndunlock(&rep->r_nmp->nm_flag);
736 return (error);
737 }
738 goto tryagain;
739 }
740 while (rep->r_flags & R_MUSTRESEND) {
741 m = m_copym(rep->r_mreq, 0, M_COPYALL, M_WAIT);
742 nfsstats.rpcretries++;
743 NFS_DPF(DUP,
744 ("nfs_receive RESEND %s\n",
745 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname));
746 error = nfs_send(so, rep->r_nmp->nm_nam, m, rep);
747 /*
748 * we also hold rcv lock so rep is still
749 * legit this point
750 */
751 if (error) {
752 if (error == EINTR || error == ERESTART ||
753 (error = nfs_reconnect(rep))) {
754 nfs_sndunlock(&rep->r_nmp->nm_flag);
755 return (error);
756 }
757 goto tryagain;
758 }
759 }
760 nfs_sndunlock(&rep->r_nmp->nm_flag);
761 if (sotype == SOCK_STREAM) {
762 aio.iov_base = (caddr_t) &len;
763 aio.iov_len = sizeof(u_long);
764 auio.uio_iov = &aio;
765 auio.uio_iovcnt = 1;
766 auio.uio_segflg = UIO_SYSSPACE;
767 auio.uio_rw = UIO_READ;
768 auio.uio_offset = 0;
769 auio.uio_resid = sizeof(u_long);
770 auio.uio_procp = p;
771 do {
772 rcvflg = MSG_WAITALL;
773 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
774 error = soreceive(so, (struct sockaddr **)0, &auio,
775 (struct mbuf **)0, (struct mbuf **)0, &rcvflg);
776 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
777 if (!rep->r_nmp) /* if unmounted then bailout */
778 goto shutout;
779 if (error == EWOULDBLOCK && rep) {
780 if (rep->r_flags & R_SOFTTERM)
781 return (EINTR);
782 }
783 } while (error == EWOULDBLOCK);
784 if (!error && auio.uio_resid > 0) {
785 log(LOG_INFO,
786 "short receive (%d/%d) from nfs server %s\n",
787 sizeof(u_long) - auio.uio_resid,
788 sizeof(u_long),
789 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
790 error = EPIPE;
791 }
792 if (error)
793 goto errout;
794 len = ntohl(len) & ~0x80000000;
795 /*
796 * This is SERIOUS! We are out of sync with the sender
797 * and forcing a disconnect/reconnect is all I can do.
798 */
799 if (len > NFS_MAXPACKET) {
800 log(LOG_ERR, "%s (%d) from nfs server %s\n",
801 "impossible packet length",
802 len,
803 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
804 error = EFBIG;
805 goto errout;
806 }
807 auio.uio_resid = len;
808
809 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
810 do {
811 rcvflg = MSG_WAITALL;
812 error = soreceive(so, (struct sockaddr **)0,
813 &auio, mp, (struct mbuf **)0, &rcvflg);
814 if (!rep->r_nmp) /* if unmounted then bailout */ {
815 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
816 goto shutout;
817 }
818 } while (error == EWOULDBLOCK || error == EINTR ||
819 error == ERESTART);
820
821 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
822
823 if (!error && auio.uio_resid > 0) {
824 log(LOG_INFO,
825 "short receive (%d/%d) from nfs server %s\n",
826 len - auio.uio_resid, len,
827 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
828 error = EPIPE;
829 }
830 } else {
831 /*
832 * NB: Since uio_resid is big, MSG_WAITALL is ignored
833 * and soreceive() will return when it has either a
834 * control msg or a data msg.
835 * We have no use for control msg., but must grab them
836 * and then throw them away so we know what is going
837 * on.
838 */
839 auio.uio_resid = len = 100000000; /* Anything Big */
840 auio.uio_procp = p;
841
842 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
843 do {
844 rcvflg = 0;
845 error = soreceive(so, (struct sockaddr **)0,
846 &auio, mp, &control, &rcvflg);
847 if (!rep->r_nmp) /* if unmounted then bailout */ {
848 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
849 goto shutout;
850 }
851 if (control)
852 m_freem(control);
853 if (error == EWOULDBLOCK && rep) {
854 if (rep->r_flags & R_SOFTTERM) {
855 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
856 return (EINTR);
857 }
858 }
859 } while (error == EWOULDBLOCK ||
860 (!error && *mp == NULL && control));
861
862 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
863
864 if ((rcvflg & MSG_EOR) == 0)
865 printf("Egad!!\n");
866 if (!error && *mp == NULL)
867 error = EPIPE;
868 len -= auio.uio_resid;
869 }
870errout:
871 if (error && error != EINTR && error != ERESTART) {
872 m_freem(*mp);
873 *mp = (struct mbuf *)0;
874 if (error != EPIPE)
875 log(LOG_INFO,
876 "receive error %d from nfs server %s\n",
877 error,
878 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
879 error = nfs_sndlock(&rep->r_nmp->nm_flag, rep);
880 if (!error)
881 error = nfs_reconnect(rep);
882 if (!error)
883 goto tryagain;
884 }
885 } else {
886 if ((so = rep->r_nmp->nm_so) == NULL)
887 return (EACCES);
888 if (so->so_state & SS_ISCONNECTED)
889 getnam = (struct sockaddr **)0;
890 else
891 getnam = &tmp_nam;;
892 auio.uio_resid = len = 1000000;
893 auio.uio_procp = p;
894
895 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
896 do {
897 rcvflg = 0;
898 error = soreceive(so, getnam, &auio, mp,
899 (struct mbuf **)0, &rcvflg);
900
901 if ((getnam) && (*getnam)) {
902 MGET(mhck, M_WAIT, MT_SONAME);
903 mhck->m_len = (*getnam)->sa_len;
904 sin = mtod(mhck, struct sockaddr_in *);
905 bcopy(*getnam, sin, sizeof(struct sockaddr_in));
906 mhck->m_hdr.mh_len = sizeof(struct sockaddr_in);
907 FREE(*getnam, M_SONAME);
908 *aname = mhck;
909 }
910 if (!rep->r_nmp) /* if unmounted then bailout */ {
911 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
912 goto shutout;
913 }
914
915 if (error == EWOULDBLOCK &&
916 (rep->r_flags & R_SOFTTERM)) {
917 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
918 return (EINTR);
919 }
920 } while (error == EWOULDBLOCK);
921
922 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
923 len -= auio.uio_resid;
924 }
925shutout:
926 if (error) {
927 m_freem(*mp);
928 *mp = (struct mbuf *)0;
929 }
930 return (error);
931}
932
933/*
934 * Implement receipt of reply on a socket.
935 * We must search through the list of received datagrams matching them
936 * with outstanding requests using the xid, until ours is found.
937 */
938/* ARGSUSED */
939int
940nfs_reply(myrep)
941 struct nfsreq *myrep;
942{
943 register struct nfsreq *rep;
944 register struct nfsmount *nmp = myrep->r_nmp;
945 register long t1;
946 struct mbuf *mrep, *md;
947 struct mbuf *nam;
948 u_long rxid, *tl;
949 caddr_t dpos, cp2;
950 int error;
951
952 /*
953 * Loop around until we get our own reply
954 */
955 for (;;) {
956 /*
957 * Lock against other receivers so that I don't get stuck in
958 * sbwait() after someone else has received my reply for me.
959 * Also necessary for connection based protocols to avoid
960 * race conditions during a reconnect.
961 * If nfs_rcvlock() returns EALREADY, that means that
962 * the reply has already been recieved by another
963 * process and we can return immediately. In this
964 * case, the lock is not taken to avoid races with
965 * other processes.
966 */
967 error = nfs_rcvlock(myrep);
968 if (error == EALREADY)
969 return (0);
970 if (error)
971 return (error);
0b4e3aa0 972
1c79356b
A
973 /*
974 * If we slept after putting bits otw, then reply may have
975 * arrived. In which case returning is required, or we
976 * would hang trying to nfs_receive an already received reply.
977 */
978 if (myrep->r_mrep != NULL) {
979 nfs_rcvunlock(&nmp->nm_flag);
fa4905b1 980 FSDBG(530, myrep->r_xid, myrep, myrep->r_nmp, -1);
1c79356b
A
981 return (0);
982 }
983 /*
0b4e3aa0 984 * Get the next Rpc reply off the socket. Assume myrep->r_nmp
fa4905b1 985 * is still intact by checks done in nfs_rcvlock.
1c79356b
A
986 */
987 error = nfs_receive(myrep, &nam, &mrep);
988 /*
0b4e3aa0 989 * Bailout asap if nfsmount struct gone (unmounted).
1c79356b 990 */
9bccf70c 991 if (!myrep->r_nmp || !nmp->nm_so) {
fa4905b1 992 FSDBG(530, myrep->r_xid, myrep, nmp, -2);
1c79356b
A
993 return (ECONNABORTED);
994 }
995 if (error) {
fa4905b1 996 FSDBG(530, myrep->r_xid, myrep, nmp, error);
1c79356b
A
997 nfs_rcvunlock(&nmp->nm_flag);
998
d7e50217
A
999 /* Bailout asap if nfsmount struct gone (unmounted). */
1000 if (!myrep->r_nmp || !nmp->nm_so)
1001 return (ECONNABORTED);
1002
1c79356b
A
1003 /*
1004 * Ignore routing errors on connectionless protocols??
1005 */
1006 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
1007 nmp->nm_so->so_error = 0;
1008 if (myrep->r_flags & R_GETONEREP)
1009 return (0);
1010 continue;
1011 }
1012 return (error);
1013 }
1014 if (nam)
1015 m_freem(nam);
1016
1017 /*
1018 * We assume all is fine, but if we did not have an error
1019 * and mrep is 0, better not dereference it. nfs_receieve
1020 * calls soreceive which carefully sets error=0 when it got
1021 * errors on sbwait (tsleep). In most cases, I assume that's
1022 * so we could go back again. In tcp case, EPIPE is returned.
1023 * In udp, case nfs_receive gets back here with no error and no
1024 * mrep. Is the right fix to have soreceive check for process
1025 * aborted after sbwait and return something non-zero? Should
1026 * nfs_receive give an EPIPE? Too risky to play with those
1027 * two this late in game for a shutdown problem. Instead,
1028 * just check here and get out. (ekn)
1029 */
1030 if (!mrep) {
fa4905b1 1031 FSDBG(530, myrep->r_xid, myrep, nmp, -3);
1c79356b
A
1032 return (ECONNABORTED); /* sounds good */
1033 }
1034
1035 /*
1036 * Get the xid and check that it is an rpc reply
1037 */
1038 md = mrep;
1039 dpos = mtod(md, caddr_t);
1040 nfsm_dissect(tl, u_long *, 2*NFSX_UNSIGNED);
1041 rxid = *tl++;
1042 if (*tl != rpc_reply) {
1043#ifndef NFS_NOSERVER
1044 if (nmp->nm_flag & NFSMNT_NQNFS) {
1045 if (nqnfs_callback(nmp, mrep, md, dpos))
1046 nfsstats.rpcinvalid++;
1047 } else {
1048 nfsstats.rpcinvalid++;
1049 m_freem(mrep);
1050 }
1051#else
1052 nfsstats.rpcinvalid++;
1053 m_freem(mrep);
1054#endif
1055nfsmout:
1056 if (nmp->nm_flag & NFSMNT_RCVLOCK)
1057 nfs_rcvunlock(&nmp->nm_flag);
1058 if (myrep->r_flags & R_GETONEREP)
1059 return (0); /* this path used by NQNFS */
1060 continue;
1061 }
1062
1063 /*
1064 * Loop through the request list to match up the reply
1065 * Iff no match, just drop the datagram
1066 */
1067 for (rep = nfs_reqq.tqh_first; rep != 0;
1068 rep = rep->r_chain.tqe_next) {
1069 if (rep->r_mrep == NULL && rxid == rep->r_xid) {
1070 /* Found it.. */
1071 rep->r_mrep = mrep;
1072 rep->r_md = md;
1073 rep->r_dpos = dpos;
1074 if (nfsrtton) {
1075 struct rttl *rt;
1076
1077 rt = &nfsrtt.rttl[nfsrtt.pos];
1078 rt->proc = rep->r_procnum;
1079 rt->rto = NFS_RTO(nmp, proct[rep->r_procnum]);
1080 rt->sent = nmp->nm_sent;
1081 rt->cwnd = nmp->nm_cwnd;
1082 if (proct[rep->r_procnum] == 0)
1083 panic("nfs_reply: proct[%d] is zero", rep->r_procnum);
1084 rt->srtt = nmp->nm_srtt[proct[rep->r_procnum] - 1];
1085 rt->sdrtt = nmp->nm_sdrtt[proct[rep->r_procnum] - 1];
1086 rt->fsid = nmp->nm_mountp->mnt_stat.f_fsid;
1087 rt->tstamp = time;
1088 if (rep->r_flags & R_TIMING)
1089 rt->rtt = rep->r_rtt;
1090 else
1091 rt->rtt = 1000000;
1092 nfsrtt.pos = (nfsrtt.pos + 1) % NFSRTTLOGSIZ;
1093 }
1094 /*
1095 * Update congestion window.
1096 * Do the additive increase of
1097 * one rpc/rtt.
1098 */
fa4905b1
A
1099 FSDBG(530, rep->r_xid, rep, nmp->nm_sent,
1100 nmp->nm_cwnd);
1c79356b
A
1101 if (nmp->nm_cwnd <= nmp->nm_sent) {
1102 nmp->nm_cwnd +=
1103 (NFS_CWNDSCALE * NFS_CWNDSCALE +
1104 (nmp->nm_cwnd >> 1)) / nmp->nm_cwnd;
1105 if (nmp->nm_cwnd > NFS_MAXCWND)
1106 nmp->nm_cwnd = NFS_MAXCWND;
1107 }
1108 if (!(rep->r_flags & R_SENT))
1109 printf("nfs_reply: unsent xid=%x",
1110 rep->r_xid);
1111 rep->r_flags &= ~R_SENT;
1112 nmp->nm_sent -= NFS_CWNDSCALE;
1113 /*
1114 * Update rtt using a gain of 0.125 on the mean
1115 * and a gain of 0.25 on the deviation.
1116 */
1117 if (rep->r_flags & R_TIMING) {
1118 /*
1119 * Since the timer resolution of
1120 * NFS_HZ is so course, it can often
1121 * result in r_rtt == 0. Since
1122 * r_rtt == N means that the actual
1123 * rtt is between N+dt and N+2-dt ticks,
1124 * add 1.
1125 */
1126 if (proct[rep->r_procnum] == 0)
1127 panic("nfs_reply: proct[%d] is zero", rep->r_procnum);
1128 t1 = rep->r_rtt + 1;
1129 t1 -= (NFS_SRTT(rep) >> 3);
1130 NFS_SRTT(rep) += t1;
1131 if (t1 < 0)
1132 t1 = -t1;
1133 t1 -= (NFS_SDRTT(rep) >> 2);
1134 NFS_SDRTT(rep) += t1;
1135 }
1136 nmp->nm_timeouts = 0;
1137 break;
1138 }
1139 }
1140 nfs_rcvunlock(&nmp->nm_flag);
1141 /*
1142 * If not matched to a request, drop it.
1143 * If it's mine, get out.
1144 */
1145 if (rep == 0) {
1146 nfsstats.rpcunexpected++;
1147 m_freem(mrep);
1148 } else if (rep == myrep) {
1149 if (rep->r_mrep == NULL)
1150 panic("nfs_reply: nil r_mrep");
1151 return (0);
1152 }
fa4905b1
A
1153 FSDBG(530, myrep->r_xid, myrep, rep,
1154 rep ? rep->r_xid : myrep->r_flags);
1c79356b
A
1155 if (myrep->r_flags & R_GETONEREP)
1156 return (0); /* this path used by NQNFS */
1157 }
1158}
1159
1160/*
1161 * nfs_request - goes something like this
1162 * - fill in request struct
1163 * - links it into list
1164 * - calls nfs_send() for first transmit
1165 * - calls nfs_receive() to get reply
1166 * - break down rpc header and return with nfs reply pointed to
1167 * by mrep or error
1168 * nb: always frees up mreq mbuf list
1169 */
1170int
fa4905b1 1171nfs_request(vp, mrest, procnum, procp, cred, mrp, mdp, dposp, xidp)
1c79356b
A
1172 struct vnode *vp;
1173 struct mbuf *mrest;
1174 int procnum;
1175 struct proc *procp;
1176 struct ucred *cred;
1177 struct mbuf **mrp;
1178 struct mbuf **mdp;
1179 caddr_t *dposp;
fa4905b1 1180 u_int64_t *xidp;
1c79356b
A
1181{
1182 register struct mbuf *m, *mrep;
1183 register struct nfsreq *rep, *rp;
1184 register u_long *tl;
1185 register int i;
1186 struct nfsmount *nmp;
1187 struct mbuf *md, *mheadend;
1188 struct nfsnode *np;
1189 char nickv[RPCX_NICKVERF];
1190 time_t reqtime, waituntil;
1191 caddr_t dpos, cp2;
1192 int t1, nqlflag, cachable, s, error = 0, mrest_len, auth_len, auth_type;
1193 int trylater_delay = NQ_TRYLATERDEL, trylater_cnt = 0, failed_auth = 0;
1194 int verf_len, verf_type;
1195 u_long xid;
1196 u_quad_t frev;
1197 char *auth_str, *verf_str;
1198 NFSKERBKEY_T key; /* save session key */
1199
fa4905b1
A
1200 if (xidp)
1201 *xidp = 0;
1c79356b
A
1202 nmp = VFSTONFS(vp->v_mount);
1203 MALLOC_ZONE(rep, struct nfsreq *,
1204 sizeof(struct nfsreq), M_NFSREQ, M_WAITOK);
fa4905b1 1205 FSDBG_TOP(531, vp, procnum, nmp, rep);
1c79356b
A
1206
1207 /*
1208 * make sure if we blocked above, that the file system didn't get
1209 * unmounted leaving nmp bogus value to trip on later and crash.
1210 * Note nfs_unmount will set rep->r_nmp if unmounted volume, but we
1211 * aren't that far yet. SO this is best we can do. I wanted to check
1212 * for vp->v_mount = 0 also below, but that caused reboot crash.
1213 * Something must think it's okay for vp-v_mount=0 during booting.
1214 * Thus the best I can do here is see if we still have a vnode.
1215 */
1216
1217 if (vp->v_type == VBAD) {
fa4905b1 1218 FSDBG_BOT(531, 1, vp, nmp, rep);
1c79356b
A
1219 _FREE_ZONE((caddr_t)rep, sizeof (struct nfsreq), M_NFSREQ);
1220 return (EINVAL);
1221 }
1222 rep->r_nmp = nmp;
1223 rep->r_vp = vp;
1224 rep->r_procp = procp;
1225 rep->r_procnum = procnum;
1226 i = 0;
1227 m = mrest;
1228 while (m) {
1229 i += m->m_len;
1230 m = m->m_next;
1231 }
1232 mrest_len = i;
1233
1234 /*
1235 * Get the RPC header with authorization.
1236 */
1237kerbauth:
1238 verf_str = auth_str = (char *)0;
1239 if (nmp->nm_flag & NFSMNT_KERB) {
1240 verf_str = nickv;
1241 verf_len = sizeof (nickv);
1242 auth_type = RPCAUTH_KERB4;
1243 bzero((caddr_t)key, sizeof (key));
1244 if (failed_auth || nfs_getnickauth(nmp, cred, &auth_str,
1245 &auth_len, verf_str, verf_len)) {
1246 error = nfs_getauth(nmp, rep, cred, &auth_str,
1247 &auth_len, verf_str, &verf_len, key);
1248 if (error) {
fa4905b1 1249 FSDBG_BOT(531, 2, vp, error, rep);
1c79356b
A
1250 _FREE_ZONE((caddr_t)rep,
1251 sizeof (struct nfsreq), M_NFSREQ);
1252 m_freem(mrest);
1253 return (error);
1254 }
1255 }
1256 } else {
1257 auth_type = RPCAUTH_UNIX;
1258 if (cred->cr_ngroups < 1)
1259 panic("nfsreq nogrps");
1260 auth_len = ((((cred->cr_ngroups - 1) > nmp->nm_numgrps) ?
1261 nmp->nm_numgrps : (cred->cr_ngroups - 1)) << 2) +
1262 5 * NFSX_UNSIGNED;
1263 }
1264 m = nfsm_rpchead(cred, nmp->nm_flag, procnum, auth_type, auth_len,
1265 auth_str, verf_len, verf_str, mrest, mrest_len, &mheadend, &xid);
fa4905b1 1266 if (xidp)
9bccf70c 1267 *xidp = ntohl(xid) + ((u_int64_t)nfs_xidwrap << 32);
1c79356b
A
1268 if (auth_str)
1269 _FREE(auth_str, M_TEMP);
1270
1271 /*
1272 * For stream protocols, insert a Sun RPC Record Mark.
1273 */
1274 if (nmp->nm_sotype == SOCK_STREAM) {
1275 M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
1276 *mtod(m, u_long *) = htonl(0x80000000 |
1277 (m->m_pkthdr.len - NFSX_UNSIGNED));
1278 }
1279 rep->r_mreq = m;
1280 rep->r_xid = xid;
1281tryagain:
1282 if (nmp->nm_flag & NFSMNT_SOFT)
1283 rep->r_retry = nmp->nm_retry;
1284 else
1285 rep->r_retry = NFS_MAXREXMIT + 1; /* past clip limit */
1286 rep->r_rtt = rep->r_rexmit = 0;
1287 if (proct[procnum] > 0)
1288 rep->r_flags = R_TIMING;
1289 else
1290 rep->r_flags = 0;
1291 rep->r_mrep = NULL;
1292
1293 /*
1294 * Do the client side RPC.
1295 */
1296 nfsstats.rpcrequests++;
1297 /*
1298 * Chain request into list of outstanding requests. Be sure
1299 * to put it LAST so timer finds oldest requests first.
1300 */
1301 s = splsoftclock();
1302 TAILQ_INSERT_TAIL(&nfs_reqq, rep, r_chain);
1303
1304 /* Get send time for nqnfs */
1305 reqtime = time.tv_sec;
1306
1307 /*
1308 * If backing off another request or avoiding congestion, don't
1309 * send this one now but let timer do it. If not timing a request,
1310 * do it now.
1311 */
1312 if (nmp->nm_so && (nmp->nm_sotype != SOCK_DGRAM ||
1313 (nmp->nm_flag & NFSMNT_DUMBTIMR) ||
1314 nmp->nm_sent < nmp->nm_cwnd)) {
1315 splx(s);
1316 if (nmp->nm_soflags & PR_CONNREQUIRED)
1317 error = nfs_sndlock(&nmp->nm_flag, rep);
1318
1319 /*
1320 * Set the R_SENT before doing the send in case another thread
1321 * processes the reply before the nfs_send returns here
1322 */
1323 if (!error) {
1324 if ((rep->r_flags & R_MUSTRESEND) == 0) {
fa4905b1
A
1325 FSDBG(531, rep->r_xid, rep, nmp->nm_sent,
1326 nmp->nm_cwnd);
1c79356b
A
1327 nmp->nm_sent += NFS_CWNDSCALE;
1328 rep->r_flags |= R_SENT;
1329 }
1330
1331 m = m_copym(m, 0, M_COPYALL, M_WAIT);
1332 error = nfs_send(nmp->nm_so, nmp->nm_nam, m, rep);
1333 if (nmp->nm_soflags & PR_CONNREQUIRED)
1334 nfs_sndunlock(&nmp->nm_flag);
1335 }
1336 if (error) {
1337 nmp->nm_sent -= NFS_CWNDSCALE;
1338 rep->r_flags &= ~R_SENT;
1339 }
1340 } else {
1341 splx(s);
1342 rep->r_rtt = -1;
1343 }
1344
1345 /*
1346 * Wait for the reply from our send or the timer's.
1347 */
1348 if (!error || error == EPIPE)
1349 error = nfs_reply(rep);
1350
1351 /*
1352 * RPC done, unlink the request.
1353 */
1354 s = splsoftclock();
1355 for (rp = nfs_reqq.tqh_first; rp;
1356 rp = rp->r_chain.tqe_next)
1357 if (rp == rep && rp->r_xid == xid)
1358 break;
1359 if (!rp)
1360 panic("nfs_request race, rep %x xid %x", rep, xid);
1361 TAILQ_REMOVE(&nfs_reqq, rep, r_chain);
1362 splx(s);
1363
1364 /*
1365 * Decrement the outstanding request count.
1366 */
1367 if (rep->r_flags & R_SENT) {
fa4905b1 1368 FSDBG(531, rep->r_xid, rep, nmp->nm_sent, nmp->nm_cwnd);
1c79356b
A
1369 rep->r_flags &= ~R_SENT; /* paranoia */
1370 nmp->nm_sent -= NFS_CWNDSCALE;
1371 }
1372
1373 /*
1374 * If there was a successful reply and a tprintf msg.
1375 * tprintf a response.
1376 */
1377 if (!error && (rep->r_flags & R_TPRINTFMSG))
1378 nfs_msg(rep->r_procp, nmp->nm_mountp->mnt_stat.f_mntfromname,
1379 "is alive again");
1380 mrep = rep->r_mrep;
1381 md = rep->r_md;
1382 dpos = rep->r_dpos;
1383 if (error) {
1384 m_freem(rep->r_mreq);
fa4905b1 1385 FSDBG_BOT(531, error, rep->r_xid, nmp, rep);
1c79356b
A
1386 _FREE_ZONE((caddr_t)rep, sizeof (struct nfsreq), M_NFSREQ);
1387 return (error);
1388 }
1389
1390 /*
1391 * break down the rpc header and check if ok
1392 */
1393 nfsm_dissect(tl, u_long *, 3 * NFSX_UNSIGNED);
1394 if (*tl++ == rpc_msgdenied) {
1395 if (*tl == rpc_mismatch)
1396 error = EOPNOTSUPP;
1397 else if ((nmp->nm_flag & NFSMNT_KERB) && *tl++ == rpc_autherr) {
1398 if (!failed_auth) {
1399 failed_auth++;
1400 mheadend->m_next = (struct mbuf *)0;
1401 m_freem(mrep);
1402 m_freem(rep->r_mreq);
1403 goto kerbauth;
1404 } else
1405 error = EAUTH;
1406 } else
1407 error = EACCES;
1408 m_freem(mrep);
1409 m_freem(rep->r_mreq);
fa4905b1 1410 FSDBG_BOT(531, error, rep->r_xid, nmp, rep);
1c79356b
A
1411 _FREE_ZONE((caddr_t)rep, sizeof (struct nfsreq), M_NFSREQ);
1412 return (error);
1413 }
1414
1415 /*
1416 * Grab any Kerberos verifier, otherwise just throw it away.
1417 */
1418 verf_type = fxdr_unsigned(int, *tl++);
1419 i = fxdr_unsigned(int, *tl);
1420 if ((nmp->nm_flag & NFSMNT_KERB) && verf_type == RPCAUTH_KERB4) {
1421 error = nfs_savenickauth(nmp, cred, i, key, &md, &dpos, mrep);
1422 if (error)
1423 goto nfsmout;
1424 } else if (i > 0)
1425 nfsm_adv(nfsm_rndup(i));
1426 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
1427 /* 0 == ok */
1428 if (*tl == 0) {
1429 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
1430 if (*tl != 0) {
1431 error = fxdr_unsigned(int, *tl);
1432 if ((nmp->nm_flag & NFSMNT_NFSV3) &&
1433 error == NFSERR_TRYLATER) {
1434 m_freem(mrep);
1435 error = 0;
1436 waituntil = time.tv_sec + trylater_delay;
1437 NFS_DPF(DUP,
1438 ("nfs_request %s flag=%x trylater_cnt=%x waituntil=%lx trylater_delay=%x\n",
1439 nmp->nm_mountp->mnt_stat.f_mntfromname,
1440 nmp->nm_flag, trylater_cnt, waituntil,
1441 trylater_delay));
1442 while (time.tv_sec < waituntil)
1443 (void)tsleep((caddr_t)&lbolt,
1444 PSOCK, "nqnfstry", 0);
1445 trylater_delay *= nfs_backoff[trylater_cnt];
1446 if (trylater_cnt < 7)
1447 trylater_cnt++;
1448 goto tryagain;
1449 }
1450
1451 /*
1452 * If the File Handle was stale, invalidate the
1453 * lookup cache, just in case.
1454 */
1455 if (error == ESTALE)
1456 cache_purge(vp);
1457 if (nmp->nm_flag & NFSMNT_NFSV3) {
1458 *mrp = mrep;
1459 *mdp = md;
1460 *dposp = dpos;
1461 error |= NFSERR_RETERR;
1462 } else
1463 m_freem(mrep);
1464 m_freem(rep->r_mreq);
fa4905b1 1465 FSDBG_BOT(531, error, rep->r_xid, nmp, rep);
1c79356b
A
1466 _FREE_ZONE((caddr_t)rep,
1467 sizeof (struct nfsreq), M_NFSREQ);
1468 return (error);
1469 }
1470
1471 /*
1472 * For nqnfs, get any lease in reply
1473 */
1474 if (nmp->nm_flag & NFSMNT_NQNFS) {
1475 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
1476 if (*tl) {
1477 np = VTONFS(vp);
1478 nqlflag = fxdr_unsigned(int, *tl);
1479 nfsm_dissect(tl, u_long *, 4*NFSX_UNSIGNED);
1480 cachable = fxdr_unsigned(int, *tl++);
1481 reqtime += fxdr_unsigned(int, *tl++);
1482 if (reqtime > time.tv_sec) {
1483 fxdr_hyper(tl, &frev);
1484 nqnfs_clientlease(nmp, np, nqlflag,
1485 cachable, reqtime, frev);
1486 }
1487 }
1488 }
1489 *mrp = mrep;
1490 *mdp = md;
1491 *dposp = dpos;
1492 m_freem(rep->r_mreq);
fa4905b1 1493 FSDBG_BOT(531, 0xf0f0f0f0, rep->r_xid, nmp, rep);
1c79356b
A
1494 FREE_ZONE((caddr_t)rep, sizeof (struct nfsreq), M_NFSREQ);
1495 return (0);
1496 }
1497 m_freem(mrep);
1498 error = EPROTONOSUPPORT;
1499nfsmout:
1500 m_freem(rep->r_mreq);
fa4905b1 1501 FSDBG_BOT(531, error, rep->r_xid, nmp, rep);
1c79356b
A
1502 _FREE_ZONE((caddr_t)rep, sizeof (struct nfsreq), M_NFSREQ);
1503 return (error);
1504}
1505
1506#ifndef NFS_NOSERVER
1507/*
1508 * Generate the rpc reply header
1509 * siz arg. is used to decide if adding a cluster is worthwhile
1510 */
1511int
1512nfs_rephead(siz, nd, slp, err, cache, frev, mrq, mbp, bposp)
1513 int siz;
1514 struct nfsrv_descript *nd;
1515 struct nfssvc_sock *slp;
1516 int err;
1517 int cache;
1518 u_quad_t *frev;
1519 struct mbuf **mrq;
1520 struct mbuf **mbp;
1521 caddr_t *bposp;
1522{
1523 register u_long *tl;
1524 register struct mbuf *mreq;
1525 caddr_t bpos;
1526 struct mbuf *mb, *mb2;
1527
1528 MGETHDR(mreq, M_WAIT, MT_DATA);
1529 mb = mreq;
1530 /*
1531 * If this is a big reply, use a cluster else
1532 * try and leave leading space for the lower level headers.
1533 */
1534 siz += RPC_REPLYSIZ;
1535 if (siz >= MINCLSIZE) {
1536 MCLGET(mreq, M_WAIT);
1537 } else
1538 mreq->m_data += max_hdr;
1539 tl = mtod(mreq, u_long *);
1540 mreq->m_len = 6 * NFSX_UNSIGNED;
1541 bpos = ((caddr_t)tl) + mreq->m_len;
1542 *tl++ = txdr_unsigned(nd->nd_retxid);
1543 *tl++ = rpc_reply;
1544 if (err == ERPCMISMATCH || (err & NFSERR_AUTHERR)) {
1545 *tl++ = rpc_msgdenied;
1546 if (err & NFSERR_AUTHERR) {
1547 *tl++ = rpc_autherr;
1548 *tl = txdr_unsigned(err & ~NFSERR_AUTHERR);
1549 mreq->m_len -= NFSX_UNSIGNED;
1550 bpos -= NFSX_UNSIGNED;
1551 } else {
1552 *tl++ = rpc_mismatch;
1553 *tl++ = txdr_unsigned(RPC_VER2);
1554 *tl = txdr_unsigned(RPC_VER2);
1555 }
1556 } else {
1557 *tl++ = rpc_msgaccepted;
1558
1559 /*
1560 * For Kerberos authentication, we must send the nickname
1561 * verifier back, otherwise just RPCAUTH_NULL.
1562 */
1563 if (nd->nd_flag & ND_KERBFULL) {
1564 register struct nfsuid *nuidp;
1565 struct timeval ktvin, ktvout;
1566
1567 for (nuidp = NUIDHASH(slp, nd->nd_cr.cr_uid)->lh_first;
1568 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
1569 if (nuidp->nu_cr.cr_uid == nd->nd_cr.cr_uid &&
1570 (!nd->nd_nam2 || netaddr_match(NU_NETFAM(nuidp),
1571 &nuidp->nu_haddr, nd->nd_nam2)))
1572 break;
1573 }
1574 if (nuidp) {
1575 ktvin.tv_sec =
1576 txdr_unsigned(nuidp->nu_timestamp.tv_sec - 1);
1577 ktvin.tv_usec =
1578 txdr_unsigned(nuidp->nu_timestamp.tv_usec);
1579
1580 /*
1581 * Encrypt the timestamp in ecb mode using the
1582 * session key.
1583 */
1584#if NFSKERB
1585 XXX
1586#endif
1587
1588 *tl++ = rpc_auth_kerb;
1589 *tl++ = txdr_unsigned(3 * NFSX_UNSIGNED);
1590 *tl = ktvout.tv_sec;
1591 nfsm_build(tl, u_long *, 3 * NFSX_UNSIGNED);
1592 *tl++ = ktvout.tv_usec;
1593 *tl++ = txdr_unsigned(nuidp->nu_cr.cr_uid);
1594 } else {
1595 *tl++ = 0;
1596 *tl++ = 0;
1597 }
1598 } else {
1599 *tl++ = 0;
1600 *tl++ = 0;
1601 }
1602 switch (err) {
1603 case EPROGUNAVAIL:
1604 *tl = txdr_unsigned(RPC_PROGUNAVAIL);
1605 break;
1606 case EPROGMISMATCH:
1607 *tl = txdr_unsigned(RPC_PROGMISMATCH);
1608 nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED);
1609 if (nd->nd_flag & ND_NQNFS) {
1610 *tl++ = txdr_unsigned(3);
1611 *tl = txdr_unsigned(3);
1612 } else {
1613 *tl++ = txdr_unsigned(2);
1614 *tl = txdr_unsigned(3);
1615 }
1616 break;
1617 case EPROCUNAVAIL:
1618 *tl = txdr_unsigned(RPC_PROCUNAVAIL);
1619 break;
1620 case EBADRPC:
1621 *tl = txdr_unsigned(RPC_GARBAGE);
1622 break;
1623 default:
1624 *tl = 0;
1625 if (err != NFSERR_RETVOID) {
1626 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1627 if (err)
1628 *tl = txdr_unsigned(nfsrv_errmap(nd, err));
1629 else
1630 *tl = 0;
1631 }
1632 break;
1633 };
1634 }
1635
1636 /*
1637 * For nqnfs, piggyback lease as requested.
1638 */
1639 if ((nd->nd_flag & ND_NQNFS) && err == 0) {
1640 if (nd->nd_flag & ND_LEASE) {
1641 nfsm_build(tl, u_long *, 5 * NFSX_UNSIGNED);
1642 *tl++ = txdr_unsigned(nd->nd_flag & ND_LEASE);
1643 *tl++ = txdr_unsigned(cache);
1644 *tl++ = txdr_unsigned(nd->nd_duration);
1645 txdr_hyper(frev, tl);
1646 } else {
1647 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1648 *tl = 0;
1649 }
1650 }
1651 if (mrq != NULL)
1652 *mrq = mreq;
1653 *mbp = mb;
1654 *bposp = bpos;
1655 if (err != 0 && err != NFSERR_RETVOID)
1656 nfsstats.srvrpc_errs++;
1657 return (0);
1658}
1659
1660
1661#endif /* NFS_NOSERVER */
1662
1663
1664/*
1665 * From FreeBSD 1.58, a Matt Dillon fix...
1666 * Flag a request as being about to terminate.
1667 * The nm_sent count is decremented now to avoid deadlocks when the process
1668 * in soreceive() hasn't yet managed to send its own request.
1669 */
1670static void
1671nfs_softterm(struct nfsreq *rep)
1672{
1673 rep->r_flags |= R_SOFTTERM;
1674 if (rep->r_flags & R_SENT) {
fa4905b1
A
1675 FSDBG(532, rep->r_xid, rep, rep->r_nmp->nm_sent,
1676 rep->r_nmp->nm_cwnd);
1c79356b
A
1677 rep->r_nmp->nm_sent -= NFS_CWNDSCALE;
1678 rep->r_flags &= ~R_SENT;
1679 }
1680}
1681
1682void
1683nfs_timer_funnel(arg)
1684 void * arg;
1685{
1686 (void) thread_funnel_set(kernel_flock, TRUE);
1687 nfs_timer(arg);
1688 (void) thread_funnel_set(kernel_flock, FALSE);
1689
1690}
1691
1692/*
1693 * Nfs timer routine
1694 * Scan the nfsreq list and retranmit any requests that have timed out
1695 * To avoid retransmission attempts on STREAM sockets (in the future) make
1696 * sure to set the r_retry field to 0 (implies nm_retry == 0).
1697 */
1698void
1699nfs_timer(arg)
1700 void *arg; /* never used */
1701{
1702 register struct nfsreq *rep, *rp;
1703 register struct mbuf *m;
1704 register struct socket *so;
1705 register struct nfsmount *nmp;
1706 register int timeo;
1707 int s, error;
1708#ifndef NFS_NOSERVER
1709 static long lasttime = 0;
1710 register struct nfssvc_sock *slp;
1711 u_quad_t cur_usec;
1712#endif /* NFS_NOSERVER */
1713#if NFSDIAG
1714 int rttdiag;
1715#endif
1716 int flags, rexmit, cwnd, sent;
1717 u_long xid;
1718
1719 s = splnet();
1720 /*
1721 * XXX If preemptable threads are implemented the spls used for the
1722 * outstanding request queue must be replaced with mutexes.
1723 */
1724rescan:
1725#ifdef NFSTRACESUSPENDERS
1726 if (NFSTRACE_SUSPENDING) {
1727 for (rep = nfs_reqq.tqh_first; rep != 0;
1728 rep = rep->r_chain.tqe_next)
1729 if (rep->r_xid == nfstracexid)
1730 break;
1731 if (!rep) {
1732 NFSTRACE_RESUME;
1733 } else if (NFSTRACE_SUSPENSEOVER) {
1734 NFSTRACE_SUSPEND;
1735 }
1736 }
1737#endif
1738 for (rep = nfs_reqq.tqh_first; rep != 0; rep = rep->r_chain.tqe_next) {
1739#ifdef NFSTRACESUSPENDERS
1740 if (rep->r_mrep && !NFSTRACE_SUSPENDING) {
1741 nfstracexid = rep->r_xid;
1742 NFSTRACE_STARTSUSPENDCOUNTDOWN;
1743 }
1744#endif
1745 nmp = rep->r_nmp;
1746 if (!nmp) /* unmounted */
1747 continue;
1748 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM))
1749 continue;
1750 if (nfs_sigintr(nmp, rep, rep->r_procp)) {
1751 nfs_softterm(rep);
1752 continue;
1753 }
1754 if (rep->r_rtt >= 0) {
1755 rep->r_rtt++;
1756 if (nmp->nm_flag & NFSMNT_DUMBTIMR)
1757 timeo = nmp->nm_timeo;
1758 else
1759 timeo = NFS_RTO(nmp, proct[rep->r_procnum]);
1760 /* ensure 62.5 ms floor */
1761 while (16 * timeo < hz)
1762 timeo *= 2;
1763 if (nmp->nm_timeouts > 0)
1764 timeo *= nfs_backoff[nmp->nm_timeouts - 1];
1765 if (rep->r_rtt <= timeo)
1766 continue;
1767 if (nmp->nm_timeouts < 8)
1768 nmp->nm_timeouts++;
1769 }
1770 /*
1771 * Check for server not responding
1772 */
1773 if ((rep->r_flags & R_TPRINTFMSG) == 0 &&
1774 rep->r_rexmit > nmp->nm_deadthresh) {
1775 nfs_msg(rep->r_procp,
1776 nmp->nm_mountp->mnt_stat.f_mntfromname,
1777 "not responding");
1778 rep->r_flags |= R_TPRINTFMSG;
1779 }
1780 if (rep->r_rexmit >= rep->r_retry) { /* too many */
1781 nfsstats.rpctimeouts++;
1782 nfs_softterm(rep);
1783 continue;
1784 }
1785 if (nmp->nm_sotype != SOCK_DGRAM) {
1786 if (++rep->r_rexmit > NFS_MAXREXMIT)
1787 rep->r_rexmit = NFS_MAXREXMIT;
1788 continue;
1789 }
1790 if ((so = nmp->nm_so) == NULL)
1791 continue;
1792
1793 /*
1794 * If there is enough space and the window allows..
1795 * Resend it
1796 * Set r_rtt to -1 in case we fail to send it now.
1797 */
1798#if NFSDIAG
1799 rttdiag = rep->r_rtt;
1800#endif
1801 rep->r_rtt = -1;
1802 if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len &&
1803 ((nmp->nm_flag & NFSMNT_DUMBTIMR) ||
1804 (rep->r_flags & R_SENT) ||
1805 nmp->nm_sent < nmp->nm_cwnd) &&
1806 (m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))){
1807
1808 struct proc *p = current_proc();
1809
1810#if NFSDIAG
1811 if (rep->r_flags & R_SENT && nfsprnttimo &&
1812 nmp->nm_timeouts >= nfsprnttimo) {
1813 int t = proct[rep->r_procnum];
1814 if (t)
1815 NFS_DPF(DUP, ("nfs_timer %s nmtm=%d tms=%d rtt=%d tm=%d p=%d A=%d D=%d\n", nmp->nm_mountp->mnt_stat.f_mntfromname, nmp->nm_timeo, nmp->nm_timeouts, rttdiag, timeo, rep->r_procnum, nmp->nm_srtt[t-1], nmp->nm_sdrtt[t-1]));
1816 else
1817 NFS_DPF(DUP, ("nfs_timer %s nmtm=%d tms=%d rtt=%d tm=%d p=%d\n", nmp->nm_mountp->mnt_stat.f_mntfromname, nmp->nm_timeo, nmp->nm_timeouts, rttdiag, timeo, rep->r_procnum));
1818 }
1819 nfsdup(rep);
1820#endif /* NFSDIAG */
1821 /*
1822 * Iff first send, start timing
1823 * else turn timing off, backoff timer
1824 * and divide congestion window by 2.
1825 * We update these *before* the send to avoid
1826 * racing against receiving the reply.
1827 * We save them so we can restore them on send error.
1828 */
1829 flags = rep->r_flags;
1830 rexmit = rep->r_rexmit;
1831 cwnd = nmp->nm_cwnd;
1832 sent = nmp->nm_sent;
1833 xid = rep->r_xid;
1834 if (rep->r_flags & R_SENT) {
1835 rep->r_flags &= ~R_TIMING;
1836 if (++rep->r_rexmit > NFS_MAXREXMIT)
1837 rep->r_rexmit = NFS_MAXREXMIT;
1838 nmp->nm_cwnd >>= 1;
1839 if (nmp->nm_cwnd < NFS_CWNDSCALE)
1840 nmp->nm_cwnd = NFS_CWNDSCALE;
1841 nfsstats.rpcretries++;
1842 } else {
1843 rep->r_flags |= R_SENT;
1844 nmp->nm_sent += NFS_CWNDSCALE;
1845 }
fa4905b1 1846 FSDBG(535, xid, rep, nmp->nm_sent, nmp->nm_cwnd);
1c79356b
A
1847
1848 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
1849
1850 if ((nmp->nm_flag & NFSMNT_NOCONN) == 0)
1851 error = (*so->so_proto->pr_usrreqs->pru_send)
1852 (so, 0, m, 0, 0, p);
1853 else
1854 error = (*so->so_proto->pr_usrreqs->pru_send)
1855 (so, 0, m, mtod(nmp->nm_nam, struct sockaddr *), 0, p);
1856
1857 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1858
fa4905b1 1859 FSDBG(535, xid, error, sent, cwnd);
1c79356b
A
1860 /*
1861 * This is to fix "nfs_sigintr" DSI panics.
1862 * We may have slept during the send so the current
1863 * place in the request queue may have been released.
1864 * Due to zone_gc it may even be part of an
1865 * unrelated newly allocated data structure.
1866 * Restart the list scan from the top if needed...
1867 */
1868 for (rp = nfs_reqq.tqh_first; rp;
1869 rp = rp->r_chain.tqe_next)
1870 if (rp == rep && rp->r_xid == xid)
1871 break;
1872 if (!rp) {
1873 if (!error)
1874 goto rescan;
1875 panic("nfs_timer: race error %d xid 0x%x\n",
1876 error, xid);
1877 }
1878
1879 if (error) {
1880 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error))
1881 so->so_error = 0;
1882 rep->r_flags = flags;
1883 rep->r_rexmit = rexmit;
1884 nmp->nm_cwnd = cwnd;
1885 nmp->nm_sent = sent;
1886 if (flags & R_SENT)
1887 nfsstats.rpcretries--;
1888 } else
1889 rep->r_rtt = 0;
1890 }
1891 }
1892#ifndef NFS_NOSERVER
1893 /*
1894 * Call the nqnfs server timer once a second to handle leases.
1895 */
1896 if (lasttime != time.tv_sec) {
1897 lasttime = time.tv_sec;
1898 nqnfs_serverd();
1899 }
1900
1901 /*
1902 * Scan the write gathering queues for writes that need to be
1903 * completed now.
1904 */
1905 cur_usec = (u_quad_t)time.tv_sec * 1000000 + (u_quad_t)time.tv_usec;
1906 for (slp = nfssvc_sockhead.tqh_first; slp != 0;
1907 slp = slp->ns_chain.tqe_next) {
1908 if (slp->ns_tq.lh_first && slp->ns_tq.lh_first->nd_time<=cur_usec)
1909 nfsrv_wakenfsd(slp);
1910 }
1911#endif /* NFS_NOSERVER */
1912 splx(s);
1913 timeout(nfs_timer_funnel, (void *)0, nfs_ticks);
1914
1915}
1916
1917
1918/*
1919 * Test for a termination condition pending on the process.
1920 * This is used for NFSMNT_INT mounts.
1921 */
1922int
1923nfs_sigintr(nmp, rep, p)
1924 struct nfsmount *nmp;
1925 struct nfsreq *rep;
1926 register struct proc *p;
1927{
1928
9bccf70c
A
1929 struct uthread *ut;
1930
1931 ut = (struct uthread *)get_bsdthread_info(current_act());
1932
1c79356b
A
1933 if (rep && (rep->r_flags & R_SOFTTERM))
1934 return (EINTR);
1935 if (!(nmp->nm_flag & NFSMNT_INT))
1936 return (0);
9bccf70c
A
1937 if (p && ut && ut->uu_siglist &&
1938 (((ut->uu_siglist & ~ut->uu_sigmask) & ~p->p_sigignore) &
1c79356b
A
1939 NFSINT_SIGMASK))
1940 return (EINTR);
1941 return (0);
1942}
1943
1944/*
1945 * Lock a socket against others.
1946 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
1947 * and also to avoid race conditions between the processes with nfs requests
1948 * in progress when a reconnect is necessary.
1949 */
1950int
1951nfs_sndlock(flagp, rep)
1952 register int *flagp;
1953 struct nfsreq *rep;
1954{
1955 struct proc *p;
1956 int slpflag = 0, slptimeo = 0;
1957
1958 if (rep) {
1959 p = rep->r_procp;
1960 if (rep->r_nmp->nm_flag & NFSMNT_INT)
1961 slpflag = PCATCH;
1962 } else
1963 p = (struct proc *)0;
1964 while (*flagp & NFSMNT_SNDLOCK) {
1965 if (nfs_sigintr(rep->r_nmp, rep, p))
1966 return (EINTR);
1967 *flagp |= NFSMNT_WANTSND;
1968 (void) tsleep((caddr_t)flagp, slpflag | (PZERO - 1), "nfsndlck",
1969 slptimeo);
1970 if (slpflag == PCATCH) {
1971 slpflag = 0;
1972 slptimeo = 2 * hz;
1973 }
0b4e3aa0
A
1974 /*
1975 * Make sure while we slept that the mountpoint didn't go away.
1976 * nfs_sigintr and callers expect it in tact.
1977 */
1978 if (!rep->r_nmp)
1979 return (ECONNABORTED); /* don't have lock until out of loop */
1c79356b
A
1980 }
1981 *flagp |= NFSMNT_SNDLOCK;
1982 return (0);
1983}
1984
1985/*
1986 * Unlock the stream socket for others.
1987 */
1988void
1989nfs_sndunlock(flagp)
1990 register int *flagp;
1991{
1992
1993 if ((*flagp & NFSMNT_SNDLOCK) == 0)
1994 panic("nfs sndunlock");
1995 *flagp &= ~NFSMNT_SNDLOCK;
1996 if (*flagp & NFSMNT_WANTSND) {
1997 *flagp &= ~NFSMNT_WANTSND;
1998 wakeup((caddr_t)flagp);
1999 }
2000}
2001
2002static int
2003nfs_rcvlock(rep)
2004 register struct nfsreq *rep;
2005{
d7e50217 2006 register int *flagp;
1c79356b
A
2007 int slpflag, slptimeo = 0;
2008
d7e50217
A
2009 /* make sure we still have our mountpoint */
2010 if (!rep->r_nmp) {
2011 if (rep->r_mrep != NULL)
2012 return (EALREADY);
2013 return (ECONNABORTED);
2014 }
2015
2016 flagp = &rep->r_nmp->nm_flag;
fa4905b1 2017 FSDBG_TOP(534, rep->r_xid, rep, rep->r_nmp, *flagp);
1c79356b
A
2018 if (*flagp & NFSMNT_INT)
2019 slpflag = PCATCH;
2020 else
2021 slpflag = 0;
2022 while (*flagp & NFSMNT_RCVLOCK) {
2023 if (nfs_sigintr(rep->r_nmp, rep, rep->r_procp)) {
fa4905b1 2024 FSDBG_BOT(534, rep->r_xid, rep, rep->r_nmp, 0x100);
1c79356b
A
2025 return (EINTR);
2026 } else if (rep->r_mrep != NULL) {
2027 /*
2028 * Don't bother sleeping if reply already arrived
2029 */
fa4905b1 2030 FSDBG_BOT(534, rep->r_xid, rep, rep->r_nmp, 0x101);
1c79356b
A
2031 return (EALREADY);
2032 }
fa4905b1 2033 FSDBG(534, rep->r_xid, rep, rep->r_nmp, 0x102);
1c79356b
A
2034 *flagp |= NFSMNT_WANTRCV;
2035 (void) tsleep((caddr_t)flagp, slpflag | (PZERO - 1), "nfsrcvlk",
2036 slptimeo);
2037 if (slpflag == PCATCH) {
2038 slpflag = 0;
2039 slptimeo = 2 * hz;
2040 }
0b4e3aa0
A
2041 /*
2042 * Make sure while we slept that the mountpoint didn't go away.
fa4905b1 2043 * nfs_sigintr and caller nfs_reply expect it intact.
0b4e3aa0 2044 */
fa4905b1
A
2045 if (!rep->r_nmp) {
2046 FSDBG_BOT(534, rep->r_xid, rep, rep->r_nmp, 0x103);
0b4e3aa0 2047 return (ECONNABORTED); /* don't have lock until out of loop */
fa4905b1 2048 }
1c79356b
A
2049 }
2050 /*
2051 * nfs_reply will handle it if reply already arrived.
2052 * (We may have slept or been preempted while on network funnel).
2053 */
fa4905b1 2054 FSDBG_BOT(534, rep->r_xid, rep, rep->r_nmp, *flagp);
1c79356b
A
2055 *flagp |= NFSMNT_RCVLOCK;
2056 return (0);
2057}
2058
2059/*
2060 * Unlock the stream socket for others.
2061 */
2062static void
2063nfs_rcvunlock(flagp)
2064 register int *flagp;
2065{
2066
fa4905b1 2067 FSDBG(533, flagp, *flagp, 0, 0);
1c79356b
A
2068 if ((*flagp & NFSMNT_RCVLOCK) == 0)
2069 panic("nfs rcvunlock");
2070 *flagp &= ~NFSMNT_RCVLOCK;
2071 if (*flagp & NFSMNT_WANTRCV) {
1c79356b
A
2072 *flagp &= ~NFSMNT_WANTRCV;
2073 wakeup((caddr_t)flagp);
1c79356b
A
2074 }
2075}
2076
2077
2078#ifndef NFS_NOSERVER
2079/*
2080 * Socket upcall routine for the nfsd sockets.
2081 * The caddr_t arg is a pointer to the "struct nfssvc_sock".
2082 * Essentially do as much as possible non-blocking, else punt and it will
2083 * be called with M_WAIT from an nfsd.
2084 */
2085 /*
2086 * Needs to eun under network funnel
2087 */
2088void
2089nfsrv_rcv(so, arg, waitflag)
2090 struct socket *so;
2091 caddr_t arg;
2092 int waitflag;
2093{
2094 register struct nfssvc_sock *slp = (struct nfssvc_sock *)arg;
2095 register struct mbuf *m;
2096 struct mbuf *mp, *mhck;
2097 struct sockaddr *nam=0;
2098 struct uio auio;
2099 int flags, error;
2100 struct sockaddr_in *sin;
2101
2102 if ((slp->ns_flag & SLP_VALID) == 0)
2103 return;
2104#ifdef notdef
2105 /*
2106 * Define this to test for nfsds handling this under heavy load.
2107 */
2108 if (waitflag == M_DONTWAIT) {
2109 slp->ns_flag |= SLP_NEEDQ; goto dorecs;
2110 }
2111#endif
2112 auio.uio_procp = NULL;
2113 if (so->so_type == SOCK_STREAM) {
2114 /*
2115 * If there are already records on the queue, defer soreceive()
2116 * to an nfsd so that there is feedback to the TCP layer that
2117 * the nfs servers are heavily loaded.
2118 */
2119 if (slp->ns_rec && waitflag == M_DONTWAIT) {
2120 slp->ns_flag |= SLP_NEEDQ;
2121 goto dorecs;
2122 }
2123
2124 /*
2125 * Do soreceive().
2126 */
2127 auio.uio_resid = 1000000000;
2128 flags = MSG_DONTWAIT;
2129 error = soreceive(so, (struct sockaddr **) 0, &auio, &mp, (struct mbuf **)0, &flags);
2130 if (error || mp == (struct mbuf *)0) {
2131 if (error == EWOULDBLOCK)
2132 slp->ns_flag |= SLP_NEEDQ;
2133 else
2134 slp->ns_flag |= SLP_DISCONN;
2135 goto dorecs;
2136 }
2137 m = mp;
2138 if (slp->ns_rawend) {
2139 slp->ns_rawend->m_next = m;
2140 slp->ns_cc += 1000000000 - auio.uio_resid;
2141 } else {
2142 slp->ns_raw = m;
2143 slp->ns_cc = 1000000000 - auio.uio_resid;
2144 }
2145 while (m->m_next)
2146 m = m->m_next;
2147 slp->ns_rawend = m;
2148
2149 /*
2150 * Now try and parse record(s) out of the raw stream data.
2151 */
2152 error = nfsrv_getstream(slp, waitflag);
2153 if (error) {
2154 if (error == EPERM)
2155 slp->ns_flag |= SLP_DISCONN;
2156 else
2157 slp->ns_flag |= SLP_NEEDQ;
2158 }
2159 } else {
2160 do {
2161 auio.uio_resid = 1000000000;
2162 flags = MSG_DONTWAIT;
2163 nam = 0;
2164 error = soreceive(so, &nam, &auio, &mp,
2165 (struct mbuf **)0, &flags);
2166
2167 if (mp) {
2168 if (nam) {
2169 MGET(mhck, M_WAIT, MT_SONAME);
2170 mhck->m_len = nam->sa_len;
2171 sin = mtod(mhck, struct sockaddr_in *);
2172 bcopy(nam, sin, sizeof(struct sockaddr_in));
2173 mhck->m_hdr.mh_len = sizeof(struct sockaddr_in);
2174 FREE(nam, M_SONAME);
2175
2176 m = mhck;
2177 m->m_next = mp;
2178 } else
2179 m = mp;
2180 if (slp->ns_recend)
2181 slp->ns_recend->m_nextpkt = m;
2182 else
2183 slp->ns_rec = m;
2184 slp->ns_recend = m;
2185 m->m_nextpkt = (struct mbuf *)0;
2186 }
2187 if (error) {
2188 if ((so->so_proto->pr_flags & PR_CONNREQUIRED)
2189 && error != EWOULDBLOCK) {
2190 slp->ns_flag |= SLP_DISCONN;
2191 goto dorecs;
2192 }
2193 }
2194 } while (mp);
2195 }
2196
2197 /*
2198 * Now try and process the request records, non-blocking.
2199 */
2200dorecs:
2201 if (waitflag == M_DONTWAIT &&
2202 (slp->ns_rec || (slp->ns_flag & (SLP_NEEDQ | SLP_DISCONN)))) {
2203 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
2204 nfsrv_wakenfsd(slp);
2205 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
2206 }
2207}
2208
2209/*
2210 * Try and extract an RPC request from the mbuf data list received on a
2211 * stream socket. The "waitflag" argument indicates whether or not it
2212 * can sleep.
2213 */
2214static int
2215nfsrv_getstream(slp, waitflag)
2216 register struct nfssvc_sock *slp;
2217 int waitflag;
2218{
2219 register struct mbuf *m, **mpp;
2220 register char *cp1, *cp2;
2221 register int len;
b4c24cb9 2222 struct mbuf *om, *m2, *recm;
1c79356b
A
2223 u_long recmark;
2224
2225 if (slp->ns_flag & SLP_GETSTREAM)
2226 panic("nfs getstream");
2227 slp->ns_flag |= SLP_GETSTREAM;
2228 for (;;) {
2229 if (slp->ns_reclen == 0) {
2230 if (slp->ns_cc < NFSX_UNSIGNED) {
2231 slp->ns_flag &= ~SLP_GETSTREAM;
2232 return (0);
2233 }
2234 m = slp->ns_raw;
2235 if (m->m_len >= NFSX_UNSIGNED) {
2236 bcopy(mtod(m, caddr_t), (caddr_t)&recmark, NFSX_UNSIGNED);
2237 m->m_data += NFSX_UNSIGNED;
2238 m->m_len -= NFSX_UNSIGNED;
2239 } else {
2240 cp1 = (caddr_t)&recmark;
2241 cp2 = mtod(m, caddr_t);
2242 while (cp1 < ((caddr_t)&recmark) + NFSX_UNSIGNED) {
2243 while (m->m_len == 0) {
2244 m = m->m_next;
2245 cp2 = mtod(m, caddr_t);
2246 }
2247 *cp1++ = *cp2++;
2248 m->m_data++;
2249 m->m_len--;
2250 }
2251 }
2252 slp->ns_cc -= NFSX_UNSIGNED;
2253 recmark = ntohl(recmark);
2254 slp->ns_reclen = recmark & ~0x80000000;
2255 if (recmark & 0x80000000)
2256 slp->ns_flag |= SLP_LASTFRAG;
2257 else
2258 slp->ns_flag &= ~SLP_LASTFRAG;
2259 if (slp->ns_reclen < NFS_MINPACKET || slp->ns_reclen > NFS_MAXPACKET) {
2260 slp->ns_flag &= ~SLP_GETSTREAM;
2261 return (EPERM);
2262 }
2263 }
2264
2265 /*
2266 * Now get the record part.
b4c24cb9
A
2267 *
2268 * Note that slp->ns_reclen may be 0. Linux sometimes
2269 * generates 0-length RPCs
1c79356b 2270 */
b4c24cb9 2271 recm = NULL;
1c79356b
A
2272 if (slp->ns_cc == slp->ns_reclen) {
2273 recm = slp->ns_raw;
2274 slp->ns_raw = slp->ns_rawend = (struct mbuf *)0;
2275 slp->ns_cc = slp->ns_reclen = 0;
2276 } else if (slp->ns_cc > slp->ns_reclen) {
2277 len = 0;
2278 m = slp->ns_raw;
2279 om = (struct mbuf *)0;
2280 while (len < slp->ns_reclen) {
2281 if ((len + m->m_len) > slp->ns_reclen) {
2282 m2 = m_copym(m, 0, slp->ns_reclen - len,
2283 waitflag);
2284 if (m2) {
2285 if (om) {
2286 om->m_next = m2;
2287 recm = slp->ns_raw;
2288 } else
2289 recm = m2;
2290 m->m_data += slp->ns_reclen - len;
2291 m->m_len -= slp->ns_reclen - len;
2292 len = slp->ns_reclen;
2293 } else {
2294 slp->ns_flag &= ~SLP_GETSTREAM;
2295 return (EWOULDBLOCK);
2296 }
2297 } else if ((len + m->m_len) == slp->ns_reclen) {
2298 om = m;
2299 len += m->m_len;
2300 m = m->m_next;
2301 recm = slp->ns_raw;
2302 om->m_next = (struct mbuf *)0;
2303 } else {
2304 om = m;
2305 len += m->m_len;
2306 m = m->m_next;
2307 }
2308 }
2309 slp->ns_raw = m;
2310 slp->ns_cc -= len;
2311 slp->ns_reclen = 0;
2312 } else {
2313 slp->ns_flag &= ~SLP_GETSTREAM;
2314 return (0);
2315 }
2316
2317 /*
2318 * Accumulate the fragments into a record.
2319 */
2320 mpp = &slp->ns_frag;
2321 while (*mpp)
2322 mpp = &((*mpp)->m_next);
2323 *mpp = recm;
2324 if (slp->ns_flag & SLP_LASTFRAG) {
2325 if (slp->ns_recend)
2326 slp->ns_recend->m_nextpkt = slp->ns_frag;
2327 else
2328 slp->ns_rec = slp->ns_frag;
2329 slp->ns_recend = slp->ns_frag;
2330 slp->ns_frag = (struct mbuf *)0;
2331 }
2332 }
2333}
2334
2335/*
2336 * Parse an RPC header.
2337 */
2338int
2339nfsrv_dorec(slp, nfsd, ndp)
2340 register struct nfssvc_sock *slp;
2341 struct nfsd *nfsd;
2342 struct nfsrv_descript **ndp;
2343{
2344 register struct mbuf *m;
2345 register struct mbuf *nam;
2346 register struct nfsrv_descript *nd;
2347 int error;
2348
2349 *ndp = NULL;
2350 if ((slp->ns_flag & SLP_VALID) == 0 ||
2351 (m = slp->ns_rec) == (struct mbuf *)0)
2352 return (ENOBUFS);
2353 slp->ns_rec = m->m_nextpkt;
2354 if (slp->ns_rec)
2355 m->m_nextpkt = (struct mbuf *)0;
2356 else
2357 slp->ns_recend = (struct mbuf *)0;
2358 if (m->m_type == MT_SONAME) {
2359 nam = m;
2360 m = m->m_next;
2361 nam->m_next = NULL;
2362 } else
2363 nam = NULL;
2364 MALLOC_ZONE(nd, struct nfsrv_descript *,
2365 sizeof (struct nfsrv_descript), M_NFSRVDESC, M_WAITOK);
2366 nd->nd_md = nd->nd_mrep = m;
2367 nd->nd_nam2 = nam;
2368 nd->nd_dpos = mtod(m, caddr_t);
2369 error = nfs_getreq(nd, nfsd, TRUE);
2370 if (error) {
2371 m_freem(nam);
2372 _FREE_ZONE((caddr_t)nd, sizeof *nd, M_NFSRVDESC);
2373 return (error);
2374 }
2375 *ndp = nd;
2376 nfsd->nfsd_nd = nd;
2377 return (0);
2378}
2379
2380/*
2381 * Parse an RPC request
2382 * - verify it
2383 * - fill in the cred struct.
2384 */
2385int
2386nfs_getreq(nd, nfsd, has_header)
2387 register struct nfsrv_descript *nd;
2388 struct nfsd *nfsd;
2389 int has_header;
2390{
2391 register int len, i;
2392 register u_long *tl;
2393 register long t1;
2394 struct uio uio;
2395 struct iovec iov;
2396 caddr_t dpos, cp2, cp;
2397 u_long nfsvers, auth_type;
2398 uid_t nickuid;
2399 int error = 0, nqnfs = 0, ticklen;
2400 struct mbuf *mrep, *md;
2401 register struct nfsuid *nuidp;
2402 struct timeval tvin, tvout;
2403#if 0 /* until encrypted keys are implemented */
2404 NFSKERBKEYSCHED_T keys; /* stores key schedule */
2405#endif
2406
2407 mrep = nd->nd_mrep;
2408 md = nd->nd_md;
2409 dpos = nd->nd_dpos;
2410 if (has_header) {
2411 nfsm_dissect(tl, u_long *, 10 * NFSX_UNSIGNED);
2412 nd->nd_retxid = fxdr_unsigned(u_long, *tl++);
2413 if (*tl++ != rpc_call) {
2414 m_freem(mrep);
2415 return (EBADRPC);
2416 }
2417 } else
2418 nfsm_dissect(tl, u_long *, 8 * NFSX_UNSIGNED);
2419 nd->nd_repstat = 0;
2420 nd->nd_flag = 0;
2421 if (*tl++ != rpc_vers) {
2422 nd->nd_repstat = ERPCMISMATCH;
2423 nd->nd_procnum = NFSPROC_NOOP;
2424 return (0);
2425 }
2426 if (*tl != nfs_prog) {
2427 if (*tl == nqnfs_prog)
2428 nqnfs++;
2429 else {
2430 nd->nd_repstat = EPROGUNAVAIL;
2431 nd->nd_procnum = NFSPROC_NOOP;
2432 return (0);
2433 }
2434 }
2435 tl++;
2436 nfsvers = fxdr_unsigned(u_long, *tl++);
2437 if (((nfsvers < NFS_VER2 || nfsvers > NFS_VER3) && !nqnfs) ||
2438 (nfsvers != NQNFS_VER3 && nqnfs)) {
2439 nd->nd_repstat = EPROGMISMATCH;
2440 nd->nd_procnum = NFSPROC_NOOP;
2441 return (0);
2442 }
2443 if (nqnfs)
2444 nd->nd_flag = (ND_NFSV3 | ND_NQNFS);
2445 else if (nfsvers == NFS_VER3)
2446 nd->nd_flag = ND_NFSV3;
2447 nd->nd_procnum = fxdr_unsigned(u_long, *tl++);
2448 if (nd->nd_procnum == NFSPROC_NULL)
2449 return (0);
2450 if (nd->nd_procnum >= NFS_NPROCS ||
2451 (!nqnfs && nd->nd_procnum >= NQNFSPROC_GETLEASE) ||
2452 (!nd->nd_flag && nd->nd_procnum > NFSV2PROC_STATFS)) {
2453 nd->nd_repstat = EPROCUNAVAIL;
2454 nd->nd_procnum = NFSPROC_NOOP;
2455 return (0);
2456 }
2457 if ((nd->nd_flag & ND_NFSV3) == 0)
2458 nd->nd_procnum = nfsv3_procid[nd->nd_procnum];
2459 auth_type = *tl++;
2460 len = fxdr_unsigned(int, *tl++);
2461 if (len < 0 || len > RPCAUTH_MAXSIZ) {
2462 m_freem(mrep);
2463 return (EBADRPC);
2464 }
2465
2466 nd->nd_flag &= ~ND_KERBAUTH;
2467 /*
2468 * Handle auth_unix or auth_kerb.
2469 */
2470 if (auth_type == rpc_auth_unix) {
2471 len = fxdr_unsigned(int, *++tl);
2472 if (len < 0 || len > NFS_MAXNAMLEN) {
2473 m_freem(mrep);
2474 return (EBADRPC);
2475 }
2476 nfsm_adv(nfsm_rndup(len));
2477 nfsm_dissect(tl, u_long *, 3 * NFSX_UNSIGNED);
2478 bzero((caddr_t)&nd->nd_cr, sizeof (struct ucred));
2479 nd->nd_cr.cr_ref = 1;
2480 nd->nd_cr.cr_uid = fxdr_unsigned(uid_t, *tl++);
2481 nd->nd_cr.cr_gid = fxdr_unsigned(gid_t, *tl++);
2482 len = fxdr_unsigned(int, *tl);
2483 if (len < 0 || len > RPCAUTH_UNIXGIDS) {
2484 m_freem(mrep);
2485 return (EBADRPC);
2486 }
2487 nfsm_dissect(tl, u_long *, (len + 2) * NFSX_UNSIGNED);
2488 for (i = 1; i <= len; i++)
2489 if (i < NGROUPS)
2490 nd->nd_cr.cr_groups[i] = fxdr_unsigned(gid_t, *tl++);
2491 else
2492 tl++;
2493 nd->nd_cr.cr_ngroups = (len >= NGROUPS) ? NGROUPS : (len + 1);
2494 if (nd->nd_cr.cr_ngroups > 1)
2495 nfsrvw_sort(nd->nd_cr.cr_groups, nd->nd_cr.cr_ngroups);
2496 len = fxdr_unsigned(int, *++tl);
2497 if (len < 0 || len > RPCAUTH_MAXSIZ) {
2498 m_freem(mrep);
2499 return (EBADRPC);
2500 }
2501 if (len > 0)
2502 nfsm_adv(nfsm_rndup(len));
2503 } else if (auth_type == rpc_auth_kerb) {
2504 switch (fxdr_unsigned(int, *tl++)) {
2505 case RPCAKN_FULLNAME:
2506 ticklen = fxdr_unsigned(int, *tl);
2507 *((u_long *)nfsd->nfsd_authstr) = *tl;
2508 uio.uio_resid = nfsm_rndup(ticklen) + NFSX_UNSIGNED;
2509 nfsd->nfsd_authlen = uio.uio_resid + NFSX_UNSIGNED;
2510 if (uio.uio_resid > (len - 2 * NFSX_UNSIGNED)) {
2511 m_freem(mrep);
2512 return (EBADRPC);
2513 }
2514 uio.uio_offset = 0;
2515 uio.uio_iov = &iov;
2516 uio.uio_iovcnt = 1;
2517 uio.uio_segflg = UIO_SYSSPACE;
2518 iov.iov_base = (caddr_t)&nfsd->nfsd_authstr[4];
2519 iov.iov_len = RPCAUTH_MAXSIZ - 4;
2520 nfsm_mtouio(&uio, uio.uio_resid);
2521 nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED);
2522 if (*tl++ != rpc_auth_kerb ||
2523 fxdr_unsigned(int, *tl) != 4 * NFSX_UNSIGNED) {
2524 printf("Bad kerb verifier\n");
2525 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
2526 nd->nd_procnum = NFSPROC_NOOP;
2527 return (0);
2528 }
2529 nfsm_dissect(cp, caddr_t, 4 * NFSX_UNSIGNED);
2530 tl = (u_long *)cp;
2531 if (fxdr_unsigned(int, *tl) != RPCAKN_FULLNAME) {
2532 printf("Not fullname kerb verifier\n");
2533 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
2534 nd->nd_procnum = NFSPROC_NOOP;
2535 return (0);
2536 }
2537 cp += NFSX_UNSIGNED;
2538 bcopy(cp, nfsd->nfsd_verfstr, 3 * NFSX_UNSIGNED);
2539 nfsd->nfsd_verflen = 3 * NFSX_UNSIGNED;
2540 nd->nd_flag |= ND_KERBFULL;
2541 nfsd->nfsd_flag |= NFSD_NEEDAUTH;
2542 break;
2543 case RPCAKN_NICKNAME:
2544 if (len != 2 * NFSX_UNSIGNED) {
2545 printf("Kerb nickname short\n");
2546 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADCRED);
2547 nd->nd_procnum = NFSPROC_NOOP;
2548 return (0);
2549 }
2550 nickuid = fxdr_unsigned(uid_t, *tl);
2551 nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED);
2552 if (*tl++ != rpc_auth_kerb ||
2553 fxdr_unsigned(int, *tl) != 3 * NFSX_UNSIGNED) {
2554 printf("Kerb nick verifier bad\n");
2555 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
2556 nd->nd_procnum = NFSPROC_NOOP;
2557 return (0);
2558 }
2559 nfsm_dissect(tl, u_long *, 3 * NFSX_UNSIGNED);
2560 tvin.tv_sec = *tl++;
2561 tvin.tv_usec = *tl;
2562
2563 for (nuidp = NUIDHASH(nfsd->nfsd_slp,nickuid)->lh_first;
2564 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
2565 if (nuidp->nu_cr.cr_uid == nickuid &&
2566 (!nd->nd_nam2 ||
2567 netaddr_match(NU_NETFAM(nuidp),
2568 &nuidp->nu_haddr, nd->nd_nam2)))
2569 break;
2570 }
2571 if (!nuidp) {
2572 nd->nd_repstat =
2573 (NFSERR_AUTHERR|AUTH_REJECTCRED);
2574 nd->nd_procnum = NFSPROC_NOOP;
2575 return (0);
2576 }
2577
2578 /*
2579 * Now, decrypt the timestamp using the session key
2580 * and validate it.
2581 */
2582#if NFSKERB
2583 XXX
2584#endif
2585
2586 tvout.tv_sec = fxdr_unsigned(long, tvout.tv_sec);
2587 tvout.tv_usec = fxdr_unsigned(long, tvout.tv_usec);
2588 if (nuidp->nu_expire < time.tv_sec ||
2589 nuidp->nu_timestamp.tv_sec > tvout.tv_sec ||
2590 (nuidp->nu_timestamp.tv_sec == tvout.tv_sec &&
2591 nuidp->nu_timestamp.tv_usec > tvout.tv_usec)) {
2592 nuidp->nu_expire = 0;
2593 nd->nd_repstat =
2594 (NFSERR_AUTHERR|AUTH_REJECTVERF);
2595 nd->nd_procnum = NFSPROC_NOOP;
2596 return (0);
2597 }
2598 nfsrv_setcred(&nuidp->nu_cr, &nd->nd_cr);
2599 nd->nd_flag |= ND_KERBNICK;
2600 };
2601 } else {
2602 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_REJECTCRED);
2603 nd->nd_procnum = NFSPROC_NOOP;
2604 return (0);
2605 }
2606
2607 /*
2608 * For nqnfs, get piggybacked lease request.
2609 */
2610 if (nqnfs && nd->nd_procnum != NQNFSPROC_EVICTED) {
2611 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
2612 nd->nd_flag |= fxdr_unsigned(int, *tl);
2613 if (nd->nd_flag & ND_LEASE) {
2614 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
2615 nd->nd_duration = fxdr_unsigned(int, *tl);
2616 } else
2617 nd->nd_duration = NQ_MINLEASE;
2618 } else
2619 nd->nd_duration = NQ_MINLEASE;
2620 nd->nd_md = md;
2621 nd->nd_dpos = dpos;
2622 return (0);
2623nfsmout:
2624 return (error);
2625}
2626
2627/*
2628 * Search for a sleeping nfsd and wake it up.
2629 * SIDE EFFECT: If none found, set NFSD_CHECKSLP flag, so that one of the
2630 * running nfsds will go look for the work in the nfssvc_sock list.
2631 */
2632void
2633nfsrv_wakenfsd(slp)
2634 struct nfssvc_sock *slp;
2635{
2636 register struct nfsd *nd;
2637
2638 if ((slp->ns_flag & SLP_VALID) == 0)
2639 return;
2640 for (nd = nfsd_head.tqh_first; nd != 0; nd = nd->nfsd_chain.tqe_next) {
2641 if (nd->nfsd_flag & NFSD_WAITING) {
2642 nd->nfsd_flag &= ~NFSD_WAITING;
2643 if (nd->nfsd_slp)
2644 panic("nfsd wakeup");
2645 slp->ns_sref++;
2646 nd->nfsd_slp = slp;
2647 wakeup((caddr_t)nd);
2648 return;
2649 }
2650 }
2651 slp->ns_flag |= SLP_DOREC;
2652 nfsd_head_flag |= NFSD_CHECKSLP;
2653}
2654#endif /* NFS_NOSERVER */
2655
2656static int
2657nfs_msg(p, server, msg)
2658 struct proc *p;
2659 char *server, *msg;
2660{
2661 tpr_t tpr;
2662
2663 if (p)
2664 tpr = tprintf_open(p);
2665 else
2666 tpr = NULL;
2667 tprintf(tpr, "nfs server %s: %s\n", server, msg);
2668 tprintf_close(tpr);
2669 return (0);
2670}