2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1989, 1991, 1993, 1995
31 * The Regents of the University of California. All rights reserved.
33 * This code is derived from software contributed to Berkeley by
34 * Rick Macklem at The University of Guelph.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * @(#)nfs_socket.c 8.5 (Berkeley) 3/30/95
65 * FreeBSD-Id: nfs_socket.c,v 1.30 1997/10/28 15:59:07 bde Exp $
69 * Socket operations for use by nfs
72 #include <sys/param.h>
73 #include <sys/systm.h>
75 #include <sys/kauth.h>
76 #include <sys/mount_internal.h>
77 #include <sys/kernel.h>
78 #include <sys/kpi_mbuf.h>
79 #include <sys/malloc.h>
80 #include <sys/vnode.h>
81 #include <sys/domain.h>
82 #include <sys/protosw.h>
83 #include <sys/socket.h>
84 #include <sys/syslog.h>
85 #include <sys/tprintf.h>
86 #include <sys/uio_internal.h>
87 #include <libkern/OSAtomic.h>
90 #include <kern/clock.h>
91 #include <kern/task.h>
92 #include <kern/thread.h>
95 #include <netinet/in.h>
96 #include <netinet/tcp.h>
98 #include <nfs/rpcv2.h>
99 #include <nfs/nfsproto.h>
101 #include <nfs/xdr_subs.h>
102 #include <nfs/nfsm_subs.h>
103 #include <nfs/nfsmount.h>
104 #include <nfs/nfsnode.h>
105 #include <nfs/nfsrtt.h>
107 #include <sys/kdebug.h>
109 #define FSDBG(A, B, C, D, E) \
110 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_NONE, \
111 (int)(B), (int)(C), (int)(D), (int)(E), 0)
112 #define FSDBG_TOP(A, B, C, D, E) \
113 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_START, \
114 (int)(B), (int)(C), (int)(D), (int)(E), 0)
115 #define FSDBG_BOT(A, B, C, D, E) \
116 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_END, \
117 (int)(B), (int)(C), (int)(D), (int)(E), 0)
120 * Estimate rto for an nfs rpc sent via. an unreliable datagram.
121 * Use the mean and mean deviation of rtt for the appropriate type of rpc
122 * for the frequent rpcs and a default for the others.
123 * The justification for doing "other" this way is that these rpcs
124 * happen so infrequently that timer est. would probably be stale.
125 * Also, since many of these rpcs are
126 * non-idempotent, a conservative timeout is desired.
127 * getattr, lookup - A+2D
131 #define NFS_RTO(n, t) \
132 ((t) == 0 ? (n)->nm_timeo : \
134 (((((n)->nm_srtt[t-1] + 3) >> 2) + (n)->nm_sdrtt[t-1] + 1) >> 1) : \
135 ((((n)->nm_srtt[t-1] + 7) >> 3) + (n)->nm_sdrtt[t-1] + 1)))
136 #define NFS_SRTT(r) (r)->r_nmp->nm_srtt[proct[(r)->r_procnum] - 1]
137 #define NFS_SDRTT(r) (r)->r_nmp->nm_sdrtt[proct[(r)->r_procnum] - 1]
139 * External data, mostly RPC constants in XDR form
141 extern u_long rpc_reply
, rpc_msgdenied
, rpc_mismatch
, rpc_vers
, rpc_auth_unix
,
142 rpc_msgaccepted
, rpc_call
, rpc_autherr
,
144 extern u_long nfs_prog
;
145 extern struct nfsstats nfsstats
;
146 extern int nfsv3_procid
[NFS_NPROCS
];
147 extern int nfs_ticks
;
148 extern u_long nfs_xidwrap
;
151 * Defines which timer to use for the procnum.
158 static int proct
[NFS_NPROCS
] = {
159 0, 1, 0, 2, 1, 3, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0
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.
175 #define NFS_CWNDSCALE 256
176 #define NFS_MAXCWND (NFS_CWNDSCALE * 32)
177 static int nfs_backoff
[8] = { 2, 4, 8, 16, 32, 64, 128, 256, };
179 struct nfsrtt nfsrtt
;
181 static int nfs_rcvlock(struct nfsreq
*);
182 static void nfs_rcvunlock(struct nfsreq
*);
183 static int nfs_receive(struct nfsreq
*rep
, mbuf_t
*mp
);
184 static int nfs_reconnect(struct nfsreq
*rep
);
185 static void nfs_repdequeue(struct nfsreq
*rep
);
188 boolean_t
current_thread_aborted(void);
189 kern_return_t
thread_terminate(thread_t
);
192 static int nfsrv_getstream(struct nfssvc_sock
*,int);
194 int (*nfsrv3_procs
[NFS_NPROCS
])(struct nfsrv_descript
*nd
,
195 struct nfssvc_sock
*slp
,
222 #endif /* NFS_NOSERVER */
226 * attempt to bind a socket to a reserved port
229 nfs_bind_resv(struct nfsmount
*nmp
)
231 socket_t so
= nmp
->nm_so
;
232 struct sockaddr_in sin
;
239 sin
.sin_len
= sizeof (struct sockaddr_in
);
240 sin
.sin_family
= AF_INET
;
241 sin
.sin_addr
.s_addr
= INADDR_ANY
;
242 tport
= IPPORT_RESERVED
- 1;
243 sin
.sin_port
= htons(tport
);
245 while (((error
= sock_bind(so
, (struct sockaddr
*) &sin
)) == EADDRINUSE
) &&
246 (--tport
> IPPORT_RESERVED
/ 2))
247 sin
.sin_port
= htons(tport
);
252 * variables for managing the nfs_bind_resv_thread
254 int nfs_resv_mounts
= 0;
255 static int nfs_bind_resv_thread_state
= 0;
256 #define NFS_BIND_RESV_THREAD_STATE_INITTED 1
257 #define NFS_BIND_RESV_THREAD_STATE_RUNNING 2
258 lck_grp_t
*nfs_bind_resv_lck_grp
;
259 lck_grp_attr_t
*nfs_bind_resv_lck_grp_attr
;
260 lck_attr_t
*nfs_bind_resv_lck_attr
;
261 lck_mtx_t
*nfs_bind_resv_mutex
;
262 struct nfs_bind_resv_request
{
263 TAILQ_ENTRY(nfs_bind_resv_request
) brr_chain
;
264 struct nfsmount
*brr_nmp
;
267 static TAILQ_HEAD(, nfs_bind_resv_request
) nfs_bind_resv_request_queue
;
270 * thread to handle any reserved port bind requests
273 nfs_bind_resv_thread(void)
275 struct nfs_bind_resv_request
*brreq
;
277 nfs_bind_resv_thread_state
= NFS_BIND_RESV_THREAD_STATE_RUNNING
;
279 while (nfs_resv_mounts
> 0) {
280 lck_mtx_lock(nfs_bind_resv_mutex
);
281 while ((brreq
= TAILQ_FIRST(&nfs_bind_resv_request_queue
))) {
282 TAILQ_REMOVE(&nfs_bind_resv_request_queue
, brreq
, brr_chain
);
283 lck_mtx_unlock(nfs_bind_resv_mutex
);
284 brreq
->brr_error
= nfs_bind_resv(brreq
->brr_nmp
);
286 lck_mtx_lock(nfs_bind_resv_mutex
);
288 msleep((caddr_t
)&nfs_bind_resv_request_queue
,
289 nfs_bind_resv_mutex
, PSOCK
| PDROP
,
290 "nfs_bind_resv_request_queue", 0);
293 nfs_bind_resv_thread_state
= NFS_BIND_RESV_THREAD_STATE_INITTED
;
294 (void) thread_terminate(current_thread());
298 nfs_bind_resv_thread_wake(void)
300 if (nfs_bind_resv_thread_state
< NFS_BIND_RESV_THREAD_STATE_RUNNING
)
302 wakeup(&nfs_bind_resv_request_queue
);
307 * underprivileged procs call this to request nfs_bind_resv_thread
308 * to perform the reserved port binding for them.
311 nfs_bind_resv_nopriv(struct nfsmount
*nmp
)
313 struct nfs_bind_resv_request brreq
;
316 if (nfs_bind_resv_thread_state
< NFS_BIND_RESV_THREAD_STATE_RUNNING
) {
317 if (nfs_bind_resv_thread_state
< NFS_BIND_RESV_THREAD_STATE_INITTED
) {
318 nfs_bind_resv_lck_grp_attr
= lck_grp_attr_alloc_init();
319 nfs_bind_resv_lck_grp
= lck_grp_alloc_init("nfs_bind_resv", nfs_bind_resv_lck_grp_attr
);
320 nfs_bind_resv_lck_attr
= lck_attr_alloc_init();
321 nfs_bind_resv_mutex
= lck_mtx_alloc_init(nfs_bind_resv_lck_grp
, nfs_bind_resv_lck_attr
);
322 TAILQ_INIT(&nfs_bind_resv_request_queue
);
323 nfs_bind_resv_thread_state
= NFS_BIND_RESV_THREAD_STATE_INITTED
;
325 kernel_thread(kernel_task
, nfs_bind_resv_thread
);
326 nfs_bind_resv_thread_state
= NFS_BIND_RESV_THREAD_STATE_RUNNING
;
332 lck_mtx_lock(nfs_bind_resv_mutex
);
333 TAILQ_INSERT_TAIL(&nfs_bind_resv_request_queue
, &brreq
, brr_chain
);
334 lck_mtx_unlock(nfs_bind_resv_mutex
);
336 error
= nfs_bind_resv_thread_wake();
338 TAILQ_REMOVE(&nfs_bind_resv_request_queue
, &brreq
, brr_chain
);
339 /* Note: we might be able to simply restart the thread */
343 tsleep((caddr_t
)&brreq
, PSOCK
, "nfsbindresv", 0);
345 return (brreq
.brr_error
);
349 * Initialize sockets and congestion for a new NFS connection.
350 * We do not free the sockaddr if error.
354 struct nfsmount
*nmp
,
355 __unused
struct nfsreq
*rep
)
358 int error
, rcvreserve
, sndreserve
;
359 struct sockaddr
*saddr
;
360 struct timeval timeo
;
363 saddr
= mbuf_data(nmp
->nm_nam
);
364 error
= sock_socket(saddr
->sa_family
, nmp
->nm_sotype
,
365 nmp
->nm_soproto
, 0, 0, &nmp
->nm_so
);
372 * Some servers require that the client port be a reserved port number.
374 if (saddr
->sa_family
== AF_INET
&& (nmp
->nm_flag
& NFSMNT_RESVPORT
)) {
377 * sobind() requires current_proc() to have superuser privs.
378 * If this bind is part of a reconnect, and the current proc
379 * doesn't have superuser privs, we hand the sobind() off to
380 * a kernel thread to process.
382 if ((nmp
->nm_state
& NFSSTA_MOUNTED
) &&
383 (p
= current_proc()) && suser(kauth_cred_get(), 0)) {
384 /* request nfs_bind_resv_thread() to do bind */
385 error
= nfs_bind_resv_nopriv(nmp
);
387 error
= nfs_bind_resv(nmp
);
394 * Protocols that do not require connections may be optionally left
395 * unconnected for servers that reply from a port other than NFS_PORT.
397 if (nmp
->nm_flag
& NFSMNT_NOCONN
) {
398 if (nmp
->nm_sotype
== SOCK_STREAM
) {
406 error
= sock_connect(so
, mbuf_data(nmp
->nm_nam
), MSG_DONTWAIT
);
407 if (error
&& error
!= EINPROGRESS
) {
411 while ((error
= sock_connectwait(so
, &tv
)) == EINPROGRESS
) {
412 if (rep
&& (error
= nfs_sigintr(nmp
, rep
, rep
->r_procp
))) {
419 * Always time out on recieve, this allows us to reconnect the
420 * socket to deal with network changes.
424 error
= sock_setsockopt(so
, SOL_SOCKET
, SO_RCVTIMEO
, &timeo
, sizeof(timeo
));
425 if (nmp
->nm_flag
& (NFSMNT_SOFT
| NFSMNT_INT
)) {
430 error
= sock_setsockopt(so
, SOL_SOCKET
, SO_SNDTIMEO
, &timeo
, sizeof(timeo
));
432 if (nmp
->nm_sotype
== SOCK_DGRAM
) {
433 sndreserve
= (nmp
->nm_wsize
+ NFS_MAXPKTHDR
) * 3;
434 rcvreserve
= (nmp
->nm_rsize
+ NFS_MAXPKTHDR
) *
435 (nmp
->nm_readahead
> 0 ? nmp
->nm_readahead
+ 1 : 2);
436 } else if (nmp
->nm_sotype
== SOCK_SEQPACKET
) {
437 sndreserve
= (nmp
->nm_wsize
+ NFS_MAXPKTHDR
) * 3;
438 rcvreserve
= (nmp
->nm_rsize
+ NFS_MAXPKTHDR
) *
439 (nmp
->nm_readahead
> 0 ? nmp
->nm_readahead
+ 1 : 2);
444 sock_gettype(so
, NULL
, NULL
, &proto
);
445 if (nmp
->nm_sotype
!= SOCK_STREAM
)
446 panic("nfscon sotype");
448 // Assume that SOCK_STREAM always requires a connection
449 sock_setsockopt(so
, SOL_SOCKET
, SO_KEEPALIVE
, &on
, sizeof(on
));
451 if (proto
== IPPROTO_TCP
) {
452 sock_setsockopt(so
, IPPROTO_TCP
, TCP_NODELAY
, &on
, sizeof(on
));
455 sndreserve
= (nmp
->nm_wsize
+ NFS_MAXPKTHDR
+ sizeof (u_long
)) * 3;
456 rcvreserve
= (nmp
->nm_rsize
+ NFS_MAXPKTHDR
+ sizeof (u_long
)) *
457 (nmp
->nm_readahead
> 0 ? nmp
->nm_readahead
+ 1 : 2);
460 if (sndreserve
> NFS_MAXSOCKBUF
)
461 sndreserve
= NFS_MAXSOCKBUF
;
462 if (rcvreserve
> NFS_MAXSOCKBUF
)
463 rcvreserve
= NFS_MAXSOCKBUF
;
464 error
= sock_setsockopt(so
, SOL_SOCKET
, SO_SNDBUF
, &sndreserve
, sizeof(sndreserve
));
468 error
= sock_setsockopt(so
, SOL_SOCKET
, SO_RCVBUF
, &rcvreserve
, sizeof(rcvreserve
));
473 sock_nointerrupt(so
, 1);
475 /* Initialize other non-zero congestion variables */
476 nmp
->nm_srtt
[0] = nmp
->nm_srtt
[1] = nmp
->nm_srtt
[2] =
477 nmp
->nm_srtt
[3] = (NFS_TIMEO
<< 3);
478 nmp
->nm_sdrtt
[0] = nmp
->nm_sdrtt
[1] = nmp
->nm_sdrtt
[2] =
479 nmp
->nm_sdrtt
[3] = 0;
480 nmp
->nm_cwnd
= NFS_MAXCWND
/ 2; /* Initial send window */
482 FSDBG(529, nmp
, nmp
->nm_state
, nmp
->nm_soflags
, nmp
->nm_cwnd
);
483 nmp
->nm_timeouts
= 0;
493 * Called when a connection is broken on a reliable protocol.
494 * - clean up the old socket
495 * - nfs_connect() again
496 * - set R_MUSTRESEND for all outstanding requests on mount point
497 * If this fails the mount point is DEAD!
498 * nb: Must be called with the nfs_sndlock() set on the mount point.
501 nfs_reconnect(struct nfsreq
*rep
)
504 struct nfsmount
*nmp
= rep
->r_nmp
;
508 while ((error
= nfs_connect(nmp
, rep
))) {
509 if (error
== EINTR
|| error
== ERESTART
)
513 nfs_down(rep
->r_nmp
, rep
->r_procp
, error
, NFSSTA_TIMEO
,
515 rep
->r_flags
|= R_TPRINTFMSG
;
516 if (!(nmp
->nm_state
& NFSSTA_MOUNTED
)) {
517 /* we're not yet completely mounted and */
518 /* we can't reconnect, so we fail */
521 if ((error
= nfs_sigintr(rep
->r_nmp
, rep
, rep
->r_procp
)))
523 tsleep((caddr_t
)&lbolt
, PSOCK
, "nfscon", 0);
527 * Loop through outstanding request list and fix up all requests
530 TAILQ_FOREACH(rp
, &nfs_reqq
, r_chain
) {
531 if (rp
->r_nmp
== nmp
)
532 rp
->r_flags
|= R_MUSTRESEND
;
538 * NFS disconnect. Clean up and unlink.
541 nfs_disconnect(struct nfsmount
*nmp
)
548 sock_shutdown(so
, 2);
554 * This is the nfs send routine. For connection based socket types, it
555 * must be called with an nfs_sndlock() on the socket.
556 * "rep == NULL" indicates that it has been called from a server.
557 * For the client side:
558 * - return EINTR if the RPC is terminated, 0 otherwise
559 * - set R_MUSTRESEND if the send fails for any reason
560 * - do any cleanup required by recoverable socket errors (???)
561 * For the server side:
562 * - return EINTR or ERESTART if interrupted by a signal
563 * - return EPIPE if a connection is lost for connection based sockets (TCP...)
564 * - do any cleanup required by recoverable socket errors (???)
567 nfs_send(so
, nam
, top
, rep
)
573 struct sockaddr
*sendnam
;
574 int error
, error2
, sotype
, flags
;
575 u_long xidqueued
= 0;
577 char savenametolog
[MAXPATHLEN
];
581 error
= nfs_sigintr(rep
->r_nmp
, rep
, rep
->r_procp
);
586 if ((so
= rep
->r_nmp
->nm_so
) == NULL
) {
587 rep
->r_flags
|= R_MUSTRESEND
;
591 rep
->r_flags
&= ~R_MUSTRESEND
;
592 TAILQ_FOREACH(rp
, &nfs_reqq
, r_chain
)
596 xidqueued
= rp
->r_xid
;
598 sock_gettype(so
, NULL
, &sotype
, NULL
);
599 if ((sotype
== SOCK_STREAM
) || (sock_isconnected(so
)) ||
601 sendnam
= (struct sockaddr
*)0;
603 sendnam
= mbuf_data(nam
);
605 if (sotype
== SOCK_SEQPACKET
)
611 * Save the name here in case mount point goes away if we block.
612 * The name is using local stack and is large, but don't
613 * want to block if we malloc.
616 strncpy(savenametolog
,
617 vfs_statfs(rep
->r_nmp
->nm_mountp
)->f_mntfromname
,
619 bzero(&msg
, sizeof(msg
));
620 msg
.msg_name
= (caddr_t
)sendnam
;
621 msg
.msg_namelen
= sendnam
== 0 ? 0 : sendnam
->sa_len
;
622 error
= sock_sendmbuf(so
, &msg
, top
, flags
, NULL
);
627 TAILQ_FOREACH(rp
, &nfs_reqq
, r_chain
)
628 if (rp
== rep
&& rp
->r_xid
== xidqueued
)
631 panic("nfs_send: error %d xid %x gone",
634 log(LOG_INFO
, "nfs send error %d for server %s\n",
635 error
, savenametolog
);
637 * Deal with errors for the client side.
639 error2
= nfs_sigintr(rep
->r_nmp
, rep
, rep
->r_procp
);
643 rep
->r_flags
|= R_MUSTRESEND
;
646 log(LOG_INFO
, "nfsd send error %d\n", error
);
649 * Handle any recoverable (soft) socket errors here. (???)
651 if (error
!= EINTR
&& error
!= ERESTART
&& error
!= EIO
&&
652 error
!= EWOULDBLOCK
&& error
!= EPIPE
) {
660 * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all
661 * done by soreceive(), but for SOCK_STREAM we must deal with the Record
662 * Mark and consolidate the data into a new mbuf list.
663 * nb: Sometimes TCP passes the data up to soreceive() in long lists of
665 * For SOCK_STREAM we must be very careful to read an entire record once
666 * we have read any of it, even if the system call has been interrupted.
669 nfs_receive(struct nfsreq
*rep
, mbuf_t
*mp
)
675 int error
, error2
, sotype
;
676 proc_t p
= current_proc(); /* XXX */
682 * Set up arguments for soreceive()
685 sotype
= rep
->r_nmp
->nm_sotype
;
688 * For reliable protocols, lock against other senders/receivers
689 * in case a reconnect is necessary.
690 * For SOCK_STREAM, first get the Record Mark to find out how much
691 * more there is to get.
692 * We must lock the socket against other receivers
693 * until we have an entire rpc request/reply.
695 if (sotype
!= SOCK_DGRAM
) {
696 error
= nfs_sndlock(rep
);
701 * Check for fatal errors and resending request.
704 * Ugh: If a reconnect attempt just happened, nm_so
705 * would have changed. NULL indicates a failed
706 * attempt that has essentially shut down this
709 if ((error
= nfs_sigintr(rep
->r_nmp
, rep
, p
)) || rep
->r_mrep
) {
715 so
= rep
->r_nmp
->nm_so
;
717 error
= nfs_reconnect(rep
);
724 while (rep
->r_flags
& R_MUSTRESEND
) {
725 error
= mbuf_copym(rep
->r_mreq
, 0, MBUF_COPYALL
, MBUF_WAITOK
, &m
);
727 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpcretries
);
728 error
= nfs_send(so
, rep
->r_nmp
->nm_nam
, m
, rep
);
731 * we also hold rcv lock so rep is still
735 if (error
== EINTR
|| error
== ERESTART
||
736 (error
= nfs_reconnect(rep
))) {
744 if (sotype
== SOCK_STREAM
) {
749 while (!error
&& !lastfragment
) {
750 aio
.iov_base
= (uintptr_t) &fraglen
;
751 aio
.iov_len
= sizeof(u_long
);
752 bzero(&msg
, sizeof(msg
));
753 msg
.msg_iov
= (struct iovec
*) &aio
;
756 error
= sock_receive(so
, &msg
, MSG_WAITALL
, &rcvlen
);
757 if (!rep
->r_nmp
) /* if unmounted then bailout */
759 if (error
== EWOULDBLOCK
&& rep
) {
760 error2
= nfs_sigintr(rep
->r_nmp
, rep
, p
);
764 } while (error
== EWOULDBLOCK
);
765 if (!error
&& rcvlen
< aio
.iov_len
) {
766 /* only log a message if we got a partial word */
769 "short receive (%d/%d) from nfs server %s\n",
770 rcvlen
, sizeof(u_long
),
771 vfs_statfs(rep
->r_nmp
->nm_mountp
)->f_mntfromname
);
776 lastfragment
= ntohl(fraglen
) & 0x80000000;
777 fraglen
= ntohl(fraglen
) & ~0x80000000;
780 * This is SERIOUS! We are out of sync with the sender
781 * and forcing a disconnect/reconnect is all I can do.
783 if (len
> NFS_MAXPACKET
) {
784 log(LOG_ERR
, "%s (%d) from nfs server %s\n",
785 "impossible RPC record length", len
,
786 vfs_statfs(rep
->r_nmp
->nm_mountp
)->f_mntfromname
);
794 error
= sock_receivembuf(so
, NULL
, &m
, MSG_WAITALL
, &rcvlen
);
795 if (!rep
->r_nmp
) /* if unmounted then bailout */ {
798 } while (error
== EWOULDBLOCK
|| error
== EINTR
||
801 if (!error
&& fraglen
> rcvlen
) {
803 "short receive (%d/%d) from nfs server %s\n",
805 vfs_statfs(rep
->r_nmp
->nm_mountp
)->f_mntfromname
);
814 error
= mbuf_setnext(mlast
, m
);
816 printf("nfs_receive: mbuf_setnext failed %d\n", error
);
820 while (mbuf_next(mlast
))
821 mlast
= mbuf_next(mlast
);
825 bzero(&msg
, sizeof(msg
));
828 error
= sock_receivembuf(so
, &msg
, mp
, 0, &rcvlen
);
829 if (!rep
->r_nmp
) /* if unmounted then bailout */ {
832 if (error
== EWOULDBLOCK
&& rep
) {
833 error2
= nfs_sigintr(rep
->r_nmp
, rep
, p
);
838 } while (error
== EWOULDBLOCK
);
840 if ((msg
.msg_flags
& MSG_EOR
) == 0)
842 if (!error
&& *mp
== NULL
)
847 if (error
&& error
!= EINTR
&& error
!= ERESTART
) {
852 "receive error %d from nfs server %s\n", error
,
853 vfs_statfs(rep
->r_nmp
->nm_mountp
)->f_mntfromname
);
854 error
= nfs_sndlock(rep
);
856 error
= nfs_reconnect(rep
);
864 * We could have failed while rebinding the datagram socket
865 * so we need to attempt to rebind here.
867 if ((so
= rep
->r_nmp
->nm_so
) == NULL
) {
868 error
= nfs_sndlock(rep
);
870 error
= nfs_reconnect(rep
);
875 if (!rep
->r_nmp
) /* if unmounted then bailout */
877 so
= rep
->r_nmp
->nm_so
;
879 bzero(&msg
, sizeof(msg
));
883 error
= sock_receivembuf(so
, &msg
, mp
, 0, &rcvlen
);
884 if (!rep
->r_nmp
) /* if unmounted then bailout */
887 error2
= nfs_sigintr(rep
->r_nmp
, rep
, p
);
893 /* Reconnect for all errors. We may be receiving
894 * soft/hard/blocking errors because of a network
896 * XXX: we should rate limit or delay this
897 * to once every N attempts or something.
898 * although TCP doesn't seem to.
901 error2
= nfs_sndlock(rep
);
903 error2
= nfs_reconnect(rep
);
906 else if (!rep
->r_nmp
) /* if unmounted then bailout */
909 so
= rep
->r_nmp
->nm_so
;
915 } while (error
== EWOULDBLOCK
);
926 * Implement receipt of reply on a socket.
927 * We must search through the list of received datagrams matching them
928 * with outstanding requests using the xid, until ours is found.
933 struct nfsreq
*myrep
;
936 struct nfsmount
*nmp
= myrep
->r_nmp
;
944 * Loop around until we get our own reply
948 * Lock against other receivers so that I don't get stuck in
949 * sbwait() after someone else has received my reply for me.
950 * Also necessary for connection based protocols to avoid
951 * race conditions during a reconnect.
952 * If nfs_rcvlock() returns EALREADY, that means that
953 * the reply has already been recieved by another
954 * process and we can return immediately. In this
955 * case, the lock is not taken to avoid races with
958 error
= nfs_rcvlock(myrep
);
959 if (error
== EALREADY
)
965 * If we slept after putting bits otw, then reply may have
966 * arrived. In which case returning is required, or we
967 * would hang trying to nfs_receive an already received reply.
969 if (myrep
->r_mrep
!= NULL
) {
970 nfs_rcvunlock(myrep
);
971 FSDBG(530, myrep
->r_xid
, myrep
, myrep
->r_nmp
, -1);
975 * Get the next Rpc reply off the socket. Assume myrep->r_nmp
976 * is still intact by checks done in nfs_rcvlock.
978 error
= nfs_receive(myrep
, &mrep
);
980 * Bailout asap if nfsmount struct gone (unmounted).
983 FSDBG(530, myrep
->r_xid
, myrep
, nmp
, -2);
989 FSDBG(530, myrep
->r_xid
, myrep
, nmp
, error
);
990 nfs_rcvunlock(myrep
);
992 /* Bailout asap if nfsmount struct gone (unmounted). */
1000 * Ignore routing errors on connectionless protocols??
1002 if (NFSIGNORE_SOERROR(nmp
->nm_sotype
, error
)) {
1005 int optlen
= sizeof(clearerror
);
1006 sock_getsockopt(nmp
->nm_so
, SOL_SOCKET
, SO_ERROR
, &clearerror
, &optlen
);
1016 * We assume all is fine, but if we did not have an error
1017 * and mrep is 0, better not dereference it. nfs_receive
1018 * calls soreceive which carefully sets error=0 when it got
1019 * errors on sbwait (tsleep). In most cases, I assume that's
1020 * so we could go back again. In tcp case, EPIPE is returned.
1021 * In udp, case nfs_receive gets back here with no error and no
1022 * mrep. Is the right fix to have soreceive check for process
1023 * aborted after sbwait and return something non-zero? Should
1024 * nfs_receive give an EPIPE? Too risky to play with those
1025 * two this late in game for a shutdown problem. Instead,
1026 * just check here and get out. (ekn)
1029 nfs_rcvunlock(myrep
);
1030 FSDBG(530, myrep
->r_xid
, myrep
, nmp
, -3);
1031 return (ENXIO
); /* sounds good */
1035 * Get the xid and check that it is an rpc reply
1038 dpos
= mbuf_data(md
);
1039 nfsm_dissect(tl
, u_long
*, 2*NFSX_UNSIGNED
);
1041 if (*tl
!= rpc_reply
) {
1042 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpcinvalid
);
1045 if (nmp
->nm_state
& NFSSTA_RCVLOCK
)
1046 nfs_rcvunlock(myrep
);
1051 * Loop through the request list to match up the reply
1052 * Iff no match, just drop the datagram
1054 TAILQ_FOREACH(rep
, &nfs_reqq
, r_chain
) {
1055 if (rep
->r_mrep
== NULL
&& rxid
== rep
->r_xid
) {
1061 * If we're tracking the round trip time
1062 * then we update the circular log here
1063 * with the stats from our current request.
1068 rt
= &nfsrtt
.rttl
[nfsrtt
.pos
];
1069 rt
->proc
= rep
->r_procnum
;
1070 rt
->rto
= NFS_RTO(nmp
, proct
[rep
->r_procnum
]);
1071 rt
->sent
= nmp
->nm_sent
;
1072 rt
->cwnd
= nmp
->nm_cwnd
;
1073 if (proct
[rep
->r_procnum
] == 0)
1074 panic("nfs_reply: proct[%d] is zero", rep
->r_procnum
);
1075 rt
->srtt
= nmp
->nm_srtt
[proct
[rep
->r_procnum
] - 1];
1076 rt
->sdrtt
= nmp
->nm_sdrtt
[proct
[rep
->r_procnum
] - 1];
1077 rt
->fsid
= vfs_statfs(nmp
->nm_mountp
)->f_fsid
;
1078 microtime(&rt
->tstamp
); // XXX unused
1079 if (rep
->r_flags
& R_TIMING
)
1080 rt
->rtt
= rep
->r_rtt
;
1083 nfsrtt
.pos
= (nfsrtt
.pos
+ 1) % NFSRTTLOGSIZ
;
1086 * Update congestion window.
1087 * Do the additive increase of
1090 FSDBG(530, rep
->r_xid
, rep
, nmp
->nm_sent
,
1092 if (nmp
->nm_cwnd
<= nmp
->nm_sent
) {
1094 (NFS_CWNDSCALE
* NFS_CWNDSCALE
+
1095 (nmp
->nm_cwnd
>> 1)) / nmp
->nm_cwnd
;
1096 if (nmp
->nm_cwnd
> NFS_MAXCWND
)
1097 nmp
->nm_cwnd
= NFS_MAXCWND
;
1099 if (rep
->r_flags
& R_SENT
) {
1100 rep
->r_flags
&= ~R_SENT
;
1101 nmp
->nm_sent
-= NFS_CWNDSCALE
;
1104 * Update rtt using a gain of 0.125 on the mean
1105 * and a gain of 0.25 on the deviation.
1107 if (rep
->r_flags
& R_TIMING
) {
1109 * Since the timer resolution of
1110 * NFS_HZ is so course, it can often
1111 * result in r_rtt == 0. Since
1112 * r_rtt == N means that the actual
1113 * rtt is between N+dt and N+2-dt ticks,
1116 if (proct
[rep
->r_procnum
] == 0)
1117 panic("nfs_reply: proct[%d] is zero", rep
->r_procnum
);
1118 t1
= rep
->r_rtt
+ 1;
1119 t1
-= (NFS_SRTT(rep
) >> 3);
1120 NFS_SRTT(rep
) += t1
;
1123 t1
-= (NFS_SDRTT(rep
) >> 2);
1124 NFS_SDRTT(rep
) += t1
;
1126 nmp
->nm_timeouts
= 0;
1130 nfs_rcvunlock(myrep
);
1132 * If not matched to a request, drop it.
1133 * If it's mine, get out.
1136 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpcunexpected
);
1138 } else if (rep
== myrep
) {
1139 if (rep
->r_mrep
== NULL
)
1140 panic("nfs_reply: nil r_mrep");
1143 FSDBG(530, myrep
->r_xid
, myrep
, rep
,
1144 rep
? rep
->r_xid
: myrep
->r_flags
);
1149 * nfs_request - goes something like this
1150 * - fill in request struct
1151 * - links it into list
1152 * - calls nfs_send() for first transmit
1153 * - calls nfs_receive() to get reply
1154 * - break down rpc header and return with nfs reply pointed to
1156 * nb: always frees up mreq mbuf list
1159 nfs_request(vp
, mp
, mrest
, procnum
, procp
, cred
, mrp
, mdp
, dposp
, xidp
)
1172 struct nfsreq re
, *rep
;
1175 struct nfsmount
*nmp
;
1176 mbuf_t md
, mheadend
;
1177 char nickv
[RPCX_NICKVERF
];
1180 int t1
, error
= 0, mrest_len
, auth_len
, auth_type
;
1181 int trylater_delay
= NFS_TRYLATERDEL
, failed_auth
= 0;
1182 int verf_len
, verf_type
;
1184 char *auth_str
, *verf_str
;
1185 NFSKERBKEY_T key
; /* save session key */
1198 nmp
= VFSTONFS(vnode_mount(vp
));
1200 (nmp
->nm_state
& (NFSSTA_FORCE
|NFSSTA_TIMEO
)) ==
1201 (NFSSTA_FORCE
|NFSSTA_TIMEO
)) {
1205 nmsotype
= nmp
->nm_sotype
;
1207 FSDBG_TOP(531, vp
, procnum
, nmp
, rep
);
1211 rep
->r_procp
= procp
;
1212 rep
->r_procnum
= procnum
;
1214 rep
->r_lastmsg
= now
.tv_sec
-
1215 ((nmp
->nm_tprintf_delay
) - (nmp
->nm_tprintf_initial_delay
));
1225 * Get the RPC header with authorization.
1228 nmp
= vp
? VFSTONFS(vnode_mount(vp
)) : rep
->r_nmp
;
1230 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1234 verf_str
= auth_str
= (char *)0;
1235 if (nmp
->nm_flag
& NFSMNT_KERB
) {
1237 verf_len
= sizeof (nickv
);
1238 auth_type
= RPCAUTH_KERB4
;
1239 bzero((caddr_t
)key
, sizeof (key
));
1240 if (failed_auth
|| nfs_getnickauth(nmp
, cred
, &auth_str
,
1241 &auth_len
, verf_str
, verf_len
)) {
1242 nmp
= vp
? VFSTONFS(vnode_mount(vp
)) : rep
->r_nmp
;
1244 FSDBG_BOT(531, 2, vp
, error
, rep
);
1248 error
= nfs_getauth(nmp
, rep
, cred
, &auth_str
,
1249 &auth_len
, verf_str
, &verf_len
, key
);
1250 nmp
= vp
? VFSTONFS(vnode_mount(vp
)) : rep
->r_nmp
;
1254 FSDBG_BOT(531, 2, vp
, error
, rep
);
1260 auth_type
= RPCAUTH_UNIX
;
1261 if (cred
->cr_ngroups
< 1)
1262 panic("nfsreq nogrps");
1263 auth_len
= ((((cred
->cr_ngroups
- 1) > nmp
->nm_numgrps
) ?
1264 nmp
->nm_numgrps
: (cred
->cr_ngroups
- 1)) << 2) +
1267 error
= nfsm_rpchead(cred
, nmp
->nm_flag
, procnum
, auth_type
, auth_len
,
1268 auth_str
, verf_len
, verf_str
, mrest
, mrest_len
, &mheadend
, &xid
, &m
);
1270 _FREE(auth_str
, M_TEMP
);
1273 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1277 *xidp
= ntohl(xid
) + ((u_int64_t
)nfs_xidwrap
<< 32);
1280 * For stream protocols, insert a Sun RPC Record Mark.
1282 if (nmsotype
== SOCK_STREAM
) {
1283 error
= mbuf_prepend(&m
, NFSX_UNSIGNED
, MBUF_WAITOK
);
1286 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1289 *((u_long
*)mbuf_data(m
)) =
1290 htonl(0x80000000 | (mbuf_pkthdr_len(m
) - NFSX_UNSIGNED
));
1295 nmp
= vp
? VFSTONFS(vnode_mount(vp
)) : rep
->r_nmp
;
1296 if (nmp
&& (nmp
->nm_flag
& NFSMNT_SOFT
))
1297 rep
->r_retry
= nmp
->nm_retry
;
1299 rep
->r_retry
= NFS_MAXREXMIT
+ 1; /* past clip limit */
1300 rep
->r_rtt
= rep
->r_rexmit
= 0;
1301 if (proct
[procnum
] > 0)
1302 rep
->r_flags
= R_TIMING
;
1308 * Do the client side RPC.
1310 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpcrequests
);
1312 * Chain request into list of outstanding requests. Be sure
1313 * to put it LAST so timer finds oldest requests first.
1315 TAILQ_INSERT_TAIL(&nfs_reqq
, rep
, r_chain
);
1318 * If backing off another request or avoiding congestion, don't
1319 * send this one now but let timer do it. If not timing a request,
1322 if (nmp
&& nmp
->nm_so
&& (nmp
->nm_sotype
!= SOCK_DGRAM
||
1323 (nmp
->nm_flag
& NFSMNT_DUMBTIMR
) ||
1324 nmp
->nm_sent
< nmp
->nm_cwnd
)) {
1325 int connrequired
= (nmp
->nm_sotype
== SOCK_STREAM
);
1328 error
= nfs_sndlock(rep
);
1331 * Set the R_SENT before doing the send in case another thread
1332 * processes the reply before the nfs_send returns here
1335 if ((rep
->r_flags
& R_MUSTRESEND
) == 0) {
1336 FSDBG(531, rep
->r_xid
, rep
, nmp
->nm_sent
,
1338 nmp
->nm_sent
+= NFS_CWNDSCALE
;
1339 rep
->r_flags
|= R_SENT
;
1342 error
= mbuf_copym(m
, 0, MBUF_COPYALL
, MBUF_WAITOK
, &m2
);
1344 error
= nfs_send(nmp
->nm_so
, nmp
->nm_nam
, m2
, rep
);
1348 nmp
= vp
? VFSTONFS(vnode_mount(vp
)) : rep
->r_nmp
;
1351 nmp
->nm_sent
-= NFS_CWNDSCALE
;
1352 rep
->r_flags
&= ~R_SENT
;
1359 * Wait for the reply from our send or the timer's.
1361 if (!error
|| error
== EPIPE
)
1362 error
= nfs_reply(rep
);
1365 * RPC done, unlink the request.
1367 nfs_repdequeue(rep
);
1369 nmp
= vp
? VFSTONFS(vnode_mount(vp
)) : rep
->r_nmp
;
1372 * Decrement the outstanding request count.
1374 if (rep
->r_flags
& R_SENT
) {
1375 rep
->r_flags
&= ~R_SENT
; /* paranoia */
1377 FSDBG(531, rep
->r_xid
, rep
, nmp
->nm_sent
, nmp
->nm_cwnd
);
1378 nmp
->nm_sent
-= NFS_CWNDSCALE
;
1383 * If there was a successful reply and a tprintf msg.
1384 * tprintf a response.
1387 nfs_up(nmp
, procp
, NFSSTA_TIMEO
,
1388 (rep
->r_flags
& R_TPRINTFMSG
) ? "is alive again" : NULL
);
1395 mbuf_freem(rep
->r_mreq
);
1396 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1401 * break down the rpc header and check if ok
1403 nfsm_dissect(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
1404 if (*tl
++ == rpc_msgdenied
) {
1405 if (*tl
== rpc_mismatch
)
1407 else if ((nmp
->nm_flag
& NFSMNT_KERB
) && *tl
++ == rpc_autherr
) {
1410 error
= mbuf_setnext(mheadend
, NULL
);
1412 mbuf_freem(rep
->r_mreq
);
1415 printf("nfs_request: mbuf_setnext failed\n");
1421 mbuf_freem(rep
->r_mreq
);
1422 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1427 * Grab any Kerberos verifier, otherwise just throw it away.
1429 verf_type
= fxdr_unsigned(int, *tl
++);
1430 i
= fxdr_unsigned(int, *tl
);
1431 if ((nmp
->nm_flag
& NFSMNT_KERB
) && verf_type
== RPCAUTH_KERB4
) {
1432 error
= nfs_savenickauth(nmp
, cred
, i
, key
, &md
, &dpos
, mrep
);
1436 nfsm_adv(nfsm_rndup(i
));
1437 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
1440 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
1442 error
= fxdr_unsigned(int, *tl
);
1443 if ((nmp
->nm_flag
& NFSMNT_NFSV3
) &&
1444 error
== NFSERR_TRYLATER
) {
1448 waituntil
= now
.tv_sec
+ trylater_delay
;
1449 while (now
.tv_sec
< waituntil
) {
1450 tsleep((caddr_t
)&lbolt
, PSOCK
, "nfstrylater", 0);
1453 trylater_delay
*= 2;
1454 if (trylater_delay
> 60)
1455 trylater_delay
= 60;
1460 * If the File Handle was stale, invalidate the
1461 * lookup cache, just in case.
1463 if ((error
== ESTALE
) && vp
)
1465 if (nmp
->nm_flag
& NFSMNT_NFSV3
) {
1469 error
|= NFSERR_RETERR
;
1472 error
&= ~NFSERR_RETERR
;
1474 mbuf_freem(rep
->r_mreq
);
1475 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1482 mbuf_freem(rep
->r_mreq
);
1483 FSDBG_BOT(531, 0xf0f0f0f0, rep
->r_xid
, nmp
, rep
);
1487 error
= EPROTONOSUPPORT
;
1489 mbuf_freem(rep
->r_mreq
);
1490 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1494 #ifndef NFS_NOSERVER
1496 * Generate the rpc reply header
1497 * siz arg. is used to decide if adding a cluster is worthwhile
1500 nfs_rephead(siz
, nd
, slp
, err
, mrq
, mbp
, bposp
)
1502 struct nfsrv_descript
*nd
;
1503 struct nfssvc_sock
*slp
;
1516 * If this is a big reply, use a cluster else
1517 * try and leave leading space for the lower level headers.
1519 siz
+= RPC_REPLYSIZ
;
1520 if (siz
>= nfs_mbuf_minclsize
) {
1521 error
= mbuf_getpacket(MBUF_WAITOK
, &mreq
);
1523 error
= mbuf_gethdr(MBUF_WAITOK
, MBUF_TYPE_DATA
, &mreq
);
1526 /* unable to allocate packet */
1531 tl
= mbuf_data(mreq
);
1532 mlen
= 6 * NFSX_UNSIGNED
;
1533 if (siz
< nfs_mbuf_minclsize
) {
1534 /* leave space for lower level headers */
1535 tl
+= 80/sizeof(*tl
); /* XXX max_hdr? XXX */
1536 mbuf_setdata(mreq
, tl
, mlen
);
1538 mbuf_setlen(mreq
, mlen
);
1540 bpos
= ((caddr_t
)tl
) + mlen
;
1541 *tl
++ = txdr_unsigned(nd
->nd_retxid
);
1543 if (err
== ERPCMISMATCH
|| (err
& NFSERR_AUTHERR
)) {
1544 *tl
++ = rpc_msgdenied
;
1545 if (err
& NFSERR_AUTHERR
) {
1546 *tl
++ = rpc_autherr
;
1547 *tl
= txdr_unsigned(err
& ~NFSERR_AUTHERR
);
1548 mlen
-= NFSX_UNSIGNED
;
1549 mbuf_setlen(mreq
, mlen
);
1550 bpos
-= NFSX_UNSIGNED
;
1552 *tl
++ = rpc_mismatch
;
1553 *tl
++ = txdr_unsigned(RPC_VER2
);
1554 *tl
= txdr_unsigned(RPC_VER2
);
1557 *tl
++ = rpc_msgaccepted
;
1560 * For Kerberos authentication, we must send the nickname
1561 * verifier back, otherwise just RPCAUTH_NULL.
1563 if (nd
->nd_flag
& ND_KERBFULL
) {
1564 struct nfsuid
*nuidp
;
1565 struct timeval ktvin
, ktvout
;
1566 uid_t uid
= kauth_cred_getuid(nd
->nd_cr
);
1568 lck_rw_lock_shared(&slp
->ns_rwlock
);
1569 for (nuidp
= NUIDHASH(slp
, uid
)->lh_first
;
1570 nuidp
!= 0; nuidp
= nuidp
->nu_hash
.le_next
) {
1571 if (kauth_cred_getuid(nuidp
->nu_cr
) == uid
&&
1572 (!nd
->nd_nam2
|| netaddr_match(NU_NETFAM(nuidp
),
1573 &nuidp
->nu_haddr
, nd
->nd_nam2
)))
1578 txdr_unsigned(nuidp
->nu_timestamp
.tv_sec
- 1);
1580 txdr_unsigned(nuidp
->nu_timestamp
.tv_usec
);
1583 * Encrypt the timestamp in ecb mode using the
1590 *tl
++ = rpc_auth_kerb
;
1591 *tl
++ = txdr_unsigned(3 * NFSX_UNSIGNED
);
1592 *tl
= ktvout
.tv_sec
;
1593 nfsm_build(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
1594 *tl
++ = ktvout
.tv_usec
;
1595 *tl
++ = txdr_unsigned(kauth_cred_getuid(nuidp
->nu_cr
));
1600 lck_rw_done(&slp
->ns_rwlock
);
1607 *tl
= txdr_unsigned(RPC_PROGUNAVAIL
);
1610 *tl
= txdr_unsigned(RPC_PROGMISMATCH
);
1611 nfsm_build(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
1612 // XXX hard coded versions
1613 *tl
++ = txdr_unsigned(2);
1614 *tl
= txdr_unsigned(3);
1617 *tl
= txdr_unsigned(RPC_PROCUNAVAIL
);
1620 *tl
= txdr_unsigned(RPC_GARBAGE
);
1624 if (err
!= NFSERR_RETVOID
) {
1625 nfsm_build(tl
, u_long
*, NFSX_UNSIGNED
);
1627 *tl
= txdr_unsigned(nfsrv_errmap(nd
, err
));
1639 if (err
!= 0 && err
!= NFSERR_RETVOID
) {
1640 OSAddAtomic(1, (SInt32
*)&nfsstats
.srvrpc_errs
);
1646 #endif /* NFS_NOSERVER */
1650 * From FreeBSD 1.58, a Matt Dillon fix...
1651 * Flag a request as being about to terminate.
1652 * The nm_sent count is decremented now to avoid deadlocks when the process
1653 * in soreceive() hasn't yet managed to send its own request.
1656 nfs_softterm(struct nfsreq
*rep
)
1659 rep
->r_flags
|= R_SOFTTERM
;
1660 if (rep
->r_flags
& R_SENT
) {
1661 FSDBG(532, rep
->r_xid
, rep
, rep
->r_nmp
->nm_sent
,
1662 rep
->r_nmp
->nm_cwnd
);
1663 rep
->r_nmp
->nm_sent
-= NFS_CWNDSCALE
;
1664 rep
->r_flags
&= ~R_SENT
;
1669 nfs_timer_funnel(void * arg
)
1671 (void) thread_funnel_set(kernel_flock
, TRUE
);
1673 (void) thread_funnel_set(kernel_flock
, FALSE
);
1678 * Ensure rep isn't in use by the timer, then dequeue it.
1681 nfs_repdequeue(struct nfsreq
*rep
)
1684 while ((rep
->r_flags
& R_BUSY
)) {
1685 rep
->r_flags
|= R_WAITING
;
1686 tsleep(rep
, PSOCK
, "repdeq", 0);
1688 TAILQ_REMOVE(&nfs_reqq
, rep
, r_chain
);
1692 * Busy (lock) a nfsreq, used by the nfs timer to make sure it's not
1693 * free()'d out from under it.
1696 nfs_repbusy(struct nfsreq
*rep
)
1699 if ((rep
->r_flags
& R_BUSY
))
1700 panic("rep locked");
1701 rep
->r_flags
|= R_BUSY
;
1705 * Unbusy the nfsreq passed in, return the next nfsreq in the chain busied.
1707 static struct nfsreq
*
1708 nfs_repnext(struct nfsreq
*rep
)
1710 struct nfsreq
* nextrep
;
1715 * We need to get and busy the next req before signalling the
1716 * current one, otherwise wakeup() may block us and we'll race to
1717 * grab the next req.
1719 nextrep
= TAILQ_NEXT(rep
, r_chain
);
1720 if (nextrep
!= NULL
)
1721 nfs_repbusy(nextrep
);
1722 /* unbusy and signal. */
1723 rep
->r_flags
&= ~R_BUSY
;
1724 if ((rep
->r_flags
& R_WAITING
)) {
1725 rep
->r_flags
&= ~R_WAITING
;
1733 * Scan the nfsreq list and retranmit any requests that have timed out
1734 * To avoid retransmission attempts on STREAM sockets (in the future) make
1735 * sure to set the r_retry field to 0 (implies nm_retry == 0).
1738 nfs_timer(__unused
void *arg
)
1743 struct nfsmount
*nmp
;
1746 #ifndef NFS_NOSERVER
1747 struct nfssvc_sock
*slp
;
1749 #endif /* NFS_NOSERVER */
1750 int flags
, rexmit
, cwnd
, sent
;
1754 rep
= TAILQ_FIRST(&nfs_reqq
);
1758 for ( ; rep
!= NULL
; rep
= nfs_repnext(rep
)) {
1760 if (!nmp
) /* unmounted */
1762 if (rep
->r_mrep
|| (rep
->r_flags
& R_SOFTTERM
))
1764 if (nfs_sigintr(nmp
, rep
, rep
->r_procp
))
1766 if (nmp
->nm_tprintf_initial_delay
!= 0 &&
1767 (rep
->r_rexmit
> 2 || (rep
->r_flags
& R_RESENDERR
)) &&
1768 rep
->r_lastmsg
+ nmp
->nm_tprintf_delay
< now
.tv_sec
) {
1769 rep
->r_lastmsg
= now
.tv_sec
;
1770 nfs_down(rep
->r_nmp
, rep
->r_procp
, 0, NFSSTA_TIMEO
,
1772 rep
->r_flags
|= R_TPRINTFMSG
;
1773 if (!(nmp
->nm_state
& NFSSTA_MOUNTED
)) {
1774 /* we're not yet completely mounted and */
1775 /* we can't complete an RPC, so we fail */
1776 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpctimeouts
);
1781 if (rep
->r_rtt
>= 0) {
1783 if (nmp
->nm_flag
& NFSMNT_DUMBTIMR
)
1784 timeo
= nmp
->nm_timeo
;
1786 timeo
= NFS_RTO(nmp
, proct
[rep
->r_procnum
]);
1787 /* ensure 62.5 ms floor */
1788 while (16 * timeo
< hz
)
1790 if (nmp
->nm_timeouts
> 0)
1791 timeo
*= nfs_backoff
[nmp
->nm_timeouts
- 1];
1792 if (rep
->r_rtt
<= timeo
)
1794 if (nmp
->nm_timeouts
< 8)
1798 * Check for too many retransmits. This is never true for
1799 * 'hard' mounts because we set r_retry to NFS_MAXREXMIT + 1
1800 * and never allow r_rexmit to be more than NFS_MAXREXMIT.
1802 if (rep
->r_rexmit
>= rep
->r_retry
) { /* too many */
1803 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpctimeouts
);
1807 if (nmp
->nm_sotype
!= SOCK_DGRAM
) {
1808 if (++rep
->r_rexmit
> NFS_MAXREXMIT
)
1809 rep
->r_rexmit
= NFS_MAXREXMIT
;
1812 if ((so
= nmp
->nm_so
) == NULL
)
1816 * If there is enough space and the window allows..
1818 * Set r_rtt to -1 in case we fail to send it now.
1821 if (((nmp
->nm_flag
& NFSMNT_DUMBTIMR
) ||
1822 (rep
->r_flags
& R_SENT
) ||
1823 nmp
->nm_sent
< nmp
->nm_cwnd
) &&
1824 (mbuf_copym(rep
->r_mreq
, 0, MBUF_COPYALL
, MBUF_DONTWAIT
, &m
) == 0)){
1827 * Iff first send, start timing
1828 * else turn timing off, backoff timer
1829 * and divide congestion window by 2.
1830 * We update these *before* the send to avoid
1831 * racing against receiving the reply.
1832 * We save them so we can restore them on send error.
1834 flags
= rep
->r_flags
;
1835 rexmit
= rep
->r_rexmit
;
1836 cwnd
= nmp
->nm_cwnd
;
1837 sent
= nmp
->nm_sent
;
1839 if (rep
->r_flags
& R_SENT
) {
1840 rep
->r_flags
&= ~R_TIMING
;
1841 if (++rep
->r_rexmit
> NFS_MAXREXMIT
)
1842 rep
->r_rexmit
= NFS_MAXREXMIT
;
1844 if (nmp
->nm_cwnd
< NFS_CWNDSCALE
)
1845 nmp
->nm_cwnd
= NFS_CWNDSCALE
;
1846 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpcretries
);
1848 rep
->r_flags
|= R_SENT
;
1849 nmp
->nm_sent
+= NFS_CWNDSCALE
;
1851 FSDBG(535, xid
, rep
, nmp
->nm_sent
, nmp
->nm_cwnd
);
1853 bzero(&msg
, sizeof(msg
));
1854 if ((nmp
->nm_flag
& NFSMNT_NOCONN
) == NFSMNT_NOCONN
) {
1855 msg
.msg_name
= mbuf_data(nmp
->nm_nam
);
1856 msg
.msg_namelen
= mbuf_len(nmp
->nm_nam
);
1858 error
= sock_sendmbuf(so
, &msg
, m
, MSG_DONTWAIT
, NULL
);
1860 FSDBG(535, xid
, error
, sent
, cwnd
);
1863 if (error
== EWOULDBLOCK
) {
1864 rep
->r_flags
= flags
;
1865 rep
->r_rexmit
= rexmit
;
1866 nmp
->nm_cwnd
= cwnd
;
1867 nmp
->nm_sent
= sent
;
1871 if (NFSIGNORE_SOERROR(nmp
->nm_sotype
, error
)) {
1873 int optlen
= sizeof(clearerror
);
1874 sock_getsockopt(nmp
->nm_so
, SOL_SOCKET
, SO_ERROR
, &clearerror
, &optlen
);
1876 rep
->r_flags
= flags
| R_RESENDERR
;
1877 rep
->r_rexmit
= rexmit
;
1878 nmp
->nm_cwnd
= cwnd
;
1879 nmp
->nm_sent
= sent
;
1881 OSAddAtomic(-1, (SInt32
*)&nfsstats
.rpcretries
);
1888 #ifndef NFS_NOSERVER
1890 * Scan the write gathering queues for writes that need to be
1893 cur_usec
= (u_quad_t
)now
.tv_sec
* 1000000 + (u_quad_t
)now
.tv_usec
;
1894 lck_mtx_lock(nfsd_mutex
);
1895 TAILQ_FOREACH(slp
, &nfssvc_sockhead
, ns_chain
) {
1896 if (slp
->ns_wgtime
&& (slp
->ns_wgtime
<= cur_usec
))
1897 nfsrv_wakenfsd(slp
);
1899 while ((slp
= TAILQ_FIRST(&nfssvc_deadsockhead
))) {
1900 if ((slp
->ns_timestamp
+ 5) > now
.tv_sec
)
1902 TAILQ_REMOVE(&nfssvc_deadsockhead
, slp
, ns_chain
);
1905 lck_mtx_unlock(nfsd_mutex
);
1906 #endif /* NFS_NOSERVER */
1908 if (nfsbuffreeuptimestamp
+ 30 <= now
.tv_sec
) {
1910 * We haven't called nfs_buf_freeup() in a little while.
1911 * So, see if we can free up any stale/unused bufs now.
1916 timeout(nfs_timer_funnel
, (void *)0, nfs_ticks
);
1922 * Test for a termination condition pending on the process.
1923 * This is used to determine if we need to bail on a mount.
1924 * EIO is returned if there has been a soft timeout.
1925 * EINTR is returned if there is a signal pending that is not being ignored
1926 * and the mount is interruptable, or if we are a thread that is in the process
1927 * of cancellation (also SIGKILL posted).
1930 nfs_sigintr(nmp
, rep
, p
)
1931 struct nfsmount
*nmp
;
1935 sigset_t pending_sigs
;
1936 int context_good
= 0;
1937 struct nfsmount
*repnmp
;
1938 extern proc_t kernproc
;
1943 repnmp
= rep
->r_nmp
;
1944 /* we've had a forced unmount. */
1947 /* request has timed out on a 'soft' mount. */
1948 if (rep
->r_flags
& R_SOFTTERM
)
1951 * We're in the progress of a force unmount and there's
1952 * been a timeout we're dead and fail IO.
1954 if ((repnmp
->nm_state
& (NFSSTA_FORCE
|NFSSTA_TIMEO
)) ==
1955 (NFSSTA_FORCE
|NFSSTA_TIMEO
))
1957 /* Someone is unmounting us, go soft and mark it. */
1958 if (repnmp
->nm_mountp
->mnt_kern_flag
& MNTK_FRCUNMOUNT
) {
1959 repnmp
->nm_flag
|= NFSMNT_SOFT
;
1960 nmp
->nm_state
|= NFSSTA_FORCE
;
1963 * If the mount is hung and we've requested not to hang
1964 * on remote filesystems, then bail now.
1966 if (p
!= NULL
&& (proc_noremotehang(p
)) != 0 &&
1967 (repnmp
->nm_state
& NFSSTA_TIMEO
) != 0)
1970 /* XXX: is this valid? this probably should be an assertion. */
1974 /* Is this thread belongs to kernel task; then abort check is not needed */
1975 if ((current_proc() != kernproc
) && current_thread_aborted()) {
1978 /* mask off thread and process blocked signals. */
1980 pending_sigs
= proc_pendingsignals(p
, NFSINT_SIGMASK
);
1981 if (pending_sigs
&& (nmp
->nm_flag
& NFSMNT_INT
) != 0)
1987 * Lock a socket against others.
1988 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
1989 * and also to avoid race conditions between the processes with nfs requests
1990 * in progress when a reconnect is necessary.
1998 int error
, slpflag
= 0, slptimeo
= 0;
2000 if (rep
->r_nmp
== NULL
)
2002 statep
= &rep
->r_nmp
->nm_state
;
2005 if (rep
->r_nmp
->nm_flag
& NFSMNT_INT
)
2007 while (*statep
& NFSSTA_SNDLOCK
) {
2008 error
= nfs_sigintr(rep
->r_nmp
, rep
, p
);
2011 *statep
|= NFSSTA_WANTSND
;
2012 if (p
!= NULL
&& (proc_noremotehang(p
)) != 0)
2014 tsleep((caddr_t
)statep
, slpflag
| (PZERO
- 1), "nfsndlck", slptimeo
);
2015 if (slpflag
== PCATCH
) {
2020 * Make sure while we slept that the mountpoint didn't go away.
2021 * nfs_sigintr and callers expect it in tact.
2024 return (ENXIO
); /* don't have lock until out of loop */
2026 *statep
|= NFSSTA_SNDLOCK
;
2031 * Unlock the stream socket for others.
2039 if (rep
->r_nmp
== NULL
)
2041 statep
= &rep
->r_nmp
->nm_state
;
2042 if ((*statep
& NFSSTA_SNDLOCK
) == 0)
2043 panic("nfs sndunlock");
2044 *statep
&= ~NFSSTA_SNDLOCK
;
2045 if (*statep
& NFSSTA_WANTSND
) {
2046 *statep
&= ~NFSSTA_WANTSND
;
2047 wakeup((caddr_t
)statep
);
2052 nfs_rcvlock(struct nfsreq
*rep
)
2055 int error
, slpflag
, slptimeo
= 0;
2057 /* make sure we still have our mountpoint */
2059 if (rep
->r_mrep
!= NULL
)
2064 statep
= &rep
->r_nmp
->nm_state
;
2065 FSDBG_TOP(534, rep
->r_xid
, rep
, rep
->r_nmp
, *statep
);
2066 if (rep
->r_nmp
->nm_flag
& NFSMNT_INT
)
2070 while (*statep
& NFSSTA_RCVLOCK
) {
2071 if ((error
= nfs_sigintr(rep
->r_nmp
, rep
, rep
->r_procp
))) {
2072 FSDBG_BOT(534, rep
->r_xid
, rep
, rep
->r_nmp
, 0x100);
2074 } else if (rep
->r_mrep
!= NULL
) {
2076 * Don't bother sleeping if reply already arrived
2078 FSDBG_BOT(534, rep
->r_xid
, rep
, rep
->r_nmp
, 0x101);
2081 FSDBG(534, rep
->r_xid
, rep
, rep
->r_nmp
, 0x102);
2082 *statep
|= NFSSTA_WANTRCV
;
2084 * We need to poll if we're P_NOREMOTEHANG so that we
2085 * call nfs_sigintr periodically above.
2087 if (rep
->r_procp
!= NULL
&&
2088 (proc_noremotehang(rep
->r_procp
)) != 0)
2090 tsleep((caddr_t
)statep
, slpflag
| (PZERO
- 1), "nfsrcvlk", slptimeo
);
2091 if (slpflag
== PCATCH
) {
2096 * Make sure while we slept that the mountpoint didn't go away.
2097 * nfs_sigintr and caller nfs_reply expect it intact.
2100 FSDBG_BOT(534, rep
->r_xid
, rep
, rep
->r_nmp
, 0x103);
2101 return (ENXIO
); /* don't have lock until out of loop */
2105 * nfs_reply will handle it if reply already arrived.
2106 * (We may have slept or been preempted).
2108 FSDBG_BOT(534, rep
->r_xid
, rep
, rep
->r_nmp
, *statep
);
2109 *statep
|= NFSSTA_RCVLOCK
;
2114 * Unlock the stream socket for others.
2117 nfs_rcvunlock(struct nfsreq
*rep
)
2121 if (rep
->r_nmp
== NULL
)
2123 statep
= &rep
->r_nmp
->nm_state
;
2125 FSDBG(533, statep
, *statep
, 0, 0);
2126 if ((*statep
& NFSSTA_RCVLOCK
) == 0)
2127 panic("nfs rcvunlock");
2128 *statep
&= ~NFSSTA_RCVLOCK
;
2129 if (*statep
& NFSSTA_WANTRCV
) {
2130 *statep
&= ~NFSSTA_WANTRCV
;
2131 wakeup((caddr_t
)statep
);
2136 #ifndef NFS_NOSERVER
2138 * Socket upcall routine for the nfsd sockets.
2139 * The caddr_t arg is a pointer to the "struct nfssvc_sock".
2140 * Essentially do as much as possible non-blocking, else punt and it will
2141 * be called with MBUF_WAITOK from an nfsd.
2144 nfsrv_rcv(socket_t so
, caddr_t arg
, int waitflag
)
2146 struct nfssvc_sock
*slp
= (struct nfssvc_sock
*)arg
;
2148 if (!nfs_numnfsd
|| !(slp
->ns_flag
& SLP_VALID
))
2151 lck_rw_lock_exclusive(&slp
->ns_rwlock
);
2152 nfsrv_rcv_locked(so
, slp
, waitflag
);
2153 /* Note: ns_rwlock gets dropped when called with MBUF_DONTWAIT */
2156 nfsrv_rcv_locked(socket_t so
, struct nfssvc_sock
*slp
, int waitflag
)
2158 mbuf_t m
, mp
, mhck
, m2
;
2159 int ns_flag
=0, error
;
2163 if ((slp
->ns_flag
& SLP_VALID
) == 0) {
2164 if (waitflag
== MBUF_DONTWAIT
)
2165 lck_rw_done(&slp
->ns_rwlock
);
2171 * Define this to test for nfsds handling this under heavy load.
2173 if (waitflag
== MBUF_DONTWAIT
) {
2174 ns_flag
= SLP_NEEDQ
;
2178 if (slp
->ns_sotype
== SOCK_STREAM
) {
2180 * If there are already records on the queue, defer soreceive()
2181 * to an nfsd so that there is feedback to the TCP layer that
2182 * the nfs servers are heavily loaded.
2184 if (slp
->ns_rec
&& waitflag
== MBUF_DONTWAIT
) {
2185 ns_flag
= SLP_NEEDQ
;
2192 bytes_read
= 1000000000;
2193 error
= sock_receivembuf(so
, NULL
, &mp
, MSG_DONTWAIT
, &bytes_read
);
2194 if (error
|| mp
== NULL
) {
2195 if (error
== EWOULDBLOCK
)
2196 ns_flag
= SLP_NEEDQ
;
2198 ns_flag
= SLP_DISCONN
;
2202 if (slp
->ns_rawend
) {
2203 if ((error
= mbuf_setnext(slp
->ns_rawend
, m
)))
2204 panic("nfsrv_rcv: mbuf_setnext failed %d\n", error
);
2205 slp
->ns_cc
+= bytes_read
;
2208 slp
->ns_cc
= bytes_read
;
2210 while ((m2
= mbuf_next(m
)))
2215 * Now try and parse record(s) out of the raw stream data.
2217 error
= nfsrv_getstream(slp
, waitflag
);
2220 ns_flag
= SLP_DISCONN
;
2222 ns_flag
= SLP_NEEDQ
;
2225 struct sockaddr_storage nam
;
2227 bzero(&msg
, sizeof(msg
));
2228 msg
.msg_name
= (caddr_t
)&nam
;
2229 msg
.msg_namelen
= sizeof(nam
);
2232 bytes_read
= 1000000000;
2233 error
= sock_receivembuf(so
, &msg
, &mp
, MSG_DONTWAIT
| MSG_NEEDSA
, &bytes_read
);
2235 if (msg
.msg_name
&& (mbuf_get(MBUF_WAITOK
, MBUF_TYPE_SONAME
, &mhck
) == 0)) {
2236 mbuf_setlen(mhck
, nam
.ss_len
);
2237 bcopy(&nam
, mbuf_data(mhck
), nam
.ss_len
);
2239 if (mbuf_setnext(m
, mp
)) {
2240 /* trouble... just drop it */
2241 printf("nfsrv_rcv: mbuf_setnext failed\n");
2249 mbuf_setnextpkt(slp
->ns_recend
, m
);
2253 mbuf_setnextpkt(m
, NULL
);
2258 * This may be needed in the future to support
2259 * non-byte-stream connection-oriented protocols
2263 * This (slp->ns_sotype == SOCK_STREAM) should really
2264 * be a check for PR_CONNREQUIRED.
2266 if ((slp
->ns_sotype
== SOCK_STREAM
)
2267 && error
!= EWOULDBLOCK
) {
2268 ns_flag
= SLP_DISCONN
;
2277 * Now try and process the request records, non-blocking.
2281 slp
->ns_flag
|= ns_flag
;
2282 if (waitflag
== MBUF_DONTWAIT
) {
2283 int wake
= (slp
->ns_rec
|| (slp
->ns_flag
& (SLP_NEEDQ
| SLP_DISCONN
)));
2284 lck_rw_done(&slp
->ns_rwlock
);
2285 if (wake
&& nfs_numnfsd
) {
2286 lck_mtx_lock(nfsd_mutex
);
2287 nfsrv_wakenfsd(slp
);
2288 lck_mtx_unlock(nfsd_mutex
);
2294 * Try and extract an RPC request from the mbuf data list received on a
2295 * stream socket. The "waitflag" argument indicates whether or not it
2299 nfsrv_getstream(slp
, waitflag
)
2300 struct nfssvc_sock
*slp
;
2304 char *cp1
, *cp2
, *mdata
;
2305 int len
, mlen
, error
;
2306 mbuf_t om
, m2
, recm
;
2309 if (slp
->ns_flag
& SLP_GETSTREAM
)
2310 panic("nfs getstream");
2311 slp
->ns_flag
|= SLP_GETSTREAM
;
2313 if (slp
->ns_reclen
== 0) {
2314 if (slp
->ns_cc
< NFSX_UNSIGNED
) {
2315 slp
->ns_flag
&= ~SLP_GETSTREAM
;
2319 mdata
= mbuf_data(m
);
2321 if (mlen
>= NFSX_UNSIGNED
) {
2322 bcopy(mdata
, (caddr_t
)&recmark
, NFSX_UNSIGNED
);
2323 mdata
+= NFSX_UNSIGNED
;
2324 mlen
-= NFSX_UNSIGNED
;
2325 mbuf_setdata(m
, mdata
, mlen
);
2327 cp1
= (caddr_t
)&recmark
;
2329 while (cp1
< ((caddr_t
)&recmark
) + NFSX_UNSIGNED
) {
2337 mbuf_setdata(m
, cp2
, mlen
);
2340 slp
->ns_cc
-= NFSX_UNSIGNED
;
2341 recmark
= ntohl(recmark
);
2342 slp
->ns_reclen
= recmark
& ~0x80000000;
2343 if (recmark
& 0x80000000)
2344 slp
->ns_flag
|= SLP_LASTFRAG
;
2346 slp
->ns_flag
&= ~SLP_LASTFRAG
;
2347 if (slp
->ns_reclen
< NFS_MINPACKET
|| slp
->ns_reclen
> NFS_MAXPACKET
) {
2348 slp
->ns_flag
&= ~SLP_GETSTREAM
;
2354 * Now get the record part.
2356 * Note that slp->ns_reclen may be 0. Linux sometimes
2357 * generates 0-length RPCs
2360 if (slp
->ns_cc
== slp
->ns_reclen
) {
2362 slp
->ns_raw
= slp
->ns_rawend
= NULL
;
2363 slp
->ns_cc
= slp
->ns_reclen
= 0;
2364 } else if (slp
->ns_cc
> slp
->ns_reclen
) {
2368 mdata
= mbuf_data(m
);
2370 while (len
< slp
->ns_reclen
) {
2371 if ((len
+ mlen
) > slp
->ns_reclen
) {
2372 if (mbuf_copym(m
, 0, slp
->ns_reclen
- len
, waitflag
, &m2
)) {
2373 slp
->ns_flag
&= ~SLP_GETSTREAM
;
2374 return (EWOULDBLOCK
);
2377 if (mbuf_setnext(om
, m2
)) {
2378 /* trouble... just drop it */
2379 printf("nfsrv_getstream: mbuf_setnext failed\n");
2381 slp
->ns_flag
&= ~SLP_GETSTREAM
;
2382 return (EWOULDBLOCK
);
2388 mdata
+= slp
->ns_reclen
- len
;
2389 mlen
-= slp
->ns_reclen
- len
;
2390 mbuf_setdata(m
, mdata
, mlen
);
2391 len
= slp
->ns_reclen
;
2392 } else if ((len
+ mlen
) == slp
->ns_reclen
) {
2397 if (mbuf_setnext(om
, NULL
)) {
2398 printf("nfsrv_getstream: mbuf_setnext failed 2\n");
2399 slp
->ns_flag
&= ~SLP_GETSTREAM
;
2400 return (EWOULDBLOCK
);
2403 mdata
= mbuf_data(m
);
2409 mdata
= mbuf_data(m
);
2416 slp
->ns_flag
&= ~SLP_GETSTREAM
;
2421 * Accumulate the fragments into a record.
2423 if (slp
->ns_frag
== NULL
) {
2424 slp
->ns_frag
= recm
;
2427 while ((m2
= mbuf_next(m
)))
2429 if ((error
= mbuf_setnext(m
, recm
)))
2430 panic("nfsrv_getstream: mbuf_setnext failed 3, %d\n", error
);
2432 if (slp
->ns_flag
& SLP_LASTFRAG
) {
2434 mbuf_setnextpkt(slp
->ns_recend
, slp
->ns_frag
);
2436 slp
->ns_rec
= slp
->ns_frag
;
2437 slp
->ns_recend
= slp
->ns_frag
;
2438 slp
->ns_frag
= NULL
;
2444 * Parse an RPC header.
2447 nfsrv_dorec(slp
, nfsd
, ndp
)
2448 struct nfssvc_sock
*slp
;
2450 struct nfsrv_descript
**ndp
;
2454 struct nfsrv_descript
*nd
;
2458 if ((slp
->ns_flag
& SLP_VALID
) == 0 || (slp
->ns_rec
== NULL
))
2460 MALLOC_ZONE(nd
, struct nfsrv_descript
*,
2461 sizeof (struct nfsrv_descript
), M_NFSRVDESC
, M_WAITOK
);
2465 slp
->ns_rec
= mbuf_nextpkt(m
);
2467 mbuf_setnextpkt(m
, NULL
);
2469 slp
->ns_recend
= NULL
;
2470 if (mbuf_type(m
) == MBUF_TYPE_SONAME
) {
2473 if ((error
= mbuf_setnext(nam
, NULL
)))
2474 panic("nfsrv_dorec: mbuf_setnext failed %d\n", error
);
2477 nd
->nd_md
= nd
->nd_mrep
= m
;
2479 nd
->nd_dpos
= mbuf_data(m
);
2480 error
= nfs_getreq(nd
, nfsd
, TRUE
);
2484 FREE_ZONE((caddr_t
)nd
, sizeof *nd
, M_NFSRVDESC
);
2493 * Parse an RPC request
2495 * - fill in the cred struct.
2498 nfs_getreq(nd
, nfsd
, has_header
)
2499 struct nfsrv_descript
*nd
;
2507 caddr_t dpos
, cp2
, cp
;
2508 u_long nfsvers
, auth_type
;
2510 int error
= 0, ticklen
;
2512 struct nfsuid
*nuidp
;
2516 struct ucred temp_cred
;
2517 struct timeval tvin
, tvout
, now
;
2518 char uio_buf
[ UIO_SIZEOF(1) ];
2519 #if 0 /* until encrypted keys are implemented */
2520 NFSKERBKEYSCHED_T keys
; /* stores key schedule */
2529 nfsm_dissect(tl
, u_long
*, 10 * NFSX_UNSIGNED
);
2530 nd
->nd_retxid
= fxdr_unsigned(u_long
, *tl
++);
2531 if (*tl
++ != rpc_call
) {
2536 nfsm_dissect(tl
, u_long
*, 8 * NFSX_UNSIGNED
);
2539 if (*tl
++ != rpc_vers
) {
2540 nd
->nd_repstat
= ERPCMISMATCH
;
2541 nd
->nd_procnum
= NFSPROC_NOOP
;
2544 if (*tl
!= nfs_prog
) {
2545 nd
->nd_repstat
= EPROGUNAVAIL
;
2546 nd
->nd_procnum
= NFSPROC_NOOP
;
2550 nfsvers
= fxdr_unsigned(u_long
, *tl
++);
2551 if ((nfsvers
< NFS_VER2
) || (nfsvers
> NFS_VER3
)) {
2552 nd
->nd_repstat
= EPROGMISMATCH
;
2553 nd
->nd_procnum
= NFSPROC_NOOP
;
2556 else if (nfsvers
== NFS_VER3
)
2557 nd
->nd_flag
= ND_NFSV3
;
2558 nd
->nd_procnum
= fxdr_unsigned(u_long
, *tl
++);
2559 if (nd
->nd_procnum
== NFSPROC_NULL
)
2561 if ((nd
->nd_procnum
>= NFS_NPROCS
) ||
2562 (!nd
->nd_flag
&& nd
->nd_procnum
> NFSV2PROC_STATFS
)) {
2563 nd
->nd_repstat
= EPROCUNAVAIL
;
2564 nd
->nd_procnum
= NFSPROC_NOOP
;
2567 if ((nd
->nd_flag
& ND_NFSV3
) == 0)
2568 nd
->nd_procnum
= nfsv3_procid
[nd
->nd_procnum
];
2570 len
= fxdr_unsigned(int, *tl
++);
2571 if (len
< 0 || len
> RPCAUTH_MAXSIZ
) {
2576 nd
->nd_flag
&= ~ND_KERBAUTH
;
2578 * Handle auth_unix or auth_kerb.
2580 if (auth_type
== rpc_auth_unix
) {
2581 len
= fxdr_unsigned(int, *++tl
);
2582 if (len
< 0 || len
> NFS_MAXNAMLEN
) {
2586 bzero(&temp_cred
, sizeof(temp_cred
));
2587 nfsm_adv(nfsm_rndup(len
));
2588 nfsm_dissect(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
2589 user_id
= fxdr_unsigned(uid_t
, *tl
++);
2590 group_id
= fxdr_unsigned(gid_t
, *tl
++);
2591 temp_cred
.cr_groups
[0] = group_id
;
2592 len
= fxdr_unsigned(int, *tl
);
2593 if (len
< 0 || len
> RPCAUTH_UNIXGIDS
) {
2597 nfsm_dissect(tl
, u_long
*, (len
+ 2) * NFSX_UNSIGNED
);
2598 for (i
= 1; i
<= len
; i
++)
2600 temp_cred
.cr_groups
[i
] = fxdr_unsigned(gid_t
, *tl
++);
2603 ngroups
= (len
>= NGROUPS
) ? NGROUPS
: (len
+ 1);
2605 nfsrvw_sort(&temp_cred
.cr_groups
[0], ngroups
);
2606 len
= fxdr_unsigned(int, *++tl
);
2607 if (len
< 0 || len
> RPCAUTH_MAXSIZ
) {
2611 temp_cred
.cr_uid
= user_id
;
2612 temp_cred
.cr_ngroups
= ngroups
;
2613 nd
->nd_cr
= kauth_cred_create(&temp_cred
);
2614 if (nd
->nd_cr
== NULL
) {
2615 nd
->nd_repstat
= ENOMEM
;
2616 nd
->nd_procnum
= NFSPROC_NOOP
;
2620 nfsm_adv(nfsm_rndup(len
));
2621 } else if (auth_type
== rpc_auth_kerb
) {
2622 switch (fxdr_unsigned(int, *tl
++)) {
2623 case RPCAKN_FULLNAME
:
2624 ticklen
= fxdr_unsigned(int, *tl
);
2625 *((u_long
*)nfsd
->nfsd_authstr
) = *tl
;
2626 uiop
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_READ
,
2627 &uio_buf
[0], sizeof(uio_buf
));
2629 nd
->nd_repstat
= ENOMEM
;
2630 nd
->nd_procnum
= NFSPROC_NOOP
;
2634 // LP64todo - fix this
2635 nfsd
->nfsd_authlen
= (nfsm_rndup(ticklen
) + (NFSX_UNSIGNED
* 2));
2636 if ((nfsm_rndup(ticklen
) + NFSX_UNSIGNED
) > (len
- 2 * NFSX_UNSIGNED
)) {
2640 uio_addiov(uiop
, CAST_USER_ADDR_T(&nfsd
->nfsd_authstr
[4]), RPCAUTH_MAXSIZ
- 4);
2641 // LP64todo - fix this
2642 nfsm_mtouio(uiop
, uio_resid(uiop
));
2643 nfsm_dissect(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
2644 if (*tl
++ != rpc_auth_kerb
||
2645 fxdr_unsigned(int, *tl
) != 4 * NFSX_UNSIGNED
) {
2646 printf("Bad kerb verifier\n");
2647 nd
->nd_repstat
= (NFSERR_AUTHERR
|AUTH_BADVERF
);
2648 nd
->nd_procnum
= NFSPROC_NOOP
;
2651 nfsm_dissect(cp
, caddr_t
, 4 * NFSX_UNSIGNED
);
2653 if (fxdr_unsigned(int, *tl
) != RPCAKN_FULLNAME
) {
2654 printf("Not fullname kerb verifier\n");
2655 nd
->nd_repstat
= (NFSERR_AUTHERR
|AUTH_BADVERF
);
2656 nd
->nd_procnum
= NFSPROC_NOOP
;
2659 cp
+= NFSX_UNSIGNED
;
2660 bcopy(cp
, nfsd
->nfsd_verfstr
, 3 * NFSX_UNSIGNED
);
2661 nfsd
->nfsd_verflen
= 3 * NFSX_UNSIGNED
;
2662 nd
->nd_flag
|= ND_KERBFULL
;
2663 nfsd
->nfsd_flag
|= NFSD_NEEDAUTH
;
2665 case RPCAKN_NICKNAME
:
2666 if (len
!= 2 * NFSX_UNSIGNED
) {
2667 printf("Kerb nickname short\n");
2668 nd
->nd_repstat
= (NFSERR_AUTHERR
|AUTH_BADCRED
);
2669 nd
->nd_procnum
= NFSPROC_NOOP
;
2672 nickuid
= fxdr_unsigned(uid_t
, *tl
);
2673 nfsm_dissect(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
2674 if (*tl
++ != rpc_auth_kerb
||
2675 fxdr_unsigned(int, *tl
) != 3 * NFSX_UNSIGNED
) {
2676 printf("Kerb nick verifier bad\n");
2677 nd
->nd_repstat
= (NFSERR_AUTHERR
|AUTH_BADVERF
);
2678 nd
->nd_procnum
= NFSPROC_NOOP
;
2681 nfsm_dissect(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
2682 tvin
.tv_sec
= *tl
++;
2685 for (nuidp
= NUIDHASH(nfsd
->nfsd_slp
,nickuid
)->lh_first
;
2686 nuidp
!= 0; nuidp
= nuidp
->nu_hash
.le_next
) {
2687 if (kauth_cred_getuid(nuidp
->nu_cr
) == nickuid
&&
2689 netaddr_match(NU_NETFAM(nuidp
),
2690 &nuidp
->nu_haddr
, nd
->nd_nam2
)))
2695 (NFSERR_AUTHERR
|AUTH_REJECTCRED
);
2696 nd
->nd_procnum
= NFSPROC_NOOP
;
2701 * Now, decrypt the timestamp using the session key
2708 tvout
.tv_sec
= fxdr_unsigned(long, tvout
.tv_sec
);
2709 tvout
.tv_usec
= fxdr_unsigned(long, tvout
.tv_usec
);
2711 if (nuidp
->nu_expire
< now
.tv_sec
||
2712 nuidp
->nu_timestamp
.tv_sec
> tvout
.tv_sec
||
2713 (nuidp
->nu_timestamp
.tv_sec
== tvout
.tv_sec
&&
2714 nuidp
->nu_timestamp
.tv_usec
> tvout
.tv_usec
)) {
2715 nuidp
->nu_expire
= 0;
2717 (NFSERR_AUTHERR
|AUTH_REJECTVERF
);
2718 nd
->nd_procnum
= NFSPROC_NOOP
;
2721 bzero(&temp_cred
, sizeof(temp_cred
));
2722 ngroups
= nuidp
->nu_cr
->cr_ngroups
;
2723 for (i
= 0; i
< ngroups
; i
++)
2724 temp_cred
.cr_groups
[i
] = nuidp
->nu_cr
->cr_groups
[i
];
2726 nfsrvw_sort(&temp_cred
.cr_groups
[0], ngroups
);
2728 temp_cred
.cr_uid
= kauth_cred_getuid(nuidp
->nu_cr
);
2729 temp_cred
.cr_ngroups
= ngroups
;
2730 nd
->nd_cr
= kauth_cred_create(&temp_cred
);
2732 nd
->nd_repstat
= ENOMEM
;
2733 nd
->nd_procnum
= NFSPROC_NOOP
;
2736 nd
->nd_flag
|= ND_KERBNICK
;
2739 nd
->nd_repstat
= (NFSERR_AUTHERR
| AUTH_REJECTCRED
);
2740 nd
->nd_procnum
= NFSPROC_NOOP
;
2748 if (IS_VALID_CRED(nd
->nd_cr
))
2749 kauth_cred_unref(&nd
->nd_cr
);
2754 * Search for a sleeping nfsd and wake it up.
2755 * SIDE EFFECT: If none found, set NFSD_CHECKSLP flag, so that one of the
2756 * running nfsds will go look for the work in the nfssvc_sock list.
2757 * Note: Must be called with nfsd_mutex held.
2760 nfsrv_wakenfsd(struct nfssvc_sock
*slp
)
2764 if ((slp
->ns_flag
& SLP_VALID
) == 0)
2767 lck_rw_lock_exclusive(&slp
->ns_rwlock
);
2770 TAILQ_FOREACH(nd
, &nfsd_head
, nfsd_chain
) {
2771 if (nd
->nfsd_flag
& NFSD_WAITING
) {
2772 nd
->nfsd_flag
&= ~NFSD_WAITING
;
2774 panic("nfsd wakeup");
2777 lck_rw_done(&slp
->ns_rwlock
);
2778 wakeup((caddr_t
)nd
);
2784 slp
->ns_flag
|= SLP_DOREC
;
2786 lck_rw_done(&slp
->ns_rwlock
);
2788 nfsd_head_flag
|= NFSD_CHECKSLP
;
2790 #endif /* NFS_NOSERVER */
2801 tpr
= tprintf_open(p
);
2805 tprintf(tpr
, "nfs server %s: %s, error %d\n", server
, msg
,
2808 tprintf(tpr
, "nfs server %s: %s\n", server
, msg
);
2814 nfs_down(nmp
, proc
, error
, flags
, msg
)
2815 struct nfsmount
*nmp
;
2822 if ((flags
& NFSSTA_TIMEO
) && !(nmp
->nm_state
& NFSSTA_TIMEO
)) {
2823 vfs_event_signal(&vfs_statfs(nmp
->nm_mountp
)->f_fsid
, VQ_NOTRESP
, 0);
2824 nmp
->nm_state
|= NFSSTA_TIMEO
;
2826 if ((flags
& NFSSTA_LOCKTIMEO
) && !(nmp
->nm_state
& NFSSTA_LOCKTIMEO
)) {
2827 vfs_event_signal(&vfs_statfs(nmp
->nm_mountp
)->f_fsid
, VQ_NOTRESPLOCK
, 0);
2828 nmp
->nm_state
|= NFSSTA_LOCKTIMEO
;
2830 nfs_msg(proc
, vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, msg
, error
);
2834 nfs_up(nmp
, proc
, flags
, msg
)
2835 struct nfsmount
*nmp
;
2843 nfs_msg(proc
, vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, msg
, 0);
2844 if ((flags
& NFSSTA_TIMEO
) && (nmp
->nm_state
& NFSSTA_TIMEO
)) {
2845 nmp
->nm_state
&= ~NFSSTA_TIMEO
;
2846 vfs_event_signal(&vfs_statfs(nmp
->nm_mountp
)->f_fsid
, VQ_NOTRESP
, 1);
2848 if ((flags
& NFSSTA_LOCKTIMEO
) && (nmp
->nm_state
& NFSSTA_LOCKTIMEO
)) {
2849 nmp
->nm_state
&= ~NFSSTA_LOCKTIMEO
;
2850 vfs_event_signal(&vfs_statfs(nmp
->nm_mountp
)->f_fsid
, VQ_NOTRESPLOCK
, 1);