2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
25 * Copyright (c) 1989, 1991, 1993, 1995
26 * The Regents of the University of California. All rights reserved.
28 * This code is derived from software contributed to Berkeley by
29 * Rick Macklem at The University of Guelph.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * @(#)nfs_socket.c 8.5 (Berkeley) 3/30/95
60 * FreeBSD-Id: nfs_socket.c,v 1.30 1997/10/28 15:59:07 bde Exp $
64 * Socket operations for use by nfs
67 #include <sys/param.h>
68 #include <sys/systm.h>
70 #include <sys/kauth.h>
71 #include <sys/mount_internal.h>
72 #include <sys/kernel.h>
73 #include <sys/kpi_mbuf.h>
74 #include <sys/malloc.h>
75 #include <sys/vnode.h>
76 #include <sys/domain.h>
77 #include <sys/protosw.h>
78 #include <sys/socket.h>
79 #include <sys/syslog.h>
80 #include <sys/tprintf.h>
81 #include <sys/uio_internal.h>
82 #include <libkern/OSAtomic.h>
85 #include <kern/clock.h>
86 #include <kern/task.h>
87 #include <kern/thread.h>
90 #include <netinet/in.h>
91 #include <netinet/tcp.h>
93 #include <nfs/rpcv2.h>
94 #include <nfs/nfsproto.h>
96 #include <nfs/xdr_subs.h>
97 #include <nfs/nfsm_subs.h>
98 #include <nfs/nfsmount.h>
99 #include <nfs/nfsnode.h>
100 #include <nfs/nfsrtt.h>
102 #include <sys/kdebug.h>
104 #define FSDBG(A, B, C, D, E) \
105 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_NONE, \
106 (int)(B), (int)(C), (int)(D), (int)(E), 0)
107 #define FSDBG_TOP(A, B, C, D, E) \
108 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_START, \
109 (int)(B), (int)(C), (int)(D), (int)(E), 0)
110 #define FSDBG_BOT(A, B, C, D, E) \
111 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_END, \
112 (int)(B), (int)(C), (int)(D), (int)(E), 0)
115 * Estimate rto for an nfs rpc sent via. an unreliable datagram.
116 * Use the mean and mean deviation of rtt for the appropriate type of rpc
117 * for the frequent rpcs and a default for the others.
118 * The justification for doing "other" this way is that these rpcs
119 * happen so infrequently that timer est. would probably be stale.
120 * Also, since many of these rpcs are
121 * non-idempotent, a conservative timeout is desired.
122 * getattr, lookup - A+2D
126 #define NFS_RTO(n, t) \
127 ((t) == 0 ? (n)->nm_timeo : \
129 (((((n)->nm_srtt[t-1] + 3) >> 2) + (n)->nm_sdrtt[t-1] + 1) >> 1) : \
130 ((((n)->nm_srtt[t-1] + 7) >> 3) + (n)->nm_sdrtt[t-1] + 1)))
131 #define NFS_SRTT(r) (r)->r_nmp->nm_srtt[proct[(r)->r_procnum] - 1]
132 #define NFS_SDRTT(r) (r)->r_nmp->nm_sdrtt[proct[(r)->r_procnum] - 1]
134 * External data, mostly RPC constants in XDR form
136 extern u_long rpc_reply
, rpc_msgdenied
, rpc_mismatch
, rpc_vers
, rpc_auth_unix
,
137 rpc_msgaccepted
, rpc_call
, rpc_autherr
,
139 extern u_long nfs_prog
;
140 extern struct nfsstats nfsstats
;
141 extern int nfsv3_procid
[NFS_NPROCS
];
142 extern int nfs_ticks
;
143 extern u_long nfs_xidwrap
;
146 * Defines which timer to use for the procnum.
153 static int proct
[NFS_NPROCS
] = {
154 0, 1, 0, 2, 1, 3, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0
158 * There is a congestion window for outstanding rpcs maintained per mount
159 * point. The cwnd size is adjusted in roughly the way that:
160 * Van Jacobson, Congestion avoidance and Control, In "Proceedings of
161 * SIGCOMM '88". ACM, August 1988.
162 * describes for TCP. The cwnd size is chopped in half on a retransmit timeout
163 * and incremented by 1/cwnd when each rpc reply is received and a full cwnd
164 * of rpcs is in progress.
165 * (The sent count and cwnd are scaled for integer arith.)
166 * Variants of "slow start" were tried and were found to be too much of a
167 * performance hit (ave. rtt 3 times larger),
168 * I suspect due to the large rtt that nfs rpcs have.
170 #define NFS_CWNDSCALE 256
171 #define NFS_MAXCWND (NFS_CWNDSCALE * 32)
172 static int nfs_backoff
[8] = { 2, 4, 8, 16, 32, 64, 128, 256, };
174 struct nfsrtt nfsrtt
;
176 static int nfs_rcvlock(struct nfsreq
*);
177 static void nfs_rcvunlock(struct nfsreq
*);
178 static int nfs_receive(struct nfsreq
*rep
, mbuf_t
*mp
);
179 static int nfs_reconnect(struct nfsreq
*rep
);
180 static void nfs_repdequeue(struct nfsreq
*rep
);
183 boolean_t
current_thread_aborted(void);
184 kern_return_t
thread_terminate(thread_t
);
187 static int nfsrv_getstream(struct nfssvc_sock
*,int);
189 int (*nfsrv3_procs
[NFS_NPROCS
])(struct nfsrv_descript
*nd
,
190 struct nfssvc_sock
*slp
,
217 #endif /* NFS_NOSERVER */
221 * attempt to bind a socket to a reserved port
224 nfs_bind_resv(struct nfsmount
*nmp
)
226 socket_t so
= nmp
->nm_so
;
227 struct sockaddr_in sin
;
234 sin
.sin_len
= sizeof (struct sockaddr_in
);
235 sin
.sin_family
= AF_INET
;
236 sin
.sin_addr
.s_addr
= INADDR_ANY
;
237 tport
= IPPORT_RESERVED
- 1;
238 sin
.sin_port
= htons(tport
);
240 while (((error
= sock_bind(so
, (struct sockaddr
*) &sin
)) == EADDRINUSE
) &&
241 (--tport
> IPPORT_RESERVED
/ 2))
242 sin
.sin_port
= htons(tport
);
247 * variables for managing the nfs_bind_resv_thread
249 int nfs_resv_mounts
= 0;
250 static int nfs_bind_resv_thread_state
= 0;
251 #define NFS_BIND_RESV_THREAD_STATE_INITTED 1
252 #define NFS_BIND_RESV_THREAD_STATE_RUNNING 2
253 lck_grp_t
*nfs_bind_resv_lck_grp
;
254 lck_grp_attr_t
*nfs_bind_resv_lck_grp_attr
;
255 lck_attr_t
*nfs_bind_resv_lck_attr
;
256 lck_mtx_t
*nfs_bind_resv_mutex
;
257 struct nfs_bind_resv_request
{
258 TAILQ_ENTRY(nfs_bind_resv_request
) brr_chain
;
259 struct nfsmount
*brr_nmp
;
262 static TAILQ_HEAD(, nfs_bind_resv_request
) nfs_bind_resv_request_queue
;
265 * thread to handle any reserved port bind requests
268 nfs_bind_resv_thread(void)
270 struct nfs_bind_resv_request
*brreq
;
272 nfs_bind_resv_thread_state
= NFS_BIND_RESV_THREAD_STATE_RUNNING
;
274 while (nfs_resv_mounts
> 0) {
275 lck_mtx_lock(nfs_bind_resv_mutex
);
276 while ((brreq
= TAILQ_FIRST(&nfs_bind_resv_request_queue
))) {
277 TAILQ_REMOVE(&nfs_bind_resv_request_queue
, brreq
, brr_chain
);
278 lck_mtx_unlock(nfs_bind_resv_mutex
);
279 brreq
->brr_error
= nfs_bind_resv(brreq
->brr_nmp
);
281 lck_mtx_lock(nfs_bind_resv_mutex
);
283 msleep((caddr_t
)&nfs_bind_resv_request_queue
,
284 nfs_bind_resv_mutex
, PSOCK
| PDROP
,
285 "nfs_bind_resv_request_queue", 0);
288 nfs_bind_resv_thread_state
= NFS_BIND_RESV_THREAD_STATE_INITTED
;
289 (void) thread_terminate(current_thread());
293 nfs_bind_resv_thread_wake(void)
295 if (nfs_bind_resv_thread_state
< NFS_BIND_RESV_THREAD_STATE_RUNNING
)
297 wakeup(&nfs_bind_resv_request_queue
);
302 * underprivileged procs call this to request nfs_bind_resv_thread
303 * to perform the reserved port binding for them.
306 nfs_bind_resv_nopriv(struct nfsmount
*nmp
)
308 struct nfs_bind_resv_request brreq
;
311 if (nfs_bind_resv_thread_state
< NFS_BIND_RESV_THREAD_STATE_RUNNING
) {
312 if (nfs_bind_resv_thread_state
< NFS_BIND_RESV_THREAD_STATE_INITTED
) {
313 nfs_bind_resv_lck_grp_attr
= lck_grp_attr_alloc_init();
314 lck_grp_attr_setstat(nfs_bind_resv_lck_grp_attr
);
315 nfs_bind_resv_lck_grp
= lck_grp_alloc_init("nfs_bind_resv", nfs_bind_resv_lck_grp_attr
);
316 nfs_bind_resv_lck_attr
= lck_attr_alloc_init();
317 nfs_bind_resv_mutex
= lck_mtx_alloc_init(nfs_bind_resv_lck_grp
, nfs_bind_resv_lck_attr
);
318 TAILQ_INIT(&nfs_bind_resv_request_queue
);
319 nfs_bind_resv_thread_state
= NFS_BIND_RESV_THREAD_STATE_INITTED
;
321 kernel_thread(kernel_task
, nfs_bind_resv_thread
);
322 nfs_bind_resv_thread_state
= NFS_BIND_RESV_THREAD_STATE_RUNNING
;
328 lck_mtx_lock(nfs_bind_resv_mutex
);
329 TAILQ_INSERT_TAIL(&nfs_bind_resv_request_queue
, &brreq
, brr_chain
);
330 lck_mtx_unlock(nfs_bind_resv_mutex
);
332 error
= nfs_bind_resv_thread_wake();
334 TAILQ_REMOVE(&nfs_bind_resv_request_queue
, &brreq
, brr_chain
);
335 /* Note: we might be able to simply restart the thread */
339 tsleep((caddr_t
)&brreq
, PSOCK
, "nfsbindresv", 0);
341 return (brreq
.brr_error
);
345 * Initialize sockets and congestion for a new NFS connection.
346 * We do not free the sockaddr if error.
350 struct nfsmount
*nmp
,
351 __unused
struct nfsreq
*rep
)
354 int error
, rcvreserve
, sndreserve
;
355 struct sockaddr
*saddr
;
356 struct timeval timeo
;
359 saddr
= mbuf_data(nmp
->nm_nam
);
360 error
= sock_socket(saddr
->sa_family
, nmp
->nm_sotype
,
361 nmp
->nm_soproto
, 0, 0, &nmp
->nm_so
);
368 * Some servers require that the client port be a reserved port number.
370 if (saddr
->sa_family
== AF_INET
&& (nmp
->nm_flag
& NFSMNT_RESVPORT
)) {
373 * sobind() requires current_proc() to have superuser privs.
374 * If this bind is part of a reconnect, and the current proc
375 * doesn't have superuser privs, we hand the sobind() off to
376 * a kernel thread to process.
378 if ((nmp
->nm_state
& NFSSTA_MOUNTED
) &&
379 (p
= current_proc()) && suser(kauth_cred_get(), 0)) {
380 /* request nfs_bind_resv_thread() to do bind */
381 error
= nfs_bind_resv_nopriv(nmp
);
383 error
= nfs_bind_resv(nmp
);
390 * Protocols that do not require connections may be optionally left
391 * unconnected for servers that reply from a port other than NFS_PORT.
393 if (nmp
->nm_flag
& NFSMNT_NOCONN
) {
394 if (nmp
->nm_sotype
== SOCK_STREAM
) {
402 error
= sock_connect(so
, mbuf_data(nmp
->nm_nam
), MSG_DONTWAIT
);
403 if (error
&& error
!= EINPROGRESS
) {
407 while ((error
= sock_connectwait(so
, &tv
)) == EINPROGRESS
) {
408 if (rep
&& (error
= nfs_sigintr(nmp
, rep
, rep
->r_procp
))) {
415 * Always time out on recieve, this allows us to reconnect the
416 * socket to deal with network changes.
420 error
= sock_setsockopt(so
, SOL_SOCKET
, SO_RCVTIMEO
, &timeo
, sizeof(timeo
));
421 if (nmp
->nm_flag
& (NFSMNT_SOFT
| NFSMNT_INT
)) {
426 error
= sock_setsockopt(so
, SOL_SOCKET
, SO_SNDTIMEO
, &timeo
, sizeof(timeo
));
428 if (nmp
->nm_sotype
== SOCK_DGRAM
) {
429 sndreserve
= (nmp
->nm_wsize
+ NFS_MAXPKTHDR
) * 3;
430 rcvreserve
= (nmp
->nm_rsize
+ NFS_MAXPKTHDR
) *
431 (nmp
->nm_readahead
> 0 ? nmp
->nm_readahead
+ 1 : 2);
432 } else if (nmp
->nm_sotype
== SOCK_SEQPACKET
) {
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);
440 sock_gettype(so
, NULL
, NULL
, &proto
);
441 if (nmp
->nm_sotype
!= SOCK_STREAM
)
442 panic("nfscon sotype");
444 // Assume that SOCK_STREAM always requires a connection
445 sock_setsockopt(so
, SOL_SOCKET
, SO_KEEPALIVE
, &on
, sizeof(on
));
447 if (proto
== IPPROTO_TCP
) {
448 sock_setsockopt(so
, IPPROTO_TCP
, TCP_NODELAY
, &on
, sizeof(on
));
451 sndreserve
= (nmp
->nm_wsize
+ NFS_MAXPKTHDR
+ sizeof (u_long
)) * 3;
452 rcvreserve
= (nmp
->nm_rsize
+ NFS_MAXPKTHDR
+ sizeof (u_long
)) *
453 (nmp
->nm_readahead
> 0 ? nmp
->nm_readahead
+ 1 : 2);
456 if (sndreserve
> NFS_MAXSOCKBUF
)
457 sndreserve
= NFS_MAXSOCKBUF
;
458 if (rcvreserve
> NFS_MAXSOCKBUF
)
459 rcvreserve
= NFS_MAXSOCKBUF
;
460 error
= sock_setsockopt(so
, SOL_SOCKET
, SO_SNDBUF
, &sndreserve
, sizeof(sndreserve
));
464 error
= sock_setsockopt(so
, SOL_SOCKET
, SO_RCVBUF
, &rcvreserve
, sizeof(rcvreserve
));
469 sock_nointerrupt(so
, 1);
471 /* Initialize other non-zero congestion variables */
472 nmp
->nm_srtt
[0] = nmp
->nm_srtt
[1] = nmp
->nm_srtt
[2] =
473 nmp
->nm_srtt
[3] = (NFS_TIMEO
<< 3);
474 nmp
->nm_sdrtt
[0] = nmp
->nm_sdrtt
[1] = nmp
->nm_sdrtt
[2] =
475 nmp
->nm_sdrtt
[3] = 0;
476 nmp
->nm_cwnd
= NFS_MAXCWND
/ 2; /* Initial send window */
478 FSDBG(529, nmp
, nmp
->nm_state
, nmp
->nm_soflags
, nmp
->nm_cwnd
);
479 nmp
->nm_timeouts
= 0;
489 * Called when a connection is broken on a reliable protocol.
490 * - clean up the old socket
491 * - nfs_connect() again
492 * - set R_MUSTRESEND for all outstanding requests on mount point
493 * If this fails the mount point is DEAD!
494 * nb: Must be called with the nfs_sndlock() set on the mount point.
497 nfs_reconnect(struct nfsreq
*rep
)
500 struct nfsmount
*nmp
= rep
->r_nmp
;
504 while ((error
= nfs_connect(nmp
, rep
))) {
505 if (error
== EINTR
|| error
== ERESTART
)
509 nfs_down(rep
->r_nmp
, rep
->r_procp
, error
, NFSSTA_TIMEO
,
511 rep
->r_flags
|= R_TPRINTFMSG
;
512 if (!(nmp
->nm_state
& NFSSTA_MOUNTED
)) {
513 /* we're not yet completely mounted and */
514 /* we can't reconnect, so we fail */
517 if ((error
= nfs_sigintr(rep
->r_nmp
, rep
, rep
->r_procp
)))
519 tsleep((caddr_t
)&lbolt
, PSOCK
, "nfscon", 0);
523 * Loop through outstanding request list and fix up all requests
526 TAILQ_FOREACH(rp
, &nfs_reqq
, r_chain
) {
527 if (rp
->r_nmp
== nmp
)
528 rp
->r_flags
|= R_MUSTRESEND
;
534 * NFS disconnect. Clean up and unlink.
537 nfs_disconnect(struct nfsmount
*nmp
)
544 sock_shutdown(so
, 2);
550 * This is the nfs send routine. For connection based socket types, it
551 * must be called with an nfs_sndlock() on the socket.
552 * "rep == NULL" indicates that it has been called from a server.
553 * For the client side:
554 * - return EINTR if the RPC is terminated, 0 otherwise
555 * - set R_MUSTRESEND if the send fails for any reason
556 * - do any cleanup required by recoverable socket errors (???)
557 * For the server side:
558 * - return EINTR or ERESTART if interrupted by a signal
559 * - return EPIPE if a connection is lost for connection based sockets (TCP...)
560 * - do any cleanup required by recoverable socket errors (???)
563 nfs_send(so
, nam
, top
, rep
)
569 struct sockaddr
*sendnam
;
570 int error
, error2
, sotype
, flags
;
571 u_long xidqueued
= 0;
573 char savenametolog
[MAXPATHLEN
];
577 error
= nfs_sigintr(rep
->r_nmp
, rep
, rep
->r_procp
);
582 if ((so
= rep
->r_nmp
->nm_so
) == NULL
) {
583 rep
->r_flags
|= R_MUSTRESEND
;
587 rep
->r_flags
&= ~R_MUSTRESEND
;
588 TAILQ_FOREACH(rp
, &nfs_reqq
, r_chain
)
592 xidqueued
= rp
->r_xid
;
594 sock_gettype(so
, NULL
, &sotype
, NULL
);
595 if ((sotype
== SOCK_STREAM
) || (sock_isconnected(so
)) ||
597 sendnam
= (struct sockaddr
*)0;
599 sendnam
= mbuf_data(nam
);
601 if (sotype
== SOCK_SEQPACKET
)
607 * Save the name here in case mount point goes away if we block.
608 * The name is using local stack and is large, but don't
609 * want to block if we malloc.
612 strncpy(savenametolog
,
613 vfs_statfs(rep
->r_nmp
->nm_mountp
)->f_mntfromname
,
615 bzero(&msg
, sizeof(msg
));
616 msg
.msg_name
= (caddr_t
)sendnam
;
617 msg
.msg_namelen
= sendnam
== 0 ? 0 : sendnam
->sa_len
;
618 error
= sock_sendmbuf(so
, &msg
, top
, flags
, NULL
);
623 TAILQ_FOREACH(rp
, &nfs_reqq
, r_chain
)
624 if (rp
== rep
&& rp
->r_xid
== xidqueued
)
627 panic("nfs_send: error %d xid %x gone",
630 log(LOG_INFO
, "nfs send error %d for server %s\n",
631 error
, savenametolog
);
633 * Deal with errors for the client side.
635 error2
= nfs_sigintr(rep
->r_nmp
, rep
, rep
->r_procp
);
639 rep
->r_flags
|= R_MUSTRESEND
;
642 log(LOG_INFO
, "nfsd send error %d\n", error
);
645 * Handle any recoverable (soft) socket errors here. (???)
647 if (error
!= EINTR
&& error
!= ERESTART
&& error
!= EIO
&&
648 error
!= EWOULDBLOCK
&& error
!= EPIPE
) {
656 * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all
657 * done by soreceive(), but for SOCK_STREAM we must deal with the Record
658 * Mark and consolidate the data into a new mbuf list.
659 * nb: Sometimes TCP passes the data up to soreceive() in long lists of
661 * For SOCK_STREAM we must be very careful to read an entire record once
662 * we have read any of it, even if the system call has been interrupted.
665 nfs_receive(struct nfsreq
*rep
, mbuf_t
*mp
)
671 int error
, error2
, sotype
;
672 proc_t p
= current_proc(); /* XXX */
678 * Set up arguments for soreceive()
681 sotype
= rep
->r_nmp
->nm_sotype
;
684 * For reliable protocols, lock against other senders/receivers
685 * in case a reconnect is necessary.
686 * For SOCK_STREAM, first get the Record Mark to find out how much
687 * more there is to get.
688 * We must lock the socket against other receivers
689 * until we have an entire rpc request/reply.
691 if (sotype
!= SOCK_DGRAM
) {
692 error
= nfs_sndlock(rep
);
697 * Check for fatal errors and resending request.
700 * Ugh: If a reconnect attempt just happened, nm_so
701 * would have changed. NULL indicates a failed
702 * attempt that has essentially shut down this
705 if ((error
= nfs_sigintr(rep
->r_nmp
, rep
, p
)) || rep
->r_mrep
) {
711 so
= rep
->r_nmp
->nm_so
;
713 error
= nfs_reconnect(rep
);
720 while (rep
->r_flags
& R_MUSTRESEND
) {
721 error
= mbuf_copym(rep
->r_mreq
, 0, MBUF_COPYALL
, MBUF_WAITOK
, &m
);
723 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpcretries
);
724 error
= nfs_send(so
, rep
->r_nmp
->nm_nam
, m
, rep
);
727 * we also hold rcv lock so rep is still
731 if (error
== EINTR
|| error
== ERESTART
||
732 (error
= nfs_reconnect(rep
))) {
740 if (sotype
== SOCK_STREAM
) {
745 while (!error
&& !lastfragment
) {
746 aio
.iov_base
= (uintptr_t) &fraglen
;
747 aio
.iov_len
= sizeof(u_long
);
748 bzero(&msg
, sizeof(msg
));
749 msg
.msg_iov
= (struct iovec
*) &aio
;
752 error
= sock_receive(so
, &msg
, MSG_WAITALL
, &rcvlen
);
753 if (!rep
->r_nmp
) /* if unmounted then bailout */
755 if (error
== EWOULDBLOCK
&& rep
) {
756 error2
= nfs_sigintr(rep
->r_nmp
, rep
, p
);
760 } while (error
== EWOULDBLOCK
);
761 if (!error
&& rcvlen
< aio
.iov_len
) {
762 /* only log a message if we got a partial word */
765 "short receive (%d/%d) from nfs server %s\n",
766 rcvlen
, sizeof(u_long
),
767 vfs_statfs(rep
->r_nmp
->nm_mountp
)->f_mntfromname
);
772 lastfragment
= ntohl(fraglen
) & 0x80000000;
773 fraglen
= ntohl(fraglen
) & ~0x80000000;
776 * This is SERIOUS! We are out of sync with the sender
777 * and forcing a disconnect/reconnect is all I can do.
779 if (len
> NFS_MAXPACKET
) {
780 log(LOG_ERR
, "%s (%d) from nfs server %s\n",
781 "impossible RPC record length", len
,
782 vfs_statfs(rep
->r_nmp
->nm_mountp
)->f_mntfromname
);
790 error
= sock_receivembuf(so
, NULL
, &m
, MSG_WAITALL
, &rcvlen
);
791 if (!rep
->r_nmp
) /* if unmounted then bailout */ {
794 } while (error
== EWOULDBLOCK
|| error
== EINTR
||
797 if (!error
&& fraglen
> rcvlen
) {
799 "short receive (%d/%d) from nfs server %s\n",
801 vfs_statfs(rep
->r_nmp
->nm_mountp
)->f_mntfromname
);
810 error
= mbuf_setnext(mlast
, m
);
812 printf("nfs_receive: mbuf_setnext failed %d\n", error
);
816 while (mbuf_next(mlast
))
817 mlast
= mbuf_next(mlast
);
821 bzero(&msg
, sizeof(msg
));
824 error
= sock_receivembuf(so
, &msg
, mp
, 0, &rcvlen
);
825 if (!rep
->r_nmp
) /* if unmounted then bailout */ {
828 if (error
== EWOULDBLOCK
&& rep
) {
829 error2
= nfs_sigintr(rep
->r_nmp
, rep
, p
);
834 } while (error
== EWOULDBLOCK
);
836 if ((msg
.msg_flags
& MSG_EOR
) == 0)
838 if (!error
&& *mp
== NULL
)
843 if (error
&& error
!= EINTR
&& error
!= ERESTART
) {
848 "receive error %d from nfs server %s\n", error
,
849 vfs_statfs(rep
->r_nmp
->nm_mountp
)->f_mntfromname
);
850 error
= nfs_sndlock(rep
);
852 error
= nfs_reconnect(rep
);
860 * We could have failed while rebinding the datagram socket
861 * so we need to attempt to rebind here.
863 if ((so
= rep
->r_nmp
->nm_so
) == NULL
) {
864 error
= nfs_sndlock(rep
);
866 error
= nfs_reconnect(rep
);
871 if (!rep
->r_nmp
) /* if unmounted then bailout */
873 so
= rep
->r_nmp
->nm_so
;
875 bzero(&msg
, sizeof(msg
));
879 error
= sock_receivembuf(so
, &msg
, mp
, 0, &rcvlen
);
880 if (!rep
->r_nmp
) /* if unmounted then bailout */
883 error2
= nfs_sigintr(rep
->r_nmp
, rep
, p
);
889 /* Reconnect for all errors. We may be receiving
890 * soft/hard/blocking errors because of a network
892 * XXX: we should rate limit or delay this
893 * to once every N attempts or something.
894 * although TCP doesn't seem to.
897 error2
= nfs_sndlock(rep
);
899 error2
= nfs_reconnect(rep
);
902 else if (!rep
->r_nmp
) /* if unmounted then bailout */
905 so
= rep
->r_nmp
->nm_so
;
911 } while (error
== EWOULDBLOCK
);
922 * Implement receipt of reply on a socket.
923 * We must search through the list of received datagrams matching them
924 * with outstanding requests using the xid, until ours is found.
929 struct nfsreq
*myrep
;
932 struct nfsmount
*nmp
= myrep
->r_nmp
;
940 * Loop around until we get our own reply
944 * Lock against other receivers so that I don't get stuck in
945 * sbwait() after someone else has received my reply for me.
946 * Also necessary for connection based protocols to avoid
947 * race conditions during a reconnect.
948 * If nfs_rcvlock() returns EALREADY, that means that
949 * the reply has already been recieved by another
950 * process and we can return immediately. In this
951 * case, the lock is not taken to avoid races with
954 error
= nfs_rcvlock(myrep
);
955 if (error
== EALREADY
)
961 * If we slept after putting bits otw, then reply may have
962 * arrived. In which case returning is required, or we
963 * would hang trying to nfs_receive an already received reply.
965 if (myrep
->r_mrep
!= NULL
) {
966 nfs_rcvunlock(myrep
);
967 FSDBG(530, myrep
->r_xid
, myrep
, myrep
->r_nmp
, -1);
971 * Get the next Rpc reply off the socket. Assume myrep->r_nmp
972 * is still intact by checks done in nfs_rcvlock.
974 error
= nfs_receive(myrep
, &mrep
);
976 * Bailout asap if nfsmount struct gone (unmounted).
979 FSDBG(530, myrep
->r_xid
, myrep
, nmp
, -2);
985 FSDBG(530, myrep
->r_xid
, myrep
, nmp
, error
);
986 nfs_rcvunlock(myrep
);
988 /* Bailout asap if nfsmount struct gone (unmounted). */
996 * Ignore routing errors on connectionless protocols??
998 if (NFSIGNORE_SOERROR(nmp
->nm_sotype
, error
)) {
1001 int optlen
= sizeof(clearerror
);
1002 sock_getsockopt(nmp
->nm_so
, SOL_SOCKET
, SO_ERROR
, &clearerror
, &optlen
);
1012 * We assume all is fine, but if we did not have an error
1013 * and mrep is 0, better not dereference it. nfs_receive
1014 * calls soreceive which carefully sets error=0 when it got
1015 * errors on sbwait (tsleep). In most cases, I assume that's
1016 * so we could go back again. In tcp case, EPIPE is returned.
1017 * In udp, case nfs_receive gets back here with no error and no
1018 * mrep. Is the right fix to have soreceive check for process
1019 * aborted after sbwait and return something non-zero? Should
1020 * nfs_receive give an EPIPE? Too risky to play with those
1021 * two this late in game for a shutdown problem. Instead,
1022 * just check here and get out. (ekn)
1025 nfs_rcvunlock(myrep
);
1026 FSDBG(530, myrep
->r_xid
, myrep
, nmp
, -3);
1027 return (ENXIO
); /* sounds good */
1031 * Get the xid and check that it is an rpc reply
1034 dpos
= mbuf_data(md
);
1035 nfsm_dissect(tl
, u_long
*, 2*NFSX_UNSIGNED
);
1037 if (*tl
!= rpc_reply
) {
1038 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpcinvalid
);
1041 if (nmp
->nm_state
& NFSSTA_RCVLOCK
)
1042 nfs_rcvunlock(myrep
);
1047 * Loop through the request list to match up the reply
1048 * Iff no match, just drop the datagram
1050 TAILQ_FOREACH(rep
, &nfs_reqq
, r_chain
) {
1051 if (rep
->r_mrep
== NULL
&& rxid
== rep
->r_xid
) {
1057 * If we're tracking the round trip time
1058 * then we update the circular log here
1059 * with the stats from our current request.
1064 rt
= &nfsrtt
.rttl
[nfsrtt
.pos
];
1065 rt
->proc
= rep
->r_procnum
;
1066 rt
->rto
= NFS_RTO(nmp
, proct
[rep
->r_procnum
]);
1067 rt
->sent
= nmp
->nm_sent
;
1068 rt
->cwnd
= nmp
->nm_cwnd
;
1069 if (proct
[rep
->r_procnum
] == 0)
1070 panic("nfs_reply: proct[%d] is zero", rep
->r_procnum
);
1071 rt
->srtt
= nmp
->nm_srtt
[proct
[rep
->r_procnum
] - 1];
1072 rt
->sdrtt
= nmp
->nm_sdrtt
[proct
[rep
->r_procnum
] - 1];
1073 rt
->fsid
= vfs_statfs(nmp
->nm_mountp
)->f_fsid
;
1074 microtime(&rt
->tstamp
); // XXX unused
1075 if (rep
->r_flags
& R_TIMING
)
1076 rt
->rtt
= rep
->r_rtt
;
1079 nfsrtt
.pos
= (nfsrtt
.pos
+ 1) % NFSRTTLOGSIZ
;
1082 * Update congestion window.
1083 * Do the additive increase of
1086 FSDBG(530, rep
->r_xid
, rep
, nmp
->nm_sent
,
1088 if (nmp
->nm_cwnd
<= nmp
->nm_sent
) {
1090 (NFS_CWNDSCALE
* NFS_CWNDSCALE
+
1091 (nmp
->nm_cwnd
>> 1)) / nmp
->nm_cwnd
;
1092 if (nmp
->nm_cwnd
> NFS_MAXCWND
)
1093 nmp
->nm_cwnd
= NFS_MAXCWND
;
1095 if (rep
->r_flags
& R_SENT
) {
1096 rep
->r_flags
&= ~R_SENT
;
1097 nmp
->nm_sent
-= NFS_CWNDSCALE
;
1100 * Update rtt using a gain of 0.125 on the mean
1101 * and a gain of 0.25 on the deviation.
1103 if (rep
->r_flags
& R_TIMING
) {
1105 * Since the timer resolution of
1106 * NFS_HZ is so course, it can often
1107 * result in r_rtt == 0. Since
1108 * r_rtt == N means that the actual
1109 * rtt is between N+dt and N+2-dt ticks,
1112 if (proct
[rep
->r_procnum
] == 0)
1113 panic("nfs_reply: proct[%d] is zero", rep
->r_procnum
);
1114 t1
= rep
->r_rtt
+ 1;
1115 t1
-= (NFS_SRTT(rep
) >> 3);
1116 NFS_SRTT(rep
) += t1
;
1119 t1
-= (NFS_SDRTT(rep
) >> 2);
1120 NFS_SDRTT(rep
) += t1
;
1122 nmp
->nm_timeouts
= 0;
1126 nfs_rcvunlock(myrep
);
1128 * If not matched to a request, drop it.
1129 * If it's mine, get out.
1132 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpcunexpected
);
1134 } else if (rep
== myrep
) {
1135 if (rep
->r_mrep
== NULL
)
1136 panic("nfs_reply: nil r_mrep");
1139 FSDBG(530, myrep
->r_xid
, myrep
, rep
,
1140 rep
? rep
->r_xid
: myrep
->r_flags
);
1145 * nfs_request - goes something like this
1146 * - fill in request struct
1147 * - links it into list
1148 * - calls nfs_send() for first transmit
1149 * - calls nfs_receive() to get reply
1150 * - break down rpc header and return with nfs reply pointed to
1152 * nb: always frees up mreq mbuf list
1155 nfs_request(vp
, mp
, mrest
, procnum
, procp
, cred
, mrp
, mdp
, dposp
, xidp
)
1168 struct nfsreq re
, *rep
;
1171 struct nfsmount
*nmp
;
1172 mbuf_t md
, mheadend
;
1173 char nickv
[RPCX_NICKVERF
];
1176 int t1
, error
= 0, mrest_len
, auth_len
, auth_type
;
1177 int trylater_delay
= NFS_TRYLATERDEL
, failed_auth
= 0;
1178 int verf_len
, verf_type
;
1180 char *auth_str
, *verf_str
;
1181 NFSKERBKEY_T key
; /* save session key */
1194 nmp
= VFSTONFS(vnode_mount(vp
));
1196 (nmp
->nm_state
& (NFSSTA_FORCE
|NFSSTA_TIMEO
)) ==
1197 (NFSSTA_FORCE
|NFSSTA_TIMEO
)) {
1201 nmsotype
= nmp
->nm_sotype
;
1203 FSDBG_TOP(531, vp
, procnum
, nmp
, rep
);
1207 rep
->r_procp
= procp
;
1208 rep
->r_procnum
= procnum
;
1210 rep
->r_lastmsg
= now
.tv_sec
-
1211 ((nmp
->nm_tprintf_delay
) - (nmp
->nm_tprintf_initial_delay
));
1221 * Get the RPC header with authorization.
1224 nmp
= vp
? VFSTONFS(vnode_mount(vp
)) : rep
->r_nmp
;
1226 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1230 verf_str
= auth_str
= (char *)0;
1231 if (nmp
->nm_flag
& NFSMNT_KERB
) {
1233 verf_len
= sizeof (nickv
);
1234 auth_type
= RPCAUTH_KERB4
;
1235 bzero((caddr_t
)key
, sizeof (key
));
1236 if (failed_auth
|| nfs_getnickauth(nmp
, cred
, &auth_str
,
1237 &auth_len
, verf_str
, verf_len
)) {
1238 nmp
= vp
? VFSTONFS(vnode_mount(vp
)) : rep
->r_nmp
;
1240 FSDBG_BOT(531, 2, vp
, error
, rep
);
1244 error
= nfs_getauth(nmp
, rep
, cred
, &auth_str
,
1245 &auth_len
, verf_str
, &verf_len
, key
);
1246 nmp
= vp
? VFSTONFS(vnode_mount(vp
)) : rep
->r_nmp
;
1250 FSDBG_BOT(531, 2, vp
, error
, rep
);
1256 auth_type
= RPCAUTH_UNIX
;
1257 if (cred
->cr_ngroups
< 1)
1258 panic("nfsreq nogrps");
1259 auth_len
= ((((cred
->cr_ngroups
- 1) > nmp
->nm_numgrps
) ?
1260 nmp
->nm_numgrps
: (cred
->cr_ngroups
- 1)) << 2) +
1263 error
= nfsm_rpchead(cred
, nmp
->nm_flag
, procnum
, auth_type
, auth_len
,
1264 auth_str
, verf_len
, verf_str
, mrest
, mrest_len
, &mheadend
, &xid
, &m
);
1266 _FREE(auth_str
, M_TEMP
);
1269 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1273 *xidp
= ntohl(xid
) + ((u_int64_t
)nfs_xidwrap
<< 32);
1276 * For stream protocols, insert a Sun RPC Record Mark.
1278 if (nmsotype
== SOCK_STREAM
) {
1279 error
= mbuf_prepend(&m
, NFSX_UNSIGNED
, MBUF_WAITOK
);
1282 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1285 *((u_long
*)mbuf_data(m
)) =
1286 htonl(0x80000000 | (mbuf_pkthdr_len(m
) - NFSX_UNSIGNED
));
1291 nmp
= vp
? VFSTONFS(vnode_mount(vp
)) : rep
->r_nmp
;
1292 if (nmp
&& (nmp
->nm_flag
& NFSMNT_SOFT
))
1293 rep
->r_retry
= nmp
->nm_retry
;
1295 rep
->r_retry
= NFS_MAXREXMIT
+ 1; /* past clip limit */
1296 rep
->r_rtt
= rep
->r_rexmit
= 0;
1297 if (proct
[procnum
] > 0)
1298 rep
->r_flags
= R_TIMING
;
1304 * Do the client side RPC.
1306 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpcrequests
);
1308 * Chain request into list of outstanding requests. Be sure
1309 * to put it LAST so timer finds oldest requests first.
1311 TAILQ_INSERT_TAIL(&nfs_reqq
, rep
, r_chain
);
1314 * If backing off another request or avoiding congestion, don't
1315 * send this one now but let timer do it. If not timing a request,
1318 if (nmp
&& nmp
->nm_so
&& (nmp
->nm_sotype
!= SOCK_DGRAM
||
1319 (nmp
->nm_flag
& NFSMNT_DUMBTIMR
) ||
1320 nmp
->nm_sent
< nmp
->nm_cwnd
)) {
1321 int connrequired
= (nmp
->nm_sotype
== SOCK_STREAM
);
1324 error
= nfs_sndlock(rep
);
1327 * Set the R_SENT before doing the send in case another thread
1328 * processes the reply before the nfs_send returns here
1331 if ((rep
->r_flags
& R_MUSTRESEND
) == 0) {
1332 FSDBG(531, rep
->r_xid
, rep
, nmp
->nm_sent
,
1334 nmp
->nm_sent
+= NFS_CWNDSCALE
;
1335 rep
->r_flags
|= R_SENT
;
1338 error
= mbuf_copym(m
, 0, MBUF_COPYALL
, MBUF_WAITOK
, &m2
);
1340 error
= nfs_send(nmp
->nm_so
, nmp
->nm_nam
, m2
, rep
);
1344 nmp
= vp
? VFSTONFS(vnode_mount(vp
)) : rep
->r_nmp
;
1347 nmp
->nm_sent
-= NFS_CWNDSCALE
;
1348 rep
->r_flags
&= ~R_SENT
;
1355 * Wait for the reply from our send or the timer's.
1357 if (!error
|| error
== EPIPE
)
1358 error
= nfs_reply(rep
);
1361 * RPC done, unlink the request.
1363 nfs_repdequeue(rep
);
1365 nmp
= vp
? VFSTONFS(vnode_mount(vp
)) : rep
->r_nmp
;
1368 * Decrement the outstanding request count.
1370 if (rep
->r_flags
& R_SENT
) {
1371 rep
->r_flags
&= ~R_SENT
; /* paranoia */
1373 FSDBG(531, rep
->r_xid
, rep
, nmp
->nm_sent
, nmp
->nm_cwnd
);
1374 nmp
->nm_sent
-= NFS_CWNDSCALE
;
1379 * If there was a successful reply and a tprintf msg.
1380 * tprintf a response.
1383 nfs_up(nmp
, procp
, NFSSTA_TIMEO
,
1384 (rep
->r_flags
& R_TPRINTFMSG
) ? "is alive again" : NULL
);
1391 mbuf_freem(rep
->r_mreq
);
1392 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1397 * break down the rpc header and check if ok
1399 nfsm_dissect(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
1400 if (*tl
++ == rpc_msgdenied
) {
1401 if (*tl
== rpc_mismatch
)
1403 else if ((nmp
->nm_flag
& NFSMNT_KERB
) && *tl
++ == rpc_autherr
) {
1406 error
= mbuf_setnext(mheadend
, NULL
);
1408 mbuf_freem(rep
->r_mreq
);
1411 printf("nfs_request: mbuf_setnext failed\n");
1417 mbuf_freem(rep
->r_mreq
);
1418 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1423 * Grab any Kerberos verifier, otherwise just throw it away.
1425 verf_type
= fxdr_unsigned(int, *tl
++);
1426 i
= fxdr_unsigned(int, *tl
);
1427 if ((nmp
->nm_flag
& NFSMNT_KERB
) && verf_type
== RPCAUTH_KERB4
) {
1428 error
= nfs_savenickauth(nmp
, cred
, i
, key
, &md
, &dpos
, mrep
);
1432 nfsm_adv(nfsm_rndup(i
));
1433 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
1436 nfsm_dissect(tl
, u_long
*, NFSX_UNSIGNED
);
1438 error
= fxdr_unsigned(int, *tl
);
1439 if ((nmp
->nm_flag
& NFSMNT_NFSV3
) &&
1440 error
== NFSERR_TRYLATER
) {
1444 waituntil
= now
.tv_sec
+ trylater_delay
;
1445 while (now
.tv_sec
< waituntil
) {
1446 tsleep((caddr_t
)&lbolt
, PSOCK
, "nfstrylater", 0);
1449 trylater_delay
*= 2;
1450 if (trylater_delay
> 60)
1451 trylater_delay
= 60;
1456 * If the File Handle was stale, invalidate the
1457 * lookup cache, just in case.
1459 if ((error
== ESTALE
) && vp
)
1461 if (nmp
->nm_flag
& NFSMNT_NFSV3
) {
1465 error
|= NFSERR_RETERR
;
1468 error
&= ~NFSERR_RETERR
;
1470 mbuf_freem(rep
->r_mreq
);
1471 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1478 mbuf_freem(rep
->r_mreq
);
1479 FSDBG_BOT(531, 0xf0f0f0f0, rep
->r_xid
, nmp
, rep
);
1483 error
= EPROTONOSUPPORT
;
1485 mbuf_freem(rep
->r_mreq
);
1486 FSDBG_BOT(531, error
, rep
->r_xid
, nmp
, rep
);
1490 #ifndef NFS_NOSERVER
1492 * Generate the rpc reply header
1493 * siz arg. is used to decide if adding a cluster is worthwhile
1496 nfs_rephead(siz
, nd
, slp
, err
, mrq
, mbp
, bposp
)
1498 struct nfsrv_descript
*nd
;
1499 struct nfssvc_sock
*slp
;
1512 * If this is a big reply, use a cluster else
1513 * try and leave leading space for the lower level headers.
1515 siz
+= RPC_REPLYSIZ
;
1516 if (siz
>= nfs_mbuf_minclsize
) {
1517 error
= mbuf_getpacket(MBUF_WAITOK
, &mreq
);
1519 error
= mbuf_gethdr(MBUF_WAITOK
, MBUF_TYPE_DATA
, &mreq
);
1522 /* unable to allocate packet */
1527 tl
= mbuf_data(mreq
);
1528 mlen
= 6 * NFSX_UNSIGNED
;
1529 if (siz
< nfs_mbuf_minclsize
) {
1530 /* leave space for lower level headers */
1531 tl
+= 80/sizeof(*tl
); /* XXX max_hdr? XXX */
1532 mbuf_setdata(mreq
, tl
, mlen
);
1534 mbuf_setlen(mreq
, mlen
);
1536 bpos
= ((caddr_t
)tl
) + mlen
;
1537 *tl
++ = txdr_unsigned(nd
->nd_retxid
);
1539 if (err
== ERPCMISMATCH
|| (err
& NFSERR_AUTHERR
)) {
1540 *tl
++ = rpc_msgdenied
;
1541 if (err
& NFSERR_AUTHERR
) {
1542 *tl
++ = rpc_autherr
;
1543 *tl
= txdr_unsigned(err
& ~NFSERR_AUTHERR
);
1544 mlen
-= NFSX_UNSIGNED
;
1545 mbuf_setlen(mreq
, mlen
);
1546 bpos
-= NFSX_UNSIGNED
;
1548 *tl
++ = rpc_mismatch
;
1549 *tl
++ = txdr_unsigned(RPC_VER2
);
1550 *tl
= txdr_unsigned(RPC_VER2
);
1553 *tl
++ = rpc_msgaccepted
;
1556 * For Kerberos authentication, we must send the nickname
1557 * verifier back, otherwise just RPCAUTH_NULL.
1559 if (nd
->nd_flag
& ND_KERBFULL
) {
1560 struct nfsuid
*nuidp
;
1561 struct timeval ktvin
, ktvout
;
1562 uid_t uid
= kauth_cred_getuid(nd
->nd_cr
);
1564 lck_rw_lock_shared(&slp
->ns_rwlock
);
1565 for (nuidp
= NUIDHASH(slp
, uid
)->lh_first
;
1566 nuidp
!= 0; nuidp
= nuidp
->nu_hash
.le_next
) {
1567 if (kauth_cred_getuid(nuidp
->nu_cr
) == uid
&&
1568 (!nd
->nd_nam2
|| netaddr_match(NU_NETFAM(nuidp
),
1569 &nuidp
->nu_haddr
, nd
->nd_nam2
)))
1574 txdr_unsigned(nuidp
->nu_timestamp
.tv_sec
- 1);
1576 txdr_unsigned(nuidp
->nu_timestamp
.tv_usec
);
1579 * Encrypt the timestamp in ecb mode using the
1586 *tl
++ = rpc_auth_kerb
;
1587 *tl
++ = txdr_unsigned(3 * NFSX_UNSIGNED
);
1588 *tl
= ktvout
.tv_sec
;
1589 nfsm_build(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
1590 *tl
++ = ktvout
.tv_usec
;
1591 *tl
++ = txdr_unsigned(kauth_cred_getuid(nuidp
->nu_cr
));
1596 lck_rw_done(&slp
->ns_rwlock
);
1603 *tl
= txdr_unsigned(RPC_PROGUNAVAIL
);
1606 *tl
= txdr_unsigned(RPC_PROGMISMATCH
);
1607 nfsm_build(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
1608 // XXX hard coded versions
1609 *tl
++ = txdr_unsigned(2);
1610 *tl
= txdr_unsigned(3);
1613 *tl
= txdr_unsigned(RPC_PROCUNAVAIL
);
1616 *tl
= txdr_unsigned(RPC_GARBAGE
);
1620 if (err
!= NFSERR_RETVOID
) {
1621 nfsm_build(tl
, u_long
*, NFSX_UNSIGNED
);
1623 *tl
= txdr_unsigned(nfsrv_errmap(nd
, err
));
1635 if (err
!= 0 && err
!= NFSERR_RETVOID
) {
1636 OSAddAtomic(1, (SInt32
*)&nfsstats
.srvrpc_errs
);
1642 #endif /* NFS_NOSERVER */
1646 * From FreeBSD 1.58, a Matt Dillon fix...
1647 * Flag a request as being about to terminate.
1648 * The nm_sent count is decremented now to avoid deadlocks when the process
1649 * in soreceive() hasn't yet managed to send its own request.
1652 nfs_softterm(struct nfsreq
*rep
)
1655 rep
->r_flags
|= R_SOFTTERM
;
1656 if (rep
->r_flags
& R_SENT
) {
1657 FSDBG(532, rep
->r_xid
, rep
, rep
->r_nmp
->nm_sent
,
1658 rep
->r_nmp
->nm_cwnd
);
1659 rep
->r_nmp
->nm_sent
-= NFS_CWNDSCALE
;
1660 rep
->r_flags
&= ~R_SENT
;
1665 nfs_timer_funnel(void * arg
)
1667 (void) thread_funnel_set(kernel_flock
, TRUE
);
1669 (void) thread_funnel_set(kernel_flock
, FALSE
);
1674 * Ensure rep isn't in use by the timer, then dequeue it.
1677 nfs_repdequeue(struct nfsreq
*rep
)
1680 while ((rep
->r_flags
& R_BUSY
)) {
1681 rep
->r_flags
|= R_WAITING
;
1682 tsleep(rep
, PSOCK
, "repdeq", 0);
1684 TAILQ_REMOVE(&nfs_reqq
, rep
, r_chain
);
1688 * Busy (lock) a nfsreq, used by the nfs timer to make sure it's not
1689 * free()'d out from under it.
1692 nfs_repbusy(struct nfsreq
*rep
)
1695 if ((rep
->r_flags
& R_BUSY
))
1696 panic("rep locked");
1697 rep
->r_flags
|= R_BUSY
;
1701 * Unbusy the nfsreq passed in, return the next nfsreq in the chain busied.
1703 static struct nfsreq
*
1704 nfs_repnext(struct nfsreq
*rep
)
1706 struct nfsreq
* nextrep
;
1711 * We need to get and busy the next req before signalling the
1712 * current one, otherwise wakeup() may block us and we'll race to
1713 * grab the next req.
1715 nextrep
= TAILQ_NEXT(rep
, r_chain
);
1716 if (nextrep
!= NULL
)
1717 nfs_repbusy(nextrep
);
1718 /* unbusy and signal. */
1719 rep
->r_flags
&= ~R_BUSY
;
1720 if ((rep
->r_flags
& R_WAITING
)) {
1721 rep
->r_flags
&= ~R_WAITING
;
1729 * Scan the nfsreq list and retranmit any requests that have timed out
1730 * To avoid retransmission attempts on STREAM sockets (in the future) make
1731 * sure to set the r_retry field to 0 (implies nm_retry == 0).
1734 nfs_timer(__unused
void *arg
)
1739 struct nfsmount
*nmp
;
1742 #ifndef NFS_NOSERVER
1743 struct nfssvc_sock
*slp
;
1745 #endif /* NFS_NOSERVER */
1746 int flags
, rexmit
, cwnd
, sent
;
1750 rep
= TAILQ_FIRST(&nfs_reqq
);
1754 for ( ; rep
!= NULL
; rep
= nfs_repnext(rep
)) {
1756 if (!nmp
) /* unmounted */
1758 if (rep
->r_mrep
|| (rep
->r_flags
& R_SOFTTERM
))
1760 if (nfs_sigintr(nmp
, rep
, rep
->r_procp
))
1762 if (nmp
->nm_tprintf_initial_delay
!= 0 &&
1763 (rep
->r_rexmit
> 2 || (rep
->r_flags
& R_RESENDERR
)) &&
1764 rep
->r_lastmsg
+ nmp
->nm_tprintf_delay
< now
.tv_sec
) {
1765 rep
->r_lastmsg
= now
.tv_sec
;
1766 nfs_down(rep
->r_nmp
, rep
->r_procp
, 0, NFSSTA_TIMEO
,
1768 rep
->r_flags
|= R_TPRINTFMSG
;
1769 if (!(nmp
->nm_state
& NFSSTA_MOUNTED
)) {
1770 /* we're not yet completely mounted and */
1771 /* we can't complete an RPC, so we fail */
1772 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpctimeouts
);
1777 if (rep
->r_rtt
>= 0) {
1779 if (nmp
->nm_flag
& NFSMNT_DUMBTIMR
)
1780 timeo
= nmp
->nm_timeo
;
1782 timeo
= NFS_RTO(nmp
, proct
[rep
->r_procnum
]);
1783 /* ensure 62.5 ms floor */
1784 while (16 * timeo
< hz
)
1786 if (nmp
->nm_timeouts
> 0)
1787 timeo
*= nfs_backoff
[nmp
->nm_timeouts
- 1];
1788 if (rep
->r_rtt
<= timeo
)
1790 if (nmp
->nm_timeouts
< 8)
1794 * Check for too many retransmits. This is never true for
1795 * 'hard' mounts because we set r_retry to NFS_MAXREXMIT + 1
1796 * and never allow r_rexmit to be more than NFS_MAXREXMIT.
1798 if (rep
->r_rexmit
>= rep
->r_retry
) { /* too many */
1799 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpctimeouts
);
1803 if (nmp
->nm_sotype
!= SOCK_DGRAM
) {
1804 if (++rep
->r_rexmit
> NFS_MAXREXMIT
)
1805 rep
->r_rexmit
= NFS_MAXREXMIT
;
1808 if ((so
= nmp
->nm_so
) == NULL
)
1812 * If there is enough space and the window allows..
1814 * Set r_rtt to -1 in case we fail to send it now.
1817 if (((nmp
->nm_flag
& NFSMNT_DUMBTIMR
) ||
1818 (rep
->r_flags
& R_SENT
) ||
1819 nmp
->nm_sent
< nmp
->nm_cwnd
) &&
1820 (mbuf_copym(rep
->r_mreq
, 0, MBUF_COPYALL
, MBUF_DONTWAIT
, &m
) == 0)){
1823 * Iff first send, start timing
1824 * else turn timing off, backoff timer
1825 * and divide congestion window by 2.
1826 * We update these *before* the send to avoid
1827 * racing against receiving the reply.
1828 * We save them so we can restore them on send error.
1830 flags
= rep
->r_flags
;
1831 rexmit
= rep
->r_rexmit
;
1832 cwnd
= nmp
->nm_cwnd
;
1833 sent
= nmp
->nm_sent
;
1835 if (rep
->r_flags
& R_SENT
) {
1836 rep
->r_flags
&= ~R_TIMING
;
1837 if (++rep
->r_rexmit
> NFS_MAXREXMIT
)
1838 rep
->r_rexmit
= NFS_MAXREXMIT
;
1840 if (nmp
->nm_cwnd
< NFS_CWNDSCALE
)
1841 nmp
->nm_cwnd
= NFS_CWNDSCALE
;
1842 OSAddAtomic(1, (SInt32
*)&nfsstats
.rpcretries
);
1844 rep
->r_flags
|= R_SENT
;
1845 nmp
->nm_sent
+= NFS_CWNDSCALE
;
1847 FSDBG(535, xid
, rep
, nmp
->nm_sent
, nmp
->nm_cwnd
);
1849 bzero(&msg
, sizeof(msg
));
1850 if ((nmp
->nm_flag
& NFSMNT_NOCONN
) == NFSMNT_NOCONN
) {
1851 msg
.msg_name
= mbuf_data(nmp
->nm_nam
);
1852 msg
.msg_namelen
= mbuf_len(nmp
->nm_nam
);
1854 error
= sock_sendmbuf(so
, &msg
, m
, MSG_DONTWAIT
, NULL
);
1856 FSDBG(535, xid
, error
, sent
, cwnd
);
1859 if (error
== EWOULDBLOCK
) {
1860 rep
->r_flags
= flags
;
1861 rep
->r_rexmit
= rexmit
;
1862 nmp
->nm_cwnd
= cwnd
;
1863 nmp
->nm_sent
= sent
;
1867 if (NFSIGNORE_SOERROR(nmp
->nm_sotype
, error
)) {
1869 int optlen
= sizeof(clearerror
);
1870 sock_getsockopt(nmp
->nm_so
, SOL_SOCKET
, SO_ERROR
, &clearerror
, &optlen
);
1872 rep
->r_flags
= flags
| R_RESENDERR
;
1873 rep
->r_rexmit
= rexmit
;
1874 nmp
->nm_cwnd
= cwnd
;
1875 nmp
->nm_sent
= sent
;
1877 OSAddAtomic(-1, (SInt32
*)&nfsstats
.rpcretries
);
1884 #ifndef NFS_NOSERVER
1886 * Scan the write gathering queues for writes that need to be
1889 cur_usec
= (u_quad_t
)now
.tv_sec
* 1000000 + (u_quad_t
)now
.tv_usec
;
1890 lck_mtx_lock(nfsd_mutex
);
1891 TAILQ_FOREACH(slp
, &nfssvc_sockhead
, ns_chain
) {
1892 if (slp
->ns_wgtime
&& (slp
->ns_wgtime
<= cur_usec
))
1893 nfsrv_wakenfsd(slp
);
1895 lck_mtx_unlock(nfsd_mutex
);
1896 #endif /* NFS_NOSERVER */
1898 if (nfsbuffreeuptimestamp
+ 30 <= now
.tv_sec
) {
1900 * We haven't called nfs_buf_freeup() in a little while.
1901 * So, see if we can free up any stale/unused bufs now.
1906 timeout(nfs_timer_funnel
, (void *)0, nfs_ticks
);
1912 * Test for a termination condition pending on the process.
1913 * This is used to determine if we need to bail on a mount.
1914 * EIO is returned if there has been a soft timeout.
1915 * EINTR is returned if there is a signal pending that is not being ignored
1916 * and the mount is interruptable, or if we are a thread that is in the process
1917 * of cancellation (also SIGKILL posted).
1920 nfs_sigintr(nmp
, rep
, p
)
1921 struct nfsmount
*nmp
;
1925 sigset_t pending_sigs
;
1926 int context_good
= 0;
1927 struct nfsmount
*repnmp
;
1928 extern proc_t kernproc
;
1933 repnmp
= rep
->r_nmp
;
1934 /* we've had a forced unmount. */
1937 /* request has timed out on a 'soft' mount. */
1938 if (rep
->r_flags
& R_SOFTTERM
)
1941 * We're in the progress of a force unmount and there's
1942 * been a timeout we're dead and fail IO.
1944 if ((repnmp
->nm_state
& (NFSSTA_FORCE
|NFSSTA_TIMEO
)) ==
1945 (NFSSTA_FORCE
|NFSSTA_TIMEO
))
1947 /* Someone is unmounting us, go soft and mark it. */
1948 if (repnmp
->nm_mountp
->mnt_kern_flag
& MNTK_FRCUNMOUNT
) {
1949 repnmp
->nm_flag
|= NFSMNT_SOFT
;
1950 nmp
->nm_state
|= NFSSTA_FORCE
;
1953 * If the mount is hung and we've requested not to hang
1954 * on remote filesystems, then bail now.
1956 if (p
!= NULL
&& (proc_noremotehang(p
)) != 0 &&
1957 (repnmp
->nm_state
& NFSSTA_TIMEO
) != 0)
1960 /* XXX: is this valid? this probably should be an assertion. */
1964 /* Is this thread belongs to kernel task; then abort check is not needed */
1965 if ((current_proc() != kernproc
) && current_thread_aborted()) {
1968 /* mask off thread and process blocked signals. */
1970 pending_sigs
= proc_pendingsignals(p
, NFSINT_SIGMASK
);
1971 if (pending_sigs
&& (nmp
->nm_flag
& NFSMNT_INT
) != 0)
1977 * Lock a socket against others.
1978 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
1979 * and also to avoid race conditions between the processes with nfs requests
1980 * in progress when a reconnect is necessary.
1988 int error
, slpflag
= 0, slptimeo
= 0;
1990 if (rep
->r_nmp
== NULL
)
1992 statep
= &rep
->r_nmp
->nm_state
;
1995 if (rep
->r_nmp
->nm_flag
& NFSMNT_INT
)
1997 while (*statep
& NFSSTA_SNDLOCK
) {
1998 error
= nfs_sigintr(rep
->r_nmp
, rep
, p
);
2001 *statep
|= NFSSTA_WANTSND
;
2002 if (p
!= NULL
&& (proc_noremotehang(p
)) != 0)
2004 tsleep((caddr_t
)statep
, slpflag
| (PZERO
- 1), "nfsndlck", slptimeo
);
2005 if (slpflag
== PCATCH
) {
2010 * Make sure while we slept that the mountpoint didn't go away.
2011 * nfs_sigintr and callers expect it in tact.
2014 return (ENXIO
); /* don't have lock until out of loop */
2016 *statep
|= NFSSTA_SNDLOCK
;
2021 * Unlock the stream socket for others.
2029 if (rep
->r_nmp
== NULL
)
2031 statep
= &rep
->r_nmp
->nm_state
;
2032 if ((*statep
& NFSSTA_SNDLOCK
) == 0)
2033 panic("nfs sndunlock");
2034 *statep
&= ~NFSSTA_SNDLOCK
;
2035 if (*statep
& NFSSTA_WANTSND
) {
2036 *statep
&= ~NFSSTA_WANTSND
;
2037 wakeup((caddr_t
)statep
);
2042 nfs_rcvlock(struct nfsreq
*rep
)
2045 int error
, slpflag
, slptimeo
= 0;
2047 /* make sure we still have our mountpoint */
2049 if (rep
->r_mrep
!= NULL
)
2054 statep
= &rep
->r_nmp
->nm_state
;
2055 FSDBG_TOP(534, rep
->r_xid
, rep
, rep
->r_nmp
, *statep
);
2056 if (rep
->r_nmp
->nm_flag
& NFSMNT_INT
)
2060 while (*statep
& NFSSTA_RCVLOCK
) {
2061 if ((error
= nfs_sigintr(rep
->r_nmp
, rep
, rep
->r_procp
))) {
2062 FSDBG_BOT(534, rep
->r_xid
, rep
, rep
->r_nmp
, 0x100);
2064 } else if (rep
->r_mrep
!= NULL
) {
2066 * Don't bother sleeping if reply already arrived
2068 FSDBG_BOT(534, rep
->r_xid
, rep
, rep
->r_nmp
, 0x101);
2071 FSDBG(534, rep
->r_xid
, rep
, rep
->r_nmp
, 0x102);
2072 *statep
|= NFSSTA_WANTRCV
;
2074 * We need to poll if we're P_NOREMOTEHANG so that we
2075 * call nfs_sigintr periodically above.
2077 if (rep
->r_procp
!= NULL
&&
2078 (proc_noremotehang(rep
->r_procp
)) != 0)
2080 tsleep((caddr_t
)statep
, slpflag
| (PZERO
- 1), "nfsrcvlk", slptimeo
);
2081 if (slpflag
== PCATCH
) {
2086 * Make sure while we slept that the mountpoint didn't go away.
2087 * nfs_sigintr and caller nfs_reply expect it intact.
2090 FSDBG_BOT(534, rep
->r_xid
, rep
, rep
->r_nmp
, 0x103);
2091 return (ENXIO
); /* don't have lock until out of loop */
2095 * nfs_reply will handle it if reply already arrived.
2096 * (We may have slept or been preempted).
2098 FSDBG_BOT(534, rep
->r_xid
, rep
, rep
->r_nmp
, *statep
);
2099 *statep
|= NFSSTA_RCVLOCK
;
2104 * Unlock the stream socket for others.
2107 nfs_rcvunlock(struct nfsreq
*rep
)
2111 if (rep
->r_nmp
== NULL
)
2113 statep
= &rep
->r_nmp
->nm_state
;
2115 FSDBG(533, statep
, *statep
, 0, 0);
2116 if ((*statep
& NFSSTA_RCVLOCK
) == 0)
2117 panic("nfs rcvunlock");
2118 *statep
&= ~NFSSTA_RCVLOCK
;
2119 if (*statep
& NFSSTA_WANTRCV
) {
2120 *statep
&= ~NFSSTA_WANTRCV
;
2121 wakeup((caddr_t
)statep
);
2126 #ifndef NFS_NOSERVER
2128 * Socket upcall routine for the nfsd sockets.
2129 * The caddr_t arg is a pointer to the "struct nfssvc_sock".
2130 * Essentially do as much as possible non-blocking, else punt and it will
2131 * be called with MBUF_WAITOK from an nfsd.
2134 nfsrv_rcv(socket_t so
, caddr_t arg
, int waitflag
)
2136 struct nfssvc_sock
*slp
= (struct nfssvc_sock
*)arg
;
2138 if (!nfs_numnfsd
|| !(slp
->ns_flag
& SLP_VALID
))
2141 lck_rw_lock_exclusive(&slp
->ns_rwlock
);
2142 nfsrv_rcv_locked(so
, slp
, waitflag
);
2143 /* Note: ns_rwlock gets dropped when called with MBUF_DONTWAIT */
2146 nfsrv_rcv_locked(socket_t so
, struct nfssvc_sock
*slp
, int waitflag
)
2148 mbuf_t m
, mp
, mhck
, m2
;
2149 int ns_flag
=0, error
;
2153 if ((slp
->ns_flag
& SLP_VALID
) == 0) {
2154 if (waitflag
== MBUF_DONTWAIT
)
2155 lck_rw_done(&slp
->ns_rwlock
);
2161 * Define this to test for nfsds handling this under heavy load.
2163 if (waitflag
== MBUF_DONTWAIT
) {
2164 ns_flag
= SLP_NEEDQ
;
2168 if (slp
->ns_sotype
== SOCK_STREAM
) {
2170 * If there are already records on the queue, defer soreceive()
2171 * to an nfsd so that there is feedback to the TCP layer that
2172 * the nfs servers are heavily loaded.
2174 if (slp
->ns_rec
&& waitflag
== MBUF_DONTWAIT
) {
2175 ns_flag
= SLP_NEEDQ
;
2182 bytes_read
= 1000000000;
2183 error
= sock_receivembuf(so
, NULL
, &mp
, MSG_DONTWAIT
, &bytes_read
);
2184 if (error
|| mp
== NULL
) {
2185 if (error
== EWOULDBLOCK
)
2186 ns_flag
= SLP_NEEDQ
;
2188 ns_flag
= SLP_DISCONN
;
2192 if (slp
->ns_rawend
) {
2193 if ((error
= mbuf_setnext(slp
->ns_rawend
, m
)))
2194 panic("nfsrv_rcv: mbuf_setnext failed %d\n", error
);
2195 slp
->ns_cc
+= bytes_read
;
2198 slp
->ns_cc
= bytes_read
;
2200 while ((m2
= mbuf_next(m
)))
2205 * Now try and parse record(s) out of the raw stream data.
2207 error
= nfsrv_getstream(slp
, waitflag
);
2210 ns_flag
= SLP_DISCONN
;
2212 ns_flag
= SLP_NEEDQ
;
2215 struct sockaddr_storage nam
;
2217 bzero(&msg
, sizeof(msg
));
2218 msg
.msg_name
= (caddr_t
)&nam
;
2219 msg
.msg_namelen
= sizeof(nam
);
2222 bytes_read
= 1000000000;
2223 error
= sock_receivembuf(so
, &msg
, &mp
, MSG_DONTWAIT
| MSG_NEEDSA
, &bytes_read
);
2225 if (msg
.msg_name
&& (mbuf_get(MBUF_WAITOK
, MBUF_TYPE_SONAME
, &mhck
) == 0)) {
2226 mbuf_setlen(mhck
, nam
.ss_len
);
2227 bcopy(&nam
, mbuf_data(mhck
), nam
.ss_len
);
2229 if (mbuf_setnext(m
, mp
)) {
2230 /* trouble... just drop it */
2231 printf("nfsrv_rcv: mbuf_setnext failed\n");
2239 mbuf_setnextpkt(slp
->ns_recend
, m
);
2243 mbuf_setnextpkt(m
, NULL
);
2248 * This may be needed in the future to support
2249 * non-byte-stream connection-oriented protocols
2253 * This (slp->ns_sotype == SOCK_STREAM) should really
2254 * be a check for PR_CONNREQUIRED.
2256 if ((slp
->ns_sotype
== SOCK_STREAM
)
2257 && error
!= EWOULDBLOCK
) {
2258 ns_flag
= SLP_DISCONN
;
2267 * Now try and process the request records, non-blocking.
2271 slp
->ns_flag
|= ns_flag
;
2272 if (waitflag
== MBUF_DONTWAIT
) {
2273 int wake
= (slp
->ns_rec
|| (slp
->ns_flag
& (SLP_NEEDQ
| SLP_DISCONN
)));
2274 lck_rw_done(&slp
->ns_rwlock
);
2275 if (wake
&& nfs_numnfsd
) {
2276 lck_mtx_lock(nfsd_mutex
);
2277 nfsrv_wakenfsd(slp
);
2278 lck_mtx_unlock(nfsd_mutex
);
2284 * Try and extract an RPC request from the mbuf data list received on a
2285 * stream socket. The "waitflag" argument indicates whether or not it
2289 nfsrv_getstream(slp
, waitflag
)
2290 struct nfssvc_sock
*slp
;
2294 char *cp1
, *cp2
, *mdata
;
2295 int len
, mlen
, error
;
2296 mbuf_t om
, m2
, recm
;
2299 if (slp
->ns_flag
& SLP_GETSTREAM
)
2300 panic("nfs getstream");
2301 slp
->ns_flag
|= SLP_GETSTREAM
;
2303 if (slp
->ns_reclen
== 0) {
2304 if (slp
->ns_cc
< NFSX_UNSIGNED
) {
2305 slp
->ns_flag
&= ~SLP_GETSTREAM
;
2309 mdata
= mbuf_data(m
);
2311 if (mlen
>= NFSX_UNSIGNED
) {
2312 bcopy(mdata
, (caddr_t
)&recmark
, NFSX_UNSIGNED
);
2313 mdata
+= NFSX_UNSIGNED
;
2314 mlen
-= NFSX_UNSIGNED
;
2315 mbuf_setdata(m
, mdata
, mlen
);
2317 cp1
= (caddr_t
)&recmark
;
2319 while (cp1
< ((caddr_t
)&recmark
) + NFSX_UNSIGNED
) {
2327 mbuf_setdata(m
, cp2
, mlen
);
2330 slp
->ns_cc
-= NFSX_UNSIGNED
;
2331 recmark
= ntohl(recmark
);
2332 slp
->ns_reclen
= recmark
& ~0x80000000;
2333 if (recmark
& 0x80000000)
2334 slp
->ns_flag
|= SLP_LASTFRAG
;
2336 slp
->ns_flag
&= ~SLP_LASTFRAG
;
2337 if (slp
->ns_reclen
< NFS_MINPACKET
|| slp
->ns_reclen
> NFS_MAXPACKET
) {
2338 slp
->ns_flag
&= ~SLP_GETSTREAM
;
2344 * Now get the record part.
2346 * Note that slp->ns_reclen may be 0. Linux sometimes
2347 * generates 0-length RPCs
2350 if (slp
->ns_cc
== slp
->ns_reclen
) {
2352 slp
->ns_raw
= slp
->ns_rawend
= NULL
;
2353 slp
->ns_cc
= slp
->ns_reclen
= 0;
2354 } else if (slp
->ns_cc
> slp
->ns_reclen
) {
2358 mdata
= mbuf_data(m
);
2360 while (len
< slp
->ns_reclen
) {
2361 if ((len
+ mlen
) > slp
->ns_reclen
) {
2362 if (mbuf_copym(m
, 0, slp
->ns_reclen
- len
, waitflag
, &m2
)) {
2363 slp
->ns_flag
&= ~SLP_GETSTREAM
;
2364 return (EWOULDBLOCK
);
2367 if (mbuf_setnext(om
, m2
)) {
2368 /* trouble... just drop it */
2369 printf("nfsrv_getstream: mbuf_setnext failed\n");
2371 slp
->ns_flag
&= ~SLP_GETSTREAM
;
2372 return (EWOULDBLOCK
);
2378 mdata
+= slp
->ns_reclen
- len
;
2379 mlen
-= slp
->ns_reclen
- len
;
2380 mbuf_setdata(m
, mdata
, mlen
);
2381 len
= slp
->ns_reclen
;
2382 } else if ((len
+ mlen
) == slp
->ns_reclen
) {
2387 if (mbuf_setnext(om
, NULL
)) {
2388 printf("nfsrv_getstream: mbuf_setnext failed 2\n");
2389 slp
->ns_flag
&= ~SLP_GETSTREAM
;
2390 return (EWOULDBLOCK
);
2393 mdata
= mbuf_data(m
);
2399 mdata
= mbuf_data(m
);
2406 slp
->ns_flag
&= ~SLP_GETSTREAM
;
2411 * Accumulate the fragments into a record.
2413 if (slp
->ns_frag
== NULL
) {
2414 slp
->ns_frag
= recm
;
2417 while ((m2
= mbuf_next(m
)))
2419 if ((error
= mbuf_setnext(m
, recm
)))
2420 panic("nfsrv_getstream: mbuf_setnext failed 3, %d\n", error
);
2422 if (slp
->ns_flag
& SLP_LASTFRAG
) {
2424 mbuf_setnextpkt(slp
->ns_recend
, slp
->ns_frag
);
2426 slp
->ns_rec
= slp
->ns_frag
;
2427 slp
->ns_recend
= slp
->ns_frag
;
2428 slp
->ns_frag
= NULL
;
2434 * Parse an RPC header.
2437 nfsrv_dorec(slp
, nfsd
, ndp
)
2438 struct nfssvc_sock
*slp
;
2440 struct nfsrv_descript
**ndp
;
2444 struct nfsrv_descript
*nd
;
2448 if ((slp
->ns_flag
& SLP_VALID
) == 0 || (slp
->ns_rec
== NULL
))
2450 MALLOC_ZONE(nd
, struct nfsrv_descript
*,
2451 sizeof (struct nfsrv_descript
), M_NFSRVDESC
, M_WAITOK
);
2455 slp
->ns_rec
= mbuf_nextpkt(m
);
2457 mbuf_setnextpkt(m
, NULL
);
2459 slp
->ns_recend
= NULL
;
2460 if (mbuf_type(m
) == MBUF_TYPE_SONAME
) {
2463 if ((error
= mbuf_setnext(nam
, NULL
)))
2464 panic("nfsrv_dorec: mbuf_setnext failed %d\n", error
);
2467 nd
->nd_md
= nd
->nd_mrep
= m
;
2469 nd
->nd_dpos
= mbuf_data(m
);
2470 error
= nfs_getreq(nd
, nfsd
, TRUE
);
2474 FREE_ZONE((caddr_t
)nd
, sizeof *nd
, M_NFSRVDESC
);
2483 * Parse an RPC request
2485 * - fill in the cred struct.
2488 nfs_getreq(nd
, nfsd
, has_header
)
2489 struct nfsrv_descript
*nd
;
2497 caddr_t dpos
, cp2
, cp
;
2498 u_long nfsvers
, auth_type
;
2500 int error
= 0, ticklen
;
2502 struct nfsuid
*nuidp
;
2506 struct ucred temp_cred
;
2507 struct timeval tvin
, tvout
, now
;
2508 char uio_buf
[ UIO_SIZEOF(1) ];
2509 #if 0 /* until encrypted keys are implemented */
2510 NFSKERBKEYSCHED_T keys
; /* stores key schedule */
2519 nfsm_dissect(tl
, u_long
*, 10 * NFSX_UNSIGNED
);
2520 nd
->nd_retxid
= fxdr_unsigned(u_long
, *tl
++);
2521 if (*tl
++ != rpc_call
) {
2526 nfsm_dissect(tl
, u_long
*, 8 * NFSX_UNSIGNED
);
2529 if (*tl
++ != rpc_vers
) {
2530 nd
->nd_repstat
= ERPCMISMATCH
;
2531 nd
->nd_procnum
= NFSPROC_NOOP
;
2534 if (*tl
!= nfs_prog
) {
2535 nd
->nd_repstat
= EPROGUNAVAIL
;
2536 nd
->nd_procnum
= NFSPROC_NOOP
;
2540 nfsvers
= fxdr_unsigned(u_long
, *tl
++);
2541 if ((nfsvers
< NFS_VER2
) || (nfsvers
> NFS_VER3
)) {
2542 nd
->nd_repstat
= EPROGMISMATCH
;
2543 nd
->nd_procnum
= NFSPROC_NOOP
;
2546 else if (nfsvers
== NFS_VER3
)
2547 nd
->nd_flag
= ND_NFSV3
;
2548 nd
->nd_procnum
= fxdr_unsigned(u_long
, *tl
++);
2549 if (nd
->nd_procnum
== NFSPROC_NULL
)
2551 if ((nd
->nd_procnum
>= NFS_NPROCS
) ||
2552 (!nd
->nd_flag
&& nd
->nd_procnum
> NFSV2PROC_STATFS
)) {
2553 nd
->nd_repstat
= EPROCUNAVAIL
;
2554 nd
->nd_procnum
= NFSPROC_NOOP
;
2557 if ((nd
->nd_flag
& ND_NFSV3
) == 0)
2558 nd
->nd_procnum
= nfsv3_procid
[nd
->nd_procnum
];
2560 len
= fxdr_unsigned(int, *tl
++);
2561 if (len
< 0 || len
> RPCAUTH_MAXSIZ
) {
2566 nd
->nd_flag
&= ~ND_KERBAUTH
;
2568 * Handle auth_unix or auth_kerb.
2570 if (auth_type
== rpc_auth_unix
) {
2571 len
= fxdr_unsigned(int, *++tl
);
2572 if (len
< 0 || len
> NFS_MAXNAMLEN
) {
2576 bzero(&temp_cred
, sizeof(temp_cred
));
2577 nfsm_adv(nfsm_rndup(len
));
2578 nfsm_dissect(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
2579 user_id
= fxdr_unsigned(uid_t
, *tl
++);
2580 group_id
= fxdr_unsigned(gid_t
, *tl
++);
2581 temp_cred
.cr_groups
[0] = group_id
;
2582 len
= fxdr_unsigned(int, *tl
);
2583 if (len
< 0 || len
> RPCAUTH_UNIXGIDS
) {
2587 nfsm_dissect(tl
, u_long
*, (len
+ 2) * NFSX_UNSIGNED
);
2588 for (i
= 1; i
<= len
; i
++)
2590 temp_cred
.cr_groups
[i
] = fxdr_unsigned(gid_t
, *tl
++);
2593 ngroups
= (len
>= NGROUPS
) ? NGROUPS
: (len
+ 1);
2595 nfsrvw_sort(&temp_cred
.cr_groups
[0], ngroups
);
2596 len
= fxdr_unsigned(int, *++tl
);
2597 if (len
< 0 || len
> RPCAUTH_MAXSIZ
) {
2601 temp_cred
.cr_uid
= user_id
;
2602 temp_cred
.cr_ngroups
= ngroups
;
2603 nd
->nd_cr
= kauth_cred_create(&temp_cred
);
2604 if (nd
->nd_cr
== NULL
) {
2605 nd
->nd_repstat
= ENOMEM
;
2606 nd
->nd_procnum
= NFSPROC_NOOP
;
2610 nfsm_adv(nfsm_rndup(len
));
2611 } else if (auth_type
== rpc_auth_kerb
) {
2612 switch (fxdr_unsigned(int, *tl
++)) {
2613 case RPCAKN_FULLNAME
:
2614 ticklen
= fxdr_unsigned(int, *tl
);
2615 *((u_long
*)nfsd
->nfsd_authstr
) = *tl
;
2616 uiop
= uio_createwithbuffer(1, 0, UIO_SYSSPACE
, UIO_READ
,
2617 &uio_buf
[0], sizeof(uio_buf
));
2619 nd
->nd_repstat
= ENOMEM
;
2620 nd
->nd_procnum
= NFSPROC_NOOP
;
2624 // LP64todo - fix this
2625 nfsd
->nfsd_authlen
= (nfsm_rndup(ticklen
) + (NFSX_UNSIGNED
* 2));
2626 if ((nfsm_rndup(ticklen
) + NFSX_UNSIGNED
) > (len
- 2 * NFSX_UNSIGNED
)) {
2630 uio_addiov(uiop
, CAST_USER_ADDR_T(&nfsd
->nfsd_authstr
[4]), RPCAUTH_MAXSIZ
- 4);
2631 // LP64todo - fix this
2632 nfsm_mtouio(uiop
, uio_resid(uiop
));
2633 nfsm_dissect(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
2634 if (*tl
++ != rpc_auth_kerb
||
2635 fxdr_unsigned(int, *tl
) != 4 * NFSX_UNSIGNED
) {
2636 printf("Bad kerb verifier\n");
2637 nd
->nd_repstat
= (NFSERR_AUTHERR
|AUTH_BADVERF
);
2638 nd
->nd_procnum
= NFSPROC_NOOP
;
2641 nfsm_dissect(cp
, caddr_t
, 4 * NFSX_UNSIGNED
);
2643 if (fxdr_unsigned(int, *tl
) != RPCAKN_FULLNAME
) {
2644 printf("Not fullname kerb verifier\n");
2645 nd
->nd_repstat
= (NFSERR_AUTHERR
|AUTH_BADVERF
);
2646 nd
->nd_procnum
= NFSPROC_NOOP
;
2649 cp
+= NFSX_UNSIGNED
;
2650 bcopy(cp
, nfsd
->nfsd_verfstr
, 3 * NFSX_UNSIGNED
);
2651 nfsd
->nfsd_verflen
= 3 * NFSX_UNSIGNED
;
2652 nd
->nd_flag
|= ND_KERBFULL
;
2653 nfsd
->nfsd_flag
|= NFSD_NEEDAUTH
;
2655 case RPCAKN_NICKNAME
:
2656 if (len
!= 2 * NFSX_UNSIGNED
) {
2657 printf("Kerb nickname short\n");
2658 nd
->nd_repstat
= (NFSERR_AUTHERR
|AUTH_BADCRED
);
2659 nd
->nd_procnum
= NFSPROC_NOOP
;
2662 nickuid
= fxdr_unsigned(uid_t
, *tl
);
2663 nfsm_dissect(tl
, u_long
*, 2 * NFSX_UNSIGNED
);
2664 if (*tl
++ != rpc_auth_kerb
||
2665 fxdr_unsigned(int, *tl
) != 3 * NFSX_UNSIGNED
) {
2666 printf("Kerb nick verifier bad\n");
2667 nd
->nd_repstat
= (NFSERR_AUTHERR
|AUTH_BADVERF
);
2668 nd
->nd_procnum
= NFSPROC_NOOP
;
2671 nfsm_dissect(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
2672 tvin
.tv_sec
= *tl
++;
2675 for (nuidp
= NUIDHASH(nfsd
->nfsd_slp
,nickuid
)->lh_first
;
2676 nuidp
!= 0; nuidp
= nuidp
->nu_hash
.le_next
) {
2677 if (kauth_cred_getuid(nuidp
->nu_cr
) == nickuid
&&
2679 netaddr_match(NU_NETFAM(nuidp
),
2680 &nuidp
->nu_haddr
, nd
->nd_nam2
)))
2685 (NFSERR_AUTHERR
|AUTH_REJECTCRED
);
2686 nd
->nd_procnum
= NFSPROC_NOOP
;
2691 * Now, decrypt the timestamp using the session key
2698 tvout
.tv_sec
= fxdr_unsigned(long, tvout
.tv_sec
);
2699 tvout
.tv_usec
= fxdr_unsigned(long, tvout
.tv_usec
);
2701 if (nuidp
->nu_expire
< now
.tv_sec
||
2702 nuidp
->nu_timestamp
.tv_sec
> tvout
.tv_sec
||
2703 (nuidp
->nu_timestamp
.tv_sec
== tvout
.tv_sec
&&
2704 nuidp
->nu_timestamp
.tv_usec
> tvout
.tv_usec
)) {
2705 nuidp
->nu_expire
= 0;
2707 (NFSERR_AUTHERR
|AUTH_REJECTVERF
);
2708 nd
->nd_procnum
= NFSPROC_NOOP
;
2711 bzero(&temp_cred
, sizeof(temp_cred
));
2712 ngroups
= nuidp
->nu_cr
->cr_ngroups
;
2713 for (i
= 0; i
< ngroups
; i
++)
2714 temp_cred
.cr_groups
[i
] = nuidp
->nu_cr
->cr_groups
[i
];
2716 nfsrvw_sort(&temp_cred
.cr_groups
[0], ngroups
);
2718 temp_cred
.cr_uid
= kauth_cred_getuid(nuidp
->nu_cr
);
2719 temp_cred
.cr_ngroups
= ngroups
;
2720 nd
->nd_cr
= kauth_cred_create(&temp_cred
);
2722 nd
->nd_repstat
= ENOMEM
;
2723 nd
->nd_procnum
= NFSPROC_NOOP
;
2726 nd
->nd_flag
|= ND_KERBNICK
;
2729 nd
->nd_repstat
= (NFSERR_AUTHERR
| AUTH_REJECTCRED
);
2730 nd
->nd_procnum
= NFSPROC_NOOP
;
2739 kauth_cred_rele(nd
->nd_cr
);
2744 * Search for a sleeping nfsd and wake it up.
2745 * SIDE EFFECT: If none found, set NFSD_CHECKSLP flag, so that one of the
2746 * running nfsds will go look for the work in the nfssvc_sock list.
2747 * Note: Must be called with nfsd_mutex held.
2750 nfsrv_wakenfsd(struct nfssvc_sock
*slp
)
2754 if ((slp
->ns_flag
& SLP_VALID
) == 0)
2757 lck_rw_lock_exclusive(&slp
->ns_rwlock
);
2760 TAILQ_FOREACH(nd
, &nfsd_head
, nfsd_chain
) {
2761 if (nd
->nfsd_flag
& NFSD_WAITING
) {
2762 nd
->nfsd_flag
&= ~NFSD_WAITING
;
2764 panic("nfsd wakeup");
2767 lck_rw_done(&slp
->ns_rwlock
);
2768 wakeup((caddr_t
)nd
);
2774 slp
->ns_flag
|= SLP_DOREC
;
2776 lck_rw_done(&slp
->ns_rwlock
);
2778 nfsd_head_flag
|= NFSD_CHECKSLP
;
2780 #endif /* NFS_NOSERVER */
2791 tpr
= tprintf_open(p
);
2795 tprintf(tpr
, "nfs server %s: %s, error %d\n", server
, msg
,
2798 tprintf(tpr
, "nfs server %s: %s\n", server
, msg
);
2804 nfs_down(nmp
, proc
, error
, flags
, msg
)
2805 struct nfsmount
*nmp
;
2812 if ((flags
& NFSSTA_TIMEO
) && !(nmp
->nm_state
& NFSSTA_TIMEO
)) {
2813 vfs_event_signal(&vfs_statfs(nmp
->nm_mountp
)->f_fsid
, VQ_NOTRESP
, 0);
2814 nmp
->nm_state
|= NFSSTA_TIMEO
;
2816 if ((flags
& NFSSTA_LOCKTIMEO
) && !(nmp
->nm_state
& NFSSTA_LOCKTIMEO
)) {
2817 vfs_event_signal(&vfs_statfs(nmp
->nm_mountp
)->f_fsid
, VQ_NOTRESPLOCK
, 0);
2818 nmp
->nm_state
|= NFSSTA_LOCKTIMEO
;
2820 nfs_msg(proc
, vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, msg
, error
);
2824 nfs_up(nmp
, proc
, flags
, msg
)
2825 struct nfsmount
*nmp
;
2833 nfs_msg(proc
, vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, msg
, 0);
2834 if ((flags
& NFSSTA_TIMEO
) && (nmp
->nm_state
& NFSSTA_TIMEO
)) {
2835 nmp
->nm_state
&= ~NFSSTA_TIMEO
;
2836 vfs_event_signal(&vfs_statfs(nmp
->nm_mountp
)->f_fsid
, VQ_NOTRESP
, 1);
2838 if ((flags
& NFSSTA_LOCKTIMEO
) && (nmp
->nm_state
& NFSSTA_LOCKTIMEO
)) {
2839 nmp
->nm_state
&= ~NFSSTA_LOCKTIMEO
;
2840 vfs_event_signal(&vfs_statfs(nmp
->nm_mountp
)->f_fsid
, VQ_NOTRESPLOCK
, 1);