2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
32 * Copyright (c) 1989, 1991, 1993, 1995
33 * The Regents of the University of California. All rights reserved.
35 * This code is derived from software contributed to Berkeley by
36 * Rick Macklem at The University of Guelph.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)nfs_socket.c 8.5 (Berkeley) 3/30/95
67 * FreeBSD-Id: nfs_socket.c,v 1.30 1997/10/28 15:59:07 bde Exp $
71 * Socket operations for use by nfs
74 #include <sys/param.h>
75 #include <sys/systm.h>
77 #include <sys/kauth.h>
78 #include <sys/mount_internal.h>
79 #include <sys/kernel.h>
80 #include <sys/kpi_mbuf.h>
81 #include <sys/malloc.h>
82 #include <sys/vnode.h>
83 #include <sys/domain.h>
84 #include <sys/protosw.h>
85 #include <sys/socket.h>
86 #include <sys/syslog.h>
87 #include <sys/tprintf.h>
88 #include <sys/uio_internal.h>
89 #include <libkern/OSAtomic.h>
92 #include <kern/clock.h>
93 #include <kern/task.h>
94 #include <kern/thread.h>
97 #include <netinet/in.h>
98 #include <netinet/tcp.h>
100 #include <nfs/rpcv2.h>
101 #include <nfs/nfsproto.h>
103 #include <nfs/xdr_subs.h>
104 #include <nfs/nfsm_subs.h>
105 #include <nfs/nfsmount.h>
106 #include <nfs/nfsnode.h>
107 #include <nfs/nfsrtt.h>
109 #include <sys/kdebug.h>
111 #define FSDBG(A, B, C, D, E) \
112 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_NONE, \
113 (int)(B), (int)(C), (int)(D), (int)(E), 0)
114 #define FSDBG_TOP(A, B, C, D, E) \
115 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_START, \
116 (int)(B), (int)(C), (int)(D), (int)(E), 0)
117 #define FSDBG_BOT(A, B, C, D, E) \
118 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_END, \
119 (int)(B), (int)(C), (int)(D), (int)(E), 0)
122 * Estimate rto for an nfs rpc sent via. an unreliable datagram.
123 * Use the mean and mean deviation of rtt for the appropriate type of rpc
124 * for the frequent rpcs and a default for the others.
125 * The justification for doing "other" this way is that these rpcs
126 * happen so infrequently that timer est. would probably be stale.
127 * Also, since many of these rpcs are
128 * non-idempotent, a conservative timeout is desired.
129 * getattr, lookup - A+2D
133 #define NFS_RTO(n, t) \
134 ((t) == 0 ? (n)->nm_timeo : \
136 (((((n)->nm_srtt[t-1] + 3) >> 2) + (n)->nm_sdrtt[t-1] + 1) >> 1) : \
137 ((((n)->nm_srtt[t-1] + 7) >> 3) + (n)->nm_sdrtt[t-1] + 1)))
138 #define NFS_SRTT(r) (r)->r_nmp->nm_srtt[proct[(r)->r_procnum] - 1]
139 #define NFS_SDRTT(r) (r)->r_nmp->nm_sdrtt[proct[(r)->r_procnum] - 1]
141 * External data, mostly RPC constants in XDR form
143 extern u_long rpc_reply
, rpc_msgdenied
, rpc_mismatch
, rpc_vers
, rpc_auth_unix
,
144 rpc_msgaccepted
, rpc_call
, rpc_autherr
,
146 extern u_long nfs_prog
;
147 extern struct nfsstats nfsstats
;
148 extern int nfsv3_procid
[NFS_NPROCS
];
149 extern int nfs_ticks
;
150 extern u_long nfs_xidwrap
;
153 * Defines which timer to use for the procnum.
160 static int proct
[NFS_NPROCS
] = {
161 0, 1, 0, 2, 1, 3, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0
165 * There is a congestion window for outstanding rpcs maintained per mount
166 * point. The cwnd size is adjusted in roughly the way that:
167 * Van Jacobson, Congestion avoidance and Control, In "Proceedings of
168 * SIGCOMM '88". ACM, August 1988.
169 * describes for TCP. The cwnd size is chopped in half on a retransmit timeout
170 * and incremented by 1/cwnd when each rpc reply is received and a full cwnd
171 * of rpcs is in progress.
172 * (The sent count and cwnd are scaled for integer arith.)
173 * Variants of "slow start" were tried and were found to be too much of a
174 * performance hit (ave. rtt 3 times larger),
175 * I suspect due to the large rtt that nfs rpcs have.
177 #define NFS_CWNDSCALE 256
178 #define NFS_MAXCWND (NFS_CWNDSCALE * 32)
179 static int nfs_backoff
[8] = { 2, 4, 8, 16, 32, 64, 128, 256, };
181 struct nfsrtt nfsrtt
;
183 static int nfs_rcvlock(struct nfsreq
*);
184 static void nfs_rcvunlock(struct nfsreq
*);
185 static int nfs_receive(struct nfsreq
*rep
, mbuf_t
*mp
);
186 static int nfs_reconnect(struct nfsreq
*rep
);
187 static void nfs_repdequeue(struct nfsreq
*rep
);
190 boolean_t
current_thread_aborted(void);
191 kern_return_t
thread_terminate(thread_t
);
194 static int nfsrv_getstream(struct nfssvc_sock
*,int);
196 int (*nfsrv3_procs
[NFS_NPROCS
])(struct nfsrv_descript
*nd
,
197 struct nfssvc_sock
*slp
,
224 #endif /* NFS_NOSERVER */
228 * attempt to bind a socket to a reserved port
231 nfs_bind_resv(struct nfsmount
*nmp
)
233 socket_t so
= nmp
->nm_so
;
234 struct sockaddr_in sin
;
241 sin
.sin_len
= sizeof (struct sockaddr_in
);
242 sin
.sin_family
= AF_INET
;
243 sin
.sin_addr
.s_addr
= INADDR_ANY
;
244 tport
= IPPORT_RESERVED
- 1;
245 sin
.sin_port
= htons(tport
);
247 while (((error
= sock_bind(so
, (struct sockaddr
*) &sin
)) == EADDRINUSE
) &&
248 (--tport
> IPPORT_RESERVED
/ 2))
249 sin
.sin_port
= htons(tport
);
254 * variables for managing the nfs_bind_resv_thread
256 int nfs_resv_mounts
= 0;
257 static int nfs_bind_resv_thread_state
= 0;
258 #define NFS_BIND_RESV_THREAD_STATE_INITTED 1
259 #define NFS_BIND_RESV_THREAD_STATE_RUNNING 2
260 lck_grp_t
*nfs_bind_resv_lck_grp
;
261 lck_grp_attr_t
*nfs_bind_resv_lck_grp_attr
;
262 lck_attr_t
*nfs_bind_resv_lck_attr
;
263 lck_mtx_t
*nfs_bind_resv_mutex
;
264 struct nfs_bind_resv_request
{
265 TAILQ_ENTRY(nfs_bind_resv_request
) brr_chain
;
266 struct nfsmount
*brr_nmp
;
269 static TAILQ_HEAD(, nfs_bind_resv_request
) nfs_bind_resv_request_queue
;
272 * thread to handle any reserved port bind requests
275 nfs_bind_resv_thread(void)
277 struct nfs_bind_resv_request
*brreq
;
279 nfs_bind_resv_thread_state
= NFS_BIND_RESV_THREAD_STATE_RUNNING
;
281 while (nfs_resv_mounts
> 0) {
282 lck_mtx_lock(nfs_bind_resv_mutex
);
283 while ((brreq
= TAILQ_FIRST(&nfs_bind_resv_request_queue
))) {
284 TAILQ_REMOVE(&nfs_bind_resv_request_queue
, brreq
, brr_chain
);
285 lck_mtx_unlock(nfs_bind_resv_mutex
);
286 brreq
->brr_error
= nfs_bind_resv(brreq
->brr_nmp
);
288 lck_mtx_lock(nfs_bind_resv_mutex
);
290 msleep((caddr_t
)&nfs_bind_resv_request_queue
,
291 nfs_bind_resv_mutex
, PSOCK
| PDROP
,
292 "nfs_bind_resv_request_queue", 0);
295 nfs_bind_resv_thread_state
= NFS_BIND_RESV_THREAD_STATE_INITTED
;
296 (void) thread_terminate(current_thread());
300 nfs_bind_resv_thread_wake(void)
302 if (nfs_bind_resv_thread_state
< NFS_BIND_RESV_THREAD_STATE_RUNNING
)
304 wakeup(&nfs_bind_resv_request_queue
);
309 * underprivileged procs call this to request nfs_bind_resv_thread
310 * to perform the reserved port binding for them.
313 nfs_bind_resv_nopriv(struct nfsmount
*nmp
)
315 struct nfs_bind_resv_request brreq
;
318 if (nfs_bind_resv_thread_state
< NFS_BIND_RESV_THREAD_STATE_RUNNING
) {
319 if (nfs_bind_resv_thread_state
< NFS_BIND_RESV_THREAD_STATE_INITTED
) {
320 nfs_bind_resv_lck_grp_attr
= lck_grp_attr_alloc_init();
321 nfs_bind_resv_lck_grp
= lck_grp_alloc_init("nfs_bind_resv", nfs_bind_resv_lck_grp_attr
);
322 nfs_bind_resv_lck_attr
= lck_attr_alloc_init();
323 nfs_bind_resv_mutex
= lck_mtx_alloc_init(nfs_bind_resv_lck_grp
, nfs_bind_resv_lck_attr
);
324 TAILQ_INIT(&nfs_bind_resv_request_queue
);
325 nfs_bind_resv_thread_state
= NFS_BIND_RESV_THREAD_STATE_INITTED
;
327 kernel_thread(kernel_task
, nfs_bind_resv_thread
);
328 nfs_bind_resv_thread_state
= NFS_BIND_RESV_THREAD_STATE_RUNNING
;
334 lck_mtx_lock(nfs_bind_resv_mutex
);
335 TAILQ_INSERT_TAIL(&nfs_bind_resv_request_queue
, &brreq
, brr_chain
);
336 lck_mtx_unlock(nfs_bind_resv_mutex
);
338 error
= nfs_bind_resv_thread_wake();
340 TAILQ_REMOVE(&nfs_bind_resv_request_queue
, &brreq
, brr_chain
);
341 /* Note: we might be able to simply restart the thread */
345 tsleep((caddr_t
)&brreq
, PSOCK
, "nfsbindresv", 0);
347 return (brreq
.brr_error
);
351 * Initialize sockets and congestion for a new NFS connection.
352 * We do not free the sockaddr if error.
356 struct nfsmount
*nmp
,
357 __unused
struct nfsreq
*rep
)
360 int error
, rcvreserve
, sndreserve
;
361 struct sockaddr
*saddr
;
362 struct timeval timeo
;
365 saddr
= mbuf_data(nmp
->nm_nam
);
366 error
= sock_socket(saddr
->sa_family
, nmp
->nm_sotype
,
367 nmp
->nm_soproto
, 0, 0, &nmp
->nm_so
);
374 * Some servers require that the client port be a reserved port number.
376 if (saddr
->sa_family
== AF_INET
&& (nmp
->nm_flag
& NFSMNT_RESVPORT
)) {
379 * sobind() requires current_proc() to have superuser privs.
380 * If this bind is part of a reconnect, and the current proc
381 * doesn't have superuser privs, we hand the sobind() off to
382 * a kernel thread to process.
384 if ((nmp
->nm_state
& NFSSTA_MOUNTED
) &&
385 (p
= current_proc()) && suser(kauth_cred_get(), 0)) {
386 /* request nfs_bind_resv_thread() to do bind */
387 error
= nfs_bind_resv_nopriv(nmp
);
389 error
= nfs_bind_resv(nmp
);
396 * Protocols that do not require connections may be optionally left
397 * unconnected for servers that reply from a port other than NFS_PORT.
399 if (nmp
->nm_flag
& NFSMNT_NOCONN
) {
400 if (nmp
->nm_sotype
== SOCK_STREAM
) {
408 error
= sock_connect(so
, mbuf_data(nmp
->nm_nam
), MSG_DONTWAIT
);
409 if (error
&& error
!= EINPROGRESS
) {
413 while ((error
= sock_connectwait(so
, &tv
)) == EINPROGRESS
) {
414 if (rep
&& (error
= nfs_sigintr(nmp
, rep
, rep
->r_procp
))) {
421 * Always time out on recieve, this allows us to reconnect the
422 * socket to deal with network changes.
426 error
= sock_setsockopt(so
, SOL_SOCKET
, SO_RCVTIMEO
, &timeo
, sizeof(timeo
));
427 if (nmp
->nm_flag
& (NFSMNT_SOFT
| NFSMNT_INT
)) {
432 error
= sock_setsockopt(so
, SOL_SOCKET
, SO_SNDTIMEO
, &timeo
, sizeof(timeo
));
434 if (nmp
->nm_sotype
== SOCK_DGRAM
) {
435 sndreserve
= (nmp
->nm_wsize
+ NFS_MAXPKTHDR
) * 3;
436 rcvreserve
= (nmp
->nm_rsize
+ NFS_MAXPKTHDR
) *
437 (nmp
->nm_readahead
> 0 ? nmp
->nm_readahead
+ 1 : 2);
438 } else if (nmp
->nm_sotype
== SOCK_SEQPACKET
) {
439 sndreserve
= (nmp
->nm_wsize
+ NFS_MAXPKTHDR
) * 3;
440 rcvreserve
= (nmp
->nm_rsize
+ NFS_MAXPKTHDR
) *
441 (nmp
->nm_readahead
> 0 ? nmp
->nm_readahead
+ 1 : 2);
446 sock_gettype(so
, NULL
, NULL
, &proto
);
447 if (nmp
->nm_sotype
!= SOCK_STREAM
)
448 panic("nfscon sotype");
450 // Assume that SOCK_STREAM always requires a connection
451 sock_setsockopt(so
, SOL_SOCKET
, SO_KEEPALIVE
, &on
, sizeof(on
));
453 if (proto
== IPPROTO_TCP
) {
454 sock_setsockopt(so
, IPPROTO_TCP
, TCP_NODELAY
, &on
, sizeof(on
));
457 sndreserve
= (nmp
->nm_wsize
+ NFS_MAXPKTHDR
+ sizeof (u_long
)) * 3;
458 rcvreserve
= (nmp
->nm_rsize
+ NFS_MAXPKTHDR
+ sizeof (u_long
)) *
459 (nmp
->nm_readahead
> 0 ? nmp
->nm_readahead
+ 1 : 2);
462 if (sndreserve
> NFS_MAXSOCKBUF
)
463 sndreserve
= NFS_MAXSOCKBUF
;
464 if (rcvreserve
> NFS_MAXSOCKBUF
)
465 rcvreserve
= NFS_MAXSOCKBUF
;
466 error
= sock_setsockopt(so
, SOL_SOCKET
, SO_SNDBUF
, &sndreserve
, sizeof(sndreserve
));
470 error
= sock_setsockopt(so
, SOL_SOCKET
, SO_RCVBUF
, &rcvreserve
, sizeof(rcvreserve
));
475 sock_nointerrupt(so
, 1);
477 /* Initialize other non-zero congestion variables */
478 nmp
->nm_srtt
[0] = nmp
->nm_srtt
[1] = nmp
->nm_srtt
[2] =
479 nmp
->nm_srtt
[3] = (NFS_TIMEO
<< 3);
480 nmp
->nm_sdrtt
[0] = nmp
->nm_sdrtt
[1] = nmp
->nm_sdrtt
[2] =
481 nmp
->nm_sdrtt
[3] = 0;
482 nmp
->nm_cwnd
= NFS_MAXCWND
/ 2; /* Initial send window */
484 FSDBG(529, nmp
, nmp
->nm_state
, nmp
->nm_soflags
, nmp
->nm_cwnd
);
485 nmp
->nm_timeouts
= 0;
495 * Called when a connection is broken on a reliable protocol.
496 * - clean up the old socket
497 * - nfs_connect() again
498 * - set R_MUSTRESEND for all outstanding requests on mount point
499 * If this fails the mount point is DEAD!
500 * nb: Must be called with the nfs_sndlock() set on the mount point.
503 nfs_reconnect(struct nfsreq
*rep
)
506 struct nfsmount
*nmp
= rep
->r_nmp
;
510 while ((error
= nfs_connect(nmp
, rep
))) {
511 if (error
== EINTR
|| error
== ERESTART
)
515 nfs_down(rep
->r_nmp
, rep
->r_procp
, error
, NFSSTA_TIMEO
,
517 rep
->r_flags
|= R_TPRINTFMSG
;
518 if (!(nmp
->nm_state
& NFSSTA_MOUNTED
)) {
519 /* we're not yet completely mounted and */
520 /* we can't reconnect, so we fail */
523 if ((error
= nfs_sigintr(rep
->r_nmp
, rep
, rep
->r_procp
)))
525 tsleep((caddr_t
)&lbolt
, PSOCK
, "nfscon", 0);
529 * Loop through outstanding request list and fix up all requests
532 TAILQ_FOREACH(rp
, &nfs_reqq
, r_chain
) {
533 if (rp
->r_nmp
== nmp
)
534 rp
->r_flags
|= R_MUSTRESEND
;
540 * NFS disconnect. Clean up and unlink.
543 nfs_disconnect(struct nfsmount
*nmp
)
550 sock_shutdown(so
, 2);
556 * This is the nfs send routine. For connection based socket types, it
557 * must be called with an nfs_sndlock() on the socket.
558 * "rep == NULL" indicates that it has been called from a server.
559 * For the client side:
560 * - return EINTR if the RPC is terminated, 0 otherwise
561 * - set R_MUSTRESEND if the send fails for any reason
562 * - do any cleanup required by recoverable socket errors (???)
563 * For the server side:
564 * - return EINTR or ERESTART if interrupted by a signal
565 * - return EPIPE if a connection is lost for connection based sockets (TCP...)
566 * - do any cleanup required by recoverable socket errors (???)
569 nfs_send(so
, nam
, top
, rep
)
575 struct sockaddr
*sendnam
;
576 int error
, error2
, sotype
, flags
;
577 u_long xidqueued
= 0;
579 char savenametolog
[MAXPATHLEN
];
583 error
= nfs_sigintr(rep
->r_nmp
, rep
, rep
->r_procp
);
588 if ((so
= rep
->r_nmp
->nm_so
) == NULL
) {
589 rep
->r_flags
|= R_MUSTRESEND
;
593 rep
->r_flags
&= ~R_MUSTRESEND
;
594 TAILQ_FOREACH(rp
, &nfs_reqq
, r_chain
)
598 xidqueued
= rp
->r_xid
;
600 sock_gettype(so
, NULL
, &sotype
, NULL
);
601 if ((sotype
== SOCK_STREAM
) || (sock_isconnected(so
)) ||
603 sendnam
= (struct sockaddr
*)0;
605 sendnam
= mbuf_data(nam
);
607 if (sotype
== SOCK_SEQPACKET
)
613 * Save the name here in case mount point goes away if we block.
614 * The name is using local stack and is large, but don't
615 * want to block if we malloc.
618 strncpy(savenametolog
,
619 vfs_statfs(rep
->r_nmp
->nm_mountp
)->f_mntfromname
,
621 bzero(&msg
, sizeof(msg
));
622 msg
.msg_name
= (caddr_t
)sendnam
;
623 msg
.msg_namelen
= sendnam
== 0 ? 0 : sendnam
->sa_len
;
624 error
= sock_sendmbuf(so
, &msg
, top
, flags
, NULL
);
629 TAILQ_FOREACH(rp
, &nfs_reqq
, r_chain
)
630 if (rp
== rep
&& rp
->r_xid
== xidqueued
)
633 panic("nfs_send: error %d xid %x gone",
636 log(LOG_INFO
, "nfs send error %d for server %s\n",
637 error
, savenametolog
);
639 * Deal with errors for the client side.
641 error2
= nfs_sigintr(rep
->r_nmp
, rep
, rep
->r_procp
);
645 rep
->r_flags
|= R_MUSTRESEND
;
648 log(LOG_INFO
, "nfsd send error %d\n", error
);
651 * Handle any recoverable (soft) socket errors here. (???)
653 if (error
!= EINTR
&& error
!= ERESTART
&& error
!= EIO
&&
654 error
!= EWOULDBLOCK
&& error
!= EPIPE
) {
662 * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all
663 * done by soreceive(), but for SOCK_STREAM we must deal with the Record
664 * Mark and consolidate the data into a new mbuf list.
665 * nb: Sometimes TCP passes the data up to soreceive() in long lists of
667 * For SOCK_STREAM we must be very careful to read an entire record once
668 * we have read any of it, even if the system call has been interrupted.
671 nfs_receive(struct nfsreq
*rep
, mbuf_t
*mp
)
677 int error
, error2
, sotype
;
678 proc_t p
= current_proc(); /* XXX */
684 * Set up arguments for soreceive()
687 sotype
= rep
->r_nmp
->nm_sotype
;
690 * For reliable protocols, lock against other senders/receivers
691 * in case a reconnect is necessary.
692 * For SOCK_STREAM, first get the Record Mark to find out how much
693 * more there is to get.
694 * We must lock the socket against other receivers
695 * until we have an entire rpc request/reply.
697 if (sotype
!= SOCK_DGRAM
) {
698 error
= nfs_sndlock(rep
);
703 * Check for fatal errors and resending request.
706 * Ugh: If a reconnect attempt just happened, nm_so
707 * would have changed. NULL indicates a failed
708 * attempt that has essentially shut down this
711 if ((error
= nfs_sigintr(rep
->r_nmp
, rep
, p
)) || rep
->r_mrep
) {
717 so
= rep
->r_nmp
->nm_so
;
719 error
= nfs_reconnect(rep
);
726 while (rep
->r_flags
& R_MUSTRESEND
) {
727 error
= mbuf_copym(rep
->r_mreq
, 0, MBUF_COPYALL
, MBUF_WAITOK
, &m
);
729 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpcretries
);
730 error
= nfs_send(so
, rep
->r_nmp
->nm_nam
, m
, rep
);
733 * we also hold rcv lock so rep is still
737 if (error
== EINTR
|| error
== ERESTART
||
738 (error
= nfs_reconnect(rep
))) {
746 if (sotype
== SOCK_STREAM
) {
751 while (!error
&& !lastfragment
) {
752 aio
.iov_base
= (uintptr_t) &fraglen
;
753 aio
.iov_len
= sizeof(u_long
);
754 bzero(&msg
, sizeof(msg
));
755 msg
.msg_iov
= (struct iovec
*) &aio
;
758 error
= sock_receive(so
, &msg
, MSG_WAITALL
, &rcvlen
);
759 if (!rep
->r_nmp
) /* if unmounted then bailout */
761 if (error
== EWOULDBLOCK
&& rep
) {
762 error2
= nfs_sigintr(rep
->r_nmp
, rep
, p
);
766 } while (error
== EWOULDBLOCK
);
767 if (!error
&& rcvlen
< aio
.iov_len
) {
768 /* only log a message if we got a partial word */
771 "short receive (%d/%d) from nfs server %s\n",
772 rcvlen
, sizeof(u_long
),
773 vfs_statfs(rep
->r_nmp
->nm_mountp
)->f_mntfromname
);
778 lastfragment
= ntohl(fraglen
) & 0x80000000;
779 fraglen
= ntohl(fraglen
) & ~0x80000000;
782 * This is SERIOUS! We are out of sync with the sender
783 * and forcing a disconnect/reconnect is all I can do.
785 if (len
> NFS_MAXPACKET
) {
786 log(LOG_ERR
, "%s (%d) from nfs server %s\n",
787 "impossible RPC record length", len
,
788 vfs_statfs(rep
->r_nmp
->nm_mountp
)->f_mntfromname
);
796 error
= sock_receivembuf(so
, NULL
, &m
, MSG_WAITALL
, &rcvlen
);
797 if (!rep
->r_nmp
) /* if unmounted then bailout */ {
800 } while (error
== EWOULDBLOCK
|| error
== EINTR
||
803 if (!error
&& fraglen
> rcvlen
) {
805 "short receive (%d/%d) from nfs server %s\n",
807 vfs_statfs(rep
->r_nmp
->nm_mountp
)->f_mntfromname
);
816 error
= mbuf_setnext(mlast
, m
);
818 printf("nfs_receive: mbuf_setnext failed %d\n", error
);
822 while (mbuf_next(mlast
))
823 mlast
= mbuf_next(mlast
);
827 bzero(&msg
, sizeof(msg
));
830 error
= sock_receivembuf(so
, &msg
, mp
, 0, &rcvlen
);
831 if (!rep
->r_nmp
) /* if unmounted then bailout */ {
834 if (error
== EWOULDBLOCK
&& rep
) {
835 error2
= nfs_sigintr(rep
->r_nmp
, rep
, p
);
840 } while (error
== EWOULDBLOCK
);
842 if ((msg
.msg_flags
& MSG_EOR
) == 0)
844 if (!error
&& *mp
== NULL
)
849 if (error
&& error
!= EINTR
&& error
!= ERESTART
) {
854 "receive error %d from nfs server %s\n", error
,
855 vfs_statfs(rep
->r_nmp
->nm_mountp
)->f_mntfromname
);
856 error
= nfs_sndlock(rep
);
858 error
= nfs_reconnect(rep
);
866 * We could have failed while rebinding the datagram socket
867 * so we need to attempt to rebind here.
869 if ((so
= rep
->r_nmp
->nm_so
) == NULL
) {
870 error
= nfs_sndlock(rep
);
872 error
= nfs_reconnect(rep
);
877 if (!rep
->r_nmp
) /* if unmounted then bailout */
879 so
= rep
->r_nmp
->nm_so
;
881 bzero(&msg
, sizeof(msg
));
885 error
= sock_receivembuf(so
, &msg
, mp
, 0, &rcvlen
);
886 if (!rep
->r_nmp
) /* if unmounted then bailout */
889 error2
= nfs_sigintr(rep
->r_nmp
, rep
, p
);
895 /* Reconnect for all errors. We may be receiving
896 * soft/hard/blocking errors because of a network
898 * XXX: we should rate limit or delay this
899 * to once every N attempts or something.
900 * although TCP doesn't seem to.
903 error2
= nfs_sndlock(rep
);
905 error2
= nfs_reconnect(rep
);
908 else if (!rep
->r_nmp
) /* if unmounted then bailout */
911 so
= rep
->r_nmp
->nm_so
;
917 } while (error
== EWOULDBLOCK
);
928 * Implement receipt of reply on a socket.
929 * We must search through the list of received datagrams matching them
930 * with outstanding requests using the xid, until ours is found.
935 struct nfsreq
*myrep
;
938 struct nfsmount
*nmp
= myrep
->r_nmp
;
946 * Loop around until we get our own reply
950 * Lock against other receivers so that I don't get stuck in
951 * sbwait() after someone else has received my reply for me.
952 * Also necessary for connection based protocols to avoid
953 * race conditions during a reconnect.
954 * If nfs_rcvlock() returns EALREADY, that means that
955 * the reply has already been recieved by another
956 * process and we can return immediately. In this
957 * case, the lock is not taken to avoid races with
960 error
= nfs_rcvlock(myrep
);
961 if (error
== EALREADY
)
967 * If we slept after putting bits otw, then reply may have
968 * arrived. In which case returning is required, or we
969 * would hang trying to nfs_receive an already received reply.
971 if (myrep
->r_mrep
!= NULL
) {
972 nfs_rcvunlock(myrep
);
973 FSDBG(530, myrep
->r_xid
, myrep
, myrep
->r_nmp
, -1);
977 * Get the next Rpc reply off the socket. Assume myrep->r_nmp
978 * is still intact by checks done in nfs_rcvlock.
980 error
= nfs_receive(myrep
, &mrep
);
982 * Bailout asap if nfsmount struct gone (unmounted).
985 FSDBG(530, myrep
->r_xid
, myrep
, nmp
, -2);
991 FSDBG(530, myrep
->r_xid
, myrep
, nmp
, error
);
992 nfs_rcvunlock(myrep
);
994 /* Bailout asap if nfsmount struct gone (unmounted). */
1002 * Ignore routing errors on connectionless protocols??
1004 if (NFSIGNORE_SOERROR(nmp
->nm_sotype
, error
)) {
1007 int optlen
= sizeof(clearerror
);
1008 sock_getsockopt(nmp
->nm_so
, SOL_SOCKET
, SO_ERROR
, &clearerror
, &optlen
);
1018 * We assume all is fine, but if we did not have an error
1019 * and mrep is 0, better not dereference it. nfs_receive
1020 * calls soreceive which carefully sets error=0 when it got
1021 * errors on sbwait (tsleep). In most cases, I assume that's
1022 * so we could go back again. In tcp case, EPIPE is returned.
1023 * In udp, case nfs_receive gets back here with no error and no
1024 * mrep. Is the right fix to have soreceive check for process
1025 * aborted after sbwait and return something non-zero? Should
1026 * nfs_receive give an EPIPE? Too risky to play with those
1027 * two this late in game for a shutdown problem. Instead,
1028 * just check here and get out. (ekn)
1031 nfs_rcvunlock(myrep
);
1032 FSDBG(530, myrep
->r_xid
, myrep
, nmp
, -3);
1033 return (ENXIO
); /* sounds good */
1037 * Get the xid and check that it is an rpc reply
1040 dpos
= mbuf_data(md
);
1041 nfsm_dissect(tl
, u_long
*, 2*NFSX_UNSIGNED
);
1043 if (*tl
!= rpc_reply
) {
1044 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpcinvalid
);
1047 if (nmp
->nm_state
& NFSSTA_RCVLOCK
)
1048 nfs_rcvunlock(myrep
);
1053 * Loop through the request list to match up the reply
1054 * Iff no match, just drop the datagram
1056 TAILQ_FOREACH(rep
, &nfs_reqq
, r_chain
) {
1057 if (rep
->r_mrep
== NULL
&& rxid
== rep
->r_xid
) {
1063 * If we're tracking the round trip time
1064 * then we update the circular log here
1065 * with the stats from our current request.
1070 rt
= &nfsrtt
.rttl
[nfsrtt
.pos
];
1071 rt
->proc
= rep
->r_procnum
;
1072 rt
->rto
= NFS_RTO(nmp
, proct
[rep
->r_procnum
]);
1073 rt
->sent
= nmp
->nm_sent
;
1074 rt
->cwnd
= nmp
->nm_cwnd
;
1075 if (proct
[rep
->r_procnum
] == 0)
1076 panic("nfs_reply: proct[%d] is zero", rep
->r_procnum
);
1077 rt
->srtt
= nmp
->nm_srtt
[proct
[rep
->r_procnum
] - 1];
1078 rt
->sdrtt
= nmp
->nm_sdrtt
[proct
[rep
->r_procnum
] - 1];
1079 rt
->fsid
= vfs_statfs(nmp
->nm_mountp
)->f_fsid
;
1080 microtime(&rt
->tstamp
); // XXX unused
1081 if (rep
->r_flags
& R_TIMING
)
1082 rt
->rtt
= rep
->r_rtt
;
1085 nfsrtt
.pos
= (nfsrtt
.pos
+ 1) % NFSRTTLOGSIZ
;
1088 * Update congestion window.
1089 * Do the additive increase of
1092 FSDBG(530, rep
->r_xid
, rep
, nmp
->nm_sent
,
1094 if (nmp
->nm_cwnd
<= nmp
->nm_sent
) {
1096 (NFS_CWNDSCALE
* NFS_CWNDSCALE
+
1097 (nmp
->nm_cwnd
>> 1)) / nmp
->nm_cwnd
;
1098 if (nmp
->nm_cwnd
> NFS_MAXCWND
)
1099 nmp
->nm_cwnd
= NFS_MAXCWND
;
1101 if (rep
->r_flags
& R_SENT
) {
1102 rep
->r_flags
&= ~R_SENT
;
1103 nmp
->nm_sent
-= NFS_CWNDSCALE
;
1106 * Update rtt using a gain of 0.125 on the mean
1107 * and a gain of 0.25 on the deviation.
1109 if (rep
->r_flags
& R_TIMING
) {
1111 * Since the timer resolution of
1112 * NFS_HZ is so course, it can often
1113 * result in r_rtt == 0. Since
1114 * r_rtt == N means that the actual
1115 * rtt is between N+dt and N+2-dt ticks,
1118 if (proct
[rep
->r_procnum
] == 0)
1119 panic("nfs_reply: proct[%d] is zero", rep
->r_procnum
);
1120 t1
= rep
->r_rtt
+ 1;
1121 t1
-= (NFS_SRTT(rep
) >> 3);
1122 NFS_SRTT(rep
) += t1
;
1125 t1
-= (NFS_SDRTT(rep
) >> 2);
1126 NFS_SDRTT(rep
) += t1
;
1128 nmp
->nm_timeouts
= 0;
1132 nfs_rcvunlock(myrep
);
1134 * If not matched to a request, drop it.
1135 * If it's mine, get out.
1138 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpcunexpected
);
1140 } else if (rep
== myrep
) {
1141 if (rep
->r_mrep
== NULL
)
1142 panic("nfs_reply: nil r_mrep");
1145 FSDBG(530, myrep
->r_xid
, myrep
, rep
,
1146 rep
? rep
->r_xid
: myrep
->r_flags
);
1151 * nfs_request - goes something like this
1152 * - fill in request struct
1153 * - links it into list
1154 * - calls nfs_send() for first transmit
1155 * - calls nfs_receive() to get reply
1156 * - break down rpc header and return with nfs reply pointed to
1158 * nb: always frees up mreq mbuf list
1161 nfs_request(vp
, mp
, mrest
, procnum
, procp
, cred
, mrp
, mdp
, dposp
, xidp
)
1174 struct nfsreq re
, *rep
;
1177 struct nfsmount
*nmp
;
1178 mbuf_t md
, mheadend
;
1179 char nickv
[RPCX_NICKVERF
];
1182 int t1
, error
= 0, mrest_len
, auth_len
, auth_type
;
1183 int trylater_delay
= NFS_TRYLATERDEL
, failed_auth
= 0;
1184 int verf_len
, verf_type
;
1186 char *auth_str
, *verf_str
;
1187 NFSKERBKEY_T key
; /* save session key */
1200 nmp
= VFSTONFS(vnode_mount(vp
));
1202 (nmp
->nm_state
& (NFSSTA_FORCE
|NFSSTA_TIMEO
)) ==
1203 (NFSSTA_FORCE
|NFSSTA_TIMEO
)) {
1207 nmsotype
= nmp
->nm_sotype
;
1209 FSDBG_TOP(531, vp
, procnum
, nmp
, rep
);
1213 rep
->r_procp
= procp
;
1214 rep
->r_procnum
= procnum
;
1216 rep
->r_lastmsg
= now
.tv_sec
-
1217 ((nmp
->nm_tprintf_delay
) - (nmp
->nm_tprintf_initial_delay
));
1227 * Get the RPC header with authorization.
1230 nmp
= vp
? VFSTONFS(vnode_mount(vp
)) : rep
->r_nmp
;
1232 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1236 verf_str
= auth_str
= (char *)0;
1237 if (nmp
->nm_flag
& NFSMNT_KERB
) {
1239 verf_len
= sizeof (nickv
);
1240 auth_type
= RPCAUTH_KERB4
;
1241 bzero((caddr_t
)key
, sizeof (key
));
1242 if (failed_auth
|| nfs_getnickauth(nmp
, cred
, &auth_str
,
1243 &auth_len
, verf_str
, verf_len
)) {
1244 nmp
= vp
? VFSTONFS(vnode_mount(vp
)) : rep
->r_nmp
;
1246 FSDBG_BOT(531, 2, vp
, error
, rep
);
1250 error
= nfs_getauth(nmp
, rep
, cred
, &auth_str
,
1251 &auth_len
, verf_str
, &verf_len
, key
);
1252 nmp
= vp
? VFSTONFS(vnode_mount(vp
)) : rep
->r_nmp
;
1256 FSDBG_BOT(531, 2, vp
, error
, rep
);
1262 auth_type
= RPCAUTH_UNIX
;
1263 if (cred
->cr_ngroups
< 1)
1264 panic("nfsreq nogrps");
1265 auth_len
= ((((cred
->cr_ngroups
- 1) > nmp
->nm_numgrps
) ?
1266 nmp
->nm_numgrps
: (cred
->cr_ngroups
- 1)) << 2) +
1269 error
= nfsm_rpchead(cred
, nmp
->nm_flag
, procnum
, auth_type
, auth_len
,
1270 auth_str
, verf_len
, verf_str
, mrest
, mrest_len
, &mheadend
, &xid
, &m
);
1272 _FREE(auth_str
, M_TEMP
);
1275 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1279 *xidp
= ntohl(xid
) + ((u_int64_t
)nfs_xidwrap
<< 32);
1282 * For stream protocols, insert a Sun RPC Record Mark.
1284 if (nmsotype
== SOCK_STREAM
) {
1285 error
= mbuf_prepend(&m
, NFSX_UNSIGNED
, MBUF_WAITOK
);
1288 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1291 *((u_long
*)mbuf_data(m
)) =
1292 htonl(0x80000000 | (mbuf_pkthdr_len(m
) - NFSX_UNSIGNED
));
1297 nmp
= vp
? VFSTONFS(vnode_mount(vp
)) : rep
->r_nmp
;
1298 if (nmp
&& (nmp
->nm_flag
& NFSMNT_SOFT
))
1299 rep
->r_retry
= nmp
->nm_retry
;
1301 rep
->r_retry
= NFS_MAXREXMIT
+ 1; /* past clip limit */
1302 rep
->r_rtt
= rep
->r_rexmit
= 0;
1303 if (proct
[procnum
] > 0)
1304 rep
->r_flags
= R_TIMING
;
1310 * Do the client side RPC.
1312 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpcrequests
);
1314 * Chain request into list of outstanding requests. Be sure
1315 * to put it LAST so timer finds oldest requests first.
1317 TAILQ_INSERT_TAIL(&nfs_reqq
, rep
, r_chain
);
1320 * If backing off another request or avoiding congestion, don't
1321 * send this one now but let timer do it. If not timing a request,
1324 if (nmp
&& nmp
->nm_so
&& (nmp
->nm_sotype
!= SOCK_DGRAM
||
1325 (nmp
->nm_flag
& NFSMNT_DUMBTIMR
) ||
1326 nmp
->nm_sent
< nmp
->nm_cwnd
)) {
1327 int connrequired
= (nmp
->nm_sotype
== SOCK_STREAM
);
1330 error
= nfs_sndlock(rep
);
1333 * Set the R_SENT before doing the send in case another thread
1334 * processes the reply before the nfs_send returns here
1337 if ((rep
->r_flags
& R_MUSTRESEND
) == 0) {
1338 FSDBG(531, rep
->r_xid
, rep
, nmp
->nm_sent
,
1340 nmp
->nm_sent
+= NFS_CWNDSCALE
;
1341 rep
->r_flags
|= R_SENT
;
1344 error
= mbuf_copym(m
, 0, MBUF_COPYALL
, MBUF_WAITOK
, &m2
);
1346 error
= nfs_send(nmp
->nm_so
, nmp
->nm_nam
, m2
, rep
);
1350 nmp
= vp
? VFSTONFS(vnode_mount(vp
)) : rep
->r_nmp
;
1353 nmp
->nm_sent
-= NFS_CWNDSCALE
;
1354 rep
->r_flags
&= ~R_SENT
;
1361 * Wait for the reply from our send or the timer's.
1363 if (!error
|| error
== EPIPE
)
1364 error
= nfs_reply(rep
);
1367 * RPC done, unlink the request.
1369 nfs_repdequeue(rep
);
1371 nmp
= vp
? VFSTONFS(vnode_mount(vp
)) : rep
->r_nmp
;
1374 * Decrement the outstanding request count.
1376 if (rep
->r_flags
& R_SENT
) {
1377 rep
->r_flags
&= ~R_SENT
; /* paranoia */
1379 FSDBG(531, rep
->r_xid
, rep
, nmp
->nm_sent
, nmp
->nm_cwnd
);
1380 nmp
->nm_sent
-= NFS_CWNDSCALE
;
1385 * If there was a successful reply and a tprintf msg.
1386 * tprintf a response.
1389 nfs_up(nmp
, procp
, NFSSTA_TIMEO
,
1390 (rep
->r_flags
& R_TPRINTFMSG
) ? "is alive again" : NULL
);
1397 mbuf_freem(rep
->r_mreq
);
1398 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1403 * break down the rpc header and check if ok
1405 nfsm_dissect(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
1406 if (*tl
++ == rpc_msgdenied
) {
1407 if (*tl
== rpc_mismatch
)
1409 else if ((nmp
->nm_flag
& NFSMNT_KERB
) && *tl
++ == rpc_autherr
) {
1412 error
= mbuf_setnext(mheadend
, NULL
);
1414 mbuf_freem(rep
->r_mreq
);
1417 printf("nfs_request: mbuf_setnext failed\n");
1423 mbuf_freem(rep
->r_mreq
);
1424 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1429 * Grab any Kerberos verifier, otherwise just throw it away.
1431 verf_type
= fxdr_unsigned(int, *tl
++);
1432 i
= fxdr_unsigned(int, *tl
);
1433 if ((nmp
->nm_flag
& NFSMNT_KERB
) && verf_type
== RPCAUTH_KERB4
) {
1434 error
= nfs_savenickauth(nmp
, cred
, i
, key
, &md
, &dpos
, mrep
);
1438 nfsm_adv(nfsm_rndup(i
));
1439 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
1442 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
1444 error
= fxdr_unsigned(int, *tl
);
1445 if ((nmp
->nm_flag
& NFSMNT_NFSV3
) &&
1446 error
== NFSERR_TRYLATER
) {
1450 waituntil
= now
.tv_sec
+ trylater_delay
;
1451 while (now
.tv_sec
< waituntil
) {
1452 tsleep((caddr_t
)&lbolt
, PSOCK
, "nfstrylater", 0);
1455 trylater_delay
*= 2;
1456 if (trylater_delay
> 60)
1457 trylater_delay
= 60;
1462 * If the File Handle was stale, invalidate the
1463 * lookup cache, just in case.
1465 if ((error
== ESTALE
) && vp
)
1467 if (nmp
->nm_flag
& NFSMNT_NFSV3
) {
1471 error
|= NFSERR_RETERR
;
1474 error
&= ~NFSERR_RETERR
;
1476 mbuf_freem(rep
->r_mreq
);
1477 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1484 mbuf_freem(rep
->r_mreq
);
1485 FSDBG_BOT(531, 0xf0f0f0f0, rep
->r_xid
, nmp
, rep
);
1489 error
= EPROTONOSUPPORT
;
1491 mbuf_freem(rep
->r_mreq
);
1492 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1496 #ifndef NFS_NOSERVER
1498 * Generate the rpc reply header
1499 * siz arg. is used to decide if adding a cluster is worthwhile
1502 nfs_rephead(siz
, nd
, slp
, err
, mrq
, mbp
, bposp
)
1504 struct nfsrv_descript
*nd
;
1505 struct nfssvc_sock
*slp
;
1518 * If this is a big reply, use a cluster else
1519 * try and leave leading space for the lower level headers.
1521 siz
+= RPC_REPLYSIZ
;
1522 if (siz
>= nfs_mbuf_minclsize
) {
1523 error
= mbuf_getpacket(MBUF_WAITOK
, &mreq
);
1525 error
= mbuf_gethdr(MBUF_WAITOK
, MBUF_TYPE_DATA
, &mreq
);
1528 /* unable to allocate packet */
1533 tl
= mbuf_data(mreq
);
1534 mlen
= 6 * NFSX_UNSIGNED
;
1535 if (siz
< nfs_mbuf_minclsize
) {
1536 /* leave space for lower level headers */
1537 tl
+= 80/sizeof(*tl
); /* XXX max_hdr? XXX */
1538 mbuf_setdata(mreq
, tl
, mlen
);
1540 mbuf_setlen(mreq
, mlen
);
1542 bpos
= ((caddr_t
)tl
) + mlen
;
1543 *tl
++ = txdr_unsigned(nd
->nd_retxid
);
1545 if (err
== ERPCMISMATCH
|| (err
& NFSERR_AUTHERR
)) {
1546 *tl
++ = rpc_msgdenied
;
1547 if (err
& NFSERR_AUTHERR
) {
1548 *tl
++ = rpc_autherr
;
1549 *tl
= txdr_unsigned(err
& ~NFSERR_AUTHERR
);
1550 mlen
-= NFSX_UNSIGNED
;
1551 mbuf_setlen(mreq
, mlen
);
1552 bpos
-= NFSX_UNSIGNED
;
1554 *tl
++ = rpc_mismatch
;
1555 *tl
++ = txdr_unsigned(RPC_VER2
);
1556 *tl
= txdr_unsigned(RPC_VER2
);
1559 *tl
++ = rpc_msgaccepted
;
1562 * For Kerberos authentication, we must send the nickname
1563 * verifier back, otherwise just RPCAUTH_NULL.
1565 if (nd
->nd_flag
& ND_KERBFULL
) {
1566 struct nfsuid
*nuidp
;
1567 struct timeval ktvin
, ktvout
;
1568 uid_t uid
= kauth_cred_getuid(nd
->nd_cr
);
1570 lck_rw_lock_shared(&slp
->ns_rwlock
);
1571 for (nuidp
= NUIDHASH(slp
, uid
)->lh_first
;
1572 nuidp
!= 0; nuidp
= nuidp
->nu_hash
.le_next
) {
1573 if (kauth_cred_getuid(nuidp
->nu_cr
) == uid
&&
1574 (!nd
->nd_nam2
|| netaddr_match(NU_NETFAM(nuidp
),
1575 &nuidp
->nu_haddr
, nd
->nd_nam2
)))
1580 txdr_unsigned(nuidp
->nu_timestamp
.tv_sec
- 1);
1582 txdr_unsigned(nuidp
->nu_timestamp
.tv_usec
);
1585 * Encrypt the timestamp in ecb mode using the
1592 *tl
++ = rpc_auth_kerb
;
1593 *tl
++ = txdr_unsigned(3 * NFSX_UNSIGNED
);
1594 *tl
= ktvout
.tv_sec
;
1595 nfsm_build(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
1596 *tl
++ = ktvout
.tv_usec
;
1597 *tl
++ = txdr_unsigned(kauth_cred_getuid(nuidp
->nu_cr
));
1602 lck_rw_done(&slp
->ns_rwlock
);
1609 *tl
= txdr_unsigned(RPC_PROGUNAVAIL
);
1612 *tl
= txdr_unsigned(RPC_PROGMISMATCH
);
1613 nfsm_build(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
1614 // XXX hard coded versions
1615 *tl
++ = txdr_unsigned(2);
1616 *tl
= txdr_unsigned(3);
1619 *tl
= txdr_unsigned(RPC_PROCUNAVAIL
);
1622 *tl
= txdr_unsigned(RPC_GARBAGE
);
1626 if (err
!= NFSERR_RETVOID
) {
1627 nfsm_build(tl
, u_long
*, NFSX_UNSIGNED
);
1629 *tl
= txdr_unsigned(nfsrv_errmap(nd
, err
));
1641 if (err
!= 0 && err
!= NFSERR_RETVOID
) {
1642 OSAddAtomic(1, (SInt32
*)&nfsstats
.srvrpc_errs
);
1648 #endif /* NFS_NOSERVER */
1652 * From FreeBSD 1.58, a Matt Dillon fix...
1653 * Flag a request as being about to terminate.
1654 * The nm_sent count is decremented now to avoid deadlocks when the process
1655 * in soreceive() hasn't yet managed to send its own request.
1658 nfs_softterm(struct nfsreq
*rep
)
1661 rep
->r_flags
|= R_SOFTTERM
;
1662 if (rep
->r_flags
& R_SENT
) {
1663 FSDBG(532, rep
->r_xid
, rep
, rep
->r_nmp
->nm_sent
,
1664 rep
->r_nmp
->nm_cwnd
);
1665 rep
->r_nmp
->nm_sent
-= NFS_CWNDSCALE
;
1666 rep
->r_flags
&= ~R_SENT
;
1671 nfs_timer_funnel(void * arg
)
1673 (void) thread_funnel_set(kernel_flock
, TRUE
);
1675 (void) thread_funnel_set(kernel_flock
, FALSE
);
1680 * Ensure rep isn't in use by the timer, then dequeue it.
1683 nfs_repdequeue(struct nfsreq
*rep
)
1686 while ((rep
->r_flags
& R_BUSY
)) {
1687 rep
->r_flags
|= R_WAITING
;
1688 tsleep(rep
, PSOCK
, "repdeq", 0);
1690 TAILQ_REMOVE(&nfs_reqq
, rep
, r_chain
);
1694 * Busy (lock) a nfsreq, used by the nfs timer to make sure it's not
1695 * free()'d out from under it.
1698 nfs_repbusy(struct nfsreq
*rep
)
1701 if ((rep
->r_flags
& R_BUSY
))
1702 panic("rep locked");
1703 rep
->r_flags
|= R_BUSY
;
1707 * Unbusy the nfsreq passed in, return the next nfsreq in the chain busied.
1709 static struct nfsreq
*
1710 nfs_repnext(struct nfsreq
*rep
)
1712 struct nfsreq
* nextrep
;
1717 * We need to get and busy the next req before signalling the
1718 * current one, otherwise wakeup() may block us and we'll race to
1719 * grab the next req.
1721 nextrep
= TAILQ_NEXT(rep
, r_chain
);
1722 if (nextrep
!= NULL
)
1723 nfs_repbusy(nextrep
);
1724 /* unbusy and signal. */
1725 rep
->r_flags
&= ~R_BUSY
;
1726 if ((rep
->r_flags
& R_WAITING
)) {
1727 rep
->r_flags
&= ~R_WAITING
;
1735 * Scan the nfsreq list and retranmit any requests that have timed out
1736 * To avoid retransmission attempts on STREAM sockets (in the future) make
1737 * sure to set the r_retry field to 0 (implies nm_retry == 0).
1740 nfs_timer(__unused
void *arg
)
1745 struct nfsmount
*nmp
;
1748 #ifndef NFS_NOSERVER
1749 struct nfssvc_sock
*slp
;
1751 #endif /* NFS_NOSERVER */
1752 int flags
, rexmit
, cwnd
, sent
;
1756 rep
= TAILQ_FIRST(&nfs_reqq
);
1760 for ( ; rep
!= NULL
; rep
= nfs_repnext(rep
)) {
1762 if (!nmp
) /* unmounted */
1764 if (rep
->r_mrep
|| (rep
->r_flags
& R_SOFTTERM
))
1766 if (nfs_sigintr(nmp
, rep
, rep
->r_procp
))
1768 if (nmp
->nm_tprintf_initial_delay
!= 0 &&
1769 (rep
->r_rexmit
> 2 || (rep
->r_flags
& R_RESENDERR
)) &&
1770 rep
->r_lastmsg
+ nmp
->nm_tprintf_delay
< now
.tv_sec
) {
1771 rep
->r_lastmsg
= now
.tv_sec
;
1772 nfs_down(rep
->r_nmp
, rep
->r_procp
, 0, NFSSTA_TIMEO
,
1774 rep
->r_flags
|= R_TPRINTFMSG
;
1775 if (!(nmp
->nm_state
& NFSSTA_MOUNTED
)) {
1776 /* we're not yet completely mounted and */
1777 /* we can't complete an RPC, so we fail */
1778 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpctimeouts
);
1783 if (rep
->r_rtt
>= 0) {
1785 if (nmp
->nm_flag
& NFSMNT_DUMBTIMR
)
1786 timeo
= nmp
->nm_timeo
;
1788 timeo
= NFS_RTO(nmp
, proct
[rep
->r_procnum
]);
1789 /* ensure 62.5 ms floor */
1790 while (16 * timeo
< hz
)
1792 if (nmp
->nm_timeouts
> 0)
1793 timeo
*= nfs_backoff
[nmp
->nm_timeouts
- 1];
1794 if (rep
->r_rtt
<= timeo
)
1796 if (nmp
->nm_timeouts
< 8)
1800 * Check for too many retransmits. This is never true for
1801 * 'hard' mounts because we set r_retry to NFS_MAXREXMIT + 1
1802 * and never allow r_rexmit to be more than NFS_MAXREXMIT.
1804 if (rep
->r_rexmit
>= rep
->r_retry
) { /* too many */
1805 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpctimeouts
);
1809 if (nmp
->nm_sotype
!= SOCK_DGRAM
) {
1810 if (++rep
->r_rexmit
> NFS_MAXREXMIT
)
1811 rep
->r_rexmit
= NFS_MAXREXMIT
;
1814 if ((so
= nmp
->nm_so
) == NULL
)
1818 * If there is enough space and the window allows..
1820 * Set r_rtt to -1 in case we fail to send it now.
1823 if (((nmp
->nm_flag
& NFSMNT_DUMBTIMR
) ||
1824 (rep
->r_flags
& R_SENT
) ||
1825 nmp
->nm_sent
< nmp
->nm_cwnd
) &&
1826 (mbuf_copym(rep
->r_mreq
, 0, MBUF_COPYALL
, MBUF_DONTWAIT
, &m
) == 0)){
1829 * Iff first send, start timing
1830 * else turn timing off, backoff timer
1831 * and divide congestion window by 2.
1832 * We update these *before* the send to avoid
1833 * racing against receiving the reply.
1834 * We save them so we can restore them on send error.
1836 flags
= rep
->r_flags
;
1837 rexmit
= rep
->r_rexmit
;
1838 cwnd
= nmp
->nm_cwnd
;
1839 sent
= nmp
->nm_sent
;
1841 if (rep
->r_flags
& R_SENT
) {
1842 rep
->r_flags
&= ~R_TIMING
;
1843 if (++rep
->r_rexmit
> NFS_MAXREXMIT
)
1844 rep
->r_rexmit
= NFS_MAXREXMIT
;
1846 if (nmp
->nm_cwnd
< NFS_CWNDSCALE
)
1847 nmp
->nm_cwnd
= NFS_CWNDSCALE
;
1848 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpcretries
);
1850 rep
->r_flags
|= R_SENT
;
1851 nmp
->nm_sent
+= NFS_CWNDSCALE
;
1853 FSDBG(535, xid
, rep
, nmp
->nm_sent
, nmp
->nm_cwnd
);
1855 bzero(&msg
, sizeof(msg
));
1856 if ((nmp
->nm_flag
& NFSMNT_NOCONN
) == NFSMNT_NOCONN
) {
1857 msg
.msg_name
= mbuf_data(nmp
->nm_nam
);
1858 msg
.msg_namelen
= mbuf_len(nmp
->nm_nam
);
1860 error
= sock_sendmbuf(so
, &msg
, m
, MSG_DONTWAIT
, NULL
);
1862 FSDBG(535, xid
, error
, sent
, cwnd
);
1865 if (error
== EWOULDBLOCK
) {
1866 rep
->r_flags
= flags
;
1867 rep
->r_rexmit
= rexmit
;
1868 nmp
->nm_cwnd
= cwnd
;
1869 nmp
->nm_sent
= sent
;
1873 if (NFSIGNORE_SOERROR(nmp
->nm_sotype
, error
)) {
1875 int optlen
= sizeof(clearerror
);
1876 sock_getsockopt(nmp
->nm_so
, SOL_SOCKET
, SO_ERROR
, &clearerror
, &optlen
);
1878 rep
->r_flags
= flags
| R_RESENDERR
;
1879 rep
->r_rexmit
= rexmit
;
1880 nmp
->nm_cwnd
= cwnd
;
1881 nmp
->nm_sent
= sent
;
1883 OSAddAtomic(-1, (SInt32
*)&nfsstats
.rpcretries
);
1890 #ifndef NFS_NOSERVER
1892 * Scan the write gathering queues for writes that need to be
1895 cur_usec
= (u_quad_t
)now
.tv_sec
* 1000000 + (u_quad_t
)now
.tv_usec
;
1896 lck_mtx_lock(nfsd_mutex
);
1897 TAILQ_FOREACH(slp
, &nfssvc_sockhead
, ns_chain
) {
1898 if (slp
->ns_wgtime
&& (slp
->ns_wgtime
<= cur_usec
))
1899 nfsrv_wakenfsd(slp
);
1901 while ((slp
= TAILQ_FIRST(&nfssvc_deadsockhead
))) {
1902 if ((slp
->ns_timestamp
+ 5) > now
.tv_sec
)
1904 TAILQ_REMOVE(&nfssvc_deadsockhead
, slp
, ns_chain
);
1907 lck_mtx_unlock(nfsd_mutex
);
1908 #endif /* NFS_NOSERVER */
1910 if (nfsbuffreeuptimestamp
+ 30 <= now
.tv_sec
) {
1912 * We haven't called nfs_buf_freeup() in a little while.
1913 * So, see if we can free up any stale/unused bufs now.
1918 timeout(nfs_timer_funnel
, (void *)0, nfs_ticks
);
1924 * Test for a termination condition pending on the process.
1925 * This is used to determine if we need to bail on a mount.
1926 * EIO is returned if there has been a soft timeout.
1927 * EINTR is returned if there is a signal pending that is not being ignored
1928 * and the mount is interruptable, or if we are a thread that is in the process
1929 * of cancellation (also SIGKILL posted).
1932 nfs_sigintr(nmp
, rep
, p
)
1933 struct nfsmount
*nmp
;
1937 sigset_t pending_sigs
;
1938 int context_good
= 0;
1939 struct nfsmount
*repnmp
;
1940 extern proc_t kernproc
;
1945 repnmp
= rep
->r_nmp
;
1946 /* we've had a forced unmount. */
1949 /* request has timed out on a 'soft' mount. */
1950 if (rep
->r_flags
& R_SOFTTERM
)
1953 * We're in the progress of a force unmount and there's
1954 * been a timeout we're dead and fail IO.
1956 if ((repnmp
->nm_state
& (NFSSTA_FORCE
|NFSSTA_TIMEO
)) ==
1957 (NFSSTA_FORCE
|NFSSTA_TIMEO
))
1959 /* Someone is unmounting us, go soft and mark it. */
1960 if (repnmp
->nm_mountp
->mnt_kern_flag
& MNTK_FRCUNMOUNT
) {
1961 repnmp
->nm_flag
|= NFSMNT_SOFT
;
1962 nmp
->nm_state
|= NFSSTA_FORCE
;
1965 * If the mount is hung and we've requested not to hang
1966 * on remote filesystems, then bail now.
1968 if (p
!= NULL
&& (proc_noremotehang(p
)) != 0 &&
1969 (repnmp
->nm_state
& NFSSTA_TIMEO
) != 0)
1972 /* XXX: is this valid? this probably should be an assertion. */
1976 /* Is this thread belongs to kernel task; then abort check is not needed */
1977 if ((current_proc() != kernproc
) && current_thread_aborted()) {
1980 /* mask off thread and process blocked signals. */
1982 pending_sigs
= proc_pendingsignals(p
, NFSINT_SIGMASK
);
1983 if (pending_sigs
&& (nmp
->nm_flag
& NFSMNT_INT
) != 0)
1989 * Lock a socket against others.
1990 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
1991 * and also to avoid race conditions between the processes with nfs requests
1992 * in progress when a reconnect is necessary.
2000 int error
, slpflag
= 0, slptimeo
= 0;
2002 if (rep
->r_nmp
== NULL
)
2004 statep
= &rep
->r_nmp
->nm_state
;
2007 if (rep
->r_nmp
->nm_flag
& NFSMNT_INT
)
2009 while (*statep
& NFSSTA_SNDLOCK
) {
2010 error
= nfs_sigintr(rep
->r_nmp
, rep
, p
);
2013 *statep
|= NFSSTA_WANTSND
;
2014 if (p
!= NULL
&& (proc_noremotehang(p
)) != 0)
2016 tsleep((caddr_t
)statep
, slpflag
| (PZERO
- 1), "nfsndlck", slptimeo
);
2017 if (slpflag
== PCATCH
) {
2022 * Make sure while we slept that the mountpoint didn't go away.
2023 * nfs_sigintr and callers expect it in tact.
2026 return (ENXIO
); /* don't have lock until out of loop */
2028 *statep
|= NFSSTA_SNDLOCK
;
2033 * Unlock the stream socket for others.
2041 if (rep
->r_nmp
== NULL
)
2043 statep
= &rep
->r_nmp
->nm_state
;
2044 if ((*statep
& NFSSTA_SNDLOCK
) == 0)
2045 panic("nfs sndunlock");
2046 *statep
&= ~NFSSTA_SNDLOCK
;
2047 if (*statep
& NFSSTA_WANTSND
) {
2048 *statep
&= ~NFSSTA_WANTSND
;
2049 wakeup((caddr_t
)statep
);
2054 nfs_rcvlock(struct nfsreq
*rep
)
2057 int error
, slpflag
, slptimeo
= 0;
2059 /* make sure we still have our mountpoint */
2061 if (rep
->r_mrep
!= NULL
)
2066 statep
= &rep
->r_nmp
->nm_state
;
2067 FSDBG_TOP(534, rep
->r_xid
, rep
, rep
->r_nmp
, *statep
);
2068 if (rep
->r_nmp
->nm_flag
& NFSMNT_INT
)
2072 while (*statep
& NFSSTA_RCVLOCK
) {
2073 if ((error
= nfs_sigintr(rep
->r_nmp
, rep
, rep
->r_procp
))) {
2074 FSDBG_BOT(534, rep
->r_xid
, rep
, rep
->r_nmp
, 0x100);
2076 } else if (rep
->r_mrep
!= NULL
) {
2078 * Don't bother sleeping if reply already arrived
2080 FSDBG_BOT(534, rep
->r_xid
, rep
, rep
->r_nmp
, 0x101);
2083 FSDBG(534, rep
->r_xid
, rep
, rep
->r_nmp
, 0x102);
2084 *statep
|= NFSSTA_WANTRCV
;
2086 * We need to poll if we're P_NOREMOTEHANG so that we
2087 * call nfs_sigintr periodically above.
2089 if (rep
->r_procp
!= NULL
&&
2090 (proc_noremotehang(rep
->r_procp
)) != 0)
2092 tsleep((caddr_t
)statep
, slpflag
| (PZERO
- 1), "nfsrcvlk", slptimeo
);
2093 if (slpflag
== PCATCH
) {
2098 * Make sure while we slept that the mountpoint didn't go away.
2099 * nfs_sigintr and caller nfs_reply expect it intact.
2102 FSDBG_BOT(534, rep
->r_xid
, rep
, rep
->r_nmp
, 0x103);
2103 return (ENXIO
); /* don't have lock until out of loop */
2107 * nfs_reply will handle it if reply already arrived.
2108 * (We may have slept or been preempted).
2110 FSDBG_BOT(534, rep
->r_xid
, rep
, rep
->r_nmp
, *statep
);
2111 *statep
|= NFSSTA_RCVLOCK
;
2116 * Unlock the stream socket for others.
2119 nfs_rcvunlock(struct nfsreq
*rep
)
2123 if (rep
->r_nmp
== NULL
)
2125 statep
= &rep
->r_nmp
->nm_state
;
2127 FSDBG(533, statep
, *statep
, 0, 0);
2128 if ((*statep
& NFSSTA_RCVLOCK
) == 0)
2129 panic("nfs rcvunlock");
2130 *statep
&= ~NFSSTA_RCVLOCK
;
2131 if (*statep
& NFSSTA_WANTRCV
) {
2132 *statep
&= ~NFSSTA_WANTRCV
;
2133 wakeup((caddr_t
)statep
);
2138 #ifndef NFS_NOSERVER
2140 * Socket upcall routine for the nfsd sockets.
2141 * The caddr_t arg is a pointer to the "struct nfssvc_sock".
2142 * Essentially do as much as possible non-blocking, else punt and it will
2143 * be called with MBUF_WAITOK from an nfsd.
2146 nfsrv_rcv(socket_t so
, caddr_t arg
, int waitflag
)
2148 struct nfssvc_sock
*slp
= (struct nfssvc_sock
*)arg
;
2150 if (!nfs_numnfsd
|| !(slp
->ns_flag
& SLP_VALID
))
2153 lck_rw_lock_exclusive(&slp
->ns_rwlock
);
2154 nfsrv_rcv_locked(so
, slp
, waitflag
);
2155 /* Note: ns_rwlock gets dropped when called with MBUF_DONTWAIT */
2158 nfsrv_rcv_locked(socket_t so
, struct nfssvc_sock
*slp
, int waitflag
)
2160 mbuf_t m
, mp
, mhck
, m2
;
2161 int ns_flag
=0, error
;
2165 if ((slp
->ns_flag
& SLP_VALID
) == 0) {
2166 if (waitflag
== MBUF_DONTWAIT
)
2167 lck_rw_done(&slp
->ns_rwlock
);
2173 * Define this to test for nfsds handling this under heavy load.
2175 if (waitflag
== MBUF_DONTWAIT
) {
2176 ns_flag
= SLP_NEEDQ
;
2180 if (slp
->ns_sotype
== SOCK_STREAM
) {
2182 * If there are already records on the queue, defer soreceive()
2183 * to an nfsd so that there is feedback to the TCP layer that
2184 * the nfs servers are heavily loaded.
2186 if (slp
->ns_rec
&& waitflag
== MBUF_DONTWAIT
) {
2187 ns_flag
= SLP_NEEDQ
;
2194 bytes_read
= 1000000000;
2195 error
= sock_receivembuf(so
, NULL
, &mp
, MSG_DONTWAIT
, &bytes_read
);
2196 if (error
|| mp
== NULL
) {
2197 if (error
== EWOULDBLOCK
)
2198 ns_flag
= SLP_NEEDQ
;
2200 ns_flag
= SLP_DISCONN
;
2204 if (slp
->ns_rawend
) {
2205 if ((error
= mbuf_setnext(slp
->ns_rawend
, m
)))
2206 panic("nfsrv_rcv: mbuf_setnext failed %d\n", error
);
2207 slp
->ns_cc
+= bytes_read
;
2210 slp
->ns_cc
= bytes_read
;
2212 while ((m2
= mbuf_next(m
)))
2217 * Now try and parse record(s) out of the raw stream data.
2219 error
= nfsrv_getstream(slp
, waitflag
);
2222 ns_flag
= SLP_DISCONN
;
2224 ns_flag
= SLP_NEEDQ
;
2227 struct sockaddr_storage nam
;
2229 bzero(&msg
, sizeof(msg
));
2230 msg
.msg_name
= (caddr_t
)&nam
;
2231 msg
.msg_namelen
= sizeof(nam
);
2234 bytes_read
= 1000000000;
2235 error
= sock_receivembuf(so
, &msg
, &mp
, MSG_DONTWAIT
| MSG_NEEDSA
, &bytes_read
);
2237 if (msg
.msg_name
&& (mbuf_get(MBUF_WAITOK
, MBUF_TYPE_SONAME
, &mhck
) == 0)) {
2238 mbuf_setlen(mhck
, nam
.ss_len
);
2239 bcopy(&nam
, mbuf_data(mhck
), nam
.ss_len
);
2241 if (mbuf_setnext(m
, mp
)) {
2242 /* trouble... just drop it */
2243 printf("nfsrv_rcv: mbuf_setnext failed\n");
2251 mbuf_setnextpkt(slp
->ns_recend
, m
);
2255 mbuf_setnextpkt(m
, NULL
);
2260 * This may be needed in the future to support
2261 * non-byte-stream connection-oriented protocols
2265 * This (slp->ns_sotype == SOCK_STREAM) should really
2266 * be a check for PR_CONNREQUIRED.
2268 if ((slp
->ns_sotype
== SOCK_STREAM
)
2269 && error
!= EWOULDBLOCK
) {
2270 ns_flag
= SLP_DISCONN
;
2279 * Now try and process the request records, non-blocking.
2283 slp
->ns_flag
|= ns_flag
;
2284 if (waitflag
== MBUF_DONTWAIT
) {
2285 int wake
= (slp
->ns_rec
|| (slp
->ns_flag
& (SLP_NEEDQ
| SLP_DISCONN
)));
2286 lck_rw_done(&slp
->ns_rwlock
);
2287 if (wake
&& nfs_numnfsd
) {
2288 lck_mtx_lock(nfsd_mutex
);
2289 nfsrv_wakenfsd(slp
);
2290 lck_mtx_unlock(nfsd_mutex
);
2296 * Try and extract an RPC request from the mbuf data list received on a
2297 * stream socket. The "waitflag" argument indicates whether or not it
2301 nfsrv_getstream(slp
, waitflag
)
2302 struct nfssvc_sock
*slp
;
2306 char *cp1
, *cp2
, *mdata
;
2307 int len
, mlen
, error
;
2308 mbuf_t om
, m2
, recm
;
2311 if (slp
->ns_flag
& SLP_GETSTREAM
)
2312 panic("nfs getstream");
2313 slp
->ns_flag
|= SLP_GETSTREAM
;
2315 if (slp
->ns_reclen
== 0) {
2316 if (slp
->ns_cc
< NFSX_UNSIGNED
) {
2317 slp
->ns_flag
&= ~SLP_GETSTREAM
;
2321 mdata
= mbuf_data(m
);
2323 if (mlen
>= NFSX_UNSIGNED
) {
2324 bcopy(mdata
, (caddr_t
)&recmark
, NFSX_UNSIGNED
);
2325 mdata
+= NFSX_UNSIGNED
;
2326 mlen
-= NFSX_UNSIGNED
;
2327 mbuf_setdata(m
, mdata
, mlen
);
2329 cp1
= (caddr_t
)&recmark
;
2331 while (cp1
< ((caddr_t
)&recmark
) + NFSX_UNSIGNED
) {
2339 mbuf_setdata(m
, cp2
, mlen
);
2342 slp
->ns_cc
-= NFSX_UNSIGNED
;
2343 recmark
= ntohl(recmark
);
2344 slp
->ns_reclen
= recmark
& ~0x80000000;
2345 if (recmark
& 0x80000000)
2346 slp
->ns_flag
|= SLP_LASTFRAG
;
2348 slp
->ns_flag
&= ~SLP_LASTFRAG
;
2349 if (slp
->ns_reclen
< NFS_MINPACKET
|| slp
->ns_reclen
> NFS_MAXPACKET
) {
2350 slp
->ns_flag
&= ~SLP_GETSTREAM
;
2356 * Now get the record part.
2358 * Note that slp->ns_reclen may be 0. Linux sometimes
2359 * generates 0-length RPCs
2362 if (slp
->ns_cc
== slp
->ns_reclen
) {
2364 slp
->ns_raw
= slp
->ns_rawend
= NULL
;
2365 slp
->ns_cc
= slp
->ns_reclen
= 0;
2366 } else if (slp
->ns_cc
> slp
->ns_reclen
) {
2370 mdata
= mbuf_data(m
);
2372 while (len
< slp
->ns_reclen
) {
2373 if ((len
+ mlen
) > slp
->ns_reclen
) {
2374 if (mbuf_copym(m
, 0, slp
->ns_reclen
- len
, waitflag
, &m2
)) {
2375 slp
->ns_flag
&= ~SLP_GETSTREAM
;
2376 return (EWOULDBLOCK
);
2379 if (mbuf_setnext(om
, m2
)) {
2380 /* trouble... just drop it */
2381 printf("nfsrv_getstream: mbuf_setnext failed\n");
2383 slp
->ns_flag
&= ~SLP_GETSTREAM
;
2384 return (EWOULDBLOCK
);
2390 mdata
+= slp
->ns_reclen
- len
;
2391 mlen
-= slp
->ns_reclen
- len
;
2392 mbuf_setdata(m
, mdata
, mlen
);
2393 len
= slp
->ns_reclen
;
2394 } else if ((len
+ mlen
) == slp
->ns_reclen
) {
2399 if (mbuf_setnext(om
, NULL
)) {
2400 printf("nfsrv_getstream: mbuf_setnext failed 2\n");
2401 slp
->ns_flag
&= ~SLP_GETSTREAM
;
2402 return (EWOULDBLOCK
);
2405 mdata
= mbuf_data(m
);
2411 mdata
= mbuf_data(m
);
2418 slp
->ns_flag
&= ~SLP_GETSTREAM
;
2423 * Accumulate the fragments into a record.
2425 if (slp
->ns_frag
== NULL
) {
2426 slp
->ns_frag
= recm
;
2429 while ((m2
= mbuf_next(m
)))
2431 if ((error
= mbuf_setnext(m
, recm
)))
2432 panic("nfsrv_getstream: mbuf_setnext failed 3, %d\n", error
);
2434 if (slp
->ns_flag
& SLP_LASTFRAG
) {
2436 mbuf_setnextpkt(slp
->ns_recend
, slp
->ns_frag
);
2438 slp
->ns_rec
= slp
->ns_frag
;
2439 slp
->ns_recend
= slp
->ns_frag
;
2440 slp
->ns_frag
= NULL
;
2446 * Parse an RPC header.
2449 nfsrv_dorec(slp
, nfsd
, ndp
)
2450 struct nfssvc_sock
*slp
;
2452 struct nfsrv_descript
**ndp
;
2456 struct nfsrv_descript
*nd
;
2460 if ((slp
->ns_flag
& SLP_VALID
) == 0 || (slp
->ns_rec
== NULL
))
2462 MALLOC_ZONE(nd
, struct nfsrv_descript
*,
2463 sizeof (struct nfsrv_descript
), M_NFSRVDESC
, M_WAITOK
);
2467 slp
->ns_rec
= mbuf_nextpkt(m
);
2469 mbuf_setnextpkt(m
, NULL
);
2471 slp
->ns_recend
= NULL
;
2472 if (mbuf_type(m
) == MBUF_TYPE_SONAME
) {
2475 if ((error
= mbuf_setnext(nam
, NULL
)))
2476 panic("nfsrv_dorec: mbuf_setnext failed %d\n", error
);
2479 nd
->nd_md
= nd
->nd_mrep
= m
;
2481 nd
->nd_dpos
= mbuf_data(m
);
2482 error
= nfs_getreq(nd
, nfsd
, TRUE
);
2486 FREE_ZONE((caddr_t
)nd
, sizeof *nd
, M_NFSRVDESC
);
2495 * Parse an RPC request
2497 * - fill in the cred struct.
2500 nfs_getreq(nd
, nfsd
, has_header
)
2501 struct nfsrv_descript
*nd
;
2509 caddr_t dpos
, cp2
, cp
;
2510 u_long nfsvers
, auth_type
;
2512 int error
= 0, ticklen
;
2514 struct nfsuid
*nuidp
;
2518 struct ucred temp_cred
;
2519 struct timeval tvin
, tvout
, now
;
2520 char uio_buf
[ UIO_SIZEOF(1) ];
2521 #if 0 /* until encrypted keys are implemented */
2522 NFSKERBKEYSCHED_T keys
; /* stores key schedule */
2531 nfsm_dissect(tl
, u_long
*, 10 * NFSX_UNSIGNED
);
2532 nd
->nd_retxid
= fxdr_unsigned(u_long
, *tl
++);
2533 if (*tl
++ != rpc_call
) {
2538 nfsm_dissect(tl
, u_long
*, 8 * NFSX_UNSIGNED
);
2541 if (*tl
++ != rpc_vers
) {
2542 nd
->nd_repstat
= ERPCMISMATCH
;
2543 nd
->nd_procnum
= NFSPROC_NOOP
;
2546 if (*tl
!= nfs_prog
) {
2547 nd
->nd_repstat
= EPROGUNAVAIL
;
2548 nd
->nd_procnum
= NFSPROC_NOOP
;
2552 nfsvers
= fxdr_unsigned(u_long
, *tl
++);
2553 if ((nfsvers
< NFS_VER2
) || (nfsvers
> NFS_VER3
)) {
2554 nd
->nd_repstat
= EPROGMISMATCH
;
2555 nd
->nd_procnum
= NFSPROC_NOOP
;
2558 else if (nfsvers
== NFS_VER3
)
2559 nd
->nd_flag
= ND_NFSV3
;
2560 nd
->nd_procnum
= fxdr_unsigned(u_long
, *tl
++);
2561 if (nd
->nd_procnum
== NFSPROC_NULL
)
2563 if ((nd
->nd_procnum
>= NFS_NPROCS
) ||
2564 (!nd
->nd_flag
&& nd
->nd_procnum
> NFSV2PROC_STATFS
)) {
2565 nd
->nd_repstat
= EPROCUNAVAIL
;
2566 nd
->nd_procnum
= NFSPROC_NOOP
;
2569 if ((nd
->nd_flag
& ND_NFSV3
) == 0)
2570 nd
->nd_procnum
= nfsv3_procid
[nd
->nd_procnum
];
2572 len
= fxdr_unsigned(int, *tl
++);
2573 if (len
< 0 || len
> RPCAUTH_MAXSIZ
) {
2578 nd
->nd_flag
&= ~ND_KERBAUTH
;
2580 * Handle auth_unix or auth_kerb.
2582 if (auth_type
== rpc_auth_unix
) {
2583 len
= fxdr_unsigned(int, *++tl
);
2584 if (len
< 0 || len
> NFS_MAXNAMLEN
) {
2588 bzero(&temp_cred
, sizeof(temp_cred
));
2589 nfsm_adv(nfsm_rndup(len
));
2590 nfsm_dissect(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
2591 user_id
= fxdr_unsigned(uid_t
, *tl
++);
2592 group_id
= fxdr_unsigned(gid_t
, *tl
++);
2593 temp_cred
.cr_groups
[0] = group_id
;
2594 len
= fxdr_unsigned(int, *tl
);
2595 if (len
< 0 || len
> RPCAUTH_UNIXGIDS
) {
2599 nfsm_dissect(tl
, u_long
*, (len
+ 2) * NFSX_UNSIGNED
);
2600 for (i
= 1; i
<= len
; i
++)
2602 temp_cred
.cr_groups
[i
] = fxdr_unsigned(gid_t
, *tl
++);
2605 ngroups
= (len
>= NGROUPS
) ? NGROUPS
: (len
+ 1);
2607 nfsrvw_sort(&temp_cred
.cr_groups
[0], ngroups
);
2608 len
= fxdr_unsigned(int, *++tl
);
2609 if (len
< 0 || len
> RPCAUTH_MAXSIZ
) {
2613 temp_cred
.cr_uid
= user_id
;
2614 temp_cred
.cr_ngroups
= ngroups
;
2615 nd
->nd_cr
= kauth_cred_create(&temp_cred
);
2616 if (nd
->nd_cr
== NULL
) {
2617 nd
->nd_repstat
= ENOMEM
;
2618 nd
->nd_procnum
= NFSPROC_NOOP
;
2622 nfsm_adv(nfsm_rndup(len
));
2623 } else if (auth_type
== rpc_auth_kerb
) {
2624 switch (fxdr_unsigned(int, *tl
++)) {
2625 case RPCAKN_FULLNAME
:
2626 ticklen
= fxdr_unsigned(int, *tl
);
2627 *((u_long
*)nfsd
->nfsd_authstr
) = *tl
;
2628 uiop
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_READ
,
2629 &uio_buf
[0], sizeof(uio_buf
));
2631 nd
->nd_repstat
= ENOMEM
;
2632 nd
->nd_procnum
= NFSPROC_NOOP
;
2636 // LP64todo - fix this
2637 nfsd
->nfsd_authlen
= (nfsm_rndup(ticklen
) + (NFSX_UNSIGNED
* 2));
2638 if ((nfsm_rndup(ticklen
) + NFSX_UNSIGNED
) > (len
- 2 * NFSX_UNSIGNED
)) {
2642 uio_addiov(uiop
, CAST_USER_ADDR_T(&nfsd
->nfsd_authstr
[4]), RPCAUTH_MAXSIZ
- 4);
2643 // LP64todo - fix this
2644 nfsm_mtouio(uiop
, uio_resid(uiop
));
2645 nfsm_dissect(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
2646 if (*tl
++ != rpc_auth_kerb
||
2647 fxdr_unsigned(int, *tl
) != 4 * NFSX_UNSIGNED
) {
2648 printf("Bad kerb verifier\n");
2649 nd
->nd_repstat
= (NFSERR_AUTHERR
|AUTH_BADVERF
);
2650 nd
->nd_procnum
= NFSPROC_NOOP
;
2653 nfsm_dissect(cp
, caddr_t
, 4 * NFSX_UNSIGNED
);
2655 if (fxdr_unsigned(int, *tl
) != RPCAKN_FULLNAME
) {
2656 printf("Not fullname kerb verifier\n");
2657 nd
->nd_repstat
= (NFSERR_AUTHERR
|AUTH_BADVERF
);
2658 nd
->nd_procnum
= NFSPROC_NOOP
;
2661 cp
+= NFSX_UNSIGNED
;
2662 bcopy(cp
, nfsd
->nfsd_verfstr
, 3 * NFSX_UNSIGNED
);
2663 nfsd
->nfsd_verflen
= 3 * NFSX_UNSIGNED
;
2664 nd
->nd_flag
|= ND_KERBFULL
;
2665 nfsd
->nfsd_flag
|= NFSD_NEEDAUTH
;
2667 case RPCAKN_NICKNAME
:
2668 if (len
!= 2 * NFSX_UNSIGNED
) {
2669 printf("Kerb nickname short\n");
2670 nd
->nd_repstat
= (NFSERR_AUTHERR
|AUTH_BADCRED
);
2671 nd
->nd_procnum
= NFSPROC_NOOP
;
2674 nickuid
= fxdr_unsigned(uid_t
, *tl
);
2675 nfsm_dissect(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
2676 if (*tl
++ != rpc_auth_kerb
||
2677 fxdr_unsigned(int, *tl
) != 3 * NFSX_UNSIGNED
) {
2678 printf("Kerb nick verifier bad\n");
2679 nd
->nd_repstat
= (NFSERR_AUTHERR
|AUTH_BADVERF
);
2680 nd
->nd_procnum
= NFSPROC_NOOP
;
2683 nfsm_dissect(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
2684 tvin
.tv_sec
= *tl
++;
2687 for (nuidp
= NUIDHASH(nfsd
->nfsd_slp
,nickuid
)->lh_first
;
2688 nuidp
!= 0; nuidp
= nuidp
->nu_hash
.le_next
) {
2689 if (kauth_cred_getuid(nuidp
->nu_cr
) == nickuid
&&
2691 netaddr_match(NU_NETFAM(nuidp
),
2692 &nuidp
->nu_haddr
, nd
->nd_nam2
)))
2697 (NFSERR_AUTHERR
|AUTH_REJECTCRED
);
2698 nd
->nd_procnum
= NFSPROC_NOOP
;
2703 * Now, decrypt the timestamp using the session key
2710 tvout
.tv_sec
= fxdr_unsigned(long, tvout
.tv_sec
);
2711 tvout
.tv_usec
= fxdr_unsigned(long, tvout
.tv_usec
);
2713 if (nuidp
->nu_expire
< now
.tv_sec
||
2714 nuidp
->nu_timestamp
.tv_sec
> tvout
.tv_sec
||
2715 (nuidp
->nu_timestamp
.tv_sec
== tvout
.tv_sec
&&
2716 nuidp
->nu_timestamp
.tv_usec
> tvout
.tv_usec
)) {
2717 nuidp
->nu_expire
= 0;
2719 (NFSERR_AUTHERR
|AUTH_REJECTVERF
);
2720 nd
->nd_procnum
= NFSPROC_NOOP
;
2723 bzero(&temp_cred
, sizeof(temp_cred
));
2724 ngroups
= nuidp
->nu_cr
->cr_ngroups
;
2725 for (i
= 0; i
< ngroups
; i
++)
2726 temp_cred
.cr_groups
[i
] = nuidp
->nu_cr
->cr_groups
[i
];
2728 nfsrvw_sort(&temp_cred
.cr_groups
[0], ngroups
);
2730 temp_cred
.cr_uid
= kauth_cred_getuid(nuidp
->nu_cr
);
2731 temp_cred
.cr_ngroups
= ngroups
;
2732 nd
->nd_cr
= kauth_cred_create(&temp_cred
);
2734 nd
->nd_repstat
= ENOMEM
;
2735 nd
->nd_procnum
= NFSPROC_NOOP
;
2738 nd
->nd_flag
|= ND_KERBNICK
;
2741 nd
->nd_repstat
= (NFSERR_AUTHERR
| AUTH_REJECTCRED
);
2742 nd
->nd_procnum
= NFSPROC_NOOP
;
2751 kauth_cred_rele(nd
->nd_cr
);
2756 * Search for a sleeping nfsd and wake it up.
2757 * SIDE EFFECT: If none found, set NFSD_CHECKSLP flag, so that one of the
2758 * running nfsds will go look for the work in the nfssvc_sock list.
2759 * Note: Must be called with nfsd_mutex held.
2762 nfsrv_wakenfsd(struct nfssvc_sock
*slp
)
2766 if ((slp
->ns_flag
& SLP_VALID
) == 0)
2769 lck_rw_lock_exclusive(&slp
->ns_rwlock
);
2772 TAILQ_FOREACH(nd
, &nfsd_head
, nfsd_chain
) {
2773 if (nd
->nfsd_flag
& NFSD_WAITING
) {
2774 nd
->nfsd_flag
&= ~NFSD_WAITING
;
2776 panic("nfsd wakeup");
2779 lck_rw_done(&slp
->ns_rwlock
);
2780 wakeup((caddr_t
)nd
);
2786 slp
->ns_flag
|= SLP_DOREC
;
2788 lck_rw_done(&slp
->ns_rwlock
);
2790 nfsd_head_flag
|= NFSD_CHECKSLP
;
2792 #endif /* NFS_NOSERVER */
2803 tpr
= tprintf_open(p
);
2807 tprintf(tpr
, "nfs server %s: %s, error %d\n", server
, msg
,
2810 tprintf(tpr
, "nfs server %s: %s\n", server
, msg
);
2816 nfs_down(nmp
, proc
, error
, flags
, msg
)
2817 struct nfsmount
*nmp
;
2824 if ((flags
& NFSSTA_TIMEO
) && !(nmp
->nm_state
& NFSSTA_TIMEO
)) {
2825 vfs_event_signal(&vfs_statfs(nmp
->nm_mountp
)->f_fsid
, VQ_NOTRESP
, 0);
2826 nmp
->nm_state
|= NFSSTA_TIMEO
;
2828 if ((flags
& NFSSTA_LOCKTIMEO
) && !(nmp
->nm_state
& NFSSTA_LOCKTIMEO
)) {
2829 vfs_event_signal(&vfs_statfs(nmp
->nm_mountp
)->f_fsid
, VQ_NOTRESPLOCK
, 0);
2830 nmp
->nm_state
|= NFSSTA_LOCKTIMEO
;
2832 nfs_msg(proc
, vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, msg
, error
);
2836 nfs_up(nmp
, proc
, flags
, msg
)
2837 struct nfsmount
*nmp
;
2845 nfs_msg(proc
, vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, msg
, 0);
2846 if ((flags
& NFSSTA_TIMEO
) && (nmp
->nm_state
& NFSSTA_TIMEO
)) {
2847 nmp
->nm_state
&= ~NFSSTA_TIMEO
;
2848 vfs_event_signal(&vfs_statfs(nmp
->nm_mountp
)->f_fsid
, VQ_NOTRESP
, 1);
2850 if ((flags
& NFSSTA_LOCKTIMEO
) && (nmp
->nm_state
& NFSSTA_LOCKTIMEO
)) {
2851 nmp
->nm_state
&= ~NFSSTA_LOCKTIMEO
;
2852 vfs_event_signal(&vfs_statfs(nmp
->nm_mountp
)->f_fsid
, VQ_NOTRESPLOCK
, 1);