2 * Copyright (c) 2000-2014 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1989, 1991, 1993, 1995
31 * The Regents of the University of California. All rights reserved.
33 * This code is derived from software contributed to Berkeley by
34 * Rick Macklem at The University of Guelph.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * @(#)nfs_socket.c 8.5 (Berkeley) 3/30/95
65 * FreeBSD-Id: nfs_socket.c,v 1.30 1997/10/28 15:59:07 bde Exp $
69 * Socket operations for use by nfs
72 #include <sys/param.h>
73 #include <sys/systm.h>
75 #include <sys/signalvar.h>
76 #include <sys/kauth.h>
77 #include <sys/mount_internal.h>
78 #include <sys/kernel.h>
79 #include <sys/kpi_mbuf.h>
80 #include <sys/malloc.h>
81 #include <sys/vnode.h>
82 #include <sys/domain.h>
83 #include <sys/protosw.h>
84 #include <sys/socket.h>
85 #include <sys/syslog.h>
86 #include <sys/tprintf.h>
87 #include <libkern/OSAtomic.h>
90 #include <kern/clock.h>
91 #include <kern/task.h>
92 #include <kern/thread.h>
93 #include <kern/thread_call.h>
97 #include <netinet/in.h>
98 #include <netinet/tcp.h>
100 #include <nfs/rpcv2.h>
101 #include <nfs/krpc.h>
102 #include <nfs/nfsproto.h>
104 #include <nfs/xdr_subs.h>
105 #include <nfs/nfsm_subs.h>
106 #include <nfs/nfs_gss.h>
107 #include <nfs/nfsmount.h>
108 #include <nfs/nfsnode.h>
110 #define NFS_SOCK_DBG(...) NFS_DBG(NFS_FAC_SOCK, 7, ## __VA_ARGS__)
113 boolean_t
current_thread_aborted(void);
114 kern_return_t
thread_terminate(thread_t
);
118 int nfsrv_sock_max_rec_queue_length
= 128; /* max # RPC records queued on (UDP) socket */
120 int nfsrv_getstream(struct nfsrv_sock
*,int);
121 int nfsrv_getreq(struct nfsrv_descript
*);
122 extern int nfsv3_procid
[NFS_NPROCS
];
123 #endif /* NFSSERVER */
126 * compare two sockaddr structures
129 nfs_sockaddr_cmp(struct sockaddr
*sa1
, struct sockaddr
*sa2
)
135 if (sa1
->sa_family
!= sa2
->sa_family
)
136 return ((sa1
->sa_family
< sa2
->sa_family
) ? -1 : 1);
137 if (sa1
->sa_len
!= sa2
->sa_len
)
138 return ((sa1
->sa_len
< sa2
->sa_len
) ? -1 : 1);
139 if (sa1
->sa_family
== AF_INET
)
140 return (bcmp(&((struct sockaddr_in
*)sa1
)->sin_addr
,
141 &((struct sockaddr_in
*)sa2
)->sin_addr
, sizeof(((struct sockaddr_in
*)sa1
)->sin_addr
)));
142 if (sa1
->sa_family
== AF_INET6
)
143 return (bcmp(&((struct sockaddr_in6
*)sa1
)->sin6_addr
,
144 &((struct sockaddr_in6
*)sa2
)->sin6_addr
, sizeof(((struct sockaddr_in6
*)sa1
)->sin6_addr
)));
150 int nfs_connect_search_new_socket(struct nfsmount
*, struct nfs_socket_search
*, struct timeval
*);
151 int nfs_connect_search_socket_connect(struct nfsmount
*, struct nfs_socket
*, int);
152 int nfs_connect_search_ping(struct nfsmount
*, struct nfs_socket
*, struct timeval
*);
153 void nfs_connect_search_socket_found(struct nfsmount
*, struct nfs_socket_search
*, struct nfs_socket
*);
154 void nfs_connect_search_socket_reap(struct nfsmount
*, struct nfs_socket_search
*, struct timeval
*);
155 int nfs_connect_search_check(struct nfsmount
*, struct nfs_socket_search
*, struct timeval
*);
156 int nfs_reconnect(struct nfsmount
*);
157 int nfs_connect_setup(struct nfsmount
*);
158 void nfs_mount_sock_thread(void *, wait_result_t
);
159 void nfs_udp_rcv(socket_t
, void*, int);
160 void nfs_tcp_rcv(socket_t
, void*, int);
161 void nfs_sock_poke(struct nfsmount
*);
162 void nfs_request_match_reply(struct nfsmount
*, mbuf_t
);
163 void nfs_reqdequeue(struct nfsreq
*);
164 void nfs_reqbusy(struct nfsreq
*);
165 struct nfsreq
*nfs_reqnext(struct nfsreq
*);
166 int nfs_wait_reply(struct nfsreq
*);
167 void nfs_softterm(struct nfsreq
*);
168 int nfs_can_squish(struct nfsmount
*);
169 int nfs_is_squishy(struct nfsmount
*);
170 int nfs_is_dead(int, struct nfsmount
*);
173 * Estimate rto for an nfs rpc sent via. an unreliable datagram.
174 * Use the mean and mean deviation of rtt for the appropriate type of rpc
175 * for the frequent rpcs and a default for the others.
176 * The justification for doing "other" this way is that these rpcs
177 * happen so infrequently that timer est. would probably be stale.
178 * Also, since many of these rpcs are
179 * non-idempotent, a conservative timeout is desired.
180 * getattr, lookup - A+2D
184 #define NFS_RTO(n, t) \
185 ((t) == 0 ? (n)->nm_timeo : \
187 (((((n)->nm_srtt[t-1] + 3) >> 2) + (n)->nm_sdrtt[t-1] + 1) >> 1) : \
188 ((((n)->nm_srtt[t-1] + 7) >> 3) + (n)->nm_sdrtt[t-1] + 1)))
189 #define NFS_SRTT(r) (r)->r_nmp->nm_srtt[proct[(r)->r_procnum] - 1]
190 #define NFS_SDRTT(r) (r)->r_nmp->nm_sdrtt[proct[(r)->r_procnum] - 1]
193 * Defines which timer to use for the procnum.
200 static int proct
[NFS_NPROCS
] = {
201 0, 1, 0, 2, 1, 3, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0
205 * There is a congestion window for outstanding rpcs maintained per mount
206 * point. The cwnd size is adjusted in roughly the way that:
207 * Van Jacobson, Congestion avoidance and Control, In "Proceedings of
208 * SIGCOMM '88". ACM, August 1988.
209 * describes for TCP. The cwnd size is chopped in half on a retransmit timeout
210 * and incremented by 1/cwnd when each rpc reply is received and a full cwnd
211 * of rpcs is in progress.
212 * (The sent count and cwnd are scaled for integer arith.)
213 * Variants of "slow start" were tried and were found to be too much of a
214 * performance hit (ave. rtt 3 times larger),
215 * I suspect due to the large rtt that nfs rpcs have.
217 #define NFS_CWNDSCALE 256
218 #define NFS_MAXCWND (NFS_CWNDSCALE * 32)
219 static int nfs_backoff
[8] = { 2, 4, 8, 16, 32, 64, 128, 256, };
222 * Increment location index to next address/server/location.
225 nfs_location_next(struct nfs_fs_locations
*nlp
, struct nfs_location_index
*nlip
)
227 uint8_t loc
= nlip
->nli_loc
;
228 uint8_t serv
= nlip
->nli_serv
;
229 uint8_t addr
= nlip
->nli_addr
;
231 /* move to next address */
233 if (addr
>= nlp
->nl_locations
[loc
]->nl_servers
[serv
]->ns_addrcount
) {
234 /* no more addresses on current server, go to first address of next server */
238 if (serv
>= nlp
->nl_locations
[loc
]->nl_servcount
) {
239 /* no more servers on current location, go to first server of next location */
242 if (loc
>= nlp
->nl_numlocs
)
243 loc
= 0; /* after last location, wrap back around to first location */
247 * It's possible for this next server to not have any addresses.
248 * Check for that here and go to the next server.
249 * But bail out if we've managed to come back around to the original
250 * location that was passed in. (That would mean no servers had any
251 * addresses. And we don't want to spin here forever.)
253 if ((loc
== nlip
->nli_loc
) && (serv
== nlip
->nli_serv
) && (addr
== nlip
->nli_addr
))
255 if (addr
>= nlp
->nl_locations
[loc
]->nl_servers
[serv
]->ns_addrcount
)
259 nlip
->nli_serv
= serv
;
260 nlip
->nli_addr
= addr
;
264 * Compare two location indices.
267 nfs_location_index_cmp(struct nfs_location_index
*nlip1
, struct nfs_location_index
*nlip2
)
269 if (nlip1
->nli_loc
!= nlip2
->nli_loc
)
270 return (nlip1
->nli_loc
- nlip2
->nli_loc
);
271 if (nlip1
->nli_serv
!= nlip2
->nli_serv
)
272 return (nlip1
->nli_serv
- nlip2
->nli_serv
);
273 return (nlip1
->nli_addr
- nlip2
->nli_addr
);
277 * Get the mntfromname (or path portion only) for a given location.
280 nfs_location_mntfromname(struct nfs_fs_locations
*locs
, struct nfs_location_index idx
, char *s
, int size
, int pathonly
)
282 struct nfs_fs_location
*fsl
= locs
->nl_locations
[idx
.nli_loc
];
288 cnt
= snprintf(p
, size
, "%s:", fsl
->nl_servers
[idx
.nli_serv
]->ns_name
);
292 if (fsl
->nl_path
.np_compcount
== 0) {
293 /* mounting root export on server */
300 /* append each server path component */
301 for (i
=0; (size
> 0) && (i
< (int)fsl
->nl_path
.np_compcount
); i
++) {
302 cnt
= snprintf(p
, size
, "/%s", fsl
->nl_path
.np_components
[i
]);
309 * NFS client connect socket upcall.
310 * (Used only during socket connect/search.)
313 nfs_connect_upcall(socket_t so
, void *arg
, __unused
int waitflag
)
315 struct nfs_socket
*nso
= arg
;
318 int error
= 0, recv
= 1;
320 if (nso
->nso_flags
& NSO_CONNECTING
) {
321 NFS_SOCK_DBG("nfs connect - socket %p upcall - connecting\n", nso
);
322 wakeup(nso
->nso_wake
);
326 lck_mtx_lock(&nso
->nso_lock
);
327 if ((nso
->nso_flags
& (NSO_UPCALL
|NSO_DISCONNECTING
|NSO_DEAD
)) || !(nso
->nso_flags
& NSO_PINGING
)) {
328 NFS_SOCK_DBG("nfs connect - socket %p upcall - nevermind\n", nso
);
329 lck_mtx_unlock(&nso
->nso_lock
);
332 NFS_SOCK_DBG("nfs connect - socket %p upcall\n", nso
);
333 nso
->nso_flags
|= NSO_UPCALL
;
335 /* loop while we make error-free progress */
336 while (!error
&& recv
) {
337 /* make sure we're still interested in this socket */
338 if (nso
->nso_flags
& (NSO_DISCONNECTING
|NSO_DEAD
))
340 lck_mtx_unlock(&nso
->nso_lock
);
342 if (nso
->nso_sotype
== SOCK_STREAM
) {
343 error
= nfs_rpc_record_read(so
, &nso
->nso_rrs
, MSG_DONTWAIT
, &recv
, &m
);
346 error
= sock_receivembuf(so
, NULL
, &m
, MSG_DONTWAIT
, &rcvlen
);
349 lck_mtx_lock(&nso
->nso_lock
);
351 /* match response with request */
352 struct nfsm_chain nmrep
;
353 uint32_t reply
= 0, rxid
= 0, verf_type
, verf_len
;
354 uint32_t reply_status
, rejected_status
, accepted_status
;
356 nfsm_chain_dissect_init(error
, &nmrep
, m
);
357 nfsm_chain_get_32(error
, &nmrep
, rxid
);
358 nfsm_chain_get_32(error
, &nmrep
, reply
);
359 if (!error
&& ((reply
!= RPC_REPLY
) || (rxid
!= nso
->nso_pingxid
)))
361 nfsm_chain_get_32(error
, &nmrep
, reply_status
);
362 if (!error
&& (reply_status
== RPC_MSGDENIED
)) {
363 nfsm_chain_get_32(error
, &nmrep
, rejected_status
);
365 error
= (rejected_status
== RPC_MISMATCH
) ? ERPCMISMATCH
: EACCES
;
367 nfsm_chain_get_32(error
, &nmrep
, verf_type
); /* verifier flavor */
368 nfsm_chain_get_32(error
, &nmrep
, verf_len
); /* verifier length */
371 nfsm_chain_adv(error
, &nmrep
, nfsm_rndup(verf_len
));
372 nfsm_chain_get_32(error
, &nmrep
, accepted_status
);
374 if ((accepted_status
== RPC_PROGMISMATCH
) && !nso
->nso_version
) {
375 uint32_t minvers
, maxvers
;
376 nfsm_chain_get_32(error
, &nmrep
, minvers
);
377 nfsm_chain_get_32(error
, &nmrep
, maxvers
);
379 if (nso
->nso_protocol
== PMAPPROG
) {
380 if ((minvers
> RPCBVERS4
) || (maxvers
< PMAPVERS
))
381 error
= EPROGMISMATCH
;
382 else if ((nso
->nso_saddr
->sa_family
== AF_INET
) &&
383 (PMAPVERS
>= minvers
) && (PMAPVERS
<= maxvers
))
384 nso
->nso_version
= PMAPVERS
;
385 else if (nso
->nso_saddr
->sa_family
== AF_INET6
) {
386 if ((RPCBVERS4
>= minvers
) && (RPCBVERS4
<= maxvers
))
387 nso
->nso_version
= RPCBVERS4
;
388 else if ((RPCBVERS3
>= minvers
) && (RPCBVERS3
<= maxvers
))
389 nso
->nso_version
= RPCBVERS3
;
391 } else if (nso
->nso_protocol
== NFS_PROG
) {
392 if ((minvers
> NFS_VER4
) || (maxvers
< NFS_VER2
))
393 error
= EPROGMISMATCH
;
394 else if ((NFS_VER3
>= minvers
) && (NFS_VER3
<= maxvers
))
395 nso
->nso_version
= NFS_VER3
;
396 else if ((NFS_VER2
>= minvers
) && (NFS_VER2
<= maxvers
))
397 nso
->nso_version
= NFS_VER2
;
398 else if ((NFS_VER4
>= minvers
) && (NFS_VER4
<= maxvers
))
399 nso
->nso_version
= NFS_VER4
;
401 if (!error
&& nso
->nso_version
)
402 accepted_status
= RPC_SUCCESS
;
405 switch (accepted_status
) {
409 case RPC_PROGUNAVAIL
:
410 error
= EPROGUNAVAIL
;
412 case RPC_PROGMISMATCH
:
413 error
= EPROGMISMATCH
;
415 case RPC_PROCUNAVAIL
:
416 error
= EPROCUNAVAIL
;
428 nso
->nso_flags
&= ~NSO_PINGING
;
430 nso
->nso_error
= error
;
431 nso
->nso_flags
|= NSO_DEAD
;
433 nso
->nso_flags
|= NSO_VERIFIED
;
436 /* wake up search thread */
437 wakeup(nso
->nso_wake
);
442 nso
->nso_flags
&= ~NSO_UPCALL
;
443 if ((error
!= EWOULDBLOCK
) && (error
|| !recv
)) {
444 /* problems with the socket... */
445 nso
->nso_error
= error
? error
: EPIPE
;
446 nso
->nso_flags
|= NSO_DEAD
;
447 wakeup(nso
->nso_wake
);
449 if (nso
->nso_flags
& NSO_DISCONNECTING
)
450 wakeup(&nso
->nso_flags
);
451 lck_mtx_unlock(&nso
->nso_lock
);
455 * Create/initialize an nfs_socket structure.
459 __unused
struct nfsmount
*nmp
,
466 struct nfs_socket
**nsop
)
468 struct nfs_socket
*nso
;
471 #ifdef NFS_SOCKET_DEBUGGING
472 char naddr
[MAX_IPv6_STR_LEN
];
475 if (sa
->sa_family
== AF_INET
)
476 sinaddr
= &((struct sockaddr_in
*)sa
)->sin_addr
;
478 sinaddr
= &((struct sockaddr_in6
*)sa
)->sin6_addr
;
479 if (inet_ntop(sa
->sa_family
, sinaddr
, naddr
, sizeof(naddr
)) != naddr
)
480 strlcpy(naddr
, "<unknown>", sizeof(naddr
));
482 char naddr
[1] = { 0 };
487 /* Create the socket. */
488 MALLOC(nso
, struct nfs_socket
*, sizeof(struct nfs_socket
), M_TEMP
, M_WAITOK
|M_ZERO
);
490 MALLOC(nso
->nso_saddr
, struct sockaddr
*, sa
->sa_len
, M_SONAME
, M_WAITOK
|M_ZERO
);
491 if (!nso
|| !nso
->nso_saddr
) {
496 lck_mtx_init(&nso
->nso_lock
, nfs_request_grp
, LCK_ATTR_NULL
);
497 nso
->nso_sotype
= sotype
;
498 if (nso
->nso_sotype
== SOCK_STREAM
)
499 nfs_rpc_record_state_init(&nso
->nso_rrs
);
501 nso
->nso_timestamp
= now
.tv_sec
;
502 bcopy(sa
, nso
->nso_saddr
, sa
->sa_len
);
503 if (sa
->sa_family
== AF_INET
)
504 ((struct sockaddr_in
*)nso
->nso_saddr
)->sin_port
= htons(port
);
505 else if (sa
->sa_family
== AF_INET6
)
506 ((struct sockaddr_in6
*)nso
->nso_saddr
)->sin6_port
= htons(port
);
507 nso
->nso_protocol
= protocol
;
508 nso
->nso_version
= vers
;
510 error
= sock_socket(sa
->sa_family
, nso
->nso_sotype
, 0, NULL
, NULL
, &nso
->nso_so
);
512 /* Some servers require that the client port be a reserved port number. */
513 if (!error
&& resvport
&& ((sa
->sa_family
== AF_INET
) || (sa
->sa_family
== AF_INET6
))) {
514 struct sockaddr_storage ss
;
515 int level
= (sa
->sa_family
== AF_INET
) ? IPPROTO_IP
: IPPROTO_IPV6
;
516 int optname
= (sa
->sa_family
== AF_INET
) ? IP_PORTRANGE
: IPV6_PORTRANGE
;
517 int portrange
= IP_PORTRANGE_LOW
;
519 error
= sock_setsockopt(nso
->nso_so
, level
, optname
, &portrange
, sizeof(portrange
));
520 if (!error
) { /* bind now to check for failure */
521 ss
.ss_len
= sa
->sa_len
;
522 ss
.ss_family
= sa
->sa_family
;
523 if (ss
.ss_family
== AF_INET
) {
524 ((struct sockaddr_in
*)&ss
)->sin_addr
.s_addr
= INADDR_ANY
;
525 ((struct sockaddr_in
*)&ss
)->sin_port
= htons(0);
526 } else if (ss
.ss_family
== AF_INET6
) {
527 ((struct sockaddr_in6
*)&ss
)->sin6_addr
= in6addr_any
;
528 ((struct sockaddr_in6
*)&ss
)->sin6_port
= htons(0);
533 error
= sock_bind(nso
->nso_so
, (struct sockaddr
*)&ss
);
538 NFS_SOCK_DBG("nfs connect %s error %d creating socket %p %s type %d%s port %d prot %d %d\n",
539 vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, error
, nso
, naddr
, sotype
,
540 resvport
? "r" : "", port
, protocol
, vers
);
541 nfs_socket_destroy(nso
);
543 NFS_SOCK_DBG("nfs connect %s created socket %p %s type %d%s port %d prot %d %d\n",
544 vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, nso
, naddr
,
545 sotype
, resvport
? "r" : "", port
, protocol
, vers
);
552 * Destroy an nfs_socket structure.
555 nfs_socket_destroy(struct nfs_socket
*nso
)
557 struct timespec ts
= { 4, 0 };
559 lck_mtx_lock(&nso
->nso_lock
);
560 nso
->nso_flags
|= NSO_DISCONNECTING
;
561 if (nso
->nso_flags
& NSO_UPCALL
) /* give upcall a chance to complete */
562 msleep(&nso
->nso_flags
, &nso
->nso_lock
, PZERO
-1, "nfswaitupcall", &ts
);
563 lck_mtx_unlock(&nso
->nso_lock
);
564 sock_shutdown(nso
->nso_so
, SHUT_RDWR
);
565 sock_close(nso
->nso_so
);
566 if (nso
->nso_sotype
== SOCK_STREAM
)
567 nfs_rpc_record_state_cleanup(&nso
->nso_rrs
);
568 lck_mtx_destroy(&nso
->nso_lock
, nfs_request_grp
);
570 FREE(nso
->nso_saddr
, M_SONAME
);
572 FREE(nso
->nso_saddr2
, M_SONAME
);
573 NFS_SOCK_DBG("nfs connect - socket %p destroyed\n", nso
);
578 * Set common socket options on an nfs_socket.
581 nfs_socket_options(struct nfsmount
*nmp
, struct nfs_socket
*nso
)
584 * Set socket send/receive timeouts
585 * - Receive timeout shouldn't matter because most receives are performed
586 * in the socket upcall non-blocking.
587 * - Send timeout should allow us to react to a blocked socket.
588 * Soft mounts will want to abort sooner.
590 struct timeval timeo
;
594 timeo
.tv_sec
= (NMFLAG(nmp
, SOFT
) || nfs_can_squish(nmp
)) ? 5 : 60;
595 sock_setsockopt(nso
->nso_so
, SOL_SOCKET
, SO_RCVTIMEO
, &timeo
, sizeof(timeo
));
596 sock_setsockopt(nso
->nso_so
, SOL_SOCKET
, SO_SNDTIMEO
, &timeo
, sizeof(timeo
));
597 if (nso
->nso_sotype
== SOCK_STREAM
) {
598 /* Assume that SOCK_STREAM always requires a connection */
599 sock_setsockopt(nso
->nso_so
, SOL_SOCKET
, SO_KEEPALIVE
, &on
, sizeof(on
));
600 /* set nodelay for TCP */
601 sock_gettype(nso
->nso_so
, NULL
, NULL
, &proto
);
602 if (proto
== IPPROTO_TCP
)
603 sock_setsockopt(nso
->nso_so
, IPPROTO_TCP
, TCP_NODELAY
, &on
, sizeof(on
));
605 if (nso
->nso_sotype
== SOCK_DGRAM
) { /* set socket buffer sizes for UDP */
606 int reserve
= NFS_UDPSOCKBUF
;
607 sock_setsockopt(nso
->nso_so
, SOL_SOCKET
, SO_SNDBUF
, &reserve
, sizeof(reserve
));
608 sock_setsockopt(nso
->nso_so
, SOL_SOCKET
, SO_RCVBUF
, &reserve
, sizeof(reserve
));
610 /* set SO_NOADDRERR to detect network changes ASAP */
611 sock_setsockopt(nso
->nso_so
, SOL_SOCKET
, SO_NOADDRERR
, &on
, sizeof(on
));
612 /* just playin' it safe with upcalls */
613 sock_setsockopt(nso
->nso_so
, SOL_SOCKET
, SO_UPCALLCLOSEWAIT
, &on
, sizeof(on
));
614 /* socket should be interruptible if the mount is */
615 if (!NMFLAG(nmp
, INTR
))
616 sock_nointerrupt(nso
->nso_so
, 1);
620 * Release resources held in an nfs_socket_search.
623 nfs_socket_search_cleanup(struct nfs_socket_search
*nss
)
625 struct nfs_socket
*nso
, *nsonext
;
627 TAILQ_FOREACH_SAFE(nso
, &nss
->nss_socklist
, nso_link
, nsonext
) {
628 TAILQ_REMOVE(&nss
->nss_socklist
, nso
, nso_link
);
630 nfs_socket_destroy(nso
);
633 nfs_socket_destroy(nss
->nss_sock
);
634 nss
->nss_sock
= NULL
;
639 * Prefer returning certain errors over others.
640 * This function returns a ranking of the given error.
643 nfs_connect_error_class(int error
)
678 * Make sure a socket search returns the best error.
681 nfs_socket_search_update_error(struct nfs_socket_search
*nss
, int error
)
683 if (nfs_connect_error_class(error
) >= nfs_connect_error_class(nss
->nss_error
))
684 nss
->nss_error
= error
;
687 /* nfs_connect_search_new_socket:
688 * Given a socket search structure for an nfs mount try to find a new socket from the set of addresses specified
691 * nss_last is set to -1 at initialization to indicate the first time. Its set to -2 if address was found but
692 * could not be used or if a socket timed out.
695 nfs_connect_search_new_socket(struct nfsmount
*nmp
, struct nfs_socket_search
*nss
, struct timeval
*now
)
697 struct nfs_fs_location
*fsl
;
698 struct nfs_fs_server
*fss
;
699 struct sockaddr_storage ss
;
700 struct nfs_socket
*nso
;
705 NFS_SOCK_DBG("nfs connect %s nss_addrcnt = %d\n",
706 vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, nss
->nss_addrcnt
);
709 * while there are addresses and:
710 * we have no sockets or
711 * the last address failed and did not produce a socket (nss_last < 0) or
712 * Its been a while (2 seconds) and we have less than the max number of concurrent sockets to search (4)
713 * then attempt to create a socket with the current address.
715 while (nss
->nss_addrcnt
> 0 && ((nss
->nss_last
< 0) || (nss
->nss_sockcnt
== 0) ||
716 ((nss
->nss_sockcnt
< 4) && (now
->tv_sec
>= (nss
->nss_last
+ 2))))) {
717 if (nmp
->nm_sockflags
& NMSOCK_UNMOUNT
)
719 /* Can we convert the address to a sockaddr? */
720 fsl
= nmp
->nm_locations
.nl_locations
[nss
->nss_nextloc
.nli_loc
];
721 fss
= fsl
->nl_servers
[nss
->nss_nextloc
.nli_serv
];
722 addrstr
= fss
->ns_addresses
[nss
->nss_nextloc
.nli_addr
];
723 if (!nfs_uaddr2sockaddr(addrstr
, (struct sockaddr
*)&ss
)) {
724 nfs_location_next(&nmp
->nm_locations
, &nss
->nss_nextloc
);
725 nss
->nss_addrcnt
-= 1;
729 /* Check that socket family is acceptable. */
730 if (nmp
->nm_sofamily
&& (ss
.ss_family
!= nmp
->nm_sofamily
)) {
731 nfs_location_next(&nmp
->nm_locations
, &nss
->nss_nextloc
);
732 nss
->nss_addrcnt
-= 1;
737 /* Create the socket. */
738 error
= nfs_socket_create(nmp
, (struct sockaddr
*)&ss
, nss
->nss_sotype
,
739 nss
->nss_port
, nss
->nss_protocol
, nss
->nss_version
,
740 ((nss
->nss_protocol
== NFS_PROG
) && NMFLAG(nmp
, RESVPORT
)), &nso
);
744 nso
->nso_location
= nss
->nss_nextloc
;
746 error
= sock_setupcall(nso
->nso_so
, nfs_connect_upcall
, nso
);
748 lck_mtx_lock(&nso
->nso_lock
);
749 nso
->nso_error
= error
;
750 nso
->nso_flags
|= NSO_DEAD
;
751 lck_mtx_unlock(&nso
->nso_lock
);
754 TAILQ_INSERT_TAIL(&nss
->nss_socklist
, nso
, nso_link
);
756 nfs_location_next(&nmp
->nm_locations
, &nss
->nss_nextloc
);
757 nss
->nss_addrcnt
-= 1;
759 nss
->nss_last
= now
->tv_sec
;
762 if (nss
->nss_addrcnt
== 0 && nss
->nss_last
< 0)
763 nss
->nss_last
= now
->tv_sec
;
769 * nfs_connect_search_socket_connect: Connect an nfs socket nso for nfsmount nmp.
770 * If successful set the socket options for the socket as require from the mount.
772 * Assumes: nso->nso_lock is held on entry and return.
775 nfs_connect_search_socket_connect(struct nfsmount
*nmp
, struct nfs_socket
*nso
, int verbose
)
779 if ((nso
->nso_sotype
!= SOCK_STREAM
) && NMFLAG(nmp
, NOCONNECT
)) {
780 /* no connection needed, just say it's already connected */
781 NFS_SOCK_DBG("nfs connect %s UDP socket %p noconnect\n",
782 vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, nso
);
783 nso
->nso_flags
|= NSO_CONNECTED
;
784 nfs_socket_options(nmp
, nso
);
785 return (1); /* Socket is connected and setup */
786 } else if (!(nso
->nso_flags
& NSO_CONNECTING
)) {
787 /* initiate the connection */
788 nso
->nso_flags
|= NSO_CONNECTING
;
789 lck_mtx_unlock(&nso
->nso_lock
);
790 NFS_SOCK_DBG("nfs connect %s connecting socket %p\n",
791 vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, nso
);
792 error
= sock_connect(nso
->nso_so
, nso
->nso_saddr
, MSG_DONTWAIT
);
793 lck_mtx_lock(&nso
->nso_lock
);
794 if (error
&& (error
!= EINPROGRESS
)) {
795 nso
->nso_error
= error
;
796 nso
->nso_flags
|= NSO_DEAD
;
800 if (nso
->nso_flags
& NSO_CONNECTING
) {
801 /* check the connection */
802 if (sock_isconnected(nso
->nso_so
)) {
803 NFS_SOCK_DBG("nfs connect %s socket %p is connected\n",
804 vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, nso
);
805 nso
->nso_flags
&= ~NSO_CONNECTING
;
806 nso
->nso_flags
|= NSO_CONNECTED
;
807 nfs_socket_options(nmp
, nso
);
808 return (1); /* Socket is connected and setup */
810 int optlen
= sizeof(error
);
812 sock_getsockopt(nso
->nso_so
, SOL_SOCKET
, SO_ERROR
, &error
, &optlen
);
813 if (error
) { /* we got an error on the socket */
814 NFS_SOCK_DBG("nfs connect %s socket %p connection error %d\n",
815 vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, nso
, error
);
817 printf("nfs connect socket error %d for %s\n",
818 error
, vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
);
819 nso
->nso_error
= error
;
820 nso
->nso_flags
|= NSO_DEAD
;
826 return (0); /* Waiting to be connected */
830 * nfs_connect_search_ping: Send a null proc on the nso socket.
833 nfs_connect_search_ping(struct nfsmount
*nmp
, struct nfs_socket
*nso
, struct timeval
*now
)
835 /* initiate a NULL RPC request */
836 uint64_t xid
= nso
->nso_pingxid
;
837 mbuf_t m
, mreq
= NULL
;
839 size_t reqlen
, sentlen
;
840 uint32_t vers
= nso
->nso_version
;
844 if (nso
->nso_protocol
== PMAPPROG
)
845 vers
= (nso
->nso_saddr
->sa_family
== AF_INET
) ? PMAPVERS
: RPCBVERS4
;
846 else if (nso
->nso_protocol
== NFS_PROG
)
849 lck_mtx_unlock(&nso
->nso_lock
);
850 error
= nfsm_rpchead2(nmp
, nso
->nso_sotype
, nso
->nso_protocol
, vers
, 0, RPCAUTH_SYS
,
851 vfs_context_ucred(vfs_context_kernel()), NULL
, NULL
, &xid
, &mreq
);
852 lck_mtx_lock(&nso
->nso_lock
);
854 nso
->nso_flags
|= NSO_PINGING
;
855 nso
->nso_pingxid
= R_XID32(xid
);
856 nso
->nso_reqtimestamp
= now
->tv_sec
;
857 bzero(&msg
, sizeof(msg
));
858 if ((nso
->nso_sotype
!= SOCK_STREAM
) && !sock_isconnected(nso
->nso_so
)) {
859 msg
.msg_name
= nso
->nso_saddr
;
860 msg
.msg_namelen
= nso
->nso_saddr
->sa_len
;
862 for (reqlen
=0, m
=mreq
; m
; m
= mbuf_next(m
))
863 reqlen
+= mbuf_len(m
);
864 lck_mtx_unlock(&nso
->nso_lock
);
865 error
= sock_sendmbuf(nso
->nso_so
, &msg
, mreq
, 0, &sentlen
);
866 NFS_SOCK_DBG("nfs connect %s verifying socket %p send rv %d\n",
867 vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, nso
, error
);
868 lck_mtx_lock(&nso
->nso_lock
);
869 if (!error
&& (sentlen
!= reqlen
))
873 nso
->nso_error
= error
;
874 nso
->nso_flags
|= NSO_DEAD
;
882 * nfs_connect_search_socket_found: Take the found socket of the socket search list and assign it to the searched socket.
883 * Set the nfs socket protocol and version if needed.
886 nfs_connect_search_socket_found(struct nfsmount
*nmp __unused
, struct nfs_socket_search
*nss
, struct nfs_socket
*nso
)
888 NFS_SOCK_DBG("nfs connect %s socket %p verified\n",
889 vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, nso
);
890 if (!nso
->nso_version
) {
891 /* If the version isn't set, the default must have worked. */
892 if (nso
->nso_protocol
== PMAPPROG
)
893 nso
->nso_version
= (nso
->nso_saddr
->sa_family
== AF_INET
) ? PMAPVERS
: RPCBVERS4
;
894 if (nso
->nso_protocol
== NFS_PROG
)
895 nso
->nso_version
= NFS_VER3
;
897 TAILQ_REMOVE(&nss
->nss_socklist
, nso
, nso_link
);
903 * nfs_connect_search_socket_reap: For each socket in the search list mark any timed out socket as dead and remove from
904 * the list. Dead socket are then destroyed.
907 nfs_connect_search_socket_reap(struct nfsmount
*nmp __unused
, struct nfs_socket_search
*nss
, struct timeval
*now
)
909 struct nfs_socket
*nso
, *nsonext
;
911 TAILQ_FOREACH_SAFE(nso
, &nss
->nss_socklist
, nso_link
, nsonext
) {
912 lck_mtx_lock(&nso
->nso_lock
);
913 if (now
->tv_sec
>= (nso
->nso_timestamp
+ nss
->nss_timeo
)) {
915 NFS_SOCK_DBG("nfs connect %s socket %p timed out\n",
916 vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, nso
);
917 nso
->nso_error
= ETIMEDOUT
;
918 nso
->nso_flags
|= NSO_DEAD
;
920 if (!(nso
->nso_flags
& NSO_DEAD
)) {
921 lck_mtx_unlock(&nso
->nso_lock
);
924 lck_mtx_unlock(&nso
->nso_lock
);
925 NFS_SOCK_DBG("nfs connect %s reaping socket %p %d\n",
926 vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, nso
, nso
->nso_error
);
927 nfs_socket_search_update_error(nss
, nso
->nso_error
);
928 TAILQ_REMOVE(&nss
->nss_socklist
, nso
, nso_link
);
930 nfs_socket_destroy(nso
);
931 /* If there are more sockets to try, force the starting of another socket */
932 if (nss
->nss_addrcnt
> 0)
938 * nfs_connect_search_check: Check on the status of search and wait for replies if needed.
941 nfs_connect_search_check(struct nfsmount
*nmp
, struct nfs_socket_search
*nss
, struct timeval
*now
)
945 /* log a warning if connect is taking a while */
946 if (((now
->tv_sec
- nss
->nss_timestamp
) >= 8) && ((nss
->nss_flags
& (NSS_VERBOSE
|NSS_WARNED
)) == NSS_VERBOSE
)) {
947 printf("nfs_connect: socket connect taking a while for %s\n", vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
);
948 nss
->nss_flags
|= NSS_WARNED
;
950 if (nmp
->nm_sockflags
& NMSOCK_UNMOUNT
)
952 if ((error
= nfs_sigintr(nmp
, NULL
, current_thread(), 0)))
955 /* If we were succesfull at sending a ping, wait up to a second for a reply */
956 if (nss
->nss_last
>= 0)
957 tsleep(nss
, PSOCK
, "nfs_connect_search_wait", hz
);
964 * Continue the socket search until we have something to report.
967 nfs_connect_search_loop(struct nfsmount
*nmp
, struct nfs_socket_search
*nss
)
969 struct nfs_socket
*nso
;
972 int verbose
= (nss
->nss_flags
& NSS_VERBOSE
);
976 NFS_SOCK_DBG("nfs connect %s search %ld\n", vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, now
.tv_sec
);
978 /* add a new socket to the socket list if needed and available */
979 error
= nfs_connect_search_new_socket(nmp
, nss
, &now
);
981 NFS_SOCK_DBG("nfs connect returned %d\n", error
);
985 /* check each active socket on the list and try to push it along */
986 TAILQ_FOREACH(nso
, &nss
->nss_socklist
, nso_link
) {
987 lck_mtx_lock(&nso
->nso_lock
);
989 /* If not connected connect it */
990 if (!(nso
->nso_flags
& NSO_CONNECTED
)) {
991 if (!nfs_connect_search_socket_connect(nmp
, nso
, verbose
)) {
992 lck_mtx_unlock(&nso
->nso_lock
);
997 /* If the socket hasn't been verified or in a ping, ping it. We also handle UDP retransmits */
998 if (!(nso
->nso_flags
& (NSO_PINGING
|NSO_VERIFIED
)) ||
999 ((nso
->nso_sotype
== SOCK_DGRAM
) && (now
.tv_sec
>= nso
->nso_reqtimestamp
+2))) {
1000 if (!nfs_connect_search_ping(nmp
, nso
, &now
)) {
1001 lck_mtx_unlock(&nso
->nso_lock
);
1006 /* Has the socket been verified by the up call routine? */
1007 if (nso
->nso_flags
& NSO_VERIFIED
) {
1008 /* WOOHOO!! This socket looks good! */
1009 nfs_connect_search_socket_found(nmp
, nss
, nso
);
1010 lck_mtx_unlock(&nso
->nso_lock
);
1013 lck_mtx_unlock(&nso
->nso_lock
);
1016 /* Check for timed out sockets and mark as dead and then remove all dead sockets. */
1017 nfs_connect_search_socket_reap(nmp
, nss
, &now
);
1020 * Keep looping if we haven't found a socket yet and we have more
1021 * sockets to (continue to) try.
1024 if (!nss
->nss_sock
&& (!TAILQ_EMPTY(&nss
->nss_socklist
) || nss
->nss_addrcnt
)) {
1025 error
= nfs_connect_search_check(nmp
, nss
, &now
);
1030 NFS_SOCK_DBG("nfs connect %s returning %d\n", vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, error
);
1035 * Initialize a new NFS connection.
1037 * Search for a location to connect a socket to and initialize the connection.
1039 * An NFS mount may have multiple locations/servers/addresses available.
1040 * We attempt to connect to each one asynchronously and will start
1041 * several sockets in parallel if other locations are slow to answer.
1042 * We'll use the first NFS socket we can successfully set up.
1044 * The search may involve contacting the portmapper service first.
1046 * A mount's initial connection may require negotiating some parameters such
1047 * as socket type and NFS version.
1050 nfs_connect(struct nfsmount
*nmp
, int verbose
, int timeo
)
1052 struct nfs_socket_search nss
;
1053 struct nfs_socket
*nso
, *nsonfs
;
1054 struct sockaddr_storage ss
;
1055 struct sockaddr
*saddr
, *oldsaddr
;
1057 struct timeval now
, start
;
1058 int error
, savederror
, nfsvers
;
1059 uint8_t sotype
= nmp
->nm_sotype
? nmp
->nm_sotype
: SOCK_STREAM
;
1060 fhandle_t
*fh
= NULL
;
1065 /* paranoia... check that we have at least one address in the locations */
1067 for (loc
=0; loc
< nmp
->nm_locations
.nl_numlocs
; loc
++) {
1068 for (serv
=0; serv
< nmp
->nm_locations
.nl_locations
[loc
]->nl_servcount
; serv
++) {
1069 addrtotal
+= nmp
->nm_locations
.nl_locations
[loc
]->nl_servers
[serv
]->ns_addrcount
;
1070 if (nmp
->nm_locations
.nl_locations
[loc
]->nl_servers
[serv
]->ns_addrcount
== 0)
1071 NFS_SOCK_DBG("nfs connect %s search, server %s has no addresses\n",
1072 vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
,
1073 nmp
->nm_locations
.nl_locations
[loc
]->nl_servers
[serv
]->ns_name
);
1077 if (addrtotal
== 0) {
1078 NFS_SOCK_DBG("nfs connect %s search failed, no addresses\n",
1079 vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
);
1082 NFS_SOCK_DBG("nfs connect %s has %d addresses\n",
1083 vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, addrtotal
);
1085 lck_mtx_lock(&nmp
->nm_lock
);
1086 nmp
->nm_sockflags
|= NMSOCK_CONNECTING
;
1088 lck_mtx_unlock(&nmp
->nm_lock
);
1089 microuptime(&start
);
1090 savederror
= error
= 0;
1093 /* initialize socket search state */
1094 bzero(&nss
, sizeof(nss
));
1095 nss
.nss_addrcnt
= addrtotal
;
1096 nss
.nss_error
= savederror
;
1097 TAILQ_INIT(&nss
.nss_socklist
);
1098 nss
.nss_sotype
= sotype
;
1099 nss
.nss_startloc
= nmp
->nm_locations
.nl_current
;
1100 nss
.nss_timestamp
= start
.tv_sec
;
1101 nss
.nss_timeo
= timeo
;
1103 nss
.nss_flags
|= NSS_VERBOSE
;
1105 /* First time connecting, we may need to negotiate some things */
1106 if (!(nmp
->nm_sockflags
& NMSOCK_HASCONNECTED
)) {
1107 if (!nmp
->nm_vers
) {
1108 /* No NFS version specified... */
1109 if (!nmp
->nm_nfsport
|| (!NM_OMATTR_GIVEN(nmp
, FH
) && !nmp
->nm_mountport
)) {
1110 /* ...connect to portmapper first if we (may) need any ports. */
1111 nss
.nss_port
= PMAPPORT
;
1112 nss
.nss_protocol
= PMAPPROG
;
1113 nss
.nss_version
= 0;
1115 /* ...connect to NFS port first. */
1116 nss
.nss_port
= nmp
->nm_nfsport
;
1117 nss
.nss_protocol
= NFS_PROG
;
1118 nss
.nss_version
= 0;
1120 } else if (nmp
->nm_vers
>= NFS_VER4
) {
1121 /* For NFSv4, we use the given (or default) port. */
1122 nss
.nss_port
= nmp
->nm_nfsport
? nmp
->nm_nfsport
: NFS_PORT
;
1123 nss
.nss_protocol
= NFS_PROG
;
1124 nss
.nss_version
= 4;
1126 /* For NFSv3/v2... */
1127 if (!nmp
->nm_nfsport
|| (!NM_OMATTR_GIVEN(nmp
, FH
) && !nmp
->nm_mountport
)) {
1128 /* ...connect to portmapper first if we need any ports. */
1129 nss
.nss_port
= PMAPPORT
;
1130 nss
.nss_protocol
= PMAPPROG
;
1131 nss
.nss_version
= 0;
1133 /* ...connect to NFS port first. */
1134 nss
.nss_port
= nmp
->nm_nfsport
;
1135 nss
.nss_protocol
= NFS_PROG
;
1136 nss
.nss_version
= nmp
->nm_vers
;
1139 NFS_SOCK_DBG("nfs connect first %s, so type %d port %d prot %d %d\n",
1140 vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, nss
.nss_sotype
, nss
.nss_port
,
1141 nss
.nss_protocol
, nss
.nss_version
);
1143 /* we've connected before, just connect to NFS port */
1144 if (!nmp
->nm_nfsport
) {
1145 /* need to ask portmapper which port that would be */
1146 nss
.nss_port
= PMAPPORT
;
1147 nss
.nss_protocol
= PMAPPROG
;
1148 nss
.nss_version
= 0;
1150 nss
.nss_port
= nmp
->nm_nfsport
;
1151 nss
.nss_protocol
= NFS_PROG
;
1152 nss
.nss_version
= nmp
->nm_vers
;
1154 NFS_SOCK_DBG("nfs connect %s, so type %d port %d prot %d %d\n",
1155 vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, nss
.nss_sotype
, nss
.nss_port
,
1156 nss
.nss_protocol
, nss
.nss_version
);
1159 /* Set next location to first valid location. */
1160 /* If start location is invalid, find next location. */
1161 nss
.nss_nextloc
= nss
.nss_startloc
;
1162 if ((nss
.nss_nextloc
.nli_serv
>= nmp
->nm_locations
.nl_locations
[nss
.nss_nextloc
.nli_loc
]->nl_servcount
) ||
1163 (nss
.nss_nextloc
.nli_addr
>= nmp
->nm_locations
.nl_locations
[nss
.nss_nextloc
.nli_loc
]->nl_servers
[nss
.nss_nextloc
.nli_serv
]->ns_addrcount
)) {
1164 nfs_location_next(&nmp
->nm_locations
, &nss
.nss_nextloc
);
1165 if (!nfs_location_index_cmp(&nss
.nss_nextloc
, &nss
.nss_startloc
)) {
1166 NFS_SOCK_DBG("nfs connect %s search failed, couldn't find a valid location index\n",
1167 vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
);
1175 error
= nfs_connect_search_loop(nmp
, &nss
);
1176 if (error
|| !nss
.nss_sock
) {
1178 nfs_socket_search_cleanup(&nss
);
1179 if (!error
&& (nss
.nss_sotype
== SOCK_STREAM
) && !nmp
->nm_sotype
&& (nmp
->nm_vers
< NFS_VER4
)) {
1181 sotype
= SOCK_DGRAM
;
1182 savederror
= nss
.nss_error
;
1183 NFS_SOCK_DBG("nfs connect %s TCP failed %d %d, trying UDP\n",
1184 vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, error
, nss
.nss_error
);
1188 error
= nss
.nss_error
? nss
.nss_error
: ETIMEDOUT
;
1189 lck_mtx_lock(&nmp
->nm_lock
);
1190 nmp
->nm_sockflags
&= ~NMSOCK_CONNECTING
;
1192 lck_mtx_unlock(&nmp
->nm_lock
);
1193 if (nss
.nss_flags
& NSS_WARNED
)
1194 log(LOG_INFO
, "nfs_connect: socket connect aborted for %s\n",
1195 vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
);
1199 FREE_ZONE(path
, MAXPATHLEN
, M_NAMEI
);
1200 NFS_SOCK_DBG("nfs connect %s search failed, returning %d\n",
1201 vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, error
);
1205 /* try to use nss_sock */
1207 nss
.nss_sock
= NULL
;
1209 /* We may be speaking to portmap first... to determine port(s). */
1210 if (nso
->nso_saddr
->sa_family
== AF_INET
)
1211 port
= ntohs(((struct sockaddr_in
*)nso
->nso_saddr
)->sin_port
);
1213 port
= ntohs(((struct sockaddr_in6
*)nso
->nso_saddr
)->sin6_port
);
1214 if (port
== PMAPPORT
) {
1215 /* Use this portmapper port to get the port #s we need. */
1216 NFS_SOCK_DBG("nfs connect %s got portmapper socket %p\n",
1217 vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, nso
);
1219 /* remove the connect upcall so nfs_portmap_lookup() can use this socket */
1220 sock_setupcall(nso
->nso_so
, NULL
, NULL
);
1222 /* Set up socket address and port for NFS socket. */
1223 bcopy(nso
->nso_saddr
, &ss
, nso
->nso_saddr
->sa_len
);
1225 /* If NFS version not set, try NFSv3 then NFSv2. */
1226 nfsvers
= nmp
->nm_vers
? nmp
->nm_vers
: NFS_VER3
;
1228 if (!(port
= nmp
->nm_nfsport
)) {
1229 if (ss
.ss_family
== AF_INET
)
1230 ((struct sockaddr_in
*)&ss
)->sin_port
= htons(0);
1231 else if (ss
.ss_family
== AF_INET6
)
1232 ((struct sockaddr_in6
*)&ss
)->sin6_port
= htons(0);
1233 error
= nfs_portmap_lookup(nmp
, vfs_context_current(), (struct sockaddr
*)&ss
,
1234 nso
->nso_so
, NFS_PROG
, nfsvers
,
1235 (nso
->nso_sotype
== SOCK_DGRAM
) ? IPPROTO_UDP
: IPPROTO_TCP
, timeo
);
1237 if (ss
.ss_family
== AF_INET
)
1238 port
= ntohs(((struct sockaddr_in
*)&ss
)->sin_port
);
1239 else if (ss
.ss_family
== AF_INET6
)
1240 port
= ntohs(((struct sockaddr_in6
*)&ss
)->sin6_port
);
1242 error
= EPROGUNAVAIL
;
1244 if (error
&& !nmp
->nm_vers
) {
1246 error
= nfs_portmap_lookup(nmp
, vfs_context_current(), (struct sockaddr
*)&ss
,
1247 nso
->nso_so
, NFS_PROG
, nfsvers
,
1248 (nso
->nso_sotype
== SOCK_DGRAM
) ? IPPROTO_UDP
: IPPROTO_TCP
, timeo
);
1250 if (ss
.ss_family
== AF_INET
)
1251 port
= ntohs(((struct sockaddr_in
*)&ss
)->sin_port
);
1252 else if (ss
.ss_family
== AF_INET6
)
1253 port
= ntohs(((struct sockaddr_in6
*)&ss
)->sin6_port
);
1255 error
= EPROGUNAVAIL
;
1259 nfs_socket_search_update_error(&nss
, error
);
1260 nfs_socket_destroy(nso
);
1264 /* Create NFS protocol socket and add it to the list of sockets. */
1265 error
= nfs_socket_create(nmp
, (struct sockaddr
*)&ss
, nso
->nso_sotype
, port
,
1266 NFS_PROG
, nfsvers
, NMFLAG(nmp
, RESVPORT
), &nsonfs
);
1268 nfs_socket_search_update_error(&nss
, error
);
1269 nfs_socket_destroy(nso
);
1272 nsonfs
->nso_location
= nso
->nso_location
;
1273 nsonfs
->nso_wake
= &nss
;
1274 error
= sock_setupcall(nsonfs
->nso_so
, nfs_connect_upcall
, nsonfs
);
1276 nfs_socket_search_update_error(&nss
, error
);
1277 nfs_socket_destroy(nsonfs
);
1278 nfs_socket_destroy(nso
);
1281 TAILQ_INSERT_TAIL(&nss
.nss_socklist
, nsonfs
, nso_link
);
1283 if ((nfsvers
< NFS_VER4
) && !(nmp
->nm_sockflags
& NMSOCK_HASCONNECTED
) && !NM_OMATTR_GIVEN(nmp
, FH
)) {
1284 /* Set up socket address and port for MOUNT socket. */
1286 bcopy(nso
->nso_saddr
, &ss
, nso
->nso_saddr
->sa_len
);
1287 port
= nmp
->nm_mountport
;
1288 if (ss
.ss_family
== AF_INET
)
1289 ((struct sockaddr_in
*)&ss
)->sin_port
= htons(port
);
1290 else if (ss
.ss_family
== AF_INET6
)
1291 ((struct sockaddr_in6
*)&ss
)->sin6_port
= htons(port
);
1293 /* Get port/sockaddr for MOUNT version corresponding to NFS version. */
1294 /* If NFS version is unknown, optimistically choose for NFSv3. */
1295 int mntvers
= (nfsvers
== NFS_VER2
) ? RPCMNT_VER1
: RPCMNT_VER3
;
1296 int mntproto
= (NM_OMFLAG(nmp
, MNTUDP
) || (nso
->nso_sotype
== SOCK_DGRAM
)) ? IPPROTO_UDP
: IPPROTO_TCP
;
1297 error
= nfs_portmap_lookup(nmp
, vfs_context_current(), (struct sockaddr
*)&ss
,
1298 nso
->nso_so
, RPCPROG_MNT
, mntvers
, mntproto
, timeo
);
1301 if (ss
.ss_family
== AF_INET
)
1302 port
= ntohs(((struct sockaddr_in
*)&ss
)->sin_port
);
1303 else if (ss
.ss_family
== AF_INET6
)
1304 port
= ntohs(((struct sockaddr_in6
*)&ss
)->sin6_port
);
1306 error
= EPROGUNAVAIL
;
1308 /* create sockaddr for MOUNT */
1310 MALLOC(nsonfs
->nso_saddr2
, struct sockaddr
*, ss
.ss_len
, M_SONAME
, M_WAITOK
|M_ZERO
);
1311 if (!error
&& !nsonfs
->nso_saddr2
)
1314 bcopy(&ss
, nsonfs
->nso_saddr2
, ss
.ss_len
);
1316 lck_mtx_lock(&nsonfs
->nso_lock
);
1317 nsonfs
->nso_error
= error
;
1318 nsonfs
->nso_flags
|= NSO_DEAD
;
1319 lck_mtx_unlock(&nsonfs
->nso_lock
);
1322 nfs_socket_destroy(nso
);
1326 /* nso is an NFS socket */
1327 NFS_SOCK_DBG("nfs connect %s got NFS socket %p\n", vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, nso
);
1329 /* If NFS version wasn't specified, it was determined during the connect. */
1330 nfsvers
= nmp
->nm_vers
? nmp
->nm_vers
: (int)nso
->nso_version
;
1332 /* Perform MOUNT call for initial NFSv2/v3 connection/mount. */
1333 if ((nfsvers
< NFS_VER4
) && !(nmp
->nm_sockflags
& NMSOCK_HASCONNECTED
) && !NM_OMATTR_GIVEN(nmp
, FH
)) {
1335 saddr
= nso
->nso_saddr2
;
1337 /* Need sockaddr for MOUNT port */
1338 bcopy(nso
->nso_saddr
, &ss
, nso
->nso_saddr
->sa_len
);
1339 port
= nmp
->nm_mountport
;
1340 if (ss
.ss_family
== AF_INET
)
1341 ((struct sockaddr_in
*)&ss
)->sin_port
= htons(port
);
1342 else if (ss
.ss_family
== AF_INET6
)
1343 ((struct sockaddr_in6
*)&ss
)->sin6_port
= htons(port
);
1345 /* Get port/sockaddr for MOUNT version corresponding to NFS version. */
1346 int mntvers
= (nfsvers
== NFS_VER2
) ? RPCMNT_VER1
: RPCMNT_VER3
;
1347 int mntproto
= (NM_OMFLAG(nmp
, MNTUDP
) || (nso
->nso_sotype
== SOCK_DGRAM
)) ? IPPROTO_UDP
: IPPROTO_TCP
;
1348 error
= nfs_portmap_lookup(nmp
, vfs_context_current(), (struct sockaddr
*)&ss
,
1349 NULL
, RPCPROG_MNT
, mntvers
, mntproto
, timeo
);
1350 if (ss
.ss_family
== AF_INET
)
1351 port
= ntohs(((struct sockaddr_in
*)&ss
)->sin_port
);
1352 else if (ss
.ss_family
== AF_INET6
)
1353 port
= ntohs(((struct sockaddr_in6
*)&ss
)->sin6_port
);
1357 saddr
= (struct sockaddr
*)&ss
;
1359 error
= EPROGUNAVAIL
;
1363 MALLOC(fh
, fhandle_t
*, sizeof(fhandle_t
), M_TEMP
, M_WAITOK
|M_ZERO
);
1365 MALLOC_ZONE(path
, char *, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
1366 if (!saddr
|| !fh
|| !path
) {
1372 FREE_ZONE(path
, MAXPATHLEN
, M_NAMEI
);
1375 nfs_socket_search_update_error(&nss
, error
);
1376 nfs_socket_destroy(nso
);
1379 nfs_location_mntfromname(&nmp
->nm_locations
, nso
->nso_location
, path
, MAXPATHLEN
, 1);
1380 error
= nfs3_mount_rpc(nmp
, saddr
, nso
->nso_sotype
, nfsvers
,
1381 path
, vfs_context_current(), timeo
, fh
, &nmp
->nm_servsec
);
1382 NFS_SOCK_DBG("nfs connect %s socket %p mount %d\n",
1383 vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, nso
, error
);
1385 /* Make sure we can agree on a security flavor. */
1386 int o
, s
; /* indices into mount option and server security flavor lists */
1389 if ((nfsvers
== NFS_VER3
) && !nmp
->nm_servsec
.count
) {
1390 /* Some servers return an empty list to indicate RPCAUTH_SYS? */
1391 nmp
->nm_servsec
.count
= 1;
1392 nmp
->nm_servsec
.flavors
[0] = RPCAUTH_SYS
;
1394 if (nmp
->nm_sec
.count
) {
1395 /* Choose the first flavor in our list that the server supports. */
1396 if (!nmp
->nm_servsec
.count
) {
1397 /* we don't know what the server supports, just use our first choice */
1398 nmp
->nm_auth
= nmp
->nm_sec
.flavors
[0];
1401 for (o
=0; !found
&& (o
< nmp
->nm_sec
.count
); o
++)
1402 for (s
=0; !found
&& (s
< nmp
->nm_servsec
.count
); s
++)
1403 if (nmp
->nm_sec
.flavors
[o
] == nmp
->nm_servsec
.flavors
[s
]) {
1404 nmp
->nm_auth
= nmp
->nm_sec
.flavors
[o
];
1408 /* Choose the first one we support from the server's list. */
1409 if (!nmp
->nm_servsec
.count
) {
1410 nmp
->nm_auth
= RPCAUTH_SYS
;
1413 for (s
=0; s
< nmp
->nm_servsec
.count
; s
++)
1414 switch (nmp
->nm_servsec
.flavors
[s
]) {
1416 /* prefer RPCAUTH_SYS to RPCAUTH_NONE */
1417 if (found
&& (nmp
->nm_auth
== RPCAUTH_NONE
))
1424 nmp
->nm_auth
= nmp
->nm_servsec
.flavors
[s
];
1430 error
= !found
? EAUTH
: 0;
1432 FREE_ZONE(path
, MAXPATHLEN
, M_NAMEI
);
1435 nfs_socket_search_update_error(&nss
, error
);
1438 nfs_socket_destroy(nso
);
1442 FREE(nmp
->nm_fh
, M_TEMP
);
1445 NFS_BITMAP_SET(nmp
->nm_flags
, NFS_MFLAG_CALLUMNT
);
1448 /* put the real upcall in place */
1449 upcall
= (nso
->nso_sotype
== SOCK_STREAM
) ? nfs_tcp_rcv
: nfs_udp_rcv
;
1450 error
= sock_setupcall(nso
->nso_so
, upcall
, nmp
);
1452 nfs_socket_search_update_error(&nss
, error
);
1453 nfs_socket_destroy(nso
);
1457 if (!(nmp
->nm_sockflags
& NMSOCK_HASCONNECTED
)) {
1458 /* set mntfromname to this location */
1459 if (!NM_OMATTR_GIVEN(nmp
, MNTFROM
))
1460 nfs_location_mntfromname(&nmp
->nm_locations
, nso
->nso_location
,
1461 vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
,
1462 sizeof(vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
), 0);
1463 /* some negotiated values need to remain unchanged for the life of the mount */
1464 if (!nmp
->nm_sotype
)
1465 nmp
->nm_sotype
= nso
->nso_sotype
;
1466 if (!nmp
->nm_vers
) {
1467 nmp
->nm_vers
= nfsvers
;
1468 /* If we negotiated NFSv4, set nm_nfsport if we ended up on the standard NFS port */
1469 if ((nfsvers
>= NFS_VER4
) && !NFS_BITMAP_ISSET(nmp
->nm_mattrs
, NFS_MATTR_NFS_PORT
)) {
1470 if (nso
->nso_saddr
->sa_family
== AF_INET
)
1471 port
= ((struct sockaddr_in
*)nso
->nso_saddr
)->sin_port
= htons(port
);
1472 else if (nso
->nso_saddr
->sa_family
== AF_INET6
)
1473 port
= ((struct sockaddr_in6
*)nso
->nso_saddr
)->sin6_port
= htons(port
);
1476 if (port
== NFS_PORT
)
1477 nmp
->nm_nfsport
= NFS_PORT
;
1480 /* do some version-specific pre-mount set up */
1481 if (nmp
->nm_vers
>= NFS_VER4
) {
1483 nmp
->nm_mounttime
= ((uint64_t)now
.tv_sec
<< 32) | now
.tv_usec
;
1484 if (!NMFLAG(nmp
, NOCALLBACK
))
1485 nfs4_mount_callback_setup(nmp
);
1489 /* Initialize NFS socket state variables */
1490 lck_mtx_lock(&nmp
->nm_lock
);
1491 nmp
->nm_srtt
[0] = nmp
->nm_srtt
[1] = nmp
->nm_srtt
[2] =
1492 nmp
->nm_srtt
[3] = (NFS_TIMEO
<< 3);
1493 nmp
->nm_sdrtt
[0] = nmp
->nm_sdrtt
[1] = nmp
->nm_sdrtt
[2] =
1494 nmp
->nm_sdrtt
[3] = 0;
1495 if (nso
->nso_sotype
== SOCK_DGRAM
) {
1496 nmp
->nm_cwnd
= NFS_MAXCWND
/ 2; /* Initial send window */
1498 } else if (nso
->nso_sotype
== SOCK_STREAM
) {
1499 nmp
->nm_timeouts
= 0;
1501 nmp
->nm_sockflags
&= ~NMSOCK_CONNECTING
;
1502 nmp
->nm_sockflags
|= NMSOCK_SETUP
;
1503 /* move the socket to the mount structure */
1505 oldsaddr
= nmp
->nm_saddr
;
1506 nmp
->nm_saddr
= nso
->nso_saddr
;
1507 lck_mtx_unlock(&nmp
->nm_lock
);
1508 error
= nfs_connect_setup(nmp
);
1509 lck_mtx_lock(&nmp
->nm_lock
);
1510 nmp
->nm_sockflags
&= ~NMSOCK_SETUP
;
1512 nmp
->nm_sockflags
|= NMSOCK_READY
;
1513 wakeup(&nmp
->nm_sockflags
);
1516 NFS_SOCK_DBG("nfs connect %s socket %p setup failed %d\n",
1517 vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, nso
, error
);
1518 nfs_socket_search_update_error(&nss
, error
);
1519 nmp
->nm_saddr
= oldsaddr
;
1520 if (!(nmp
->nm_sockflags
& NMSOCK_HASCONNECTED
)) {
1521 /* undo settings made prior to setup */
1522 if (!NFS_BITMAP_ISSET(nmp
->nm_mattrs
, NFS_MATTR_SOCKET_TYPE
))
1524 if (!NFS_BITMAP_ISSET(nmp
->nm_mattrs
, NFS_MATTR_NFS_VERSION
)) {
1525 if (nmp
->nm_vers
>= NFS_VER4
) {
1526 if (!NFS_BITMAP_ISSET(nmp
->nm_mattrs
, NFS_MATTR_NFS_PORT
))
1527 nmp
->nm_nfsport
= 0;
1529 nfs4_mount_callback_shutdown(nmp
);
1530 if (IS_VALID_CRED(nmp
->nm_mcred
))
1531 kauth_cred_unref(&nmp
->nm_mcred
);
1532 bzero(&nmp
->nm_un
, sizeof(nmp
->nm_un
));
1537 lck_mtx_unlock(&nmp
->nm_lock
);
1539 nfs_socket_destroy(nso
);
1543 /* update current location */
1544 if ((nmp
->nm_locations
.nl_current
.nli_flags
& NLI_VALID
) &&
1545 (nmp
->nm_locations
.nl_current
.nli_serv
!= nso
->nso_location
.nli_serv
)) {
1546 /* server has changed, we should initiate failover/recovery */
1549 nmp
->nm_locations
.nl_current
= nso
->nso_location
;
1550 nmp
->nm_locations
.nl_current
.nli_flags
|= NLI_VALID
;
1552 if (!(nmp
->nm_sockflags
& NMSOCK_HASCONNECTED
)) {
1553 /* We have now successfully connected... make a note of it. */
1554 nmp
->nm_sockflags
|= NMSOCK_HASCONNECTED
;
1557 lck_mtx_unlock(&nmp
->nm_lock
);
1559 FREE(oldsaddr
, M_SONAME
);
1561 if (nss
.nss_flags
& NSS_WARNED
)
1562 log(LOG_INFO
, "nfs_connect: socket connect completed for %s\n",
1563 vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
);
1566 nfs_socket_search_cleanup(&nss
);
1570 FREE_ZONE(path
, MAXPATHLEN
, M_NAMEI
);
1571 NFS_SOCK_DBG("nfs connect %s success\n", vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
);
1576 /* setup & confirm socket connection is functional */
1578 nfs_connect_setup(struct nfsmount
*nmp
)
1582 if (nmp
->nm_vers
>= NFS_VER4
) {
1583 if (nmp
->nm_state
& NFSSTA_CLIENTID
) {
1584 /* first, try to renew our current state */
1585 error
= nfs4_renew(nmp
, R_SETUP
);
1586 if ((error
== NFSERR_ADMIN_REVOKED
) ||
1587 (error
== NFSERR_CB_PATH_DOWN
) ||
1588 (error
== NFSERR_EXPIRED
) ||
1589 (error
== NFSERR_LEASE_MOVED
) ||
1590 (error
== NFSERR_STALE_CLIENTID
)) {
1591 lck_mtx_lock(&nmp
->nm_lock
);
1592 nfs_need_recover(nmp
, error
);
1593 lck_mtx_unlock(&nmp
->nm_lock
);
1596 error
= nfs4_setclientid(nmp
);
1602 * NFS socket reconnect routine:
1603 * Called when a connection is broken.
1604 * - disconnect the old socket
1605 * - nfs_connect() again
1606 * - set R_MUSTRESEND for all outstanding requests on mount point
1607 * If this fails the mount point is DEAD!
1610 nfs_reconnect(struct nfsmount
*nmp
)
1614 thread_t thd
= current_thread();
1615 int error
, wentdown
= 0, verbose
= 1;
1620 lastmsg
= now
.tv_sec
- (nmp
->nm_tprintf_delay
- nmp
->nm_tprintf_initial_delay
);
1622 nfs_disconnect(nmp
);
1625 lck_mtx_lock(&nmp
->nm_lock
);
1626 timeo
= nfs_is_squishy(nmp
) ? 8 : 30;
1627 lck_mtx_unlock(&nmp
->nm_lock
);
1629 while ((error
= nfs_connect(nmp
, verbose
, timeo
))) {
1631 nfs_disconnect(nmp
);
1632 if ((error
== EINTR
) || (error
== ERESTART
))
1637 if ((lastmsg
+ nmp
->nm_tprintf_delay
) < now
.tv_sec
) {
1638 lastmsg
= now
.tv_sec
;
1639 nfs_down(nmp
, thd
, error
, NFSSTA_TIMEO
, "can not connect", 0);
1642 lck_mtx_lock(&nmp
->nm_lock
);
1643 if (!(nmp
->nm_state
& NFSSTA_MOUNTED
)) {
1644 /* we're not yet completely mounted and */
1645 /* we can't reconnect, so we fail */
1646 lck_mtx_unlock(&nmp
->nm_lock
);
1647 NFS_SOCK_DBG("Not mounted returning %d\n", error
);
1651 if (nfs_mount_check_dead_timeout(nmp
)) {
1652 nfs_mount_make_zombie(nmp
);
1653 lck_mtx_unlock(&nmp
->nm_lock
);
1657 if ((error
= nfs_sigintr(nmp
, NULL
, thd
, 1))) {
1658 lck_mtx_unlock(&nmp
->nm_lock
);
1661 lck_mtx_unlock(&nmp
->nm_lock
);
1662 tsleep(nfs_reconnect
, PSOCK
, "nfs_reconnect_delay", 2*hz
);
1663 if ((error
= nfs_sigintr(nmp
, NULL
, thd
, 0)))
1668 nfs_up(nmp
, thd
, NFSSTA_TIMEO
, "connected");
1671 * Loop through outstanding request list and mark all requests
1672 * as needing a resend. (Though nfs_need_reconnect() probably
1673 * marked them all already.)
1675 lck_mtx_lock(nfs_request_mutex
);
1676 TAILQ_FOREACH(rq
, &nfs_reqq
, r_chain
) {
1677 if (rq
->r_nmp
== nmp
) {
1678 lck_mtx_lock(&rq
->r_mtx
);
1679 if (!rq
->r_error
&& !rq
->r_nmrep
.nmc_mhead
&& !(rq
->r_flags
& R_MUSTRESEND
)) {
1680 rq
->r_flags
|= R_MUSTRESEND
;
1683 if ((rq
->r_flags
& (R_ASYNC
|R_ASYNCWAIT
|R_SENDING
)) == R_ASYNC
)
1684 nfs_asyncio_resend(rq
);
1686 lck_mtx_unlock(&rq
->r_mtx
);
1689 lck_mtx_unlock(nfs_request_mutex
);
1694 * NFS disconnect. Clean up and unlink.
1697 nfs_disconnect(struct nfsmount
*nmp
)
1699 struct nfs_socket
*nso
;
1701 lck_mtx_lock(&nmp
->nm_lock
);
1704 struct timespec ts
= { 1, 0 };
1705 if (nmp
->nm_state
& NFSSTA_SENDING
) { /* wait for sending to complete */
1706 nmp
->nm_state
|= NFSSTA_WANTSND
;
1707 msleep(&nmp
->nm_state
, &nmp
->nm_lock
, PZERO
-1, "nfswaitsending", &ts
);
1710 if (nmp
->nm_sockflags
& NMSOCK_POKE
) { /* wait for poking to complete */
1711 msleep(&nmp
->nm_sockflags
, &nmp
->nm_lock
, PZERO
-1, "nfswaitpoke", &ts
);
1714 nmp
->nm_sockflags
|= NMSOCK_DISCONNECTING
;
1715 nmp
->nm_sockflags
&= ~NMSOCK_READY
;
1718 if (nso
->nso_saddr
== nmp
->nm_saddr
)
1719 nso
->nso_saddr
= NULL
;
1720 lck_mtx_unlock(&nmp
->nm_lock
);
1721 nfs_socket_destroy(nso
);
1722 lck_mtx_lock(&nmp
->nm_lock
);
1723 nmp
->nm_sockflags
&= ~NMSOCK_DISCONNECTING
;
1724 lck_mtx_unlock(&nmp
->nm_lock
);
1726 lck_mtx_unlock(&nmp
->nm_lock
);
1731 * mark an NFS mount as needing a reconnect/resends.
1734 nfs_need_reconnect(struct nfsmount
*nmp
)
1738 lck_mtx_lock(&nmp
->nm_lock
);
1739 nmp
->nm_sockflags
&= ~(NMSOCK_READY
|NMSOCK_SETUP
);
1740 lck_mtx_unlock(&nmp
->nm_lock
);
1743 * Loop through outstanding request list and
1744 * mark all requests as needing a resend.
1746 lck_mtx_lock(nfs_request_mutex
);
1747 TAILQ_FOREACH(rq
, &nfs_reqq
, r_chain
) {
1748 if (rq
->r_nmp
== nmp
) {
1749 lck_mtx_lock(&rq
->r_mtx
);
1750 if (!rq
->r_error
&& !rq
->r_nmrep
.nmc_mhead
&& !(rq
->r_flags
& R_MUSTRESEND
)) {
1751 rq
->r_flags
|= R_MUSTRESEND
;
1754 if ((rq
->r_flags
& (R_ASYNC
|R_ASYNCWAIT
|R_SENDING
)) == R_ASYNC
)
1755 nfs_asyncio_resend(rq
);
1757 lck_mtx_unlock(&rq
->r_mtx
);
1760 lck_mtx_unlock(nfs_request_mutex
);
1765 * thread to handle miscellaneous async NFS socket work (reconnects/resends)
1768 nfs_mount_sock_thread(void *arg
, __unused wait_result_t wr
)
1770 struct nfsmount
*nmp
= arg
;
1771 struct timespec ts
= { 30, 0 };
1772 thread_t thd
= current_thread();
1775 int error
, dofinish
;
1777 int do_reconnect_sleep
= 0;
1779 lck_mtx_lock(&nmp
->nm_lock
);
1780 while (!(nmp
->nm_sockflags
& NMSOCK_READY
) ||
1781 !TAILQ_EMPTY(&nmp
->nm_resendq
) ||
1782 !LIST_EMPTY(&nmp
->nm_monlist
) ||
1783 nmp
->nm_deadto_start
||
1784 (nmp
->nm_state
& NFSSTA_RECOVER
) ||
1785 ((nmp
->nm_vers
>= NFS_VER4
) && !TAILQ_EMPTY(&nmp
->nm_dreturnq
)))
1787 if (nmp
->nm_sockflags
& NMSOCK_UNMOUNT
)
1789 /* do reconnect, if necessary */
1790 if (!(nmp
->nm_sockflags
& NMSOCK_READY
) && !(nmp
->nm_state
& (NFSSTA_FORCE
|NFSSTA_DEAD
))) {
1791 if (nmp
->nm_reconnect_start
<= 0) {
1793 nmp
->nm_reconnect_start
= now
.tv_sec
;
1795 lck_mtx_unlock(&nmp
->nm_lock
);
1796 NFS_SOCK_DBG("nfs reconnect %s\n", vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
);
1798 * XXX We don't want to call reconnect again right away if returned errors
1799 * before that may not have blocked. This has caused spamming null procs
1800 * from machines in the pass.
1802 if (do_reconnect_sleep
)
1803 tsleep(nfs_mount_sock_thread
, PSOCK
, "nfs_reconnect_sock_thread_delay", hz
);
1804 error
= nfs_reconnect(nmp
);
1807 if (error
== EIO
|| error
== EINTR
) {
1808 lvl
= (do_reconnect_sleep
++ % 600) ? 7 : 0;
1810 nfs_printf(NFS_FAC_SOCK
, lvl
, "nfs reconnect %s: returned %d\n",
1811 vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, error
);
1813 nmp
->nm_reconnect_start
= 0;
1814 do_reconnect_sleep
= 0;
1816 lck_mtx_lock(&nmp
->nm_lock
);
1818 if ((nmp
->nm_sockflags
& NMSOCK_READY
) &&
1819 (nmp
->nm_state
& NFSSTA_RECOVER
) &&
1820 !(nmp
->nm_sockflags
& NMSOCK_UNMOUNT
) &&
1821 !(nmp
->nm_state
& (NFSSTA_FORCE
|NFSSTA_DEAD
))) {
1822 /* perform state recovery */
1823 lck_mtx_unlock(&nmp
->nm_lock
);
1825 lck_mtx_lock(&nmp
->nm_lock
);
1827 /* handle NFSv4 delegation returns */
1828 while ((nmp
->nm_vers
>= NFS_VER4
) && !(nmp
->nm_state
& (NFSSTA_FORCE
|NFSSTA_DEAD
)) &&
1829 (nmp
->nm_sockflags
& NMSOCK_READY
) && !(nmp
->nm_state
& NFSSTA_RECOVER
) &&
1830 ((np
= TAILQ_FIRST(&nmp
->nm_dreturnq
)))) {
1831 lck_mtx_unlock(&nmp
->nm_lock
);
1832 nfs4_delegation_return(np
, R_RECOVER
, thd
, nmp
->nm_mcred
);
1833 lck_mtx_lock(&nmp
->nm_lock
);
1835 /* do resends, if necessary/possible */
1836 while ((((nmp
->nm_sockflags
& NMSOCK_READY
) && !(nmp
->nm_state
& NFSSTA_RECOVER
)) ||
1837 (nmp
->nm_state
& (NFSSTA_FORCE
|NFSSTA_DEAD
))) &&
1838 ((req
= TAILQ_FIRST(&nmp
->nm_resendq
)))) {
1839 if (req
->r_resendtime
)
1841 while (req
&& !(nmp
->nm_state
& (NFSSTA_FORCE
|NFSSTA_DEAD
)) && req
->r_resendtime
&& (now
.tv_sec
< req
->r_resendtime
))
1842 req
= TAILQ_NEXT(req
, r_rchain
);
1845 TAILQ_REMOVE(&nmp
->nm_resendq
, req
, r_rchain
);
1846 req
->r_rchain
.tqe_next
= NFSREQNOLIST
;
1847 lck_mtx_unlock(&nmp
->nm_lock
);
1848 lck_mtx_lock(&req
->r_mtx
);
1849 if (req
->r_error
|| req
->r_nmrep
.nmc_mhead
) {
1850 dofinish
= req
->r_callback
.rcb_func
&& !(req
->r_flags
& R_WAITSENT
);
1851 req
->r_flags
&= ~R_RESENDQ
;
1853 lck_mtx_unlock(&req
->r_mtx
);
1855 nfs_asyncio_finish(req
);
1856 lck_mtx_lock(&nmp
->nm_lock
);
1859 if ((req
->r_flags
& R_RESTART
) || nfs_request_using_gss(req
)) {
1860 req
->r_flags
&= ~R_RESTART
;
1861 req
->r_resendtime
= 0;
1862 lck_mtx_unlock(&req
->r_mtx
);
1863 /* async RPCs on GSS mounts need to be rebuilt and resent. */
1864 nfs_reqdequeue(req
);
1865 if (nfs_request_using_gss(req
)) {
1866 nfs_gss_clnt_rpcdone(req
);
1867 error
= nfs_gss_clnt_args_restore(req
);
1868 if (error
== ENEEDAUTH
)
1871 NFS_SOCK_DBG("nfs async%s restart: p %d x 0x%llx f 0x%x rtt %d\n",
1872 nfs_request_using_gss(req
) ? " gss" : "", req
->r_procnum
, req
->r_xid
,
1873 req
->r_flags
, req
->r_rtt
);
1874 error
= nfs_sigintr(nmp
, req
, req
->r_thread
, 0);
1876 error
= nfs_request_add_header(req
);
1878 error
= nfs_request_send(req
, 0);
1879 lck_mtx_lock(&req
->r_mtx
);
1880 if (req
->r_flags
& R_RESENDQ
)
1881 req
->r_flags
&= ~R_RESENDQ
;
1883 req
->r_error
= error
;
1885 dofinish
= error
&& req
->r_callback
.rcb_func
&& !(req
->r_flags
& R_WAITSENT
);
1886 lck_mtx_unlock(&req
->r_mtx
);
1888 nfs_asyncio_finish(req
);
1889 lck_mtx_lock(&nmp
->nm_lock
);
1893 NFS_SOCK_DBG("nfs async resend: p %d x 0x%llx f 0x%x rtt %d\n",
1894 req
->r_procnum
, req
->r_xid
, req
->r_flags
, req
->r_rtt
);
1895 error
= nfs_sigintr(nmp
, req
, req
->r_thread
, 0);
1897 req
->r_flags
|= R_SENDING
;
1898 lck_mtx_unlock(&req
->r_mtx
);
1899 error
= nfs_send(req
, 0);
1900 lck_mtx_lock(&req
->r_mtx
);
1902 if (req
->r_flags
& R_RESENDQ
)
1903 req
->r_flags
&= ~R_RESENDQ
;
1905 lck_mtx_unlock(&req
->r_mtx
);
1906 lck_mtx_lock(&nmp
->nm_lock
);
1910 req
->r_error
= error
;
1911 if (req
->r_flags
& R_RESENDQ
)
1912 req
->r_flags
&= ~R_RESENDQ
;
1914 dofinish
= req
->r_callback
.rcb_func
&& !(req
->r_flags
& R_WAITSENT
);
1915 lck_mtx_unlock(&req
->r_mtx
);
1917 nfs_asyncio_finish(req
);
1918 lck_mtx_lock(&nmp
->nm_lock
);
1920 if (nfs_mount_check_dead_timeout(nmp
)) {
1921 nfs_mount_make_zombie(nmp
);
1925 if (nmp
->nm_state
& (NFSSTA_FORCE
|NFSSTA_DEAD
))
1927 /* check monitored nodes, if necessary/possible */
1928 if (!LIST_EMPTY(&nmp
->nm_monlist
)) {
1929 nmp
->nm_state
|= NFSSTA_MONITOR_SCAN
;
1930 LIST_FOREACH(np
, &nmp
->nm_monlist
, n_monlink
) {
1931 if (!(nmp
->nm_sockflags
& NMSOCK_READY
) ||
1932 (nmp
->nm_state
& (NFSSTA_RECOVER
|NFSSTA_UNMOUNTING
|NFSSTA_FORCE
|NFSSTA_DEAD
)))
1934 np
->n_mflag
|= NMMONSCANINPROG
;
1935 lck_mtx_unlock(&nmp
->nm_lock
);
1936 error
= nfs_getattr(np
, NULL
, vfs_context_kernel(), (NGA_UNCACHED
|NGA_MONITOR
));
1937 if (!error
&& ISSET(np
->n_flag
, NUPDATESIZE
)) /* update quickly to avoid multiple events */
1938 nfs_data_update_size(np
, 0);
1939 lck_mtx_lock(&nmp
->nm_lock
);
1940 np
->n_mflag
&= ~NMMONSCANINPROG
;
1941 if (np
->n_mflag
& NMMONSCANWANT
) {
1942 np
->n_mflag
&= ~NMMONSCANWANT
;
1943 wakeup(&np
->n_mflag
);
1945 if (error
|| !(nmp
->nm_sockflags
& NMSOCK_READY
) ||
1946 (nmp
->nm_state
& (NFSSTA_RECOVER
|NFSSTA_UNMOUNTING
|NFSSTA_FORCE
|NFSSTA_DEAD
)))
1949 nmp
->nm_state
&= ~NFSSTA_MONITOR_SCAN
;
1950 if (nmp
->nm_state
& NFSSTA_UNMOUNTING
)
1951 wakeup(&nmp
->nm_state
); /* let unmounting thread know scan is done */
1953 if ((nmp
->nm_sockflags
& NMSOCK_READY
) || (nmp
->nm_state
& (NFSSTA_RECOVER
|NFSSTA_UNMOUNTING
))) {
1954 if (nmp
->nm_deadto_start
|| !TAILQ_EMPTY(&nmp
->nm_resendq
) ||
1955 (nmp
->nm_state
& NFSSTA_RECOVER
))
1959 msleep(&nmp
->nm_sockthd
, &nmp
->nm_lock
, PSOCK
, "nfssockthread", &ts
);
1963 /* If we're unmounting, send the unmount RPC, if requested/appropriate. */
1964 if ((nmp
->nm_sockflags
& NMSOCK_UNMOUNT
) &&
1965 (nmp
->nm_state
& NFSSTA_MOUNTED
) && NMFLAG(nmp
, CALLUMNT
) &&
1966 (nmp
->nm_vers
< NFS_VER4
) && !(nmp
->nm_state
& (NFSSTA_FORCE
|NFSSTA_DEAD
))) {
1967 lck_mtx_unlock(&nmp
->nm_lock
);
1968 nfs3_umount_rpc(nmp
, vfs_context_kernel(),
1969 (nmp
->nm_sockflags
& NMSOCK_READY
) ? 6 : 2);
1970 lck_mtx_lock(&nmp
->nm_lock
);
1973 if (nmp
->nm_sockthd
== thd
)
1974 nmp
->nm_sockthd
= NULL
;
1975 lck_mtx_unlock(&nmp
->nm_lock
);
1976 wakeup(&nmp
->nm_sockthd
);
1977 thread_terminate(thd
);
1980 /* start or wake a mount's socket thread */
1982 nfs_mount_sock_thread_wake(struct nfsmount
*nmp
)
1984 if (nmp
->nm_sockthd
)
1985 wakeup(&nmp
->nm_sockthd
);
1986 else if (kernel_thread_start(nfs_mount_sock_thread
, nmp
, &nmp
->nm_sockthd
) == KERN_SUCCESS
)
1987 thread_deallocate(nmp
->nm_sockthd
);
1991 * Check if we should mark the mount dead because the
1992 * unresponsive mount has reached the dead timeout.
1993 * (must be called with nmp locked)
1996 nfs_mount_check_dead_timeout(struct nfsmount
*nmp
)
2000 if (nmp
->nm_state
& NFSSTA_DEAD
)
2002 if (nmp
->nm_deadto_start
== 0)
2004 nfs_is_squishy(nmp
);
2005 if (nmp
->nm_curdeadtimeout
<= 0)
2008 if ((now
.tv_sec
- nmp
->nm_deadto_start
) < nmp
->nm_curdeadtimeout
)
2014 * Call nfs_mount_zombie to remove most of the
2015 * nfs state for the mount, and then ask to be forcibly unmounted.
2017 * Assumes the nfs mount structure lock nm_lock is held.
2021 nfs_mount_make_zombie(struct nfsmount
*nmp
)
2028 if (nmp
->nm_state
& NFSSTA_DEAD
)
2031 printf("nfs server %s: %sdead\n", vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
,
2032 (nmp
->nm_curdeadtimeout
!= nmp
->nm_deadtimeout
) ? "squished " : "");
2033 fsid
= vfs_statfs(nmp
->nm_mountp
)->f_fsid
;
2034 lck_mtx_unlock(&nmp
->nm_lock
);
2035 nfs_mount_zombie(nmp
, NFSSTA_DEAD
);
2036 vfs_event_signal(&fsid
, VQ_DEAD
, 0);
2037 lck_mtx_lock(&nmp
->nm_lock
);
2042 * NFS callback channel socket state
2044 struct nfs_callback_socket
2046 TAILQ_ENTRY(nfs_callback_socket
) ncbs_link
;
2047 socket_t ncbs_so
; /* the socket */
2048 struct sockaddr_storage ncbs_saddr
; /* socket address */
2049 struct nfs_rpc_record_state ncbs_rrs
; /* RPC record parsing state */
2050 time_t ncbs_stamp
; /* last accessed at */
2051 uint32_t ncbs_flags
; /* see below */
2053 #define NCBSOCK_UPCALL 0x0001
2054 #define NCBSOCK_UPCALLWANT 0x0002
2055 #define NCBSOCK_DEAD 0x0004
2058 * NFS callback channel state
2060 * One listening socket for accepting socket connections from servers and
2061 * a list of connected sockets to handle callback requests on.
2062 * Mounts registered with the callback channel are assigned IDs and
2063 * put on a list so that the callback request handling code can match
2064 * the requests up with mounts.
2066 socket_t nfs4_cb_so
= NULL
;
2067 socket_t nfs4_cb_so6
= NULL
;
2068 in_port_t nfs4_cb_port
= 0;
2069 in_port_t nfs4_cb_port6
= 0;
2070 uint32_t nfs4_cb_id
= 0;
2071 uint32_t nfs4_cb_so_usecount
= 0;
2072 TAILQ_HEAD(nfs4_cb_sock_list
,nfs_callback_socket
) nfs4_cb_socks
;
2073 TAILQ_HEAD(nfs4_cb_mount_list
,nfsmount
) nfs4_cb_mounts
;
2075 int nfs4_cb_handler(struct nfs_callback_socket
*, mbuf_t
);
2078 * Set up the callback channel for the NFS mount.
2080 * Initializes the callback channel socket state and
2081 * assigns a callback ID to the mount.
2084 nfs4_mount_callback_setup(struct nfsmount
*nmp
)
2086 struct sockaddr_in sin
;
2087 struct sockaddr_in6 sin6
;
2089 socket_t so6
= NULL
;
2090 struct timeval timeo
;
2094 lck_mtx_lock(nfs_global_mutex
);
2095 if (nfs4_cb_id
== 0) {
2096 TAILQ_INIT(&nfs4_cb_mounts
);
2097 TAILQ_INIT(&nfs4_cb_socks
);
2100 nmp
->nm_cbid
= nfs4_cb_id
++;
2101 if (nmp
->nm_cbid
== 0)
2102 nmp
->nm_cbid
= nfs4_cb_id
++;
2103 nfs4_cb_so_usecount
++;
2104 TAILQ_INSERT_HEAD(&nfs4_cb_mounts
, nmp
, nm_cblink
);
2107 lck_mtx_unlock(nfs_global_mutex
);
2112 error
= sock_socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
, nfs4_cb_accept
, NULL
, &nfs4_cb_so
);
2114 log(LOG_INFO
, "nfs callback setup: error %d creating listening IPv4 socket\n", error
);
2119 sock_setsockopt(so
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
2120 sin
.sin_len
= sizeof(struct sockaddr_in
);
2121 sin
.sin_family
= AF_INET
;
2122 sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
2123 sin
.sin_port
= htons(nfs_callback_port
); /* try to use specified port */
2124 error
= sock_bind(so
, (struct sockaddr
*)&sin
);
2126 log(LOG_INFO
, "nfs callback setup: error %d binding listening IPv4 socket\n", error
);
2129 error
= sock_getsockname(so
, (struct sockaddr
*)&sin
, sin
.sin_len
);
2131 log(LOG_INFO
, "nfs callback setup: error %d getting listening IPv4 socket port\n", error
);
2134 nfs4_cb_port
= ntohs(sin
.sin_port
);
2136 error
= sock_listen(so
, 32);
2138 log(LOG_INFO
, "nfs callback setup: error %d on IPv4 listen\n", error
);
2142 /* receive timeout shouldn't matter. If timeout on send, we'll want to drop the socket */
2145 error
= sock_setsockopt(so
, SOL_SOCKET
, SO_RCVTIMEO
, &timeo
, sizeof(timeo
));
2147 log(LOG_INFO
, "nfs callback setup: error %d setting IPv4 socket rx timeout\n", error
);
2148 error
= sock_setsockopt(so
, SOL_SOCKET
, SO_SNDTIMEO
, &timeo
, sizeof(timeo
));
2150 log(LOG_INFO
, "nfs callback setup: error %d setting IPv4 socket tx timeout\n", error
);
2151 sock_setsockopt(so
, IPPROTO_TCP
, TCP_NODELAY
, &on
, sizeof(on
));
2152 sock_setsockopt(so
, SOL_SOCKET
, SO_NOADDRERR
, &on
, sizeof(on
));
2153 sock_setsockopt(so
, SOL_SOCKET
, SO_UPCALLCLOSEWAIT
, &on
, sizeof(on
));
2157 error
= sock_socket(AF_INET6
, SOCK_STREAM
, IPPROTO_TCP
, nfs4_cb_accept
, NULL
, &nfs4_cb_so6
);
2159 log(LOG_INFO
, "nfs callback setup: error %d creating listening IPv6 socket\n", error
);
2164 sock_setsockopt(so6
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
2165 sock_setsockopt(so6
, IPPROTO_IPV6
, IPV6_V6ONLY
, &on
, sizeof(on
));
2166 /* try to use specified port or same port as IPv4 */
2167 port
= nfs_callback_port
? nfs_callback_port
: nfs4_cb_port
;
2169 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
2170 sin6
.sin6_family
= AF_INET6
;
2171 sin6
.sin6_addr
= in6addr_any
;
2172 sin6
.sin6_port
= htons(port
);
2173 error
= sock_bind(so6
, (struct sockaddr
*)&sin6
);
2175 if (port
!= nfs_callback_port
) {
2176 /* if we simply tried to match the IPv4 port, then try any port */
2178 goto ipv6_bind_again
;
2180 log(LOG_INFO
, "nfs callback setup: error %d binding listening IPv6 socket\n", error
);
2183 error
= sock_getsockname(so6
, (struct sockaddr
*)&sin6
, sin6
.sin6_len
);
2185 log(LOG_INFO
, "nfs callback setup: error %d getting listening IPv6 socket port\n", error
);
2188 nfs4_cb_port6
= ntohs(sin6
.sin6_port
);
2190 error
= sock_listen(so6
, 32);
2192 log(LOG_INFO
, "nfs callback setup: error %d on IPv6 listen\n", error
);
2196 /* receive timeout shouldn't matter. If timeout on send, we'll want to drop the socket */
2199 error
= sock_setsockopt(so6
, SOL_SOCKET
, SO_RCVTIMEO
, &timeo
, sizeof(timeo
));
2201 log(LOG_INFO
, "nfs callback setup: error %d setting IPv6 socket rx timeout\n", error
);
2202 error
= sock_setsockopt(so6
, SOL_SOCKET
, SO_SNDTIMEO
, &timeo
, sizeof(timeo
));
2204 log(LOG_INFO
, "nfs callback setup: error %d setting IPv6 socket tx timeout\n", error
);
2205 sock_setsockopt(so6
, IPPROTO_TCP
, TCP_NODELAY
, &on
, sizeof(on
));
2206 sock_setsockopt(so6
, SOL_SOCKET
, SO_NOADDRERR
, &on
, sizeof(on
));
2207 sock_setsockopt(so6
, SOL_SOCKET
, SO_UPCALLCLOSEWAIT
, &on
, sizeof(on
));
2212 nfs4_cb_so
= nfs4_cb_so6
= NULL
;
2213 lck_mtx_unlock(nfs_global_mutex
);
2215 sock_shutdown(so
, SHUT_RDWR
);
2219 sock_shutdown(so6
, SHUT_RDWR
);
2223 lck_mtx_unlock(nfs_global_mutex
);
2228 * Shut down the callback channel for the NFS mount.
2230 * Clears the mount's callback ID and releases the mounts
2231 * reference on the callback socket. Last reference dropped
2232 * will also shut down the callback socket(s).
2235 nfs4_mount_callback_shutdown(struct nfsmount
*nmp
)
2237 struct nfs_callback_socket
*ncbsp
;
2239 struct nfs4_cb_sock_list cb_socks
;
2240 struct timespec ts
= {1,0};
2242 lck_mtx_lock(nfs_global_mutex
);
2243 TAILQ_REMOVE(&nfs4_cb_mounts
, nmp
, nm_cblink
);
2244 /* wait for any callbacks in progress to complete */
2245 while (nmp
->nm_cbrefs
)
2246 msleep(&nmp
->nm_cbrefs
, nfs_global_mutex
, PSOCK
, "cbshutwait", &ts
);
2248 if (--nfs4_cb_so_usecount
) {
2249 lck_mtx_unlock(nfs_global_mutex
);
2254 nfs4_cb_so
= nfs4_cb_so6
= NULL
;
2255 TAILQ_INIT(&cb_socks
);
2256 TAILQ_CONCAT(&cb_socks
, &nfs4_cb_socks
, ncbs_link
);
2257 lck_mtx_unlock(nfs_global_mutex
);
2259 sock_shutdown(so
, SHUT_RDWR
);
2263 sock_shutdown(so6
, SHUT_RDWR
);
2266 while ((ncbsp
= TAILQ_FIRST(&cb_socks
))) {
2267 TAILQ_REMOVE(&cb_socks
, ncbsp
, ncbs_link
);
2268 sock_shutdown(ncbsp
->ncbs_so
, SHUT_RDWR
);
2269 sock_close(ncbsp
->ncbs_so
);
2270 nfs_rpc_record_state_cleanup(&ncbsp
->ncbs_rrs
);
2271 FREE(ncbsp
, M_TEMP
);
2276 * Check periodically for stale/unused nfs callback sockets
2278 #define NFS4_CB_TIMER_PERIOD 30
2279 #define NFS4_CB_IDLE_MAX 300
2281 nfs4_callback_timer(__unused
void *param0
, __unused
void *param1
)
2283 struct nfs_callback_socket
*ncbsp
, *nextncbsp
;
2287 lck_mtx_lock(nfs_global_mutex
);
2288 if (TAILQ_EMPTY(&nfs4_cb_socks
)) {
2289 nfs4_callback_timer_on
= 0;
2290 lck_mtx_unlock(nfs_global_mutex
);
2294 TAILQ_FOREACH_SAFE(ncbsp
, &nfs4_cb_socks
, ncbs_link
, nextncbsp
) {
2295 if (!(ncbsp
->ncbs_flags
& NCBSOCK_DEAD
) &&
2296 (now
.tv_sec
< (ncbsp
->ncbs_stamp
+ NFS4_CB_IDLE_MAX
)))
2298 TAILQ_REMOVE(&nfs4_cb_socks
, ncbsp
, ncbs_link
);
2299 lck_mtx_unlock(nfs_global_mutex
);
2300 sock_shutdown(ncbsp
->ncbs_so
, SHUT_RDWR
);
2301 sock_close(ncbsp
->ncbs_so
);
2302 nfs_rpc_record_state_cleanup(&ncbsp
->ncbs_rrs
);
2303 FREE(ncbsp
, M_TEMP
);
2306 nfs4_callback_timer_on
= 1;
2307 nfs_interval_timer_start(nfs4_callback_timer_call
,
2308 NFS4_CB_TIMER_PERIOD
* 1000);
2309 lck_mtx_unlock(nfs_global_mutex
);
2313 * Accept a new callback socket.
2316 nfs4_cb_accept(socket_t so
, __unused
void *arg
, __unused
int waitflag
)
2318 socket_t newso
= NULL
;
2319 struct nfs_callback_socket
*ncbsp
;
2320 struct nfsmount
*nmp
;
2321 struct timeval timeo
, now
;
2322 int error
, on
= 1, ip
;
2324 if (so
== nfs4_cb_so
)
2326 else if (so
== nfs4_cb_so6
)
2331 /* allocate/initialize a new nfs_callback_socket */
2332 MALLOC(ncbsp
, struct nfs_callback_socket
*, sizeof(struct nfs_callback_socket
), M_TEMP
, M_WAITOK
);
2334 log(LOG_ERR
, "nfs callback accept: no memory for new socket\n");
2337 bzero(ncbsp
, sizeof(*ncbsp
));
2338 ncbsp
->ncbs_saddr
.ss_len
= (ip
== 4) ? sizeof(struct sockaddr_in
) : sizeof(struct sockaddr_in6
);
2339 nfs_rpc_record_state_init(&ncbsp
->ncbs_rrs
);
2341 /* accept a new socket */
2342 error
= sock_accept(so
, (struct sockaddr
*)&ncbsp
->ncbs_saddr
,
2343 ncbsp
->ncbs_saddr
.ss_len
, MSG_DONTWAIT
,
2344 nfs4_cb_rcv
, ncbsp
, &newso
);
2346 log(LOG_INFO
, "nfs callback accept: error %d accepting IPv%d socket\n", error
, ip
);
2347 FREE(ncbsp
, M_TEMP
);
2351 /* set up the new socket */
2352 /* receive timeout shouldn't matter. If timeout on send, we'll want to drop the socket */
2355 error
= sock_setsockopt(newso
, SOL_SOCKET
, SO_RCVTIMEO
, &timeo
, sizeof(timeo
));
2357 log(LOG_INFO
, "nfs callback socket: error %d setting IPv%d socket rx timeout\n", error
, ip
);
2358 error
= sock_setsockopt(newso
, SOL_SOCKET
, SO_SNDTIMEO
, &timeo
, sizeof(timeo
));
2360 log(LOG_INFO
, "nfs callback socket: error %d setting IPv%d socket tx timeout\n", error
, ip
);
2361 sock_setsockopt(newso
, IPPROTO_TCP
, TCP_NODELAY
, &on
, sizeof(on
));
2362 sock_setsockopt(newso
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
2363 sock_setsockopt(newso
, SOL_SOCKET
, SO_NOADDRERR
, &on
, sizeof(on
));
2364 sock_setsockopt(newso
, SOL_SOCKET
, SO_UPCALLCLOSEWAIT
, &on
, sizeof(on
));
2366 ncbsp
->ncbs_so
= newso
;
2368 ncbsp
->ncbs_stamp
= now
.tv_sec
;
2370 lck_mtx_lock(nfs_global_mutex
);
2372 /* add it to the list */
2373 TAILQ_INSERT_HEAD(&nfs4_cb_socks
, ncbsp
, ncbs_link
);
2375 /* verify it's from a host we have mounted */
2376 TAILQ_FOREACH(nmp
, &nfs4_cb_mounts
, nm_cblink
) {
2377 /* check if socket's source address matches this mount's server address */
2380 if (nfs_sockaddr_cmp((struct sockaddr
*)&ncbsp
->ncbs_saddr
, nmp
->nm_saddr
) == 0)
2383 if (!nmp
) /* we don't want this socket, mark it dead */
2384 ncbsp
->ncbs_flags
|= NCBSOCK_DEAD
;
2386 /* make sure the callback socket cleanup timer is running */
2387 /* (shorten the timer if we've got a socket we don't want) */
2388 if (!nfs4_callback_timer_on
) {
2389 nfs4_callback_timer_on
= 1;
2390 nfs_interval_timer_start(nfs4_callback_timer_call
,
2391 !nmp
? 500 : (NFS4_CB_TIMER_PERIOD
* 1000));
2392 } else if (!nmp
&& (nfs4_callback_timer_on
< 2)) {
2393 nfs4_callback_timer_on
= 2;
2394 thread_call_cancel(nfs4_callback_timer_call
);
2395 nfs_interval_timer_start(nfs4_callback_timer_call
, 500);
2398 lck_mtx_unlock(nfs_global_mutex
);
2402 * Receive mbufs from callback sockets into RPC records and process each record.
2403 * Detect connection has been closed and shut down.
2406 nfs4_cb_rcv(socket_t so
, void *arg
, __unused
int waitflag
)
2408 struct nfs_callback_socket
*ncbsp
= arg
;
2409 struct timespec ts
= {1,0};
2412 int error
= 0, recv
= 1;
2414 lck_mtx_lock(nfs_global_mutex
);
2415 while (ncbsp
->ncbs_flags
& NCBSOCK_UPCALL
) {
2416 /* wait if upcall is already in progress */
2417 ncbsp
->ncbs_flags
|= NCBSOCK_UPCALLWANT
;
2418 msleep(ncbsp
, nfs_global_mutex
, PSOCK
, "cbupcall", &ts
);
2420 ncbsp
->ncbs_flags
|= NCBSOCK_UPCALL
;
2421 lck_mtx_unlock(nfs_global_mutex
);
2423 /* loop while we make error-free progress */
2424 while (!error
&& recv
) {
2425 error
= nfs_rpc_record_read(so
, &ncbsp
->ncbs_rrs
, MSG_DONTWAIT
, &recv
, &m
);
2426 if (m
) /* handle the request */
2427 error
= nfs4_cb_handler(ncbsp
, m
);
2430 /* note: no error and no data indicates server closed its end */
2431 if ((error
!= EWOULDBLOCK
) && (error
|| !recv
)) {
2433 * Socket is either being closed or should be.
2434 * We can't close the socket in the context of the upcall.
2435 * So we mark it as dead and leave it for the cleanup timer to reap.
2437 ncbsp
->ncbs_stamp
= 0;
2438 ncbsp
->ncbs_flags
|= NCBSOCK_DEAD
;
2441 ncbsp
->ncbs_stamp
= now
.tv_sec
;
2444 lck_mtx_lock(nfs_global_mutex
);
2445 ncbsp
->ncbs_flags
&= ~NCBSOCK_UPCALL
;
2446 lck_mtx_unlock(nfs_global_mutex
);
2451 * Handle an NFS callback channel request.
2454 nfs4_cb_handler(struct nfs_callback_socket
*ncbsp
, mbuf_t mreq
)
2456 socket_t so
= ncbsp
->ncbs_so
;
2457 struct nfsm_chain nmreq
, nmrep
;
2458 mbuf_t mhead
= NULL
, mrest
= NULL
, m
;
2460 struct nfsmount
*nmp
;
2463 nfs_stateid stateid
;
2464 uint32_t bitmap
[NFS_ATTR_BITMAP_LEN
], rbitmap
[NFS_ATTR_BITMAP_LEN
], bmlen
, truncate
, attrbytes
;
2465 uint32_t val
, xid
, procnum
, taglen
, cbid
, numops
, op
, status
;
2466 uint32_t auth_type
, auth_len
;
2467 uint32_t numres
, *pnumres
;
2468 int error
= 0, replen
, len
;
2471 xid
= numops
= op
= status
= procnum
= taglen
= cbid
= 0;
2473 nfsm_chain_dissect_init(error
, &nmreq
, mreq
);
2474 nfsm_chain_get_32(error
, &nmreq
, xid
); // RPC XID
2475 nfsm_chain_get_32(error
, &nmreq
, val
); // RPC Call
2476 nfsm_assert(error
, (val
== RPC_CALL
), EBADRPC
);
2477 nfsm_chain_get_32(error
, &nmreq
, val
); // RPC Version
2478 nfsm_assert(error
, (val
== RPC_VER2
), ERPCMISMATCH
);
2479 nfsm_chain_get_32(error
, &nmreq
, val
); // RPC Program Number
2480 nfsm_assert(error
, (val
== NFS4_CALLBACK_PROG
), EPROGUNAVAIL
);
2481 nfsm_chain_get_32(error
, &nmreq
, val
); // NFS Callback Program Version Number
2482 nfsm_assert(error
, (val
== NFS4_CALLBACK_PROG_VERSION
), EPROGMISMATCH
);
2483 nfsm_chain_get_32(error
, &nmreq
, procnum
); // NFS Callback Procedure Number
2484 nfsm_assert(error
, (procnum
<= NFSPROC4_CB_COMPOUND
), EPROCUNAVAIL
);
2486 /* Handle authentication */
2487 /* XXX just ignore auth for now - handling kerberos may be tricky */
2488 nfsm_chain_get_32(error
, &nmreq
, auth_type
); // RPC Auth Flavor
2489 nfsm_chain_get_32(error
, &nmreq
, auth_len
); // RPC Auth Length
2490 nfsm_assert(error
, (auth_len
<= RPCAUTH_MAXSIZ
), EBADRPC
);
2491 if (!error
&& (auth_len
> 0))
2492 nfsm_chain_adv(error
, &nmreq
, nfsm_rndup(auth_len
));
2493 nfsm_chain_adv(error
, &nmreq
, NFSX_UNSIGNED
); // verifier flavor (should be AUTH_NONE)
2494 nfsm_chain_get_32(error
, &nmreq
, auth_len
); // verifier length
2495 nfsm_assert(error
, (auth_len
<= RPCAUTH_MAXSIZ
), EBADRPC
);
2496 if (!error
&& (auth_len
> 0))
2497 nfsm_chain_adv(error
, &nmreq
, nfsm_rndup(auth_len
));
2505 case NFSPROC4_CB_NULL
:
2506 status
= NFSERR_RETVOID
;
2508 case NFSPROC4_CB_COMPOUND
:
2509 /* tag, minorversion, cb ident, numops, op array */
2510 nfsm_chain_get_32(error
, &nmreq
, taglen
); /* tag length */
2511 nfsm_assert(error
, (val
<= NFS4_OPAQUE_LIMIT
), EBADRPC
);
2513 /* start building the body of the response */
2514 nfsm_mbuf_get(error
, &mrest
, nfsm_rndup(taglen
) + 5*NFSX_UNSIGNED
);
2515 nfsm_chain_init(&nmrep
, mrest
);
2517 /* copy tag from request to response */
2518 nfsm_chain_add_32(error
, &nmrep
, taglen
); /* tag length */
2519 for (len
= (int)taglen
; !error
&& (len
> 0); len
-= NFSX_UNSIGNED
) {
2520 nfsm_chain_get_32(error
, &nmreq
, val
);
2521 nfsm_chain_add_32(error
, &nmrep
, val
);
2524 /* insert number of results placeholder */
2526 nfsm_chain_add_32(error
, &nmrep
, numres
);
2527 pnumres
= (uint32_t*)(nmrep
.nmc_ptr
- NFSX_UNSIGNED
);
2529 nfsm_chain_get_32(error
, &nmreq
, val
); /* minorversion */
2530 nfsm_assert(error
, (val
== 0), NFSERR_MINOR_VERS_MISMATCH
);
2531 nfsm_chain_get_32(error
, &nmreq
, cbid
); /* callback ID */
2532 nfsm_chain_get_32(error
, &nmreq
, numops
); /* number of operations */
2534 if ((error
== EBADRPC
) || (error
== NFSERR_MINOR_VERS_MISMATCH
))
2536 else if ((error
== ENOBUFS
) || (error
== ENOMEM
))
2537 status
= NFSERR_RESOURCE
;
2539 status
= NFSERR_SERVERFAULT
;
2541 nfsm_chain_null(&nmrep
);
2544 /* match the callback ID to a registered mount */
2545 lck_mtx_lock(nfs_global_mutex
);
2546 TAILQ_FOREACH(nmp
, &nfs4_cb_mounts
, nm_cblink
) {
2547 if (nmp
->nm_cbid
!= cbid
)
2549 /* verify socket's source address matches this mount's server address */
2552 if (nfs_sockaddr_cmp((struct sockaddr
*)&ncbsp
->ncbs_saddr
, nmp
->nm_saddr
) == 0)
2555 /* mark the NFS mount as busy */
2558 lck_mtx_unlock(nfs_global_mutex
);
2560 /* if no mount match, just drop socket. */
2562 nfsm_chain_null(&nmrep
);
2566 /* process ops, adding results to mrest */
2567 while (numops
> 0) {
2569 nfsm_chain_get_32(error
, &nmreq
, op
);
2573 case NFS_OP_CB_GETATTR
:
2574 // (FH, BITMAP) -> (STATUS, BITMAP, ATTRS)
2576 nfsm_chain_get_fh(error
, &nmreq
, NFS_VER4
, &fh
);
2577 bmlen
= NFS_ATTR_BITMAP_LEN
;
2578 nfsm_chain_get_bitmap(error
, &nmreq
, bitmap
, bmlen
);
2582 numops
= 0; /* don't process any more ops */
2584 /* find the node for the file handle */
2585 error
= nfs_nget(nmp
->nm_mountp
, NULL
, NULL
, fh
.fh_data
, fh
.fh_len
, NULL
, NULL
, RPCAUTH_UNKNOWN
, NG_NOCREATE
, &np
);
2587 status
= NFSERR_BADHANDLE
;
2590 numops
= 0; /* don't process any more ops */
2593 nfsm_chain_add_32(error
, &nmrep
, op
);
2594 nfsm_chain_add_32(error
, &nmrep
, status
);
2595 if (!error
&& (status
== EBADRPC
))
2598 /* only allow returning size, change, and mtime attrs */
2599 NFS_CLEAR_ATTRIBUTES(&rbitmap
);
2601 if (NFS_BITMAP_ISSET(&bitmap
, NFS_FATTR_CHANGE
)) {
2602 NFS_BITMAP_SET(&rbitmap
, NFS_FATTR_CHANGE
);
2603 attrbytes
+= 2 * NFSX_UNSIGNED
;
2605 if (NFS_BITMAP_ISSET(&bitmap
, NFS_FATTR_SIZE
)) {
2606 NFS_BITMAP_SET(&rbitmap
, NFS_FATTR_SIZE
);
2607 attrbytes
+= 2 * NFSX_UNSIGNED
;
2609 if (NFS_BITMAP_ISSET(&bitmap
, NFS_FATTR_TIME_MODIFY
)) {
2610 NFS_BITMAP_SET(&rbitmap
, NFS_FATTR_TIME_MODIFY
);
2611 attrbytes
+= 3 * NFSX_UNSIGNED
;
2613 nfsm_chain_add_bitmap(error
, &nmrep
, rbitmap
, NFS_ATTR_BITMAP_LEN
);
2614 nfsm_chain_add_32(error
, &nmrep
, attrbytes
);
2615 if (NFS_BITMAP_ISSET(&bitmap
, NFS_FATTR_CHANGE
))
2616 nfsm_chain_add_64(error
, &nmrep
,
2617 np
->n_vattr
.nva_change
+ ((np
->n_flag
& NMODIFIED
) ? 1 : 0));
2618 if (NFS_BITMAP_ISSET(&bitmap
, NFS_FATTR_SIZE
))
2619 nfsm_chain_add_64(error
, &nmrep
, np
->n_size
);
2620 if (NFS_BITMAP_ISSET(&bitmap
, NFS_FATTR_TIME_MODIFY
)) {
2621 nfsm_chain_add_64(error
, &nmrep
, np
->n_vattr
.nva_timesec
[NFSTIME_MODIFY
]);
2622 nfsm_chain_add_32(error
, &nmrep
, np
->n_vattr
.nva_timensec
[NFSTIME_MODIFY
]);
2624 nfs_node_unlock(np
);
2625 vnode_put(NFSTOV(np
));
2629 * If we hit an error building the reply, we can't easily back up.
2630 * So we'll just update the status and hope the server ignores the
2634 case NFS_OP_CB_RECALL
:
2635 // (STATEID, TRUNCATE, FH) -> (STATUS)
2637 nfsm_chain_get_stateid(error
, &nmreq
, &stateid
);
2638 nfsm_chain_get_32(error
, &nmreq
, truncate
);
2639 nfsm_chain_get_fh(error
, &nmreq
, NFS_VER4
, &fh
);
2643 numops
= 0; /* don't process any more ops */
2645 /* find the node for the file handle */
2646 error
= nfs_nget(nmp
->nm_mountp
, NULL
, NULL
, fh
.fh_data
, fh
.fh_len
, NULL
, NULL
, RPCAUTH_UNKNOWN
, NG_NOCREATE
, &np
);
2648 status
= NFSERR_BADHANDLE
;
2651 numops
= 0; /* don't process any more ops */
2652 } else if (!(np
->n_openflags
& N_DELEG_MASK
) ||
2653 bcmp(&np
->n_dstateid
, &stateid
, sizeof(stateid
))) {
2654 /* delegation stateid state doesn't match */
2655 status
= NFSERR_BAD_STATEID
;
2656 numops
= 0; /* don't process any more ops */
2658 if (!status
) /* add node to recall queue, and wake socket thread */
2659 nfs4_delegation_return_enqueue(np
);
2661 nfs_node_unlock(np
);
2662 vnode_put(NFSTOV(np
));
2665 nfsm_chain_add_32(error
, &nmrep
, op
);
2666 nfsm_chain_add_32(error
, &nmrep
, status
);
2667 if (!error
&& (status
== EBADRPC
))
2670 case NFS_OP_CB_ILLEGAL
:
2672 nfsm_chain_add_32(error
, &nmrep
, NFS_OP_CB_ILLEGAL
);
2673 status
= NFSERR_OP_ILLEGAL
;
2674 nfsm_chain_add_32(error
, &nmrep
, status
);
2675 numops
= 0; /* don't process any more ops */
2681 if (!status
&& error
) {
2682 if (error
== EBADRPC
)
2684 else if ((error
== ENOBUFS
) || (error
== ENOMEM
))
2685 status
= NFSERR_RESOURCE
;
2687 status
= NFSERR_SERVERFAULT
;
2691 /* Now, set the numres field */
2692 *pnumres
= txdr_unsigned(numres
);
2693 nfsm_chain_build_done(error
, &nmrep
);
2694 nfsm_chain_null(&nmrep
);
2696 /* drop the callback reference on the mount */
2697 lck_mtx_lock(nfs_global_mutex
);
2700 wakeup(&nmp
->nm_cbrefs
);
2701 lck_mtx_unlock(nfs_global_mutex
);
2706 if (status
== EBADRPC
)
2707 OSAddAtomic64(1, &nfsstats
.rpcinvalid
);
2709 /* build reply header */
2710 error
= mbuf_gethdr(MBUF_WAITOK
, MBUF_TYPE_DATA
, &mhead
);
2711 nfsm_chain_init(&nmrep
, mhead
);
2712 nfsm_chain_add_32(error
, &nmrep
, 0); /* insert space for an RPC record mark */
2713 nfsm_chain_add_32(error
, &nmrep
, xid
);
2714 nfsm_chain_add_32(error
, &nmrep
, RPC_REPLY
);
2715 if ((status
== ERPCMISMATCH
) || (status
& NFSERR_AUTHERR
)) {
2716 nfsm_chain_add_32(error
, &nmrep
, RPC_MSGDENIED
);
2717 if (status
& NFSERR_AUTHERR
) {
2718 nfsm_chain_add_32(error
, &nmrep
, RPC_AUTHERR
);
2719 nfsm_chain_add_32(error
, &nmrep
, (status
& ~NFSERR_AUTHERR
));
2721 nfsm_chain_add_32(error
, &nmrep
, RPC_MISMATCH
);
2722 nfsm_chain_add_32(error
, &nmrep
, RPC_VER2
);
2723 nfsm_chain_add_32(error
, &nmrep
, RPC_VER2
);
2727 nfsm_chain_add_32(error
, &nmrep
, RPC_MSGACCEPTED
);
2728 /* XXX RPCAUTH_NULL verifier */
2729 nfsm_chain_add_32(error
, &nmrep
, RPCAUTH_NULL
);
2730 nfsm_chain_add_32(error
, &nmrep
, 0);
2731 /* accepted status */
2734 nfsm_chain_add_32(error
, &nmrep
, RPC_PROGUNAVAIL
);
2737 nfsm_chain_add_32(error
, &nmrep
, RPC_PROGMISMATCH
);
2738 nfsm_chain_add_32(error
, &nmrep
, NFS4_CALLBACK_PROG_VERSION
);
2739 nfsm_chain_add_32(error
, &nmrep
, NFS4_CALLBACK_PROG_VERSION
);
2742 nfsm_chain_add_32(error
, &nmrep
, RPC_PROCUNAVAIL
);
2745 nfsm_chain_add_32(error
, &nmrep
, RPC_GARBAGE
);
2748 nfsm_chain_add_32(error
, &nmrep
, RPC_SUCCESS
);
2749 if (status
!= NFSERR_RETVOID
)
2750 nfsm_chain_add_32(error
, &nmrep
, status
);
2754 nfsm_chain_build_done(error
, &nmrep
);
2756 nfsm_chain_null(&nmrep
);
2759 error
= mbuf_setnext(nmrep
.nmc_mcur
, mrest
);
2761 printf("nfs cb: mbuf_setnext failed %d\n", error
);
2765 /* Calculate the size of the reply */
2767 for (m
= nmrep
.nmc_mhead
; m
; m
= mbuf_next(m
))
2768 replen
+= mbuf_len(m
);
2769 mbuf_pkthdr_setlen(mhead
, replen
);
2770 error
= mbuf_pkthdr_setrcvif(mhead
, NULL
);
2771 nfsm_chain_set_recmark(error
, &nmrep
, (replen
- NFSX_UNSIGNED
) | 0x80000000);
2772 nfsm_chain_null(&nmrep
);
2774 /* send the reply */
2775 bzero(&msg
, sizeof(msg
));
2776 error
= sock_sendmbuf(so
, &msg
, mhead
, 0, &sentlen
);
2778 if (!error
&& ((int)sentlen
!= replen
))
2779 error
= EWOULDBLOCK
;
2780 if (error
== EWOULDBLOCK
) /* inability to send response is considered fatal */
2784 nfsm_chain_cleanup(&nmrep
);
2796 * Initialize an nfs_rpc_record_state structure.
2799 nfs_rpc_record_state_init(struct nfs_rpc_record_state
*nrrsp
)
2801 bzero(nrrsp
, sizeof(*nrrsp
));
2802 nrrsp
->nrrs_markerleft
= sizeof(nrrsp
->nrrs_fragleft
);
2806 * Clean up an nfs_rpc_record_state structure.
2809 nfs_rpc_record_state_cleanup(struct nfs_rpc_record_state
*nrrsp
)
2811 if (nrrsp
->nrrs_m
) {
2812 mbuf_freem(nrrsp
->nrrs_m
);
2813 nrrsp
->nrrs_m
= nrrsp
->nrrs_mlast
= NULL
;
2818 * Read the next (marked) RPC record from the socket.
2820 * *recvp returns if any data was received.
2821 * *mp returns the next complete RPC record
2824 nfs_rpc_record_read(socket_t so
, struct nfs_rpc_record_state
*nrrsp
, int flags
, int *recvp
, mbuf_t
*mp
)
2835 /* read the TCP RPC record marker */
2836 while (!error
&& nrrsp
->nrrs_markerleft
) {
2837 aio
.iov_base
= ((char*)&nrrsp
->nrrs_fragleft
+
2838 sizeof(nrrsp
->nrrs_fragleft
) - nrrsp
->nrrs_markerleft
);
2839 aio
.iov_len
= nrrsp
->nrrs_markerleft
;
2840 bzero(&msg
, sizeof(msg
));
2843 error
= sock_receive(so
, &msg
, flags
, &rcvlen
);
2844 if (error
|| !rcvlen
)
2847 nrrsp
->nrrs_markerleft
-= rcvlen
;
2848 if (nrrsp
->nrrs_markerleft
)
2850 /* record marker complete */
2851 nrrsp
->nrrs_fragleft
= ntohl(nrrsp
->nrrs_fragleft
);
2852 if (nrrsp
->nrrs_fragleft
& 0x80000000) {
2853 nrrsp
->nrrs_lastfrag
= 1;
2854 nrrsp
->nrrs_fragleft
&= ~0x80000000;
2856 nrrsp
->nrrs_reclen
+= nrrsp
->nrrs_fragleft
;
2857 if (nrrsp
->nrrs_reclen
> NFS_MAXPACKET
) {
2858 /* This is SERIOUS! We are out of sync with the sender. */
2859 log(LOG_ERR
, "impossible RPC record length (%d) on callback", nrrsp
->nrrs_reclen
);
2864 /* read the TCP RPC record fragment */
2865 while (!error
&& !nrrsp
->nrrs_markerleft
&& nrrsp
->nrrs_fragleft
) {
2867 rcvlen
= nrrsp
->nrrs_fragleft
;
2868 error
= sock_receivembuf(so
, NULL
, &m
, flags
, &rcvlen
);
2869 if (error
|| !rcvlen
|| !m
)
2872 /* append mbufs to list */
2873 nrrsp
->nrrs_fragleft
-= rcvlen
;
2874 if (!nrrsp
->nrrs_m
) {
2877 error
= mbuf_setnext(nrrsp
->nrrs_mlast
, m
);
2879 printf("nfs tcp rcv: mbuf_setnext failed %d\n", error
);
2884 while (mbuf_next(m
))
2886 nrrsp
->nrrs_mlast
= m
;
2889 /* done reading fragment? */
2890 if (!error
&& !nrrsp
->nrrs_markerleft
&& !nrrsp
->nrrs_fragleft
) {
2891 /* reset socket fragment parsing state */
2892 nrrsp
->nrrs_markerleft
= sizeof(nrrsp
->nrrs_fragleft
);
2893 if (nrrsp
->nrrs_lastfrag
) {
2894 /* RPC record complete */
2895 *mp
= nrrsp
->nrrs_m
;
2896 /* reset socket record parsing state */
2897 nrrsp
->nrrs_reclen
= 0;
2898 nrrsp
->nrrs_m
= nrrsp
->nrrs_mlast
= NULL
;
2899 nrrsp
->nrrs_lastfrag
= 0;
2909 * The NFS client send routine.
2911 * Send the given NFS request out the mount's socket.
2912 * Holds nfs_sndlock() for the duration of this call.
2914 * - check for request termination (sigintr)
2915 * - wait for reconnect, if necessary
2916 * - UDP: check the congestion window
2917 * - make a copy of the request to send
2918 * - UDP: update the congestion window
2919 * - send the request
2921 * If sent successfully, R_MUSTRESEND and R_RESENDERR are cleared.
2922 * rexmit count is also updated if this isn't the first send.
2924 * If the send is not successful, make sure R_MUSTRESEND is set.
2925 * If this wasn't the first transmit, set R_RESENDERR.
2926 * Also, undo any UDP congestion window changes made.
2928 * If the error appears to indicate that the socket should
2929 * be reconnected, mark the socket for reconnection.
2931 * Only return errors when the request should be aborted.
2934 nfs_send(struct nfsreq
*req
, int wait
)
2936 struct nfsmount
*nmp
;
2937 struct nfs_socket
*nso
;
2938 int error
, error2
, sotype
, rexmit
, slpflag
= 0, needrecon
;
2940 struct sockaddr
*sendnam
;
2943 struct timespec ts
= { 2, 0 };
2946 error
= nfs_sndlock(req
);
2948 lck_mtx_lock(&req
->r_mtx
);
2949 req
->r_error
= error
;
2950 req
->r_flags
&= ~R_SENDING
;
2951 lck_mtx_unlock(&req
->r_mtx
);
2955 error
= nfs_sigintr(req
->r_nmp
, req
, NULL
, 0);
2958 lck_mtx_lock(&req
->r_mtx
);
2959 req
->r_error
= error
;
2960 req
->r_flags
&= ~R_SENDING
;
2961 lck_mtx_unlock(&req
->r_mtx
);
2965 sotype
= nmp
->nm_sotype
;
2968 * If it's a setup RPC but we're not in SETUP... must need reconnect.
2969 * If it's a recovery RPC but the socket's not ready... must need reconnect.
2971 if (((req
->r_flags
& R_SETUP
) && !(nmp
->nm_sockflags
& NMSOCK_SETUP
)) ||
2972 ((req
->r_flags
& R_RECOVER
) && !(nmp
->nm_sockflags
& NMSOCK_READY
))) {
2975 lck_mtx_lock(&req
->r_mtx
);
2976 req
->r_error
= error
;
2977 req
->r_flags
&= ~R_SENDING
;
2978 lck_mtx_unlock(&req
->r_mtx
);
2982 /* If the socket needs reconnection, do that now. */
2983 /* wait until socket is ready - unless this request is part of setup */
2984 lck_mtx_lock(&nmp
->nm_lock
);
2985 if (!(nmp
->nm_sockflags
& NMSOCK_READY
) &&
2986 !((nmp
->nm_sockflags
& NMSOCK_SETUP
) && (req
->r_flags
& R_SETUP
))) {
2987 if (NMFLAG(nmp
, INTR
) && !(req
->r_flags
& R_NOINTR
))
2989 lck_mtx_unlock(&nmp
->nm_lock
);
2992 lck_mtx_lock(&req
->r_mtx
);
2993 req
->r_flags
&= ~R_SENDING
;
2994 req
->r_flags
|= R_MUSTRESEND
;
2996 lck_mtx_unlock(&req
->r_mtx
);
2999 NFS_SOCK_DBG("nfs_send: 0x%llx wait reconnect\n", req
->r_xid
);
3000 lck_mtx_lock(&req
->r_mtx
);
3001 req
->r_flags
&= ~R_MUSTRESEND
;
3003 lck_mtx_unlock(&req
->r_mtx
);
3004 lck_mtx_lock(&nmp
->nm_lock
);
3005 while (!(nmp
->nm_sockflags
& NMSOCK_READY
)) {
3006 /* don't bother waiting if the socket thread won't be reconnecting it */
3007 if (nmp
->nm_state
& (NFSSTA_FORCE
|NFSSTA_DEAD
)) {
3011 if ((NMFLAG(nmp
, SOFT
) || (req
->r_flags
& R_SOFT
)) && (nmp
->nm_reconnect_start
> 0)) {
3014 if ((now
.tv_sec
- nmp
->nm_reconnect_start
) >= 8) {
3015 /* soft mount in reconnect for a while... terminate ASAP */
3016 OSAddAtomic64(1, &nfsstats
.rpctimeouts
);
3017 req
->r_flags
|= R_SOFTTERM
;
3018 req
->r_error
= error
= ETIMEDOUT
;
3022 /* make sure socket thread is running, then wait */
3023 nfs_mount_sock_thread_wake(nmp
);
3024 if ((error
= nfs_sigintr(req
->r_nmp
, req
, req
->r_thread
, 1)))
3026 msleep(req
, &nmp
->nm_lock
, slpflag
|PSOCK
, "nfsconnectwait", &ts
);
3029 lck_mtx_unlock(&nmp
->nm_lock
);
3031 lck_mtx_lock(&req
->r_mtx
);
3032 req
->r_error
= error
;
3033 req
->r_flags
&= ~R_SENDING
;
3034 lck_mtx_unlock(&req
->r_mtx
);
3040 /* note that we're using the mount's socket to do the send */
3041 nmp
->nm_state
|= NFSSTA_SENDING
; /* will be cleared by nfs_sndunlock() */
3042 lck_mtx_unlock(&nmp
->nm_lock
);
3045 lck_mtx_lock(&req
->r_mtx
);
3046 req
->r_flags
&= ~R_SENDING
;
3047 req
->r_flags
|= R_MUSTRESEND
;
3049 lck_mtx_unlock(&req
->r_mtx
);
3053 lck_mtx_lock(&req
->r_mtx
);
3054 rexmit
= (req
->r_flags
& R_SENT
);
3056 if (sotype
== SOCK_DGRAM
) {
3057 lck_mtx_lock(&nmp
->nm_lock
);
3058 if (!(req
->r_flags
& R_CWND
) && (nmp
->nm_sent
>= nmp
->nm_cwnd
)) {
3059 /* if we can't send this out yet, wait on the cwnd queue */
3060 slpflag
= (NMFLAG(nmp
, INTR
) && req
->r_thread
) ? PCATCH
: 0;
3061 lck_mtx_unlock(&nmp
->nm_lock
);
3063 req
->r_flags
&= ~R_SENDING
;
3064 req
->r_flags
|= R_MUSTRESEND
;
3065 lck_mtx_unlock(&req
->r_mtx
);
3070 lck_mtx_lock(&nmp
->nm_lock
);
3071 while (nmp
->nm_sent
>= nmp
->nm_cwnd
) {
3072 if ((error
= nfs_sigintr(req
->r_nmp
, req
, req
->r_thread
, 1)))
3074 TAILQ_INSERT_TAIL(&nmp
->nm_cwndq
, req
, r_cchain
);
3075 msleep(req
, &nmp
->nm_lock
, slpflag
| (PZERO
- 1), "nfswaitcwnd", &ts
);
3077 if ((req
->r_cchain
.tqe_next
!= NFSREQNOLIST
)) {
3078 TAILQ_REMOVE(&nmp
->nm_cwndq
, req
, r_cchain
);
3079 req
->r_cchain
.tqe_next
= NFSREQNOLIST
;
3082 lck_mtx_unlock(&nmp
->nm_lock
);
3086 * We update these *before* the send to avoid racing
3087 * against others who may be looking to send requests.
3090 /* first transmit */
3091 req
->r_flags
|= R_CWND
;
3092 nmp
->nm_sent
+= NFS_CWNDSCALE
;
3095 * When retransmitting, turn timing off
3096 * and divide congestion window by 2.
3098 req
->r_flags
&= ~R_TIMING
;
3100 if (nmp
->nm_cwnd
< NFS_CWNDSCALE
)
3101 nmp
->nm_cwnd
= NFS_CWNDSCALE
;
3103 lck_mtx_unlock(&nmp
->nm_lock
);
3106 req
->r_flags
&= ~R_MUSTRESEND
;
3107 lck_mtx_unlock(&req
->r_mtx
);
3109 error
= mbuf_copym(req
->r_mhead
, 0, MBUF_COPYALL
,
3110 wait
? MBUF_WAITOK
: MBUF_DONTWAIT
, &mreqcopy
);
3113 log(LOG_INFO
, "nfs_send: mbuf copy failed %d\n", error
);
3115 lck_mtx_lock(&req
->r_mtx
);
3116 req
->r_flags
&= ~R_SENDING
;
3117 req
->r_flags
|= R_MUSTRESEND
;
3119 lck_mtx_unlock(&req
->r_mtx
);
3123 bzero(&msg
, sizeof(msg
));
3124 if ((sotype
!= SOCK_STREAM
) && !sock_isconnected(nso
->nso_so
) && ((sendnam
= nmp
->nm_saddr
))) {
3125 msg
.msg_name
= (caddr_t
)sendnam
;
3126 msg
.msg_namelen
= sendnam
->sa_len
;
3128 error
= sock_sendmbuf(nso
->nso_so
, &msg
, mreqcopy
, 0, &sentlen
);
3129 if (error
|| (sentlen
!= req
->r_mreqlen
)) {
3130 NFS_SOCK_DBG("nfs_send: 0x%llx sent %d/%d error %d\n",
3131 req
->r_xid
, (int)sentlen
, (int)req
->r_mreqlen
, error
);
3134 if (!error
&& (sentlen
!= req
->r_mreqlen
))
3135 error
= EWOULDBLOCK
;
3136 needrecon
= ((sotype
== SOCK_STREAM
) && sentlen
&& (sentlen
!= req
->r_mreqlen
));
3138 lck_mtx_lock(&req
->r_mtx
);
3139 req
->r_flags
&= ~R_SENDING
;
3141 if (rexmit
&& (++req
->r_rexmit
> NFS_MAXREXMIT
))
3142 req
->r_rexmit
= NFS_MAXREXMIT
;
3146 req
->r_flags
&= ~R_RESENDERR
;
3148 OSAddAtomic64(1, &nfsstats
.rpcretries
);
3149 req
->r_flags
|= R_SENT
;
3150 if (req
->r_flags
& R_WAITSENT
) {
3151 req
->r_flags
&= ~R_WAITSENT
;
3155 lck_mtx_unlock(&req
->r_mtx
);
3160 req
->r_flags
|= R_MUSTRESEND
;
3162 req
->r_flags
|= R_RESENDERR
;
3163 if ((error
== EINTR
) || (error
== ERESTART
))
3164 req
->r_error
= error
;
3165 lck_mtx_unlock(&req
->r_mtx
);
3167 if (sotype
== SOCK_DGRAM
) {
3169 * Note: even though a first send may fail, we consider
3170 * the request sent for congestion window purposes.
3171 * So we don't need to undo any of the changes made above.
3174 * Socket errors ignored for connectionless sockets??
3175 * For now, ignore them all
3177 if ((error
!= EINTR
) && (error
!= ERESTART
) &&
3178 (error
!= EWOULDBLOCK
) && (error
!= EIO
) && (nso
== nmp
->nm_nso
)) {
3179 int clearerror
= 0, optlen
= sizeof(clearerror
);
3180 sock_getsockopt(nso
->nso_so
, SOL_SOCKET
, SO_ERROR
, &clearerror
, &optlen
);
3181 #ifdef NFS_SOCKET_DEBUGGING
3183 NFS_SOCK_DBG("nfs_send: ignoring UDP socket error %d so %d\n",
3189 /* check if it appears we should reconnect the socket */
3192 /* if send timed out, reconnect if on TCP */
3193 if (sotype
!= SOCK_STREAM
)
3210 if (needrecon
&& (nso
== nmp
->nm_nso
)) { /* mark socket as needing reconnect */
3211 NFS_SOCK_DBG("nfs_send: 0x%llx need reconnect %d\n", req
->r_xid
, error
);
3212 nfs_need_reconnect(nmp
);
3218 * Don't log some errors:
3219 * EPIPE errors may be common with servers that drop idle connections.
3220 * EADDRNOTAVAIL may occur on network transitions.
3221 * ENOTCONN may occur under some network conditions.
3223 if ((error
== EPIPE
) || (error
== EADDRNOTAVAIL
) || (error
== ENOTCONN
))
3225 if (error
&& (error
!= EINTR
) && (error
!= ERESTART
))
3226 log(LOG_INFO
, "nfs send error %d for server %s\n", error
,
3227 !req
->r_nmp
? "<unmounted>" :
3228 vfs_statfs(req
->r_nmp
->nm_mountp
)->f_mntfromname
);
3230 if (nfs_is_dead(error
, nmp
))
3233 /* prefer request termination error over other errors */
3234 error2
= nfs_sigintr(req
->r_nmp
, req
, req
->r_thread
, 0);
3238 /* only allow the following errors to be returned */
3239 if ((error
!= EINTR
) && (error
!= ERESTART
) && (error
!= EIO
) &&
3240 (error
!= ENXIO
) && (error
!= ETIMEDOUT
))
3246 * NFS client socket upcalls
3248 * Pull RPC replies out of an NFS mount's socket and match them
3249 * up with the pending request.
3251 * The datagram code is simple because we always get whole
3252 * messages out of the socket.
3254 * The stream code is more involved because we have to parse
3255 * the RPC records out of the stream.
3258 /* NFS client UDP socket upcall */
3260 nfs_udp_rcv(socket_t so
, void *arg
, __unused
int waitflag
)
3262 struct nfsmount
*nmp
= arg
;
3263 struct nfs_socket
*nso
= nmp
->nm_nso
;
3268 if (nmp
->nm_sockflags
& NMSOCK_CONNECTING
)
3272 /* make sure we're on the current socket */
3273 if (!nso
|| (nso
->nso_so
!= so
))
3278 error
= sock_receivembuf(so
, NULL
, &m
, MSG_DONTWAIT
, &rcvlen
);
3280 nfs_request_match_reply(nmp
, m
);
3281 } while (m
&& !error
);
3283 if (error
&& (error
!= EWOULDBLOCK
)) {
3284 /* problems with the socket... mark for reconnection */
3285 NFS_SOCK_DBG("nfs_udp_rcv: need reconnect %d\n", error
);
3286 nfs_need_reconnect(nmp
);
3290 /* NFS client TCP socket upcall */
3292 nfs_tcp_rcv(socket_t so
, void *arg
, __unused
int waitflag
)
3294 struct nfsmount
*nmp
= arg
;
3295 struct nfs_socket
*nso
= nmp
->nm_nso
;
3296 struct nfs_rpc_record_state nrrs
;
3302 if (nmp
->nm_sockflags
& NMSOCK_CONNECTING
)
3305 /* make sure we're on the current socket */
3306 lck_mtx_lock(&nmp
->nm_lock
);
3308 if (!nso
|| (nso
->nso_so
!= so
) || (nmp
->nm_sockflags
& (NMSOCK_DISCONNECTING
))) {
3309 lck_mtx_unlock(&nmp
->nm_lock
);
3312 lck_mtx_unlock(&nmp
->nm_lock
);
3314 /* make sure this upcall should be trying to do work */
3315 lck_mtx_lock(&nso
->nso_lock
);
3316 if (nso
->nso_flags
& (NSO_UPCALL
|NSO_DISCONNECTING
|NSO_DEAD
)) {
3317 lck_mtx_unlock(&nso
->nso_lock
);
3320 nso
->nso_flags
|= NSO_UPCALL
;
3321 nrrs
= nso
->nso_rrs
;
3322 lck_mtx_unlock(&nso
->nso_lock
);
3324 /* loop while we make error-free progress */
3325 while (!error
&& recv
) {
3326 error
= nfs_rpc_record_read(so
, &nrrs
, MSG_DONTWAIT
, &recv
, &m
);
3327 if (m
) /* match completed response with request */
3328 nfs_request_match_reply(nmp
, m
);
3331 /* Update the sockets's rpc parsing state */
3332 lck_mtx_lock(&nso
->nso_lock
);
3333 nso
->nso_rrs
= nrrs
;
3334 if (nso
->nso_flags
& NSO_DISCONNECTING
)
3336 nso
->nso_flags
&= ~NSO_UPCALL
;
3337 lck_mtx_unlock(&nso
->nso_lock
);
3339 wakeup(&nso
->nso_flags
);
3341 #ifdef NFS_SOCKET_DEBUGGING
3342 if (!recv
&& (error
!= EWOULDBLOCK
))
3343 NFS_SOCK_DBG("nfs_tcp_rcv: got nothing, error %d, got FIN?\n", error
);
3345 /* note: no error and no data indicates server closed its end */
3346 if ((error
!= EWOULDBLOCK
) && (error
|| !recv
)) {
3347 /* problems with the socket... mark for reconnection */
3348 NFS_SOCK_DBG("nfs_tcp_rcv: need reconnect %d\n", error
);
3349 nfs_need_reconnect(nmp
);
3354 * "poke" a socket to try to provoke any pending errors
3357 nfs_sock_poke(struct nfsmount
*nmp
)
3365 lck_mtx_lock(&nmp
->nm_lock
);
3366 if ((nmp
->nm_sockflags
& NMSOCK_UNMOUNT
) ||
3367 !(nmp
->nm_sockflags
& NMSOCK_READY
) || !nmp
->nm_nso
|| !nmp
->nm_nso
->nso_so
) {
3368 /* Nothing to poke */
3369 nmp
->nm_sockflags
&= ~NMSOCK_POKE
;
3370 wakeup(&nmp
->nm_sockflags
);
3371 lck_mtx_unlock(&nmp
->nm_lock
);
3374 lck_mtx_unlock(&nmp
->nm_lock
);
3375 aio
.iov_base
= &dummy
;
3378 bzero(&msg
, sizeof(msg
));
3381 error
= sock_send(nmp
->nm_nso
->nso_so
, &msg
, MSG_DONTWAIT
, &len
);
3382 NFS_SOCK_DBG("nfs_sock_poke: error %d\n", error
);
3383 lck_mtx_lock(&nmp
->nm_lock
);
3384 nmp
->nm_sockflags
&= ~NMSOCK_POKE
;
3385 wakeup(&nmp
->nm_sockflags
);
3386 lck_mtx_unlock(&nmp
->nm_lock
);
3387 nfs_is_dead(error
, nmp
);
3391 * Match an RPC reply with the corresponding request
3394 nfs_request_match_reply(struct nfsmount
*nmp
, mbuf_t mrep
)
3397 struct nfsm_chain nmrep
;
3398 u_int32_t reply
= 0, rxid
= 0;
3399 int error
= 0, asyncioq
, t1
;
3401 /* Get the xid and check that it is an rpc reply */
3402 nfsm_chain_dissect_init(error
, &nmrep
, mrep
);
3403 nfsm_chain_get_32(error
, &nmrep
, rxid
);
3404 nfsm_chain_get_32(error
, &nmrep
, reply
);
3405 if (error
|| (reply
!= RPC_REPLY
)) {
3406 OSAddAtomic64(1, &nfsstats
.rpcinvalid
);
3412 * Loop through the request list to match up the reply
3413 * Iff no match, just drop it.
3415 lck_mtx_lock(nfs_request_mutex
);
3416 TAILQ_FOREACH(req
, &nfs_reqq
, r_chain
) {
3417 if (req
->r_nmrep
.nmc_mhead
|| (rxid
!= R_XID32(req
->r_xid
)))
3419 /* looks like we have it, grab lock and double check */
3420 lck_mtx_lock(&req
->r_mtx
);
3421 if (req
->r_nmrep
.nmc_mhead
|| (rxid
!= R_XID32(req
->r_xid
))) {
3422 lck_mtx_unlock(&req
->r_mtx
);
3426 req
->r_nmrep
= nmrep
;
3427 lck_mtx_lock(&nmp
->nm_lock
);
3428 if (nmp
->nm_sotype
== SOCK_DGRAM
) {
3430 * Update congestion window.
3431 * Do the additive increase of one rpc/rtt.
3433 FSDBG(530, R_XID32(req
->r_xid
), req
, nmp
->nm_sent
, nmp
->nm_cwnd
);
3434 if (nmp
->nm_cwnd
<= nmp
->nm_sent
) {
3436 ((NFS_CWNDSCALE
* NFS_CWNDSCALE
) +
3437 (nmp
->nm_cwnd
>> 1)) / nmp
->nm_cwnd
;
3438 if (nmp
->nm_cwnd
> NFS_MAXCWND
)
3439 nmp
->nm_cwnd
= NFS_MAXCWND
;
3441 if (req
->r_flags
& R_CWND
) {
3442 nmp
->nm_sent
-= NFS_CWNDSCALE
;
3443 req
->r_flags
&= ~R_CWND
;
3445 if ((nmp
->nm_sent
< nmp
->nm_cwnd
) && !TAILQ_EMPTY(&nmp
->nm_cwndq
)) {
3446 /* congestion window is open, poke the cwnd queue */
3447 struct nfsreq
*req2
= TAILQ_FIRST(&nmp
->nm_cwndq
);
3448 TAILQ_REMOVE(&nmp
->nm_cwndq
, req2
, r_cchain
);
3449 req2
->r_cchain
.tqe_next
= NFSREQNOLIST
;
3454 * Update rtt using a gain of 0.125 on the mean
3455 * and a gain of 0.25 on the deviation.
3457 if (req
->r_flags
& R_TIMING
) {
3459 * Since the timer resolution of
3460 * NFS_HZ is so course, it can often
3461 * result in r_rtt == 0. Since
3462 * r_rtt == N means that the actual
3463 * rtt is between N+dt and N+2-dt ticks,
3466 if (proct
[req
->r_procnum
] == 0)
3467 panic("nfs_request_match_reply: proct[%d] is zero", req
->r_procnum
);
3468 t1
= req
->r_rtt
+ 1;
3469 t1
-= (NFS_SRTT(req
) >> 3);
3470 NFS_SRTT(req
) += t1
;
3473 t1
-= (NFS_SDRTT(req
) >> 2);
3474 NFS_SDRTT(req
) += t1
;
3476 nmp
->nm_timeouts
= 0;
3477 lck_mtx_unlock(&nmp
->nm_lock
);
3478 /* signal anyone waiting on this request */
3480 asyncioq
= (req
->r_callback
.rcb_func
!= NULL
);
3481 if (nfs_request_using_gss(req
))
3482 nfs_gss_clnt_rpcdone(req
);
3483 lck_mtx_unlock(&req
->r_mtx
);
3484 lck_mtx_unlock(nfs_request_mutex
);
3485 /* if it's an async RPC with a callback, queue it up */
3487 nfs_asyncio_finish(req
);
3492 /* not matched to a request, so drop it. */
3493 lck_mtx_unlock(nfs_request_mutex
);
3494 OSAddAtomic64(1, &nfsstats
.rpcunexpected
);
3500 * Wait for the reply for a given request...
3501 * ...potentially resending the request if necessary.
3504 nfs_wait_reply(struct nfsreq
*req
)
3506 struct timespec ts
= { 2, 0 };
3507 int error
= 0, slpflag
, first
= 1;
3509 if (req
->r_nmp
&& NMFLAG(req
->r_nmp
, INTR
) && req
->r_thread
&& !(req
->r_flags
& R_NOINTR
))
3514 lck_mtx_lock(&req
->r_mtx
);
3515 while (!req
->r_nmrep
.nmc_mhead
) {
3516 if ((error
= nfs_sigintr(req
->r_nmp
, req
, first
? NULL
: req
->r_thread
, 0)))
3518 if (((error
= req
->r_error
)) || req
->r_nmrep
.nmc_mhead
)
3520 /* check if we need to resend */
3521 if (req
->r_flags
& R_MUSTRESEND
) {
3522 NFS_SOCK_DBG("nfs wait resend: p %d x 0x%llx f 0x%x rtt %d\n",
3523 req
->r_procnum
, req
->r_xid
, req
->r_flags
, req
->r_rtt
);
3524 req
->r_flags
|= R_SENDING
;
3525 lck_mtx_unlock(&req
->r_mtx
);
3526 if (nfs_request_using_gss(req
)) {
3528 * It's an RPCSEC_GSS request.
3529 * Can't just resend the original request
3530 * without bumping the cred sequence number.
3531 * Go back and re-build the request.
3533 lck_mtx_lock(&req
->r_mtx
);
3534 req
->r_flags
&= ~R_SENDING
;
3535 lck_mtx_unlock(&req
->r_mtx
);
3538 error
= nfs_send(req
, 1);
3539 lck_mtx_lock(&req
->r_mtx
);
3540 NFS_SOCK_DBG("nfs wait resend: p %d x 0x%llx f 0x%x rtt %d err %d\n",
3541 req
->r_procnum
, req
->r_xid
, req
->r_flags
, req
->r_rtt
, error
);
3544 if (((error
= req
->r_error
)) || req
->r_nmrep
.nmc_mhead
)
3547 /* need to poll if we're P_NOREMOTEHANG */
3548 if (nfs_noremotehang(req
->r_thread
))
3550 msleep(req
, &req
->r_mtx
, slpflag
| (PZERO
- 1), "nfswaitreply", &ts
);
3551 first
= slpflag
= 0;
3553 lck_mtx_unlock(&req
->r_mtx
);
3559 * An NFS request goes something like this:
3560 * (nb: always frees up mreq mbuf list)
3561 * nfs_request_create()
3562 * - allocates a request struct if one is not provided
3563 * - initial fill-in of the request struct
3564 * nfs_request_add_header()
3565 * - add the RPC header
3566 * nfs_request_send()
3567 * - link it into list
3568 * - call nfs_send() for first transmit
3569 * nfs_request_wait()
3570 * - call nfs_wait_reply() to wait for the reply
3571 * nfs_request_finish()
3572 * - break down rpc header and return with error or nfs reply
3573 * pointed to by nmrep.
3574 * nfs_request_rele()
3575 * nfs_request_destroy()
3576 * - clean up the request struct
3577 * - free the request struct if it was allocated by nfs_request_create()
3581 * Set up an NFS request struct (allocating if no request passed in).
3586 mount_t mp
, /* used only if !np */
3587 struct nfsm_chain
*nmrest
,
3591 struct nfsreq
**reqp
)
3593 struct nfsreq
*req
, *newreq
= NULL
;
3594 struct nfsmount
*nmp
;
3598 /* allocate a new NFS request structure */
3599 MALLOC_ZONE(newreq
, struct nfsreq
*, sizeof(*newreq
), M_NFSREQ
, M_WAITOK
);
3601 mbuf_freem(nmrest
->nmc_mhead
);
3602 nmrest
->nmc_mhead
= NULL
;
3608 bzero(req
, sizeof(*req
));
3610 req
->r_flags
= R_ALLOCATED
;
3612 nmp
= VFSTONFS(np
? NFSTOMP(np
) : mp
);
3613 if (nfs_mount_gone(nmp
)) {
3615 FREE_ZONE(newreq
, sizeof(*newreq
), M_NFSREQ
);
3618 lck_mtx_lock(&nmp
->nm_lock
);
3619 if ((nmp
->nm_state
& (NFSSTA_FORCE
|NFSSTA_DEAD
)) &&
3620 (nmp
->nm_state
& NFSSTA_TIMEO
)) {
3621 lck_mtx_unlock(&nmp
->nm_lock
);
3622 mbuf_freem(nmrest
->nmc_mhead
);
3623 nmrest
->nmc_mhead
= NULL
;
3625 FREE_ZONE(newreq
, sizeof(*newreq
), M_NFSREQ
);
3629 if ((nmp
->nm_vers
!= NFS_VER4
) && (procnum
>= 0) && (procnum
< NFS_NPROCS
))
3630 OSAddAtomic64(1, &nfsstats
.rpccnt
[procnum
]);
3631 if ((nmp
->nm_vers
== NFS_VER4
) && (procnum
!= NFSPROC4_COMPOUND
) && (procnum
!= NFSPROC4_NULL
))
3632 panic("nfs_request: invalid NFSv4 RPC request %d\n", procnum
);
3634 lck_mtx_init(&req
->r_mtx
, nfs_request_grp
, LCK_ATTR_NULL
);
3638 req
->r_thread
= thd
;
3640 req
->r_flags
|= R_NOINTR
;
3641 if (IS_VALID_CRED(cred
)) {
3642 kauth_cred_ref(cred
);
3645 req
->r_procnum
= procnum
;
3646 if (proct
[procnum
] > 0)
3647 req
->r_flags
|= R_TIMING
;
3648 req
->r_nmrep
.nmc_mhead
= NULL
;
3649 SLIST_INIT(&req
->r_gss_seqlist
);
3650 req
->r_achain
.tqe_next
= NFSREQNOLIST
;
3651 req
->r_rchain
.tqe_next
= NFSREQNOLIST
;
3652 req
->r_cchain
.tqe_next
= NFSREQNOLIST
;
3654 /* set auth flavor to use for request */
3656 req
->r_auth
= RPCAUTH_NONE
;
3657 else if (req
->r_np
&& (req
->r_np
->n_auth
!= RPCAUTH_INVALID
))
3658 req
->r_auth
= req
->r_np
->n_auth
;
3660 req
->r_auth
= nmp
->nm_auth
;
3662 lck_mtx_unlock(&nmp
->nm_lock
);
3664 /* move the request mbuf chain to the nfsreq */
3665 req
->r_mrest
= nmrest
->nmc_mhead
;
3666 nmrest
->nmc_mhead
= NULL
;
3668 req
->r_flags
|= R_INITTED
;
3676 * Clean up and free an NFS request structure.
3679 nfs_request_destroy(struct nfsreq
*req
)
3681 struct nfsmount
*nmp
= req
->r_np
? NFSTONMP(req
->r_np
) : req
->r_nmp
;
3682 struct gss_seq
*gsp
, *ngsp
;
3683 int clearjbtimeo
= 0;
3684 struct timespec ts
= { 1, 0 };
3686 if (!req
|| !(req
->r_flags
& R_INITTED
))
3688 req
->r_flags
&= ~R_INITTED
;
3689 if (req
->r_lflags
& RL_QUEUED
)
3690 nfs_reqdequeue(req
);
3692 if (req
->r_achain
.tqe_next
!= NFSREQNOLIST
&&
3693 req
->r_achain
.tqe_next
!= NFSIODCOMPLETING
) {
3695 * Still on an async I/O queue?
3696 * %%% But which one, we may be on a local iod.
3698 lck_mtx_lock(nfsiod_mutex
);
3699 if (nmp
&& req
->r_achain
.tqe_next
!= NFSREQNOLIST
&&
3700 req
->r_achain
.tqe_next
!= NFSIODCOMPLETING
) {
3701 TAILQ_REMOVE(&nmp
->nm_iodq
, req
, r_achain
);
3702 req
->r_achain
.tqe_next
= NFSREQNOLIST
;
3704 lck_mtx_unlock(nfsiod_mutex
);
3707 lck_mtx_lock(&req
->r_mtx
);
3709 lck_mtx_lock(&nmp
->nm_lock
);
3710 if (req
->r_flags
& R_CWND
) {
3711 /* Decrement the outstanding request count. */
3712 req
->r_flags
&= ~R_CWND
;
3713 nmp
->nm_sent
-= NFS_CWNDSCALE
;
3714 if ((nmp
->nm_sent
< nmp
->nm_cwnd
) && !TAILQ_EMPTY(&nmp
->nm_cwndq
)) {
3715 /* congestion window is open, poke the cwnd queue */
3716 struct nfsreq
*req2
= TAILQ_FIRST(&nmp
->nm_cwndq
);
3717 TAILQ_REMOVE(&nmp
->nm_cwndq
, req2
, r_cchain
);
3718 req2
->r_cchain
.tqe_next
= NFSREQNOLIST
;
3722 if (req
->r_rchain
.tqe_next
!= NFSREQNOLIST
) {
3723 TAILQ_REMOVE(&nmp
->nm_resendq
, req
, r_rchain
);
3724 req
->r_rchain
.tqe_next
= NFSREQNOLIST
;
3725 if (req
->r_flags
& R_RESENDQ
)
3726 req
->r_flags
&= ~R_RESENDQ
;
3728 if (req
->r_cchain
.tqe_next
!= NFSREQNOLIST
) {
3729 TAILQ_REMOVE(&nmp
->nm_cwndq
, req
, r_cchain
);
3730 req
->r_cchain
.tqe_next
= NFSREQNOLIST
;
3732 if (req
->r_flags
& R_JBTPRINTFMSG
) {
3733 req
->r_flags
&= ~R_JBTPRINTFMSG
;
3735 clearjbtimeo
= (nmp
->nm_jbreqs
== 0) ? NFSSTA_JUKEBOXTIMEO
: 0;
3737 lck_mtx_unlock(&nmp
->nm_lock
);
3739 /* Wait for the mount_sock_thread to finish with the resend */
3740 while (req
->r_flags
& R_RESENDQ
)
3741 msleep(req
, &req
->r_mtx
, (PZERO
- 1), "nfsresendqwait", &ts
);
3742 lck_mtx_unlock(&req
->r_mtx
);
3745 nfs_up(nmp
, req
->r_thread
, clearjbtimeo
, NULL
);
3747 mbuf_freem(req
->r_mhead
);
3748 else if (req
->r_mrest
)
3749 mbuf_freem(req
->r_mrest
);
3750 if (req
->r_nmrep
.nmc_mhead
)
3751 mbuf_freem(req
->r_nmrep
.nmc_mhead
);
3752 if (IS_VALID_CRED(req
->r_cred
))
3753 kauth_cred_unref(&req
->r_cred
);
3754 if (nfs_request_using_gss(req
))
3755 nfs_gss_clnt_rpcdone(req
);
3756 SLIST_FOREACH_SAFE(gsp
, &req
->r_gss_seqlist
, gss_seqnext
, ngsp
)
3759 nfs_gss_clnt_ctx_unref(req
);
3760 if (req
->r_wrongsec
)
3761 FREE(req
->r_wrongsec
, M_TEMP
);
3763 nfs_mount_rele(nmp
);
3764 lck_mtx_destroy(&req
->r_mtx
, nfs_request_grp
);
3765 if (req
->r_flags
& R_ALLOCATED
)
3766 FREE_ZONE(req
, sizeof(*req
), M_NFSREQ
);
3770 nfs_request_ref(struct nfsreq
*req
, int locked
)
3773 lck_mtx_lock(&req
->r_mtx
);
3774 if (req
->r_refs
<= 0)
3775 panic("nfsreq reference error");
3778 lck_mtx_unlock(&req
->r_mtx
);
3782 nfs_request_rele(struct nfsreq
*req
)
3786 lck_mtx_lock(&req
->r_mtx
);
3787 if (req
->r_refs
<= 0)
3788 panic("nfsreq reference underflow");
3790 destroy
= (req
->r_refs
== 0);
3791 lck_mtx_unlock(&req
->r_mtx
);
3793 nfs_request_destroy(req
);
3798 * Add an (updated) RPC header with authorization to an NFS request.
3801 nfs_request_add_header(struct nfsreq
*req
)
3803 struct nfsmount
*nmp
;
3807 /* free up any previous header */
3808 if ((m
= req
->r_mhead
)) {
3809 while (m
&& (m
!= req
->r_mrest
))
3811 req
->r_mhead
= NULL
;
3814 nmp
= req
->r_np
? NFSTONMP(req
->r_np
) : req
->r_nmp
;
3815 if (nfs_mount_gone(nmp
))
3818 error
= nfsm_rpchead(req
, req
->r_mrest
, &req
->r_xid
, &req
->r_mhead
);
3822 req
->r_mreqlen
= mbuf_pkthdr_len(req
->r_mhead
);
3823 nmp
= req
->r_np
? NFSTONMP(req
->r_np
) : req
->r_nmp
;
3824 if (nfs_mount_gone(nmp
))
3826 lck_mtx_lock(&nmp
->nm_lock
);
3827 if (NMFLAG(nmp
, SOFT
) || (req
->r_flags
& R_SOFT
))
3828 req
->r_retry
= nmp
->nm_retry
;
3830 req
->r_retry
= NFS_MAXREXMIT
+ 1; /* past clip limit */
3831 lck_mtx_unlock(&nmp
->nm_lock
);
3838 * Queue an NFS request up and send it out.
3841 nfs_request_send(struct nfsreq
*req
, int wait
)
3843 struct nfsmount
*nmp
;
3846 lck_mtx_lock(&req
->r_mtx
);
3847 req
->r_flags
|= R_SENDING
;
3848 lck_mtx_unlock(&req
->r_mtx
);
3850 lck_mtx_lock(nfs_request_mutex
);
3852 nmp
= req
->r_np
? NFSTONMP(req
->r_np
) : req
->r_nmp
;
3853 if (nfs_mount_gone(nmp
)) {
3854 lck_mtx_unlock(nfs_request_mutex
);
3859 if (!req
->r_start
) {
3860 req
->r_start
= now
.tv_sec
;
3861 req
->r_lastmsg
= now
.tv_sec
-
3862 ((nmp
->nm_tprintf_delay
) - (nmp
->nm_tprintf_initial_delay
));
3865 OSAddAtomic64(1, &nfsstats
.rpcrequests
);
3868 * Chain request into list of outstanding requests. Be sure
3869 * to put it LAST so timer finds oldest requests first.
3870 * Make sure that the request queue timer is running
3871 * to check for possible request timeout.
3873 TAILQ_INSERT_TAIL(&nfs_reqq
, req
, r_chain
);
3874 req
->r_lflags
|= RL_QUEUED
;
3875 if (!nfs_request_timer_on
) {
3876 nfs_request_timer_on
= 1;
3877 nfs_interval_timer_start(nfs_request_timer_call
,
3880 lck_mtx_unlock(nfs_request_mutex
);
3882 /* Send the request... */
3883 return (nfs_send(req
, wait
));
3887 * Call nfs_wait_reply() to wait for the reply.
3890 nfs_request_wait(struct nfsreq
*req
)
3892 req
->r_error
= nfs_wait_reply(req
);
3896 * Finish up an NFS request by dequeueing it and
3897 * doing the initial NFS request reply processing.
3902 struct nfsm_chain
*nmrepp
,
3905 struct nfsmount
*nmp
;
3908 uint32_t verf_len
= 0;
3909 uint32_t reply_status
= 0;
3910 uint32_t rejected_status
= 0;
3911 uint32_t auth_status
= 0;
3912 uint32_t accepted_status
= 0;
3913 struct nfsm_chain nmrep
;
3914 int error
, clearjbtimeo
;
3916 error
= req
->r_error
;
3919 nmrepp
->nmc_mhead
= NULL
;
3921 /* RPC done, unlink the request. */
3922 nfs_reqdequeue(req
);
3924 mrep
= req
->r_nmrep
.nmc_mhead
;
3926 nmp
= req
->r_np
? NFSTONMP(req
->r_np
) : req
->r_nmp
;
3928 if ((req
->r_flags
& R_CWND
) && nmp
) {
3930 * Decrement the outstanding request count.
3932 req
->r_flags
&= ~R_CWND
;
3933 lck_mtx_lock(&nmp
->nm_lock
);
3934 FSDBG(273, R_XID32(req
->r_xid
), req
, nmp
->nm_sent
, nmp
->nm_cwnd
);
3935 nmp
->nm_sent
-= NFS_CWNDSCALE
;
3936 if ((nmp
->nm_sent
< nmp
->nm_cwnd
) && !TAILQ_EMPTY(&nmp
->nm_cwndq
)) {
3937 /* congestion window is open, poke the cwnd queue */
3938 struct nfsreq
*req2
= TAILQ_FIRST(&nmp
->nm_cwndq
);
3939 TAILQ_REMOVE(&nmp
->nm_cwndq
, req2
, r_cchain
);
3940 req2
->r_cchain
.tqe_next
= NFSREQNOLIST
;
3943 lck_mtx_unlock(&nmp
->nm_lock
);
3946 if (nfs_request_using_gss(req
)) {
3948 * If the request used an RPCSEC_GSS credential
3949 * then reset its sequence number bit in the
3952 nfs_gss_clnt_rpcdone(req
);
3955 * If we need to re-send, go back and re-build the
3956 * request based on a new sequence number.
3957 * Note that we're using the original XID.
3959 if (error
== EAGAIN
) {
3963 error
= nfs_gss_clnt_args_restore(req
); // remove any trailer mbufs
3964 req
->r_nmrep
.nmc_mhead
= NULL
;
3965 req
->r_flags
|= R_RESTART
;
3966 if (error
== ENEEDAUTH
) {
3967 req
->r_xid
= 0; // get a new XID
3975 * If there was a successful reply, make sure to mark the mount as up.
3976 * If a tprintf message was given (or if this is a timed-out soft mount)
3977 * then post a tprintf message indicating the server is alive again.
3980 if ((req
->r_flags
& R_TPRINTFMSG
) ||
3981 (nmp
&& (NMFLAG(nmp
, SOFT
) || (req
->r_flags
& R_SOFT
)) &&
3982 ((nmp
->nm_state
& (NFSSTA_TIMEO
|NFSSTA_FORCE
|NFSSTA_DEAD
)) == NFSSTA_TIMEO
)))
3983 nfs_up(nmp
, req
->r_thread
, NFSSTA_TIMEO
, "is alive again");
3985 nfs_up(nmp
, req
->r_thread
, NFSSTA_TIMEO
, NULL
);
3992 * break down the RPC header and check if ok
3994 nmrep
= req
->r_nmrep
;
3995 nfsm_chain_get_32(error
, &nmrep
, reply_status
);
3997 if (reply_status
== RPC_MSGDENIED
) {
3998 nfsm_chain_get_32(error
, &nmrep
, rejected_status
);
4000 if (rejected_status
== RPC_MISMATCH
) {
4004 nfsm_chain_get_32(error
, &nmrep
, auth_status
);
4006 switch (auth_status
) {
4007 case RPCSEC_GSS_CREDPROBLEM
:
4008 case RPCSEC_GSS_CTXPROBLEM
:
4010 * An RPCSEC_GSS cred or context problem.
4011 * We can't use it anymore.
4012 * Restore the args, renew the context
4013 * and set up for a resend.
4015 error
= nfs_gss_clnt_args_restore(req
);
4016 if (error
&& error
!= ENEEDAUTH
)
4020 error
= nfs_gss_clnt_ctx_renew(req
);
4025 req
->r_nmrep
.nmc_mhead
= NULL
;
4026 req
->r_xid
= 0; // get a new XID
4027 req
->r_flags
|= R_RESTART
;
4036 /* Now check the verifier */
4037 nfsm_chain_get_32(error
, &nmrep
, verf_type
); // verifier flavor
4038 nfsm_chain_get_32(error
, &nmrep
, verf_len
); // verifier length
4041 switch (req
->r_auth
) {
4044 /* Any AUTH_SYS verifier is ignored */
4046 nfsm_chain_adv(error
, &nmrep
, nfsm_rndup(verf_len
));
4047 nfsm_chain_get_32(error
, &nmrep
, accepted_status
);
4052 error
= nfs_gss_clnt_verf_get(req
, &nmrep
,
4053 verf_type
, verf_len
, &accepted_status
);
4058 switch (accepted_status
) {
4060 if (req
->r_procnum
== NFSPROC_NULL
) {
4062 * The NFS null procedure is unique,
4063 * in not returning an NFS status.
4067 nfsm_chain_get_32(error
, &nmrep
, *status
);
4071 if ((nmp
->nm_vers
!= NFS_VER2
) && (*status
== NFSERR_TRYLATER
)) {
4073 * It's a JUKEBOX error - delay and try again
4075 int delay
, slpflag
= (NMFLAG(nmp
, INTR
) && !(req
->r_flags
& R_NOINTR
)) ? PCATCH
: 0;
4078 req
->r_nmrep
.nmc_mhead
= NULL
;
4079 if ((req
->r_delay
>= 30) && !(nmp
->nm_state
& NFSSTA_MOUNTED
)) {
4080 /* we're not yet completely mounted and */
4081 /* we can't complete an RPC, so we fail */
4082 OSAddAtomic64(1, &nfsstats
.rpctimeouts
);
4084 error
= req
->r_error
;
4087 req
->r_delay
= !req
->r_delay
? NFS_TRYLATERDEL
: (req
->r_delay
* 2);
4088 if (req
->r_delay
> 30)
4090 if (nmp
->nm_tprintf_initial_delay
&& (req
->r_delay
>= nmp
->nm_tprintf_initial_delay
)) {
4091 if (!(req
->r_flags
& R_JBTPRINTFMSG
)) {
4092 req
->r_flags
|= R_JBTPRINTFMSG
;
4093 lck_mtx_lock(&nmp
->nm_lock
);
4095 lck_mtx_unlock(&nmp
->nm_lock
);
4097 nfs_down(req
->r_nmp
, req
->r_thread
, 0, NFSSTA_JUKEBOXTIMEO
,
4098 "resource temporarily unavailable (jukebox)", 0);
4100 if ((NMFLAG(nmp
, SOFT
) || (req
->r_flags
& R_SOFT
)) && (req
->r_delay
== 30) &&
4101 !(req
->r_flags
& R_NOINTR
)) {
4102 /* for soft mounts, just give up after a short while */
4103 OSAddAtomic64(1, &nfsstats
.rpctimeouts
);
4105 error
= req
->r_error
;
4108 delay
= req
->r_delay
;
4109 if (req
->r_callback
.rcb_func
) {
4112 req
->r_resendtime
= now
.tv_sec
+ delay
;
4115 if ((error
= nfs_sigintr(req
->r_nmp
, req
, req
->r_thread
, 0)))
4117 tsleep(nfs_request_finish
, PSOCK
|slpflag
, "nfs_jukebox_trylater", hz
);
4119 } while (--delay
> 0);
4121 req
->r_xid
= 0; // get a new XID
4122 req
->r_flags
|= R_RESTART
;
4124 FSDBG(273, R_XID32(req
->r_xid
), nmp
, req
, NFSERR_TRYLATER
);
4128 if (req
->r_flags
& R_JBTPRINTFMSG
) {
4129 req
->r_flags
&= ~R_JBTPRINTFMSG
;
4130 lck_mtx_lock(&nmp
->nm_lock
);
4132 clearjbtimeo
= (nmp
->nm_jbreqs
== 0) ? NFSSTA_JUKEBOXTIMEO
: 0;
4133 lck_mtx_unlock(&nmp
->nm_lock
);
4134 nfs_up(nmp
, req
->r_thread
, clearjbtimeo
, "resource available again");
4137 if ((nmp
->nm_vers
>= NFS_VER4
) && (*status
== NFSERR_WRONGSEC
)) {
4139 * Hmmm... we need to try a different security flavor.
4140 * The first time a request hits this, we will allocate an array
4141 * to track flavors to try. We fill the array with the mount's
4142 * preferred flavors or the server's preferred flavors or just the
4143 * flavors we support.
4145 uint32_t srvflavors
[NX_MAX_SEC_FLAVORS
];
4148 /* Call SECINFO to try to get list of flavors from server. */
4149 srvcount
= NX_MAX_SEC_FLAVORS
;
4150 nfs4_secinfo_rpc(nmp
, &req
->r_secinfo
, req
->r_cred
, srvflavors
, &srvcount
);
4152 if (!req
->r_wrongsec
) {
4153 /* first time... set up flavor array */
4154 MALLOC(req
->r_wrongsec
, uint32_t*, NX_MAX_SEC_FLAVORS
*sizeof(uint32_t), M_TEMP
, M_WAITOK
);
4155 if (!req
->r_wrongsec
) {
4160 if (nmp
->nm_sec
.count
) { /* use the mount's preferred list of flavors */
4161 for(; i
< nmp
->nm_sec
.count
; i
++)
4162 req
->r_wrongsec
[i
] = nmp
->nm_sec
.flavors
[i
];
4163 } else if (srvcount
) { /* otherwise use the server's list of flavors */
4164 for(; i
< srvcount
; i
++)
4165 req
->r_wrongsec
[i
] = srvflavors
[i
];
4166 } else { /* otherwise, just try the flavors we support. */
4167 req
->r_wrongsec
[i
++] = RPCAUTH_KRB5P
;
4168 req
->r_wrongsec
[i
++] = RPCAUTH_KRB5I
;
4169 req
->r_wrongsec
[i
++] = RPCAUTH_KRB5
;
4170 req
->r_wrongsec
[i
++] = RPCAUTH_SYS
;
4171 req
->r_wrongsec
[i
++] = RPCAUTH_NONE
;
4173 for(; i
< NX_MAX_SEC_FLAVORS
; i
++) /* invalidate any remaining slots */
4174 req
->r_wrongsec
[i
] = RPCAUTH_INVALID
;
4177 /* clear the current flavor from the list */
4178 for(i
=0; i
< NX_MAX_SEC_FLAVORS
; i
++)
4179 if (req
->r_wrongsec
[i
] == req
->r_auth
)
4180 req
->r_wrongsec
[i
] = RPCAUTH_INVALID
;
4182 /* find the next flavor to try */
4183 for(i
=0; i
< NX_MAX_SEC_FLAVORS
; i
++)
4184 if (req
->r_wrongsec
[i
] != RPCAUTH_INVALID
) {
4185 if (!srvcount
) /* no server list, just try it */
4187 /* check that it's in the server's list */
4188 for(j
=0; j
< srvcount
; j
++)
4189 if (req
->r_wrongsec
[i
] == srvflavors
[j
])
4191 if (j
< srvcount
) /* found */
4193 /* not found in server list */
4194 req
->r_wrongsec
[i
] = RPCAUTH_INVALID
;
4196 if (i
== NX_MAX_SEC_FLAVORS
) {
4197 /* nothing left to try! */
4202 /* retry with the next auth flavor */
4203 req
->r_auth
= req
->r_wrongsec
[i
];
4204 req
->r_xid
= 0; // get a new XID
4205 req
->r_flags
|= R_RESTART
;
4207 FSDBG(273, R_XID32(req
->r_xid
), nmp
, req
, NFSERR_WRONGSEC
);
4210 if ((nmp
->nm_vers
>= NFS_VER4
) && req
->r_wrongsec
) {
4212 * We renegotiated security for this request; so update the
4213 * default security flavor for the associated node.
4216 req
->r_np
->n_auth
= req
->r_auth
;
4219 if (*status
== NFS_OK
) {
4221 * Successful NFS request
4224 req
->r_nmrep
.nmc_mhead
= NULL
;
4227 /* Got an NFS error of some kind */
4230 * If the File Handle was stale, invalidate the
4231 * lookup cache, just in case.
4233 if ((*status
== ESTALE
) && req
->r_np
) {
4234 cache_purge(NFSTOV(req
->r_np
));
4235 /* if monitored, also send delete event */
4236 if (vnode_ismonitored(NFSTOV(req
->r_np
)))
4237 nfs_vnode_notify(req
->r_np
, (VNODE_EVENT_ATTRIB
|VNODE_EVENT_DELETE
));
4239 if (nmp
->nm_vers
== NFS_VER2
)
4243 req
->r_nmrep
.nmc_mhead
= NULL
;
4246 case RPC_PROGUNAVAIL
:
4247 error
= EPROGUNAVAIL
;
4249 case RPC_PROGMISMATCH
:
4250 error
= ERPCMISMATCH
;
4252 case RPC_PROCUNAVAIL
:
4253 error
= EPROCUNAVAIL
;
4258 case RPC_SYSTEM_ERR
:
4264 if (req
->r_flags
& R_JBTPRINTFMSG
) {
4265 req
->r_flags
&= ~R_JBTPRINTFMSG
;
4266 lck_mtx_lock(&nmp
->nm_lock
);
4268 clearjbtimeo
= (nmp
->nm_jbreqs
== 0) ? NFSSTA_JUKEBOXTIMEO
: 0;
4269 lck_mtx_unlock(&nmp
->nm_lock
);
4271 nfs_up(nmp
, req
->r_thread
, clearjbtimeo
, NULL
);
4273 FSDBG(273, R_XID32(req
->r_xid
), nmp
, req
,
4274 (!error
&& (*status
== NFS_OK
)) ? 0xf0f0f0f0 : error
);
4279 * NFS request using a GSS/Kerberos security flavor?
4282 nfs_request_using_gss(struct nfsreq
*req
)
4284 if (!req
->r_gss_ctx
)
4286 switch (req
->r_auth
) {
4296 * Perform an NFS request synchronously.
4302 mount_t mp
, /* used only if !np */
4303 struct nfsm_chain
*nmrest
,
4306 struct nfsreq_secinfo_args
*si
,
4307 struct nfsm_chain
*nmrepp
,
4311 return nfs_request2(np
, mp
, nmrest
, procnum
,
4312 vfs_context_thread(ctx
), vfs_context_ucred(ctx
),
4313 si
, 0, nmrepp
, xidp
, status
);
4319 mount_t mp
, /* used only if !np */
4320 struct nfsm_chain
*nmrest
,
4324 struct nfsreq_secinfo_args
*si
,
4326 struct nfsm_chain
*nmrepp
,
4330 struct nfsreq rq
, *req
= &rq
;
4333 if ((error
= nfs_request_create(np
, mp
, nmrest
, procnum
, thd
, cred
, &req
)))
4335 req
->r_flags
|= (flags
& (R_OPTMASK
| R_SOFT
));
4337 req
->r_secinfo
= *si
;
4339 FSDBG_TOP(273, R_XID32(req
->r_xid
), np
, procnum
, 0);
4342 req
->r_flags
&= ~R_RESTART
;
4343 if ((error
= nfs_request_add_header(req
)))
4347 if ((error
= nfs_request_send(req
, 1)))
4349 nfs_request_wait(req
);
4350 if ((error
= nfs_request_finish(req
, nmrepp
, status
)))
4352 } while (req
->r_flags
& R_RESTART
);
4354 FSDBG_BOT(273, R_XID32(req
->r_xid
), np
, procnum
, error
);
4355 nfs_request_rele(req
);
4361 * Set up a new null proc request to exchange GSS context tokens with the
4362 * server. Associate the context that we are setting up with the request that we
4369 struct nfsm_chain
*nmrest
,
4373 struct nfs_gss_clnt_ctx
*cp
, /* Set to gss context to renew or setup */
4374 struct nfsm_chain
*nmrepp
,
4377 struct nfsreq rq
, *req
= &rq
;
4378 int error
, wait
= 1;
4380 if ((error
= nfs_request_create(NULL
, mp
, nmrest
, NFSPROC_NULL
, thd
, cred
, &req
)))
4382 req
->r_flags
|= (flags
& R_OPTMASK
);
4385 printf("nfs_request_gss request has no context\n");
4386 nfs_request_rele(req
);
4387 return (NFSERR_EAUTH
);
4389 nfs_gss_clnt_ctx_ref(req
, cp
);
4392 * Don't wait for a reply to a context destroy advisory
4393 * to avoid hanging on a dead server.
4395 if (cp
->gss_clnt_proc
== RPCSEC_GSS_DESTROY
)
4398 FSDBG_TOP(273, R_XID32(req
->r_xid
), NULL
, NFSPROC_NULL
, 0);
4401 req
->r_flags
&= ~R_RESTART
;
4402 if ((error
= nfs_request_add_header(req
)))
4405 if ((error
= nfs_request_send(req
, wait
)))
4410 nfs_request_wait(req
);
4411 if ((error
= nfs_request_finish(req
, nmrepp
, status
)))
4413 } while (req
->r_flags
& R_RESTART
);
4415 FSDBG_BOT(273, R_XID32(req
->r_xid
), NULL
, NFSPROC_NULL
, error
);
4417 nfs_gss_clnt_ctx_unref(req
);
4418 nfs_request_rele(req
);
4424 * Create and start an asynchronous NFS request.
4429 mount_t mp
, /* used only if !np */
4430 struct nfsm_chain
*nmrest
,
4434 struct nfsreq_secinfo_args
*si
,
4436 struct nfsreq_cbinfo
*cb
,
4437 struct nfsreq
**reqp
)
4440 struct nfsmount
*nmp
;
4443 error
= nfs_request_create(np
, mp
, nmrest
, procnum
, thd
, cred
, reqp
);
4445 FSDBG(274, (req
? R_XID32(req
->r_xid
) : 0), np
, procnum
, error
);
4448 req
->r_flags
|= (flags
& R_OPTMASK
);
4449 req
->r_flags
|= R_ASYNC
;
4451 req
->r_secinfo
= *si
;
4453 req
->r_callback
= *cb
;
4454 error
= nfs_request_add_header(req
);
4456 req
->r_flags
|= R_WAITSENT
;
4457 if (req
->r_callback
.rcb_func
)
4458 nfs_request_ref(req
, 0);
4459 error
= nfs_request_send(req
, 1);
4460 lck_mtx_lock(&req
->r_mtx
);
4461 if (!error
&& !(req
->r_flags
& R_SENT
) && req
->r_callback
.rcb_func
) {
4462 /* make sure to wait until this async I/O request gets sent */
4463 int slpflag
= (req
->r_nmp
&& NMFLAG(req
->r_nmp
, INTR
) && req
->r_thread
&& !(req
->r_flags
& R_NOINTR
)) ? PCATCH
: 0;
4464 struct timespec ts
= { 2, 0 };
4465 while (!(req
->r_flags
& R_SENT
)) {
4467 if ((req
->r_flags
& R_RESENDQ
) && !nfs_mount_gone(nmp
)) {
4468 lck_mtx_lock(&nmp
->nm_lock
);
4469 if ((nmp
->nm_state
& NFSSTA_RECOVER
) && (req
->r_rchain
.tqe_next
!= NFSREQNOLIST
)) {
4471 * It's not going to get off the resend queue if we're in recovery.
4472 * So, just take it off ourselves. We could be holding mount state
4473 * busy and thus holding up the start of recovery.
4475 TAILQ_REMOVE(&nmp
->nm_resendq
, req
, r_rchain
);
4476 req
->r_rchain
.tqe_next
= NFSREQNOLIST
;
4477 if (req
->r_flags
& R_RESENDQ
)
4478 req
->r_flags
&= ~R_RESENDQ
;
4479 lck_mtx_unlock(&nmp
->nm_lock
);
4480 req
->r_flags
|= R_SENDING
;
4481 lck_mtx_unlock(&req
->r_mtx
);
4482 error
= nfs_send(req
, 1);
4483 lck_mtx_lock(&req
->r_mtx
);
4488 lck_mtx_unlock(&nmp
->nm_lock
);
4490 if ((error
= nfs_sigintr(req
->r_nmp
, req
, req
->r_thread
, 0)))
4492 msleep(req
, &req
->r_mtx
, slpflag
| (PZERO
- 1), "nfswaitsent", &ts
);
4496 sent
= req
->r_flags
& R_SENT
;
4497 lck_mtx_unlock(&req
->r_mtx
);
4498 if (error
&& req
->r_callback
.rcb_func
&& !sent
) {
4499 nfs_request_rele(req
);
4502 FSDBG(274, R_XID32(req
->r_xid
), np
, procnum
, error
);
4503 if (error
|| req
->r_callback
.rcb_func
)
4504 nfs_request_rele(req
);
4510 * Wait for and finish an asynchronous NFS request.
4513 nfs_request_async_finish(
4515 struct nfsm_chain
*nmrepp
,
4519 int error
= 0, asyncio
= req
->r_callback
.rcb_func
? 1 : 0;
4520 struct nfsmount
*nmp
;
4522 lck_mtx_lock(&req
->r_mtx
);
4524 req
->r_flags
|= R_ASYNCWAIT
;
4525 while (req
->r_flags
& R_RESENDQ
) { /* wait until the request is off the resend queue */
4526 struct timespec ts
= { 2, 0 };
4528 if ((nmp
= req
->r_nmp
)) {
4529 lck_mtx_lock(&nmp
->nm_lock
);
4530 if ((nmp
->nm_state
& NFSSTA_RECOVER
) && (req
->r_rchain
.tqe_next
!= NFSREQNOLIST
)) {
4532 * It's not going to get off the resend queue if we're in recovery.
4533 * So, just take it off ourselves. We could be holding mount state
4534 * busy and thus holding up the start of recovery.
4536 TAILQ_REMOVE(&nmp
->nm_resendq
, req
, r_rchain
);
4537 req
->r_rchain
.tqe_next
= NFSREQNOLIST
;
4538 if (req
->r_flags
& R_RESENDQ
)
4539 req
->r_flags
&= ~R_RESENDQ
;
4540 lck_mtx_unlock(&nmp
->nm_lock
);
4543 lck_mtx_unlock(&nmp
->nm_lock
);
4545 if ((error
= nfs_sigintr(req
->r_nmp
, req
, req
->r_thread
, 0)))
4547 msleep(req
, &req
->r_mtx
, PZERO
-1, "nfsresendqwait", &ts
);
4549 lck_mtx_unlock(&req
->r_mtx
);
4552 nfs_request_wait(req
);
4553 error
= nfs_request_finish(req
, nmrepp
, status
);
4556 while (!error
&& (req
->r_flags
& R_RESTART
)) {
4557 if (asyncio
&& req
->r_resendtime
) { /* send later */
4558 lck_mtx_lock(&req
->r_mtx
);
4559 nfs_asyncio_resend(req
);
4560 lck_mtx_unlock(&req
->r_mtx
);
4561 return (EINPROGRESS
);
4564 req
->r_flags
&= ~R_RESTART
;
4565 if ((error
= nfs_request_add_header(req
)))
4567 if ((error
= nfs_request_send(req
, !asyncio
)))
4570 return (EINPROGRESS
);
4571 nfs_request_wait(req
);
4572 if ((error
= nfs_request_finish(req
, nmrepp
, status
)))
4578 FSDBG(275, R_XID32(req
->r_xid
), req
->r_np
, req
->r_procnum
, error
);
4579 nfs_request_rele(req
);
4584 * Cancel a pending asynchronous NFS request.
4587 nfs_request_async_cancel(struct nfsreq
*req
)
4589 FSDBG(275, R_XID32(req
->r_xid
), req
->r_np
, req
->r_procnum
, 0xD1ED1E);
4590 nfs_request_rele(req
);
4594 * Flag a request as being terminated.
4597 nfs_softterm(struct nfsreq
*req
)
4599 struct nfsmount
*nmp
= req
->r_nmp
;
4600 req
->r_flags
|= R_SOFTTERM
;
4601 req
->r_error
= ETIMEDOUT
;
4602 if (!(req
->r_flags
& R_CWND
) || nfs_mount_gone(nmp
))
4604 /* update congestion window */
4605 req
->r_flags
&= ~R_CWND
;
4606 lck_mtx_lock(&nmp
->nm_lock
);
4607 FSDBG(532, R_XID32(req
->r_xid
), req
, nmp
->nm_sent
, nmp
->nm_cwnd
);
4608 nmp
->nm_sent
-= NFS_CWNDSCALE
;
4609 if ((nmp
->nm_sent
< nmp
->nm_cwnd
) && !TAILQ_EMPTY(&nmp
->nm_cwndq
)) {
4610 /* congestion window is open, poke the cwnd queue */
4611 struct nfsreq
*req2
= TAILQ_FIRST(&nmp
->nm_cwndq
);
4612 TAILQ_REMOVE(&nmp
->nm_cwndq
, req2
, r_cchain
);
4613 req2
->r_cchain
.tqe_next
= NFSREQNOLIST
;
4616 lck_mtx_unlock(&nmp
->nm_lock
);
4620 * Ensure req isn't in use by the timer, then dequeue it.
4623 nfs_reqdequeue(struct nfsreq
*req
)
4625 lck_mtx_lock(nfs_request_mutex
);
4626 while (req
->r_lflags
& RL_BUSY
) {
4627 req
->r_lflags
|= RL_WAITING
;
4628 msleep(&req
->r_lflags
, nfs_request_mutex
, PSOCK
, "reqdeq", NULL
);
4630 if (req
->r_lflags
& RL_QUEUED
) {
4631 TAILQ_REMOVE(&nfs_reqq
, req
, r_chain
);
4632 req
->r_lflags
&= ~RL_QUEUED
;
4634 lck_mtx_unlock(nfs_request_mutex
);
4638 * Busy (lock) a nfsreq, used by the nfs timer to make sure it's not
4639 * free()'d out from under it.
4642 nfs_reqbusy(struct nfsreq
*req
)
4644 if (req
->r_lflags
& RL_BUSY
)
4645 panic("req locked");
4646 req
->r_lflags
|= RL_BUSY
;
4650 * Unbusy the nfsreq passed in, return the next nfsreq in the chain busied.
4653 nfs_reqnext(struct nfsreq
*req
)
4655 struct nfsreq
* nextreq
;
4660 * We need to get and busy the next req before signalling the
4661 * current one, otherwise wakeup() may block us and we'll race to
4662 * grab the next req.
4664 nextreq
= TAILQ_NEXT(req
, r_chain
);
4665 if (nextreq
!= NULL
)
4666 nfs_reqbusy(nextreq
);
4667 /* unbusy and signal. */
4668 req
->r_lflags
&= ~RL_BUSY
;
4669 if (req
->r_lflags
& RL_WAITING
) {
4670 req
->r_lflags
&= ~RL_WAITING
;
4671 wakeup(&req
->r_lflags
);
4677 * NFS request queue timer routine
4679 * Scan the NFS request queue for any requests that have timed out.
4681 * Alert the system of unresponsive servers.
4682 * Mark expired requests on soft mounts as terminated.
4683 * For UDP, mark/signal requests for retransmission.
4686 nfs_request_timer(__unused
void *param0
, __unused
void *param1
)
4689 struct nfsmount
*nmp
;
4690 int timeo
, maxtime
, finish_asyncio
, error
;
4692 TAILQ_HEAD(nfs_mount_pokeq
, nfsmount
) nfs_mount_poke_queue
;
4695 lck_mtx_lock(nfs_request_mutex
);
4696 req
= TAILQ_FIRST(&nfs_reqq
);
4697 if (req
== NULL
) { /* no requests - turn timer off */
4698 nfs_request_timer_on
= 0;
4699 lck_mtx_unlock(nfs_request_mutex
);
4704 TAILQ_INIT(&nfs_mount_poke_queue
);
4707 for ( ; req
!= NULL
; req
= nfs_reqnext(req
)) {
4710 NFS_SOCK_DBG("Found a request with out a mount!\n");
4713 if (req
->r_error
|| req
->r_nmrep
.nmc_mhead
)
4715 if ((error
= nfs_sigintr(nmp
, req
, req
->r_thread
, 0))) {
4716 if (req
->r_callback
.rcb_func
!= NULL
) {
4717 /* async I/O RPC needs to be finished */
4718 lck_mtx_lock(&req
->r_mtx
);
4719 req
->r_error
= error
;
4720 finish_asyncio
= !(req
->r_flags
& R_WAITSENT
);
4722 lck_mtx_unlock(&req
->r_mtx
);
4724 nfs_asyncio_finish(req
);
4729 lck_mtx_lock(&req
->r_mtx
);
4731 if (nmp
->nm_tprintf_initial_delay
&&
4732 ((req
->r_rexmit
> 2) || (req
->r_flags
& R_RESENDERR
)) &&
4733 ((req
->r_lastmsg
+ nmp
->nm_tprintf_delay
) < now
.tv_sec
)) {
4734 req
->r_lastmsg
= now
.tv_sec
;
4735 nfs_down(req
->r_nmp
, req
->r_thread
, 0, NFSSTA_TIMEO
,
4736 "not responding", 1);
4737 req
->r_flags
|= R_TPRINTFMSG
;
4738 lck_mtx_lock(&nmp
->nm_lock
);
4739 if (!(nmp
->nm_state
& NFSSTA_MOUNTED
)) {
4740 lck_mtx_unlock(&nmp
->nm_lock
);
4741 /* we're not yet completely mounted and */
4742 /* we can't complete an RPC, so we fail */
4743 OSAddAtomic64(1, &nfsstats
.rpctimeouts
);
4745 finish_asyncio
= ((req
->r_callback
.rcb_func
!= NULL
) && !(req
->r_flags
& R_WAITSENT
));
4747 lck_mtx_unlock(&req
->r_mtx
);
4749 nfs_asyncio_finish(req
);
4752 lck_mtx_unlock(&nmp
->nm_lock
);
4756 * Put a reasonable limit on the maximum timeout,
4757 * and reduce that limit when soft mounts get timeouts or are in reconnect.
4759 if (!(NMFLAG(nmp
, SOFT
) || (req
->r_flags
& R_SOFT
)) && !nfs_can_squish(nmp
))
4760 maxtime
= NFS_MAXTIMEO
;
4761 else if ((req
->r_flags
& (R_SETUP
|R_RECOVER
)) ||
4762 ((nmp
->nm_reconnect_start
<= 0) || ((now
.tv_sec
- nmp
->nm_reconnect_start
) < 8)))
4763 maxtime
= (NFS_MAXTIMEO
/ (nmp
->nm_timeouts
+1))/2;
4765 maxtime
= NFS_MINTIMEO
/4;
4768 * Check for request timeout.
4770 if (req
->r_rtt
>= 0) {
4772 lck_mtx_lock(&nmp
->nm_lock
);
4773 if (req
->r_flags
& R_RESENDERR
) {
4774 /* with resend errors, retry every few seconds */
4777 if (req
->r_procnum
== NFSPROC_NULL
&& req
->r_gss_ctx
!= NULL
)
4778 timeo
= NFS_MINIDEMTIMEO
; // gss context setup
4779 else if (NMFLAG(nmp
, DUMBTIMER
))
4780 timeo
= nmp
->nm_timeo
;
4782 timeo
= NFS_RTO(nmp
, proct
[req
->r_procnum
]);
4784 /* ensure 62.5 ms floor */
4785 while (16 * timeo
< hz
)
4787 if (nmp
->nm_timeouts
> 0)
4788 timeo
*= nfs_backoff
[nmp
->nm_timeouts
- 1];
4790 /* limit timeout to max */
4791 if (timeo
> maxtime
)
4793 if (req
->r_rtt
<= timeo
) {
4794 NFS_SOCK_DBG("nfs timeout: req time %d and timeo is %d continue\n", req
->r_rtt
, timeo
);
4795 lck_mtx_unlock(&nmp
->nm_lock
);
4796 lck_mtx_unlock(&req
->r_mtx
);
4799 /* The request has timed out */
4800 NFS_SOCK_DBG("nfs timeout: proc %d %d xid %llx rtt %d to %d # %d, t %ld/%d\n",
4801 req
->r_procnum
, proct
[req
->r_procnum
],
4802 req
->r_xid
, req
->r_rtt
, timeo
, nmp
->nm_timeouts
,
4803 (now
.tv_sec
- req
->r_start
)*NFS_HZ
, maxtime
);
4804 if (nmp
->nm_timeouts
< 8)
4806 if (nfs_mount_check_dead_timeout(nmp
)) {
4807 /* Unbusy this request */
4808 req
->r_lflags
&= ~RL_BUSY
;
4809 if (req
->r_lflags
& RL_WAITING
) {
4810 req
->r_lflags
&= ~RL_WAITING
;
4811 wakeup(&req
->r_lflags
);
4813 lck_mtx_unlock(&req
->r_mtx
);
4815 /* No need to poke this mount */
4816 if (nmp
->nm_sockflags
& NMSOCK_POKE
) {
4817 nmp
->nm_sockflags
&= ~NMSOCK_POKE
;
4818 TAILQ_REMOVE(&nfs_mount_poke_queue
, nmp
, nm_pokeq
);
4820 /* Release our lock state, so we can become a zombie */
4821 lck_mtx_unlock(nfs_request_mutex
);
4824 * Note nfs_mount_make zombie(nmp) must be
4825 * called with nm_lock held. After doing some
4826 * work we release nm_lock in
4827 * nfs_make_mount_zombie with out acquiring any
4828 * other locks. (Later, in nfs_mount_zombie we
4829 * will acquire nfs_request_mutex, r_mtx,
4830 * nm_lock in that order). So we should not be
4831 * introducing deadlock here. We take a reference
4832 * on the mount so that its still there when we
4836 nfs_mount_make_zombie(nmp
);
4837 lck_mtx_unlock(&nmp
->nm_lock
);
4838 nfs_mount_rele(nmp
);
4841 * All the request for this mount have now been
4842 * removed from the request queue. Restart to
4843 * process the remaining mounts
4848 /* if it's been a few seconds, try poking the socket */
4849 if ((nmp
->nm_sotype
== SOCK_STREAM
) &&
4850 ((now
.tv_sec
- req
->r_start
) >= 3) &&
4851 !(nmp
->nm_sockflags
& (NMSOCK_POKE
|NMSOCK_UNMOUNT
)) &&
4852 (nmp
->nm_sockflags
& NMSOCK_READY
)) {
4853 nmp
->nm_sockflags
|= NMSOCK_POKE
;
4854 TAILQ_INSERT_TAIL(&nfs_mount_poke_queue
, nmp
, nm_pokeq
);
4856 lck_mtx_unlock(&nmp
->nm_lock
);
4859 /* For soft mounts (& SETUPs/RECOVERs), check for too many retransmits/timeout. */
4860 if ((NMFLAG(nmp
, SOFT
) || (req
->r_flags
& (R_SETUP
|R_RECOVER
|R_SOFT
))) &&
4861 ((req
->r_rexmit
>= req
->r_retry
) || /* too many */
4862 ((now
.tv_sec
- req
->r_start
)*NFS_HZ
> maxtime
))) { /* too long */
4863 OSAddAtomic64(1, &nfsstats
.rpctimeouts
);
4864 lck_mtx_lock(&nmp
->nm_lock
);
4865 if (!(nmp
->nm_state
& NFSSTA_TIMEO
)) {
4866 lck_mtx_unlock(&nmp
->nm_lock
);
4867 /* make sure we note the unresponsive server */
4868 /* (maxtime may be less than tprintf delay) */
4869 nfs_down(req
->r_nmp
, req
->r_thread
, 0, NFSSTA_TIMEO
,
4870 "not responding", 1);
4871 req
->r_lastmsg
= now
.tv_sec
;
4872 req
->r_flags
|= R_TPRINTFMSG
;
4874 lck_mtx_unlock(&nmp
->nm_lock
);
4876 if (req
->r_flags
& R_NOINTR
) {
4877 /* don't terminate nointr requests on timeout */
4878 lck_mtx_unlock(&req
->r_mtx
);
4881 NFS_SOCK_DBG("nfs timer TERMINATE: p %d x 0x%llx f 0x%x rtt %d t %ld\n",
4882 req
->r_procnum
, req
->r_xid
, req
->r_flags
, req
->r_rtt
,
4883 now
.tv_sec
- req
->r_start
);
4885 finish_asyncio
= ((req
->r_callback
.rcb_func
!= NULL
) && !(req
->r_flags
& R_WAITSENT
));
4887 lck_mtx_unlock(&req
->r_mtx
);
4889 nfs_asyncio_finish(req
);
4893 /* for TCP, only resend if explicitly requested */
4894 if ((nmp
->nm_sotype
== SOCK_STREAM
) && !(req
->r_flags
& R_MUSTRESEND
)) {
4895 if (++req
->r_rexmit
> NFS_MAXREXMIT
)
4896 req
->r_rexmit
= NFS_MAXREXMIT
;
4898 lck_mtx_unlock(&req
->r_mtx
);
4903 * The request needs to be (re)sent. Kick the requester to resend it.
4904 * (unless it's already marked as needing a resend)
4906 if ((req
->r_flags
& R_MUSTRESEND
) && (req
->r_rtt
== -1)) {
4907 lck_mtx_unlock(&req
->r_mtx
);
4910 NFS_SOCK_DBG("nfs timer mark resend: p %d x 0x%llx f 0x%x rtt %d\n",
4911 req
->r_procnum
, req
->r_xid
, req
->r_flags
, req
->r_rtt
);
4912 req
->r_flags
|= R_MUSTRESEND
;
4915 if ((req
->r_flags
& (R_ASYNC
|R_ASYNCWAIT
|R_SENDING
)) == R_ASYNC
)
4916 nfs_asyncio_resend(req
);
4917 lck_mtx_unlock(&req
->r_mtx
);
4920 lck_mtx_unlock(nfs_request_mutex
);
4922 /* poke any sockets */
4923 while ((nmp
= TAILQ_FIRST(&nfs_mount_poke_queue
))) {
4924 TAILQ_REMOVE(&nfs_mount_poke_queue
, nmp
, nm_pokeq
);
4928 nfs_interval_timer_start(nfs_request_timer_call
, NFS_REQUESTDELAY
);
4932 * check a thread's proc for the "noremotehang" flag.
4935 nfs_noremotehang(thread_t thd
)
4937 proc_t p
= thd
? get_bsdthreadtask_info(thd
) : NULL
;
4938 return (p
&& proc_noremotehang(p
));
4942 * Test for a termination condition pending on the process.
4943 * This is used to determine if we need to bail on a mount.
4944 * ETIMEDOUT is returned if there has been a soft timeout.
4945 * EINTR is returned if there is a signal pending that is not being ignored
4946 * and the mount is interruptable, or if we are a thread that is in the process
4947 * of cancellation (also SIGKILL posted).
4949 extern int sigprop
[NSIG
+1];
4951 nfs_sigintr(struct nfsmount
*nmp
, struct nfsreq
*req
, thread_t thd
, int nmplocked
)
4959 if (req
&& (req
->r_flags
& R_SOFTTERM
))
4960 return (ETIMEDOUT
); /* request has been terminated. */
4961 if (req
&& (req
->r_flags
& R_NOINTR
))
4962 thd
= NULL
; /* don't check for signal on R_NOINTR */
4965 lck_mtx_lock(&nmp
->nm_lock
);
4966 if (nmp
->nm_state
& NFSSTA_FORCE
) {
4967 /* If a force unmount is in progress then fail. */
4969 } else if (vfs_isforce(nmp
->nm_mountp
)) {
4970 /* Someone is unmounting us, go soft and mark it. */
4971 NFS_BITMAP_SET(nmp
->nm_flags
, NFS_MFLAG_SOFT
);
4972 nmp
->nm_state
|= NFSSTA_FORCE
;
4975 /* Check if the mount is marked dead. */
4976 if (!error
&& (nmp
->nm_state
& NFSSTA_DEAD
))
4980 * If the mount is hung and we've requested not to hang
4981 * on remote filesystems, then bail now.
4983 if (current_proc() != kernproc
&&
4984 !error
&& (nmp
->nm_state
& NFSSTA_TIMEO
) && nfs_noremotehang(thd
))
4988 lck_mtx_unlock(&nmp
->nm_lock
);
4992 /* may not have a thread for async I/O */
4993 if (thd
== NULL
|| current_proc() == kernproc
)
4997 * Check if the process is aborted, but don't interrupt if we
4998 * were killed by a signal and this is the exiting thread which
4999 * is attempting to dump core.
5001 if (((p
= current_proc()) != kernproc
) && current_thread_aborted() &&
5002 (!(p
->p_acflag
& AXSIG
) || (p
->exit_thread
!= current_thread()) ||
5003 (p
->p_sigacts
== NULL
) ||
5004 (p
->p_sigacts
->ps_sig
< 1) || (p
->p_sigacts
->ps_sig
> NSIG
) ||
5005 !(sigprop
[p
->p_sigacts
->ps_sig
] & SA_CORE
)))
5008 /* mask off thread and process blocked signals. */
5009 if (NMFLAG(nmp
, INTR
) && ((p
= get_bsdthreadtask_info(thd
))) &&
5010 proc_pendingsignals(p
, NFSINT_SIGMASK
))
5016 * Lock a socket against others.
5017 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
5018 * and also to avoid race conditions between the processes with nfs requests
5019 * in progress when a reconnect is necessary.
5022 nfs_sndlock(struct nfsreq
*req
)
5024 struct nfsmount
*nmp
= req
->r_nmp
;
5026 int error
= 0, slpflag
= 0;
5027 struct timespec ts
= { 0, 0 };
5029 if (nfs_mount_gone(nmp
))
5032 lck_mtx_lock(&nmp
->nm_lock
);
5033 statep
= &nmp
->nm_state
;
5035 if (NMFLAG(nmp
, INTR
) && req
->r_thread
&& !(req
->r_flags
& R_NOINTR
))
5037 while (*statep
& NFSSTA_SNDLOCK
) {
5038 if ((error
= nfs_sigintr(nmp
, req
, req
->r_thread
, 1)))
5040 *statep
|= NFSSTA_WANTSND
;
5041 if (nfs_noremotehang(req
->r_thread
))
5043 msleep(statep
, &nmp
->nm_lock
, slpflag
| (PZERO
- 1), "nfsndlck", &ts
);
5044 if (slpflag
== PCATCH
) {
5050 *statep
|= NFSSTA_SNDLOCK
;
5051 lck_mtx_unlock(&nmp
->nm_lock
);
5056 * Unlock the stream socket for others.
5059 nfs_sndunlock(struct nfsreq
*req
)
5061 struct nfsmount
*nmp
= req
->r_nmp
;
5062 int *statep
, wake
= 0;
5066 lck_mtx_lock(&nmp
->nm_lock
);
5067 statep
= &nmp
->nm_state
;
5068 if ((*statep
& NFSSTA_SNDLOCK
) == 0)
5069 panic("nfs sndunlock");
5070 *statep
&= ~(NFSSTA_SNDLOCK
|NFSSTA_SENDING
);
5071 if (*statep
& NFSSTA_WANTSND
) {
5072 *statep
&= ~NFSSTA_WANTSND
;
5075 lck_mtx_unlock(&nmp
->nm_lock
);
5082 struct nfsmount
*nmp
,
5084 struct sockaddr
*saddr
,
5091 struct nfsm_chain
*nmrep
)
5093 int error
= 0, on
= 1, try, sendat
= 2, soproto
, recv
, optlen
, restoreto
= 0;
5094 socket_t newso
= NULL
;
5095 struct sockaddr_storage ss
;
5096 struct timeval orig_rcvto
, orig_sndto
, tv
= { 1, 0 };
5097 mbuf_t m
, mrep
= NULL
;
5099 uint32_t rxid
= 0, reply
= 0, reply_status
, rejected_status
;
5100 uint32_t verf_type
, verf_len
, accepted_status
;
5101 size_t readlen
, sentlen
;
5102 struct nfs_rpc_record_state nrrs
;
5105 /* create socket and set options */
5106 soproto
= (sotype
== SOCK_DGRAM
) ? IPPROTO_UDP
: IPPROTO_TCP
;
5107 if ((error
= sock_socket(saddr
->sa_family
, sotype
, soproto
, NULL
, NULL
, &newso
)))
5111 int level
= (saddr
->sa_family
== AF_INET
) ? IPPROTO_IP
: IPPROTO_IPV6
;
5112 int optname
= (saddr
->sa_family
== AF_INET
) ? IP_PORTRANGE
: IPV6_PORTRANGE
;
5113 int portrange
= IP_PORTRANGE_LOW
;
5114 error
= sock_setsockopt(newso
, level
, optname
, &portrange
, sizeof(portrange
));
5116 ss
.ss_len
= saddr
->sa_len
;
5117 ss
.ss_family
= saddr
->sa_family
;
5118 if (ss
.ss_family
== AF_INET
) {
5119 ((struct sockaddr_in
*)&ss
)->sin_addr
.s_addr
= INADDR_ANY
;
5120 ((struct sockaddr_in
*)&ss
)->sin_port
= htons(0);
5121 } else if (ss
.ss_family
== AF_INET6
) {
5122 ((struct sockaddr_in6
*)&ss
)->sin6_addr
= in6addr_any
;
5123 ((struct sockaddr_in6
*)&ss
)->sin6_port
= htons(0);
5128 error
= sock_bind(newso
, (struct sockaddr
*)&ss
);
5132 if (sotype
== SOCK_STREAM
) {
5133 # define NFS_AUX_CONNECTION_TIMEOUT 4 /* 4 second timeout for connections */
5136 error
= sock_connect(newso
, saddr
, MSG_DONTWAIT
);
5137 if (error
== EINPROGRESS
)
5141 while ((error
= sock_connectwait(newso
, &tv
)) == EINPROGRESS
) {
5142 /* After NFS_AUX_CONNECTION_TIMEOUT bail */
5143 if (++count
>= NFS_AUX_CONNECTION_TIMEOUT
) {
5150 if (((error
= sock_setsockopt(newso
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
)))) ||
5151 ((error
= sock_setsockopt(newso
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
)))) ||
5152 ((error
= sock_setsockopt(newso
, SOL_SOCKET
, SO_NOADDRERR
, &on
, sizeof(on
)))))
5156 /* make sure socket is using a one second timeout in this function */
5157 optlen
= sizeof(orig_rcvto
);
5158 error
= sock_getsockopt(so
, SOL_SOCKET
, SO_RCVTIMEO
, &orig_rcvto
, &optlen
);
5160 optlen
= sizeof(orig_sndto
);
5161 error
= sock_getsockopt(so
, SOL_SOCKET
, SO_SNDTIMEO
, &orig_sndto
, &optlen
);
5164 sock_setsockopt(so
, SOL_SOCKET
, SO_RCVTIMEO
, &tv
, sizeof(tv
));
5165 sock_setsockopt(so
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
));
5170 if (sotype
== SOCK_STREAM
) {
5171 sendat
= 0; /* we only resend the request for UDP */
5172 nfs_rpc_record_state_init(&nrrs
);
5175 for (try=0; try < timeo
; try++) {
5176 if ((error
= nfs_sigintr(nmp
, NULL
, !try ? NULL
: thd
, 0)))
5178 if (!try || (try == sendat
)) {
5179 /* send the request (resending periodically for UDP) */
5180 if ((error
= mbuf_copym(mreq
, 0, MBUF_COPYALL
, MBUF_WAITOK
, &m
)))
5182 bzero(&msg
, sizeof(msg
));
5183 if ((sotype
== SOCK_DGRAM
) && !sock_isconnected(so
)) {
5184 msg
.msg_name
= saddr
;
5185 msg
.msg_namelen
= saddr
->sa_len
;
5187 if ((error
= sock_sendmbuf(so
, &msg
, m
, 0, &sentlen
)))
5193 /* wait for the response */
5194 if (sotype
== SOCK_STREAM
) {
5195 /* try to read (more of) record */
5196 error
= nfs_rpc_record_read(so
, &nrrs
, 0, &recv
, &mrep
);
5197 /* if we don't have the whole record yet, we'll keep trying */
5200 bzero(&msg
, sizeof(msg
));
5201 error
= sock_receivembuf(so
, &msg
, &mrep
, 0, &readlen
);
5203 if (error
== EWOULDBLOCK
)
5206 /* parse the response */
5207 nfsm_chain_dissect_init(error
, nmrep
, mrep
);
5208 nfsm_chain_get_32(error
, nmrep
, rxid
);
5209 nfsm_chain_get_32(error
, nmrep
, reply
);
5211 if ((rxid
!= xid
) || (reply
!= RPC_REPLY
))
5213 nfsm_chain_get_32(error
, nmrep
, reply_status
);
5215 if (reply_status
== RPC_MSGDENIED
) {
5216 nfsm_chain_get_32(error
, nmrep
, rejected_status
);
5218 error
= (rejected_status
== RPC_MISMATCH
) ? ERPCMISMATCH
: EACCES
;
5221 nfsm_chain_get_32(error
, nmrep
, verf_type
); /* verifier flavor */
5222 nfsm_chain_get_32(error
, nmrep
, verf_len
); /* verifier length */
5225 nfsm_chain_adv(error
, nmrep
, nfsm_rndup(verf_len
));
5226 nfsm_chain_get_32(error
, nmrep
, accepted_status
);
5228 switch (accepted_status
) {
5232 case RPC_PROGUNAVAIL
:
5233 error
= EPROGUNAVAIL
;
5235 case RPC_PROGMISMATCH
:
5236 error
= EPROGMISMATCH
;
5238 case RPC_PROCUNAVAIL
:
5239 error
= EPROCUNAVAIL
;
5244 case RPC_SYSTEM_ERR
:
5253 sock_setsockopt(so
, SOL_SOCKET
, SO_RCVTIMEO
, &orig_rcvto
, sizeof(tv
));
5254 sock_setsockopt(so
, SOL_SOCKET
, SO_SNDTIMEO
, &orig_sndto
, sizeof(tv
));
5257 sock_shutdown(newso
, SHUT_RDWR
);
5266 struct nfsmount
*nmp
,
5268 struct sockaddr
*sa
,
5275 thread_t thd
= vfs_context_thread(ctx
);
5276 kauth_cred_t cred
= vfs_context_ucred(ctx
);
5277 struct sockaddr_storage ss
;
5278 struct sockaddr
*saddr
= (struct sockaddr
*)&ss
;
5279 struct nfsm_chain nmreq
, nmrep
;
5281 int error
= 0, ip
, pmprog
, pmvers
, pmproc
, ualen
= 0;
5284 char uaddr
[MAX_IPv6_STR_LEN
+16];
5286 bcopy(sa
, saddr
, min(sizeof(ss
), sa
->sa_len
));
5287 if (saddr
->sa_family
== AF_INET
) {
5291 pmproc
= PMAPPROC_GETPORT
;
5292 } else if (saddr
->sa_family
== AF_INET6
) {
5296 pmproc
= RPCBPROC_GETVERSADDR
;
5300 nfsm_chain_null(&nmreq
);
5301 nfsm_chain_null(&nmrep
);
5304 /* send portmapper request to get port/uaddr */
5306 ((struct sockaddr_in
*)saddr
)->sin_port
= htons(PMAPPORT
);
5308 ((struct sockaddr_in6
*)saddr
)->sin6_port
= htons(PMAPPORT
);
5309 nfsm_chain_build_alloc_init(error
, &nmreq
, 8*NFSX_UNSIGNED
);
5310 nfsm_chain_add_32(error
, &nmreq
, protocol
);
5311 nfsm_chain_add_32(error
, &nmreq
, vers
);
5313 nfsm_chain_add_32(error
, &nmreq
, ipproto
);
5314 nfsm_chain_add_32(error
, &nmreq
, 0);
5316 if (ipproto
== IPPROTO_TCP
)
5317 nfsm_chain_add_string(error
, &nmreq
, "tcp6", 4);
5319 nfsm_chain_add_string(error
, &nmreq
, "udp6", 4);
5320 nfsm_chain_add_string(error
, &nmreq
, "", 0); /* uaddr */
5321 nfsm_chain_add_string(error
, &nmreq
, "", 0); /* owner */
5323 nfsm_chain_build_done(error
, &nmreq
);
5325 error
= nfsm_rpchead2(nmp
, (ipproto
== IPPROTO_UDP
) ? SOCK_DGRAM
: SOCK_STREAM
,
5326 pmprog
, pmvers
, pmproc
, RPCAUTH_SYS
, cred
, NULL
, nmreq
.nmc_mhead
,
5329 nmreq
.nmc_mhead
= NULL
;
5330 error
= nfs_aux_request(nmp
, thd
, saddr
, so
, (ipproto
== IPPROTO_UDP
) ? SOCK_DGRAM
: SOCK_STREAM
,
5331 mreq
, R_XID32(xid
), 0, timeo
, &nmrep
);
5333 /* grab port from portmap response */
5335 nfsm_chain_get_32(error
, &nmrep
, port
);
5337 ((struct sockaddr_in
*)sa
)->sin_port
= htons(port
);
5339 /* get uaddr string and convert to sockaddr */
5340 nfsm_chain_get_32(error
, &nmrep
, ualen
);
5342 if (ualen
> ((int)sizeof(uaddr
)-1))
5345 /* program is not available, just return a zero port */
5346 bcopy(sa
, saddr
, min(sizeof(ss
), sa
->sa_len
));
5347 ((struct sockaddr_in6
*)saddr
)->sin6_port
= htons(0);
5349 nfsm_chain_get_opaque(error
, &nmrep
, ualen
, uaddr
);
5351 uaddr
[ualen
] = '\0';
5352 if (!nfs_uaddr2sockaddr(uaddr
, saddr
))
5357 if ((error
== EPROGMISMATCH
) || (error
== EPROCUNAVAIL
) || (error
== EIO
) || (error
== EBADRPC
)) {
5358 /* remote doesn't support rpcbind version or proc (or we couldn't parse uaddr) */
5359 if (pmvers
== RPCBVERS4
) {
5360 /* fall back to v3 and GETADDR */
5362 pmproc
= RPCBPROC_GETADDR
;
5363 nfsm_chain_cleanup(&nmreq
);
5364 nfsm_chain_cleanup(&nmrep
);
5365 bcopy(sa
, saddr
, min(sizeof(ss
), sa
->sa_len
));
5372 bcopy(saddr
, sa
, min(saddr
->sa_len
, sa
->sa_len
));
5375 nfsm_chain_cleanup(&nmreq
);
5376 nfsm_chain_cleanup(&nmrep
);
5381 nfs_msg(thread_t thd
,
5386 proc_t p
= thd
? get_bsdthreadtask_info(thd
) : NULL
;
5390 tpr
= tprintf_open(p
);
5394 tprintf(tpr
, "nfs server %s: %s, error %d\n", server
, msg
, error
);
5396 tprintf(tpr
, "nfs server %s: %s\n", server
, msg
);
5401 #define NFS_SQUISH_MOBILE_ONLY 0x0001 /* Squish mounts only on mobile machines */
5402 #define NFS_SQUISH_AUTOMOUNTED_ONLY 0x0002 /* Squish mounts only if the are automounted */
5403 #define NFS_SQUISH_SOFT 0x0004 /* Treat all soft mounts as though they were on a mobile machine */
5404 #define NFS_SQUISH_QUICK 0x0008 /* Try to squish mounts more quickly. */
5405 #define NFS_SQUISH_SHUTDOWN 0x1000 /* Squish all mounts on shutdown. Currently not implemented */
5407 uint32_t nfs_squishy_flags
= NFS_SQUISH_MOBILE_ONLY
| NFS_SQUISH_AUTOMOUNTED_ONLY
| NFS_SQUISH_QUICK
;
5408 int32_t nfs_is_mobile
;
5410 #define NFS_SQUISHY_DEADTIMEOUT 8 /* Dead time out for squishy mounts */
5411 #define NFS_SQUISHY_QUICKTIMEOUT 4 /* Quicker dead time out when nfs_squish_flags NFS_SQUISH_QUICK bit is set*/
5414 * Could this mount be squished?
5417 nfs_can_squish(struct nfsmount
*nmp
)
5419 uint64_t flags
= vfs_flags(nmp
->nm_mountp
);
5420 int softsquish
= ((nfs_squishy_flags
& NFS_SQUISH_SOFT
) & NMFLAG(nmp
, SOFT
));
5422 if (!softsquish
&& (nfs_squishy_flags
& NFS_SQUISH_MOBILE_ONLY
) && nfs_is_mobile
== 0)
5425 if ((nfs_squishy_flags
& NFS_SQUISH_AUTOMOUNTED_ONLY
) && (flags
& MNT_AUTOMOUNTED
) == 0)
5432 * NFS mounts default to "rw,hard" - but frequently on mobile clients
5433 * the mount may become "not responding". It's desirable to be able
5434 * to unmount these dead mounts, but only if there is no risk of
5435 * losing data or crashing applications. A "squishy" NFS mount is one
5436 * that can be force unmounted with little risk of harm.
5438 * nfs_is_squishy checks if a mount is in a squishy state. A mount is
5439 * in a squishy state iff it is allowed to be squishy and there are no
5440 * dirty pages and there are no mmapped files and there are no files
5441 * open for write. Mounts are allowed to be squishy is controlled by
5442 * the settings of the nfs_squishy_flags and its mobility state. These
5443 * flags can be set by sysctls.
5445 * If nfs_is_squishy determines that we are in a squishy state we will
5446 * update the current dead timeout to at least NFS_SQUISHY_DEADTIMEOUT
5447 * (or NFS_SQUISHY_QUICKTIMEOUT if NFS_SQUISH_QUICK is set) (see
5448 * above) or 1/8th of the mount's nm_deadtimeout value, otherwise we just
5449 * update the current dead timeout with the mount's nm_deadtimeout
5450 * value set at mount time.
5452 * Assumes that nm_lock is held.
5454 * Note this routine is racey, but its effects on setting the
5455 * dead timeout only have effects when we're in trouble and are likely
5456 * to stay that way. Since by default its only for automounted
5457 * volumes on mobile machines; this is a reasonable trade off between
5458 * data integrity and user experience. It can be disabled or set via
5463 nfs_is_squishy(struct nfsmount
*nmp
)
5465 mount_t mp
= nmp
->nm_mountp
;
5467 int timeo
= (nfs_squishy_flags
& NFS_SQUISH_QUICK
) ? NFS_SQUISHY_QUICKTIMEOUT
: NFS_SQUISHY_DEADTIMEOUT
;
5469 NFS_SOCK_DBG("%s: nm_curdeadtimeout = %d, nfs_is_mobile = %d\n",
5470 vfs_statfs(mp
)->f_mntfromname
, nmp
->nm_curdeadtimeout
, nfs_is_mobile
);
5472 if (!nfs_can_squish(nmp
))
5475 timeo
= (nmp
->nm_deadtimeout
> timeo
) ? max(nmp
->nm_deadtimeout
/8, timeo
) : timeo
;
5476 NFS_SOCK_DBG("nm_writers = %d nm_mappers = %d timeo = %d\n", nmp
->nm_writers
, nmp
->nm_mappers
, timeo
);
5478 if (nmp
->nm_writers
== 0 && nmp
->nm_mappers
== 0) {
5479 uint64_t flags
= mp
? vfs_flags(mp
) : 0;
5483 * Walk the nfs nodes and check for dirty buffers it we're not
5484 * RDONLY and we've not already been declared as squishy since
5485 * this can be a bit expensive.
5487 if (!(flags
& MNT_RDONLY
) && !(nmp
->nm_state
& NFSSTA_SQUISHY
))
5488 squishy
= !nfs_mount_is_dirty(mp
);
5493 nmp
->nm_state
|= NFSSTA_SQUISHY
;
5495 nmp
->nm_state
&= ~NFSSTA_SQUISHY
;
5497 nmp
->nm_curdeadtimeout
= squishy
? timeo
: nmp
->nm_deadtimeout
;
5499 NFS_SOCK_DBG("nm_curdeadtimeout = %d\n", nmp
->nm_curdeadtimeout
);
5505 * On a send operation, if we can't reach the server and we've got only one server to talk to
5506 * and NFS_SQUISH_QUICK flag is set and we are in a squishy state then mark the mount as dead
5507 * and ask to be forcibly unmounted. Return 1 if we're dead and 0 otherwise.
5510 nfs_is_dead(int error
, struct nfsmount
*nmp
)
5514 lck_mtx_lock(&nmp
->nm_lock
);
5515 if (nmp
->nm_state
& NFSSTA_DEAD
) {
5516 lck_mtx_unlock(&nmp
->nm_lock
);
5520 if ((error
!= ENETUNREACH
&& error
!= EHOSTUNREACH
&& error
!= EADDRNOTAVAIL
) ||
5521 !(nmp
->nm_locations
.nl_numlocs
== 1 && nmp
->nm_locations
.nl_locations
[0]->nl_servcount
== 1)) {
5522 lck_mtx_unlock(&nmp
->nm_lock
);
5526 if ((nfs_squishy_flags
& NFS_SQUISH_QUICK
) && nfs_is_squishy(nmp
)) {
5527 printf("nfs_is_dead: nfs server %s: unreachable. Squished dead\n", vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
);
5528 fsid
= vfs_statfs(nmp
->nm_mountp
)->f_fsid
;
5529 lck_mtx_unlock(&nmp
->nm_lock
);
5530 nfs_mount_zombie(nmp
, NFSSTA_DEAD
);
5531 vfs_event_signal(&fsid
, VQ_DEAD
, 0);
5534 lck_mtx_unlock(&nmp
->nm_lock
);
5539 * If we've experienced timeouts and we're not really a
5540 * classic hard mount, then just return cached data to
5541 * the caller instead of likely hanging on an RPC.
5544 nfs_use_cache(struct nfsmount
*nmp
)
5547 *%%% We always let mobile users goto the cache,
5548 * perhaps we should not even require them to have
5551 int cache_ok
= (nfs_is_mobile
|| NMFLAG(nmp
, SOFT
) ||
5552 nfs_can_squish(nmp
) || nmp
->nm_deadtimeout
);
5554 int timeoutmask
= NFSSTA_TIMEO
| NFSSTA_LOCKTIMEO
| NFSSTA_JUKEBOXTIMEO
;
5557 * So if we have a timeout and we're not really a hard hard-mount,
5558 * return 1 to not get things out of the cache.
5561 return ((nmp
->nm_state
& timeoutmask
) && cache_ok
);
5565 * Log a message that nfs or lockd server is unresponsive. Check if we
5566 * can be squished and if we can, or that our dead timeout has
5567 * expired, and we're not holding state, set our mount as dead, remove
5568 * our mount state and ask to be unmounted. If we are holding state
5569 * we're being called from the nfs_request_timer and will soon detect
5570 * that we need to unmount.
5573 nfs_down(struct nfsmount
*nmp
, thread_t thd
, int error
, int flags
, const char *msg
, int holding_state
)
5575 int timeoutmask
, wasunresponsive
, unresponsive
, softnobrowse
;
5576 uint32_t do_vfs_signal
= 0;
5579 if (nfs_mount_gone(nmp
))
5582 lck_mtx_lock(&nmp
->nm_lock
);
5584 timeoutmask
= NFSSTA_TIMEO
| NFSSTA_LOCKTIMEO
| NFSSTA_JUKEBOXTIMEO
;
5585 if (NMFLAG(nmp
, MUTEJUKEBOX
)) /* jukebox timeouts don't count as unresponsive if muted */
5586 timeoutmask
&= ~NFSSTA_JUKEBOXTIMEO
;
5587 wasunresponsive
= (nmp
->nm_state
& timeoutmask
);
5589 /* XXX don't allow users to know about/disconnect unresponsive, soft, nobrowse mounts */
5590 softnobrowse
= (NMFLAG(nmp
, SOFT
) && (vfs_flags(nmp
->nm_mountp
) & MNT_DONTBROWSE
));
5592 if ((flags
& NFSSTA_TIMEO
) && !(nmp
->nm_state
& NFSSTA_TIMEO
))
5593 nmp
->nm_state
|= NFSSTA_TIMEO
;
5594 if ((flags
& NFSSTA_LOCKTIMEO
) && !(nmp
->nm_state
& NFSSTA_LOCKTIMEO
))
5595 nmp
->nm_state
|= NFSSTA_LOCKTIMEO
;
5596 if ((flags
& NFSSTA_JUKEBOXTIMEO
) && !(nmp
->nm_state
& NFSSTA_JUKEBOXTIMEO
))
5597 nmp
->nm_state
|= NFSSTA_JUKEBOXTIMEO
;
5599 unresponsive
= (nmp
->nm_state
& timeoutmask
);
5601 nfs_is_squishy(nmp
);
5603 if (unresponsive
&& (nmp
->nm_curdeadtimeout
> 0)) {
5605 if (!wasunresponsive
) {
5606 nmp
->nm_deadto_start
= now
.tv_sec
;
5607 nfs_mount_sock_thread_wake(nmp
);
5608 } else if ((now
.tv_sec
- nmp
->nm_deadto_start
) > nmp
->nm_curdeadtimeout
&& !holding_state
) {
5609 if (!(nmp
->nm_state
& NFSSTA_DEAD
))
5610 printf("nfs server %s: %sdead\n", vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
,
5611 (nmp
->nm_curdeadtimeout
!= nmp
->nm_deadtimeout
) ? "squished " : "");
5612 do_vfs_signal
= VQ_DEAD
;
5615 lck_mtx_unlock(&nmp
->nm_lock
);
5617 if (do_vfs_signal
== VQ_DEAD
&& !(nmp
->nm_state
& NFSSTA_DEAD
))
5618 nfs_mount_zombie(nmp
, NFSSTA_DEAD
);
5619 else if (softnobrowse
|| wasunresponsive
|| !unresponsive
)
5622 do_vfs_signal
= VQ_NOTRESP
;
5624 vfs_event_signal(&vfs_statfs(nmp
->nm_mountp
)->f_fsid
, do_vfs_signal
, 0);
5626 nfs_msg(thd
, vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, msg
, error
);
5630 nfs_up(struct nfsmount
*nmp
, thread_t thd
, int flags
, const char *msg
)
5632 int timeoutmask
, wasunresponsive
, unresponsive
, softnobrowse
;
5635 if (nfs_mount_gone(nmp
))
5639 nfs_msg(thd
, vfs_statfs(nmp
->nm_mountp
)->f_mntfromname
, msg
, 0);
5641 lck_mtx_lock(&nmp
->nm_lock
);
5643 timeoutmask
= NFSSTA_TIMEO
| NFSSTA_LOCKTIMEO
| NFSSTA_JUKEBOXTIMEO
;
5644 if (NMFLAG(nmp
, MUTEJUKEBOX
)) /* jukebox timeouts don't count as unresponsive if muted */
5645 timeoutmask
&= ~NFSSTA_JUKEBOXTIMEO
;
5646 wasunresponsive
= (nmp
->nm_state
& timeoutmask
);
5648 /* XXX don't allow users to know about/disconnect unresponsive, soft, nobrowse mounts */
5649 softnobrowse
= (NMFLAG(nmp
, SOFT
) && (vfs_flags(nmp
->nm_mountp
) & MNT_DONTBROWSE
));
5651 if ((flags
& NFSSTA_TIMEO
) && (nmp
->nm_state
& NFSSTA_TIMEO
))
5652 nmp
->nm_state
&= ~NFSSTA_TIMEO
;
5653 if ((flags
& NFSSTA_LOCKTIMEO
) && (nmp
->nm_state
& NFSSTA_LOCKTIMEO
))
5654 nmp
->nm_state
&= ~NFSSTA_LOCKTIMEO
;
5655 if ((flags
& NFSSTA_JUKEBOXTIMEO
) && (nmp
->nm_state
& NFSSTA_JUKEBOXTIMEO
))
5656 nmp
->nm_state
&= ~NFSSTA_JUKEBOXTIMEO
;
5658 unresponsive
= (nmp
->nm_state
& timeoutmask
);
5660 nmp
->nm_deadto_start
= 0;
5661 nmp
->nm_curdeadtimeout
= nmp
->nm_deadtimeout
;
5662 nmp
->nm_state
&= ~NFSSTA_SQUISHY
;
5663 lck_mtx_unlock(&nmp
->nm_lock
);
5668 do_vfs_signal
= (wasunresponsive
&& !unresponsive
);
5670 vfs_event_signal(&vfs_statfs(nmp
->nm_mountp
)->f_fsid
, VQ_NOTRESP
, 1);
5674 #endif /* NFSCLIENT */
5679 * Generate the rpc reply header
5680 * siz arg. is used to decide if adding a cluster is worthwhile
5684 struct nfsrv_descript
*nd
,
5685 __unused
struct nfsrv_sock
*slp
,
5686 struct nfsm_chain
*nmrepp
,
5691 struct nfsm_chain nmrep
;
5694 err
= nd
->nd_repstat
;
5695 if (err
&& (nd
->nd_vers
== NFS_VER2
))
5699 * If this is a big reply, use a cluster else
5700 * try and leave leading space for the lower level headers.
5702 siz
+= RPC_REPLYSIZ
;
5703 if (siz
>= nfs_mbuf_minclsize
) {
5704 error
= mbuf_getpacket(MBUF_WAITOK
, &mrep
);
5706 error
= mbuf_gethdr(MBUF_WAITOK
, MBUF_TYPE_DATA
, &mrep
);
5709 /* unable to allocate packet */
5710 /* XXX should we keep statistics for these errors? */
5713 if (siz
< nfs_mbuf_minclsize
) {
5714 /* leave space for lower level headers */
5715 tl
= mbuf_data(mrep
);
5716 tl
+= 80/sizeof(*tl
); /* XXX max_hdr? XXX */
5717 mbuf_setdata(mrep
, tl
, 6 * NFSX_UNSIGNED
);
5719 nfsm_chain_init(&nmrep
, mrep
);
5720 nfsm_chain_add_32(error
, &nmrep
, nd
->nd_retxid
);
5721 nfsm_chain_add_32(error
, &nmrep
, RPC_REPLY
);
5722 if (err
== ERPCMISMATCH
|| (err
& NFSERR_AUTHERR
)) {
5723 nfsm_chain_add_32(error
, &nmrep
, RPC_MSGDENIED
);
5724 if (err
& NFSERR_AUTHERR
) {
5725 nfsm_chain_add_32(error
, &nmrep
, RPC_AUTHERR
);
5726 nfsm_chain_add_32(error
, &nmrep
, (err
& ~NFSERR_AUTHERR
));
5728 nfsm_chain_add_32(error
, &nmrep
, RPC_MISMATCH
);
5729 nfsm_chain_add_32(error
, &nmrep
, RPC_VER2
);
5730 nfsm_chain_add_32(error
, &nmrep
, RPC_VER2
);
5734 nfsm_chain_add_32(error
, &nmrep
, RPC_MSGACCEPTED
);
5735 if (nd
->nd_gss_context
!= NULL
) {
5736 /* RPCSEC_GSS verifier */
5737 error
= nfs_gss_svc_verf_put(nd
, &nmrep
);
5739 nfsm_chain_add_32(error
, &nmrep
, RPC_SYSTEM_ERR
);
5743 /* RPCAUTH_NULL verifier */
5744 nfsm_chain_add_32(error
, &nmrep
, RPCAUTH_NULL
);
5745 nfsm_chain_add_32(error
, &nmrep
, 0);
5747 /* accepted status */
5750 nfsm_chain_add_32(error
, &nmrep
, RPC_PROGUNAVAIL
);
5753 nfsm_chain_add_32(error
, &nmrep
, RPC_PROGMISMATCH
);
5754 /* XXX hard coded versions? */
5755 nfsm_chain_add_32(error
, &nmrep
, NFS_VER2
);
5756 nfsm_chain_add_32(error
, &nmrep
, NFS_VER3
);
5759 nfsm_chain_add_32(error
, &nmrep
, RPC_PROCUNAVAIL
);
5762 nfsm_chain_add_32(error
, &nmrep
, RPC_GARBAGE
);
5765 nfsm_chain_add_32(error
, &nmrep
, RPC_SUCCESS
);
5766 if (nd
->nd_gss_context
!= NULL
)
5767 error
= nfs_gss_svc_prepare_reply(nd
, &nmrep
);
5768 if (err
!= NFSERR_RETVOID
)
5769 nfsm_chain_add_32(error
, &nmrep
,
5770 (err
? nfsrv_errmap(nd
, err
) : 0));
5776 nfsm_chain_build_done(error
, &nmrep
);
5778 /* error composing reply header */
5779 /* XXX should we keep statistics for these errors? */
5785 if ((err
!= 0) && (err
!= NFSERR_RETVOID
))
5786 OSAddAtomic64(1, &nfsstats
.srvrpc_errs
);
5791 * The nfs server send routine.
5793 * - return EINTR or ERESTART if interrupted by a signal
5794 * - return EPIPE if a connection is lost for connection based sockets (TCP...)
5795 * - do any cleanup required by recoverable socket errors (???)
5798 nfsrv_send(struct nfsrv_sock
*slp
, mbuf_t nam
, mbuf_t top
)
5801 socket_t so
= slp
->ns_so
;
5802 struct sockaddr
*sendnam
;
5805 bzero(&msg
, sizeof(msg
));
5806 if (nam
&& !sock_isconnected(so
) && (slp
->ns_sotype
!= SOCK_STREAM
)) {
5807 if ((sendnam
= mbuf_data(nam
))) {
5808 msg
.msg_name
= (caddr_t
)sendnam
;
5809 msg
.msg_namelen
= sendnam
->sa_len
;
5812 error
= sock_sendmbuf(so
, &msg
, top
, 0, NULL
);
5815 log(LOG_INFO
, "nfsd send error %d\n", error
);
5817 if ((error
== EWOULDBLOCK
) && (slp
->ns_sotype
== SOCK_STREAM
))
5818 error
= EPIPE
; /* zap TCP sockets if they time out on send */
5820 /* Handle any recoverable (soft) socket errors here. (???) */
5821 if (error
!= EINTR
&& error
!= ERESTART
&& error
!= EIO
&&
5822 error
!= EWOULDBLOCK
&& error
!= EPIPE
)
5829 * Socket upcall routine for the nfsd sockets.
5830 * The caddr_t arg is a pointer to the "struct nfsrv_sock".
5831 * Essentially do as much as possible non-blocking, else punt and it will
5832 * be called with MBUF_WAITOK from an nfsd.
5835 nfsrv_rcv(socket_t so
, void *arg
, int waitflag
)
5837 struct nfsrv_sock
*slp
= arg
;
5839 if (!nfsd_thread_count
|| !(slp
->ns_flag
& SLP_VALID
))
5842 lck_rw_lock_exclusive(&slp
->ns_rwlock
);
5843 nfsrv_rcv_locked(so
, slp
, waitflag
);
5844 /* Note: ns_rwlock gets dropped when called with MBUF_DONTWAIT */
5847 nfsrv_rcv_locked(socket_t so
, struct nfsrv_sock
*slp
, int waitflag
)
5849 mbuf_t m
, mp
, mhck
, m2
;
5850 int ns_flag
=0, error
;
5854 if ((slp
->ns_flag
& SLP_VALID
) == 0) {
5855 if (waitflag
== MBUF_DONTWAIT
)
5856 lck_rw_done(&slp
->ns_rwlock
);
5862 * Define this to test for nfsds handling this under heavy load.
5864 if (waitflag
== MBUF_DONTWAIT
) {
5865 ns_flag
= SLP_NEEDQ
;
5869 if (slp
->ns_sotype
== SOCK_STREAM
) {
5871 * If there are already records on the queue, defer soreceive()
5872 * to an(other) nfsd so that there is feedback to the TCP layer that
5873 * the nfs servers are heavily loaded.
5876 ns_flag
= SLP_NEEDQ
;
5883 bytes_read
= 1000000000;
5884 error
= sock_receivembuf(so
, NULL
, &mp
, MSG_DONTWAIT
, &bytes_read
);
5885 if (error
|| mp
== NULL
) {
5886 if (error
== EWOULDBLOCK
)
5887 ns_flag
= (waitflag
== MBUF_DONTWAIT
) ? SLP_NEEDQ
: 0;
5889 ns_flag
= SLP_DISCONN
;
5893 if (slp
->ns_rawend
) {
5894 if ((error
= mbuf_setnext(slp
->ns_rawend
, m
)))
5895 panic("nfsrv_rcv: mbuf_setnext failed %d\n", error
);
5896 slp
->ns_cc
+= bytes_read
;
5899 slp
->ns_cc
= bytes_read
;
5901 while ((m2
= mbuf_next(m
)))
5906 * Now try and parse record(s) out of the raw stream data.
5908 error
= nfsrv_getstream(slp
, waitflag
);
5911 ns_flag
= SLP_DISCONN
;
5913 ns_flag
= SLP_NEEDQ
;
5916 struct sockaddr_storage nam
;
5918 if (slp
->ns_reccnt
>= nfsrv_sock_max_rec_queue_length
) {
5919 /* already have max # RPC records queued on this socket */
5920 ns_flag
= SLP_NEEDQ
;
5924 bzero(&msg
, sizeof(msg
));
5925 msg
.msg_name
= (caddr_t
)&nam
;
5926 msg
.msg_namelen
= sizeof(nam
);
5929 bytes_read
= 1000000000;
5930 error
= sock_receivembuf(so
, &msg
, &mp
, MSG_DONTWAIT
| MSG_NEEDSA
, &bytes_read
);
5932 if (msg
.msg_name
&& (mbuf_get(MBUF_WAITOK
, MBUF_TYPE_SONAME
, &mhck
) == 0)) {
5933 mbuf_setlen(mhck
, nam
.ss_len
);
5934 bcopy(&nam
, mbuf_data(mhck
), nam
.ss_len
);
5936 if (mbuf_setnext(m
, mp
)) {
5937 /* trouble... just drop it */
5938 printf("nfsrv_rcv: mbuf_setnext failed\n");
5946 mbuf_setnextpkt(slp
->ns_recend
, m
);
5949 slp
->ns_flag
|= SLP_DOREC
;
5952 mbuf_setnextpkt(m
, NULL
);
5959 * Now try and process the request records, non-blocking.
5963 slp
->ns_flag
|= ns_flag
;
5964 if (waitflag
== MBUF_DONTWAIT
) {
5965 int wake
= (slp
->ns_flag
& SLP_WORKTODO
);
5966 lck_rw_done(&slp
->ns_rwlock
);
5967 if (wake
&& nfsd_thread_count
) {
5968 lck_mtx_lock(nfsd_mutex
);
5969 nfsrv_wakenfsd(slp
);
5970 lck_mtx_unlock(nfsd_mutex
);
5976 * Try and extract an RPC request from the mbuf data list received on a
5977 * stream socket. The "waitflag" argument indicates whether or not it
5981 nfsrv_getstream(struct nfsrv_sock
*slp
, int waitflag
)
5984 char *cp1
, *cp2
, *mdata
;
5985 int len
, mlen
, error
;
5986 mbuf_t om
, m2
, recm
;
5989 if (slp
->ns_flag
& SLP_GETSTREAM
)
5990 panic("nfs getstream");
5991 slp
->ns_flag
|= SLP_GETSTREAM
;
5993 if (slp
->ns_reclen
== 0) {
5994 if (slp
->ns_cc
< NFSX_UNSIGNED
) {
5995 slp
->ns_flag
&= ~SLP_GETSTREAM
;
5999 mdata
= mbuf_data(m
);
6001 if (mlen
>= NFSX_UNSIGNED
) {
6002 bcopy(mdata
, (caddr_t
)&recmark
, NFSX_UNSIGNED
);
6003 mdata
+= NFSX_UNSIGNED
;
6004 mlen
-= NFSX_UNSIGNED
;
6005 mbuf_setdata(m
, mdata
, mlen
);
6007 cp1
= (caddr_t
)&recmark
;
6009 while (cp1
< ((caddr_t
)&recmark
) + NFSX_UNSIGNED
) {
6017 mbuf_setdata(m
, cp2
, mlen
);
6020 slp
->ns_cc
-= NFSX_UNSIGNED
;
6021 recmark
= ntohl(recmark
);
6022 slp
->ns_reclen
= recmark
& ~0x80000000;
6023 if (recmark
& 0x80000000)
6024 slp
->ns_flag
|= SLP_LASTFRAG
;
6026 slp
->ns_flag
&= ~SLP_LASTFRAG
;
6027 if (slp
->ns_reclen
<= 0 || slp
->ns_reclen
> NFS_MAXPACKET
) {
6028 slp
->ns_flag
&= ~SLP_GETSTREAM
;
6034 * Now get the record part.
6036 * Note that slp->ns_reclen may be 0. Linux sometimes
6037 * generates 0-length RPCs
6040 if (slp
->ns_cc
== slp
->ns_reclen
) {
6042 slp
->ns_raw
= slp
->ns_rawend
= NULL
;
6043 slp
->ns_cc
= slp
->ns_reclen
= 0;
6044 } else if (slp
->ns_cc
> slp
->ns_reclen
) {
6048 mdata
= mbuf_data(m
);
6050 while (len
< slp
->ns_reclen
) {
6051 if ((len
+ mlen
) > slp
->ns_reclen
) {
6052 if (mbuf_copym(m
, 0, slp
->ns_reclen
- len
, waitflag
, &m2
)) {
6053 slp
->ns_flag
&= ~SLP_GETSTREAM
;
6054 return (EWOULDBLOCK
);
6057 if (mbuf_setnext(om
, m2
)) {
6058 /* trouble... just drop it */
6059 printf("nfsrv_getstream: mbuf_setnext failed\n");
6061 slp
->ns_flag
&= ~SLP_GETSTREAM
;
6062 return (EWOULDBLOCK
);
6068 mdata
+= slp
->ns_reclen
- len
;
6069 mlen
-= slp
->ns_reclen
- len
;
6070 mbuf_setdata(m
, mdata
, mlen
);
6071 len
= slp
->ns_reclen
;
6072 } else if ((len
+ mlen
) == slp
->ns_reclen
) {
6077 if (mbuf_setnext(om
, NULL
)) {
6078 printf("nfsrv_getstream: mbuf_setnext failed 2\n");
6079 slp
->ns_flag
&= ~SLP_GETSTREAM
;
6080 return (EWOULDBLOCK
);
6083 mdata
= mbuf_data(m
);
6089 mdata
= mbuf_data(m
);
6096 slp
->ns_flag
&= ~SLP_GETSTREAM
;
6101 * Accumulate the fragments into a record.
6103 if (slp
->ns_frag
== NULL
) {
6104 slp
->ns_frag
= recm
;
6107 while ((m2
= mbuf_next(m
)))
6109 if ((error
= mbuf_setnext(m
, recm
)))
6110 panic("nfsrv_getstream: mbuf_setnext failed 3, %d\n", error
);
6112 if (slp
->ns_flag
& SLP_LASTFRAG
) {
6114 mbuf_setnextpkt(slp
->ns_recend
, slp
->ns_frag
);
6116 slp
->ns_rec
= slp
->ns_frag
;
6117 slp
->ns_flag
|= SLP_DOREC
;
6119 slp
->ns_recend
= slp
->ns_frag
;
6120 slp
->ns_frag
= NULL
;
6126 * Parse an RPC header.
6130 struct nfsrv_sock
*slp
,
6132 struct nfsrv_descript
**ndp
)
6136 struct nfsrv_descript
*nd
;
6140 if (!(slp
->ns_flag
& (SLP_VALID
|SLP_DOREC
)) || (slp
->ns_rec
== NULL
))
6142 MALLOC_ZONE(nd
, struct nfsrv_descript
*,
6143 sizeof (struct nfsrv_descript
), M_NFSRVDESC
, M_WAITOK
);
6147 slp
->ns_rec
= mbuf_nextpkt(m
);
6149 mbuf_setnextpkt(m
, NULL
);
6151 slp
->ns_flag
&= ~SLP_DOREC
;
6152 slp
->ns_recend
= NULL
;
6155 if (mbuf_type(m
) == MBUF_TYPE_SONAME
) {
6158 if ((error
= mbuf_setnext(nam
, NULL
)))
6159 panic("nfsrv_dorec: mbuf_setnext failed %d\n", error
);
6163 nfsm_chain_dissect_init(error
, &nd
->nd_nmreq
, m
);
6165 error
= nfsrv_getreq(nd
);
6169 if (nd
->nd_gss_context
)
6170 nfs_gss_svc_ctx_deref(nd
->nd_gss_context
);
6171 FREE_ZONE(nd
, sizeof(*nd
), M_NFSRVDESC
);
6181 * Parse an RPC request
6183 * - fill in the cred struct.
6186 nfsrv_getreq(struct nfsrv_descript
*nd
)
6188 struct nfsm_chain
*nmreq
;
6190 u_int32_t nfsvers
, auth_type
;
6198 nd
->nd_gss_context
= NULL
;
6199 nd
->nd_gss_seqnum
= 0;
6200 nd
->nd_gss_mb
= NULL
;
6202 user_id
= group_id
= -2;
6203 val
= auth_type
= len
= 0;
6205 nmreq
= &nd
->nd_nmreq
;
6206 nfsm_chain_get_32(error
, nmreq
, nd
->nd_retxid
); // XID
6207 nfsm_chain_get_32(error
, nmreq
, val
); // RPC Call
6208 if (!error
&& (val
!= RPC_CALL
))
6212 nfsm_chain_get_32(error
, nmreq
, val
); // RPC Version
6214 if (val
!= RPC_VER2
) {
6215 nd
->nd_repstat
= ERPCMISMATCH
;
6216 nd
->nd_procnum
= NFSPROC_NOOP
;
6219 nfsm_chain_get_32(error
, nmreq
, val
); // RPC Program Number
6221 if (val
!= NFS_PROG
) {
6222 nd
->nd_repstat
= EPROGUNAVAIL
;
6223 nd
->nd_procnum
= NFSPROC_NOOP
;
6226 nfsm_chain_get_32(error
, nmreq
, nfsvers
);// NFS Version Number
6228 if ((nfsvers
< NFS_VER2
) || (nfsvers
> NFS_VER3
)) {
6229 nd
->nd_repstat
= EPROGMISMATCH
;
6230 nd
->nd_procnum
= NFSPROC_NOOP
;
6233 nd
->nd_vers
= nfsvers
;
6234 nfsm_chain_get_32(error
, nmreq
, nd
->nd_procnum
);// NFS Procedure Number
6236 if ((nd
->nd_procnum
>= NFS_NPROCS
) ||
6237 ((nd
->nd_vers
== NFS_VER2
) && (nd
->nd_procnum
> NFSV2PROC_STATFS
))) {
6238 nd
->nd_repstat
= EPROCUNAVAIL
;
6239 nd
->nd_procnum
= NFSPROC_NOOP
;
6242 if (nfsvers
!= NFS_VER3
)
6243 nd
->nd_procnum
= nfsv3_procid
[nd
->nd_procnum
];
6244 nfsm_chain_get_32(error
, nmreq
, auth_type
); // Auth Flavor
6245 nfsm_chain_get_32(error
, nmreq
, len
); // Auth Length
6246 if (!error
&& (len
< 0 || len
> RPCAUTH_MAXSIZ
))
6250 /* Handle authentication */
6251 if (auth_type
== RPCAUTH_SYS
) {
6252 struct posix_cred temp_pcred
;
6253 if (nd
->nd_procnum
== NFSPROC_NULL
)
6255 nd
->nd_sec
= RPCAUTH_SYS
;
6256 nfsm_chain_adv(error
, nmreq
, NFSX_UNSIGNED
); // skip stamp
6257 nfsm_chain_get_32(error
, nmreq
, len
); // hostname length
6258 if (len
< 0 || len
> NFS_MAXNAMLEN
)
6260 nfsm_chain_adv(error
, nmreq
, nfsm_rndup(len
)); // skip hostname
6263 /* create a temporary credential using the bits from the wire */
6264 bzero(&temp_pcred
, sizeof(temp_pcred
));
6265 nfsm_chain_get_32(error
, nmreq
, user_id
);
6266 nfsm_chain_get_32(error
, nmreq
, group_id
);
6267 temp_pcred
.cr_groups
[0] = group_id
;
6268 nfsm_chain_get_32(error
, nmreq
, len
); // extra GID count
6269 if ((len
< 0) || (len
> RPCAUTH_UNIXGIDS
))
6272 for (i
= 1; i
<= len
; i
++)
6274 nfsm_chain_get_32(error
, nmreq
, temp_pcred
.cr_groups
[i
]);
6276 nfsm_chain_adv(error
, nmreq
, NFSX_UNSIGNED
);
6278 ngroups
= (len
>= NGROUPS
) ? NGROUPS
: (len
+ 1);
6280 nfsrv_group_sort(&temp_pcred
.cr_groups
[0], ngroups
);
6281 nfsm_chain_adv(error
, nmreq
, NFSX_UNSIGNED
); // verifier flavor (should be AUTH_NONE)
6282 nfsm_chain_get_32(error
, nmreq
, len
); // verifier length
6283 if (len
< 0 || len
> RPCAUTH_MAXSIZ
)
6286 nfsm_chain_adv(error
, nmreq
, nfsm_rndup(len
));
6288 /* request creation of a real credential */
6289 temp_pcred
.cr_uid
= user_id
;
6290 temp_pcred
.cr_ngroups
= ngroups
;
6291 nd
->nd_cr
= posix_cred_create(&temp_pcred
);
6292 if (nd
->nd_cr
== NULL
) {
6293 nd
->nd_repstat
= ENOMEM
;
6294 nd
->nd_procnum
= NFSPROC_NOOP
;
6297 } else if (auth_type
== RPCSEC_GSS
) {
6298 error
= nfs_gss_svc_cred_get(nd
, nmreq
);
6300 if (error
== EINVAL
)
6301 goto nfsmout
; // drop the request
6302 nd
->nd_repstat
= error
;
6303 nd
->nd_procnum
= NFSPROC_NOOP
;
6307 if (nd
->nd_procnum
== NFSPROC_NULL
) // assume it's AUTH_NONE
6309 nd
->nd_repstat
= (NFSERR_AUTHERR
| AUTH_REJECTCRED
);
6310 nd
->nd_procnum
= NFSPROC_NOOP
;
6315 if (IS_VALID_CRED(nd
->nd_cr
))
6316 kauth_cred_unref(&nd
->nd_cr
);
6317 nfsm_chain_cleanup(nmreq
);
6322 * Search for a sleeping nfsd and wake it up.
6323 * SIDE EFFECT: If none found, make sure the socket is queued up so that one
6324 * of the running nfsds will go look for the work in the nfsrv_sockwait list.
6325 * Note: Must be called with nfsd_mutex held.
6328 nfsrv_wakenfsd(struct nfsrv_sock
*slp
)
6332 if ((slp
->ns_flag
& SLP_VALID
) == 0)
6335 lck_rw_lock_exclusive(&slp
->ns_rwlock
);
6336 /* if there's work to do on this socket, make sure it's queued up */
6337 if ((slp
->ns_flag
& SLP_WORKTODO
) && !(slp
->ns_flag
& SLP_QUEUED
)) {
6338 TAILQ_INSERT_TAIL(&nfsrv_sockwait
, slp
, ns_svcq
);
6339 slp
->ns_flag
|= SLP_WAITQ
;
6341 lck_rw_done(&slp
->ns_rwlock
);
6343 /* wake up a waiting nfsd, if possible */
6344 nd
= TAILQ_FIRST(&nfsd_queue
);
6348 TAILQ_REMOVE(&nfsd_queue
, nd
, nfsd_queue
);
6349 nd
->nfsd_flag
&= ~NFSD_WAITING
;
6353 #endif /* NFSSERVER */