2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1989, 1991, 1993, 1995
25 * The Regents of the University of California. All rights reserved.
27 * This code is derived from software contributed to Berkeley by
28 * Rick Macklem at The University of Guelph.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * @(#)nfs_socket.c 8.5 (Berkeley) 3/30/95
59 * FreeBSD-Id: nfs_socket.c,v 1.30 1997/10/28 15:59:07 bde Exp $
63 * Socket operations for use by nfs
66 #include <sys/param.h>
67 #include <sys/systm.h>
69 #include <sys/kauth.h>
70 #include <sys/mount_internal.h>
71 #include <sys/kernel.h>
72 #include <sys/kpi_mbuf.h>
73 #include <sys/malloc.h>
74 #include <sys/vnode.h>
75 #include <sys/domain.h>
76 #include <sys/protosw.h>
77 #include <sys/socket.h>
78 #include <sys/syslog.h>
79 #include <sys/tprintf.h>
80 #include <sys/uio_internal.h>
81 #include <libkern/OSAtomic.h>
84 #include <kern/clock.h>
85 #include <kern/task.h>
86 #include <kern/thread.h>
89 #include <netinet/in.h>
90 #include <netinet/tcp.h>
92 #include <nfs/rpcv2.h>
93 #include <nfs/nfsproto.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>
101 #include <sys/kdebug.h>
103 #define FSDBG(A, B, C, D, E) \
104 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_NONE, \
105 (int)(B), (int)(C), (int)(D), (int)(E), 0)
106 #define FSDBG_TOP(A, B, C, D, E) \
107 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_START, \
108 (int)(B), (int)(C), (int)(D), (int)(E), 0)
109 #define FSDBG_BOT(A, B, C, D, E) \
110 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_END, \
111 (int)(B), (int)(C), (int)(D), (int)(E), 0)
114 * Estimate rto for an nfs rpc sent via. an unreliable datagram.
115 * Use the mean and mean deviation of rtt for the appropriate type of rpc
116 * for the frequent rpcs and a default for the others.
117 * The justification for doing "other" this way is that these rpcs
118 * happen so infrequently that timer est. would probably be stale.
119 * Also, since many of these rpcs are
120 * non-idempotent, a conservative timeout is desired.
121 * getattr, lookup - A+2D
125 #define NFS_RTO(n, t) \
126 ((t) == 0 ? (n)->nm_timeo : \
128 (((((n)->nm_srtt[t-1] + 3) >> 2) + (n)->nm_sdrtt[t-1] + 1) >> 1) : \
129 ((((n)->nm_srtt[t-1] + 7) >> 3) + (n)->nm_sdrtt[t-1] + 1)))
130 #define NFS_SRTT(r) (r)->r_nmp->nm_srtt[proct[(r)->r_procnum] - 1]
131 #define NFS_SDRTT(r) (r)->r_nmp->nm_sdrtt[proct[(r)->r_procnum] - 1]
133 * External data, mostly RPC constants in XDR form
135 extern u_long rpc_reply
, rpc_msgdenied
, rpc_mismatch
, rpc_vers
, rpc_auth_unix
,
136 rpc_msgaccepted
, rpc_call
, rpc_autherr
,
138 extern u_long nfs_prog
;
139 extern struct nfsstats nfsstats
;
140 extern int nfsv3_procid
[NFS_NPROCS
];
141 extern int nfs_ticks
;
142 extern u_long nfs_xidwrap
;
145 * Defines which timer to use for the procnum.
152 static int proct
[NFS_NPROCS
] = {
153 0, 1, 0, 2, 1, 3, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0
157 * There is a congestion window for outstanding rpcs maintained per mount
158 * point. The cwnd size is adjusted in roughly the way that:
159 * Van Jacobson, Congestion avoidance and Control, In "Proceedings of
160 * SIGCOMM '88". ACM, August 1988.
161 * describes for TCP. The cwnd size is chopped in half on a retransmit timeout
162 * and incremented by 1/cwnd when each rpc reply is received and a full cwnd
163 * of rpcs is in progress.
164 * (The sent count and cwnd are scaled for integer arith.)
165 * Variants of "slow start" were tried and were found to be too much of a
166 * performance hit (ave. rtt 3 times larger),
167 * I suspect due to the large rtt that nfs rpcs have.
169 #define NFS_CWNDSCALE 256
170 #define NFS_MAXCWND (NFS_CWNDSCALE * 32)
171 static int nfs_backoff
[8] = { 2, 4, 8, 16, 32, 64, 128, 256, };
173 struct nfsrtt nfsrtt
;
175 static int nfs_rcvlock(struct nfsreq
*);
176 static void nfs_rcvunlock(struct nfsreq
*);
177 static int nfs_receive(struct nfsreq
*rep
, mbuf_t
*mp
);
178 static int nfs_reconnect(struct nfsreq
*rep
);
179 static void nfs_repdequeue(struct nfsreq
*rep
);
182 boolean_t
current_thread_aborted(void);
183 kern_return_t
thread_terminate(thread_t
);
186 static int nfsrv_getstream(struct nfssvc_sock
*,int);
188 int (*nfsrv3_procs
[NFS_NPROCS
])(struct nfsrv_descript
*nd
,
189 struct nfssvc_sock
*slp
,
216 #endif /* NFS_NOSERVER */
220 * attempt to bind a socket to a reserved port
223 nfs_bind_resv(struct nfsmount
*nmp
)
225 socket_t so
= nmp
->nm_so
;
226 struct sockaddr_in sin
;
233 sin
.sin_len
= sizeof (struct sockaddr_in
);
234 sin
.sin_family
= AF_INET
;
235 sin
.sin_addr
.s_addr
= INADDR_ANY
;
236 tport
= IPPORT_RESERVED
- 1;
237 sin
.sin_port
= htons(tport
);
239 while (((error
= sock_bind(so
, (struct sockaddr
*) &sin
)) == EADDRINUSE
) &&
240 (--tport
> IPPORT_RESERVED
/ 2))
241 sin
.sin_port
= htons(tport
);
246 * variables for managing the nfs_bind_resv_thread
248 int nfs_resv_mounts
= 0;
249 static int nfs_bind_resv_thread_state
= 0;
250 #define NFS_BIND_RESV_THREAD_STATE_INITTED 1
251 #define NFS_BIND_RESV_THREAD_STATE_RUNNING 2
252 lck_grp_t
*nfs_bind_resv_lck_grp
;
253 lck_grp_attr_t
*nfs_bind_resv_lck_grp_attr
;
254 lck_attr_t
*nfs_bind_resv_lck_attr
;
255 lck_mtx_t
*nfs_bind_resv_mutex
;
256 struct nfs_bind_resv_request
{
257 TAILQ_ENTRY(nfs_bind_resv_request
) brr_chain
;
258 struct nfsmount
*brr_nmp
;
261 static TAILQ_HEAD(, nfs_bind_resv_request
) nfs_bind_resv_request_queue
;
264 * thread to handle any reserved port bind requests
267 nfs_bind_resv_thread(void)
269 struct nfs_bind_resv_request
*brreq
;
271 nfs_bind_resv_thread_state
= NFS_BIND_RESV_THREAD_STATE_RUNNING
;
273 while (nfs_resv_mounts
> 0) {
274 lck_mtx_lock(nfs_bind_resv_mutex
);
275 while ((brreq
= TAILQ_FIRST(&nfs_bind_resv_request_queue
))) {
276 TAILQ_REMOVE(&nfs_bind_resv_request_queue
, brreq
, brr_chain
);
277 lck_mtx_unlock(nfs_bind_resv_mutex
);
278 brreq
->brr_error
= nfs_bind_resv(brreq
->brr_nmp
);
280 lck_mtx_lock(nfs_bind_resv_mutex
);
282 msleep((caddr_t
)&nfs_bind_resv_request_queue
,
283 nfs_bind_resv_mutex
, PSOCK
| PDROP
,
284 "nfs_bind_resv_request_queue", 0);
287 nfs_bind_resv_thread_state
= NFS_BIND_RESV_THREAD_STATE_INITTED
;
288 (void) thread_terminate(current_thread());
292 nfs_bind_resv_thread_wake(void)
294 if (nfs_bind_resv_thread_state
< NFS_BIND_RESV_THREAD_STATE_RUNNING
)
296 wakeup(&nfs_bind_resv_request_queue
);
301 * underprivileged procs call this to request nfs_bind_resv_thread
302 * to perform the reserved port binding for them.
305 nfs_bind_resv_nopriv(struct nfsmount
*nmp
)
307 struct nfs_bind_resv_request brreq
;
310 if (nfs_bind_resv_thread_state
< NFS_BIND_RESV_THREAD_STATE_RUNNING
) {
311 if (nfs_bind_resv_thread_state
< NFS_BIND_RESV_THREAD_STATE_INITTED
) {
312 nfs_bind_resv_lck_grp_attr
= lck_grp_attr_alloc_init();
313 nfs_bind_resv_lck_grp
= lck_grp_alloc_init("nfs_bind_resv", nfs_bind_resv_lck_grp_attr
);
314 nfs_bind_resv_lck_attr
= lck_attr_alloc_init();
315 nfs_bind_resv_mutex
= lck_mtx_alloc_init(nfs_bind_resv_lck_grp
, nfs_bind_resv_lck_attr
);
316 TAILQ_INIT(&nfs_bind_resv_request_queue
);
317 nfs_bind_resv_thread_state
= NFS_BIND_RESV_THREAD_STATE_INITTED
;
319 kernel_thread(kernel_task
, nfs_bind_resv_thread
);
320 nfs_bind_resv_thread_state
= NFS_BIND_RESV_THREAD_STATE_RUNNING
;
326 lck_mtx_lock(nfs_bind_resv_mutex
);
327 TAILQ_INSERT_TAIL(&nfs_bind_resv_request_queue
, &brreq
, brr_chain
);
328 lck_mtx_unlock(nfs_bind_resv_mutex
);
330 error
= nfs_bind_resv_thread_wake();
332 TAILQ_REMOVE(&nfs_bind_resv_request_queue
, &brreq
, brr_chain
);
333 /* Note: we might be able to simply restart the thread */
337 tsleep((caddr_t
)&brreq
, PSOCK
, "nfsbindresv", 0);
339 return (brreq
.brr_error
);
343 * Initialize sockets and congestion for a new NFS connection.
344 * We do not free the sockaddr if error.
348 struct nfsmount
*nmp
,
349 __unused
struct nfsreq
*rep
)
352 int error
, rcvreserve
, sndreserve
;
353 struct sockaddr
*saddr
;
354 struct timeval timeo
;
357 saddr
= mbuf_data(nmp
->nm_nam
);
358 error
= sock_socket(saddr
->sa_family
, nmp
->nm_sotype
,
359 nmp
->nm_soproto
, 0, 0, &nmp
->nm_so
);
366 * Some servers require that the client port be a reserved port number.
368 if (saddr
->sa_family
== AF_INET
&& (nmp
->nm_flag
& NFSMNT_RESVPORT
)) {
371 * sobind() requires current_proc() to have superuser privs.
372 * If this bind is part of a reconnect, and the current proc
373 * doesn't have superuser privs, we hand the sobind() off to
374 * a kernel thread to process.
376 if ((nmp
->nm_state
& NFSSTA_MOUNTED
) &&
377 (p
= current_proc()) && suser(kauth_cred_get(), 0)) {
378 /* request nfs_bind_resv_thread() to do bind */
379 error
= nfs_bind_resv_nopriv(nmp
);
381 error
= nfs_bind_resv(nmp
);
388 * Protocols that do not require connections may be optionally left
389 * unconnected for servers that reply from a port other than NFS_PORT.
391 if (nmp
->nm_flag
& NFSMNT_NOCONN
) {
392 if (nmp
->nm_sotype
== SOCK_STREAM
) {
400 error
= sock_connect(so
, mbuf_data(nmp
->nm_nam
), MSG_DONTWAIT
);
401 if (error
&& error
!= EINPROGRESS
) {
405 while ((error
= sock_connectwait(so
, &tv
)) == EINPROGRESS
) {
406 if (rep
&& (error
= nfs_sigintr(nmp
, rep
, rep
->r_procp
))) {
413 * Always time out on recieve, this allows us to reconnect the
414 * socket to deal with network changes.
418 error
= sock_setsockopt(so
, SOL_SOCKET
, SO_RCVTIMEO
, &timeo
, sizeof(timeo
));
419 if (nmp
->nm_flag
& (NFSMNT_SOFT
| NFSMNT_INT
)) {
424 error
= sock_setsockopt(so
, SOL_SOCKET
, SO_SNDTIMEO
, &timeo
, sizeof(timeo
));
426 if (nmp
->nm_sotype
== SOCK_DGRAM
) {
427 sndreserve
= (nmp
->nm_wsize
+ NFS_MAXPKTHDR
) * 3;
428 rcvreserve
= (nmp
->nm_rsize
+ NFS_MAXPKTHDR
) *
429 (nmp
->nm_readahead
> 0 ? nmp
->nm_readahead
+ 1 : 2);
430 } else if (nmp
->nm_sotype
== SOCK_SEQPACKET
) {
431 sndreserve
= (nmp
->nm_wsize
+ NFS_MAXPKTHDR
) * 3;
432 rcvreserve
= (nmp
->nm_rsize
+ NFS_MAXPKTHDR
) *
433 (nmp
->nm_readahead
> 0 ? nmp
->nm_readahead
+ 1 : 2);
438 sock_gettype(so
, NULL
, NULL
, &proto
);
439 if (nmp
->nm_sotype
!= SOCK_STREAM
)
440 panic("nfscon sotype");
442 // Assume that SOCK_STREAM always requires a connection
443 sock_setsockopt(so
, SOL_SOCKET
, SO_KEEPALIVE
, &on
, sizeof(on
));
445 if (proto
== IPPROTO_TCP
) {
446 sock_setsockopt(so
, IPPROTO_TCP
, TCP_NODELAY
, &on
, sizeof(on
));
449 sndreserve
= (nmp
->nm_wsize
+ NFS_MAXPKTHDR
+ sizeof (u_long
)) * 3;
450 rcvreserve
= (nmp
->nm_rsize
+ NFS_MAXPKTHDR
+ sizeof (u_long
)) *
451 (nmp
->nm_readahead
> 0 ? nmp
->nm_readahead
+ 1 : 2);
454 if (sndreserve
> NFS_MAXSOCKBUF
)
455 sndreserve
= NFS_MAXSOCKBUF
;
456 if (rcvreserve
> NFS_MAXSOCKBUF
)
457 rcvreserve
= NFS_MAXSOCKBUF
;
458 error
= sock_setsockopt(so
, SOL_SOCKET
, SO_SNDBUF
, &sndreserve
, sizeof(sndreserve
));
462 error
= sock_setsockopt(so
, SOL_SOCKET
, SO_RCVBUF
, &rcvreserve
, sizeof(rcvreserve
));
467 sock_nointerrupt(so
, 1);
469 /* Initialize other non-zero congestion variables */
470 nmp
->nm_srtt
[0] = nmp
->nm_srtt
[1] = nmp
->nm_srtt
[2] =
471 nmp
->nm_srtt
[3] = (NFS_TIMEO
<< 3);
472 nmp
->nm_sdrtt
[0] = nmp
->nm_sdrtt
[1] = nmp
->nm_sdrtt
[2] =
473 nmp
->nm_sdrtt
[3] = 0;
474 nmp
->nm_cwnd
= NFS_MAXCWND
/ 2; /* Initial send window */
476 FSDBG(529, nmp
, nmp
->nm_state
, nmp
->nm_soflags
, nmp
->nm_cwnd
);
477 nmp
->nm_timeouts
= 0;
487 * Called when a connection is broken on a reliable protocol.
488 * - clean up the old socket
489 * - nfs_connect() again
490 * - set R_MUSTRESEND for all outstanding requests on mount point
491 * If this fails the mount point is DEAD!
492 * nb: Must be called with the nfs_sndlock() set on the mount point.
495 nfs_reconnect(struct nfsreq
*rep
)
498 struct nfsmount
*nmp
= rep
->r_nmp
;
502 while ((error
= nfs_connect(nmp
, rep
))) {
503 if (error
== EINTR
|| error
== ERESTART
)
507 nfs_down(rep
->r_nmp
, rep
->r_procp
, error
, NFSSTA_TIMEO
,
509 rep
->r_flags
|= R_TPRINTFMSG
;
510 if (!(nmp
->nm_state
& NFSSTA_MOUNTED
)) {
511 /* we're not yet completely mounted and */
512 /* we can't reconnect, so we fail */
515 if ((error
= nfs_sigintr(rep
->r_nmp
, rep
, rep
->r_procp
)))
517 tsleep((caddr_t
)&lbolt
, PSOCK
, "nfscon", 0);
521 * Loop through outstanding request list and fix up all requests
524 TAILQ_FOREACH(rp
, &nfs_reqq
, r_chain
) {
525 if (rp
->r_nmp
== nmp
)
526 rp
->r_flags
|= R_MUSTRESEND
;
532 * NFS disconnect. Clean up and unlink.
535 nfs_disconnect(struct nfsmount
*nmp
)
542 sock_shutdown(so
, 2);
548 * This is the nfs send routine. For connection based socket types, it
549 * must be called with an nfs_sndlock() on the socket.
550 * "rep == NULL" indicates that it has been called from a server.
551 * For the client side:
552 * - return EINTR if the RPC is terminated, 0 otherwise
553 * - set R_MUSTRESEND if the send fails for any reason
554 * - do any cleanup required by recoverable socket errors (???)
555 * For the server side:
556 * - return EINTR or ERESTART if interrupted by a signal
557 * - return EPIPE if a connection is lost for connection based sockets (TCP...)
558 * - do any cleanup required by recoverable socket errors (???)
561 nfs_send(so
, nam
, top
, rep
)
567 struct sockaddr
*sendnam
;
568 int error
, error2
, sotype
, flags
;
569 u_long xidqueued
= 0;
571 char savenametolog
[MAXPATHLEN
];
575 error
= nfs_sigintr(rep
->r_nmp
, rep
, rep
->r_procp
);
580 if ((so
= rep
->r_nmp
->nm_so
) == NULL
) {
581 rep
->r_flags
|= R_MUSTRESEND
;
585 rep
->r_flags
&= ~R_MUSTRESEND
;
586 TAILQ_FOREACH(rp
, &nfs_reqq
, r_chain
)
590 xidqueued
= rp
->r_xid
;
592 sock_gettype(so
, NULL
, &sotype
, NULL
);
593 if ((sotype
== SOCK_STREAM
) || (sock_isconnected(so
)) ||
595 sendnam
= (struct sockaddr
*)0;
597 sendnam
= mbuf_data(nam
);
599 if (sotype
== SOCK_SEQPACKET
)
605 * Save the name here in case mount point goes away if we block.
606 * The name is using local stack and is large, but don't
607 * want to block if we malloc.
610 strncpy(savenametolog
,
611 vfs_statfs(rep
->r_nmp
->nm_mountp
)->f_mntfromname
,
613 bzero(&msg
, sizeof(msg
));
614 msg
.msg_name
= (caddr_t
)sendnam
;
615 msg
.msg_namelen
= sendnam
== 0 ? 0 : sendnam
->sa_len
;
616 error
= sock_sendmbuf(so
, &msg
, top
, flags
, NULL
);
621 TAILQ_FOREACH(rp
, &nfs_reqq
, r_chain
)
622 if (rp
== rep
&& rp
->r_xid
== xidqueued
)
625 panic("nfs_send: error %d xid %x gone",
628 log(LOG_INFO
, "nfs send error %d for server %s\n",
629 error
, savenametolog
);
631 * Deal with errors for the client side.
633 error2
= nfs_sigintr(rep
->r_nmp
, rep
, rep
->r_procp
);
637 rep
->r_flags
|= R_MUSTRESEND
;
640 log(LOG_INFO
, "nfsd send error %d\n", error
);
643 * Handle any recoverable (soft) socket errors here. (???)
645 if (error
!= EINTR
&& error
!= ERESTART
&& error
!= EIO
&&
646 error
!= EWOULDBLOCK
&& error
!= EPIPE
) {
654 * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all
655 * done by soreceive(), but for SOCK_STREAM we must deal with the Record
656 * Mark and consolidate the data into a new mbuf list.
657 * nb: Sometimes TCP passes the data up to soreceive() in long lists of
659 * For SOCK_STREAM we must be very careful to read an entire record once
660 * we have read any of it, even if the system call has been interrupted.
663 nfs_receive(struct nfsreq
*rep
, mbuf_t
*mp
)
669 int error
, error2
, sotype
;
670 proc_t p
= current_proc(); /* XXX */
676 * Set up arguments for soreceive()
679 sotype
= rep
->r_nmp
->nm_sotype
;
682 * For reliable protocols, lock against other senders/receivers
683 * in case a reconnect is necessary.
684 * For SOCK_STREAM, first get the Record Mark to find out how much
685 * more there is to get.
686 * We must lock the socket against other receivers
687 * until we have an entire rpc request/reply.
689 if (sotype
!= SOCK_DGRAM
) {
690 error
= nfs_sndlock(rep
);
695 * Check for fatal errors and resending request.
698 * Ugh: If a reconnect attempt just happened, nm_so
699 * would have changed. NULL indicates a failed
700 * attempt that has essentially shut down this
703 if ((error
= nfs_sigintr(rep
->r_nmp
, rep
, p
)) || rep
->r_mrep
) {
709 so
= rep
->r_nmp
->nm_so
;
711 error
= nfs_reconnect(rep
);
718 while (rep
->r_flags
& R_MUSTRESEND
) {
719 error
= mbuf_copym(rep
->r_mreq
, 0, MBUF_COPYALL
, MBUF_WAITOK
, &m
);
721 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpcretries
);
722 error
= nfs_send(so
, rep
->r_nmp
->nm_nam
, m
, rep
);
725 * we also hold rcv lock so rep is still
729 if (error
== EINTR
|| error
== ERESTART
||
730 (error
= nfs_reconnect(rep
))) {
738 if (sotype
== SOCK_STREAM
) {
743 while (!error
&& !lastfragment
) {
744 aio
.iov_base
= (uintptr_t) &fraglen
;
745 aio
.iov_len
= sizeof(u_long
);
746 bzero(&msg
, sizeof(msg
));
747 msg
.msg_iov
= (struct iovec
*) &aio
;
750 error
= sock_receive(so
, &msg
, MSG_WAITALL
, &rcvlen
);
751 if (!rep
->r_nmp
) /* if unmounted then bailout */
753 if (error
== EWOULDBLOCK
&& rep
) {
754 error2
= nfs_sigintr(rep
->r_nmp
, rep
, p
);
758 } while (error
== EWOULDBLOCK
);
759 if (!error
&& rcvlen
< aio
.iov_len
) {
760 /* only log a message if we got a partial word */
763 "short receive (%d/%d) from nfs server %s\n",
764 rcvlen
, sizeof(u_long
),
765 vfs_statfs(rep
->r_nmp
->nm_mountp
)->f_mntfromname
);
770 lastfragment
= ntohl(fraglen
) & 0x80000000;
771 fraglen
= ntohl(fraglen
) & ~0x80000000;
774 * This is SERIOUS! We are out of sync with the sender
775 * and forcing a disconnect/reconnect is all I can do.
777 if (len
> NFS_MAXPACKET
) {
778 log(LOG_ERR
, "%s (%d) from nfs server %s\n",
779 "impossible RPC record length", len
,
780 vfs_statfs(rep
->r_nmp
->nm_mountp
)->f_mntfromname
);
788 error
= sock_receivembuf(so
, NULL
, &m
, MSG_WAITALL
, &rcvlen
);
789 if (!rep
->r_nmp
) /* if unmounted then bailout */ {
792 } while (error
== EWOULDBLOCK
|| error
== EINTR
||
795 if (!error
&& fraglen
> rcvlen
) {
797 "short receive (%d/%d) from nfs server %s\n",
799 vfs_statfs(rep
->r_nmp
->nm_mountp
)->f_mntfromname
);
808 error
= mbuf_setnext(mlast
, m
);
810 printf("nfs_receive: mbuf_setnext failed %d\n", error
);
814 while (mbuf_next(mlast
))
815 mlast
= mbuf_next(mlast
);
819 bzero(&msg
, sizeof(msg
));
822 error
= sock_receivembuf(so
, &msg
, mp
, 0, &rcvlen
);
823 if (!rep
->r_nmp
) /* if unmounted then bailout */ {
826 if (error
== EWOULDBLOCK
&& rep
) {
827 error2
= nfs_sigintr(rep
->r_nmp
, rep
, p
);
832 } while (error
== EWOULDBLOCK
);
834 if ((msg
.msg_flags
& MSG_EOR
) == 0)
836 if (!error
&& *mp
== NULL
)
841 if (error
&& error
!= EINTR
&& error
!= ERESTART
) {
846 "receive error %d from nfs server %s\n", error
,
847 vfs_statfs(rep
->r_nmp
->nm_mountp
)->f_mntfromname
);
848 error
= nfs_sndlock(rep
);
850 error
= nfs_reconnect(rep
);
858 * We could have failed while rebinding the datagram socket
859 * so we need to attempt to rebind here.
861 if ((so
= rep
->r_nmp
->nm_so
) == NULL
) {
862 error
= nfs_sndlock(rep
);
864 error
= nfs_reconnect(rep
);
869 if (!rep
->r_nmp
) /* if unmounted then bailout */
871 so
= rep
->r_nmp
->nm_so
;
873 bzero(&msg
, sizeof(msg
));
877 error
= sock_receivembuf(so
, &msg
, mp
, 0, &rcvlen
);
878 if (!rep
->r_nmp
) /* if unmounted then bailout */
881 error2
= nfs_sigintr(rep
->r_nmp
, rep
, p
);
887 /* Reconnect for all errors. We may be receiving
888 * soft/hard/blocking errors because of a network
890 * XXX: we should rate limit or delay this
891 * to once every N attempts or something.
892 * although TCP doesn't seem to.
895 error2
= nfs_sndlock(rep
);
897 error2
= nfs_reconnect(rep
);
900 else if (!rep
->r_nmp
) /* if unmounted then bailout */
903 so
= rep
->r_nmp
->nm_so
;
909 } while (error
== EWOULDBLOCK
);
920 * Implement receipt of reply on a socket.
921 * We must search through the list of received datagrams matching them
922 * with outstanding requests using the xid, until ours is found.
927 struct nfsreq
*myrep
;
930 struct nfsmount
*nmp
= myrep
->r_nmp
;
938 * Loop around until we get our own reply
942 * Lock against other receivers so that I don't get stuck in
943 * sbwait() after someone else has received my reply for me.
944 * Also necessary for connection based protocols to avoid
945 * race conditions during a reconnect.
946 * If nfs_rcvlock() returns EALREADY, that means that
947 * the reply has already been recieved by another
948 * process and we can return immediately. In this
949 * case, the lock is not taken to avoid races with
952 error
= nfs_rcvlock(myrep
);
953 if (error
== EALREADY
)
959 * If we slept after putting bits otw, then reply may have
960 * arrived. In which case returning is required, or we
961 * would hang trying to nfs_receive an already received reply.
963 if (myrep
->r_mrep
!= NULL
) {
964 nfs_rcvunlock(myrep
);
965 FSDBG(530, myrep
->r_xid
, myrep
, myrep
->r_nmp
, -1);
969 * Get the next Rpc reply off the socket. Assume myrep->r_nmp
970 * is still intact by checks done in nfs_rcvlock.
972 error
= nfs_receive(myrep
, &mrep
);
974 * Bailout asap if nfsmount struct gone (unmounted).
977 FSDBG(530, myrep
->r_xid
, myrep
, nmp
, -2);
983 FSDBG(530, myrep
->r_xid
, myrep
, nmp
, error
);
984 nfs_rcvunlock(myrep
);
986 /* Bailout asap if nfsmount struct gone (unmounted). */
994 * Ignore routing errors on connectionless protocols??
996 if (NFSIGNORE_SOERROR(nmp
->nm_sotype
, error
)) {
999 int optlen
= sizeof(clearerror
);
1000 sock_getsockopt(nmp
->nm_so
, SOL_SOCKET
, SO_ERROR
, &clearerror
, &optlen
);
1010 * We assume all is fine, but if we did not have an error
1011 * and mrep is 0, better not dereference it. nfs_receive
1012 * calls soreceive which carefully sets error=0 when it got
1013 * errors on sbwait (tsleep). In most cases, I assume that's
1014 * so we could go back again. In tcp case, EPIPE is returned.
1015 * In udp, case nfs_receive gets back here with no error and no
1016 * mrep. Is the right fix to have soreceive check for process
1017 * aborted after sbwait and return something non-zero? Should
1018 * nfs_receive give an EPIPE? Too risky to play with those
1019 * two this late in game for a shutdown problem. Instead,
1020 * just check here and get out. (ekn)
1023 nfs_rcvunlock(myrep
);
1024 FSDBG(530, myrep
->r_xid
, myrep
, nmp
, -3);
1025 return (ENXIO
); /* sounds good */
1029 * Get the xid and check that it is an rpc reply
1032 dpos
= mbuf_data(md
);
1033 nfsm_dissect(tl
, u_long
*, 2*NFSX_UNSIGNED
);
1035 if (*tl
!= rpc_reply
) {
1036 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpcinvalid
);
1039 if (nmp
->nm_state
& NFSSTA_RCVLOCK
)
1040 nfs_rcvunlock(myrep
);
1045 * Loop through the request list to match up the reply
1046 * Iff no match, just drop the datagram
1048 TAILQ_FOREACH(rep
, &nfs_reqq
, r_chain
) {
1049 if (rep
->r_mrep
== NULL
&& rxid
== rep
->r_xid
) {
1055 * If we're tracking the round trip time
1056 * then we update the circular log here
1057 * with the stats from our current request.
1062 rt
= &nfsrtt
.rttl
[nfsrtt
.pos
];
1063 rt
->proc
= rep
->r_procnum
;
1064 rt
->rto
= NFS_RTO(nmp
, proct
[rep
->r_procnum
]);
1065 rt
->sent
= nmp
->nm_sent
;
1066 rt
->cwnd
= nmp
->nm_cwnd
;
1067 if (proct
[rep
->r_procnum
] == 0)
1068 panic("nfs_reply: proct[%d] is zero", rep
->r_procnum
);
1069 rt
->srtt
= nmp
->nm_srtt
[proct
[rep
->r_procnum
] - 1];
1070 rt
->sdrtt
= nmp
->nm_sdrtt
[proct
[rep
->r_procnum
] - 1];
1071 rt
->fsid
= vfs_statfs(nmp
->nm_mountp
)->f_fsid
;
1072 microtime(&rt
->tstamp
); // XXX unused
1073 if (rep
->r_flags
& R_TIMING
)
1074 rt
->rtt
= rep
->r_rtt
;
1077 nfsrtt
.pos
= (nfsrtt
.pos
+ 1) % NFSRTTLOGSIZ
;
1080 * Update congestion window.
1081 * Do the additive increase of
1084 FSDBG(530, rep
->r_xid
, rep
, nmp
->nm_sent
,
1086 if (nmp
->nm_cwnd
<= nmp
->nm_sent
) {
1088 (NFS_CWNDSCALE
* NFS_CWNDSCALE
+
1089 (nmp
->nm_cwnd
>> 1)) / nmp
->nm_cwnd
;
1090 if (nmp
->nm_cwnd
> NFS_MAXCWND
)
1091 nmp
->nm_cwnd
= NFS_MAXCWND
;
1093 if (rep
->r_flags
& R_SENT
) {
1094 rep
->r_flags
&= ~R_SENT
;
1095 nmp
->nm_sent
-= NFS_CWNDSCALE
;
1098 * Update rtt using a gain of 0.125 on the mean
1099 * and a gain of 0.25 on the deviation.
1101 if (rep
->r_flags
& R_TIMING
) {
1103 * Since the timer resolution of
1104 * NFS_HZ is so course, it can often
1105 * result in r_rtt == 0. Since
1106 * r_rtt == N means that the actual
1107 * rtt is between N+dt and N+2-dt ticks,
1110 if (proct
[rep
->r_procnum
] == 0)
1111 panic("nfs_reply: proct[%d] is zero", rep
->r_procnum
);
1112 t1
= rep
->r_rtt
+ 1;
1113 t1
-= (NFS_SRTT(rep
) >> 3);
1114 NFS_SRTT(rep
) += t1
;
1117 t1
-= (NFS_SDRTT(rep
) >> 2);
1118 NFS_SDRTT(rep
) += t1
;
1120 nmp
->nm_timeouts
= 0;
1124 nfs_rcvunlock(myrep
);
1126 * If not matched to a request, drop it.
1127 * If it's mine, get out.
1130 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpcunexpected
);
1132 } else if (rep
== myrep
) {
1133 if (rep
->r_mrep
== NULL
)
1134 panic("nfs_reply: nil r_mrep");
1137 FSDBG(530, myrep
->r_xid
, myrep
, rep
,
1138 rep
? rep
->r_xid
: myrep
->r_flags
);
1143 * nfs_request - goes something like this
1144 * - fill in request struct
1145 * - links it into list
1146 * - calls nfs_send() for first transmit
1147 * - calls nfs_receive() to get reply
1148 * - break down rpc header and return with nfs reply pointed to
1150 * nb: always frees up mreq mbuf list
1153 nfs_request(vp
, mp
, mrest
, procnum
, procp
, cred
, mrp
, mdp
, dposp
, xidp
)
1166 struct nfsreq re
, *rep
;
1169 struct nfsmount
*nmp
;
1170 mbuf_t md
, mheadend
;
1171 char nickv
[RPCX_NICKVERF
];
1174 int t1
, error
= 0, mrest_len
, auth_len
, auth_type
;
1175 int trylater_delay
= NFS_TRYLATERDEL
, failed_auth
= 0;
1176 int verf_len
, verf_type
;
1178 char *auth_str
, *verf_str
;
1179 NFSKERBKEY_T key
; /* save session key */
1192 nmp
= VFSTONFS(vnode_mount(vp
));
1194 (nmp
->nm_state
& (NFSSTA_FORCE
|NFSSTA_TIMEO
)) ==
1195 (NFSSTA_FORCE
|NFSSTA_TIMEO
)) {
1199 nmsotype
= nmp
->nm_sotype
;
1201 FSDBG_TOP(531, vp
, procnum
, nmp
, rep
);
1205 rep
->r_procp
= procp
;
1206 rep
->r_procnum
= procnum
;
1208 rep
->r_lastmsg
= now
.tv_sec
-
1209 ((nmp
->nm_tprintf_delay
) - (nmp
->nm_tprintf_initial_delay
));
1219 * Get the RPC header with authorization.
1222 nmp
= vp
? VFSTONFS(vnode_mount(vp
)) : rep
->r_nmp
;
1224 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1228 verf_str
= auth_str
= (char *)0;
1229 if (nmp
->nm_flag
& NFSMNT_KERB
) {
1231 verf_len
= sizeof (nickv
);
1232 auth_type
= RPCAUTH_KERB4
;
1233 bzero((caddr_t
)key
, sizeof (key
));
1234 if (failed_auth
|| nfs_getnickauth(nmp
, cred
, &auth_str
,
1235 &auth_len
, verf_str
, verf_len
)) {
1236 nmp
= vp
? VFSTONFS(vnode_mount(vp
)) : rep
->r_nmp
;
1238 FSDBG_BOT(531, 2, vp
, error
, rep
);
1242 error
= nfs_getauth(nmp
, rep
, cred
, &auth_str
,
1243 &auth_len
, verf_str
, &verf_len
, key
);
1244 nmp
= vp
? VFSTONFS(vnode_mount(vp
)) : rep
->r_nmp
;
1248 FSDBG_BOT(531, 2, vp
, error
, rep
);
1254 auth_type
= RPCAUTH_UNIX
;
1255 if (cred
->cr_ngroups
< 1)
1256 panic("nfsreq nogrps");
1257 auth_len
= ((((cred
->cr_ngroups
- 1) > nmp
->nm_numgrps
) ?
1258 nmp
->nm_numgrps
: (cred
->cr_ngroups
- 1)) << 2) +
1261 error
= nfsm_rpchead(cred
, nmp
->nm_flag
, procnum
, auth_type
, auth_len
,
1262 auth_str
, verf_len
, verf_str
, mrest
, mrest_len
, &mheadend
, &xid
, &m
);
1264 _FREE(auth_str
, M_TEMP
);
1267 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1271 *xidp
= ntohl(xid
) + ((u_int64_t
)nfs_xidwrap
<< 32);
1274 * For stream protocols, insert a Sun RPC Record Mark.
1276 if (nmsotype
== SOCK_STREAM
) {
1277 error
= mbuf_prepend(&m
, NFSX_UNSIGNED
, MBUF_WAITOK
);
1280 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1283 *((u_long
*)mbuf_data(m
)) =
1284 htonl(0x80000000 | (mbuf_pkthdr_len(m
) - NFSX_UNSIGNED
));
1289 nmp
= vp
? VFSTONFS(vnode_mount(vp
)) : rep
->r_nmp
;
1290 if (nmp
&& (nmp
->nm_flag
& NFSMNT_SOFT
))
1291 rep
->r_retry
= nmp
->nm_retry
;
1293 rep
->r_retry
= NFS_MAXREXMIT
+ 1; /* past clip limit */
1294 rep
->r_rtt
= rep
->r_rexmit
= 0;
1295 if (proct
[procnum
] > 0)
1296 rep
->r_flags
= R_TIMING
;
1302 * Do the client side RPC.
1304 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpcrequests
);
1306 * Chain request into list of outstanding requests. Be sure
1307 * to put it LAST so timer finds oldest requests first.
1309 TAILQ_INSERT_TAIL(&nfs_reqq
, rep
, r_chain
);
1312 * If backing off another request or avoiding congestion, don't
1313 * send this one now but let timer do it. If not timing a request,
1316 if (nmp
&& nmp
->nm_so
&& (nmp
->nm_sotype
!= SOCK_DGRAM
||
1317 (nmp
->nm_flag
& NFSMNT_DUMBTIMR
) ||
1318 nmp
->nm_sent
< nmp
->nm_cwnd
)) {
1319 int connrequired
= (nmp
->nm_sotype
== SOCK_STREAM
);
1322 error
= nfs_sndlock(rep
);
1325 * Set the R_SENT before doing the send in case another thread
1326 * processes the reply before the nfs_send returns here
1329 if ((rep
->r_flags
& R_MUSTRESEND
) == 0) {
1330 FSDBG(531, rep
->r_xid
, rep
, nmp
->nm_sent
,
1332 nmp
->nm_sent
+= NFS_CWNDSCALE
;
1333 rep
->r_flags
|= R_SENT
;
1336 error
= mbuf_copym(m
, 0, MBUF_COPYALL
, MBUF_WAITOK
, &m2
);
1338 error
= nfs_send(nmp
->nm_so
, nmp
->nm_nam
, m2
, rep
);
1342 nmp
= vp
? VFSTONFS(vnode_mount(vp
)) : rep
->r_nmp
;
1345 nmp
->nm_sent
-= NFS_CWNDSCALE
;
1346 rep
->r_flags
&= ~R_SENT
;
1353 * Wait for the reply from our send or the timer's.
1355 if (!error
|| error
== EPIPE
)
1356 error
= nfs_reply(rep
);
1359 * RPC done, unlink the request.
1361 nfs_repdequeue(rep
);
1363 nmp
= vp
? VFSTONFS(vnode_mount(vp
)) : rep
->r_nmp
;
1366 * Decrement the outstanding request count.
1368 if (rep
->r_flags
& R_SENT
) {
1369 rep
->r_flags
&= ~R_SENT
; /* paranoia */
1371 FSDBG(531, rep
->r_xid
, rep
, nmp
->nm_sent
, nmp
->nm_cwnd
);
1372 nmp
->nm_sent
-= NFS_CWNDSCALE
;
1377 * If there was a successful reply and a tprintf msg.
1378 * tprintf a response.
1381 nfs_up(nmp
, procp
, NFSSTA_TIMEO
,
1382 (rep
->r_flags
& R_TPRINTFMSG
) ? "is alive again" : NULL
);
1389 mbuf_freem(rep
->r_mreq
);
1390 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1395 * break down the rpc header and check if ok
1397 nfsm_dissect(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
1398 if (*tl
++ == rpc_msgdenied
) {
1399 if (*tl
== rpc_mismatch
)
1401 else if ((nmp
->nm_flag
& NFSMNT_KERB
) && *tl
++ == rpc_autherr
) {
1404 error
= mbuf_setnext(mheadend
, NULL
);
1406 mbuf_freem(rep
->r_mreq
);
1409 printf("nfs_request: mbuf_setnext failed\n");
1415 mbuf_freem(rep
->r_mreq
);
1416 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1421 * Grab any Kerberos verifier, otherwise just throw it away.
1423 verf_type
= fxdr_unsigned(int, *tl
++);
1424 i
= fxdr_unsigned(int, *tl
);
1425 if ((nmp
->nm_flag
& NFSMNT_KERB
) && verf_type
== RPCAUTH_KERB4
) {
1426 error
= nfs_savenickauth(nmp
, cred
, i
, key
, &md
, &dpos
, mrep
);
1430 nfsm_adv(nfsm_rndup(i
));
1431 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
1434 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
1436 error
= fxdr_unsigned(int, *tl
);
1437 if ((nmp
->nm_flag
& NFSMNT_NFSV3
) &&
1438 error
== NFSERR_TRYLATER
) {
1442 waituntil
= now
.tv_sec
+ trylater_delay
;
1443 while (now
.tv_sec
< waituntil
) {
1444 tsleep((caddr_t
)&lbolt
, PSOCK
, "nfstrylater", 0);
1447 trylater_delay
*= 2;
1448 if (trylater_delay
> 60)
1449 trylater_delay
= 60;
1454 * If the File Handle was stale, invalidate the
1455 * lookup cache, just in case.
1457 if ((error
== ESTALE
) && vp
)
1459 if (nmp
->nm_flag
& NFSMNT_NFSV3
) {
1463 error
|= NFSERR_RETERR
;
1466 error
&= ~NFSERR_RETERR
;
1468 mbuf_freem(rep
->r_mreq
);
1469 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1476 mbuf_freem(rep
->r_mreq
);
1477 FSDBG_BOT(531, 0xf0f0f0f0, rep
->r_xid
, nmp
, rep
);
1481 error
= EPROTONOSUPPORT
;
1483 mbuf_freem(rep
->r_mreq
);
1484 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1488 #ifndef NFS_NOSERVER
1490 * Generate the rpc reply header
1491 * siz arg. is used to decide if adding a cluster is worthwhile
1494 nfs_rephead(siz
, nd
, slp
, err
, mrq
, mbp
, bposp
)
1496 struct nfsrv_descript
*nd
;
1497 struct nfssvc_sock
*slp
;
1510 * If this is a big reply, use a cluster else
1511 * try and leave leading space for the lower level headers.
1513 siz
+= RPC_REPLYSIZ
;
1514 if (siz
>= nfs_mbuf_minclsize
) {
1515 error
= mbuf_getpacket(MBUF_WAITOK
, &mreq
);
1517 error
= mbuf_gethdr(MBUF_WAITOK
, MBUF_TYPE_DATA
, &mreq
);
1520 /* unable to allocate packet */
1525 tl
= mbuf_data(mreq
);
1526 mlen
= 6 * NFSX_UNSIGNED
;
1527 if (siz
< nfs_mbuf_minclsize
) {
1528 /* leave space for lower level headers */
1529 tl
+= 80/sizeof(*tl
); /* XXX max_hdr? XXX */
1530 mbuf_setdata(mreq
, tl
, mlen
);
1532 mbuf_setlen(mreq
, mlen
);
1534 bpos
= ((caddr_t
)tl
) + mlen
;
1535 *tl
++ = txdr_unsigned(nd
->nd_retxid
);
1537 if (err
== ERPCMISMATCH
|| (err
& NFSERR_AUTHERR
)) {
1538 *tl
++ = rpc_msgdenied
;
1539 if (err
& NFSERR_AUTHERR
) {
1540 *tl
++ = rpc_autherr
;
1541 *tl
= txdr_unsigned(err
& ~NFSERR_AUTHERR
);
1542 mlen
-= NFSX_UNSIGNED
;
1543 mbuf_setlen(mreq
, mlen
);
1544 bpos
-= NFSX_UNSIGNED
;
1546 *tl
++ = rpc_mismatch
;
1547 *tl
++ = txdr_unsigned(RPC_VER2
);
1548 *tl
= txdr_unsigned(RPC_VER2
);
1551 *tl
++ = rpc_msgaccepted
;
1554 * For Kerberos authentication, we must send the nickname
1555 * verifier back, otherwise just RPCAUTH_NULL.
1557 if (nd
->nd_flag
& ND_KERBFULL
) {
1558 struct nfsuid
*nuidp
;
1559 struct timeval ktvin
, ktvout
;
1560 uid_t uid
= kauth_cred_getuid(nd
->nd_cr
);
1562 lck_rw_lock_shared(&slp
->ns_rwlock
);
1563 for (nuidp
= NUIDHASH(slp
, uid
)->lh_first
;
1564 nuidp
!= 0; nuidp
= nuidp
->nu_hash
.le_next
) {
1565 if (kauth_cred_getuid(nuidp
->nu_cr
) == uid
&&
1566 (!nd
->nd_nam2
|| netaddr_match(NU_NETFAM(nuidp
),
1567 &nuidp
->nu_haddr
, nd
->nd_nam2
)))
1572 txdr_unsigned(nuidp
->nu_timestamp
.tv_sec
- 1);
1574 txdr_unsigned(nuidp
->nu_timestamp
.tv_usec
);
1577 * Encrypt the timestamp in ecb mode using the
1584 *tl
++ = rpc_auth_kerb
;
1585 *tl
++ = txdr_unsigned(3 * NFSX_UNSIGNED
);
1586 *tl
= ktvout
.tv_sec
;
1587 nfsm_build(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
1588 *tl
++ = ktvout
.tv_usec
;
1589 *tl
++ = txdr_unsigned(kauth_cred_getuid(nuidp
->nu_cr
));
1594 lck_rw_done(&slp
->ns_rwlock
);
1601 *tl
= txdr_unsigned(RPC_PROGUNAVAIL
);
1604 *tl
= txdr_unsigned(RPC_PROGMISMATCH
);
1605 nfsm_build(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
1606 // XXX hard coded versions
1607 *tl
++ = txdr_unsigned(2);
1608 *tl
= txdr_unsigned(3);
1611 *tl
= txdr_unsigned(RPC_PROCUNAVAIL
);
1614 *tl
= txdr_unsigned(RPC_GARBAGE
);
1618 if (err
!= NFSERR_RETVOID
) {
1619 nfsm_build(tl
, u_long
*, NFSX_UNSIGNED
);
1621 *tl
= txdr_unsigned(nfsrv_errmap(nd
, err
));
1633 if (err
!= 0 && err
!= NFSERR_RETVOID
) {
1634 OSAddAtomic(1, (SInt32
*)&nfsstats
.srvrpc_errs
);
1640 #endif /* NFS_NOSERVER */
1644 * From FreeBSD 1.58, a Matt Dillon fix...
1645 * Flag a request as being about to terminate.
1646 * The nm_sent count is decremented now to avoid deadlocks when the process
1647 * in soreceive() hasn't yet managed to send its own request.
1650 nfs_softterm(struct nfsreq
*rep
)
1653 rep
->r_flags
|= R_SOFTTERM
;
1654 if (rep
->r_flags
& R_SENT
) {
1655 FSDBG(532, rep
->r_xid
, rep
, rep
->r_nmp
->nm_sent
,
1656 rep
->r_nmp
->nm_cwnd
);
1657 rep
->r_nmp
->nm_sent
-= NFS_CWNDSCALE
;
1658 rep
->r_flags
&= ~R_SENT
;
1663 nfs_timer_funnel(void * arg
)
1665 (void) thread_funnel_set(kernel_flock
, TRUE
);
1667 (void) thread_funnel_set(kernel_flock
, FALSE
);
1672 * Ensure rep isn't in use by the timer, then dequeue it.
1675 nfs_repdequeue(struct nfsreq
*rep
)
1678 while ((rep
->r_flags
& R_BUSY
)) {
1679 rep
->r_flags
|= R_WAITING
;
1680 tsleep(rep
, PSOCK
, "repdeq", 0);
1682 TAILQ_REMOVE(&nfs_reqq
, rep
, r_chain
);
1686 * Busy (lock) a nfsreq, used by the nfs timer to make sure it's not
1687 * free()'d out from under it.
1690 nfs_repbusy(struct nfsreq
*rep
)
1693 if ((rep
->r_flags
& R_BUSY
))
1694 panic("rep locked");
1695 rep
->r_flags
|= R_BUSY
;
1699 * Unbusy the nfsreq passed in, return the next nfsreq in the chain busied.
1701 static struct nfsreq
*
1702 nfs_repnext(struct nfsreq
*rep
)
1704 struct nfsreq
* nextrep
;
1709 * We need to get and busy the next req before signalling the
1710 * current one, otherwise wakeup() may block us and we'll race to
1711 * grab the next req.
1713 nextrep
= TAILQ_NEXT(rep
, r_chain
);
1714 if (nextrep
!= NULL
)
1715 nfs_repbusy(nextrep
);
1716 /* unbusy and signal. */
1717 rep
->r_flags
&= ~R_BUSY
;
1718 if ((rep
->r_flags
& R_WAITING
)) {
1719 rep
->r_flags
&= ~R_WAITING
;
1727 * Scan the nfsreq list and retranmit any requests that have timed out
1728 * To avoid retransmission attempts on STREAM sockets (in the future) make
1729 * sure to set the r_retry field to 0 (implies nm_retry == 0).
1732 nfs_timer(__unused
void *arg
)
1737 struct nfsmount
*nmp
;
1740 #ifndef NFS_NOSERVER
1741 struct nfssvc_sock
*slp
;
1743 #endif /* NFS_NOSERVER */
1744 int flags
, rexmit
, cwnd
, sent
;
1748 rep
= TAILQ_FIRST(&nfs_reqq
);
1752 for ( ; rep
!= NULL
; rep
= nfs_repnext(rep
)) {
1754 if (!nmp
) /* unmounted */
1756 if (rep
->r_mrep
|| (rep
->r_flags
& R_SOFTTERM
))
1758 if (nfs_sigintr(nmp
, rep
, rep
->r_procp
))
1760 if (nmp
->nm_tprintf_initial_delay
!= 0 &&
1761 (rep
->r_rexmit
> 2 || (rep
->r_flags
& R_RESENDERR
)) &&
1762 rep
->r_lastmsg
+ nmp
->nm_tprintf_delay
< now
.tv_sec
) {
1763 rep
->r_lastmsg
= now
.tv_sec
;
1764 nfs_down(rep
->r_nmp
, rep
->r_procp
, 0, NFSSTA_TIMEO
,
1766 rep
->r_flags
|= R_TPRINTFMSG
;
1767 if (!(nmp
->nm_state
& NFSSTA_MOUNTED
)) {
1768 /* we're not yet completely mounted and */
1769 /* we can't complete an RPC, so we fail */
1770 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpctimeouts
);
1775 if (rep
->r_rtt
>= 0) {
1777 if (nmp
->nm_flag
& NFSMNT_DUMBTIMR
)
1778 timeo
= nmp
->nm_timeo
;
1780 timeo
= NFS_RTO(nmp
, proct
[rep
->r_procnum
]);
1781 /* ensure 62.5 ms floor */
1782 while (16 * timeo
< hz
)
1784 if (nmp
->nm_timeouts
> 0)
1785 timeo
*= nfs_backoff
[nmp
->nm_timeouts
- 1];
1786 if (rep
->r_rtt
<= timeo
)
1788 if (nmp
->nm_timeouts
< 8)
1792 * Check for too many retransmits. This is never true for
1793 * 'hard' mounts because we set r_retry to NFS_MAXREXMIT + 1
1794 * and never allow r_rexmit to be more than NFS_MAXREXMIT.
1796 if (rep
->r_rexmit
>= rep
->r_retry
) { /* too many */
1797 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpctimeouts
);
1801 if (nmp
->nm_sotype
!= SOCK_DGRAM
) {
1802 if (++rep
->r_rexmit
> NFS_MAXREXMIT
)
1803 rep
->r_rexmit
= NFS_MAXREXMIT
;
1806 if ((so
= nmp
->nm_so
) == NULL
)
1810 * If there is enough space and the window allows..
1812 * Set r_rtt to -1 in case we fail to send it now.
1815 if (((nmp
->nm_flag
& NFSMNT_DUMBTIMR
) ||
1816 (rep
->r_flags
& R_SENT
) ||
1817 nmp
->nm_sent
< nmp
->nm_cwnd
) &&
1818 (mbuf_copym(rep
->r_mreq
, 0, MBUF_COPYALL
, MBUF_DONTWAIT
, &m
) == 0)){
1821 * Iff first send, start timing
1822 * else turn timing off, backoff timer
1823 * and divide congestion window by 2.
1824 * We update these *before* the send to avoid
1825 * racing against receiving the reply.
1826 * We save them so we can restore them on send error.
1828 flags
= rep
->r_flags
;
1829 rexmit
= rep
->r_rexmit
;
1830 cwnd
= nmp
->nm_cwnd
;
1831 sent
= nmp
->nm_sent
;
1833 if (rep
->r_flags
& R_SENT
) {
1834 rep
->r_flags
&= ~R_TIMING
;
1835 if (++rep
->r_rexmit
> NFS_MAXREXMIT
)
1836 rep
->r_rexmit
= NFS_MAXREXMIT
;
1838 if (nmp
->nm_cwnd
< NFS_CWNDSCALE
)
1839 nmp
->nm_cwnd
= NFS_CWNDSCALE
;
1840 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpcretries
);
1842 rep
->r_flags
|= R_SENT
;
1843 nmp
->nm_sent
+= NFS_CWNDSCALE
;
1845 FSDBG(535, xid
, rep
, nmp
->nm_sent
, nmp
->nm_cwnd
);
1847 bzero(&msg
, sizeof(msg
));
1848 if ((nmp
->nm_flag
& NFSMNT_NOCONN
) == NFSMNT_NOCONN
) {
1849 msg
.msg_name
= mbuf_data(nmp
->nm_nam
);
1850 msg
.msg_namelen
= mbuf_len(nmp
->nm_nam
);
1852 error
= sock_sendmbuf(so
, &msg
, m
, MSG_DONTWAIT
, NULL
);
1854 FSDBG(535, xid
, error
, sent
, cwnd
);
1857 if (error
== EWOULDBLOCK
) {
1858 rep
->r_flags
= flags
;
1859 rep
->r_rexmit
= rexmit
;
1860 nmp
->nm_cwnd
= cwnd
;
1861 nmp
->nm_sent
= sent
;
1865 if (NFSIGNORE_SOERROR(nmp
->nm_sotype
, error
)) {
1867 int optlen
= sizeof(clearerror
);
1868 sock_getsockopt(nmp
->nm_so
, SOL_SOCKET
, SO_ERROR
, &clearerror
, &optlen
);
1870 rep
->r_flags
= flags
| R_RESENDERR
;
1871 rep
->r_rexmit
= rexmit
;
1872 nmp
->nm_cwnd
= cwnd
;
1873 nmp
->nm_sent
= sent
;
1875 OSAddAtomic(-1, (SInt32
*)&nfsstats
.rpcretries
);
1882 #ifndef NFS_NOSERVER
1884 * Scan the write gathering queues for writes that need to be
1887 cur_usec
= (u_quad_t
)now
.tv_sec
* 1000000 + (u_quad_t
)now
.tv_usec
;
1888 lck_mtx_lock(nfsd_mutex
);
1889 TAILQ_FOREACH(slp
, &nfssvc_sockhead
, ns_chain
) {
1890 if (slp
->ns_wgtime
&& (slp
->ns_wgtime
<= cur_usec
))
1891 nfsrv_wakenfsd(slp
);
1893 while ((slp
= TAILQ_FIRST(&nfssvc_deadsockhead
))) {
1894 if ((slp
->ns_timestamp
+ 5) > now
.tv_sec
)
1896 TAILQ_REMOVE(&nfssvc_deadsockhead
, slp
, ns_chain
);
1899 lck_mtx_unlock(nfsd_mutex
);
1900 #endif /* NFS_NOSERVER */
1902 if (nfsbuffreeuptimestamp
+ 30 <= now
.tv_sec
) {
1904 * We haven't called nfs_buf_freeup() in a little while.
1905 * So, see if we can free up any stale/unused bufs now.
1910 timeout(nfs_timer_funnel
, (void *)0, nfs_ticks
);
1916 * Test for a termination condition pending on the process.
1917 * This is used to determine if we need to bail on a mount.
1918 * EIO is returned if there has been a soft timeout.
1919 * EINTR is returned if there is a signal pending that is not being ignored
1920 * and the mount is interruptable, or if we are a thread that is in the process
1921 * of cancellation (also SIGKILL posted).
1924 nfs_sigintr(nmp
, rep
, p
)
1925 struct nfsmount
*nmp
;
1929 sigset_t pending_sigs
;
1930 int context_good
= 0;
1931 struct nfsmount
*repnmp
;
1932 extern proc_t kernproc
;
1937 repnmp
= rep
->r_nmp
;
1938 /* we've had a forced unmount. */
1941 /* request has timed out on a 'soft' mount. */
1942 if (rep
->r_flags
& R_SOFTTERM
)
1945 * We're in the progress of a force unmount and there's
1946 * been a timeout we're dead and fail IO.
1948 if ((repnmp
->nm_state
& (NFSSTA_FORCE
|NFSSTA_TIMEO
)) ==
1949 (NFSSTA_FORCE
|NFSSTA_TIMEO
))
1951 /* Someone is unmounting us, go soft and mark it. */
1952 if (repnmp
->nm_mountp
->mnt_kern_flag
& MNTK_FRCUNMOUNT
) {
1953 repnmp
->nm_flag
|= NFSMNT_SOFT
;
1954 nmp
->nm_state
|= NFSSTA_FORCE
;
1957 * If the mount is hung and we've requested not to hang
1958 * on remote filesystems, then bail now.
1960 if (p
!= NULL
&& (proc_noremotehang(p
)) != 0 &&
1961 (repnmp
->nm_state
& NFSSTA_TIMEO
) != 0)
1964 /* XXX: is this valid? this probably should be an assertion. */
1968 /* Is this thread belongs to kernel task; then abort check is not needed */
1969 if ((current_proc() != kernproc
) && current_thread_aborted()) {
1972 /* mask off thread and process blocked signals. */
1974 pending_sigs
= proc_pendingsignals(p
, NFSINT_SIGMASK
);
1975 if (pending_sigs
&& (nmp
->nm_flag
& NFSMNT_INT
) != 0)
1981 * Lock a socket against others.
1982 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
1983 * and also to avoid race conditions between the processes with nfs requests
1984 * in progress when a reconnect is necessary.
1992 int error
, slpflag
= 0, slptimeo
= 0;
1994 if (rep
->r_nmp
== NULL
)
1996 statep
= &rep
->r_nmp
->nm_state
;
1999 if (rep
->r_nmp
->nm_flag
& NFSMNT_INT
)
2001 while (*statep
& NFSSTA_SNDLOCK
) {
2002 error
= nfs_sigintr(rep
->r_nmp
, rep
, p
);
2005 *statep
|= NFSSTA_WANTSND
;
2006 if (p
!= NULL
&& (proc_noremotehang(p
)) != 0)
2008 tsleep((caddr_t
)statep
, slpflag
| (PZERO
- 1), "nfsndlck", slptimeo
);
2009 if (slpflag
== PCATCH
) {
2014 * Make sure while we slept that the mountpoint didn't go away.
2015 * nfs_sigintr and callers expect it in tact.
2018 return (ENXIO
); /* don't have lock until out of loop */
2020 *statep
|= NFSSTA_SNDLOCK
;
2025 * Unlock the stream socket for others.
2033 if (rep
->r_nmp
== NULL
)
2035 statep
= &rep
->r_nmp
->nm_state
;
2036 if ((*statep
& NFSSTA_SNDLOCK
) == 0)
2037 panic("nfs sndunlock");
2038 *statep
&= ~NFSSTA_SNDLOCK
;
2039 if (*statep
& NFSSTA_WANTSND
) {
2040 *statep
&= ~NFSSTA_WANTSND
;
2041 wakeup((caddr_t
)statep
);
2046 nfs_rcvlock(struct nfsreq
*rep
)
2049 int error
, slpflag
, slptimeo
= 0;
2051 /* make sure we still have our mountpoint */
2053 if (rep
->r_mrep
!= NULL
)
2058 statep
= &rep
->r_nmp
->nm_state
;
2059 FSDBG_TOP(534, rep
->r_xid
, rep
, rep
->r_nmp
, *statep
);
2060 if (rep
->r_nmp
->nm_flag
& NFSMNT_INT
)
2064 while (*statep
& NFSSTA_RCVLOCK
) {
2065 if ((error
= nfs_sigintr(rep
->r_nmp
, rep
, rep
->r_procp
))) {
2066 FSDBG_BOT(534, rep
->r_xid
, rep
, rep
->r_nmp
, 0x100);
2068 } else if (rep
->r_mrep
!= NULL
) {
2070 * Don't bother sleeping if reply already arrived
2072 FSDBG_BOT(534, rep
->r_xid
, rep
, rep
->r_nmp
, 0x101);
2075 FSDBG(534, rep
->r_xid
, rep
, rep
->r_nmp
, 0x102);
2076 *statep
|= NFSSTA_WANTRCV
;
2078 * We need to poll if we're P_NOREMOTEHANG so that we
2079 * call nfs_sigintr periodically above.
2081 if (rep
->r_procp
!= NULL
&&
2082 (proc_noremotehang(rep
->r_procp
)) != 0)
2084 tsleep((caddr_t
)statep
, slpflag
| (PZERO
- 1), "nfsrcvlk", slptimeo
);
2085 if (slpflag
== PCATCH
) {
2090 * Make sure while we slept that the mountpoint didn't go away.
2091 * nfs_sigintr and caller nfs_reply expect it intact.
2094 FSDBG_BOT(534, rep
->r_xid
, rep
, rep
->r_nmp
, 0x103);
2095 return (ENXIO
); /* don't have lock until out of loop */
2099 * nfs_reply will handle it if reply already arrived.
2100 * (We may have slept or been preempted).
2102 FSDBG_BOT(534, rep
->r_xid
, rep
, rep
->r_nmp
, *statep
);
2103 *statep
|= NFSSTA_RCVLOCK
;
2108 * Unlock the stream socket for others.
2111 nfs_rcvunlock(struct nfsreq
*rep
)
2115 if (rep
->r_nmp
== NULL
)
2117 statep
= &rep
->r_nmp
->nm_state
;
2119 FSDBG(533, statep
, *statep
, 0, 0);
2120 if ((*statep
& NFSSTA_RCVLOCK
) == 0)
2121 panic("nfs rcvunlock");
2122 *statep
&= ~NFSSTA_RCVLOCK
;
2123 if (*statep
& NFSSTA_WANTRCV
) {
2124 *statep
&= ~NFSSTA_WANTRCV
;
2125 wakeup((caddr_t
)statep
);
2130 #ifndef NFS_NOSERVER
2132 * Socket upcall routine for the nfsd sockets.
2133 * The caddr_t arg is a pointer to the "struct nfssvc_sock".
2134 * Essentially do as much as possible non-blocking, else punt and it will
2135 * be called with MBUF_WAITOK from an nfsd.
2138 nfsrv_rcv(socket_t so
, caddr_t arg
, int waitflag
)
2140 struct nfssvc_sock
*slp
= (struct nfssvc_sock
*)arg
;
2142 if (!nfs_numnfsd
|| !(slp
->ns_flag
& SLP_VALID
))
2145 lck_rw_lock_exclusive(&slp
->ns_rwlock
);
2146 nfsrv_rcv_locked(so
, slp
, waitflag
);
2147 /* Note: ns_rwlock gets dropped when called with MBUF_DONTWAIT */
2150 nfsrv_rcv_locked(socket_t so
, struct nfssvc_sock
*slp
, int waitflag
)
2152 mbuf_t m
, mp
, mhck
, m2
;
2153 int ns_flag
=0, error
;
2157 if ((slp
->ns_flag
& SLP_VALID
) == 0) {
2158 if (waitflag
== MBUF_DONTWAIT
)
2159 lck_rw_done(&slp
->ns_rwlock
);
2165 * Define this to test for nfsds handling this under heavy load.
2167 if (waitflag
== MBUF_DONTWAIT
) {
2168 ns_flag
= SLP_NEEDQ
;
2172 if (slp
->ns_sotype
== SOCK_STREAM
) {
2174 * If there are already records on the queue, defer soreceive()
2175 * to an nfsd so that there is feedback to the TCP layer that
2176 * the nfs servers are heavily loaded.
2178 if (slp
->ns_rec
&& waitflag
== MBUF_DONTWAIT
) {
2179 ns_flag
= SLP_NEEDQ
;
2186 bytes_read
= 1000000000;
2187 error
= sock_receivembuf(so
, NULL
, &mp
, MSG_DONTWAIT
, &bytes_read
);
2188 if (error
|| mp
== NULL
) {
2189 if (error
== EWOULDBLOCK
)
2190 ns_flag
= SLP_NEEDQ
;
2192 ns_flag
= SLP_DISCONN
;
2196 if (slp
->ns_rawend
) {
2197 if ((error
= mbuf_setnext(slp
->ns_rawend
, m
)))
2198 panic("nfsrv_rcv: mbuf_setnext failed %d\n", error
);
2199 slp
->ns_cc
+= bytes_read
;
2202 slp
->ns_cc
= bytes_read
;
2204 while ((m2
= mbuf_next(m
)))
2209 * Now try and parse record(s) out of the raw stream data.
2211 error
= nfsrv_getstream(slp
, waitflag
);
2214 ns_flag
= SLP_DISCONN
;
2216 ns_flag
= SLP_NEEDQ
;
2219 struct sockaddr_storage nam
;
2221 bzero(&msg
, sizeof(msg
));
2222 msg
.msg_name
= (caddr_t
)&nam
;
2223 msg
.msg_namelen
= sizeof(nam
);
2226 bytes_read
= 1000000000;
2227 error
= sock_receivembuf(so
, &msg
, &mp
, MSG_DONTWAIT
| MSG_NEEDSA
, &bytes_read
);
2229 if (msg
.msg_name
&& (mbuf_get(MBUF_WAITOK
, MBUF_TYPE_SONAME
, &mhck
) == 0)) {
2230 mbuf_setlen(mhck
, nam
.ss_len
);
2231 bcopy(&nam
, mbuf_data(mhck
), nam
.ss_len
);
2233 if (mbuf_setnext(m
, mp
)) {
2234 /* trouble... just drop it */
2235 printf("nfsrv_rcv: mbuf_setnext failed\n");
2243 mbuf_setnextpkt(slp
->ns_recend
, m
);
2247 mbuf_setnextpkt(m
, NULL
);
2252 * This may be needed in the future to support
2253 * non-byte-stream connection-oriented protocols
2257 * This (slp->ns_sotype == SOCK_STREAM) should really
2258 * be a check for PR_CONNREQUIRED.
2260 if ((slp
->ns_sotype
== SOCK_STREAM
)
2261 && error
!= EWOULDBLOCK
) {
2262 ns_flag
= SLP_DISCONN
;
2271 * Now try and process the request records, non-blocking.
2275 slp
->ns_flag
|= ns_flag
;
2276 if (waitflag
== MBUF_DONTWAIT
) {
2277 int wake
= (slp
->ns_rec
|| (slp
->ns_flag
& (SLP_NEEDQ
| SLP_DISCONN
)));
2278 lck_rw_done(&slp
->ns_rwlock
);
2279 if (wake
&& nfs_numnfsd
) {
2280 lck_mtx_lock(nfsd_mutex
);
2281 nfsrv_wakenfsd(slp
);
2282 lck_mtx_unlock(nfsd_mutex
);
2288 * Try and extract an RPC request from the mbuf data list received on a
2289 * stream socket. The "waitflag" argument indicates whether or not it
2293 nfsrv_getstream(slp
, waitflag
)
2294 struct nfssvc_sock
*slp
;
2298 char *cp1
, *cp2
, *mdata
;
2299 int len
, mlen
, error
;
2300 mbuf_t om
, m2
, recm
;
2303 if (slp
->ns_flag
& SLP_GETSTREAM
)
2304 panic("nfs getstream");
2305 slp
->ns_flag
|= SLP_GETSTREAM
;
2307 if (slp
->ns_reclen
== 0) {
2308 if (slp
->ns_cc
< NFSX_UNSIGNED
) {
2309 slp
->ns_flag
&= ~SLP_GETSTREAM
;
2313 mdata
= mbuf_data(m
);
2315 if (mlen
>= NFSX_UNSIGNED
) {
2316 bcopy(mdata
, (caddr_t
)&recmark
, NFSX_UNSIGNED
);
2317 mdata
+= NFSX_UNSIGNED
;
2318 mlen
-= NFSX_UNSIGNED
;
2319 mbuf_setdata(m
, mdata
, mlen
);
2321 cp1
= (caddr_t
)&recmark
;
2323 while (cp1
< ((caddr_t
)&recmark
) + NFSX_UNSIGNED
) {
2331 mbuf_setdata(m
, cp2
, mlen
);
2334 slp
->ns_cc
-= NFSX_UNSIGNED
;
2335 recmark
= ntohl(recmark
);
2336 slp
->ns_reclen
= recmark
& ~0x80000000;
2337 if (recmark
& 0x80000000)
2338 slp
->ns_flag
|= SLP_LASTFRAG
;
2340 slp
->ns_flag
&= ~SLP_LASTFRAG
;
2341 if (slp
->ns_reclen
< NFS_MINPACKET
|| slp
->ns_reclen
> NFS_MAXPACKET
) {
2342 slp
->ns_flag
&= ~SLP_GETSTREAM
;
2348 * Now get the record part.
2350 * Note that slp->ns_reclen may be 0. Linux sometimes
2351 * generates 0-length RPCs
2354 if (slp
->ns_cc
== slp
->ns_reclen
) {
2356 slp
->ns_raw
= slp
->ns_rawend
= NULL
;
2357 slp
->ns_cc
= slp
->ns_reclen
= 0;
2358 } else if (slp
->ns_cc
> slp
->ns_reclen
) {
2362 mdata
= mbuf_data(m
);
2364 while (len
< slp
->ns_reclen
) {
2365 if ((len
+ mlen
) > slp
->ns_reclen
) {
2366 if (mbuf_copym(m
, 0, slp
->ns_reclen
- len
, waitflag
, &m2
)) {
2367 slp
->ns_flag
&= ~SLP_GETSTREAM
;
2368 return (EWOULDBLOCK
);
2371 if (mbuf_setnext(om
, m2
)) {
2372 /* trouble... just drop it */
2373 printf("nfsrv_getstream: mbuf_setnext failed\n");
2375 slp
->ns_flag
&= ~SLP_GETSTREAM
;
2376 return (EWOULDBLOCK
);
2382 mdata
+= slp
->ns_reclen
- len
;
2383 mlen
-= slp
->ns_reclen
- len
;
2384 mbuf_setdata(m
, mdata
, mlen
);
2385 len
= slp
->ns_reclen
;
2386 } else if ((len
+ mlen
) == slp
->ns_reclen
) {
2391 if (mbuf_setnext(om
, NULL
)) {
2392 printf("nfsrv_getstream: mbuf_setnext failed 2\n");
2393 slp
->ns_flag
&= ~SLP_GETSTREAM
;
2394 return (EWOULDBLOCK
);
2397 mdata
= mbuf_data(m
);
2403 mdata
= mbuf_data(m
);
2410 slp
->ns_flag
&= ~SLP_GETSTREAM
;
2415 * Accumulate the fragments into a record.
2417 if (slp
->ns_frag
== NULL
) {
2418 slp
->ns_frag
= recm
;
2421 while ((m2
= mbuf_next(m
)))
2423 if ((error
= mbuf_setnext(m
, recm
)))
2424 panic("nfsrv_getstream: mbuf_setnext failed 3, %d\n", error
);
2426 if (slp
->ns_flag
& SLP_LASTFRAG
) {
2428 mbuf_setnextpkt(slp
->ns_recend
, slp
->ns_frag
);
2430 slp
->ns_rec
= slp
->ns_frag
;
2431 slp
->ns_recend
= slp
->ns_frag
;
2432 slp
->ns_frag
= NULL
;
2438 * Parse an RPC header.
2441 nfsrv_dorec(slp
, nfsd
, ndp
)
2442 struct nfssvc_sock
*slp
;
2444 struct nfsrv_descript
**ndp
;
2448 struct nfsrv_descript
*nd
;
2452 if ((slp
->ns_flag
& SLP_VALID
) == 0 || (slp
->ns_rec
== NULL
))
2454 MALLOC_ZONE(nd
, struct nfsrv_descript
*,
2455 sizeof (struct nfsrv_descript
), M_NFSRVDESC
, M_WAITOK
);
2459 slp
->ns_rec
= mbuf_nextpkt(m
);
2461 mbuf_setnextpkt(m
, NULL
);
2463 slp
->ns_recend
= NULL
;
2464 if (mbuf_type(m
) == MBUF_TYPE_SONAME
) {
2467 if ((error
= mbuf_setnext(nam
, NULL
)))
2468 panic("nfsrv_dorec: mbuf_setnext failed %d\n", error
);
2471 nd
->nd_md
= nd
->nd_mrep
= m
;
2473 nd
->nd_dpos
= mbuf_data(m
);
2474 error
= nfs_getreq(nd
, nfsd
, TRUE
);
2478 FREE_ZONE((caddr_t
)nd
, sizeof *nd
, M_NFSRVDESC
);
2487 * Parse an RPC request
2489 * - fill in the cred struct.
2492 nfs_getreq(nd
, nfsd
, has_header
)
2493 struct nfsrv_descript
*nd
;
2501 caddr_t dpos
, cp2
, cp
;
2502 u_long nfsvers
, auth_type
;
2504 int error
= 0, ticklen
;
2506 struct nfsuid
*nuidp
;
2510 struct ucred temp_cred
;
2511 struct timeval tvin
, tvout
, now
;
2512 char uio_buf
[ UIO_SIZEOF(1) ];
2513 #if 0 /* until encrypted keys are implemented */
2514 NFSKERBKEYSCHED_T keys
; /* stores key schedule */
2523 nfsm_dissect(tl
, u_long
*, 10 * NFSX_UNSIGNED
);
2524 nd
->nd_retxid
= fxdr_unsigned(u_long
, *tl
++);
2525 if (*tl
++ != rpc_call
) {
2530 nfsm_dissect(tl
, u_long
*, 8 * NFSX_UNSIGNED
);
2533 if (*tl
++ != rpc_vers
) {
2534 nd
->nd_repstat
= ERPCMISMATCH
;
2535 nd
->nd_procnum
= NFSPROC_NOOP
;
2538 if (*tl
!= nfs_prog
) {
2539 nd
->nd_repstat
= EPROGUNAVAIL
;
2540 nd
->nd_procnum
= NFSPROC_NOOP
;
2544 nfsvers
= fxdr_unsigned(u_long
, *tl
++);
2545 if ((nfsvers
< NFS_VER2
) || (nfsvers
> NFS_VER3
)) {
2546 nd
->nd_repstat
= EPROGMISMATCH
;
2547 nd
->nd_procnum
= NFSPROC_NOOP
;
2550 else if (nfsvers
== NFS_VER3
)
2551 nd
->nd_flag
= ND_NFSV3
;
2552 nd
->nd_procnum
= fxdr_unsigned(u_long
, *tl
++);
2553 if (nd
->nd_procnum
== NFSPROC_NULL
)
2555 if ((nd
->nd_procnum
>= NFS_NPROCS
) ||
2556 (!nd
->nd_flag
&& nd
->nd_procnum
> NFSV2PROC_STATFS
)) {
2557 nd
->nd_repstat
= EPROCUNAVAIL
;
2558 nd
->nd_procnum
= NFSPROC_NOOP
;
2561 if ((nd
->nd_flag
& ND_NFSV3
) == 0)
2562 nd
->nd_procnum
= nfsv3_procid
[nd
->nd_procnum
];
2564 len
= fxdr_unsigned(int, *tl
++);
2565 if (len
< 0 || len
> RPCAUTH_MAXSIZ
) {
2570 nd
->nd_flag
&= ~ND_KERBAUTH
;
2572 * Handle auth_unix or auth_kerb.
2574 if (auth_type
== rpc_auth_unix
) {
2575 len
= fxdr_unsigned(int, *++tl
);
2576 if (len
< 0 || len
> NFS_MAXNAMLEN
) {
2580 bzero(&temp_cred
, sizeof(temp_cred
));
2581 nfsm_adv(nfsm_rndup(len
));
2582 nfsm_dissect(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
2583 user_id
= fxdr_unsigned(uid_t
, *tl
++);
2584 group_id
= fxdr_unsigned(gid_t
, *tl
++);
2585 temp_cred
.cr_groups
[0] = group_id
;
2586 len
= fxdr_unsigned(int, *tl
);
2587 if (len
< 0 || len
> RPCAUTH_UNIXGIDS
) {
2591 nfsm_dissect(tl
, u_long
*, (len
+ 2) * NFSX_UNSIGNED
);
2592 for (i
= 1; i
<= len
; i
++)
2594 temp_cred
.cr_groups
[i
] = fxdr_unsigned(gid_t
, *tl
++);
2597 ngroups
= (len
>= NGROUPS
) ? NGROUPS
: (len
+ 1);
2599 nfsrvw_sort(&temp_cred
.cr_groups
[0], ngroups
);
2600 len
= fxdr_unsigned(int, *++tl
);
2601 if (len
< 0 || len
> RPCAUTH_MAXSIZ
) {
2605 temp_cred
.cr_uid
= user_id
;
2606 temp_cred
.cr_ngroups
= ngroups
;
2607 nd
->nd_cr
= kauth_cred_create(&temp_cred
);
2608 if (nd
->nd_cr
== NULL
) {
2609 nd
->nd_repstat
= ENOMEM
;
2610 nd
->nd_procnum
= NFSPROC_NOOP
;
2614 nfsm_adv(nfsm_rndup(len
));
2615 } else if (auth_type
== rpc_auth_kerb
) {
2616 switch (fxdr_unsigned(int, *tl
++)) {
2617 case RPCAKN_FULLNAME
:
2618 ticklen
= fxdr_unsigned(int, *tl
);
2619 *((u_long
*)nfsd
->nfsd_authstr
) = *tl
;
2620 uiop
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_READ
,
2621 &uio_buf
[0], sizeof(uio_buf
));
2623 nd
->nd_repstat
= ENOMEM
;
2624 nd
->nd_procnum
= NFSPROC_NOOP
;
2628 // LP64todo - fix this
2629 nfsd
->nfsd_authlen
= (nfsm_rndup(ticklen
) + (NFSX_UNSIGNED
* 2));
2630 if ((nfsm_rndup(ticklen
) + NFSX_UNSIGNED
) > (len
- 2 * NFSX_UNSIGNED
)) {
2634 uio_addiov(uiop
, CAST_USER_ADDR_T(&nfsd
->nfsd_authstr
[4]), RPCAUTH_MAXSIZ
- 4);
2635 // LP64todo - fix this
2636 nfsm_mtouio(uiop
, uio_resid(uiop
));
2637 nfsm_dissect(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
2638 if (*tl
++ != rpc_auth_kerb
||
2639 fxdr_unsigned(int, *tl
) != 4 * NFSX_UNSIGNED
) {
2640 printf("Bad kerb verifier\n");
2641 nd
->nd_repstat
= (NFSERR_AUTHERR
|AUTH_BADVERF
);
2642 nd
->nd_procnum
= NFSPROC_NOOP
;
2645 nfsm_dissect(cp
, caddr_t
, 4 * NFSX_UNSIGNED
);
2647 if (fxdr_unsigned(int, *tl
) != RPCAKN_FULLNAME
) {
2648 printf("Not fullname kerb verifier\n");
2649 nd
->nd_repstat
= (NFSERR_AUTHERR
|AUTH_BADVERF
);
2650 nd
->nd_procnum
= NFSPROC_NOOP
;
2653 cp
+= NFSX_UNSIGNED
;
2654 bcopy(cp
, nfsd
->nfsd_verfstr
, 3 * NFSX_UNSIGNED
);
2655 nfsd
->nfsd_verflen
= 3 * NFSX_UNSIGNED
;
2656 nd
->nd_flag
|= ND_KERBFULL
;
2657 nfsd
->nfsd_flag
|= NFSD_NEEDAUTH
;
2659 case RPCAKN_NICKNAME
:
2660 if (len
!= 2 * NFSX_UNSIGNED
) {
2661 printf("Kerb nickname short\n");
2662 nd
->nd_repstat
= (NFSERR_AUTHERR
|AUTH_BADCRED
);
2663 nd
->nd_procnum
= NFSPROC_NOOP
;
2666 nickuid
= fxdr_unsigned(uid_t
, *tl
);
2667 nfsm_dissect(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
2668 if (*tl
++ != rpc_auth_kerb
||
2669 fxdr_unsigned(int, *tl
) != 3 * NFSX_UNSIGNED
) {
2670 printf("Kerb nick verifier bad\n");
2671 nd
->nd_repstat
= (NFSERR_AUTHERR
|AUTH_BADVERF
);
2672 nd
->nd_procnum
= NFSPROC_NOOP
;
2675 nfsm_dissect(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
2676 tvin
.tv_sec
= *tl
++;
2679 for (nuidp
= NUIDHASH(nfsd
->nfsd_slp
,nickuid
)->lh_first
;
2680 nuidp
!= 0; nuidp
= nuidp
->nu_hash
.le_next
) {
2681 if (kauth_cred_getuid(nuidp
->nu_cr
) == nickuid
&&
2683 netaddr_match(NU_NETFAM(nuidp
),
2684 &nuidp
->nu_haddr
, nd
->nd_nam2
)))
2689 (NFSERR_AUTHERR
|AUTH_REJECTCRED
);
2690 nd
->nd_procnum
= NFSPROC_NOOP
;
2695 * Now, decrypt the timestamp using the session key
2702 tvout
.tv_sec
= fxdr_unsigned(long, tvout
.tv_sec
);
2703 tvout
.tv_usec
= fxdr_unsigned(long, tvout
.tv_usec
);
2705 if (nuidp
->nu_expire
< now
.tv_sec
||
2706 nuidp
->nu_timestamp
.tv_sec
> tvout
.tv_sec
||
2707 (nuidp
->nu_timestamp
.tv_sec
== tvout
.tv_sec
&&
2708 nuidp
->nu_timestamp
.tv_usec
> tvout
.tv_usec
)) {
2709 nuidp
->nu_expire
= 0;
2711 (NFSERR_AUTHERR
|AUTH_REJECTVERF
);
2712 nd
->nd_procnum
= NFSPROC_NOOP
;
2715 bzero(&temp_cred
, sizeof(temp_cred
));
2716 ngroups
= nuidp
->nu_cr
->cr_ngroups
;
2717 for (i
= 0; i
< ngroups
; i
++)
2718 temp_cred
.cr_groups
[i
] = nuidp
->nu_cr
->cr_groups
[i
];
2720 nfsrvw_sort(&temp_cred
.cr_groups
[0], ngroups
);
2722 temp_cred
.cr_uid
= kauth_cred_getuid(nuidp
->nu_cr
);
2723 temp_cred
.cr_ngroups
= ngroups
;
2724 nd
->nd_cr
= kauth_cred_create(&temp_cred
);
2726 nd
->nd_repstat
= ENOMEM
;
2727 nd
->nd_procnum
= NFSPROC_NOOP
;
2730 nd
->nd_flag
|= ND_KERBNICK
;
2733 nd
->nd_repstat
= (NFSERR_AUTHERR
| AUTH_REJECTCRED
);
2734 nd
->nd_procnum
= NFSPROC_NOOP
;
2743 kauth_cred_rele(nd
->nd_cr
);
2748 * Search for a sleeping nfsd and wake it up.
2749 * SIDE EFFECT: If none found, set NFSD_CHECKSLP flag, so that one of the
2750 * running nfsds will go look for the work in the nfssvc_sock list.
2751 * Note: Must be called with nfsd_mutex held.
2754 nfsrv_wakenfsd(struct nfssvc_sock
*slp
)
2758 if ((slp
->ns_flag
& SLP_VALID
) == 0)
2761 lck_rw_lock_exclusive(&slp
->ns_rwlock
);
2764 TAILQ_FOREACH(nd
, &nfsd_head
, nfsd_chain
) {
2765 if (nd
->nfsd_flag
& NFSD_WAITING
) {
2766 nd
->nfsd_flag
&= ~NFSD_WAITING
;
2768 panic("nfsd wakeup");
2771 lck_rw_done(&slp
->ns_rwlock
);
2772 wakeup((caddr_t
)nd
);
2778 slp
->ns_flag
|= SLP_DOREC
;
2780 lck_rw_done(&slp
->ns_rwlock
);
2782 nfsd_head_flag
|= NFSD_CHECKSLP
;
2784 #endif /* NFS_NOSERVER */
2795 tpr
= tprintf_open(p
);
2799 tprintf(tpr
, "nfs server %s: %s, error %d\n", server
, msg
,
2802 tprintf(tpr
, "nfs server %s: %s\n", server
, msg
);
2808 nfs_down(nmp
, proc
, error
, flags
, msg
)
2809 struct nfsmount
*nmp
;
2816 if ((flags
& NFSSTA_TIMEO
) && !(nmp
->nm_state
& NFSSTA_TIMEO
)) {
2817 vfs_event_signal(&vfs_statfs(nmp
->nm_mountp
)->f_fsid
, VQ_NOTRESP
, 0);
2818 nmp
->nm_state
|= NFSSTA_TIMEO
;
2820 if ((flags
& NFSSTA_LOCKTIMEO
) && !(nmp
->nm_state
& NFSSTA_LOCKTIMEO
)) {
2821 vfs_event_signal(&vfs_statfs(nmp
->nm_mountp
)->f_fsid
, VQ_NOTRESPLOCK
, 0);
2822 nmp
->nm_state
|= NFSSTA_LOCKTIMEO
;
2824 nfs_msg(proc
, vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, msg
, error
);
2828 nfs_up(nmp
, proc
, flags
, msg
)
2829 struct nfsmount
*nmp
;
2837 nfs_msg(proc
, vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, msg
, 0);
2838 if ((flags
& NFSSTA_TIMEO
) && (nmp
->nm_state
& NFSSTA_TIMEO
)) {
2839 nmp
->nm_state
&= ~NFSSTA_TIMEO
;
2840 vfs_event_signal(&vfs_statfs(nmp
->nm_mountp
)->f_fsid
, VQ_NOTRESP
, 1);
2842 if ((flags
& NFSSTA_LOCKTIMEO
) && (nmp
->nm_state
& NFSSTA_LOCKTIMEO
)) {
2843 nmp
->nm_state
&= ~NFSSTA_LOCKTIMEO
;
2844 vfs_event_signal(&vfs_statfs(nmp
->nm_mountp
)->f_fsid
, VQ_NOTRESPLOCK
, 1);