]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
3e170ce0 | 2 | * Copyright (c) 2000-2015 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
0a7de745 | 5 | * |
2d21ac55 A |
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. | |
0a7de745 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
0a7de745 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */ | |
29 | /* | |
30 | * Copyright (c) 1989, 1991, 1993, 1995 | |
31 | * The Regents of the University of California. All rights reserved. | |
32 | * | |
33 | * This code is derived from software contributed to Berkeley by | |
34 | * Rick Macklem at The University of Guelph. | |
35 | * | |
36 | * Redistribution and use in source and binary forms, with or without | |
37 | * modification, are permitted provided that the following conditions | |
38 | * are met: | |
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. | |
51 | * | |
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 | |
62 | * SUCH DAMAGE. | |
63 | * | |
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 $ | |
66 | */ | |
67 | ||
68 | /* | |
69 | * Socket operations for use by nfs | |
70 | */ | |
71 | ||
72 | #include <sys/param.h> | |
73 | #include <sys/systm.h> | |
74 | #include <sys/proc.h> | |
6d2010ae | 75 | #include <sys/signalvar.h> |
91447636 A |
76 | #include <sys/kauth.h> |
77 | #include <sys/mount_internal.h> | |
1c79356b | 78 | #include <sys/kernel.h> |
91447636 | 79 | #include <sys/kpi_mbuf.h> |
1c79356b A |
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> | |
1c79356b A |
85 | #include <sys/syslog.h> |
86 | #include <sys/tprintf.h> | |
91447636 | 87 | #include <libkern/OSAtomic.h> |
1c79356b A |
88 | |
89 | #include <sys/time.h> | |
90 | #include <kern/clock.h> | |
4a249263 A |
91 | #include <kern/task.h> |
92 | #include <kern/thread.h> | |
2d21ac55 | 93 | #include <kern/thread_call.h> |
9bccf70c | 94 | #include <sys/user.h> |
6d2010ae | 95 | #include <sys/acct.h> |
1c79356b A |
96 | |
97 | #include <netinet/in.h> | |
98 | #include <netinet/tcp.h> | |
99 | ||
100 | #include <nfs/rpcv2.h> | |
6d2010ae | 101 | #include <nfs/krpc.h> |
1c79356b A |
102 | #include <nfs/nfsproto.h> |
103 | #include <nfs/nfs.h> | |
104 | #include <nfs/xdr_subs.h> | |
105 | #include <nfs/nfsm_subs.h> | |
2d21ac55 | 106 | #include <nfs/nfs_gss.h> |
1c79356b A |
107 | #include <nfs/nfsmount.h> |
108 | #include <nfs/nfsnode.h> | |
1c79356b | 109 | |
39236c6e A |
110 | #define NFS_SOCK_DBG(...) NFS_DBG(NFS_FAC_SOCK, 7, ## __VA_ARGS__) |
111 | ||
2d21ac55 | 112 | /* XXX */ |
0a7de745 A |
113 | boolean_t current_thread_aborted(void); |
114 | kern_return_t thread_terminate(thread_t); | |
2d21ac55 A |
115 | |
116 | ||
117 | #if NFSSERVER | |
118 | int nfsrv_sock_max_rec_queue_length = 128; /* max # RPC records queued on (UDP) socket */ | |
119 | ||
0a7de745 | 120 | int nfsrv_getstream(struct nfsrv_sock *, int); |
b0d623f7 | 121 | int nfsrv_getreq(struct nfsrv_descript *); |
2d21ac55 A |
122 | extern int nfsv3_procid[NFS_NPROCS]; |
123 | #endif /* NFSSERVER */ | |
124 | ||
6d2010ae A |
125 | /* |
126 | * compare two sockaddr structures | |
127 | */ | |
128 | int | |
129 | nfs_sockaddr_cmp(struct sockaddr *sa1, struct sockaddr *sa2) | |
130 | { | |
0a7de745 A |
131 | if (!sa1) { |
132 | return -1; | |
133 | } | |
134 | if (!sa2) { | |
135 | return 1; | |
136 | } | |
137 | if (sa1->sa_family != sa2->sa_family) { | |
138 | return (sa1->sa_family < sa2->sa_family) ? -1 : 1; | |
139 | } | |
140 | if (sa1->sa_len != sa2->sa_len) { | |
141 | return (sa1->sa_len < sa2->sa_len) ? -1 : 1; | |
142 | } | |
143 | if (sa1->sa_family == AF_INET) { | |
144 | return bcmp(&((struct sockaddr_in*)sa1)->sin_addr, | |
145 | &((struct sockaddr_in*)sa2)->sin_addr, sizeof(((struct sockaddr_in*)sa1)->sin_addr)); | |
146 | } | |
147 | if (sa1->sa_family == AF_INET6) { | |
148 | return bcmp(&((struct sockaddr_in6*)sa1)->sin6_addr, | |
149 | &((struct sockaddr_in6*)sa2)->sin6_addr, sizeof(((struct sockaddr_in6*)sa1)->sin6_addr)); | |
150 | } | |
151 | return -1; | |
6d2010ae A |
152 | } |
153 | ||
2d21ac55 A |
154 | #if NFSCLIENT |
155 | ||
0a7de745 A |
156 | int nfs_connect_search_new_socket(struct nfsmount *, struct nfs_socket_search *, struct timeval *); |
157 | int nfs_connect_search_socket_connect(struct nfsmount *, struct nfs_socket *, int); | |
158 | int nfs_connect_search_ping(struct nfsmount *, struct nfs_socket *, struct timeval *); | |
159 | void nfs_connect_search_socket_found(struct nfsmount *, struct nfs_socket_search *, struct nfs_socket *); | |
160 | void nfs_connect_search_socket_reap(struct nfsmount *, struct nfs_socket_search *, struct timeval *); | |
161 | int nfs_connect_search_check(struct nfsmount *, struct nfs_socket_search *, struct timeval *); | |
162 | int nfs_reconnect(struct nfsmount *); | |
163 | int nfs_connect_setup(struct nfsmount *); | |
164 | void nfs_mount_sock_thread(void *, wait_result_t); | |
165 | void nfs_udp_rcv(socket_t, void*, int); | |
166 | void nfs_tcp_rcv(socket_t, void*, int); | |
167 | void nfs_sock_poke(struct nfsmount *); | |
168 | void nfs_request_match_reply(struct nfsmount *, mbuf_t); | |
169 | void nfs_reqdequeue(struct nfsreq *); | |
170 | void nfs_reqbusy(struct nfsreq *); | |
b0d623f7 | 171 | struct nfsreq *nfs_reqnext(struct nfsreq *); |
0a7de745 A |
172 | int nfs_wait_reply(struct nfsreq *); |
173 | void nfs_softterm(struct nfsreq *); | |
174 | int nfs_can_squish(struct nfsmount *); | |
175 | int nfs_is_squishy(struct nfsmount *); | |
176 | int nfs_is_dead(int, struct nfsmount *); | |
fa4905b1 | 177 | |
1c79356b A |
178 | /* |
179 | * Estimate rto for an nfs rpc sent via. an unreliable datagram. | |
180 | * Use the mean and mean deviation of rtt for the appropriate type of rpc | |
181 | * for the frequent rpcs and a default for the others. | |
182 | * The justification for doing "other" this way is that these rpcs | |
183 | * happen so infrequently that timer est. would probably be stale. | |
184 | * Also, since many of these rpcs are | |
185 | * non-idempotent, a conservative timeout is desired. | |
186 | * getattr, lookup - A+2D | |
187 | * read, write - A+4D | |
188 | * other - nm_timeo | |
189 | */ | |
0a7de745 | 190 | #define NFS_RTO(n, t) \ |
1c79356b A |
191 | ((t) == 0 ? (n)->nm_timeo : \ |
192 | ((t) < 3 ? \ | |
193 | (((((n)->nm_srtt[t-1] + 3) >> 2) + (n)->nm_sdrtt[t-1] + 1) >> 1) : \ | |
194 | ((((n)->nm_srtt[t-1] + 7) >> 3) + (n)->nm_sdrtt[t-1] + 1))) | |
0a7de745 A |
195 | #define NFS_SRTT(r) (r)->r_nmp->nm_srtt[proct[(r)->r_procnum] - 1] |
196 | #define NFS_SDRTT(r) (r)->r_nmp->nm_sdrtt[proct[(r)->r_procnum] - 1] | |
1c79356b A |
197 | |
198 | /* | |
199 | * Defines which timer to use for the procnum. | |
200 | * 0 - default | |
201 | * 1 - getattr | |
202 | * 2 - lookup | |
203 | * 3 - read | |
204 | * 4 - write | |
205 | */ | |
206 | static int proct[NFS_NPROCS] = { | |
91447636 | 207 | 0, 1, 0, 2, 1, 3, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0 |
1c79356b A |
208 | }; |
209 | ||
210 | /* | |
211 | * There is a congestion window for outstanding rpcs maintained per mount | |
212 | * point. The cwnd size is adjusted in roughly the way that: | |
213 | * Van Jacobson, Congestion avoidance and Control, In "Proceedings of | |
214 | * SIGCOMM '88". ACM, August 1988. | |
215 | * describes for TCP. The cwnd size is chopped in half on a retransmit timeout | |
216 | * and incremented by 1/cwnd when each rpc reply is received and a full cwnd | |
217 | * of rpcs is in progress. | |
218 | * (The sent count and cwnd are scaled for integer arith.) | |
219 | * Variants of "slow start" were tried and were found to be too much of a | |
220 | * performance hit (ave. rtt 3 times larger), | |
221 | * I suspect due to the large rtt that nfs rpcs have. | |
222 | */ | |
0a7de745 A |
223 | #define NFS_CWNDSCALE 256 |
224 | #define NFS_MAXCWND (NFS_CWNDSCALE * 32) | |
1c79356b | 225 | static int nfs_backoff[8] = { 2, 4, 8, 16, 32, 64, 128, 256, }; |
4a249263 | 226 | |
1c79356b | 227 | /* |
6d2010ae | 228 | * Increment location index to next address/server/location. |
1c79356b | 229 | */ |
6d2010ae A |
230 | void |
231 | nfs_location_next(struct nfs_fs_locations *nlp, struct nfs_location_index *nlip) | |
1c79356b | 232 | { |
6d2010ae A |
233 | uint8_t loc = nlip->nli_loc; |
234 | uint8_t serv = nlip->nli_serv; | |
235 | uint8_t addr = nlip->nli_addr; | |
236 | ||
237 | /* move to next address */ | |
238 | addr++; | |
239 | if (addr >= nlp->nl_locations[loc]->nl_servers[serv]->ns_addrcount) { | |
240 | /* no more addresses on current server, go to first address of next server */ | |
241 | next_server: | |
242 | addr = 0; | |
243 | serv++; | |
244 | if (serv >= nlp->nl_locations[loc]->nl_servcount) { | |
245 | /* no more servers on current location, go to first server of next location */ | |
246 | serv = 0; | |
247 | loc++; | |
0a7de745 | 248 | if (loc >= nlp->nl_numlocs) { |
6d2010ae | 249 | loc = 0; /* after last location, wrap back around to first location */ |
0a7de745 | 250 | } |
b0d623f7 | 251 | } |
1c79356b | 252 | } |
1c79356b | 253 | /* |
6d2010ae A |
254 | * It's possible for this next server to not have any addresses. |
255 | * Check for that here and go to the next server. | |
256 | * But bail out if we've managed to come back around to the original | |
257 | * location that was passed in. (That would mean no servers had any | |
258 | * addresses. And we don't want to spin here forever.) | |
1c79356b | 259 | */ |
0a7de745 | 260 | if ((loc == nlip->nli_loc) && (serv == nlip->nli_serv) && (addr == nlip->nli_addr)) { |
6d2010ae | 261 | return; |
0a7de745 A |
262 | } |
263 | if (addr >= nlp->nl_locations[loc]->nl_servers[serv]->ns_addrcount) { | |
6d2010ae | 264 | goto next_server; |
0a7de745 | 265 | } |
6d2010ae A |
266 | |
267 | nlip->nli_loc = loc; | |
268 | nlip->nli_serv = serv; | |
269 | nlip->nli_addr = addr; | |
270 | } | |
271 | ||
272 | /* | |
273 | * Compare two location indices. | |
274 | */ | |
275 | int | |
276 | nfs_location_index_cmp(struct nfs_location_index *nlip1, struct nfs_location_index *nlip2) | |
277 | { | |
0a7de745 A |
278 | if (nlip1->nli_loc != nlip2->nli_loc) { |
279 | return nlip1->nli_loc - nlip2->nli_loc; | |
280 | } | |
281 | if (nlip1->nli_serv != nlip2->nli_serv) { | |
282 | return nlip1->nli_serv - nlip2->nli_serv; | |
283 | } | |
284 | return nlip1->nli_addr - nlip2->nli_addr; | |
6d2010ae A |
285 | } |
286 | ||
287 | /* | |
288 | * Get the mntfromname (or path portion only) for a given location. | |
289 | */ | |
290 | void | |
291 | nfs_location_mntfromname(struct nfs_fs_locations *locs, struct nfs_location_index idx, char *s, int size, int pathonly) | |
292 | { | |
293 | struct nfs_fs_location *fsl = locs->nl_locations[idx.nli_loc]; | |
294 | char *p; | |
295 | int cnt, i; | |
296 | ||
297 | p = s; | |
298 | if (!pathonly) { | |
299 | cnt = snprintf(p, size, "%s:", fsl->nl_servers[idx.nli_serv]->ns_name); | |
300 | p += cnt; | |
301 | size -= cnt; | |
302 | } | |
303 | if (fsl->nl_path.np_compcount == 0) { | |
304 | /* mounting root export on server */ | |
305 | if (size > 0) { | |
306 | *p++ = '/'; | |
307 | *p++ = '\0'; | |
1c79356b | 308 | } |
6d2010ae A |
309 | return; |
310 | } | |
311 | /* append each server path component */ | |
0a7de745 | 312 | for (i = 0; (size > 0) && (i < (int)fsl->nl_path.np_compcount); i++) { |
6d2010ae A |
313 | cnt = snprintf(p, size, "/%s", fsl->nl_path.np_components[i]); |
314 | p += cnt; | |
315 | size -= cnt; | |
316 | } | |
317 | } | |
2d21ac55 | 318 | |
6d2010ae A |
319 | /* |
320 | * NFS client connect socket upcall. | |
321 | * (Used only during socket connect/search.) | |
322 | */ | |
323 | void | |
324 | nfs_connect_upcall(socket_t so, void *arg, __unused int waitflag) | |
325 | { | |
326 | struct nfs_socket *nso = arg; | |
327 | size_t rcvlen; | |
328 | mbuf_t m; | |
329 | int error = 0, recv = 1; | |
330 | ||
331 | if (nso->nso_flags & NSO_CONNECTING) { | |
39236c6e | 332 | NFS_SOCK_DBG("nfs connect - socket %p upcall - connecting\n", nso); |
6d2010ae A |
333 | wakeup(nso->nso_wake); |
334 | return; | |
335 | } | |
336 | ||
337 | lck_mtx_lock(&nso->nso_lock); | |
0a7de745 | 338 | if ((nso->nso_flags & (NSO_UPCALL | NSO_DISCONNECTING | NSO_DEAD)) || !(nso->nso_flags & NSO_PINGING)) { |
39236c6e | 339 | NFS_SOCK_DBG("nfs connect - socket %p upcall - nevermind\n", nso); |
6d2010ae A |
340 | lck_mtx_unlock(&nso->nso_lock); |
341 | return; | |
342 | } | |
39236c6e | 343 | NFS_SOCK_DBG("nfs connect - socket %p upcall\n", nso); |
6d2010ae A |
344 | nso->nso_flags |= NSO_UPCALL; |
345 | ||
346 | /* loop while we make error-free progress */ | |
347 | while (!error && recv) { | |
348 | /* make sure we're still interested in this socket */ | |
0a7de745 | 349 | if (nso->nso_flags & (NSO_DISCONNECTING | NSO_DEAD)) { |
6d2010ae | 350 | break; |
0a7de745 | 351 | } |
6d2010ae A |
352 | lck_mtx_unlock(&nso->nso_lock); |
353 | m = NULL; | |
354 | if (nso->nso_sotype == SOCK_STREAM) { | |
355 | error = nfs_rpc_record_read(so, &nso->nso_rrs, MSG_DONTWAIT, &recv, &m); | |
356 | } else { | |
357 | rcvlen = 1000000; | |
358 | error = sock_receivembuf(so, NULL, &m, MSG_DONTWAIT, &rcvlen); | |
359 | recv = m ? 1 : 0; | |
360 | } | |
361 | lck_mtx_lock(&nso->nso_lock); | |
362 | if (m) { | |
363 | /* match response with request */ | |
364 | struct nfsm_chain nmrep; | |
365 | uint32_t reply = 0, rxid = 0, verf_type, verf_len; | |
366 | uint32_t reply_status, rejected_status, accepted_status; | |
367 | ||
368 | nfsm_chain_dissect_init(error, &nmrep, m); | |
369 | nfsm_chain_get_32(error, &nmrep, rxid); | |
370 | nfsm_chain_get_32(error, &nmrep, reply); | |
0a7de745 | 371 | if (!error && ((reply != RPC_REPLY) || (rxid != nso->nso_pingxid))) { |
6d2010ae | 372 | error = EBADRPC; |
0a7de745 | 373 | } |
6d2010ae A |
374 | nfsm_chain_get_32(error, &nmrep, reply_status); |
375 | if (!error && (reply_status == RPC_MSGDENIED)) { | |
376 | nfsm_chain_get_32(error, &nmrep, rejected_status); | |
0a7de745 | 377 | if (!error) { |
6d2010ae | 378 | error = (rejected_status == RPC_MISMATCH) ? ERPCMISMATCH : EACCES; |
0a7de745 | 379 | } |
2d21ac55 | 380 | } |
6d2010ae A |
381 | nfsm_chain_get_32(error, &nmrep, verf_type); /* verifier flavor */ |
382 | nfsm_chain_get_32(error, &nmrep, verf_len); /* verifier length */ | |
383 | nfsmout_if(error); | |
0a7de745 | 384 | if (verf_len) { |
6d2010ae | 385 | nfsm_chain_adv(error, &nmrep, nfsm_rndup(verf_len)); |
0a7de745 | 386 | } |
6d2010ae A |
387 | nfsm_chain_get_32(error, &nmrep, accepted_status); |
388 | nfsmout_if(error); | |
389 | if ((accepted_status == RPC_PROGMISMATCH) && !nso->nso_version) { | |
390 | uint32_t minvers, maxvers; | |
391 | nfsm_chain_get_32(error, &nmrep, minvers); | |
392 | nfsm_chain_get_32(error, &nmrep, maxvers); | |
393 | nfsmout_if(error); | |
394 | if (nso->nso_protocol == PMAPPROG) { | |
0a7de745 | 395 | if ((minvers > RPCBVERS4) || (maxvers < PMAPVERS)) { |
6d2010ae | 396 | error = EPROGMISMATCH; |
0a7de745 A |
397 | } else if ((nso->nso_saddr->sa_family == AF_INET) && |
398 | (PMAPVERS >= minvers) && (PMAPVERS <= maxvers)) { | |
6d2010ae | 399 | nso->nso_version = PMAPVERS; |
0a7de745 A |
400 | } else if (nso->nso_saddr->sa_family == AF_INET6) { |
401 | if ((RPCBVERS4 >= minvers) && (RPCBVERS4 <= maxvers)) { | |
6d2010ae | 402 | nso->nso_version = RPCBVERS4; |
0a7de745 | 403 | } else if ((RPCBVERS3 >= minvers) && (RPCBVERS3 <= maxvers)) { |
6d2010ae | 404 | nso->nso_version = RPCBVERS3; |
0a7de745 | 405 | } |
6d2010ae A |
406 | } |
407 | } else if (nso->nso_protocol == NFS_PROG) { | |
3e170ce0 A |
408 | int vers; |
409 | ||
410 | /* | |
411 | * N.B. Both portmapper and rpcbind V3 are happy to return | |
412 | * addresses for other versions than the one you ask (getport or | |
413 | * getaddr) and thus we may have fallen to this code path. So if | |
414 | * we get a version that we support, use highest supported | |
415 | * version. This assumes that the server supports all versions | |
416 | * between minvers and maxvers. Note for IPv6 we will try and | |
417 | * use rpcbind V4 which has getversaddr and we should not get | |
418 | * here if that was successful. | |
419 | */ | |
420 | for (vers = nso->nso_nfs_max_vers; vers >= (int)nso->nso_nfs_min_vers; vers--) { | |
0a7de745 A |
421 | if (vers >= (int)minvers && vers <= (int)maxvers) { |
422 | break; | |
423 | } | |
3e170ce0 A |
424 | } |
425 | nso->nso_version = (vers < (int)nso->nso_nfs_min_vers) ? 0 : vers; | |
6d2010ae | 426 | } |
0a7de745 | 427 | if (!error && nso->nso_version) { |
6d2010ae | 428 | accepted_status = RPC_SUCCESS; |
0a7de745 | 429 | } |
1c79356b | 430 | } |
6d2010ae A |
431 | if (!error) { |
432 | switch (accepted_status) { | |
433 | case RPC_SUCCESS: | |
434 | error = 0; | |
435 | break; | |
436 | case RPC_PROGUNAVAIL: | |
437 | error = EPROGUNAVAIL; | |
438 | break; | |
439 | case RPC_PROGMISMATCH: | |
440 | error = EPROGMISMATCH; | |
441 | break; | |
442 | case RPC_PROCUNAVAIL: | |
443 | error = EPROCUNAVAIL; | |
444 | break; | |
445 | case RPC_GARBAGE: | |
446 | error = EBADRPC; | |
447 | break; | |
448 | case RPC_SYSTEM_ERR: | |
449 | default: | |
450 | error = EIO; | |
451 | break; | |
452 | } | |
453 | } | |
454 | nfsmout: | |
455 | nso->nso_flags &= ~NSO_PINGING; | |
456 | if (error) { | |
457 | nso->nso_error = error; | |
458 | nso->nso_flags |= NSO_DEAD; | |
459 | } else { | |
460 | nso->nso_flags |= NSO_VERIFIED; | |
461 | } | |
462 | mbuf_freem(m); | |
463 | /* wake up search thread */ | |
464 | wakeup(nso->nso_wake); | |
465 | break; | |
2d21ac55 | 466 | } |
6d2010ae A |
467 | } |
468 | ||
469 | nso->nso_flags &= ~NSO_UPCALL; | |
470 | if ((error != EWOULDBLOCK) && (error || !recv)) { | |
471 | /* problems with the socket... */ | |
472 | nso->nso_error = error ? error : EPIPE; | |
473 | nso->nso_flags |= NSO_DEAD; | |
474 | wakeup(nso->nso_wake); | |
475 | } | |
0a7de745 | 476 | if (nso->nso_flags & NSO_DISCONNECTING) { |
6d2010ae | 477 | wakeup(&nso->nso_flags); |
0a7de745 | 478 | } |
6d2010ae A |
479 | lck_mtx_unlock(&nso->nso_lock); |
480 | } | |
481 | ||
482 | /* | |
483 | * Create/initialize an nfs_socket structure. | |
484 | */ | |
485 | int | |
486 | nfs_socket_create( | |
3e170ce0 | 487 | struct nfsmount *nmp, |
6d2010ae A |
488 | struct sockaddr *sa, |
489 | int sotype, | |
490 | in_port_t port, | |
491 | uint32_t protocol, | |
492 | uint32_t vers, | |
493 | int resvport, | |
494 | struct nfs_socket **nsop) | |
495 | { | |
496 | struct nfs_socket *nso; | |
497 | struct timeval now; | |
498 | int error; | |
499 | #ifdef NFS_SOCKET_DEBUGGING | |
500 | char naddr[MAX_IPv6_STR_LEN]; | |
501 | void *sinaddr; | |
502 | ||
0a7de745 | 503 | if (sa->sa_family == AF_INET) { |
6d2010ae | 504 | sinaddr = &((struct sockaddr_in*)sa)->sin_addr; |
0a7de745 | 505 | } else { |
6d2010ae | 506 | sinaddr = &((struct sockaddr_in6*)sa)->sin6_addr; |
0a7de745 A |
507 | } |
508 | if (inet_ntop(sa->sa_family, sinaddr, naddr, sizeof(naddr)) != naddr) { | |
6d2010ae | 509 | strlcpy(naddr, "<unknown>", sizeof(naddr)); |
0a7de745 | 510 | } |
39236c6e A |
511 | #else |
512 | char naddr[1] = { 0 }; | |
6d2010ae A |
513 | #endif |
514 | ||
515 | *nsop = NULL; | |
516 | ||
517 | /* Create the socket. */ | |
0a7de745 A |
518 | MALLOC(nso, struct nfs_socket *, sizeof(struct nfs_socket), M_TEMP, M_WAITOK | M_ZERO); |
519 | if (nso) { | |
520 | MALLOC(nso->nso_saddr, struct sockaddr *, sa->sa_len, M_SONAME, M_WAITOK | M_ZERO); | |
521 | } | |
6d2010ae | 522 | if (!nso || !nso->nso_saddr) { |
0a7de745 | 523 | if (nso) { |
6d2010ae | 524 | FREE(nso, M_TEMP); |
0a7de745 A |
525 | } |
526 | return ENOMEM; | |
6d2010ae A |
527 | } |
528 | lck_mtx_init(&nso->nso_lock, nfs_request_grp, LCK_ATTR_NULL); | |
529 | nso->nso_sotype = sotype; | |
0a7de745 | 530 | if (nso->nso_sotype == SOCK_STREAM) { |
6d2010ae | 531 | nfs_rpc_record_state_init(&nso->nso_rrs); |
0a7de745 | 532 | } |
6d2010ae A |
533 | microuptime(&now); |
534 | nso->nso_timestamp = now.tv_sec; | |
535 | bcopy(sa, nso->nso_saddr, sa->sa_len); | |
0a7de745 | 536 | if (sa->sa_family == AF_INET) { |
6d2010ae | 537 | ((struct sockaddr_in*)nso->nso_saddr)->sin_port = htons(port); |
0a7de745 | 538 | } else if (sa->sa_family == AF_INET6) { |
6d2010ae | 539 | ((struct sockaddr_in6*)nso->nso_saddr)->sin6_port = htons(port); |
0a7de745 | 540 | } |
6d2010ae A |
541 | nso->nso_protocol = protocol; |
542 | nso->nso_version = vers; | |
3e170ce0 A |
543 | nso->nso_nfs_min_vers = PVER2MAJOR(nmp->nm_min_vers); |
544 | nso->nso_nfs_max_vers = PVER2MAJOR(nmp->nm_max_vers); | |
6d2010ae A |
545 | |
546 | error = sock_socket(sa->sa_family, nso->nso_sotype, 0, NULL, NULL, &nso->nso_so); | |
547 | ||
548 | /* Some servers require that the client port be a reserved port number. */ | |
549 | if (!error && resvport && ((sa->sa_family == AF_INET) || (sa->sa_family == AF_INET6))) { | |
550 | struct sockaddr_storage ss; | |
551 | int level = (sa->sa_family == AF_INET) ? IPPROTO_IP : IPPROTO_IPV6; | |
552 | int optname = (sa->sa_family == AF_INET) ? IP_PORTRANGE : IPV6_PORTRANGE; | |
553 | int portrange = IP_PORTRANGE_LOW; | |
554 | ||
555 | error = sock_setsockopt(nso->nso_so, level, optname, &portrange, sizeof(portrange)); | |
0a7de745 | 556 | if (!error) { /* bind now to check for failure */ |
6d2010ae A |
557 | ss.ss_len = sa->sa_len; |
558 | ss.ss_family = sa->sa_family; | |
559 | if (ss.ss_family == AF_INET) { | |
560 | ((struct sockaddr_in*)&ss)->sin_addr.s_addr = INADDR_ANY; | |
561 | ((struct sockaddr_in*)&ss)->sin_port = htons(0); | |
562 | } else if (ss.ss_family == AF_INET6) { | |
563 | ((struct sockaddr_in6*)&ss)->sin6_addr = in6addr_any; | |
564 | ((struct sockaddr_in6*)&ss)->sin6_port = htons(0); | |
565 | } else { | |
566 | error = EINVAL; | |
567 | } | |
0a7de745 | 568 | if (!error) { |
6d2010ae | 569 | error = sock_bind(nso->nso_so, (struct sockaddr*)&ss); |
0a7de745 | 570 | } |
1c79356b | 571 | } |
1c79356b | 572 | } |
2d21ac55 | 573 | |
6d2010ae | 574 | if (error) { |
39236c6e | 575 | NFS_SOCK_DBG("nfs connect %s error %d creating socket %p %s type %d%s port %d prot %d %d\n", |
0a7de745 A |
576 | vfs_statfs(nmp->nm_mountp)->f_mntfromname, error, nso, naddr, sotype, |
577 | resvport ? "r" : "", port, protocol, vers); | |
6d2010ae A |
578 | nfs_socket_destroy(nso); |
579 | } else { | |
39236c6e | 580 | NFS_SOCK_DBG("nfs connect %s created socket %p %s type %d%s port %d prot %d %d\n", |
0a7de745 A |
581 | vfs_statfs(nmp->nm_mountp)->f_mntfromname, nso, naddr, |
582 | sotype, resvport ? "r" : "", port, protocol, vers); | |
6d2010ae A |
583 | *nsop = nso; |
584 | } | |
0a7de745 | 585 | return error; |
6d2010ae A |
586 | } |
587 | ||
588 | /* | |
589 | * Destroy an nfs_socket structure. | |
590 | */ | |
591 | void | |
592 | nfs_socket_destroy(struct nfs_socket *nso) | |
593 | { | |
594 | struct timespec ts = { 4, 0 }; | |
595 | ||
596 | lck_mtx_lock(&nso->nso_lock); | |
597 | nso->nso_flags |= NSO_DISCONNECTING; | |
0a7de745 A |
598 | if (nso->nso_flags & NSO_UPCALL) { /* give upcall a chance to complete */ |
599 | msleep(&nso->nso_flags, &nso->nso_lock, PZERO - 1, "nfswaitupcall", &ts); | |
600 | } | |
6d2010ae A |
601 | lck_mtx_unlock(&nso->nso_lock); |
602 | sock_shutdown(nso->nso_so, SHUT_RDWR); | |
603 | sock_close(nso->nso_so); | |
0a7de745 | 604 | if (nso->nso_sotype == SOCK_STREAM) { |
6d2010ae | 605 | nfs_rpc_record_state_cleanup(&nso->nso_rrs); |
0a7de745 | 606 | } |
6d2010ae | 607 | lck_mtx_destroy(&nso->nso_lock, nfs_request_grp); |
0a7de745 | 608 | if (nso->nso_saddr) { |
6d2010ae | 609 | FREE(nso->nso_saddr, M_SONAME); |
0a7de745 A |
610 | } |
611 | if (nso->nso_saddr2) { | |
6d2010ae | 612 | FREE(nso->nso_saddr2, M_SONAME); |
0a7de745 | 613 | } |
39236c6e | 614 | NFS_SOCK_DBG("nfs connect - socket %p destroyed\n", nso); |
6d2010ae A |
615 | FREE(nso, M_TEMP); |
616 | } | |
617 | ||
618 | /* | |
619 | * Set common socket options on an nfs_socket. | |
620 | */ | |
621 | void | |
622 | nfs_socket_options(struct nfsmount *nmp, struct nfs_socket *nso) | |
623 | { | |
55e303ae | 624 | /* |
2d21ac55 | 625 | * Set socket send/receive timeouts |
6d2010ae | 626 | * - Receive timeout shouldn't matter because most receives are performed |
2d21ac55 A |
627 | * in the socket upcall non-blocking. |
628 | * - Send timeout should allow us to react to a blocked socket. | |
629 | * Soft mounts will want to abort sooner. | |
55e303ae | 630 | */ |
6d2010ae A |
631 | struct timeval timeo; |
632 | int on = 1, proto; | |
1c79356b | 633 | |
6d2010ae | 634 | timeo.tv_usec = 0; |
316670eb | 635 | timeo.tv_sec = (NMFLAG(nmp, SOFT) || nfs_can_squish(nmp)) ? 5 : 60; |
6d2010ae A |
636 | sock_setsockopt(nso->nso_so, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo)); |
637 | sock_setsockopt(nso->nso_so, SOL_SOCKET, SO_SNDTIMEO, &timeo, sizeof(timeo)); | |
638 | if (nso->nso_sotype == SOCK_STREAM) { | |
2d21ac55 | 639 | /* Assume that SOCK_STREAM always requires a connection */ |
6d2010ae | 640 | sock_setsockopt(nso->nso_so, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on)); |
2d21ac55 | 641 | /* set nodelay for TCP */ |
6d2010ae | 642 | sock_gettype(nso->nso_so, NULL, NULL, &proto); |
0a7de745 | 643 | if (proto == IPPROTO_TCP) { |
6d2010ae | 644 | sock_setsockopt(nso->nso_so, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)); |
0a7de745 | 645 | } |
1c79356b | 646 | } |
6d2010ae | 647 | if (nso->nso_sotype == SOCK_DGRAM) { /* set socket buffer sizes for UDP */ |
2d21ac55 | 648 | int reserve = NFS_UDPSOCKBUF; |
6d2010ae A |
649 | sock_setsockopt(nso->nso_so, SOL_SOCKET, SO_SNDBUF, &reserve, sizeof(reserve)); |
650 | sock_setsockopt(nso->nso_so, SOL_SOCKET, SO_RCVBUF, &reserve, sizeof(reserve)); | |
651 | } | |
652 | /* set SO_NOADDRERR to detect network changes ASAP */ | |
653 | sock_setsockopt(nso->nso_so, SOL_SOCKET, SO_NOADDRERR, &on, sizeof(on)); | |
654 | /* just playin' it safe with upcalls */ | |
655 | sock_setsockopt(nso->nso_so, SOL_SOCKET, SO_UPCALLCLOSEWAIT, &on, sizeof(on)); | |
656 | /* socket should be interruptible if the mount is */ | |
0a7de745 | 657 | if (!NMFLAG(nmp, INTR)) { |
6d2010ae | 658 | sock_nointerrupt(nso->nso_so, 1); |
0a7de745 | 659 | } |
6d2010ae A |
660 | } |
661 | ||
662 | /* | |
663 | * Release resources held in an nfs_socket_search. | |
664 | */ | |
665 | void | |
666 | nfs_socket_search_cleanup(struct nfs_socket_search *nss) | |
667 | { | |
668 | struct nfs_socket *nso, *nsonext; | |
669 | ||
670 | TAILQ_FOREACH_SAFE(nso, &nss->nss_socklist, nso_link, nsonext) { | |
671 | TAILQ_REMOVE(&nss->nss_socklist, nso, nso_link); | |
672 | nss->nss_sockcnt--; | |
673 | nfs_socket_destroy(nso); | |
674 | } | |
675 | if (nss->nss_sock) { | |
676 | nfs_socket_destroy(nss->nss_sock); | |
677 | nss->nss_sock = NULL; | |
678 | } | |
679 | } | |
680 | ||
681 | /* | |
682 | * Prefer returning certain errors over others. | |
683 | * This function returns a ranking of the given error. | |
684 | */ | |
685 | int | |
686 | nfs_connect_error_class(int error) | |
687 | { | |
688 | switch (error) { | |
689 | case 0: | |
0a7de745 | 690 | return 0; |
6d2010ae A |
691 | case ETIMEDOUT: |
692 | case EAGAIN: | |
0a7de745 | 693 | return 1; |
6d2010ae A |
694 | case EPIPE: |
695 | case EADDRNOTAVAIL: | |
696 | case ENETDOWN: | |
697 | case ENETUNREACH: | |
698 | case ENETRESET: | |
699 | case ECONNABORTED: | |
700 | case ECONNRESET: | |
701 | case EISCONN: | |
702 | case ENOTCONN: | |
703 | case ESHUTDOWN: | |
704 | case ECONNREFUSED: | |
705 | case EHOSTDOWN: | |
706 | case EHOSTUNREACH: | |
0a7de745 | 707 | return 2; |
6d2010ae A |
708 | case ERPCMISMATCH: |
709 | case EPROCUNAVAIL: | |
710 | case EPROGMISMATCH: | |
711 | case EPROGUNAVAIL: | |
0a7de745 | 712 | return 3; |
6d2010ae | 713 | case EBADRPC: |
0a7de745 | 714 | return 4; |
6d2010ae | 715 | default: |
0a7de745 | 716 | return 5; |
6d2010ae A |
717 | } |
718 | } | |
719 | ||
720 | /* | |
721 | * Make sure a socket search returns the best error. | |
722 | */ | |
723 | void | |
724 | nfs_socket_search_update_error(struct nfs_socket_search *nss, int error) | |
725 | { | |
0a7de745 | 726 | if (nfs_connect_error_class(error) >= nfs_connect_error_class(nss->nss_error)) { |
6d2010ae | 727 | nss->nss_error = error; |
0a7de745 | 728 | } |
6d2010ae A |
729 | } |
730 | ||
39236c6e | 731 | /* nfs_connect_search_new_socket: |
0a7de745 | 732 | * Given a socket search structure for an nfs mount try to find a new socket from the set of addresses specified |
39236c6e A |
733 | * by nss. |
734 | * | |
735 | * nss_last is set to -1 at initialization to indicate the first time. Its set to -2 if address was found but | |
736 | * could not be used or if a socket timed out. | |
6d2010ae A |
737 | */ |
738 | int | |
39236c6e | 739 | nfs_connect_search_new_socket(struct nfsmount *nmp, struct nfs_socket_search *nss, struct timeval *now) |
6d2010ae | 740 | { |
6d2010ae A |
741 | struct nfs_fs_location *fsl; |
742 | struct nfs_fs_server *fss; | |
743 | struct sockaddr_storage ss; | |
39236c6e | 744 | struct nfs_socket *nso; |
6d2010ae | 745 | char *addrstr; |
39236c6e | 746 | int error = 0; |
0a7de745 | 747 | |
6d2010ae | 748 | |
39236c6e | 749 | NFS_SOCK_DBG("nfs connect %s nss_addrcnt = %d\n", |
0a7de745 | 750 | vfs_statfs(nmp->nm_mountp)->f_mntfromname, nss->nss_addrcnt); |
6d2010ae | 751 | |
39236c6e A |
752 | /* |
753 | * while there are addresses and: | |
754 | * we have no sockets or | |
755 | * the last address failed and did not produce a socket (nss_last < 0) or | |
756 | * Its been a while (2 seconds) and we have less than the max number of concurrent sockets to search (4) | |
757 | * then attempt to create a socket with the current address. | |
758 | */ | |
759 | while (nss->nss_addrcnt > 0 && ((nss->nss_last < 0) || (nss->nss_sockcnt == 0) || | |
0a7de745 A |
760 | ((nss->nss_sockcnt < 4) && (now->tv_sec >= (nss->nss_last + 2))))) { |
761 | if (nmp->nm_sockflags & NMSOCK_UNMOUNT) { | |
762 | return EINTR; | |
763 | } | |
6d2010ae A |
764 | /* Can we convert the address to a sockaddr? */ |
765 | fsl = nmp->nm_locations.nl_locations[nss->nss_nextloc.nli_loc]; | |
766 | fss = fsl->nl_servers[nss->nss_nextloc.nli_serv]; | |
767 | addrstr = fss->ns_addresses[nss->nss_nextloc.nli_addr]; | |
768 | if (!nfs_uaddr2sockaddr(addrstr, (struct sockaddr*)&ss)) { | |
769 | nfs_location_next(&nmp->nm_locations, &nss->nss_nextloc); | |
39236c6e | 770 | nss->nss_addrcnt -= 1; |
6d2010ae A |
771 | nss->nss_last = -2; |
772 | continue; | |
773 | } | |
774 | /* Check that socket family is acceptable. */ | |
775 | if (nmp->nm_sofamily && (ss.ss_family != nmp->nm_sofamily)) { | |
776 | nfs_location_next(&nmp->nm_locations, &nss->nss_nextloc); | |
39236c6e | 777 | nss->nss_addrcnt -= 1; |
6d2010ae A |
778 | nss->nss_last = -2; |
779 | continue; | |
780 | } | |
781 | ||
782 | /* Create the socket. */ | |
783 | error = nfs_socket_create(nmp, (struct sockaddr*)&ss, nss->nss_sotype, | |
0a7de745 A |
784 | nss->nss_port, nss->nss_protocol, nss->nss_version, |
785 | ((nss->nss_protocol == NFS_PROG) && NMFLAG(nmp, RESVPORT)), &nso); | |
786 | if (error) { | |
787 | return error; | |
788 | } | |
6d2010ae A |
789 | |
790 | nso->nso_location = nss->nss_nextloc; | |
791 | nso->nso_wake = nss; | |
792 | error = sock_setupcall(nso->nso_so, nfs_connect_upcall, nso); | |
2d21ac55 | 793 | if (error) { |
6d2010ae A |
794 | lck_mtx_lock(&nso->nso_lock); |
795 | nso->nso_error = error; | |
796 | nso->nso_flags |= NSO_DEAD; | |
797 | lck_mtx_unlock(&nso->nso_lock); | |
798 | } | |
799 | ||
800 | TAILQ_INSERT_TAIL(&nss->nss_socklist, nso, nso_link); | |
801 | nss->nss_sockcnt++; | |
802 | nfs_location_next(&nmp->nm_locations, &nss->nss_nextloc); | |
39236c6e | 803 | nss->nss_addrcnt -= 1; |
0a7de745 | 804 | |
39236c6e | 805 | nss->nss_last = now->tv_sec; |
6d2010ae A |
806 | } |
807 | ||
0a7de745 | 808 | if (nss->nss_addrcnt == 0 && nss->nss_last < 0) { |
39236c6e | 809 | nss->nss_last = now->tv_sec; |
0a7de745 A |
810 | } |
811 | ||
812 | return error; | |
39236c6e A |
813 | } |
814 | ||
815 | /* | |
816 | * nfs_connect_search_socket_connect: Connect an nfs socket nso for nfsmount nmp. | |
817 | * If successful set the socket options for the socket as require from the mount. | |
818 | * | |
819 | * Assumes: nso->nso_lock is held on entry and return. | |
820 | */ | |
821 | int | |
822 | nfs_connect_search_socket_connect(struct nfsmount *nmp, struct nfs_socket *nso, int verbose) | |
823 | { | |
824 | int error; | |
0a7de745 | 825 | |
39236c6e A |
826 | if ((nso->nso_sotype != SOCK_STREAM) && NMFLAG(nmp, NOCONNECT)) { |
827 | /* no connection needed, just say it's already connected */ | |
828 | NFS_SOCK_DBG("nfs connect %s UDP socket %p noconnect\n", | |
0a7de745 | 829 | vfs_statfs(nmp->nm_mountp)->f_mntfromname, nso); |
39236c6e A |
830 | nso->nso_flags |= NSO_CONNECTED; |
831 | nfs_socket_options(nmp, nso); | |
0a7de745 | 832 | return 1; /* Socket is connected and setup */ |
39236c6e A |
833 | } else if (!(nso->nso_flags & NSO_CONNECTING)) { |
834 | /* initiate the connection */ | |
835 | nso->nso_flags |= NSO_CONNECTING; | |
836 | lck_mtx_unlock(&nso->nso_lock); | |
837 | NFS_SOCK_DBG("nfs connect %s connecting socket %p\n", | |
0a7de745 | 838 | vfs_statfs(nmp->nm_mountp)->f_mntfromname, nso); |
39236c6e | 839 | error = sock_connect(nso->nso_so, nso->nso_saddr, MSG_DONTWAIT); |
6d2010ae | 840 | lck_mtx_lock(&nso->nso_lock); |
39236c6e A |
841 | if (error && (error != EINPROGRESS)) { |
842 | nso->nso_error = error; | |
843 | nso->nso_flags |= NSO_DEAD; | |
0a7de745 | 844 | return 0; |
6d2010ae | 845 | } |
39236c6e A |
846 | } |
847 | if (nso->nso_flags & NSO_CONNECTING) { | |
848 | /* check the connection */ | |
849 | if (sock_isconnected(nso->nso_so)) { | |
850 | NFS_SOCK_DBG("nfs connect %s socket %p is connected\n", | |
0a7de745 | 851 | vfs_statfs(nmp->nm_mountp)->f_mntfromname, nso); |
39236c6e A |
852 | nso->nso_flags &= ~NSO_CONNECTING; |
853 | nso->nso_flags |= NSO_CONNECTED; | |
854 | nfs_socket_options(nmp, nso); | |
0a7de745 | 855 | return 1; /* Socket is connected and setup */ |
39236c6e A |
856 | } else { |
857 | int optlen = sizeof(error); | |
858 | error = 0; | |
859 | sock_getsockopt(nso->nso_so, SOL_SOCKET, SO_ERROR, &error, &optlen); | |
860 | if (error) { /* we got an error on the socket */ | |
861 | NFS_SOCK_DBG("nfs connect %s socket %p connection error %d\n", | |
0a7de745 A |
862 | vfs_statfs(nmp->nm_mountp)->f_mntfromname, nso, error); |
863 | if (verbose) { | |
39236c6e A |
864 | printf("nfs connect socket error %d for %s\n", |
865 | error, vfs_statfs(nmp->nm_mountp)->f_mntfromname); | |
0a7de745 | 866 | } |
6d2010ae A |
867 | nso->nso_error = error; |
868 | nso->nso_flags |= NSO_DEAD; | |
0a7de745 | 869 | return 0; |
6d2010ae A |
870 | } |
871 | } | |
39236c6e | 872 | } |
0a7de745 A |
873 | |
874 | return 0; /* Waiting to be connected */ | |
39236c6e A |
875 | } |
876 | ||
877 | /* | |
878 | * nfs_connect_search_ping: Send a null proc on the nso socket. | |
879 | */ | |
880 | int | |
881 | nfs_connect_search_ping(struct nfsmount *nmp, struct nfs_socket *nso, struct timeval *now) | |
882 | { | |
883 | /* initiate a NULL RPC request */ | |
884 | uint64_t xid = nso->nso_pingxid; | |
885 | mbuf_t m, mreq = NULL; | |
886 | struct msghdr msg; | |
887 | size_t reqlen, sentlen; | |
888 | uint32_t vers = nso->nso_version; | |
889 | int error; | |
890 | ||
891 | if (!vers) { | |
0a7de745 | 892 | if (nso->nso_protocol == PMAPPROG) { |
39236c6e | 893 | vers = (nso->nso_saddr->sa_family == AF_INET) ? PMAPVERS : RPCBVERS4; |
0a7de745 | 894 | } else if (nso->nso_protocol == NFS_PROG) { |
3e170ce0 | 895 | vers = PVER2MAJOR(nmp->nm_max_vers); |
0a7de745 | 896 | } |
39236c6e A |
897 | } |
898 | lck_mtx_unlock(&nso->nso_lock); | |
899 | error = nfsm_rpchead2(nmp, nso->nso_sotype, nso->nso_protocol, vers, 0, RPCAUTH_SYS, | |
0a7de745 | 900 | vfs_context_ucred(vfs_context_kernel()), NULL, NULL, &xid, &mreq); |
39236c6e A |
901 | lck_mtx_lock(&nso->nso_lock); |
902 | if (!error) { | |
903 | nso->nso_flags |= NSO_PINGING; | |
904 | nso->nso_pingxid = R_XID32(xid); | |
905 | nso->nso_reqtimestamp = now->tv_sec; | |
906 | bzero(&msg, sizeof(msg)); | |
907 | if ((nso->nso_sotype != SOCK_STREAM) && !sock_isconnected(nso->nso_so)) { | |
908 | msg.msg_name = nso->nso_saddr; | |
909 | msg.msg_namelen = nso->nso_saddr->sa_len; | |
6d2010ae | 910 | } |
0a7de745 | 911 | for (reqlen = 0, m = mreq; m; m = mbuf_next(m)) { |
39236c6e | 912 | reqlen += mbuf_len(m); |
0a7de745 | 913 | } |
6d2010ae | 914 | lck_mtx_unlock(&nso->nso_lock); |
39236c6e A |
915 | error = sock_sendmbuf(nso->nso_so, &msg, mreq, 0, &sentlen); |
916 | NFS_SOCK_DBG("nfs connect %s verifying socket %p send rv %d\n", | |
0a7de745 | 917 | vfs_statfs(nmp->nm_mountp)->f_mntfromname, nso, error); |
39236c6e | 918 | lck_mtx_lock(&nso->nso_lock); |
0a7de745 | 919 | if (!error && (sentlen != reqlen)) { |
39236c6e | 920 | error = ETIMEDOUT; |
0a7de745 | 921 | } |
39236c6e A |
922 | } |
923 | if (error) { | |
924 | nso->nso_error = error; | |
925 | nso->nso_flags |= NSO_DEAD; | |
0a7de745 | 926 | return 0; |
6d2010ae A |
927 | } |
928 | ||
0a7de745 | 929 | return 1; |
39236c6e A |
930 | } |
931 | ||
932 | /* | |
933 | * nfs_connect_search_socket_found: Take the found socket of the socket search list and assign it to the searched socket. | |
0a7de745 | 934 | * Set the nfs socket protocol and version if needed. |
39236c6e A |
935 | */ |
936 | void | |
3e170ce0 | 937 | nfs_connect_search_socket_found(struct nfsmount *nmp, struct nfs_socket_search *nss, struct nfs_socket *nso) |
39236c6e A |
938 | { |
939 | NFS_SOCK_DBG("nfs connect %s socket %p verified\n", | |
0a7de745 | 940 | vfs_statfs(nmp->nm_mountp)->f_mntfromname, nso); |
39236c6e A |
941 | if (!nso->nso_version) { |
942 | /* If the version isn't set, the default must have worked. */ | |
0a7de745 | 943 | if (nso->nso_protocol == PMAPPROG) { |
39236c6e | 944 | nso->nso_version = (nso->nso_saddr->sa_family == AF_INET) ? PMAPVERS : RPCBVERS4; |
0a7de745 A |
945 | } |
946 | if (nso->nso_protocol == NFS_PROG) { | |
3e170ce0 | 947 | nso->nso_version = PVER2MAJOR(nmp->nm_max_vers); |
0a7de745 | 948 | } |
39236c6e A |
949 | } |
950 | TAILQ_REMOVE(&nss->nss_socklist, nso, nso_link); | |
951 | nss->nss_sockcnt--; | |
952 | nss->nss_sock = nso; | |
953 | } | |
954 | ||
955 | /* | |
956 | * nfs_connect_search_socket_reap: For each socket in the search list mark any timed out socket as dead and remove from | |
957 | * the list. Dead socket are then destroyed. | |
958 | */ | |
959 | void | |
960 | nfs_connect_search_socket_reap(struct nfsmount *nmp __unused, struct nfs_socket_search *nss, struct timeval *now) | |
961 | { | |
962 | struct nfs_socket *nso, *nsonext; | |
0a7de745 | 963 | |
6d2010ae A |
964 | TAILQ_FOREACH_SAFE(nso, &nss->nss_socklist, nso_link, nsonext) { |
965 | lck_mtx_lock(&nso->nso_lock); | |
39236c6e | 966 | if (now->tv_sec >= (nso->nso_timestamp + nss->nss_timeo)) { |
6d2010ae | 967 | /* took too long */ |
39236c6e | 968 | NFS_SOCK_DBG("nfs connect %s socket %p timed out\n", |
0a7de745 | 969 | vfs_statfs(nmp->nm_mountp)->f_mntfromname, nso); |
6d2010ae A |
970 | nso->nso_error = ETIMEDOUT; |
971 | nso->nso_flags |= NSO_DEAD; | |
972 | } | |
973 | if (!(nso->nso_flags & NSO_DEAD)) { | |
974 | lck_mtx_unlock(&nso->nso_lock); | |
975 | continue; | |
976 | } | |
977 | lck_mtx_unlock(&nso->nso_lock); | |
39236c6e | 978 | NFS_SOCK_DBG("nfs connect %s reaping socket %p %d\n", |
0a7de745 | 979 | vfs_statfs(nmp->nm_mountp)->f_mntfromname, nso, nso->nso_error); |
6d2010ae A |
980 | nfs_socket_search_update_error(nss, nso->nso_error); |
981 | TAILQ_REMOVE(&nss->nss_socklist, nso, nso_link); | |
982 | nss->nss_sockcnt--; | |
983 | nfs_socket_destroy(nso); | |
39236c6e | 984 | /* If there are more sockets to try, force the starting of another socket */ |
0a7de745 | 985 | if (nss->nss_addrcnt > 0) { |
6d2010ae | 986 | nss->nss_last = -2; |
0a7de745 | 987 | } |
6d2010ae | 988 | } |
39236c6e A |
989 | } |
990 | ||
991 | /* | |
992 | * nfs_connect_search_check: Check on the status of search and wait for replies if needed. | |
993 | */ | |
994 | int | |
995 | nfs_connect_search_check(struct nfsmount *nmp, struct nfs_socket_search *nss, struct timeval *now) | |
996 | { | |
997 | int error; | |
998 | ||
999 | /* log a warning if connect is taking a while */ | |
0a7de745 | 1000 | if (((now->tv_sec - nss->nss_timestamp) >= 8) && ((nss->nss_flags & (NSS_VERBOSE | NSS_WARNED)) == NSS_VERBOSE)) { |
39236c6e A |
1001 | printf("nfs_connect: socket connect taking a while for %s\n", vfs_statfs(nmp->nm_mountp)->f_mntfromname); |
1002 | nss->nss_flags |= NSS_WARNED; | |
1003 | } | |
0a7de745 A |
1004 | if (nmp->nm_sockflags & NMSOCK_UNMOUNT) { |
1005 | return EINTR; | |
1006 | } | |
1007 | if ((error = nfs_sigintr(nmp, NULL, current_thread(), 0))) { | |
1008 | return error; | |
1009 | } | |
39236c6e A |
1010 | |
1011 | /* If we were succesfull at sending a ping, wait up to a second for a reply */ | |
0a7de745 | 1012 | if (nss->nss_last >= 0) { |
39236c6e | 1013 | tsleep(nss, PSOCK, "nfs_connect_search_wait", hz); |
0a7de745 A |
1014 | } |
1015 | ||
1016 | return 0; | |
39236c6e A |
1017 | } |
1018 | ||
6d2010ae | 1019 | |
39236c6e A |
1020 | /* |
1021 | * Continue the socket search until we have something to report. | |
1022 | */ | |
1023 | int | |
1024 | nfs_connect_search_loop(struct nfsmount *nmp, struct nfs_socket_search *nss) | |
1025 | { | |
1026 | struct nfs_socket *nso; | |
1027 | struct timeval now; | |
1028 | int error; | |
1029 | int verbose = (nss->nss_flags & NSS_VERBOSE); | |
0a7de745 | 1030 | |
39236c6e A |
1031 | loop: |
1032 | microuptime(&now); | |
1033 | NFS_SOCK_DBG("nfs connect %s search %ld\n", vfs_statfs(nmp->nm_mountp)->f_mntfromname, now.tv_sec); | |
1034 | ||
1035 | /* add a new socket to the socket list if needed and available */ | |
1036 | error = nfs_connect_search_new_socket(nmp, nss, &now); | |
1037 | if (error) { | |
1038 | NFS_SOCK_DBG("nfs connect returned %d\n", error); | |
0a7de745 | 1039 | return error; |
39236c6e | 1040 | } |
0a7de745 | 1041 | |
39236c6e A |
1042 | /* check each active socket on the list and try to push it along */ |
1043 | TAILQ_FOREACH(nso, &nss->nss_socklist, nso_link) { | |
1044 | lck_mtx_lock(&nso->nso_lock); | |
1045 | ||
1046 | /* If not connected connect it */ | |
1047 | if (!(nso->nso_flags & NSO_CONNECTED)) { | |
1048 | if (!nfs_connect_search_socket_connect(nmp, nso, verbose)) { | |
1049 | lck_mtx_unlock(&nso->nso_lock); | |
1050 | continue; | |
1051 | } | |
1052 | } | |
1053 | ||
1054 | /* If the socket hasn't been verified or in a ping, ping it. We also handle UDP retransmits */ | |
0a7de745 A |
1055 | if (!(nso->nso_flags & (NSO_PINGING | NSO_VERIFIED)) || |
1056 | ((nso->nso_sotype == SOCK_DGRAM) && (now.tv_sec >= nso->nso_reqtimestamp + 2))) { | |
39236c6e A |
1057 | if (!nfs_connect_search_ping(nmp, nso, &now)) { |
1058 | lck_mtx_unlock(&nso->nso_lock); | |
1059 | continue; | |
1060 | } | |
1061 | } | |
1062 | ||
1063 | /* Has the socket been verified by the up call routine? */ | |
1064 | if (nso->nso_flags & NSO_VERIFIED) { | |
1065 | /* WOOHOO!! This socket looks good! */ | |
1066 | nfs_connect_search_socket_found(nmp, nss, nso); | |
1067 | lck_mtx_unlock(&nso->nso_lock); | |
1068 | break; | |
1069 | } | |
1070 | lck_mtx_unlock(&nso->nso_lock); | |
1071 | } | |
0a7de745 | 1072 | |
39236c6e A |
1073 | /* Check for timed out sockets and mark as dead and then remove all dead sockets. */ |
1074 | nfs_connect_search_socket_reap(nmp, nss, &now); | |
0a7de745 | 1075 | |
6d2010ae A |
1076 | /* |
1077 | * Keep looping if we haven't found a socket yet and we have more | |
1078 | * sockets to (continue to) try. | |
1079 | */ | |
1080 | error = 0; | |
39236c6e A |
1081 | if (!nss->nss_sock && (!TAILQ_EMPTY(&nss->nss_socklist) || nss->nss_addrcnt)) { |
1082 | error = nfs_connect_search_check(nmp, nss, &now); | |
0a7de745 | 1083 | if (!error) { |
39236c6e | 1084 | goto loop; |
0a7de745 | 1085 | } |
6d2010ae A |
1086 | } |
1087 | ||
39236c6e | 1088 | NFS_SOCK_DBG("nfs connect %s returning %d\n", vfs_statfs(nmp->nm_mountp)->f_mntfromname, error); |
0a7de745 | 1089 | return error; |
6d2010ae A |
1090 | } |
1091 | ||
1092 | /* | |
1093 | * Initialize a new NFS connection. | |
1094 | * | |
1095 | * Search for a location to connect a socket to and initialize the connection. | |
1096 | * | |
1097 | * An NFS mount may have multiple locations/servers/addresses available. | |
1098 | * We attempt to connect to each one asynchronously and will start | |
1099 | * several sockets in parallel if other locations are slow to answer. | |
1100 | * We'll use the first NFS socket we can successfully set up. | |
1101 | * | |
1102 | * The search may involve contacting the portmapper service first. | |
1103 | * | |
1104 | * A mount's initial connection may require negotiating some parameters such | |
1105 | * as socket type and NFS version. | |
1106 | */ | |
3e170ce0 | 1107 | |
6d2010ae A |
1108 | int |
1109 | nfs_connect(struct nfsmount *nmp, int verbose, int timeo) | |
1110 | { | |
1111 | struct nfs_socket_search nss; | |
1112 | struct nfs_socket *nso, *nsonfs; | |
1113 | struct sockaddr_storage ss; | |
1114 | struct sockaddr *saddr, *oldsaddr; | |
1115 | sock_upcall upcall; | |
1116 | struct timeval now, start; | |
1117 | int error, savederror, nfsvers; | |
3e170ce0 | 1118 | int tryv4 = 1; |
0a7de745 | 1119 | uint8_t sotype = nmp->nm_sotype ? nmp->nm_sotype : SOCK_STREAM; |
6d2010ae A |
1120 | fhandle_t *fh = NULL; |
1121 | char *path = NULL; | |
1122 | in_port_t port; | |
39236c6e | 1123 | int addrtotal = 0; |
0a7de745 | 1124 | |
6d2010ae A |
1125 | /* paranoia... check that we have at least one address in the locations */ |
1126 | uint32_t loc, serv; | |
0a7de745 A |
1127 | for (loc = 0; loc < nmp->nm_locations.nl_numlocs; loc++) { |
1128 | for (serv = 0; serv < nmp->nm_locations.nl_locations[loc]->nl_servcount; serv++) { | |
39236c6e | 1129 | addrtotal += nmp->nm_locations.nl_locations[loc]->nl_servers[serv]->ns_addrcount; |
0a7de745 | 1130 | if (nmp->nm_locations.nl_locations[loc]->nl_servers[serv]->ns_addrcount == 0) { |
39236c6e | 1131 | NFS_SOCK_DBG("nfs connect %s search, server %s has no addresses\n", |
0a7de745 A |
1132 | vfs_statfs(nmp->nm_mountp)->f_mntfromname, |
1133 | nmp->nm_locations.nl_locations[loc]->nl_servers[serv]->ns_name); | |
1134 | } | |
6d2010ae | 1135 | } |
6d2010ae | 1136 | } |
39236c6e A |
1137 | |
1138 | if (addrtotal == 0) { | |
1139 | NFS_SOCK_DBG("nfs connect %s search failed, no addresses\n", | |
0a7de745 A |
1140 | vfs_statfs(nmp->nm_mountp)->f_mntfromname); |
1141 | return EINVAL; | |
1142 | } else { | |
39236c6e | 1143 | NFS_SOCK_DBG("nfs connect %s has %d addresses\n", |
0a7de745 A |
1144 | vfs_statfs(nmp->nm_mountp)->f_mntfromname, addrtotal); |
1145 | } | |
6d2010ae A |
1146 | |
1147 | lck_mtx_lock(&nmp->nm_lock); | |
1148 | nmp->nm_sockflags |= NMSOCK_CONNECTING; | |
1149 | nmp->nm_nss = &nss; | |
1150 | lck_mtx_unlock(&nmp->nm_lock); | |
1151 | microuptime(&start); | |
1152 | savederror = error = 0; | |
1153 | ||
1154 | tryagain: | |
1155 | /* initialize socket search state */ | |
1156 | bzero(&nss, sizeof(nss)); | |
39236c6e | 1157 | nss.nss_addrcnt = addrtotal; |
6d2010ae A |
1158 | nss.nss_error = savederror; |
1159 | TAILQ_INIT(&nss.nss_socklist); | |
1160 | nss.nss_sotype = sotype; | |
1161 | nss.nss_startloc = nmp->nm_locations.nl_current; | |
1162 | nss.nss_timestamp = start.tv_sec; | |
1163 | nss.nss_timeo = timeo; | |
0a7de745 | 1164 | if (verbose) { |
6d2010ae | 1165 | nss.nss_flags |= NSS_VERBOSE; |
0a7de745 | 1166 | } |
6d2010ae A |
1167 | |
1168 | /* First time connecting, we may need to negotiate some things */ | |
1169 | if (!(nmp->nm_sockflags & NMSOCK_HASCONNECTED)) { | |
1170 | if (!nmp->nm_vers) { | |
1171 | /* No NFS version specified... */ | |
1172 | if (!nmp->nm_nfsport || (!NM_OMATTR_GIVEN(nmp, FH) && !nmp->nm_mountport)) { | |
3e170ce0 A |
1173 | if (PVER2MAJOR(nmp->nm_max_vers) >= NFS_VER4 && tryv4) { |
1174 | nss.nss_port = NFS_PORT; | |
1175 | nss.nss_protocol = NFS_PROG; | |
1176 | nss.nss_version = 4; | |
1177 | nss.nss_flags |= NSS_FALLBACK2PMAP; | |
1178 | } else { | |
1179 | /* ...connect to portmapper first if we (may) need any ports. */ | |
1180 | nss.nss_port = PMAPPORT; | |
1181 | nss.nss_protocol = PMAPPROG; | |
1182 | nss.nss_version = 0; | |
1183 | } | |
6d2010ae A |
1184 | } else { |
1185 | /* ...connect to NFS port first. */ | |
1186 | nss.nss_port = nmp->nm_nfsport; | |
1187 | nss.nss_protocol = NFS_PROG; | |
1188 | nss.nss_version = 0; | |
1189 | } | |
1190 | } else if (nmp->nm_vers >= NFS_VER4) { | |
3e170ce0 A |
1191 | if (tryv4) { |
1192 | /* For NFSv4, we use the given (or default) port. */ | |
1193 | nss.nss_port = nmp->nm_nfsport ? nmp->nm_nfsport : NFS_PORT; | |
1194 | nss.nss_protocol = NFS_PROG; | |
1195 | nss.nss_version = 4; | |
1196 | /* | |
1197 | * set NSS_FALLBACK2PMAP here to pick up any non standard port | |
1198 | * if no port is specified on the mount; | |
1199 | * Note nm_vers is set so we will only try NFS_VER4. | |
1200 | */ | |
0a7de745 | 1201 | if (!nmp->nm_nfsport) { |
3e170ce0 | 1202 | nss.nss_flags |= NSS_FALLBACK2PMAP; |
0a7de745 | 1203 | } |
3e170ce0 A |
1204 | } else { |
1205 | nss.nss_port = PMAPPORT; | |
1206 | nss.nss_protocol = PMAPPROG; | |
1207 | nss.nss_version = 0; | |
1208 | } | |
6d2010ae A |
1209 | } else { |
1210 | /* For NFSv3/v2... */ | |
1211 | if (!nmp->nm_nfsport || (!NM_OMATTR_GIVEN(nmp, FH) && !nmp->nm_mountport)) { | |
1212 | /* ...connect to portmapper first if we need any ports. */ | |
1213 | nss.nss_port = PMAPPORT; | |
1214 | nss.nss_protocol = PMAPPROG; | |
1215 | nss.nss_version = 0; | |
1216 | } else { | |
1217 | /* ...connect to NFS port first. */ | |
1218 | nss.nss_port = nmp->nm_nfsport; | |
1219 | nss.nss_protocol = NFS_PROG; | |
1220 | nss.nss_version = nmp->nm_vers; | |
1221 | } | |
1222 | } | |
39236c6e | 1223 | NFS_SOCK_DBG("nfs connect first %s, so type %d port %d prot %d %d\n", |
0a7de745 A |
1224 | vfs_statfs(nmp->nm_mountp)->f_mntfromname, nss.nss_sotype, nss.nss_port, |
1225 | nss.nss_protocol, nss.nss_version); | |
6d2010ae A |
1226 | } else { |
1227 | /* we've connected before, just connect to NFS port */ | |
1228 | if (!nmp->nm_nfsport) { | |
1229 | /* need to ask portmapper which port that would be */ | |
1230 | nss.nss_port = PMAPPORT; | |
1231 | nss.nss_protocol = PMAPPROG; | |
1232 | nss.nss_version = 0; | |
1233 | } else { | |
1234 | nss.nss_port = nmp->nm_nfsport; | |
1235 | nss.nss_protocol = NFS_PROG; | |
1236 | nss.nss_version = nmp->nm_vers; | |
1237 | } | |
39236c6e | 1238 | NFS_SOCK_DBG("nfs connect %s, so type %d port %d prot %d %d\n", |
0a7de745 A |
1239 | vfs_statfs(nmp->nm_mountp)->f_mntfromname, nss.nss_sotype, nss.nss_port, |
1240 | nss.nss_protocol, nss.nss_version); | |
6d2010ae A |
1241 | } |
1242 | ||
1243 | /* Set next location to first valid location. */ | |
1244 | /* If start location is invalid, find next location. */ | |
1245 | nss.nss_nextloc = nss.nss_startloc; | |
1246 | if ((nss.nss_nextloc.nli_serv >= nmp->nm_locations.nl_locations[nss.nss_nextloc.nli_loc]->nl_servcount) || | |
1247 | (nss.nss_nextloc.nli_addr >= nmp->nm_locations.nl_locations[nss.nss_nextloc.nli_loc]->nl_servers[nss.nss_nextloc.nli_serv]->ns_addrcount)) { | |
1248 | nfs_location_next(&nmp->nm_locations, &nss.nss_nextloc); | |
1249 | if (!nfs_location_index_cmp(&nss.nss_nextloc, &nss.nss_startloc)) { | |
39236c6e | 1250 | NFS_SOCK_DBG("nfs connect %s search failed, couldn't find a valid location index\n", |
0a7de745 A |
1251 | vfs_statfs(nmp->nm_mountp)->f_mntfromname); |
1252 | return ENOENT; | |
6d2010ae A |
1253 | } |
1254 | } | |
1255 | nss.nss_last = -1; | |
1256 | ||
1257 | keepsearching: | |
1258 | ||
1259 | error = nfs_connect_search_loop(nmp, &nss); | |
1260 | if (error || !nss.nss_sock) { | |
1261 | /* search failed */ | |
1262 | nfs_socket_search_cleanup(&nss); | |
3e170ce0 A |
1263 | if (nss.nss_flags & NSS_FALLBACK2PMAP) { |
1264 | tryv4 = 0; | |
1265 | NFS_SOCK_DBG("nfs connect %s TCP failed for V4 %d %d, trying PORTMAP\n", | |
0a7de745 | 1266 | vfs_statfs(nmp->nm_mountp)->f_mntfromname, error, nss.nss_error); |
3e170ce0 A |
1267 | goto tryagain; |
1268 | } | |
1269 | ||
6d2010ae A |
1270 | if (!error && (nss.nss_sotype == SOCK_STREAM) && !nmp->nm_sotype && (nmp->nm_vers < NFS_VER4)) { |
1271 | /* Try using UDP */ | |
1272 | sotype = SOCK_DGRAM; | |
1273 | savederror = nss.nss_error; | |
39236c6e | 1274 | NFS_SOCK_DBG("nfs connect %s TCP failed %d %d, trying UDP\n", |
0a7de745 | 1275 | vfs_statfs(nmp->nm_mountp)->f_mntfromname, error, nss.nss_error); |
6d2010ae A |
1276 | goto tryagain; |
1277 | } | |
0a7de745 | 1278 | if (!error) { |
6d2010ae | 1279 | error = nss.nss_error ? nss.nss_error : ETIMEDOUT; |
0a7de745 | 1280 | } |
6d2010ae A |
1281 | lck_mtx_lock(&nmp->nm_lock); |
1282 | nmp->nm_sockflags &= ~NMSOCK_CONNECTING; | |
1283 | nmp->nm_nss = NULL; | |
1284 | lck_mtx_unlock(&nmp->nm_lock); | |
0a7de745 | 1285 | if (nss.nss_flags & NSS_WARNED) { |
6d2010ae | 1286 | log(LOG_INFO, "nfs_connect: socket connect aborted for %s\n", |
0a7de745 A |
1287 | vfs_statfs(nmp->nm_mountp)->f_mntfromname); |
1288 | } | |
1289 | if (fh) { | |
6d2010ae | 1290 | FREE(fh, M_TEMP); |
0a7de745 A |
1291 | } |
1292 | if (path) { | |
6d2010ae | 1293 | FREE_ZONE(path, MAXPATHLEN, M_NAMEI); |
0a7de745 | 1294 | } |
39236c6e | 1295 | NFS_SOCK_DBG("nfs connect %s search failed, returning %d\n", |
0a7de745 A |
1296 | vfs_statfs(nmp->nm_mountp)->f_mntfromname, error); |
1297 | return error; | |
6d2010ae A |
1298 | } |
1299 | ||
1300 | /* try to use nss_sock */ | |
1301 | nso = nss.nss_sock; | |
1302 | nss.nss_sock = NULL; | |
1303 | ||
1304 | /* We may be speaking to portmap first... to determine port(s). */ | |
0a7de745 | 1305 | if (nso->nso_saddr->sa_family == AF_INET) { |
6d2010ae | 1306 | port = ntohs(((struct sockaddr_in*)nso->nso_saddr)->sin_port); |
0a7de745 | 1307 | } else { |
6d2010ae | 1308 | port = ntohs(((struct sockaddr_in6*)nso->nso_saddr)->sin6_port); |
0a7de745 | 1309 | } |
6d2010ae A |
1310 | if (port == PMAPPORT) { |
1311 | /* Use this portmapper port to get the port #s we need. */ | |
39236c6e | 1312 | NFS_SOCK_DBG("nfs connect %s got portmapper socket %p\n", |
0a7de745 | 1313 | vfs_statfs(nmp->nm_mountp)->f_mntfromname, nso); |
6d2010ae A |
1314 | |
1315 | /* remove the connect upcall so nfs_portmap_lookup() can use this socket */ | |
1316 | sock_setupcall(nso->nso_so, NULL, NULL); | |
1317 | ||
1318 | /* Set up socket address and port for NFS socket. */ | |
1319 | bcopy(nso->nso_saddr, &ss, nso->nso_saddr->sa_len); | |
1320 | ||
3e170ce0 A |
1321 | /* If NFS version not set, try nm_max_vers down to nm_min_vers */ |
1322 | nfsvers = nmp->nm_vers ? nmp->nm_vers : PVER2MAJOR(nmp->nm_max_vers); | |
6d2010ae | 1323 | if (!(port = nmp->nm_nfsport)) { |
0a7de745 | 1324 | if (ss.ss_family == AF_INET) { |
6d2010ae | 1325 | ((struct sockaddr_in*)&ss)->sin_port = htons(0); |
0a7de745 | 1326 | } else if (ss.ss_family == AF_INET6) { |
6d2010ae | 1327 | ((struct sockaddr_in6*)&ss)->sin6_port = htons(0); |
0a7de745 | 1328 | } |
3e170ce0 | 1329 | for (; nfsvers >= (int)PVER2MAJOR(nmp->nm_min_vers); nfsvers--) { |
0a7de745 | 1330 | if (nmp->nm_vers && nmp->nm_vers != nfsvers) { |
3e170ce0 | 1331 | continue; /* Wrong version */ |
0a7de745 A |
1332 | } |
1333 | if (nfsvers == NFS_VER4 && nso->nso_sotype == SOCK_DGRAM) { | |
3e170ce0 | 1334 | continue; /* NFSv4 does not do UDP */ |
0a7de745 | 1335 | } |
6d2010ae | 1336 | error = nfs_portmap_lookup(nmp, vfs_context_current(), (struct sockaddr*)&ss, |
0a7de745 A |
1337 | nso->nso_so, NFS_PROG, nfsvers, |
1338 | (nso->nso_sotype == SOCK_DGRAM) ? IPPROTO_UDP : IPPROTO_TCP, timeo); | |
6d2010ae | 1339 | if (!error) { |
0a7de745 | 1340 | if (ss.ss_family == AF_INET) { |
6d2010ae | 1341 | port = ntohs(((struct sockaddr_in*)&ss)->sin_port); |
0a7de745 | 1342 | } else if (ss.ss_family == AF_INET6) { |
6d2010ae | 1343 | port = ntohs(((struct sockaddr_in6*)&ss)->sin6_port); |
0a7de745 A |
1344 | } |
1345 | if (!port) { | |
6d2010ae | 1346 | error = EPROGUNAVAIL; |
0a7de745 A |
1347 | } |
1348 | if (port == NFS_PORT && nfsvers == NFS_VER4 && tryv4 == 0) { | |
3e170ce0 | 1349 | continue; /* We already tried this */ |
0a7de745 | 1350 | } |
6d2010ae | 1351 | } |
0a7de745 | 1352 | if (!error) { |
3e170ce0 | 1353 | break; |
0a7de745 | 1354 | } |
6d2010ae | 1355 | } |
0a7de745 | 1356 | if (nfsvers < (int)PVER2MAJOR(nmp->nm_min_vers) && error == 0) { |
3e170ce0 | 1357 | error = EPROGUNAVAIL; |
0a7de745 | 1358 | } |
6d2010ae A |
1359 | if (error) { |
1360 | nfs_socket_search_update_error(&nss, error); | |
1361 | nfs_socket_destroy(nso); | |
1362 | goto keepsearching; | |
1363 | } | |
1364 | } | |
1365 | /* Create NFS protocol socket and add it to the list of sockets. */ | |
3e170ce0 | 1366 | /* N.B. If nfsvers is NFS_VER4 at this point then we're on a non standard port */ |
6d2010ae | 1367 | error = nfs_socket_create(nmp, (struct sockaddr*)&ss, nso->nso_sotype, port, |
0a7de745 | 1368 | NFS_PROG, nfsvers, NMFLAG(nmp, RESVPORT), &nsonfs); |
6d2010ae A |
1369 | if (error) { |
1370 | nfs_socket_search_update_error(&nss, error); | |
1371 | nfs_socket_destroy(nso); | |
1372 | goto keepsearching; | |
1373 | } | |
1374 | nsonfs->nso_location = nso->nso_location; | |
1375 | nsonfs->nso_wake = &nss; | |
1376 | error = sock_setupcall(nsonfs->nso_so, nfs_connect_upcall, nsonfs); | |
1377 | if (error) { | |
1378 | nfs_socket_search_update_error(&nss, error); | |
1379 | nfs_socket_destroy(nsonfs); | |
1380 | nfs_socket_destroy(nso); | |
1381 | goto keepsearching; | |
1382 | } | |
1383 | TAILQ_INSERT_TAIL(&nss.nss_socklist, nsonfs, nso_link); | |
1384 | nss.nss_sockcnt++; | |
1385 | if ((nfsvers < NFS_VER4) && !(nmp->nm_sockflags & NMSOCK_HASCONNECTED) && !NM_OMATTR_GIVEN(nmp, FH)) { | |
1386 | /* Set up socket address and port for MOUNT socket. */ | |
2d21ac55 | 1387 | error = 0; |
6d2010ae A |
1388 | bcopy(nso->nso_saddr, &ss, nso->nso_saddr->sa_len); |
1389 | port = nmp->nm_mountport; | |
0a7de745 | 1390 | if (ss.ss_family == AF_INET) { |
6d2010ae | 1391 | ((struct sockaddr_in*)&ss)->sin_port = htons(port); |
0a7de745 | 1392 | } else if (ss.ss_family == AF_INET6) { |
6d2010ae | 1393 | ((struct sockaddr_in6*)&ss)->sin6_port = htons(port); |
0a7de745 | 1394 | } |
6d2010ae A |
1395 | if (!port) { |
1396 | /* Get port/sockaddr for MOUNT version corresponding to NFS version. */ | |
1397 | /* If NFS version is unknown, optimistically choose for NFSv3. */ | |
1398 | int mntvers = (nfsvers == NFS_VER2) ? RPCMNT_VER1 : RPCMNT_VER3; | |
1399 | int mntproto = (NM_OMFLAG(nmp, MNTUDP) || (nso->nso_sotype == SOCK_DGRAM)) ? IPPROTO_UDP : IPPROTO_TCP; | |
1400 | error = nfs_portmap_lookup(nmp, vfs_context_current(), (struct sockaddr*)&ss, | |
0a7de745 | 1401 | nso->nso_so, RPCPROG_MNT, mntvers, mntproto, timeo); |
6d2010ae A |
1402 | } |
1403 | if (!error) { | |
0a7de745 | 1404 | if (ss.ss_family == AF_INET) { |
6d2010ae | 1405 | port = ntohs(((struct sockaddr_in*)&ss)->sin_port); |
0a7de745 | 1406 | } else if (ss.ss_family == AF_INET6) { |
6d2010ae | 1407 | port = ntohs(((struct sockaddr_in6*)&ss)->sin6_port); |
0a7de745 A |
1408 | } |
1409 | if (!port) { | |
6d2010ae | 1410 | error = EPROGUNAVAIL; |
0a7de745 | 1411 | } |
6d2010ae A |
1412 | } |
1413 | /* create sockaddr for MOUNT */ | |
0a7de745 A |
1414 | if (!error) { |
1415 | MALLOC(nsonfs->nso_saddr2, struct sockaddr *, ss.ss_len, M_SONAME, M_WAITOK | M_ZERO); | |
1416 | } | |
1417 | if (!error && !nsonfs->nso_saddr2) { | |
6d2010ae | 1418 | error = ENOMEM; |
0a7de745 A |
1419 | } |
1420 | if (!error) { | |
6d2010ae | 1421 | bcopy(&ss, nsonfs->nso_saddr2, ss.ss_len); |
0a7de745 | 1422 | } |
6d2010ae A |
1423 | if (error) { |
1424 | lck_mtx_lock(&nsonfs->nso_lock); | |
1425 | nsonfs->nso_error = error; | |
1426 | nsonfs->nso_flags |= NSO_DEAD; | |
1427 | lck_mtx_unlock(&nsonfs->nso_lock); | |
1428 | } | |
2d21ac55 | 1429 | } |
6d2010ae A |
1430 | nfs_socket_destroy(nso); |
1431 | goto keepsearching; | |
91447636 | 1432 | } |
2d21ac55 | 1433 | |
6d2010ae | 1434 | /* nso is an NFS socket */ |
39236c6e | 1435 | NFS_SOCK_DBG("nfs connect %s got NFS socket %p\n", vfs_statfs(nmp->nm_mountp)->f_mntfromname, nso); |
6d2010ae A |
1436 | |
1437 | /* If NFS version wasn't specified, it was determined during the connect. */ | |
1438 | nfsvers = nmp->nm_vers ? nmp->nm_vers : (int)nso->nso_version; | |
1439 | ||
1440 | /* Perform MOUNT call for initial NFSv2/v3 connection/mount. */ | |
1441 | if ((nfsvers < NFS_VER4) && !(nmp->nm_sockflags & NMSOCK_HASCONNECTED) && !NM_OMATTR_GIVEN(nmp, FH)) { | |
1442 | error = 0; | |
1443 | saddr = nso->nso_saddr2; | |
1444 | if (!saddr) { | |
1445 | /* Need sockaddr for MOUNT port */ | |
1446 | bcopy(nso->nso_saddr, &ss, nso->nso_saddr->sa_len); | |
1447 | port = nmp->nm_mountport; | |
0a7de745 | 1448 | if (ss.ss_family == AF_INET) { |
6d2010ae | 1449 | ((struct sockaddr_in*)&ss)->sin_port = htons(port); |
0a7de745 | 1450 | } else if (ss.ss_family == AF_INET6) { |
6d2010ae | 1451 | ((struct sockaddr_in6*)&ss)->sin6_port = htons(port); |
0a7de745 | 1452 | } |
6d2010ae A |
1453 | if (!port) { |
1454 | /* Get port/sockaddr for MOUNT version corresponding to NFS version. */ | |
1455 | int mntvers = (nfsvers == NFS_VER2) ? RPCMNT_VER1 : RPCMNT_VER3; | |
1456 | int mntproto = (NM_OMFLAG(nmp, MNTUDP) || (nso->nso_sotype == SOCK_DGRAM)) ? IPPROTO_UDP : IPPROTO_TCP; | |
1457 | error = nfs_portmap_lookup(nmp, vfs_context_current(), (struct sockaddr*)&ss, | |
0a7de745 A |
1458 | NULL, RPCPROG_MNT, mntvers, mntproto, timeo); |
1459 | if (ss.ss_family == AF_INET) { | |
6d2010ae | 1460 | port = ntohs(((struct sockaddr_in*)&ss)->sin_port); |
0a7de745 | 1461 | } else if (ss.ss_family == AF_INET6) { |
6d2010ae | 1462 | port = ntohs(((struct sockaddr_in6*)&ss)->sin6_port); |
0a7de745 | 1463 | } |
6d2010ae A |
1464 | } |
1465 | if (!error) { | |
0a7de745 | 1466 | if (port) { |
6d2010ae | 1467 | saddr = (struct sockaddr*)&ss; |
0a7de745 | 1468 | } else { |
6d2010ae | 1469 | error = EPROGUNAVAIL; |
0a7de745 | 1470 | } |
6d2010ae A |
1471 | } |
1472 | } | |
0a7de745 A |
1473 | if (saddr) { |
1474 | MALLOC(fh, fhandle_t *, sizeof(fhandle_t), M_TEMP, M_WAITOK | M_ZERO); | |
1475 | } | |
1476 | if (saddr && fh) { | |
316670eb | 1477 | MALLOC_ZONE(path, char *, MAXPATHLEN, M_NAMEI, M_WAITOK); |
0a7de745 | 1478 | } |
6d2010ae | 1479 | if (!saddr || !fh || !path) { |
0a7de745 | 1480 | if (!error) { |
6d2010ae | 1481 | error = ENOMEM; |
0a7de745 A |
1482 | } |
1483 | if (fh) { | |
6d2010ae | 1484 | FREE(fh, M_TEMP); |
0a7de745 A |
1485 | } |
1486 | if (path) { | |
6d2010ae | 1487 | FREE_ZONE(path, MAXPATHLEN, M_NAMEI); |
0a7de745 | 1488 | } |
6d2010ae A |
1489 | fh = NULL; |
1490 | path = NULL; | |
1491 | nfs_socket_search_update_error(&nss, error); | |
1492 | nfs_socket_destroy(nso); | |
1493 | goto keepsearching; | |
1494 | } | |
1495 | nfs_location_mntfromname(&nmp->nm_locations, nso->nso_location, path, MAXPATHLEN, 1); | |
1496 | error = nfs3_mount_rpc(nmp, saddr, nso->nso_sotype, nfsvers, | |
0a7de745 | 1497 | path, vfs_context_current(), timeo, fh, &nmp->nm_servsec); |
39236c6e | 1498 | NFS_SOCK_DBG("nfs connect %s socket %p mount %d\n", |
0a7de745 | 1499 | vfs_statfs(nmp->nm_mountp)->f_mntfromname, nso, error); |
6d2010ae A |
1500 | if (!error) { |
1501 | /* Make sure we can agree on a security flavor. */ | |
1502 | int o, s; /* indices into mount option and server security flavor lists */ | |
1503 | int found = 0; | |
1504 | ||
1505 | if ((nfsvers == NFS_VER3) && !nmp->nm_servsec.count) { | |
1506 | /* Some servers return an empty list to indicate RPCAUTH_SYS? */ | |
1507 | nmp->nm_servsec.count = 1; | |
1508 | nmp->nm_servsec.flavors[0] = RPCAUTH_SYS; | |
1509 | } | |
1510 | if (nmp->nm_sec.count) { | |
1511 | /* Choose the first flavor in our list that the server supports. */ | |
1512 | if (!nmp->nm_servsec.count) { | |
1513 | /* we don't know what the server supports, just use our first choice */ | |
1514 | nmp->nm_auth = nmp->nm_sec.flavors[0]; | |
1515 | found = 1; | |
1516 | } | |
0a7de745 A |
1517 | for (o = 0; !found && (o < nmp->nm_sec.count); o++) { |
1518 | for (s = 0; !found && (s < nmp->nm_servsec.count); s++) { | |
6d2010ae A |
1519 | if (nmp->nm_sec.flavors[o] == nmp->nm_servsec.flavors[s]) { |
1520 | nmp->nm_auth = nmp->nm_sec.flavors[o]; | |
1521 | found = 1; | |
1522 | } | |
0a7de745 A |
1523 | } |
1524 | } | |
6d2010ae A |
1525 | } else { |
1526 | /* Choose the first one we support from the server's list. */ | |
1527 | if (!nmp->nm_servsec.count) { | |
1528 | nmp->nm_auth = RPCAUTH_SYS; | |
1529 | found = 1; | |
1530 | } | |
0a7de745 | 1531 | for (s = 0; s < nmp->nm_servsec.count; s++) { |
6d2010ae A |
1532 | switch (nmp->nm_servsec.flavors[s]) { |
1533 | case RPCAUTH_SYS: | |
1534 | /* prefer RPCAUTH_SYS to RPCAUTH_NONE */ | |
0a7de745 | 1535 | if (found && (nmp->nm_auth == RPCAUTH_NONE)) { |
6d2010ae | 1536 | found = 0; |
0a7de745 | 1537 | } |
6d2010ae A |
1538 | case RPCAUTH_NONE: |
1539 | case RPCAUTH_KRB5: | |
1540 | case RPCAUTH_KRB5I: | |
1541 | case RPCAUTH_KRB5P: | |
1542 | if (!found) { | |
1543 | nmp->nm_auth = nmp->nm_servsec.flavors[s]; | |
1544 | found = 1; | |
1545 | } | |
1546 | break; | |
1547 | } | |
0a7de745 | 1548 | } |
6d2010ae A |
1549 | } |
1550 | error = !found ? EAUTH : 0; | |
1551 | } | |
1552 | FREE_ZONE(path, MAXPATHLEN, M_NAMEI); | |
1553 | path = NULL; | |
1554 | if (error) { | |
1555 | nfs_socket_search_update_error(&nss, error); | |
1556 | FREE(fh, M_TEMP); | |
1557 | fh = NULL; | |
1558 | nfs_socket_destroy(nso); | |
1559 | goto keepsearching; | |
1560 | } | |
0a7de745 | 1561 | if (nmp->nm_fh) { |
6d2010ae | 1562 | FREE(nmp->nm_fh, M_TEMP); |
0a7de745 | 1563 | } |
6d2010ae A |
1564 | nmp->nm_fh = fh; |
1565 | fh = NULL; | |
1566 | NFS_BITMAP_SET(nmp->nm_flags, NFS_MFLAG_CALLUMNT); | |
1567 | } | |
1568 | ||
1569 | /* put the real upcall in place */ | |
1570 | upcall = (nso->nso_sotype == SOCK_STREAM) ? nfs_tcp_rcv : nfs_udp_rcv; | |
1571 | error = sock_setupcall(nso->nso_so, upcall, nmp); | |
1c79356b | 1572 | if (error) { |
6d2010ae A |
1573 | nfs_socket_search_update_error(&nss, error); |
1574 | nfs_socket_destroy(nso); | |
1575 | goto keepsearching; | |
1c79356b | 1576 | } |
1c79356b | 1577 | |
6d2010ae A |
1578 | if (!(nmp->nm_sockflags & NMSOCK_HASCONNECTED)) { |
1579 | /* set mntfromname to this location */ | |
0a7de745 | 1580 | if (!NM_OMATTR_GIVEN(nmp, MNTFROM)) { |
6d2010ae | 1581 | nfs_location_mntfromname(&nmp->nm_locations, nso->nso_location, |
0a7de745 A |
1582 | vfs_statfs(nmp->nm_mountp)->f_mntfromname, |
1583 | sizeof(vfs_statfs(nmp->nm_mountp)->f_mntfromname), 0); | |
1584 | } | |
6d2010ae | 1585 | /* some negotiated values need to remain unchanged for the life of the mount */ |
0a7de745 | 1586 | if (!nmp->nm_sotype) { |
6d2010ae | 1587 | nmp->nm_sotype = nso->nso_sotype; |
0a7de745 | 1588 | } |
6d2010ae A |
1589 | if (!nmp->nm_vers) { |
1590 | nmp->nm_vers = nfsvers; | |
1591 | /* If we negotiated NFSv4, set nm_nfsport if we ended up on the standard NFS port */ | |
1592 | if ((nfsvers >= NFS_VER4) && !NFS_BITMAP_ISSET(nmp->nm_mattrs, NFS_MATTR_NFS_PORT)) { | |
0a7de745 | 1593 | if (nso->nso_saddr->sa_family == AF_INET) { |
6d2010ae | 1594 | port = ((struct sockaddr_in*)nso->nso_saddr)->sin_port = htons(port); |
0a7de745 | 1595 | } else if (nso->nso_saddr->sa_family == AF_INET6) { |
6d2010ae | 1596 | port = ((struct sockaddr_in6*)nso->nso_saddr)->sin6_port = htons(port); |
0a7de745 | 1597 | } else { |
6d2010ae | 1598 | port = 0; |
0a7de745 A |
1599 | } |
1600 | if (port == NFS_PORT) { | |
6d2010ae | 1601 | nmp->nm_nfsport = NFS_PORT; |
0a7de745 | 1602 | } |
6d2010ae A |
1603 | } |
1604 | } | |
1605 | /* do some version-specific pre-mount set up */ | |
1606 | if (nmp->nm_vers >= NFS_VER4) { | |
1607 | microtime(&now); | |
1608 | nmp->nm_mounttime = ((uint64_t)now.tv_sec << 32) | now.tv_usec; | |
0a7de745 | 1609 | if (!NMFLAG(nmp, NOCALLBACK)) { |
6d2010ae | 1610 | nfs4_mount_callback_setup(nmp); |
0a7de745 | 1611 | } |
6d2010ae A |
1612 | } |
1613 | } | |
1c79356b | 1614 | |
6d2010ae A |
1615 | /* Initialize NFS socket state variables */ |
1616 | lck_mtx_lock(&nmp->nm_lock); | |
1c79356b | 1617 | nmp->nm_srtt[0] = nmp->nm_srtt[1] = nmp->nm_srtt[2] = |
0a7de745 | 1618 | nmp->nm_srtt[3] = (NFS_TIMEO << 3); |
1c79356b | 1619 | nmp->nm_sdrtt[0] = nmp->nm_sdrtt[1] = nmp->nm_sdrtt[2] = |
0a7de745 | 1620 | nmp->nm_sdrtt[3] = 0; |
6d2010ae | 1621 | if (nso->nso_sotype == SOCK_DGRAM) { |
0a7de745 | 1622 | nmp->nm_cwnd = NFS_MAXCWND / 2; /* Initial send window */ |
2d21ac55 | 1623 | nmp->nm_sent = 0; |
6d2010ae | 1624 | } else if (nso->nso_sotype == SOCK_STREAM) { |
2d21ac55 A |
1625 | nmp->nm_timeouts = 0; |
1626 | } | |
1627 | nmp->nm_sockflags &= ~NMSOCK_CONNECTING; | |
1628 | nmp->nm_sockflags |= NMSOCK_SETUP; | |
6d2010ae A |
1629 | /* move the socket to the mount structure */ |
1630 | nmp->nm_nso = nso; | |
1631 | oldsaddr = nmp->nm_saddr; | |
1632 | nmp->nm_saddr = nso->nso_saddr; | |
2d21ac55 A |
1633 | lck_mtx_unlock(&nmp->nm_lock); |
1634 | error = nfs_connect_setup(nmp); | |
2d21ac55 | 1635 | lck_mtx_lock(&nmp->nm_lock); |
6d2010ae | 1636 | nmp->nm_sockflags &= ~NMSOCK_SETUP; |
2d21ac55 A |
1637 | if (!error) { |
1638 | nmp->nm_sockflags |= NMSOCK_READY; | |
1639 | wakeup(&nmp->nm_sockflags); | |
1640 | } | |
6d2010ae | 1641 | if (error) { |
39236c6e | 1642 | NFS_SOCK_DBG("nfs connect %s socket %p setup failed %d\n", |
0a7de745 | 1643 | vfs_statfs(nmp->nm_mountp)->f_mntfromname, nso, error); |
6d2010ae A |
1644 | nfs_socket_search_update_error(&nss, error); |
1645 | nmp->nm_saddr = oldsaddr; | |
1646 | if (!(nmp->nm_sockflags & NMSOCK_HASCONNECTED)) { | |
1647 | /* undo settings made prior to setup */ | |
0a7de745 | 1648 | if (!NFS_BITMAP_ISSET(nmp->nm_mattrs, NFS_MATTR_SOCKET_TYPE)) { |
6d2010ae | 1649 | nmp->nm_sotype = 0; |
0a7de745 | 1650 | } |
6d2010ae A |
1651 | if (!NFS_BITMAP_ISSET(nmp->nm_mattrs, NFS_MATTR_NFS_VERSION)) { |
1652 | if (nmp->nm_vers >= NFS_VER4) { | |
0a7de745 | 1653 | if (!NFS_BITMAP_ISSET(nmp->nm_mattrs, NFS_MATTR_NFS_PORT)) { |
6d2010ae | 1654 | nmp->nm_nfsport = 0; |
0a7de745 A |
1655 | } |
1656 | if (nmp->nm_cbid) { | |
6d2010ae | 1657 | nfs4_mount_callback_shutdown(nmp); |
0a7de745 A |
1658 | } |
1659 | if (IS_VALID_CRED(nmp->nm_mcred)) { | |
6d2010ae | 1660 | kauth_cred_unref(&nmp->nm_mcred); |
0a7de745 | 1661 | } |
6d2010ae A |
1662 | bzero(&nmp->nm_un, sizeof(nmp->nm_un)); |
1663 | } | |
1664 | nmp->nm_vers = 0; | |
1665 | } | |
1666 | } | |
1667 | lck_mtx_unlock(&nmp->nm_lock); | |
1668 | nmp->nm_nso = NULL; | |
1669 | nfs_socket_destroy(nso); | |
1670 | goto keepsearching; | |
1671 | } | |
1672 | ||
1673 | /* update current location */ | |
1674 | if ((nmp->nm_locations.nl_current.nli_flags & NLI_VALID) && | |
1675 | (nmp->nm_locations.nl_current.nli_serv != nso->nso_location.nli_serv)) { | |
1676 | /* server has changed, we should initiate failover/recovery */ | |
1677 | // XXX | |
1678 | } | |
1679 | nmp->nm_locations.nl_current = nso->nso_location; | |
1680 | nmp->nm_locations.nl_current.nli_flags |= NLI_VALID; | |
1681 | ||
1682 | if (!(nmp->nm_sockflags & NMSOCK_HASCONNECTED)) { | |
1683 | /* We have now successfully connected... make a note of it. */ | |
1684 | nmp->nm_sockflags |= NMSOCK_HASCONNECTED; | |
1685 | } | |
1686 | ||
2d21ac55 | 1687 | lck_mtx_unlock(&nmp->nm_lock); |
0a7de745 | 1688 | if (oldsaddr) { |
6d2010ae | 1689 | FREE(oldsaddr, M_SONAME); |
0a7de745 | 1690 | } |
6d2010ae | 1691 | |
0a7de745 | 1692 | if (nss.nss_flags & NSS_WARNED) { |
6d2010ae | 1693 | log(LOG_INFO, "nfs_connect: socket connect completed for %s\n", |
0a7de745 A |
1694 | vfs_statfs(nmp->nm_mountp)->f_mntfromname); |
1695 | } | |
6d2010ae A |
1696 | |
1697 | nmp->nm_nss = NULL; | |
1698 | nfs_socket_search_cleanup(&nss); | |
0a7de745 | 1699 | if (fh) { |
6d2010ae | 1700 | FREE(fh, M_TEMP); |
0a7de745 A |
1701 | } |
1702 | if (path) { | |
6d2010ae | 1703 | FREE_ZONE(path, MAXPATHLEN, M_NAMEI); |
0a7de745 | 1704 | } |
39236c6e | 1705 | NFS_SOCK_DBG("nfs connect %s success\n", vfs_statfs(nmp->nm_mountp)->f_mntfromname); |
0a7de745 | 1706 | return 0; |
2d21ac55 A |
1707 | } |
1708 | ||
6d2010ae | 1709 | |
2d21ac55 | 1710 | /* setup & confirm socket connection is functional */ |
b0d623f7 | 1711 | int |
2d21ac55 A |
1712 | nfs_connect_setup(struct nfsmount *nmp) |
1713 | { | |
6d2010ae | 1714 | int error = 0; |
2d21ac55 A |
1715 | |
1716 | if (nmp->nm_vers >= NFS_VER4) { | |
6d2010ae A |
1717 | if (nmp->nm_state & NFSSTA_CLIENTID) { |
1718 | /* first, try to renew our current state */ | |
1719 | error = nfs4_renew(nmp, R_SETUP); | |
1720 | if ((error == NFSERR_ADMIN_REVOKED) || | |
1721 | (error == NFSERR_CB_PATH_DOWN) || | |
1722 | (error == NFSERR_EXPIRED) || | |
1723 | (error == NFSERR_LEASE_MOVED) || | |
1724 | (error == NFSERR_STALE_CLIENTID)) { | |
1725 | lck_mtx_lock(&nmp->nm_lock); | |
1726 | nfs_need_recover(nmp, error); | |
1727 | lck_mtx_unlock(&nmp->nm_lock); | |
1728 | } | |
b0d623f7 | 1729 | } |
6d2010ae | 1730 | error = nfs4_setclientid(nmp); |
2d21ac55 | 1731 | } |
0a7de745 | 1732 | return error; |
1c79356b A |
1733 | } |
1734 | ||
1735 | /* | |
2d21ac55 A |
1736 | * NFS socket reconnect routine: |
1737 | * Called when a connection is broken. | |
1738 | * - disconnect the old socket | |
1c79356b A |
1739 | * - nfs_connect() again |
1740 | * - set R_MUSTRESEND for all outstanding requests on mount point | |
1741 | * If this fails the mount point is DEAD! | |
1c79356b | 1742 | */ |
b0d623f7 | 1743 | int |
2d21ac55 | 1744 | nfs_reconnect(struct nfsmount *nmp) |
1c79356b | 1745 | { |
2d21ac55 A |
1746 | struct nfsreq *rq; |
1747 | struct timeval now; | |
1748 | thread_t thd = current_thread(); | |
b0d623f7 A |
1749 | int error, wentdown = 0, verbose = 1; |
1750 | time_t lastmsg; | |
316670eb | 1751 | int timeo; |
2d21ac55 A |
1752 | |
1753 | microuptime(&now); | |
1754 | lastmsg = now.tv_sec - (nmp->nm_tprintf_delay - nmp->nm_tprintf_initial_delay); | |
1c79356b A |
1755 | |
1756 | nfs_disconnect(nmp); | |
2d21ac55 | 1757 | |
316670eb A |
1758 | |
1759 | lck_mtx_lock(&nmp->nm_lock); | |
1760 | timeo = nfs_is_squishy(nmp) ? 8 : 30; | |
1761 | lck_mtx_unlock(&nmp->nm_lock); | |
1762 | ||
1763 | while ((error = nfs_connect(nmp, verbose, timeo))) { | |
b0d623f7 A |
1764 | verbose = 0; |
1765 | nfs_disconnect(nmp); | |
0a7de745 A |
1766 | if ((error == EINTR) || (error == ERESTART)) { |
1767 | return EINTR; | |
1768 | } | |
1769 | if (error == EIO) { | |
1770 | return EIO; | |
1771 | } | |
2d21ac55 A |
1772 | microuptime(&now); |
1773 | if ((lastmsg + nmp->nm_tprintf_delay) < now.tv_sec) { | |
1774 | lastmsg = now.tv_sec; | |
fe8ab488 | 1775 | nfs_down(nmp, thd, error, NFSSTA_TIMEO, "can not connect", 0); |
2d21ac55 A |
1776 | wentdown = 1; |
1777 | } | |
1778 | lck_mtx_lock(&nmp->nm_lock); | |
4a249263 A |
1779 | if (!(nmp->nm_state & NFSSTA_MOUNTED)) { |
1780 | /* we're not yet completely mounted and */ | |
1781 | /* we can't reconnect, so we fail */ | |
2d21ac55 | 1782 | lck_mtx_unlock(&nmp->nm_lock); |
39236c6e | 1783 | NFS_SOCK_DBG("Not mounted returning %d\n", error); |
0a7de745 | 1784 | return error; |
2d21ac55 | 1785 | } |
fe8ab488 A |
1786 | |
1787 | if (nfs_mount_check_dead_timeout(nmp)) { | |
1788 | nfs_mount_make_zombie(nmp); | |
1789 | lck_mtx_unlock(&nmp->nm_lock); | |
0a7de745 | 1790 | return ENXIO; |
fe8ab488 | 1791 | } |
0a7de745 | 1792 | |
2d21ac55 A |
1793 | if ((error = nfs_sigintr(nmp, NULL, thd, 1))) { |
1794 | lck_mtx_unlock(&nmp->nm_lock); | |
0a7de745 | 1795 | return error; |
4a249263 | 1796 | } |
2d21ac55 | 1797 | lck_mtx_unlock(&nmp->nm_lock); |
0a7de745 A |
1798 | tsleep(nfs_reconnect, PSOCK, "nfs_reconnect_delay", 2 * hz); |
1799 | if ((error = nfs_sigintr(nmp, NULL, thd, 0))) { | |
1800 | return error; | |
1801 | } | |
1c79356b A |
1802 | } |
1803 | ||
0a7de745 | 1804 | if (wentdown) { |
2d21ac55 | 1805 | nfs_up(nmp, thd, NFSSTA_TIMEO, "connected"); |
0a7de745 | 1806 | } |
2d21ac55 | 1807 | |
1c79356b | 1808 | /* |
2d21ac55 A |
1809 | * Loop through outstanding request list and mark all requests |
1810 | * as needing a resend. (Though nfs_need_reconnect() probably | |
1811 | * marked them all already.) | |
1c79356b | 1812 | */ |
2d21ac55 A |
1813 | lck_mtx_lock(nfs_request_mutex); |
1814 | TAILQ_FOREACH(rq, &nfs_reqq, r_chain) { | |
1815 | if (rq->r_nmp == nmp) { | |
1816 | lck_mtx_lock(&rq->r_mtx); | |
1817 | if (!rq->r_error && !rq->r_nmrep.nmc_mhead && !(rq->r_flags & R_MUSTRESEND)) { | |
1818 | rq->r_flags |= R_MUSTRESEND; | |
1819 | rq->r_rtt = -1; | |
1820 | wakeup(rq); | |
0a7de745 | 1821 | if ((rq->r_flags & (R_IOD | R_ASYNC | R_ASYNCWAIT | R_SENDING)) == R_ASYNC) { |
2d21ac55 | 1822 | nfs_asyncio_resend(rq); |
0a7de745 | 1823 | } |
2d21ac55 A |
1824 | } |
1825 | lck_mtx_unlock(&rq->r_mtx); | |
1826 | } | |
1c79356b | 1827 | } |
2d21ac55 | 1828 | lck_mtx_unlock(nfs_request_mutex); |
0a7de745 | 1829 | return 0; |
1c79356b A |
1830 | } |
1831 | ||
1832 | /* | |
1833 | * NFS disconnect. Clean up and unlink. | |
1834 | */ | |
1835 | void | |
91447636 | 1836 | nfs_disconnect(struct nfsmount *nmp) |
1c79356b | 1837 | { |
6d2010ae | 1838 | struct nfs_socket *nso; |
1c79356b | 1839 | |
2d21ac55 | 1840 | lck_mtx_lock(&nmp->nm_lock); |
6d2010ae A |
1841 | tryagain: |
1842 | if (nmp->nm_nso) { | |
1843 | struct timespec ts = { 1, 0 }; | |
1844 | if (nmp->nm_state & NFSSTA_SENDING) { /* wait for sending to complete */ | |
1845 | nmp->nm_state |= NFSSTA_WANTSND; | |
0a7de745 | 1846 | msleep(&nmp->nm_state, &nmp->nm_lock, PZERO - 1, "nfswaitsending", &ts); |
6d2010ae A |
1847 | goto tryagain; |
1848 | } | |
1849 | if (nmp->nm_sockflags & NMSOCK_POKE) { /* wait for poking to complete */ | |
0a7de745 | 1850 | msleep(&nmp->nm_sockflags, &nmp->nm_lock, PZERO - 1, "nfswaitpoke", &ts); |
6d2010ae A |
1851 | goto tryagain; |
1852 | } | |
1853 | nmp->nm_sockflags |= NMSOCK_DISCONNECTING; | |
1854 | nmp->nm_sockflags &= ~NMSOCK_READY; | |
1855 | nso = nmp->nm_nso; | |
1856 | nmp->nm_nso = NULL; | |
0a7de745 | 1857 | if (nso->nso_saddr == nmp->nm_saddr) { |
6d2010ae | 1858 | nso->nso_saddr = NULL; |
0a7de745 | 1859 | } |
6d2010ae A |
1860 | lck_mtx_unlock(&nmp->nm_lock); |
1861 | nfs_socket_destroy(nso); | |
1862 | lck_mtx_lock(&nmp->nm_lock); | |
1863 | nmp->nm_sockflags &= ~NMSOCK_DISCONNECTING; | |
2d21ac55 | 1864 | lck_mtx_unlock(&nmp->nm_lock); |
2d21ac55 A |
1865 | } else { |
1866 | lck_mtx_unlock(&nmp->nm_lock); | |
1c79356b | 1867 | } |
1c79356b A |
1868 | } |
1869 | ||
1870 | /* | |
2d21ac55 | 1871 | * mark an NFS mount as needing a reconnect/resends. |
1c79356b | 1872 | */ |
b0d623f7 | 1873 | void |
2d21ac55 | 1874 | nfs_need_reconnect(struct nfsmount *nmp) |
1c79356b | 1875 | { |
2d21ac55 | 1876 | struct nfsreq *rq; |
1c79356b | 1877 | |
2d21ac55 | 1878 | lck_mtx_lock(&nmp->nm_lock); |
0a7de745 | 1879 | nmp->nm_sockflags &= ~(NMSOCK_READY | NMSOCK_SETUP); |
2d21ac55 | 1880 | lck_mtx_unlock(&nmp->nm_lock); |
1c79356b | 1881 | |
2d21ac55 A |
1882 | /* |
1883 | * Loop through outstanding request list and | |
1884 | * mark all requests as needing a resend. | |
1c79356b | 1885 | */ |
2d21ac55 A |
1886 | lck_mtx_lock(nfs_request_mutex); |
1887 | TAILQ_FOREACH(rq, &nfs_reqq, r_chain) { | |
1888 | if (rq->r_nmp == nmp) { | |
1889 | lck_mtx_lock(&rq->r_mtx); | |
1890 | if (!rq->r_error && !rq->r_nmrep.nmc_mhead && !(rq->r_flags & R_MUSTRESEND)) { | |
1891 | rq->r_flags |= R_MUSTRESEND; | |
1892 | rq->r_rtt = -1; | |
1893 | wakeup(rq); | |
0a7de745 | 1894 | if ((rq->r_flags & (R_IOD | R_ASYNC | R_ASYNCWAIT | R_SENDING)) == R_ASYNC) { |
2d21ac55 | 1895 | nfs_asyncio_resend(rq); |
0a7de745 | 1896 | } |
1c79356b | 1897 | } |
2d21ac55 | 1898 | lck_mtx_unlock(&rq->r_mtx); |
55e303ae | 1899 | } |
1c79356b | 1900 | } |
2d21ac55 | 1901 | lck_mtx_unlock(nfs_request_mutex); |
1c79356b A |
1902 | } |
1903 | ||
6d2010ae | 1904 | |
1c79356b | 1905 | /* |
2d21ac55 | 1906 | * thread to handle miscellaneous async NFS socket work (reconnects/resends) |
1c79356b | 1907 | */ |
b0d623f7 | 1908 | void |
2d21ac55 | 1909 | nfs_mount_sock_thread(void *arg, __unused wait_result_t wr) |
1c79356b | 1910 | { |
2d21ac55 A |
1911 | struct nfsmount *nmp = arg; |
1912 | struct timespec ts = { 30, 0 }; | |
1913 | thread_t thd = current_thread(); | |
1914 | struct nfsreq *req; | |
1915 | struct timeval now; | |
6d2010ae | 1916 | int error, dofinish; |
b0d623f7 | 1917 | nfsnode_t np; |
39236c6e | 1918 | int do_reconnect_sleep = 0; |
1c79356b | 1919 | |
2d21ac55 | 1920 | lck_mtx_lock(&nmp->nm_lock); |
b0d623f7 | 1921 | while (!(nmp->nm_sockflags & NMSOCK_READY) || |
0a7de745 A |
1922 | !TAILQ_EMPTY(&nmp->nm_resendq) || |
1923 | !LIST_EMPTY(&nmp->nm_monlist) || | |
1924 | nmp->nm_deadto_start || | |
1925 | (nmp->nm_state & NFSSTA_RECOVER) || | |
1926 | ((nmp->nm_vers >= NFS_VER4) && !TAILQ_EMPTY(&nmp->nm_dreturnq))) { | |
1927 | if (nmp->nm_sockflags & NMSOCK_UNMOUNT) { | |
2d21ac55 | 1928 | break; |
0a7de745 | 1929 | } |
2d21ac55 | 1930 | /* do reconnect, if necessary */ |
0a7de745 | 1931 | if (!(nmp->nm_sockflags & NMSOCK_READY) && !(nmp->nm_state & (NFSSTA_FORCE | NFSSTA_DEAD))) { |
2d21ac55 A |
1932 | if (nmp->nm_reconnect_start <= 0) { |
1933 | microuptime(&now); | |
1934 | nmp->nm_reconnect_start = now.tv_sec; | |
1c79356b | 1935 | } |
2d21ac55 | 1936 | lck_mtx_unlock(&nmp->nm_lock); |
39236c6e A |
1937 | NFS_SOCK_DBG("nfs reconnect %s\n", vfs_statfs(nmp->nm_mountp)->f_mntfromname); |
1938 | /* | |
0a7de745 | 1939 | * XXX We don't want to call reconnect again right away if returned errors |
39236c6e A |
1940 | * before that may not have blocked. This has caused spamming null procs |
1941 | * from machines in the pass. | |
1942 | */ | |
0a7de745 | 1943 | if (do_reconnect_sleep) { |
39236c6e | 1944 | tsleep(nfs_mount_sock_thread, PSOCK, "nfs_reconnect_sock_thread_delay", hz); |
0a7de745 | 1945 | } |
39236c6e A |
1946 | error = nfs_reconnect(nmp); |
1947 | if (error) { | |
1948 | int lvl = 7; | |
1949 | if (error == EIO || error == EINTR) { | |
1950 | lvl = (do_reconnect_sleep++ % 600) ? 7 : 0; | |
1951 | } | |
1952 | nfs_printf(NFS_FAC_SOCK, lvl, "nfs reconnect %s: returned %d\n", | |
0a7de745 | 1953 | vfs_statfs(nmp->nm_mountp)->f_mntfromname, error); |
39236c6e | 1954 | } else { |
2d21ac55 | 1955 | nmp->nm_reconnect_start = 0; |
39236c6e A |
1956 | do_reconnect_sleep = 0; |
1957 | } | |
2d21ac55 | 1958 | lck_mtx_lock(&nmp->nm_lock); |
1c79356b | 1959 | } |
b0d623f7 A |
1960 | if ((nmp->nm_sockflags & NMSOCK_READY) && |
1961 | (nmp->nm_state & NFSSTA_RECOVER) && | |
6d2010ae | 1962 | !(nmp->nm_sockflags & NMSOCK_UNMOUNT) && |
0a7de745 | 1963 | !(nmp->nm_state & (NFSSTA_FORCE | NFSSTA_DEAD))) { |
b0d623f7 A |
1964 | /* perform state recovery */ |
1965 | lck_mtx_unlock(&nmp->nm_lock); | |
6d2010ae | 1966 | nfs_recover(nmp); |
b0d623f7 A |
1967 | lck_mtx_lock(&nmp->nm_lock); |
1968 | } | |
6d2010ae | 1969 | /* handle NFSv4 delegation returns */ |
0a7de745 A |
1970 | while ((nmp->nm_vers >= NFS_VER4) && !(nmp->nm_state & (NFSSTA_FORCE | NFSSTA_DEAD)) && |
1971 | (nmp->nm_sockflags & NMSOCK_READY) && !(nmp->nm_state & NFSSTA_RECOVER) && | |
1972 | ((np = TAILQ_FIRST(&nmp->nm_dreturnq)))) { | |
b0d623f7 | 1973 | lck_mtx_unlock(&nmp->nm_lock); |
6d2010ae | 1974 | nfs4_delegation_return(np, R_RECOVER, thd, nmp->nm_mcred); |
b0d623f7 A |
1975 | lck_mtx_lock(&nmp->nm_lock); |
1976 | } | |
2d21ac55 | 1977 | /* do resends, if necessary/possible */ |
fe8ab488 | 1978 | while ((((nmp->nm_sockflags & NMSOCK_READY) && !(nmp->nm_state & NFSSTA_RECOVER)) || |
0a7de745 A |
1979 | (nmp->nm_state & (NFSSTA_FORCE | NFSSTA_DEAD))) && |
1980 | ((req = TAILQ_FIRST(&nmp->nm_resendq)))) { | |
1981 | if (req->r_resendtime) { | |
2d21ac55 | 1982 | microuptime(&now); |
0a7de745 A |
1983 | } |
1984 | while (req && !(nmp->nm_state & (NFSSTA_FORCE | NFSSTA_DEAD)) && req->r_resendtime && (now.tv_sec < req->r_resendtime)) { | |
2d21ac55 | 1985 | req = TAILQ_NEXT(req, r_rchain); |
0a7de745 A |
1986 | } |
1987 | if (!req) { | |
2d21ac55 | 1988 | break; |
0a7de745 | 1989 | } |
2d21ac55 A |
1990 | TAILQ_REMOVE(&nmp->nm_resendq, req, r_rchain); |
1991 | req->r_rchain.tqe_next = NFSREQNOLIST; | |
1992 | lck_mtx_unlock(&nmp->nm_lock); | |
1993 | lck_mtx_lock(&req->r_mtx); | |
3e170ce0 | 1994 | /* Note that we have a reference on the request that was taken nfs_asyncio_resend */ |
2d21ac55 A |
1995 | if (req->r_error || req->r_nmrep.nmc_mhead) { |
1996 | dofinish = req->r_callback.rcb_func && !(req->r_flags & R_WAITSENT); | |
1997 | req->r_flags &= ~R_RESENDQ; | |
1998 | wakeup(req); | |
1999 | lck_mtx_unlock(&req->r_mtx); | |
0a7de745 | 2000 | if (dofinish) { |
2d21ac55 | 2001 | nfs_asyncio_finish(req); |
0a7de745 | 2002 | } |
3e170ce0 | 2003 | nfs_request_rele(req); |
2d21ac55 A |
2004 | lck_mtx_lock(&nmp->nm_lock); |
2005 | continue; | |
1c79356b | 2006 | } |
6d2010ae | 2007 | if ((req->r_flags & R_RESTART) || nfs_request_using_gss(req)) { |
2d21ac55 A |
2008 | req->r_flags &= ~R_RESTART; |
2009 | req->r_resendtime = 0; | |
2010 | lck_mtx_unlock(&req->r_mtx); | |
2011 | /* async RPCs on GSS mounts need to be rebuilt and resent. */ | |
2012 | nfs_reqdequeue(req); | |
6d2010ae | 2013 | if (nfs_request_using_gss(req)) { |
2d21ac55 A |
2014 | nfs_gss_clnt_rpcdone(req); |
2015 | error = nfs_gss_clnt_args_restore(req); | |
0a7de745 | 2016 | if (error == ENEEDAUTH) { |
2d21ac55 | 2017 | req->r_xid = 0; |
0a7de745 | 2018 | } |
1c79356b | 2019 | } |
39236c6e | 2020 | NFS_SOCK_DBG("nfs async%s restart: p %d x 0x%llx f 0x%x rtt %d\n", |
0a7de745 A |
2021 | nfs_request_using_gss(req) ? " gss" : "", req->r_procnum, req->r_xid, |
2022 | req->r_flags, req->r_rtt); | |
fe8ab488 | 2023 | error = nfs_sigintr(nmp, req, req->r_thread, 0); |
0a7de745 | 2024 | if (!error) { |
2d21ac55 | 2025 | error = nfs_request_add_header(req); |
0a7de745 A |
2026 | } |
2027 | if (!error) { | |
2d21ac55 | 2028 | error = nfs_request_send(req, 0); |
0a7de745 | 2029 | } |
2d21ac55 | 2030 | lck_mtx_lock(&req->r_mtx); |
0a7de745 | 2031 | if (req->r_flags & R_RESENDQ) { |
2d21ac55 | 2032 | req->r_flags &= ~R_RESENDQ; |
0a7de745 A |
2033 | } |
2034 | if (error) { | |
2d21ac55 | 2035 | req->r_error = error; |
0a7de745 | 2036 | } |
2d21ac55 A |
2037 | wakeup(req); |
2038 | dofinish = error && req->r_callback.rcb_func && !(req->r_flags & R_WAITSENT); | |
2039 | lck_mtx_unlock(&req->r_mtx); | |
0a7de745 | 2040 | if (dofinish) { |
2d21ac55 | 2041 | nfs_asyncio_finish(req); |
0a7de745 | 2042 | } |
3e170ce0 | 2043 | nfs_request_rele(req); |
2d21ac55 A |
2044 | lck_mtx_lock(&nmp->nm_lock); |
2045 | error = 0; | |
2046 | continue; | |
ccc36f2f | 2047 | } |
39236c6e | 2048 | NFS_SOCK_DBG("nfs async resend: p %d x 0x%llx f 0x%x rtt %d\n", |
0a7de745 | 2049 | req->r_procnum, req->r_xid, req->r_flags, req->r_rtt); |
fe8ab488 | 2050 | error = nfs_sigintr(nmp, req, req->r_thread, 0); |
55e303ae | 2051 | if (!error) { |
b0d623f7 | 2052 | req->r_flags |= R_SENDING; |
2d21ac55 A |
2053 | lck_mtx_unlock(&req->r_mtx); |
2054 | error = nfs_send(req, 0); | |
2055 | lck_mtx_lock(&req->r_mtx); | |
2056 | if (!error) { | |
0a7de745 | 2057 | if (req->r_flags & R_RESENDQ) { |
2d21ac55 | 2058 | req->r_flags &= ~R_RESENDQ; |
0a7de745 | 2059 | } |
2d21ac55 A |
2060 | wakeup(req); |
2061 | lck_mtx_unlock(&req->r_mtx); | |
3e170ce0 | 2062 | nfs_request_rele(req); |
2d21ac55 A |
2063 | lck_mtx_lock(&nmp->nm_lock); |
2064 | continue; | |
55e303ae | 2065 | } |
1c79356b | 2066 | } |
2d21ac55 | 2067 | req->r_error = error; |
0a7de745 | 2068 | if (req->r_flags & R_RESENDQ) { |
2d21ac55 | 2069 | req->r_flags &= ~R_RESENDQ; |
0a7de745 | 2070 | } |
2d21ac55 A |
2071 | wakeup(req); |
2072 | dofinish = req->r_callback.rcb_func && !(req->r_flags & R_WAITSENT); | |
2073 | lck_mtx_unlock(&req->r_mtx); | |
0a7de745 | 2074 | if (dofinish) { |
2d21ac55 | 2075 | nfs_asyncio_finish(req); |
0a7de745 | 2076 | } |
3e170ce0 | 2077 | nfs_request_rele(req); |
2d21ac55 A |
2078 | lck_mtx_lock(&nmp->nm_lock); |
2079 | } | |
fe8ab488 A |
2080 | if (nfs_mount_check_dead_timeout(nmp)) { |
2081 | nfs_mount_make_zombie(nmp); | |
2082 | break; | |
2083 | } | |
0a7de745 A |
2084 | |
2085 | if (nmp->nm_state & (NFSSTA_FORCE | NFSSTA_DEAD)) { | |
2d21ac55 | 2086 | break; |
0a7de745 | 2087 | } |
6d2010ae A |
2088 | /* check monitored nodes, if necessary/possible */ |
2089 | if (!LIST_EMPTY(&nmp->nm_monlist)) { | |
2090 | nmp->nm_state |= NFSSTA_MONITOR_SCAN; | |
2091 | LIST_FOREACH(np, &nmp->nm_monlist, n_monlink) { | |
fe8ab488 | 2092 | if (!(nmp->nm_sockflags & NMSOCK_READY) || |
0a7de745 | 2093 | (nmp->nm_state & (NFSSTA_RECOVER | NFSSTA_UNMOUNTING | NFSSTA_FORCE | NFSSTA_DEAD))) { |
6d2010ae | 2094 | break; |
0a7de745 | 2095 | } |
6d2010ae A |
2096 | np->n_mflag |= NMMONSCANINPROG; |
2097 | lck_mtx_unlock(&nmp->nm_lock); | |
0a7de745 A |
2098 | error = nfs_getattr(np, NULL, vfs_context_kernel(), (NGA_UNCACHED | NGA_MONITOR)); |
2099 | if (!error && ISSET(np->n_flag, NUPDATESIZE)) { /* update quickly to avoid multiple events */ | |
6d2010ae | 2100 | nfs_data_update_size(np, 0); |
0a7de745 | 2101 | } |
6d2010ae A |
2102 | lck_mtx_lock(&nmp->nm_lock); |
2103 | np->n_mflag &= ~NMMONSCANINPROG; | |
2104 | if (np->n_mflag & NMMONSCANWANT) { | |
2105 | np->n_mflag &= ~NMMONSCANWANT; | |
2106 | wakeup(&np->n_mflag); | |
2107 | } | |
fe8ab488 | 2108 | if (error || !(nmp->nm_sockflags & NMSOCK_READY) || |
0a7de745 | 2109 | (nmp->nm_state & (NFSSTA_RECOVER | NFSSTA_UNMOUNTING | NFSSTA_FORCE | NFSSTA_DEAD))) { |
6d2010ae | 2110 | break; |
0a7de745 | 2111 | } |
6d2010ae A |
2112 | } |
2113 | nmp->nm_state &= ~NFSSTA_MONITOR_SCAN; | |
0a7de745 | 2114 | if (nmp->nm_state & NFSSTA_UNMOUNTING) { |
6d2010ae | 2115 | wakeup(&nmp->nm_state); /* let unmounting thread know scan is done */ |
0a7de745 | 2116 | } |
6d2010ae | 2117 | } |
0a7de745 | 2118 | if ((nmp->nm_sockflags & NMSOCK_READY) || (nmp->nm_state & (NFSSTA_RECOVER | NFSSTA_UNMOUNTING))) { |
b0d623f7 | 2119 | if (nmp->nm_deadto_start || !TAILQ_EMPTY(&nmp->nm_resendq) || |
0a7de745 | 2120 | (nmp->nm_state & NFSSTA_RECOVER)) { |
b0d623f7 | 2121 | ts.tv_sec = 1; |
0a7de745 | 2122 | } else { |
6d2010ae | 2123 | ts.tv_sec = 5; |
0a7de745 | 2124 | } |
b0d623f7 A |
2125 | msleep(&nmp->nm_sockthd, &nmp->nm_lock, PSOCK, "nfssockthread", &ts); |
2126 | } | |
2127 | } | |
2128 | ||
2129 | /* If we're unmounting, send the unmount RPC, if requested/appropriate. */ | |
6d2010ae A |
2130 | if ((nmp->nm_sockflags & NMSOCK_UNMOUNT) && |
2131 | (nmp->nm_state & NFSSTA_MOUNTED) && NMFLAG(nmp, CALLUMNT) && | |
0a7de745 | 2132 | (nmp->nm_vers < NFS_VER4) && !(nmp->nm_state & (NFSSTA_FORCE | NFSSTA_DEAD))) { |
b0d623f7 A |
2133 | lck_mtx_unlock(&nmp->nm_lock); |
2134 | nfs3_umount_rpc(nmp, vfs_context_kernel(), | |
0a7de745 | 2135 | (nmp->nm_sockflags & NMSOCK_READY) ? 6 : 2); |
b0d623f7 | 2136 | lck_mtx_lock(&nmp->nm_lock); |
1c79356b | 2137 | } |
2d21ac55 | 2138 | |
0a7de745 | 2139 | if (nmp->nm_sockthd == thd) { |
2d21ac55 | 2140 | nmp->nm_sockthd = NULL; |
0a7de745 | 2141 | } |
2d21ac55 A |
2142 | lck_mtx_unlock(&nmp->nm_lock); |
2143 | wakeup(&nmp->nm_sockthd); | |
2144 | thread_terminate(thd); | |
2145 | } | |
2146 | ||
2147 | /* start or wake a mount's socket thread */ | |
2148 | void | |
2149 | nfs_mount_sock_thread_wake(struct nfsmount *nmp) | |
2150 | { | |
0a7de745 | 2151 | if (nmp->nm_sockthd) { |
2d21ac55 | 2152 | wakeup(&nmp->nm_sockthd); |
0a7de745 | 2153 | } else if (kernel_thread_start(nfs_mount_sock_thread, nmp, &nmp->nm_sockthd) == KERN_SUCCESS) { |
2d21ac55 | 2154 | thread_deallocate(nmp->nm_sockthd); |
0a7de745 | 2155 | } |
1c79356b A |
2156 | } |
2157 | ||
b0d623f7 A |
2158 | /* |
2159 | * Check if we should mark the mount dead because the | |
2160 | * unresponsive mount has reached the dead timeout. | |
2161 | * (must be called with nmp locked) | |
2162 | */ | |
fe8ab488 | 2163 | int |
b0d623f7 A |
2164 | nfs_mount_check_dead_timeout(struct nfsmount *nmp) |
2165 | { | |
2166 | struct timeval now; | |
2167 | ||
0a7de745 | 2168 | if (nmp->nm_state & NFSSTA_DEAD) { |
fe8ab488 | 2169 | return 1; |
0a7de745 A |
2170 | } |
2171 | if (nmp->nm_deadto_start == 0) { | |
fe8ab488 | 2172 | return 0; |
0a7de745 | 2173 | } |
316670eb | 2174 | nfs_is_squishy(nmp); |
0a7de745 | 2175 | if (nmp->nm_curdeadtimeout <= 0) { |
fe8ab488 | 2176 | return 0; |
0a7de745 | 2177 | } |
b0d623f7 | 2178 | microuptime(&now); |
0a7de745 | 2179 | if ((now.tv_sec - nmp->nm_deadto_start) < nmp->nm_curdeadtimeout) { |
fe8ab488 | 2180 | return 0; |
0a7de745 | 2181 | } |
fe8ab488 A |
2182 | return 1; |
2183 | } | |
2184 | ||
2185 | /* | |
2186 | * Call nfs_mount_zombie to remove most of the | |
2187 | * nfs state for the mount, and then ask to be forcibly unmounted. | |
2188 | * | |
2189 | * Assumes the nfs mount structure lock nm_lock is held. | |
2190 | */ | |
2191 | ||
2192 | void | |
2193 | nfs_mount_make_zombie(struct nfsmount *nmp) | |
2194 | { | |
2195 | fsid_t fsid; | |
0a7de745 A |
2196 | |
2197 | if (!nmp) { | |
fe8ab488 | 2198 | return; |
0a7de745 | 2199 | } |
fe8ab488 | 2200 | |
0a7de745 | 2201 | if (nmp->nm_state & NFSSTA_DEAD) { |
b0d623f7 | 2202 | return; |
0a7de745 | 2203 | } |
fe8ab488 | 2204 | |
316670eb | 2205 | printf("nfs server %s: %sdead\n", vfs_statfs(nmp->nm_mountp)->f_mntfromname, |
0a7de745 | 2206 | (nmp->nm_curdeadtimeout != nmp->nm_deadtimeout) ? "squished " : ""); |
fe8ab488 A |
2207 | fsid = vfs_statfs(nmp->nm_mountp)->f_fsid; |
2208 | lck_mtx_unlock(&nmp->nm_lock); | |
2209 | nfs_mount_zombie(nmp, NFSSTA_DEAD); | |
2210 | vfs_event_signal(&fsid, VQ_DEAD, 0); | |
2211 | lck_mtx_lock(&nmp->nm_lock); | |
b0d623f7 A |
2212 | } |
2213 | ||
fe8ab488 | 2214 | |
b0d623f7 A |
2215 | /* |
2216 | * NFS callback channel socket state | |
2217 | */ | |
0a7de745 | 2218 | struct nfs_callback_socket { |
b0d623f7 | 2219 | TAILQ_ENTRY(nfs_callback_socket) ncbs_link; |
0a7de745 A |
2220 | socket_t ncbs_so; /* the socket */ |
2221 | struct sockaddr_storage ncbs_saddr; /* socket address */ | |
2222 | struct nfs_rpc_record_state ncbs_rrs; /* RPC record parsing state */ | |
2223 | time_t ncbs_stamp; /* last accessed at */ | |
2224 | uint32_t ncbs_flags; /* see below */ | |
b0d623f7 | 2225 | }; |
0a7de745 A |
2226 | #define NCBSOCK_UPCALL 0x0001 |
2227 | #define NCBSOCK_UPCALLWANT 0x0002 | |
2228 | #define NCBSOCK_DEAD 0x0004 | |
b0d623f7 A |
2229 | |
2230 | /* | |
2231 | * NFS callback channel state | |
2232 | * | |
2233 | * One listening socket for accepting socket connections from servers and | |
2234 | * a list of connected sockets to handle callback requests on. | |
2235 | * Mounts registered with the callback channel are assigned IDs and | |
2236 | * put on a list so that the callback request handling code can match | |
2237 | * the requests up with mounts. | |
2238 | */ | |
2239 | socket_t nfs4_cb_so = NULL; | |
6d2010ae | 2240 | socket_t nfs4_cb_so6 = NULL; |
b0d623f7 | 2241 | in_port_t nfs4_cb_port = 0; |
6d2010ae | 2242 | in_port_t nfs4_cb_port6 = 0; |
b0d623f7 A |
2243 | uint32_t nfs4_cb_id = 0; |
2244 | uint32_t nfs4_cb_so_usecount = 0; | |
0a7de745 A |
2245 | TAILQ_HEAD(nfs4_cb_sock_list, nfs_callback_socket) nfs4_cb_socks; |
2246 | TAILQ_HEAD(nfs4_cb_mount_list, nfsmount) nfs4_cb_mounts; | |
b0d623f7 A |
2247 | |
2248 | int nfs4_cb_handler(struct nfs_callback_socket *, mbuf_t); | |
2249 | ||
2250 | /* | |
2251 | * Set up the callback channel for the NFS mount. | |
2252 | * | |
2253 | * Initializes the callback channel socket state and | |
2254 | * assigns a callback ID to the mount. | |
2255 | */ | |
2256 | void | |
2257 | nfs4_mount_callback_setup(struct nfsmount *nmp) | |
2258 | { | |
2259 | struct sockaddr_in sin; | |
6d2010ae | 2260 | struct sockaddr_in6 sin6; |
b0d623f7 | 2261 | socket_t so = NULL; |
6d2010ae | 2262 | socket_t so6 = NULL; |
b0d623f7 A |
2263 | struct timeval timeo; |
2264 | int error, on = 1; | |
6d2010ae | 2265 | in_port_t port; |
b0d623f7 A |
2266 | |
2267 | lck_mtx_lock(nfs_global_mutex); | |
2268 | if (nfs4_cb_id == 0) { | |
2269 | TAILQ_INIT(&nfs4_cb_mounts); | |
2270 | TAILQ_INIT(&nfs4_cb_socks); | |
2271 | nfs4_cb_id++; | |
2272 | } | |
2273 | nmp->nm_cbid = nfs4_cb_id++; | |
0a7de745 | 2274 | if (nmp->nm_cbid == 0) { |
b0d623f7 | 2275 | nmp->nm_cbid = nfs4_cb_id++; |
0a7de745 | 2276 | } |
b0d623f7 A |
2277 | nfs4_cb_so_usecount++; |
2278 | TAILQ_INSERT_HEAD(&nfs4_cb_mounts, nmp, nm_cblink); | |
2279 | ||
2280 | if (nfs4_cb_so) { | |
2281 | lck_mtx_unlock(nfs_global_mutex); | |
2282 | return; | |
2283 | } | |
2284 | ||
6d2010ae | 2285 | /* IPv4 */ |
b0d623f7 A |
2286 | error = sock_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP, nfs4_cb_accept, NULL, &nfs4_cb_so); |
2287 | if (error) { | |
6d2010ae | 2288 | log(LOG_INFO, "nfs callback setup: error %d creating listening IPv4 socket\n", error); |
b0d623f7 A |
2289 | goto fail; |
2290 | } | |
2291 | so = nfs4_cb_so; | |
2292 | ||
6d2010ae | 2293 | sock_setsockopt(so, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
b0d623f7 A |
2294 | sin.sin_len = sizeof(struct sockaddr_in); |
2295 | sin.sin_family = AF_INET; | |
2296 | sin.sin_addr.s_addr = htonl(INADDR_ANY); | |
6d2010ae | 2297 | sin.sin_port = htons(nfs_callback_port); /* try to use specified port */ |
b0d623f7 A |
2298 | error = sock_bind(so, (struct sockaddr *)&sin); |
2299 | if (error) { | |
6d2010ae | 2300 | log(LOG_INFO, "nfs callback setup: error %d binding listening IPv4 socket\n", error); |
b0d623f7 A |
2301 | goto fail; |
2302 | } | |
2303 | error = sock_getsockname(so, (struct sockaddr *)&sin, sin.sin_len); | |
2304 | if (error) { | |
6d2010ae | 2305 | log(LOG_INFO, "nfs callback setup: error %d getting listening IPv4 socket port\n", error); |
b0d623f7 A |
2306 | goto fail; |
2307 | } | |
2308 | nfs4_cb_port = ntohs(sin.sin_port); | |
2309 | ||
2310 | error = sock_listen(so, 32); | |
2311 | if (error) { | |
6d2010ae | 2312 | log(LOG_INFO, "nfs callback setup: error %d on IPv4 listen\n", error); |
b0d623f7 A |
2313 | goto fail; |
2314 | } | |
2315 | ||
2316 | /* receive timeout shouldn't matter. If timeout on send, we'll want to drop the socket */ | |
2317 | timeo.tv_usec = 0; | |
2318 | timeo.tv_sec = 60; | |
2319 | error = sock_setsockopt(so, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo)); | |
0a7de745 | 2320 | if (error) { |
6d2010ae | 2321 | log(LOG_INFO, "nfs callback setup: error %d setting IPv4 socket rx timeout\n", error); |
0a7de745 | 2322 | } |
b0d623f7 | 2323 | error = sock_setsockopt(so, SOL_SOCKET, SO_SNDTIMEO, &timeo, sizeof(timeo)); |
0a7de745 | 2324 | if (error) { |
6d2010ae | 2325 | log(LOG_INFO, "nfs callback setup: error %d setting IPv4 socket tx timeout\n", error); |
0a7de745 | 2326 | } |
b0d623f7 A |
2327 | sock_setsockopt(so, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)); |
2328 | sock_setsockopt(so, SOL_SOCKET, SO_NOADDRERR, &on, sizeof(on)); | |
2329 | sock_setsockopt(so, SOL_SOCKET, SO_UPCALLCLOSEWAIT, &on, sizeof(on)); | |
2330 | error = 0; | |
2331 | ||
6d2010ae A |
2332 | /* IPv6 */ |
2333 | error = sock_socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP, nfs4_cb_accept, NULL, &nfs4_cb_so6); | |
2334 | if (error) { | |
2335 | log(LOG_INFO, "nfs callback setup: error %d creating listening IPv6 socket\n", error); | |
2336 | goto fail; | |
2337 | } | |
2338 | so6 = nfs4_cb_so6; | |
2339 | ||
2340 | sock_setsockopt(so6, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); | |
2341 | sock_setsockopt(so6, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)); | |
2342 | /* try to use specified port or same port as IPv4 */ | |
2343 | port = nfs_callback_port ? nfs_callback_port : nfs4_cb_port; | |
2344 | ipv6_bind_again: | |
2345 | sin6.sin6_len = sizeof(struct sockaddr_in6); | |
2346 | sin6.sin6_family = AF_INET6; | |
2347 | sin6.sin6_addr = in6addr_any; | |
2348 | sin6.sin6_port = htons(port); | |
2349 | error = sock_bind(so6, (struct sockaddr *)&sin6); | |
2350 | if (error) { | |
2351 | if (port != nfs_callback_port) { | |
2352 | /* if we simply tried to match the IPv4 port, then try any port */ | |
2353 | port = 0; | |
2354 | goto ipv6_bind_again; | |
2355 | } | |
2356 | log(LOG_INFO, "nfs callback setup: error %d binding listening IPv6 socket\n", error); | |
2357 | goto fail; | |
2358 | } | |
2359 | error = sock_getsockname(so6, (struct sockaddr *)&sin6, sin6.sin6_len); | |
2360 | if (error) { | |
2361 | log(LOG_INFO, "nfs callback setup: error %d getting listening IPv6 socket port\n", error); | |
2362 | goto fail; | |
2363 | } | |
2364 | nfs4_cb_port6 = ntohs(sin6.sin6_port); | |
2365 | ||
2366 | error = sock_listen(so6, 32); | |
2367 | if (error) { | |
2368 | log(LOG_INFO, "nfs callback setup: error %d on IPv6 listen\n", error); | |
2369 | goto fail; | |
2370 | } | |
2371 | ||
2372 | /* receive timeout shouldn't matter. If timeout on send, we'll want to drop the socket */ | |
2373 | timeo.tv_usec = 0; | |
2374 | timeo.tv_sec = 60; | |
2375 | error = sock_setsockopt(so6, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo)); | |
0a7de745 | 2376 | if (error) { |
6d2010ae | 2377 | log(LOG_INFO, "nfs callback setup: error %d setting IPv6 socket rx timeout\n", error); |
0a7de745 | 2378 | } |
6d2010ae | 2379 | error = sock_setsockopt(so6, SOL_SOCKET, SO_SNDTIMEO, &timeo, sizeof(timeo)); |
0a7de745 | 2380 | if (error) { |
6d2010ae | 2381 | log(LOG_INFO, "nfs callback setup: error %d setting IPv6 socket tx timeout\n", error); |
0a7de745 | 2382 | } |
6d2010ae A |
2383 | sock_setsockopt(so6, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)); |
2384 | sock_setsockopt(so6, SOL_SOCKET, SO_NOADDRERR, &on, sizeof(on)); | |
2385 | sock_setsockopt(so6, SOL_SOCKET, SO_UPCALLCLOSEWAIT, &on, sizeof(on)); | |
2386 | error = 0; | |
2387 | ||
b0d623f7 A |
2388 | fail: |
2389 | if (error) { | |
6d2010ae | 2390 | nfs4_cb_so = nfs4_cb_so6 = NULL; |
b0d623f7 A |
2391 | lck_mtx_unlock(nfs_global_mutex); |
2392 | if (so) { | |
2393 | sock_shutdown(so, SHUT_RDWR); | |
2394 | sock_close(so); | |
2395 | } | |
6d2010ae A |
2396 | if (so6) { |
2397 | sock_shutdown(so6, SHUT_RDWR); | |
2398 | sock_close(so6); | |
2399 | } | |
b0d623f7 A |
2400 | } else { |
2401 | lck_mtx_unlock(nfs_global_mutex); | |
2402 | } | |
2403 | } | |
2404 | ||
2405 | /* | |
2406 | * Shut down the callback channel for the NFS mount. | |
2407 | * | |
2408 | * Clears the mount's callback ID and releases the mounts | |
2409 | * reference on the callback socket. Last reference dropped | |
2410 | * will also shut down the callback socket(s). | |
2411 | */ | |
2412 | void | |
2413 | nfs4_mount_callback_shutdown(struct nfsmount *nmp) | |
2414 | { | |
2415 | struct nfs_callback_socket *ncbsp; | |
6d2010ae | 2416 | socket_t so, so6; |
b0d623f7 | 2417 | struct nfs4_cb_sock_list cb_socks; |
0a7de745 | 2418 | struct timespec ts = {1, 0}; |
b0d623f7 A |
2419 | |
2420 | lck_mtx_lock(nfs_global_mutex); | |
2421 | TAILQ_REMOVE(&nfs4_cb_mounts, nmp, nm_cblink); | |
2422 | /* wait for any callbacks in progress to complete */ | |
0a7de745 | 2423 | while (nmp->nm_cbrefs) { |
b0d623f7 | 2424 | msleep(&nmp->nm_cbrefs, nfs_global_mutex, PSOCK, "cbshutwait", &ts); |
0a7de745 | 2425 | } |
6d2010ae | 2426 | nmp->nm_cbid = 0; |
b0d623f7 A |
2427 | if (--nfs4_cb_so_usecount) { |
2428 | lck_mtx_unlock(nfs_global_mutex); | |
2429 | return; | |
2430 | } | |
2431 | so = nfs4_cb_so; | |
6d2010ae A |
2432 | so6 = nfs4_cb_so6; |
2433 | nfs4_cb_so = nfs4_cb_so6 = NULL; | |
b0d623f7 A |
2434 | TAILQ_INIT(&cb_socks); |
2435 | TAILQ_CONCAT(&cb_socks, &nfs4_cb_socks, ncbs_link); | |
2436 | lck_mtx_unlock(nfs_global_mutex); | |
2437 | if (so) { | |
2438 | sock_shutdown(so, SHUT_RDWR); | |
2439 | sock_close(so); | |
2440 | } | |
6d2010ae A |
2441 | if (so6) { |
2442 | sock_shutdown(so6, SHUT_RDWR); | |
2443 | sock_close(so6); | |
2444 | } | |
b0d623f7 A |
2445 | while ((ncbsp = TAILQ_FIRST(&cb_socks))) { |
2446 | TAILQ_REMOVE(&cb_socks, ncbsp, ncbs_link); | |
2447 | sock_shutdown(ncbsp->ncbs_so, SHUT_RDWR); | |
2448 | sock_close(ncbsp->ncbs_so); | |
6d2010ae | 2449 | nfs_rpc_record_state_cleanup(&ncbsp->ncbs_rrs); |
b0d623f7 A |
2450 | FREE(ncbsp, M_TEMP); |
2451 | } | |
2452 | } | |
2453 | ||
2454 | /* | |
2455 | * Check periodically for stale/unused nfs callback sockets | |
2456 | */ | |
0a7de745 A |
2457 | #define NFS4_CB_TIMER_PERIOD 30 |
2458 | #define NFS4_CB_IDLE_MAX 300 | |
b0d623f7 A |
2459 | void |
2460 | nfs4_callback_timer(__unused void *param0, __unused void *param1) | |
2461 | { | |
2462 | struct nfs_callback_socket *ncbsp, *nextncbsp; | |
2463 | struct timeval now; | |
2464 | ||
2465 | loop: | |
2466 | lck_mtx_lock(nfs_global_mutex); | |
2467 | if (TAILQ_EMPTY(&nfs4_cb_socks)) { | |
2468 | nfs4_callback_timer_on = 0; | |
2469 | lck_mtx_unlock(nfs_global_mutex); | |
2470 | return; | |
2471 | } | |
2472 | microuptime(&now); | |
2473 | TAILQ_FOREACH_SAFE(ncbsp, &nfs4_cb_socks, ncbs_link, nextncbsp) { | |
2474 | if (!(ncbsp->ncbs_flags & NCBSOCK_DEAD) && | |
0a7de745 | 2475 | (now.tv_sec < (ncbsp->ncbs_stamp + NFS4_CB_IDLE_MAX))) { |
b0d623f7 | 2476 | continue; |
0a7de745 | 2477 | } |
b0d623f7 A |
2478 | TAILQ_REMOVE(&nfs4_cb_socks, ncbsp, ncbs_link); |
2479 | lck_mtx_unlock(nfs_global_mutex); | |
2480 | sock_shutdown(ncbsp->ncbs_so, SHUT_RDWR); | |
2481 | sock_close(ncbsp->ncbs_so); | |
6d2010ae | 2482 | nfs_rpc_record_state_cleanup(&ncbsp->ncbs_rrs); |
b0d623f7 A |
2483 | FREE(ncbsp, M_TEMP); |
2484 | goto loop; | |
2485 | } | |
2486 | nfs4_callback_timer_on = 1; | |
2487 | nfs_interval_timer_start(nfs4_callback_timer_call, | |
0a7de745 | 2488 | NFS4_CB_TIMER_PERIOD * 1000); |
b0d623f7 A |
2489 | lck_mtx_unlock(nfs_global_mutex); |
2490 | } | |
2491 | ||
2492 | /* | |
2493 | * Accept a new callback socket. | |
2494 | */ | |
2495 | void | |
2496 | nfs4_cb_accept(socket_t so, __unused void *arg, __unused int waitflag) | |
2497 | { | |
2498 | socket_t newso = NULL; | |
2499 | struct nfs_callback_socket *ncbsp; | |
2500 | struct nfsmount *nmp; | |
2501 | struct timeval timeo, now; | |
6d2010ae | 2502 | int error, on = 1, ip; |
b0d623f7 | 2503 | |
0a7de745 | 2504 | if (so == nfs4_cb_so) { |
6d2010ae | 2505 | ip = 4; |
0a7de745 | 2506 | } else if (so == nfs4_cb_so6) { |
6d2010ae | 2507 | ip = 6; |
0a7de745 | 2508 | } else { |
b0d623f7 | 2509 | return; |
0a7de745 | 2510 | } |
b0d623f7 A |
2511 | |
2512 | /* allocate/initialize a new nfs_callback_socket */ | |
2513 | MALLOC(ncbsp, struct nfs_callback_socket *, sizeof(struct nfs_callback_socket), M_TEMP, M_WAITOK); | |
2514 | if (!ncbsp) { | |
2515 | log(LOG_ERR, "nfs callback accept: no memory for new socket\n"); | |
2516 | return; | |
2517 | } | |
2518 | bzero(ncbsp, sizeof(*ncbsp)); | |
6d2010ae A |
2519 | ncbsp->ncbs_saddr.ss_len = (ip == 4) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6); |
2520 | nfs_rpc_record_state_init(&ncbsp->ncbs_rrs); | |
b0d623f7 A |
2521 | |
2522 | /* accept a new socket */ | |
6d2010ae | 2523 | error = sock_accept(so, (struct sockaddr*)&ncbsp->ncbs_saddr, |
0a7de745 A |
2524 | ncbsp->ncbs_saddr.ss_len, MSG_DONTWAIT, |
2525 | nfs4_cb_rcv, ncbsp, &newso); | |
b0d623f7 | 2526 | if (error) { |
6d2010ae | 2527 | log(LOG_INFO, "nfs callback accept: error %d accepting IPv%d socket\n", error, ip); |
b0d623f7 A |
2528 | FREE(ncbsp, M_TEMP); |
2529 | return; | |
2530 | } | |
2531 | ||
2532 | /* set up the new socket */ | |
2533 | /* receive timeout shouldn't matter. If timeout on send, we'll want to drop the socket */ | |
2534 | timeo.tv_usec = 0; | |
2535 | timeo.tv_sec = 60; | |
2536 | error = sock_setsockopt(newso, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo)); | |
0a7de745 | 2537 | if (error) { |
6d2010ae | 2538 | log(LOG_INFO, "nfs callback socket: error %d setting IPv%d socket rx timeout\n", error, ip); |
0a7de745 | 2539 | } |
b0d623f7 | 2540 | error = sock_setsockopt(newso, SOL_SOCKET, SO_SNDTIMEO, &timeo, sizeof(timeo)); |
0a7de745 | 2541 | if (error) { |
6d2010ae | 2542 | log(LOG_INFO, "nfs callback socket: error %d setting IPv%d socket tx timeout\n", error, ip); |
0a7de745 | 2543 | } |
b0d623f7 | 2544 | sock_setsockopt(newso, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)); |
6d2010ae | 2545 | sock_setsockopt(newso, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
b0d623f7 A |
2546 | sock_setsockopt(newso, SOL_SOCKET, SO_NOADDRERR, &on, sizeof(on)); |
2547 | sock_setsockopt(newso, SOL_SOCKET, SO_UPCALLCLOSEWAIT, &on, sizeof(on)); | |
2548 | ||
2549 | ncbsp->ncbs_so = newso; | |
2550 | microuptime(&now); | |
2551 | ncbsp->ncbs_stamp = now.tv_sec; | |
2552 | ||
2553 | lck_mtx_lock(nfs_global_mutex); | |
2554 | ||
2555 | /* add it to the list */ | |
2556 | TAILQ_INSERT_HEAD(&nfs4_cb_socks, ncbsp, ncbs_link); | |
2557 | ||
2558 | /* verify it's from a host we have mounted */ | |
2559 | TAILQ_FOREACH(nmp, &nfs4_cb_mounts, nm_cblink) { | |
6d2010ae | 2560 | /* check if socket's source address matches this mount's server address */ |
0a7de745 | 2561 | if (!nmp->nm_saddr) { |
6d2010ae | 2562 | continue; |
0a7de745 A |
2563 | } |
2564 | if (nfs_sockaddr_cmp((struct sockaddr*)&ncbsp->ncbs_saddr, nmp->nm_saddr) == 0) { | |
b0d623f7 | 2565 | break; |
0a7de745 | 2566 | } |
b0d623f7 | 2567 | } |
0a7de745 | 2568 | if (!nmp) { /* we don't want this socket, mark it dead */ |
b0d623f7 | 2569 | ncbsp->ncbs_flags |= NCBSOCK_DEAD; |
0a7de745 | 2570 | } |
b0d623f7 A |
2571 | |
2572 | /* make sure the callback socket cleanup timer is running */ | |
2573 | /* (shorten the timer if we've got a socket we don't want) */ | |
2574 | if (!nfs4_callback_timer_on) { | |
2575 | nfs4_callback_timer_on = 1; | |
2576 | nfs_interval_timer_start(nfs4_callback_timer_call, | |
0a7de745 | 2577 | !nmp ? 500 : (NFS4_CB_TIMER_PERIOD * 1000)); |
b0d623f7 A |
2578 | } else if (!nmp && (nfs4_callback_timer_on < 2)) { |
2579 | nfs4_callback_timer_on = 2; | |
2580 | thread_call_cancel(nfs4_callback_timer_call); | |
2581 | nfs_interval_timer_start(nfs4_callback_timer_call, 500); | |
2582 | } | |
2583 | ||
2584 | lck_mtx_unlock(nfs_global_mutex); | |
2585 | } | |
2586 | ||
2587 | /* | |
2588 | * Receive mbufs from callback sockets into RPC records and process each record. | |
2589 | * Detect connection has been closed and shut down. | |
2590 | */ | |
2591 | void | |
2592 | nfs4_cb_rcv(socket_t so, void *arg, __unused int waitflag) | |
2593 | { | |
2594 | struct nfs_callback_socket *ncbsp = arg; | |
0a7de745 | 2595 | struct timespec ts = {1, 0}; |
b0d623f7 A |
2596 | struct timeval now; |
2597 | mbuf_t m; | |
2598 | int error = 0, recv = 1; | |
2599 | ||
2600 | lck_mtx_lock(nfs_global_mutex); | |
2601 | while (ncbsp->ncbs_flags & NCBSOCK_UPCALL) { | |
2602 | /* wait if upcall is already in progress */ | |
2603 | ncbsp->ncbs_flags |= NCBSOCK_UPCALLWANT; | |
2604 | msleep(ncbsp, nfs_global_mutex, PSOCK, "cbupcall", &ts); | |
2605 | } | |
2606 | ncbsp->ncbs_flags |= NCBSOCK_UPCALL; | |
2607 | lck_mtx_unlock(nfs_global_mutex); | |
2608 | ||
2609 | /* loop while we make error-free progress */ | |
2610 | while (!error && recv) { | |
6d2010ae | 2611 | error = nfs_rpc_record_read(so, &ncbsp->ncbs_rrs, MSG_DONTWAIT, &recv, &m); |
0a7de745 | 2612 | if (m) { /* handle the request */ |
b0d623f7 | 2613 | error = nfs4_cb_handler(ncbsp, m); |
0a7de745 | 2614 | } |
b0d623f7 A |
2615 | } |
2616 | ||
2617 | /* note: no error and no data indicates server closed its end */ | |
2618 | if ((error != EWOULDBLOCK) && (error || !recv)) { | |
2619 | /* | |
2620 | * Socket is either being closed or should be. | |
2621 | * We can't close the socket in the context of the upcall. | |
2622 | * So we mark it as dead and leave it for the cleanup timer to reap. | |
2623 | */ | |
2624 | ncbsp->ncbs_stamp = 0; | |
2625 | ncbsp->ncbs_flags |= NCBSOCK_DEAD; | |
2626 | } else { | |
2627 | microuptime(&now); | |
2628 | ncbsp->ncbs_stamp = now.tv_sec; | |
2629 | } | |
2630 | ||
2631 | lck_mtx_lock(nfs_global_mutex); | |
2632 | ncbsp->ncbs_flags &= ~NCBSOCK_UPCALL; | |
2633 | lck_mtx_unlock(nfs_global_mutex); | |
2634 | wakeup(ncbsp); | |
2635 | } | |
2636 | ||
2637 | /* | |
2638 | * Handle an NFS callback channel request. | |
2639 | */ | |
2640 | int | |
2641 | nfs4_cb_handler(struct nfs_callback_socket *ncbsp, mbuf_t mreq) | |
2642 | { | |
2643 | socket_t so = ncbsp->ncbs_so; | |
2644 | struct nfsm_chain nmreq, nmrep; | |
2645 | mbuf_t mhead = NULL, mrest = NULL, m; | |
b0d623f7 A |
2646 | struct msghdr msg; |
2647 | struct nfsmount *nmp; | |
2648 | fhandle_t fh; | |
2649 | nfsnode_t np; | |
2650 | nfs_stateid stateid; | |
2651 | uint32_t bitmap[NFS_ATTR_BITMAP_LEN], rbitmap[NFS_ATTR_BITMAP_LEN], bmlen, truncate, attrbytes; | |
2652 | uint32_t val, xid, procnum, taglen, cbid, numops, op, status; | |
2653 | uint32_t auth_type, auth_len; | |
2654 | uint32_t numres, *pnumres; | |
2655 | int error = 0, replen, len; | |
2656 | size_t sentlen = 0; | |
2657 | ||
2658 | xid = numops = op = status = procnum = taglen = cbid = 0; | |
2659 | ||
2660 | nfsm_chain_dissect_init(error, &nmreq, mreq); | |
0a7de745 A |
2661 | nfsm_chain_get_32(error, &nmreq, xid); // RPC XID |
2662 | nfsm_chain_get_32(error, &nmreq, val); // RPC Call | |
b0d623f7 | 2663 | nfsm_assert(error, (val == RPC_CALL), EBADRPC); |
0a7de745 | 2664 | nfsm_chain_get_32(error, &nmreq, val); // RPC Version |
b0d623f7 | 2665 | nfsm_assert(error, (val == RPC_VER2), ERPCMISMATCH); |
0a7de745 | 2666 | nfsm_chain_get_32(error, &nmreq, val); // RPC Program Number |
b0d623f7 | 2667 | nfsm_assert(error, (val == NFS4_CALLBACK_PROG), EPROGUNAVAIL); |
0a7de745 | 2668 | nfsm_chain_get_32(error, &nmreq, val); // NFS Callback Program Version Number |
b0d623f7 | 2669 | nfsm_assert(error, (val == NFS4_CALLBACK_PROG_VERSION), EPROGMISMATCH); |
0a7de745 | 2670 | nfsm_chain_get_32(error, &nmreq, procnum); // NFS Callback Procedure Number |
b0d623f7 A |
2671 | nfsm_assert(error, (procnum <= NFSPROC4_CB_COMPOUND), EPROCUNAVAIL); |
2672 | ||
2673 | /* Handle authentication */ | |
2674 | /* XXX just ignore auth for now - handling kerberos may be tricky */ | |
0a7de745 A |
2675 | nfsm_chain_get_32(error, &nmreq, auth_type); // RPC Auth Flavor |
2676 | nfsm_chain_get_32(error, &nmreq, auth_len); // RPC Auth Length | |
b0d623f7 | 2677 | nfsm_assert(error, (auth_len <= RPCAUTH_MAXSIZ), EBADRPC); |
0a7de745 | 2678 | if (!error && (auth_len > 0)) { |
b0d623f7 | 2679 | nfsm_chain_adv(error, &nmreq, nfsm_rndup(auth_len)); |
0a7de745 A |
2680 | } |
2681 | nfsm_chain_adv(error, &nmreq, NFSX_UNSIGNED); // verifier flavor (should be AUTH_NONE) | |
2682 | nfsm_chain_get_32(error, &nmreq, auth_len); // verifier length | |
b0d623f7 | 2683 | nfsm_assert(error, (auth_len <= RPCAUTH_MAXSIZ), EBADRPC); |
0a7de745 | 2684 | if (!error && (auth_len > 0)) { |
b0d623f7 | 2685 | nfsm_chain_adv(error, &nmreq, nfsm_rndup(auth_len)); |
0a7de745 | 2686 | } |
b0d623f7 A |
2687 | if (error) { |
2688 | status = error; | |
2689 | error = 0; | |
2690 | goto nfsmout; | |
2691 | } | |
2692 | ||
2693 | switch (procnum) { | |
2694 | case NFSPROC4_CB_NULL: | |
2695 | status = NFSERR_RETVOID; | |
2696 | break; | |
2697 | case NFSPROC4_CB_COMPOUND: | |
2698 | /* tag, minorversion, cb ident, numops, op array */ | |
0a7de745 | 2699 | nfsm_chain_get_32(error, &nmreq, taglen); /* tag length */ |
b0d623f7 A |
2700 | nfsm_assert(error, (val <= NFS4_OPAQUE_LIMIT), EBADRPC); |
2701 | ||
2702 | /* start building the body of the response */ | |
0a7de745 | 2703 | nfsm_mbuf_get(error, &mrest, nfsm_rndup(taglen) + 5 * NFSX_UNSIGNED); |
b0d623f7 A |
2704 | nfsm_chain_init(&nmrep, mrest); |
2705 | ||
2706 | /* copy tag from request to response */ | |
0a7de745 | 2707 | nfsm_chain_add_32(error, &nmrep, taglen); /* tag length */ |
b0d623f7 A |
2708 | for (len = (int)taglen; !error && (len > 0); len -= NFSX_UNSIGNED) { |
2709 | nfsm_chain_get_32(error, &nmreq, val); | |
2710 | nfsm_chain_add_32(error, &nmrep, val); | |
2711 | } | |
2712 | ||
2713 | /* insert number of results placeholder */ | |
2714 | numres = 0; | |
2715 | nfsm_chain_add_32(error, &nmrep, numres); | |
2716 | pnumres = (uint32_t*)(nmrep.nmc_ptr - NFSX_UNSIGNED); | |
2717 | ||
0a7de745 | 2718 | nfsm_chain_get_32(error, &nmreq, val); /* minorversion */ |
b0d623f7 | 2719 | nfsm_assert(error, (val == 0), NFSERR_MINOR_VERS_MISMATCH); |
0a7de745 A |
2720 | nfsm_chain_get_32(error, &nmreq, cbid); /* callback ID */ |
2721 | nfsm_chain_get_32(error, &nmreq, numops); /* number of operations */ | |
b0d623f7 | 2722 | if (error) { |
0a7de745 | 2723 | if ((error == EBADRPC) || (error == NFSERR_MINOR_VERS_MISMATCH)) { |
b0d623f7 | 2724 | status = error; |
0a7de745 | 2725 | } else if ((error == ENOBUFS) || (error == ENOMEM)) { |
b0d623f7 | 2726 | status = NFSERR_RESOURCE; |
0a7de745 | 2727 | } else { |
b0d623f7 | 2728 | status = NFSERR_SERVERFAULT; |
0a7de745 | 2729 | } |
b0d623f7 A |
2730 | error = 0; |
2731 | nfsm_chain_null(&nmrep); | |
2732 | goto nfsmout; | |
2733 | } | |
2734 | /* match the callback ID to a registered mount */ | |
2735 | lck_mtx_lock(nfs_global_mutex); | |
2736 | TAILQ_FOREACH(nmp, &nfs4_cb_mounts, nm_cblink) { | |
0a7de745 | 2737 | if (nmp->nm_cbid != cbid) { |
b0d623f7 | 2738 | continue; |
0a7de745 | 2739 | } |
b0d623f7 | 2740 | /* verify socket's source address matches this mount's server address */ |
0a7de745 | 2741 | if (!nmp->nm_saddr) { |
b0d623f7 | 2742 | continue; |
0a7de745 A |
2743 | } |
2744 | if (nfs_sockaddr_cmp((struct sockaddr*)&ncbsp->ncbs_saddr, nmp->nm_saddr) == 0) { | |
6d2010ae | 2745 | break; |
0a7de745 | 2746 | } |
b0d623f7 A |
2747 | } |
2748 | /* mark the NFS mount as busy */ | |
0a7de745 | 2749 | if (nmp) { |
b0d623f7 | 2750 | nmp->nm_cbrefs++; |
0a7de745 | 2751 | } |
b0d623f7 A |
2752 | lck_mtx_unlock(nfs_global_mutex); |
2753 | if (!nmp) { | |
2754 | /* if no mount match, just drop socket. */ | |
2755 | error = EPERM; | |
2756 | nfsm_chain_null(&nmrep); | |
2757 | goto out; | |
2758 | } | |
2759 | ||
2760 | /* process ops, adding results to mrest */ | |
2761 | while (numops > 0) { | |
2762 | numops--; | |
2763 | nfsm_chain_get_32(error, &nmreq, op); | |
0a7de745 | 2764 | if (error) { |
b0d623f7 | 2765 | break; |
0a7de745 | 2766 | } |
b0d623f7 A |
2767 | switch (op) { |
2768 | case NFS_OP_CB_GETATTR: | |
2769 | // (FH, BITMAP) -> (STATUS, BITMAP, ATTRS) | |
2770 | np = NULL; | |
2771 | nfsm_chain_get_fh(error, &nmreq, NFS_VER4, &fh); | |
2772 | bmlen = NFS_ATTR_BITMAP_LEN; | |
2773 | nfsm_chain_get_bitmap(error, &nmreq, bitmap, bmlen); | |
2774 | if (error) { | |
2775 | status = error; | |
2776 | error = 0; | |
2777 | numops = 0; /* don't process any more ops */ | |
2778 | } else { | |
2779 | /* find the node for the file handle */ | |
6d2010ae | 2780 | error = nfs_nget(nmp->nm_mountp, NULL, NULL, fh.fh_data, fh.fh_len, NULL, NULL, RPCAUTH_UNKNOWN, NG_NOCREATE, &np); |
b0d623f7 A |
2781 | if (error || !np) { |
2782 | status = NFSERR_BADHANDLE; | |
2783 | error = 0; | |
2784 | np = NULL; | |
2785 | numops = 0; /* don't process any more ops */ | |
2786 | } | |
2787 | } | |
2788 | nfsm_chain_add_32(error, &nmrep, op); | |
2789 | nfsm_chain_add_32(error, &nmrep, status); | |
0a7de745 | 2790 | if (!error && (status == EBADRPC)) { |
b0d623f7 | 2791 | error = status; |
0a7de745 | 2792 | } |
b0d623f7 A |
2793 | if (np) { |
2794 | /* only allow returning size, change, and mtime attrs */ | |
2795 | NFS_CLEAR_ATTRIBUTES(&rbitmap); | |
2796 | attrbytes = 0; | |
2797 | if (NFS_BITMAP_ISSET(&bitmap, NFS_FATTR_CHANGE)) { | |
2798 | NFS_BITMAP_SET(&rbitmap, NFS_FATTR_CHANGE); | |
2799 | attrbytes += 2 * NFSX_UNSIGNED; | |
2800 | } | |
2801 | if (NFS_BITMAP_ISSET(&bitmap, NFS_FATTR_SIZE)) { | |
2802 | NFS_BITMAP_SET(&rbitmap, NFS_FATTR_SIZE); | |
2803 | attrbytes += 2 * NFSX_UNSIGNED; | |
2804 | } | |
2805 | if (NFS_BITMAP_ISSET(&bitmap, NFS_FATTR_TIME_MODIFY)) { | |
2806 | NFS_BITMAP_SET(&rbitmap, NFS_FATTR_TIME_MODIFY); | |
2807 | attrbytes += 3 * NFSX_UNSIGNED; | |
2808 | } | |
2809 | nfsm_chain_add_bitmap(error, &nmrep, rbitmap, NFS_ATTR_BITMAP_LEN); | |
2810 | nfsm_chain_add_32(error, &nmrep, attrbytes); | |
0a7de745 | 2811 | if (NFS_BITMAP_ISSET(&bitmap, NFS_FATTR_CHANGE)) { |
b0d623f7 | 2812 | nfsm_chain_add_64(error, &nmrep, |
0a7de745 A |
2813 | np->n_vattr.nva_change + ((np->n_flag & NMODIFIED) ? 1 : 0)); |
2814 | } | |
2815 | if (NFS_BITMAP_ISSET(&bitmap, NFS_FATTR_SIZE)) { | |
b0d623f7 | 2816 | nfsm_chain_add_64(error, &nmrep, np->n_size); |
0a7de745 | 2817 | } |
b0d623f7 A |
2818 | if (NFS_BITMAP_ISSET(&bitmap, NFS_FATTR_TIME_MODIFY)) { |
2819 | nfsm_chain_add_64(error, &nmrep, np->n_vattr.nva_timesec[NFSTIME_MODIFY]); | |
2820 | nfsm_chain_add_32(error, &nmrep, np->n_vattr.nva_timensec[NFSTIME_MODIFY]); | |
2821 | } | |
2822 | nfs_node_unlock(np); | |
2823 | vnode_put(NFSTOV(np)); | |
2824 | np = NULL; | |
2825 | } | |
2826 | /* | |
2827 | * If we hit an error building the reply, we can't easily back up. | |
2828 | * So we'll just update the status and hope the server ignores the | |
2829 | * extra garbage. | |
2830 | */ | |
2831 | break; | |
2832 | case NFS_OP_CB_RECALL: | |
2833 | // (STATEID, TRUNCATE, FH) -> (STATUS) | |
2834 | np = NULL; | |
2835 | nfsm_chain_get_stateid(error, &nmreq, &stateid); | |
2836 | nfsm_chain_get_32(error, &nmreq, truncate); | |
2837 | nfsm_chain_get_fh(error, &nmreq, NFS_VER4, &fh); | |
2838 | if (error) { | |
2839 | status = error; | |
2840 | error = 0; | |
2841 | numops = 0; /* don't process any more ops */ | |
2842 | } else { | |
2843 | /* find the node for the file handle */ | |
6d2010ae | 2844 | error = nfs_nget(nmp->nm_mountp, NULL, NULL, fh.fh_data, fh.fh_len, NULL, NULL, RPCAUTH_UNKNOWN, NG_NOCREATE, &np); |
b0d623f7 A |
2845 | if (error || !np) { |
2846 | status = NFSERR_BADHANDLE; | |
2847 | error = 0; | |
2848 | np = NULL; | |
2849 | numops = 0; /* don't process any more ops */ | |
2850 | } else if (!(np->n_openflags & N_DELEG_MASK) || | |
0a7de745 | 2851 | bcmp(&np->n_dstateid, &stateid, sizeof(stateid))) { |
b0d623f7 A |
2852 | /* delegation stateid state doesn't match */ |
2853 | status = NFSERR_BAD_STATEID; | |
2854 | numops = 0; /* don't process any more ops */ | |
2855 | } | |
0a7de745 | 2856 | if (!status) { /* add node to recall queue, and wake socket thread */ |
6d2010ae | 2857 | nfs4_delegation_return_enqueue(np); |
0a7de745 | 2858 | } |
b0d623f7 A |
2859 | if (np) { |
2860 | nfs_node_unlock(np); | |
2861 | vnode_put(NFSTOV(np)); | |
2862 | } | |
2863 | } | |
2864 | nfsm_chain_add_32(error, &nmrep, op); | |
2865 | nfsm_chain_add_32(error, &nmrep, status); | |
0a7de745 | 2866 | if (!error && (status == EBADRPC)) { |
b0d623f7 | 2867 | error = status; |
0a7de745 | 2868 | } |
b0d623f7 A |
2869 | break; |
2870 | case NFS_OP_CB_ILLEGAL: | |
2871 | default: | |
2872 | nfsm_chain_add_32(error, &nmrep, NFS_OP_CB_ILLEGAL); | |
2873 | status = NFSERR_OP_ILLEGAL; | |
2874 | nfsm_chain_add_32(error, &nmrep, status); | |
2875 | numops = 0; /* don't process any more ops */ | |
2876 | break; | |
2877 | } | |
2878 | numres++; | |
2879 | } | |
2880 | ||
2881 | if (!status && error) { | |
0a7de745 | 2882 | if (error == EBADRPC) { |
b0d623f7 | 2883 | status = error; |
0a7de745 | 2884 | } else if ((error == ENOBUFS) || (error == ENOMEM)) { |
b0d623f7 | 2885 | status = NFSERR_RESOURCE; |
0a7de745 | 2886 | } else { |
b0d623f7 | 2887 | status = NFSERR_SERVERFAULT; |
0a7de745 | 2888 | } |
b0d623f7 A |
2889 | error = 0; |
2890 | } | |
2891 | ||
2892 | /* Now, set the numres field */ | |
2893 | *pnumres = txdr_unsigned(numres); | |
2894 | nfsm_chain_build_done(error, &nmrep); | |
2895 | nfsm_chain_null(&nmrep); | |
2896 | ||
2897 | /* drop the callback reference on the mount */ | |
2898 | lck_mtx_lock(nfs_global_mutex); | |
2899 | nmp->nm_cbrefs--; | |
0a7de745 | 2900 | if (!nmp->nm_cbid) { |
b0d623f7 | 2901 | wakeup(&nmp->nm_cbrefs); |
0a7de745 | 2902 | } |
b0d623f7 A |
2903 | lck_mtx_unlock(nfs_global_mutex); |
2904 | break; | |
2905 | } | |
2906 | ||
2907 | nfsmout: | |
0a7de745 | 2908 | if (status == EBADRPC) { |
316670eb | 2909 | OSAddAtomic64(1, &nfsstats.rpcinvalid); |
0a7de745 | 2910 | } |
b0d623f7 A |
2911 | |
2912 | /* build reply header */ | |
2913 | error = mbuf_gethdr(MBUF_WAITOK, MBUF_TYPE_DATA, &mhead); | |
2914 | nfsm_chain_init(&nmrep, mhead); | |
2915 | nfsm_chain_add_32(error, &nmrep, 0); /* insert space for an RPC record mark */ | |
2916 | nfsm_chain_add_32(error, &nmrep, xid); | |
2917 | nfsm_chain_add_32(error, &nmrep, RPC_REPLY); | |
2918 | if ((status == ERPCMISMATCH) || (status & NFSERR_AUTHERR)) { | |
2919 | nfsm_chain_add_32(error, &nmrep, RPC_MSGDENIED); | |
2920 | if (status & NFSERR_AUTHERR) { | |
2921 | nfsm_chain_add_32(error, &nmrep, RPC_AUTHERR); | |
2922 | nfsm_chain_add_32(error, &nmrep, (status & ~NFSERR_AUTHERR)); | |
2923 | } else { | |
2924 | nfsm_chain_add_32(error, &nmrep, RPC_MISMATCH); | |
2925 | nfsm_chain_add_32(error, &nmrep, RPC_VER2); | |
2926 | nfsm_chain_add_32(error, &nmrep, RPC_VER2); | |
2927 | } | |
2928 | } else { | |
2929 | /* reply status */ | |
2930 | nfsm_chain_add_32(error, &nmrep, RPC_MSGACCEPTED); | |
2931 | /* XXX RPCAUTH_NULL verifier */ | |
2932 | nfsm_chain_add_32(error, &nmrep, RPCAUTH_NULL); | |
2933 | nfsm_chain_add_32(error, &nmrep, 0); | |
2934 | /* accepted status */ | |
2935 | switch (status) { | |
2936 | case EPROGUNAVAIL: | |
2937 | nfsm_chain_add_32(error, &nmrep, RPC_PROGUNAVAIL); | |
2938 | break; | |
2939 | case EPROGMISMATCH: | |
2940 | nfsm_chain_add_32(error, &nmrep, RPC_PROGMISMATCH); | |
2941 | nfsm_chain_add_32(error, &nmrep, NFS4_CALLBACK_PROG_VERSION); | |
2942 | nfsm_chain_add_32(error, &nmrep, NFS4_CALLBACK_PROG_VERSION); | |
2943 | break; | |
2944 | case EPROCUNAVAIL: | |
2945 | nfsm_chain_add_32(error, &nmrep, RPC_PROCUNAVAIL); | |
2946 | break; | |
2947 | case EBADRPC: | |
2948 | nfsm_chain_add_32(error, &nmrep, RPC_GARBAGE); | |
2949 | break; | |
2950 | default: | |
2951 | nfsm_chain_add_32(error, &nmrep, RPC_SUCCESS); | |
0a7de745 | 2952 | if (status != NFSERR_RETVOID) { |
b0d623f7 | 2953 | nfsm_chain_add_32(error, &nmrep, status); |
0a7de745 | 2954 | } |
b0d623f7 A |
2955 | break; |
2956 | } | |
2957 | } | |
2958 | nfsm_chain_build_done(error, &nmrep); | |
2959 | if (error) { | |
2960 | nfsm_chain_null(&nmrep); | |
2961 | goto out; | |
2962 | } | |
2963 | error = mbuf_setnext(nmrep.nmc_mcur, mrest); | |
2964 | if (error) { | |
2965 | printf("nfs cb: mbuf_setnext failed %d\n", error); | |
2966 | goto out; | |
2967 | } | |
2968 | mrest = NULL; | |
2969 | /* Calculate the size of the reply */ | |
2970 | replen = 0; | |
0a7de745 | 2971 | for (m = nmrep.nmc_mhead; m; m = mbuf_next(m)) { |
b0d623f7 | 2972 | replen += mbuf_len(m); |
0a7de745 | 2973 | } |
b0d623f7 A |
2974 | mbuf_pkthdr_setlen(mhead, replen); |
2975 | error = mbuf_pkthdr_setrcvif(mhead, NULL); | |
2976 | nfsm_chain_set_recmark(error, &nmrep, (replen - NFSX_UNSIGNED) | 0x80000000); | |
2977 | nfsm_chain_null(&nmrep); | |
2978 | ||
2979 | /* send the reply */ | |
2980 | bzero(&msg, sizeof(msg)); | |
2981 | error = sock_sendmbuf(so, &msg, mhead, 0, &sentlen); | |
2982 | mhead = NULL; | |
0a7de745 | 2983 | if (!error && ((int)sentlen != replen)) { |
b0d623f7 | 2984 | error = EWOULDBLOCK; |
0a7de745 A |
2985 | } |
2986 | if (error == EWOULDBLOCK) { /* inability to send response is considered fatal */ | |
b0d623f7 | 2987 | error = ETIMEDOUT; |
0a7de745 | 2988 | } |
b0d623f7 | 2989 | out: |
0a7de745 | 2990 | if (error) { |
b0d623f7 | 2991 | nfsm_chain_cleanup(&nmrep); |
0a7de745 A |
2992 | } |
2993 | if (mhead) { | |
b0d623f7 | 2994 | mbuf_freem(mhead); |
0a7de745 A |
2995 | } |
2996 | if (mrest) { | |
b0d623f7 | 2997 | mbuf_freem(mrest); |
0a7de745 A |
2998 | } |
2999 | if (mreq) { | |
b0d623f7 | 3000 | mbuf_freem(mreq); |
0a7de745 A |
3001 | } |
3002 | return error; | |
b0d623f7 A |
3003 | } |
3004 | ||
3005 | ||
6d2010ae A |
3006 | /* |
3007 | * Initialize an nfs_rpc_record_state structure. | |
3008 | */ | |
3009 | void | |
3010 | nfs_rpc_record_state_init(struct nfs_rpc_record_state *nrrsp) | |
3011 | { | |
3012 | bzero(nrrsp, sizeof(*nrrsp)); | |
3013 | nrrsp->nrrs_markerleft = sizeof(nrrsp->nrrs_fragleft); | |
3014 | } | |
3015 | ||
3016 | /* | |
3017 | * Clean up an nfs_rpc_record_state structure. | |
3018 | */ | |
3019 | void | |
3020 | nfs_rpc_record_state_cleanup(struct nfs_rpc_record_state *nrrsp) | |
3021 | { | |
3022 | if (nrrsp->nrrs_m) { | |
3023 | mbuf_freem(nrrsp->nrrs_m); | |
3024 | nrrsp->nrrs_m = nrrsp->nrrs_mlast = NULL; | |
3025 | } | |
3026 | } | |
3027 | ||
b0d623f7 A |
3028 | /* |
3029 | * Read the next (marked) RPC record from the socket. | |
3030 | * | |
3031 | * *recvp returns if any data was received. | |
3032 | * *mp returns the next complete RPC record | |
3033 | */ | |
3034 | int | |
6d2010ae | 3035 | nfs_rpc_record_read(socket_t so, struct nfs_rpc_record_state *nrrsp, int flags, int *recvp, mbuf_t *mp) |
b0d623f7 A |
3036 | { |
3037 | struct iovec aio; | |
3038 | struct msghdr msg; | |
3039 | size_t rcvlen; | |
3040 | int error = 0; | |
3041 | mbuf_t m; | |
3042 | ||
3043 | *recvp = 0; | |
3044 | *mp = NULL; | |
3045 | ||
3046 | /* read the TCP RPC record marker */ | |
3047 | while (!error && nrrsp->nrrs_markerleft) { | |
3048 | aio.iov_base = ((char*)&nrrsp->nrrs_fragleft + | |
0a7de745 | 3049 | sizeof(nrrsp->nrrs_fragleft) - nrrsp->nrrs_markerleft); |
b0d623f7 A |
3050 | aio.iov_len = nrrsp->nrrs_markerleft; |
3051 | bzero(&msg, sizeof(msg)); | |
3052 | msg.msg_iov = &aio; | |
3053 | msg.msg_iovlen = 1; | |
6d2010ae | 3054 | error = sock_receive(so, &msg, flags, &rcvlen); |
0a7de745 | 3055 | if (error || !rcvlen) { |
b0d623f7 | 3056 | break; |
0a7de745 | 3057 | } |
b0d623f7 A |
3058 | *recvp = 1; |
3059 | nrrsp->nrrs_markerleft -= rcvlen; | |
0a7de745 | 3060 | if (nrrsp->nrrs_markerleft) { |
b0d623f7 | 3061 | continue; |
0a7de745 | 3062 | } |
b0d623f7 A |
3063 | /* record marker complete */ |
3064 | nrrsp->nrrs_fragleft = ntohl(nrrsp->nrrs_fragleft); | |
3065 | if (nrrsp->nrrs_fragleft & 0x80000000) { | |
3066 | nrrsp->nrrs_lastfrag = 1; | |
3067 | nrrsp->nrrs_fragleft &= ~0x80000000; | |
3068 | } | |
3069 | nrrsp->nrrs_reclen += nrrsp->nrrs_fragleft; | |
3070 | if (nrrsp->nrrs_reclen > NFS_MAXPACKET) { | |
6d2010ae | 3071 | /* This is SERIOUS! We are out of sync with the sender. */ |
b0d623f7 A |
3072 | log(LOG_ERR, "impossible RPC record length (%d) on callback", nrrsp->nrrs_reclen); |
3073 | error = EFBIG; | |
3074 | } | |
3075 | } | |
3076 | ||
3077 | /* read the TCP RPC record fragment */ | |
3078 | while (!error && !nrrsp->nrrs_markerleft && nrrsp->nrrs_fragleft) { | |
3079 | m = NULL; | |
3080 | rcvlen = nrrsp->nrrs_fragleft; | |
6d2010ae | 3081 | error = sock_receivembuf(so, NULL, &m, flags, &rcvlen); |
0a7de745 | 3082 | if (error || !rcvlen || !m) { |
b0d623f7 | 3083 | break; |
0a7de745 | 3084 | } |
b0d623f7 A |
3085 | *recvp = 1; |
3086 | /* append mbufs to list */ | |
3087 | nrrsp->nrrs_fragleft -= rcvlen; | |
3088 | if (!nrrsp->nrrs_m) { | |
3089 | nrrsp->nrrs_m = m; | |
3090 | } else { | |
3091 | error = mbuf_setnext(nrrsp->nrrs_mlast, m); | |
3092 | if (error) { | |
3093 | printf("nfs tcp rcv: mbuf_setnext failed %d\n", error); | |
3094 | mbuf_freem(m); | |
3095 | break; | |
3096 | } | |
3097 | } | |
0a7de745 | 3098 | while (mbuf_next(m)) { |
b0d623f7 | 3099 | m = mbuf_next(m); |
0a7de745 | 3100 | } |
b0d623f7 A |
3101 | nrrsp->nrrs_mlast = m; |
3102 | } | |
3103 | ||
3104 | /* done reading fragment? */ | |
3105 | if (!error && !nrrsp->nrrs_markerleft && !nrrsp->nrrs_fragleft) { | |
3106 | /* reset socket fragment parsing state */ | |
3107 | nrrsp->nrrs_markerleft = sizeof(nrrsp->nrrs_fragleft); | |
3108 | if (nrrsp->nrrs_lastfrag) { | |
3109 | /* RPC record complete */ | |
3110 | *mp = nrrsp->nrrs_m; | |
3111 | /* reset socket record parsing state */ | |
3112 | nrrsp->nrrs_reclen = 0; | |
3113 | nrrsp->nrrs_m = nrrsp->nrrs_mlast = NULL; | |
3114 | nrrsp->nrrs_lastfrag = 0; | |
3115 | } | |
3116 | } | |
3117 | ||
0a7de745 | 3118 | return error; |
b0d623f7 A |
3119 | } |
3120 | ||
3121 | ||
3122 | ||
1c79356b | 3123 | /* |
2d21ac55 A |
3124 | * The NFS client send routine. |
3125 | * | |
3126 | * Send the given NFS request out the mount's socket. | |
3127 | * Holds nfs_sndlock() for the duration of this call. | |
3128 | * | |
3129 | * - check for request termination (sigintr) | |
b0d623f7 | 3130 | * - wait for reconnect, if necessary |
2d21ac55 A |
3131 | * - UDP: check the congestion window |
3132 | * - make a copy of the request to send | |
3133 | * - UDP: update the congestion window | |
3134 | * - send the request | |
3135 | * | |
3136 | * If sent successfully, R_MUSTRESEND and R_RESENDERR are cleared. | |
3137 | * rexmit count is also updated if this isn't the first send. | |
3138 | * | |
3139 | * If the send is not successful, make sure R_MUSTRESEND is set. | |
3140 | * If this wasn't the first transmit, set R_RESENDERR. | |
3141 | * Also, undo any UDP congestion window changes made. | |
3142 | * | |
3143 | * If the error appears to indicate that the socket should | |
3144 | * be reconnected, mark the socket for reconnection. | |
3145 | * | |
3146 | * Only return errors when the request should be aborted. | |
1c79356b | 3147 | */ |
1c79356b | 3148 | int |
2d21ac55 | 3149 | nfs_send(struct nfsreq *req, int wait) |
1c79356b | 3150 | { |
2d21ac55 | 3151 | struct nfsmount *nmp; |
6d2010ae | 3152 | struct nfs_socket *nso; |
36401178 | 3153 | int error, error2, sotype, rexmit, slpflag = 0, needrecon; |
2d21ac55 A |
3154 | struct msghdr msg; |
3155 | struct sockaddr *sendnam; | |
3156 | mbuf_t mreqcopy; | |
3157 | size_t sentlen = 0; | |
3158 | struct timespec ts = { 2, 0 }; | |
1c79356b | 3159 | |
2d21ac55 A |
3160 | again: |
3161 | error = nfs_sndlock(req); | |
b0d623f7 A |
3162 | if (error) { |
3163 | lck_mtx_lock(&req->r_mtx); | |
3164 | req->r_error = error; | |
3165 | req->r_flags &= ~R_SENDING; | |
3166 | lck_mtx_unlock(&req->r_mtx); | |
0a7de745 | 3167 | return error; |
b0d623f7 | 3168 | } |
2d21ac55 | 3169 | |
6d2010ae | 3170 | error = nfs_sigintr(req->r_nmp, req, NULL, 0); |
2d21ac55 A |
3171 | if (error) { |
3172 | nfs_sndunlock(req); | |
b0d623f7 A |
3173 | lck_mtx_lock(&req->r_mtx); |
3174 | req->r_error = error; | |
3175 | req->r_flags &= ~R_SENDING; | |
3176 | lck_mtx_unlock(&req->r_mtx); | |
0a7de745 | 3177 | return error; |
2d21ac55 A |
3178 | } |
3179 | nmp = req->r_nmp; | |
3180 | sotype = nmp->nm_sotype; | |
3181 | ||
b0d623f7 A |
3182 | /* |
3183 | * If it's a setup RPC but we're not in SETUP... must need reconnect. | |
3184 | * If it's a recovery RPC but the socket's not ready... must need reconnect. | |
3185 | */ | |
3186 | if (((req->r_flags & R_SETUP) && !(nmp->nm_sockflags & NMSOCK_SETUP)) || | |
3187 | ((req->r_flags & R_RECOVER) && !(nmp->nm_sockflags & NMSOCK_READY))) { | |
3188 | error = ETIMEDOUT; | |
2d21ac55 | 3189 | nfs_sndunlock(req); |
b0d623f7 A |
3190 | lck_mtx_lock(&req->r_mtx); |
3191 | req->r_error = error; | |
3192 | req->r_flags &= ~R_SENDING; | |
3193 | lck_mtx_unlock(&req->r_mtx); | |
0a7de745 | 3194 | return error; |
2d21ac55 A |
3195 | } |
3196 | ||
3197 | /* If the socket needs reconnection, do that now. */ | |
3198 | /* wait until socket is ready - unless this request is part of setup */ | |
3199 | lck_mtx_lock(&nmp->nm_lock); | |
3200 | if (!(nmp->nm_sockflags & NMSOCK_READY) && | |
3201 | !((nmp->nm_sockflags & NMSOCK_SETUP) && (req->r_flags & R_SETUP))) { | |
0a7de745 | 3202 | if (NMFLAG(nmp, INTR) && !(req->r_flags & R_NOINTR)) { |
2d21ac55 | 3203 | slpflag |= PCATCH; |
0a7de745 | 3204 | } |
2d21ac55 A |
3205 | lck_mtx_unlock(&nmp->nm_lock); |
3206 | nfs_sndunlock(req); | |
3207 | if (!wait) { | |
3208 | lck_mtx_lock(&req->r_mtx); | |
b0d623f7 | 3209 | req->r_flags &= ~R_SENDING; |
2d21ac55 A |
3210 | req->r_flags |= R_MUSTRESEND; |
3211 | req->r_rtt = 0; | |
3212 | lck_mtx_unlock(&req->r_mtx); | |
0a7de745 | 3213 | return 0; |
2d21ac55 | 3214 | } |
39236c6e | 3215 | NFS_SOCK_DBG("nfs_send: 0x%llx wait reconnect\n", req->r_xid); |
2d21ac55 A |
3216 | lck_mtx_lock(&req->r_mtx); |
3217 | req->r_flags &= ~R_MUSTRESEND; | |
3218 | req->r_rtt = 0; | |
3219 | lck_mtx_unlock(&req->r_mtx); | |
3220 | lck_mtx_lock(&nmp->nm_lock); | |
3221 | while (!(nmp->nm_sockflags & NMSOCK_READY)) { | |
3222 | /* don't bother waiting if the socket thread won't be reconnecting it */ | |
0a7de745 | 3223 | if (nmp->nm_state & (NFSSTA_FORCE | NFSSTA_DEAD)) { |
2d21ac55 A |
3224 | error = EIO; |
3225 | break; | |
3226 | } | |
fe8ab488 | 3227 | if ((NMFLAG(nmp, SOFT) || (req->r_flags & R_SOFT)) && (nmp->nm_reconnect_start > 0)) { |
b0d623f7 A |
3228 | struct timeval now; |
3229 | microuptime(&now); | |
3230 | if ((now.tv_sec - nmp->nm_reconnect_start) >= 8) { | |
3231 | /* soft mount in reconnect for a while... terminate ASAP */ | |
316670eb | 3232 | OSAddAtomic64(1, &nfsstats.rpctimeouts); |
b0d623f7 A |
3233 | req->r_flags |= R_SOFTTERM; |
3234 | req->r_error = error = ETIMEDOUT; | |
3235 | break; | |
3236 | } | |
3237 | } | |
2d21ac55 A |
3238 | /* make sure socket thread is running, then wait */ |
3239 | nfs_mount_sock_thread_wake(nmp); | |
0a7de745 | 3240 | if ((error = nfs_sigintr(req->r_nmp, req, req->r_thread, 1))) { |
2d21ac55 | 3241 | break; |
0a7de745 A |
3242 | } |
3243 | msleep(req, &nmp->nm_lock, slpflag | PSOCK, "nfsconnectwait", &ts); | |
36401178 | 3244 | slpflag = 0; |
2d21ac55 A |
3245 | } |
3246 | lck_mtx_unlock(&nmp->nm_lock); | |
b0d623f7 A |
3247 | if (error) { |
3248 | lck_mtx_lock(&req->r_mtx); | |
3249 | req->r_error = error; | |
3250 | req->r_flags &= ~R_SENDING; | |
3251 | lck_mtx_unlock(&req->r_mtx); | |
0a7de745 | 3252 | return error; |
b0d623f7 | 3253 | } |
2d21ac55 A |
3254 | goto again; |
3255 | } | |
6d2010ae A |
3256 | nso = nmp->nm_nso; |
3257 | /* note that we're using the mount's socket to do the send */ | |
3258 | nmp->nm_state |= NFSSTA_SENDING; /* will be cleared by nfs_sndunlock() */ | |
2d21ac55 | 3259 | lck_mtx_unlock(&nmp->nm_lock); |
6d2010ae | 3260 | if (!nso) { |
2d21ac55 A |
3261 | nfs_sndunlock(req); |
3262 | lck_mtx_lock(&req->r_mtx); | |
b0d623f7 | 3263 | req->r_flags &= ~R_SENDING; |
2d21ac55 A |
3264 | req->r_flags |= R_MUSTRESEND; |
3265 | req->r_rtt = 0; | |
3266 | lck_mtx_unlock(&req->r_mtx); | |
0a7de745 | 3267 | return 0; |
2d21ac55 A |
3268 | } |
3269 | ||
3270 | lck_mtx_lock(&req->r_mtx); | |
3271 | rexmit = (req->r_flags & R_SENT); | |
3272 | ||
3273 | if (sotype == SOCK_DGRAM) { | |
3274 | lck_mtx_lock(&nmp->nm_lock); | |
3275 | if (!(req->r_flags & R_CWND) && (nmp->nm_sent >= nmp->nm_cwnd)) { | |
3276 | /* if we can't send this out yet, wait on the cwnd queue */ | |
6d2010ae | 3277 | slpflag = (NMFLAG(nmp, INTR) && req->r_thread) ? PCATCH : 0; |
2d21ac55 A |
3278 | lck_mtx_unlock(&nmp->nm_lock); |
3279 | nfs_sndunlock(req); | |
b0d623f7 | 3280 | req->r_flags &= ~R_SENDING; |
2d21ac55 A |
3281 | req->r_flags |= R_MUSTRESEND; |
3282 | lck_mtx_unlock(&req->r_mtx); | |
3283 | if (!wait) { | |
3284 | req->r_rtt = 0; | |
0a7de745 | 3285 | return 0; |
2d21ac55 A |
3286 | } |
3287 | lck_mtx_lock(&nmp->nm_lock); | |
3288 | while (nmp->nm_sent >= nmp->nm_cwnd) { | |
0a7de745 | 3289 | if ((error = nfs_sigintr(req->r_nmp, req, req->r_thread, 1))) { |
2d21ac55 | 3290 | break; |
0a7de745 | 3291 | } |
2d21ac55 | 3292 | TAILQ_INSERT_TAIL(&nmp->nm_cwndq, req, r_cchain); |
36401178 A |
3293 | msleep(req, &nmp->nm_lock, slpflag | (PZERO - 1), "nfswaitcwnd", &ts); |
3294 | slpflag = 0; | |
2d21ac55 A |
3295 | if ((req->r_cchain.tqe_next != NFSREQNOLIST)) { |
3296 | TAILQ_REMOVE(&nmp->nm_cwndq, req, r_cchain); | |
3297 | req->r_cchain.tqe_next = NFSREQNOLIST; | |
3298 | } | |
2d21ac55 A |
3299 | } |
3300 | lck_mtx_unlock(&nmp->nm_lock); | |
2d21ac55 A |
3301 | goto again; |
3302 | } | |
1c79356b | 3303 | /* |
2d21ac55 A |
3304 | * We update these *before* the send to avoid racing |
3305 | * against others who may be looking to send requests. | |
1c79356b | 3306 | */ |
2d21ac55 A |
3307 | if (!rexmit) { |
3308 | /* first transmit */ | |
3309 | req->r_flags |= R_CWND; | |
3310 | nmp->nm_sent += NFS_CWNDSCALE; | |
3311 | } else { | |
3312 | /* | |
3313 | * When retransmitting, turn timing off | |
316670eb | 3314 | * and divide congestion window by 2. |
2d21ac55 A |
3315 | */ |
3316 | req->r_flags &= ~R_TIMING; | |
3317 | nmp->nm_cwnd >>= 1; | |
0a7de745 | 3318 | if (nmp->nm_cwnd < NFS_CWNDSCALE) { |
2d21ac55 | 3319 | nmp->nm_cwnd = NFS_CWNDSCALE; |
0a7de745 | 3320 | } |
1c79356b | 3321 | } |
2d21ac55 A |
3322 | lck_mtx_unlock(&nmp->nm_lock); |
3323 | } | |
3324 | ||
3325 | req->r_flags &= ~R_MUSTRESEND; | |
3326 | lck_mtx_unlock(&req->r_mtx); | |
3327 | ||
3328 | error = mbuf_copym(req->r_mhead, 0, MBUF_COPYALL, | |
0a7de745 | 3329 | wait ? MBUF_WAITOK : MBUF_DONTWAIT, &mreqcopy); |
2d21ac55 | 3330 | if (error) { |
0a7de745 | 3331 | if (wait) { |
2d21ac55 | 3332 | log(LOG_INFO, "nfs_send: mbuf copy failed %d\n", error); |
0a7de745 | 3333 | } |
2d21ac55 A |
3334 | nfs_sndunlock(req); |
3335 | lck_mtx_lock(&req->r_mtx); | |
b0d623f7 | 3336 | req->r_flags &= ~R_SENDING; |
2d21ac55 A |
3337 | req->r_flags |= R_MUSTRESEND; |
3338 | req->r_rtt = 0; | |
3339 | lck_mtx_unlock(&req->r_mtx); | |
0a7de745 | 3340 | return 0; |
2d21ac55 A |
3341 | } |
3342 | ||
3343 | bzero(&msg, sizeof(msg)); | |
6d2010ae A |
3344 | if ((sotype != SOCK_STREAM) && !sock_isconnected(nso->nso_so) && ((sendnam = nmp->nm_saddr))) { |
3345 | msg.msg_name = (caddr_t)sendnam; | |
3346 | msg.msg_namelen = sendnam->sa_len; | |
2d21ac55 | 3347 | } |
6d2010ae | 3348 | error = sock_sendmbuf(nso->nso_so, &msg, mreqcopy, 0, &sentlen); |
fe8ab488 | 3349 | if (error || (sentlen != req->r_mreqlen)) { |
39236c6e | 3350 | NFS_SOCK_DBG("nfs_send: 0x%llx sent %d/%d error %d\n", |
0a7de745 | 3351 | req->r_xid, (int)sentlen, (int)req->r_mreqlen, error); |
fe8ab488 | 3352 | } |
0a7de745 A |
3353 | |
3354 | if (!error && (sentlen != req->r_mreqlen)) { | |
2d21ac55 | 3355 | error = EWOULDBLOCK; |
0a7de745 | 3356 | } |
2d21ac55 A |
3357 | needrecon = ((sotype == SOCK_STREAM) && sentlen && (sentlen != req->r_mreqlen)); |
3358 | ||
3359 | lck_mtx_lock(&req->r_mtx); | |
b0d623f7 | 3360 | req->r_flags &= ~R_SENDING; |
2d21ac55 | 3361 | req->r_rtt = 0; |
0a7de745 | 3362 | if (rexmit && (++req->r_rexmit > NFS_MAXREXMIT)) { |
2d21ac55 | 3363 | req->r_rexmit = NFS_MAXREXMIT; |
0a7de745 | 3364 | } |
2d21ac55 A |
3365 | |
3366 | if (!error) { | |
3367 | /* SUCCESS */ | |
3368 | req->r_flags &= ~R_RESENDERR; | |
0a7de745 | 3369 | if (rexmit) { |
316670eb | 3370 | OSAddAtomic64(1, &nfsstats.rpcretries); |
0a7de745 | 3371 | } |
2d21ac55 A |
3372 | req->r_flags |= R_SENT; |
3373 | if (req->r_flags & R_WAITSENT) { | |
3374 | req->r_flags &= ~R_WAITSENT; | |
3375 | wakeup(req); | |
3376 | } | |
3377 | nfs_sndunlock(req); | |
3378 | lck_mtx_unlock(&req->r_mtx); | |
0a7de745 | 3379 | return 0; |
2d21ac55 A |
3380 | } |
3381 | ||
3382 | /* send failed */ | |
3383 | req->r_flags |= R_MUSTRESEND; | |
0a7de745 | 3384 | if (rexmit) { |
2d21ac55 | 3385 | req->r_flags |= R_RESENDERR; |
0a7de745 A |
3386 | } |
3387 | if ((error == EINTR) || (error == ERESTART)) { | |
2d21ac55 | 3388 | req->r_error = error; |
0a7de745 | 3389 | } |
2d21ac55 A |
3390 | lck_mtx_unlock(&req->r_mtx); |
3391 | ||
3392 | if (sotype == SOCK_DGRAM) { | |
1c79356b | 3393 | /* |
2d21ac55 A |
3394 | * Note: even though a first send may fail, we consider |
3395 | * the request sent for congestion window purposes. | |
3396 | * So we don't need to undo any of the changes made above. | |
1c79356b | 3397 | */ |
1c79356b | 3398 | /* |
2d21ac55 A |
3399 | * Socket errors ignored for connectionless sockets?? |
3400 | * For now, ignore them all | |
1c79356b | 3401 | */ |
2d21ac55 | 3402 | if ((error != EINTR) && (error != ERESTART) && |
6d2010ae | 3403 | (error != EWOULDBLOCK) && (error != EIO) && (nso == nmp->nm_nso)) { |
2d21ac55 | 3404 | int clearerror = 0, optlen = sizeof(clearerror); |
6d2010ae | 3405 | sock_getsockopt(nso->nso_so, SOL_SOCKET, SO_ERROR, &clearerror, &optlen); |
2d21ac55 | 3406 | #ifdef NFS_SOCKET_DEBUGGING |
0a7de745 | 3407 | if (clearerror) { |
39236c6e | 3408 | NFS_SOCK_DBG("nfs_send: ignoring UDP socket error %d so %d\n", |
0a7de745 A |
3409 | error, clearerror); |
3410 | } | |
2d21ac55 | 3411 | #endif |
1c79356b | 3412 | } |
2d21ac55 A |
3413 | } |
3414 | ||
3415 | /* check if it appears we should reconnect the socket */ | |
3416 | switch (error) { | |
3417 | case EWOULDBLOCK: | |
3418 | /* if send timed out, reconnect if on TCP */ | |
0a7de745 | 3419 | if (sotype != SOCK_STREAM) { |
2d21ac55 | 3420 | break; |
0a7de745 | 3421 | } |
2d21ac55 A |
3422 | case EPIPE: |
3423 | case EADDRNOTAVAIL: | |
3424 | case ENETDOWN: | |
3425 | case ENETUNREACH: | |
3426 | case ENETRESET: | |
3427 | case ECONNABORTED: | |
3428 | case ECONNRESET: | |
3429 | case ENOTCONN: | |
3430 | case ESHUTDOWN: | |
3431 | case ECONNREFUSED: | |
3432 | case EHOSTDOWN: | |
3433 | case EHOSTUNREACH: | |
0a7de745 | 3434 | /* case ECANCELED??? */ |
2d21ac55 A |
3435 | needrecon = 1; |
3436 | break; | |
3437 | } | |
6d2010ae | 3438 | if (needrecon && (nso == nmp->nm_nso)) { /* mark socket as needing reconnect */ |
39236c6e | 3439 | NFS_SOCK_DBG("nfs_send: 0x%llx need reconnect %d\n", req->r_xid, error); |
2d21ac55 A |
3440 | nfs_need_reconnect(nmp); |
3441 | } | |
3442 | ||
3443 | nfs_sndunlock(req); | |
3444 | ||
0a7de745 | 3445 | if (nfs_is_dead(error, nmp)) { |
3e170ce0 | 3446 | error = EIO; |
0a7de745 | 3447 | } |
3e170ce0 | 3448 | |
2d21ac55 A |
3449 | /* |
3450 | * Don't log some errors: | |
3451 | * EPIPE errors may be common with servers that drop idle connections. | |
3452 | * EADDRNOTAVAIL may occur on network transitions. | |
3453 | * ENOTCONN may occur under some network conditions. | |
3454 | */ | |
0a7de745 | 3455 | if ((error == EPIPE) || (error == EADDRNOTAVAIL) || (error == ENOTCONN)) { |
2d21ac55 | 3456 | error = 0; |
0a7de745 A |
3457 | } |
3458 | if (error && (error != EINTR) && (error != ERESTART)) { | |
2d21ac55 | 3459 | log(LOG_INFO, "nfs send error %d for server %s\n", error, |
0a7de745 A |
3460 | !req->r_nmp ? "<unmounted>" : |
3461 | vfs_statfs(req->r_nmp->nm_mountp)->f_mntfromname); | |
3462 | } | |
2d21ac55 A |
3463 | |
3464 | /* prefer request termination error over other errors */ | |
3465 | error2 = nfs_sigintr(req->r_nmp, req, req->r_thread, 0); | |
0a7de745 | 3466 | if (error2) { |
2d21ac55 | 3467 | error = error2; |
0a7de745 | 3468 | } |
2d21ac55 A |
3469 | |
3470 | /* only allow the following errors to be returned */ | |
3471 | if ((error != EINTR) && (error != ERESTART) && (error != EIO) && | |
0a7de745 | 3472 | (error != ENXIO) && (error != ETIMEDOUT)) { |
a39ff7e2 A |
3473 | /* |
3474 | * We got some error we don't know what do do with, | |
3475 | * i.e., we're not reconnecting, we map it to | |
3476 | * EIO. Presumably our send failed and we better tell | |
3477 | * the caller so they don't wait for a reply that is | |
3478 | * never going to come. If we are reconnecting we | |
3479 | * return 0 and the request will be resent. | |
3480 | */ | |
3481 | error = needrecon ? 0 : EIO; | |
0a7de745 A |
3482 | } |
3483 | return error; | |
2d21ac55 A |
3484 | } |
3485 | ||
3486 | /* | |
3487 | * NFS client socket upcalls | |
3488 | * | |
3489 | * Pull RPC replies out of an NFS mount's socket and match them | |
3490 | * up with the pending request. | |
3491 | * | |
3492 | * The datagram code is simple because we always get whole | |
3493 | * messages out of the socket. | |
3494 | * | |
3495 | * The stream code is more involved because we have to parse | |
3496 | * the RPC records out of the stream. | |
3497 | */ | |
3498 | ||
3499 | /* NFS client UDP socket upcall */ | |
b0d623f7 | 3500 | void |
2d21ac55 A |
3501 | nfs_udp_rcv(socket_t so, void *arg, __unused int waitflag) |
3502 | { | |
3503 | struct nfsmount *nmp = arg; | |
6d2010ae | 3504 | struct nfs_socket *nso = nmp->nm_nso; |
2d21ac55 A |
3505 | size_t rcvlen; |
3506 | mbuf_t m; | |
3507 | int error = 0; | |
3508 | ||
0a7de745 | 3509 | if (nmp->nm_sockflags & NMSOCK_CONNECTING) { |
2d21ac55 | 3510 | return; |
0a7de745 | 3511 | } |
2d21ac55 A |
3512 | |
3513 | do { | |
6d2010ae | 3514 | /* make sure we're on the current socket */ |
0a7de745 | 3515 | if (!nso || (nso->nso_so != so)) { |
6d2010ae | 3516 | return; |
0a7de745 | 3517 | } |
6d2010ae | 3518 | |
2d21ac55 A |
3519 | m = NULL; |
3520 | rcvlen = 1000000; | |
3521 | error = sock_receivembuf(so, NULL, &m, MSG_DONTWAIT, &rcvlen); | |
0a7de745 | 3522 | if (m) { |
2d21ac55 | 3523 | nfs_request_match_reply(nmp, m); |
0a7de745 | 3524 | } |
2d21ac55 A |
3525 | } while (m && !error); |
3526 | ||
3527 | if (error && (error != EWOULDBLOCK)) { | |
3528 | /* problems with the socket... mark for reconnection */ | |
39236c6e | 3529 | NFS_SOCK_DBG("nfs_udp_rcv: need reconnect %d\n", error); |
2d21ac55 A |
3530 | nfs_need_reconnect(nmp); |
3531 | } | |
3532 | } | |
3533 | ||
3534 | /* NFS client TCP socket upcall */ | |
b0d623f7 | 3535 | void |
2d21ac55 A |
3536 | nfs_tcp_rcv(socket_t so, void *arg, __unused int waitflag) |
3537 | { | |
3538 | struct nfsmount *nmp = arg; | |
6d2010ae A |
3539 | struct nfs_socket *nso = nmp->nm_nso; |
3540 | struct nfs_rpc_record_state nrrs; | |
2d21ac55 A |
3541 | mbuf_t m; |
3542 | int error = 0; | |
6d2010ae | 3543 | int recv = 1; |
fe8ab488 | 3544 | int wup = 0; |
d12e1678 | 3545 | |
0a7de745 | 3546 | if (nmp->nm_sockflags & NMSOCK_CONNECTING) { |
2d21ac55 | 3547 | return; |
0a7de745 | 3548 | } |
2d21ac55 A |
3549 | |
3550 | /* make sure we're on the current socket */ | |
2d21ac55 | 3551 | lck_mtx_lock(&nmp->nm_lock); |
6d2010ae A |
3552 | nso = nmp->nm_nso; |
3553 | if (!nso || (nso->nso_so != so) || (nmp->nm_sockflags & (NMSOCK_DISCONNECTING))) { | |
2d21ac55 A |
3554 | lck_mtx_unlock(&nmp->nm_lock); |
3555 | return; | |
3556 | } | |
6d2010ae | 3557 | lck_mtx_unlock(&nmp->nm_lock); |
2d21ac55 | 3558 | |
6d2010ae A |
3559 | /* make sure this upcall should be trying to do work */ |
3560 | lck_mtx_lock(&nso->nso_lock); | |
0a7de745 | 3561 | if (nso->nso_flags & (NSO_UPCALL | NSO_DISCONNECTING | NSO_DEAD)) { |
6d2010ae A |
3562 | lck_mtx_unlock(&nso->nso_lock); |
3563 | return; | |
2d21ac55 | 3564 | } |
6d2010ae A |
3565 | nso->nso_flags |= NSO_UPCALL; |
3566 | nrrs = nso->nso_rrs; | |
3567 | lck_mtx_unlock(&nso->nso_lock); | |
2d21ac55 | 3568 | |
6d2010ae A |
3569 | /* loop while we make error-free progress */ |
3570 | while (!error && recv) { | |
3571 | error = nfs_rpc_record_read(so, &nrrs, MSG_DONTWAIT, &recv, &m); | |
0a7de745 | 3572 | if (m) { /* match completed response with request */ |
6d2010ae | 3573 | nfs_request_match_reply(nmp, m); |
0a7de745 | 3574 | } |
2d21ac55 | 3575 | } |
1c79356b | 3576 | |
fe8ab488 A |
3577 | /* Update the sockets's rpc parsing state */ |
3578 | lck_mtx_lock(&nso->nso_lock); | |
3579 | nso->nso_rrs = nrrs; | |
0a7de745 | 3580 | if (nso->nso_flags & NSO_DISCONNECTING) { |
fe8ab488 | 3581 | wup = 1; |
0a7de745 | 3582 | } |
fe8ab488 A |
3583 | nso->nso_flags &= ~NSO_UPCALL; |
3584 | lck_mtx_unlock(&nso->nso_lock); | |
0a7de745 | 3585 | if (wup) { |
fe8ab488 | 3586 | wakeup(&nso->nso_flags); |
0a7de745 | 3587 | } |
fe8ab488 | 3588 | |
2d21ac55 | 3589 | #ifdef NFS_SOCKET_DEBUGGING |
0a7de745 | 3590 | if (!recv && (error != EWOULDBLOCK)) { |
39236c6e | 3591 | NFS_SOCK_DBG("nfs_tcp_rcv: got nothing, error %d, got FIN?\n", error); |
0a7de745 | 3592 | } |
2d21ac55 A |
3593 | #endif |
3594 | /* note: no error and no data indicates server closed its end */ | |
3595 | if ((error != EWOULDBLOCK) && (error || !recv)) { | |
3596 | /* problems with the socket... mark for reconnection */ | |
39236c6e | 3597 | NFS_SOCK_DBG("nfs_tcp_rcv: need reconnect %d\n", error); |
2d21ac55 A |
3598 | nfs_need_reconnect(nmp); |
3599 | } | |
3600 | } | |
3601 | ||
3602 | /* | |
3603 | * "poke" a socket to try to provoke any pending errors | |
3604 | */ | |
b0d623f7 | 3605 | void |
2d21ac55 A |
3606 | nfs_sock_poke(struct nfsmount *nmp) |
3607 | { | |
b0d623f7 | 3608 | struct iovec aio; |
2d21ac55 A |
3609 | struct msghdr msg; |
3610 | size_t len; | |
3611 | int error = 0; | |
3612 | int dummy; | |
3613 | ||
3614 | lck_mtx_lock(&nmp->nm_lock); | |
6d2010ae A |
3615 | if ((nmp->nm_sockflags & NMSOCK_UNMOUNT) || |
3616 | !(nmp->nm_sockflags & NMSOCK_READY) || !nmp->nm_nso || !nmp->nm_nso->nso_so) { | |
fe8ab488 A |
3617 | /* Nothing to poke */ |
3618 | nmp->nm_sockflags &= ~NMSOCK_POKE; | |
3619 | wakeup(&nmp->nm_sockflags); | |
2d21ac55 A |
3620 | lck_mtx_unlock(&nmp->nm_lock); |
3621 | return; | |
3622 | } | |
3623 | lck_mtx_unlock(&nmp->nm_lock); | |
b0d623f7 | 3624 | aio.iov_base = &dummy; |
2d21ac55 A |
3625 | aio.iov_len = 0; |
3626 | len = 0; | |
3627 | bzero(&msg, sizeof(msg)); | |
b0d623f7 | 3628 | msg.msg_iov = &aio; |
2d21ac55 | 3629 | msg.msg_iovlen = 1; |
6d2010ae | 3630 | error = sock_send(nmp->nm_nso->nso_so, &msg, MSG_DONTWAIT, &len); |
39236c6e | 3631 | NFS_SOCK_DBG("nfs_sock_poke: error %d\n", error); |
fe8ab488 A |
3632 | lck_mtx_lock(&nmp->nm_lock); |
3633 | nmp->nm_sockflags &= ~NMSOCK_POKE; | |
3634 | wakeup(&nmp->nm_sockflags); | |
3635 | lck_mtx_unlock(&nmp->nm_lock); | |
316670eb | 3636 | nfs_is_dead(error, nmp); |
2d21ac55 A |
3637 | } |
3638 | ||
3639 | /* | |
3640 | * Match an RPC reply with the corresponding request | |
3641 | */ | |
b0d623f7 | 3642 | void |
2d21ac55 A |
3643 | nfs_request_match_reply(struct nfsmount *nmp, mbuf_t mrep) |
3644 | { | |
3645 | struct nfsreq *req; | |
3646 | struct nfsm_chain nmrep; | |
b0d623f7 A |
3647 | u_int32_t reply = 0, rxid = 0; |
3648 | int error = 0, asyncioq, t1; | |
2d21ac55 A |
3649 | |
3650 | /* Get the xid and check that it is an rpc reply */ | |
3651 | nfsm_chain_dissect_init(error, &nmrep, mrep); | |
3652 | nfsm_chain_get_32(error, &nmrep, rxid); | |
3653 | nfsm_chain_get_32(error, &nmrep, reply); | |
3654 | if (error || (reply != RPC_REPLY)) { | |
316670eb | 3655 | OSAddAtomic64(1, &nfsstats.rpcinvalid); |
2d21ac55 A |
3656 | mbuf_freem(mrep); |
3657 | return; | |
3658 | } | |
3659 | ||
3660 | /* | |
3661 | * Loop through the request list to match up the reply | |
3662 | * Iff no match, just drop it. | |
3663 | */ | |
3664 | lck_mtx_lock(nfs_request_mutex); | |
3665 | TAILQ_FOREACH(req, &nfs_reqq, r_chain) { | |
0a7de745 | 3666 | if (req->r_nmrep.nmc_mhead || (rxid != R_XID32(req->r_xid))) { |
2d21ac55 | 3667 | continue; |
0a7de745 | 3668 | } |
2d21ac55 A |
3669 | /* looks like we have it, grab lock and double check */ |
3670 | lck_mtx_lock(&req->r_mtx); | |
3671 | if (req->r_nmrep.nmc_mhead || (rxid != R_XID32(req->r_xid))) { | |
3672 | lck_mtx_unlock(&req->r_mtx); | |
1c79356b A |
3673 | continue; |
3674 | } | |
2d21ac55 A |
3675 | /* Found it.. */ |
3676 | req->r_nmrep = nmrep; | |
3677 | lck_mtx_lock(&nmp->nm_lock); | |
3678 | if (nmp->nm_sotype == SOCK_DGRAM) { | |
3679 | /* | |
3680 | * Update congestion window. | |
3681 | * Do the additive increase of one rpc/rtt. | |
3682 | */ | |
3683 | FSDBG(530, R_XID32(req->r_xid), req, nmp->nm_sent, nmp->nm_cwnd); | |
3684 | if (nmp->nm_cwnd <= nmp->nm_sent) { | |
3685 | nmp->nm_cwnd += | |
0a7de745 | 3686 | ((NFS_CWNDSCALE * NFS_CWNDSCALE) + |
2d21ac55 | 3687 | (nmp->nm_cwnd >> 1)) / nmp->nm_cwnd; |
0a7de745 | 3688 | if (nmp->nm_cwnd > NFS_MAXCWND) { |
2d21ac55 | 3689 | nmp->nm_cwnd = NFS_MAXCWND; |
0a7de745 | 3690 | } |
2d21ac55 A |
3691 | } |
3692 | if (req->r_flags & R_CWND) { | |
3693 | nmp->nm_sent -= NFS_CWNDSCALE; | |
3694 | req->r_flags &= ~R_CWND; | |
3695 | } | |
3696 | if ((nmp->nm_sent < nmp->nm_cwnd) && !TAILQ_EMPTY(&nmp->nm_cwndq)) { | |
3697 | /* congestion window is open, poke the cwnd queue */ | |
3698 | struct nfsreq *req2 = TAILQ_FIRST(&nmp->nm_cwndq); | |
3699 | TAILQ_REMOVE(&nmp->nm_cwndq, req2, r_cchain); | |
3700 | req2->r_cchain.tqe_next = NFSREQNOLIST; | |
3701 | wakeup(req2); | |
3702 | } | |
3703 | } | |
1c79356b | 3704 | /* |
2d21ac55 A |
3705 | * Update rtt using a gain of 0.125 on the mean |
3706 | * and a gain of 0.25 on the deviation. | |
1c79356b | 3707 | */ |
2d21ac55 A |
3708 | if (req->r_flags & R_TIMING) { |
3709 | /* | |
3710 | * Since the timer resolution of | |
3711 | * NFS_HZ is so course, it can often | |
3712 | * result in r_rtt == 0. Since | |
3713 | * r_rtt == N means that the actual | |
3714 | * rtt is between N+dt and N+2-dt ticks, | |
3715 | * add 1. | |
3716 | */ | |
0a7de745 | 3717 | if (proct[req->r_procnum] == 0) { |
2d21ac55 | 3718 | panic("nfs_request_match_reply: proct[%d] is zero", req->r_procnum); |
0a7de745 | 3719 | } |
2d21ac55 A |
3720 | t1 = req->r_rtt + 1; |
3721 | t1 -= (NFS_SRTT(req) >> 3); | |
3722 | NFS_SRTT(req) += t1; | |
0a7de745 | 3723 | if (t1 < 0) { |
2d21ac55 | 3724 | t1 = -t1; |
0a7de745 | 3725 | } |
2d21ac55 A |
3726 | t1 -= (NFS_SDRTT(req) >> 2); |
3727 | NFS_SDRTT(req) += t1; | |
3728 | } | |
3729 | nmp->nm_timeouts = 0; | |
3730 | lck_mtx_unlock(&nmp->nm_lock); | |
3731 | /* signal anyone waiting on this request */ | |
3732 | wakeup(req); | |
3733 | asyncioq = (req->r_callback.rcb_func != NULL); | |
0a7de745 | 3734 | if (nfs_request_using_gss(req)) { |
b0d623f7 | 3735 | nfs_gss_clnt_rpcdone(req); |
0a7de745 | 3736 | } |
2d21ac55 A |
3737 | lck_mtx_unlock(&req->r_mtx); |
3738 | lck_mtx_unlock(nfs_request_mutex); | |
2d21ac55 | 3739 | /* if it's an async RPC with a callback, queue it up */ |
0a7de745 | 3740 | if (asyncioq) { |
2d21ac55 | 3741 | nfs_asyncio_finish(req); |
0a7de745 | 3742 | } |
2d21ac55 A |
3743 | break; |
3744 | } | |
3745 | ||
3746 | if (!req) { | |
3747 | /* not matched to a request, so drop it. */ | |
3748 | lck_mtx_unlock(nfs_request_mutex); | |
316670eb | 3749 | OSAddAtomic64(1, &nfsstats.rpcunexpected); |
2d21ac55 A |
3750 | mbuf_freem(mrep); |
3751 | } | |
3752 | } | |
3753 | ||
3754 | /* | |
3755 | * Wait for the reply for a given request... | |
3756 | * ...potentially resending the request if necessary. | |
3757 | */ | |
b0d623f7 | 3758 | int |
2d21ac55 A |
3759 | nfs_wait_reply(struct nfsreq *req) |
3760 | { | |
b0d623f7 | 3761 | struct timespec ts = { 2, 0 }; |
6d2010ae | 3762 | int error = 0, slpflag, first = 1; |
2d21ac55 | 3763 | |
0a7de745 | 3764 | if (req->r_nmp && NMFLAG(req->r_nmp, INTR) && req->r_thread && !(req->r_flags & R_NOINTR)) { |
2d21ac55 | 3765 | slpflag = PCATCH; |
0a7de745 | 3766 | } else { |
2d21ac55 | 3767 | slpflag = 0; |
0a7de745 | 3768 | } |
2d21ac55 A |
3769 | |
3770 | lck_mtx_lock(&req->r_mtx); | |
3771 | while (!req->r_nmrep.nmc_mhead) { | |
0a7de745 | 3772 | if ((error = nfs_sigintr(req->r_nmp, req, first ? NULL : req->r_thread, 0))) { |
2d21ac55 | 3773 | break; |
0a7de745 A |
3774 | } |
3775 | if (((error = req->r_error)) || req->r_nmrep.nmc_mhead) { | |
2d21ac55 | 3776 | break; |
0a7de745 | 3777 | } |
2d21ac55 A |
3778 | /* check if we need to resend */ |
3779 | if (req->r_flags & R_MUSTRESEND) { | |
39236c6e | 3780 | NFS_SOCK_DBG("nfs wait resend: p %d x 0x%llx f 0x%x rtt %d\n", |
0a7de745 | 3781 | req->r_procnum, req->r_xid, req->r_flags, req->r_rtt); |
b0d623f7 | 3782 | req->r_flags |= R_SENDING; |
2d21ac55 | 3783 | lck_mtx_unlock(&req->r_mtx); |
6d2010ae | 3784 | if (nfs_request_using_gss(req)) { |
1c79356b | 3785 | /* |
6d2010ae | 3786 | * It's an RPCSEC_GSS request. |
2d21ac55 A |
3787 | * Can't just resend the original request |
3788 | * without bumping the cred sequence number. | |
3789 | * Go back and re-build the request. | |
1c79356b | 3790 | */ |
b0d623f7 A |
3791 | lck_mtx_lock(&req->r_mtx); |
3792 | req->r_flags &= ~R_SENDING; | |
3793 | lck_mtx_unlock(&req->r_mtx); | |
0a7de745 | 3794 | return EAGAIN; |
1c79356b | 3795 | } |
2d21ac55 A |
3796 | error = nfs_send(req, 1); |
3797 | lck_mtx_lock(&req->r_mtx); | |
39236c6e | 3798 | NFS_SOCK_DBG("nfs wait resend: p %d x 0x%llx f 0x%x rtt %d err %d\n", |
0a7de745 A |
3799 | req->r_procnum, req->r_xid, req->r_flags, req->r_rtt, error); |
3800 | if (error) { | |
2d21ac55 | 3801 | break; |
0a7de745 A |
3802 | } |
3803 | if (((error = req->r_error)) || req->r_nmrep.nmc_mhead) { | |
2d21ac55 | 3804 | break; |
0a7de745 | 3805 | } |
1c79356b | 3806 | } |
2d21ac55 | 3807 | /* need to poll if we're P_NOREMOTEHANG */ |
0a7de745 | 3808 | if (nfs_noremotehang(req->r_thread)) { |
2d21ac55 | 3809 | ts.tv_sec = 1; |
0a7de745 | 3810 | } |
36401178 | 3811 | msleep(req, &req->r_mtx, slpflag | (PZERO - 1), "nfswaitreply", &ts); |
6d2010ae | 3812 | first = slpflag = 0; |
1c79356b | 3813 | } |
2d21ac55 A |
3814 | lck_mtx_unlock(&req->r_mtx); |
3815 | ||
0a7de745 | 3816 | return error; |
1c79356b A |
3817 | } |
3818 | ||
3819 | /* | |
2d21ac55 A |
3820 | * An NFS request goes something like this: |
3821 | * (nb: always frees up mreq mbuf list) | |
3822 | * nfs_request_create() | |
3823 | * - allocates a request struct if one is not provided | |
3824 | * - initial fill-in of the request struct | |
3825 | * nfs_request_add_header() | |
3826 | * - add the RPC header | |
3827 | * nfs_request_send() | |
3828 | * - link it into list | |
3829 | * - call nfs_send() for first transmit | |
3830 | * nfs_request_wait() | |
3831 | * - call nfs_wait_reply() to wait for the reply | |
3832 | * nfs_request_finish() | |
3833 | * - break down rpc header and return with error or nfs reply | |
3834 | * pointed to by nmrep. | |
3835 | * nfs_request_rele() | |
3836 | * nfs_request_destroy() | |
3837 | * - clean up the request struct | |
3838 | * - free the request struct if it was allocated by nfs_request_create() | |
3839 | */ | |
3840 | ||
3841 | /* | |
3842 | * Set up an NFS request struct (allocating if no request passed in). | |
1c79356b A |
3843 | */ |
3844 | int | |
2d21ac55 A |
3845 | nfs_request_create( |
3846 | nfsnode_t np, | |
0a7de745 | 3847 | mount_t mp, /* used only if !np */ |
2d21ac55 A |
3848 | struct nfsm_chain *nmrest, |
3849 | int procnum, | |
3850 | thread_t thd, | |
3851 | kauth_cred_t cred, | |
3852 | struct nfsreq **reqp) | |
1c79356b | 3853 | { |
2d21ac55 | 3854 | struct nfsreq *req, *newreq = NULL; |
1c79356b | 3855 | struct nfsmount *nmp; |
1c79356b | 3856 | |
2d21ac55 A |
3857 | req = *reqp; |
3858 | if (!req) { | |
3859 | /* allocate a new NFS request structure */ | |
3860 | MALLOC_ZONE(newreq, struct nfsreq*, sizeof(*newreq), M_NFSREQ, M_WAITOK); | |
3861 | if (!newreq) { | |
3862 | mbuf_freem(nmrest->nmc_mhead); | |
3863 | nmrest->nmc_mhead = NULL; | |
0a7de745 | 3864 | return ENOMEM; |
2d21ac55 A |
3865 | } |
3866 | req = newreq; | |
3867 | } | |
55e303ae | 3868 | |
2d21ac55 | 3869 | bzero(req, sizeof(*req)); |
0a7de745 | 3870 | if (req == newreq) { |
2d21ac55 | 3871 | req->r_flags = R_ALLOCATED; |
0a7de745 | 3872 | } |
1c79356b | 3873 | |
2d21ac55 | 3874 | nmp = VFSTONFS(np ? NFSTOMP(np) : mp); |
fe8ab488 | 3875 | if (nfs_mount_gone(nmp)) { |
0a7de745 | 3876 | if (newreq) { |
2d21ac55 | 3877 | FREE_ZONE(newreq, sizeof(*newreq), M_NFSREQ); |
0a7de745 A |
3878 | } |
3879 | return ENXIO; | |
2d21ac55 A |
3880 | } |
3881 | lck_mtx_lock(&nmp->nm_lock); | |
0a7de745 | 3882 | if ((nmp->nm_state & (NFSSTA_FORCE | NFSSTA_DEAD)) && |
fe8ab488 | 3883 | (nmp->nm_state & NFSSTA_TIMEO)) { |
2d21ac55 A |
3884 | lck_mtx_unlock(&nmp->nm_lock); |
3885 | mbuf_freem(nmrest->nmc_mhead); | |
3886 | nmrest->nmc_mhead = NULL; | |
0a7de745 | 3887 | if (newreq) { |
2d21ac55 | 3888 | FREE_ZONE(newreq, sizeof(*newreq), M_NFSREQ); |
0a7de745 A |
3889 | } |
3890 | return ENXIO; | |
1c79356b | 3891 | } |
0a7de745 A |
3892 | |
3893 | if ((nmp->nm_vers != NFS_VER4) && (procnum >= 0) && (procnum < NFS_NPROCS)) { | |
316670eb | 3894 | OSAddAtomic64(1, &nfsstats.rpccnt[procnum]); |
0a7de745 A |
3895 | } |
3896 | if ((nmp->nm_vers == NFS_VER4) && (procnum != NFSPROC4_COMPOUND) && (procnum != NFSPROC4_NULL)) { | |
2d21ac55 | 3897 | panic("nfs_request: invalid NFSv4 RPC request %d\n", procnum); |
0a7de745 | 3898 | } |
55e303ae | 3899 | |
2d21ac55 A |
3900 | lck_mtx_init(&req->r_mtx, nfs_request_grp, LCK_ATTR_NULL); |
3901 | req->r_nmp = nmp; | |
fe8ab488 | 3902 | nmp->nm_ref++; |
2d21ac55 A |
3903 | req->r_np = np; |
3904 | req->r_thread = thd; | |
0a7de745 | 3905 | if (!thd) { |
6d2010ae | 3906 | req->r_flags |= R_NOINTR; |
0a7de745 | 3907 | } |
2d21ac55 A |
3908 | if (IS_VALID_CRED(cred)) { |
3909 | kauth_cred_ref(cred); | |
3910 | req->r_cred = cred; | |
1c79356b | 3911 | } |
2d21ac55 | 3912 | req->r_procnum = procnum; |
0a7de745 | 3913 | if (proct[procnum] > 0) { |
2d21ac55 | 3914 | req->r_flags |= R_TIMING; |
0a7de745 | 3915 | } |
2d21ac55 A |
3916 | req->r_nmrep.nmc_mhead = NULL; |
3917 | SLIST_INIT(&req->r_gss_seqlist); | |
3918 | req->r_achain.tqe_next = NFSREQNOLIST; | |
3919 | req->r_rchain.tqe_next = NFSREQNOLIST; | |
3920 | req->r_cchain.tqe_next = NFSREQNOLIST; | |
3921 | ||
6d2010ae | 3922 | /* set auth flavor to use for request */ |
0a7de745 | 3923 | if (!req->r_cred) { |
6d2010ae | 3924 | req->r_auth = RPCAUTH_NONE; |
0a7de745 | 3925 | } else if (req->r_np && (req->r_np->n_auth != RPCAUTH_INVALID)) { |
6d2010ae | 3926 | req->r_auth = req->r_np->n_auth; |
0a7de745 | 3927 | } else { |
6d2010ae | 3928 | req->r_auth = nmp->nm_auth; |
0a7de745 | 3929 | } |
6d2010ae | 3930 | |
2d21ac55 A |
3931 | lck_mtx_unlock(&nmp->nm_lock); |
3932 | ||
3933 | /* move the request mbuf chain to the nfsreq */ | |
3934 | req->r_mrest = nmrest->nmc_mhead; | |
3935 | nmrest->nmc_mhead = NULL; | |
3936 | ||
3937 | req->r_flags |= R_INITTED; | |
3938 | req->r_refs = 1; | |
0a7de745 | 3939 | if (newreq) { |
2d21ac55 | 3940 | *reqp = req; |
0a7de745 A |
3941 | } |
3942 | return 0; | |
2d21ac55 A |
3943 | } |
3944 | ||
3945 | /* | |
3946 | * Clean up and free an NFS request structure. | |
3947 | */ | |
3948 | void | |
3949 | nfs_request_destroy(struct nfsreq *req) | |
3950 | { | |
3e170ce0 | 3951 | struct nfsmount *nmp; |
2d21ac55 | 3952 | struct gss_seq *gsp, *ngsp; |
b0d623f7 | 3953 | int clearjbtimeo = 0; |
2d21ac55 | 3954 | |
0a7de745 | 3955 | if (!req || !(req->r_flags & R_INITTED)) { |
2d21ac55 | 3956 | return; |
0a7de745 | 3957 | } |
5ba3f43e | 3958 | nmp = req->r_nmp; |
2d21ac55 | 3959 | req->r_flags &= ~R_INITTED; |
0a7de745 | 3960 | if (req->r_lflags & RL_QUEUED) { |
2d21ac55 | 3961 | nfs_reqdequeue(req); |
0a7de745 | 3962 | } |
fe8ab488 | 3963 | |
3e170ce0 | 3964 | if (req->r_achain.tqe_next != NFSREQNOLIST) { |
0a7de745 | 3965 | /* |
fe8ab488 A |
3966 | * Still on an async I/O queue? |
3967 | * %%% But which one, we may be on a local iod. | |
3968 | */ | |
2d21ac55 | 3969 | lck_mtx_lock(nfsiod_mutex); |
3e170ce0 | 3970 | if (nmp && req->r_achain.tqe_next != NFSREQNOLIST) { |
2d21ac55 A |
3971 | TAILQ_REMOVE(&nmp->nm_iodq, req, r_achain); |
3972 | req->r_achain.tqe_next = NFSREQNOLIST; | |
3973 | } | |
3974 | lck_mtx_unlock(nfsiod_mutex); | |
3975 | } | |
fe8ab488 | 3976 | |
b0d623f7 | 3977 | lck_mtx_lock(&req->r_mtx); |
2d21ac55 A |
3978 | if (nmp) { |
3979 | lck_mtx_lock(&nmp->nm_lock); | |
6d2010ae A |
3980 | if (req->r_flags & R_CWND) { |
3981 | /* Decrement the outstanding request count. */ | |
3982 | req->r_flags &= ~R_CWND; | |
3983 | nmp->nm_sent -= NFS_CWNDSCALE; | |
3984 | if ((nmp->nm_sent < nmp->nm_cwnd) && !TAILQ_EMPTY(&nmp->nm_cwndq)) { | |
3985 | /* congestion window is open, poke the cwnd queue */ | |
3986 | struct nfsreq *req2 = TAILQ_FIRST(&nmp->nm_cwndq); | |
3987 | TAILQ_REMOVE(&nmp->nm_cwndq, req2, r_cchain); | |
3988 | req2->r_cchain.tqe_next = NFSREQNOLIST; | |
3989 | wakeup(req2); | |
3990 | } | |
3991 | } | |
3e170ce0 A |
3992 | assert((req->r_flags & R_RESENDQ) == 0); |
3993 | /* XXX should we just remove this conditional, we should have a reference if we're resending */ | |
2d21ac55 A |
3994 | if (req->r_rchain.tqe_next != NFSREQNOLIST) { |
3995 | TAILQ_REMOVE(&nmp->nm_resendq, req, r_rchain); | |
3996 | req->r_rchain.tqe_next = NFSREQNOLIST; | |
0a7de745 | 3997 | if (req->r_flags & R_RESENDQ) { |
b0d623f7 | 3998 | req->r_flags &= ~R_RESENDQ; |
0a7de745 | 3999 | } |
2d21ac55 A |
4000 | } |
4001 | if (req->r_cchain.tqe_next != NFSREQNOLIST) { | |
4002 | TAILQ_REMOVE(&nmp->nm_cwndq, req, r_cchain); | |
4003 | req->r_cchain.tqe_next = NFSREQNOLIST; | |
4004 | } | |
b0d623f7 A |
4005 | if (req->r_flags & R_JBTPRINTFMSG) { |
4006 | req->r_flags &= ~R_JBTPRINTFMSG; | |
4007 | nmp->nm_jbreqs--; | |
4008 | clearjbtimeo = (nmp->nm_jbreqs == 0) ? NFSSTA_JUKEBOXTIMEO : 0; | |
4009 | } | |
2d21ac55 A |
4010 | lck_mtx_unlock(&nmp->nm_lock); |
4011 | } | |
2d21ac55 | 4012 | lck_mtx_unlock(&req->r_mtx); |
fe8ab488 | 4013 | |
0a7de745 | 4014 | if (clearjbtimeo) { |
b0d623f7 | 4015 | nfs_up(nmp, req->r_thread, clearjbtimeo, NULL); |
0a7de745 A |
4016 | } |
4017 | if (req->r_mhead) { | |
2d21ac55 | 4018 | mbuf_freem(req->r_mhead); |
0a7de745 | 4019 | } else if (req->r_mrest) { |
2d21ac55 | 4020 | mbuf_freem(req->r_mrest); |
0a7de745 A |
4021 | } |
4022 | if (req->r_nmrep.nmc_mhead) { | |
2d21ac55 | 4023 | mbuf_freem(req->r_nmrep.nmc_mhead); |
0a7de745 A |
4024 | } |
4025 | if (IS_VALID_CRED(req->r_cred)) { | |
2d21ac55 | 4026 | kauth_cred_unref(&req->r_cred); |
0a7de745 A |
4027 | } |
4028 | if (nfs_request_using_gss(req)) { | |
2d21ac55 | 4029 | nfs_gss_clnt_rpcdone(req); |
0a7de745 | 4030 | } |
2d21ac55 | 4031 | SLIST_FOREACH_SAFE(gsp, &req->r_gss_seqlist, gss_seqnext, ngsp) |
0a7de745 A |
4032 | FREE(gsp, M_TEMP); |
4033 | if (req->r_gss_ctx) { | |
2d21ac55 | 4034 | nfs_gss_clnt_ctx_unref(req); |
0a7de745 A |
4035 | } |
4036 | if (req->r_wrongsec) { | |
6d2010ae | 4037 | FREE(req->r_wrongsec, M_TEMP); |
0a7de745 A |
4038 | } |
4039 | if (nmp) { | |
fe8ab488 | 4040 | nfs_mount_rele(nmp); |
0a7de745 | 4041 | } |
2d21ac55 | 4042 | lck_mtx_destroy(&req->r_mtx, nfs_request_grp); |
0a7de745 | 4043 | if (req->r_flags & R_ALLOCATED) { |
2d21ac55 | 4044 | FREE_ZONE(req, sizeof(*req), M_NFSREQ); |
0a7de745 | 4045 | } |
2d21ac55 A |
4046 | } |
4047 | ||
4048 | void | |
4049 | nfs_request_ref(struct nfsreq *req, int locked) | |
4050 | { | |
0a7de745 | 4051 | if (!locked) { |
2d21ac55 | 4052 | lck_mtx_lock(&req->r_mtx); |
0a7de745 A |
4053 | } |
4054 | if (req->r_refs <= 0) { | |
2d21ac55 | 4055 | panic("nfsreq reference error"); |
0a7de745 | 4056 | } |
2d21ac55 | 4057 | req->r_refs++; |
0a7de745 | 4058 | if (!locked) { |
2d21ac55 | 4059 | lck_mtx_unlock(&req->r_mtx); |
0a7de745 | 4060 | } |
2d21ac55 A |
4061 | } |
4062 | ||
4063 | void | |
4064 | nfs_request_rele(struct nfsreq *req) | |
4065 | { | |
4066 | int destroy; | |
4067 | ||
4068 | lck_mtx_lock(&req->r_mtx); | |
0a7de745 | 4069 | if (req->r_refs <= 0) { |
2d21ac55 | 4070 | panic("nfsreq reference underflow"); |
0a7de745 | 4071 | } |
2d21ac55 A |
4072 | req->r_refs--; |
4073 | destroy = (req->r_refs == 0); | |
4074 | lck_mtx_unlock(&req->r_mtx); | |
0a7de745 | 4075 | if (destroy) { |
2d21ac55 | 4076 | nfs_request_destroy(req); |
0a7de745 | 4077 | } |
2d21ac55 A |
4078 | } |
4079 | ||
4080 | ||
4081 | /* | |
4082 | * Add an (updated) RPC header with authorization to an NFS request. | |
4083 | */ | |
4084 | int | |
4085 | nfs_request_add_header(struct nfsreq *req) | |
4086 | { | |
4087 | struct nfsmount *nmp; | |
6d2010ae | 4088 | int error = 0; |
2d21ac55 A |
4089 | mbuf_t m; |
4090 | ||
4091 | /* free up any previous header */ | |
4092 | if ((m = req->r_mhead)) { | |
0a7de745 | 4093 | while (m && (m != req->r_mrest)) { |
2d21ac55 | 4094 | m = mbuf_free(m); |
0a7de745 | 4095 | } |
2d21ac55 A |
4096 | req->r_mhead = NULL; |
4097 | } | |
4098 | ||
5ba3f43e | 4099 | nmp = req->r_nmp; |
0a7de745 A |
4100 | if (nfs_mount_gone(nmp)) { |
4101 | return ENXIO; | |
4102 | } | |
2d21ac55 | 4103 | |
6d2010ae | 4104 | error = nfsm_rpchead(req, req->r_mrest, &req->r_xid, &req->r_mhead); |
0a7de745 A |
4105 | if (error) { |
4106 | return error; | |
4107 | } | |
2d21ac55 A |
4108 | |
4109 | req->r_mreqlen = mbuf_pkthdr_len(req->r_mhead); | |
5ba3f43e | 4110 | nmp = req->r_nmp; |
0a7de745 A |
4111 | if (nfs_mount_gone(nmp)) { |
4112 | return ENXIO; | |
4113 | } | |
2d21ac55 | 4114 | lck_mtx_lock(&nmp->nm_lock); |
0a7de745 | 4115 | if (NMFLAG(nmp, SOFT) || (req->r_flags & R_SOFT)) { |
2d21ac55 | 4116 | req->r_retry = nmp->nm_retry; |
0a7de745 A |
4117 | } else { |
4118 | req->r_retry = NFS_MAXREXMIT + 1; /* past clip limit */ | |
4119 | } | |
2d21ac55 A |
4120 | lck_mtx_unlock(&nmp->nm_lock); |
4121 | ||
0a7de745 | 4122 | return error; |
2d21ac55 A |
4123 | } |
4124 | ||
4125 | ||
4126 | /* | |
4127 | * Queue an NFS request up and send it out. | |
4128 | */ | |
4129 | int | |
4130 | nfs_request_send(struct nfsreq *req, int wait) | |
4131 | { | |
4132 | struct nfsmount *nmp; | |
4133 | struct timeval now; | |
4134 | ||
b0d623f7 A |
4135 | lck_mtx_lock(&req->r_mtx); |
4136 | req->r_flags |= R_SENDING; | |
4137 | lck_mtx_unlock(&req->r_mtx); | |
4138 | ||
2d21ac55 | 4139 | lck_mtx_lock(nfs_request_mutex); |
1c79356b | 4140 | |
5ba3f43e | 4141 | nmp = req->r_nmp; |
fe8ab488 | 4142 | if (nfs_mount_gone(nmp)) { |
2d21ac55 | 4143 | lck_mtx_unlock(nfs_request_mutex); |
0a7de745 | 4144 | return ENXIO; |
55e303ae | 4145 | } |
1c79356b | 4146 | |
2d21ac55 A |
4147 | microuptime(&now); |
4148 | if (!req->r_start) { | |
4149 | req->r_start = now.tv_sec; | |
4150 | req->r_lastmsg = now.tv_sec - | |
4151 | ((nmp->nm_tprintf_delay) - (nmp->nm_tprintf_initial_delay)); | |
1c79356b | 4152 | } |
1c79356b | 4153 | |
316670eb | 4154 | OSAddAtomic64(1, &nfsstats.rpcrequests); |
2d21ac55 | 4155 | |
1c79356b A |
4156 | /* |
4157 | * Chain request into list of outstanding requests. Be sure | |
4158 | * to put it LAST so timer finds oldest requests first. | |
2d21ac55 A |
4159 | * Make sure that the request queue timer is running |
4160 | * to check for possible request timeout. | |
1c79356b | 4161 | */ |
2d21ac55 A |
4162 | TAILQ_INSERT_TAIL(&nfs_reqq, req, r_chain); |
4163 | req->r_lflags |= RL_QUEUED; | |
4164 | if (!nfs_request_timer_on) { | |
4165 | nfs_request_timer_on = 1; | |
4166 | nfs_interval_timer_start(nfs_request_timer_call, | |
0a7de745 | 4167 | NFS_REQUESTDELAY); |
2d21ac55 A |
4168 | } |
4169 | lck_mtx_unlock(nfs_request_mutex); | |
1c79356b | 4170 | |
2d21ac55 | 4171 | /* Send the request... */ |
0a7de745 | 4172 | return nfs_send(req, wait); |
2d21ac55 A |
4173 | } |
4174 | ||
4175 | /* | |
4176 | * Call nfs_wait_reply() to wait for the reply. | |
4177 | */ | |
4178 | void | |
4179 | nfs_request_wait(struct nfsreq *req) | |
4180 | { | |
4181 | req->r_error = nfs_wait_reply(req); | |
4182 | } | |
55e303ae | 4183 | |
2d21ac55 A |
4184 | /* |
4185 | * Finish up an NFS request by dequeueing it and | |
4186 | * doing the initial NFS request reply processing. | |
4187 | */ | |
4188 | int | |
4189 | nfs_request_finish( | |
4190 | struct nfsreq *req, | |
4191 | struct nfsm_chain *nmrepp, | |
4192 | int *status) | |
4193 | { | |
4194 | struct nfsmount *nmp; | |
4195 | mbuf_t mrep; | |
4196 | int verf_type = 0; | |
4197 | uint32_t verf_len = 0; | |
4198 | uint32_t reply_status = 0; | |
4199 | uint32_t rejected_status = 0; | |
4200 | uint32_t auth_status = 0; | |
4201 | uint32_t accepted_status = 0; | |
4202 | struct nfsm_chain nmrep; | |
6d2010ae | 4203 | int error, clearjbtimeo; |
1c79356b | 4204 | |
2d21ac55 | 4205 | error = req->r_error; |
1c79356b | 4206 | |
0a7de745 | 4207 | if (nmrepp) { |
2d21ac55 | 4208 | nmrepp->nmc_mhead = NULL; |
0a7de745 | 4209 | } |
1c79356b | 4210 | |
2d21ac55 A |
4211 | /* RPC done, unlink the request. */ |
4212 | nfs_reqdequeue(req); | |
1c79356b | 4213 | |
2d21ac55 | 4214 | mrep = req->r_nmrep.nmc_mhead; |
55e303ae | 4215 | |
5ba3f43e | 4216 | nmp = req->r_nmp; |
1c79356b | 4217 | |
b0d623f7 | 4218 | if ((req->r_flags & R_CWND) && nmp) { |
6d2010ae A |
4219 | /* |
4220 | * Decrement the outstanding request count. | |
4221 | */ | |
2d21ac55 A |
4222 | req->r_flags &= ~R_CWND; |
4223 | lck_mtx_lock(&nmp->nm_lock); | |
4224 | FSDBG(273, R_XID32(req->r_xid), req, nmp->nm_sent, nmp->nm_cwnd); | |
4225 | nmp->nm_sent -= NFS_CWNDSCALE; | |
4226 | if ((nmp->nm_sent < nmp->nm_cwnd) && !TAILQ_EMPTY(&nmp->nm_cwndq)) { | |
4227 | /* congestion window is open, poke the cwnd queue */ | |
4228 | struct nfsreq *req2 = TAILQ_FIRST(&nmp->nm_cwndq); | |
4229 | TAILQ_REMOVE(&nmp->nm_cwndq, req2, r_cchain); | |
4230 | req2->r_cchain.tqe_next = NFSREQNOLIST; | |
4231 | wakeup(req2); | |
55e303ae | 4232 | } |
2d21ac55 | 4233 | lck_mtx_unlock(&nmp->nm_lock); |
1c79356b A |
4234 | } |
4235 | ||
6d2010ae | 4236 | if (nfs_request_using_gss(req)) { |
2d21ac55 | 4237 | /* |
6d2010ae | 4238 | * If the request used an RPCSEC_GSS credential |
2d21ac55 A |
4239 | * then reset its sequence number bit in the |
4240 | * request window. | |
4241 | */ | |
4242 | nfs_gss_clnt_rpcdone(req); | |
4243 | ||
4244 | /* | |
4245 | * If we need to re-send, go back and re-build the | |
4246 | * request based on a new sequence number. | |
4247 | * Note that we're using the original XID. | |
4248 | */ | |
4249 | if (error == EAGAIN) { | |
4250 | req->r_error = 0; | |
0a7de745 | 4251 | if (mrep) { |
2d21ac55 | 4252 | mbuf_freem(mrep); |
0a7de745 A |
4253 | } |
4254 | error = nfs_gss_clnt_args_restore(req); // remove any trailer mbufs | |
2d21ac55 A |
4255 | req->r_nmrep.nmc_mhead = NULL; |
4256 | req->r_flags |= R_RESTART; | |
4257 | if (error == ENEEDAUTH) { | |
0a7de745 | 4258 | req->r_xid = 0; // get a new XID |
2d21ac55 A |
4259 | error = 0; |
4260 | } | |
4261 | goto nfsmout; | |
4262 | } | |
1c79356b A |
4263 | } |
4264 | ||
4265 | /* | |
2d21ac55 A |
4266 | * If there was a successful reply, make sure to mark the mount as up. |
4267 | * If a tprintf message was given (or if this is a timed-out soft mount) | |
4268 | * then post a tprintf message indicating the server is alive again. | |
1c79356b | 4269 | */ |
2d21ac55 A |
4270 | if (!error) { |
4271 | if ((req->r_flags & R_TPRINTFMSG) || | |
fe8ab488 | 4272 | (nmp && (NMFLAG(nmp, SOFT) || (req->r_flags & R_SOFT)) && |
0a7de745 | 4273 | ((nmp->nm_state & (NFSSTA_TIMEO | NFSSTA_FORCE | NFSSTA_DEAD)) == NFSSTA_TIMEO))) { |
2d21ac55 | 4274 | nfs_up(nmp, req->r_thread, NFSSTA_TIMEO, "is alive again"); |
0a7de745 | 4275 | } else { |
2d21ac55 | 4276 | nfs_up(nmp, req->r_thread, NFSSTA_TIMEO, NULL); |
0a7de745 | 4277 | } |
1c79356b | 4278 | } |
0a7de745 | 4279 | if (!error && !nmp) { |
2d21ac55 | 4280 | error = ENXIO; |
0a7de745 | 4281 | } |
2d21ac55 | 4282 | nfsmout_if(error); |
1c79356b A |
4283 | |
4284 | /* | |
2d21ac55 | 4285 | * break down the RPC header and check if ok |
1c79356b | 4286 | */ |
2d21ac55 A |
4287 | nmrep = req->r_nmrep; |
4288 | nfsm_chain_get_32(error, &nmrep, reply_status); | |
4289 | nfsmout_if(error); | |
4290 | if (reply_status == RPC_MSGDENIED) { | |
4291 | nfsm_chain_get_32(error, &nmrep, rejected_status); | |
4292 | nfsmout_if(error); | |
4293 | if (rejected_status == RPC_MISMATCH) { | |
4294 | error = ENOTSUP; | |
1c79356b | 4295 | goto nfsmout; |
2d21ac55 A |
4296 | } |
4297 | nfsm_chain_get_32(error, &nmrep, auth_status); | |
4298 | nfsmout_if(error); | |
4299 | switch (auth_status) { | |
4300 | case RPCSEC_GSS_CREDPROBLEM: | |
4301 | case RPCSEC_GSS_CTXPROBLEM: | |
4302 | /* | |
4303 | * An RPCSEC_GSS cred or context problem. | |
4304 | * We can't use it anymore. | |
4305 | * Restore the args, renew the context | |
4306 | * and set up for a resend. | |
4307 | */ | |
4308 | error = nfs_gss_clnt_args_restore(req); | |
0a7de745 | 4309 | if (error && error != ENEEDAUTH) { |
2d21ac55 | 4310 | break; |
0a7de745 | 4311 | } |
b0d623f7 | 4312 | |
2d21ac55 A |
4313 | if (!error) { |
4314 | error = nfs_gss_clnt_ctx_renew(req); | |
0a7de745 | 4315 | if (error) { |
2d21ac55 | 4316 | break; |
0a7de745 | 4317 | } |
1c79356b | 4318 | } |
2d21ac55 A |
4319 | mbuf_freem(mrep); |
4320 | req->r_nmrep.nmc_mhead = NULL; | |
0a7de745 | 4321 | req->r_xid = 0; // get a new XID |
2d21ac55 A |
4322 | req->r_flags |= R_RESTART; |
4323 | goto nfsmout; | |
4324 | default: | |
4325 | error = EACCES; | |
4326 | break; | |
4327 | } | |
4328 | goto nfsmout; | |
4329 | } | |
4330 | ||
4331 | /* Now check the verifier */ | |
4332 | nfsm_chain_get_32(error, &nmrep, verf_type); // verifier flavor | |
4333 | nfsm_chain_get_32(error, &nmrep, verf_len); // verifier length | |
4334 | nfsmout_if(error); | |
4335 | ||
6d2010ae A |
4336 | switch (req->r_auth) { |
4337 | case RPCAUTH_NONE: | |
4338 | case RPCAUTH_SYS: | |
4339 | /* Any AUTH_SYS verifier is ignored */ | |
0a7de745 | 4340 | if (verf_len > 0) { |
2d21ac55 | 4341 | nfsm_chain_adv(error, &nmrep, nfsm_rndup(verf_len)); |
0a7de745 | 4342 | } |
2d21ac55 A |
4343 | nfsm_chain_get_32(error, &nmrep, accepted_status); |
4344 | break; | |
4345 | case RPCAUTH_KRB5: | |
4346 | case RPCAUTH_KRB5I: | |
4347 | case RPCAUTH_KRB5P: | |
4348 | error = nfs_gss_clnt_verf_get(req, &nmrep, | |
0a7de745 | 4349 | verf_type, verf_len, &accepted_status); |
2d21ac55 A |
4350 | break; |
4351 | } | |
4352 | nfsmout_if(error); | |
4353 | ||
4354 | switch (accepted_status) { | |
4355 | case RPC_SUCCESS: | |
4356 | if (req->r_procnum == NFSPROC_NULL) { | |
4357 | /* | |
4358 | * The NFS null procedure is unique, | |
4359 | * in not returning an NFS status. | |
4360 | */ | |
4361 | *status = NFS_OK; | |
4362 | } else { | |
4363 | nfsm_chain_get_32(error, &nmrep, *status); | |
4364 | nfsmout_if(error); | |
4365 | } | |
1c79356b | 4366 | |
2d21ac55 | 4367 | if ((nmp->nm_vers != NFS_VER2) && (*status == NFSERR_TRYLATER)) { |
1c79356b | 4368 | /* |
2d21ac55 | 4369 | * It's a JUKEBOX error - delay and try again |
1c79356b | 4370 | */ |
6d2010ae | 4371 | int delay, slpflag = (NMFLAG(nmp, INTR) && !(req->r_flags & R_NOINTR)) ? PCATCH : 0; |
2d21ac55 A |
4372 | |
4373 | mbuf_freem(mrep); | |
4374 | req->r_nmrep.nmc_mhead = NULL; | |
4375 | if ((req->r_delay >= 30) && !(nmp->nm_state & NFSSTA_MOUNTED)) { | |
4376 | /* we're not yet completely mounted and */ | |
4377 | /* we can't complete an RPC, so we fail */ | |
316670eb | 4378 | OSAddAtomic64(1, &nfsstats.rpctimeouts); |
2d21ac55 A |
4379 | nfs_softterm(req); |
4380 | error = req->r_error; | |
4381 | goto nfsmout; | |
4382 | } | |
4383 | req->r_delay = !req->r_delay ? NFS_TRYLATERDEL : (req->r_delay * 2); | |
0a7de745 | 4384 | if (req->r_delay > 30) { |
2d21ac55 | 4385 | req->r_delay = 30; |
0a7de745 | 4386 | } |
b0d623f7 A |
4387 | if (nmp->nm_tprintf_initial_delay && (req->r_delay >= nmp->nm_tprintf_initial_delay)) { |
4388 | if (!(req->r_flags & R_JBTPRINTFMSG)) { | |
4389 | req->r_flags |= R_JBTPRINTFMSG; | |
4390 | lck_mtx_lock(&nmp->nm_lock); | |
4391 | nmp->nm_jbreqs++; | |
4392 | lck_mtx_unlock(&nmp->nm_lock); | |
4393 | } | |
2d21ac55 | 4394 | nfs_down(req->r_nmp, req->r_thread, 0, NFSSTA_JUKEBOXTIMEO, |
0a7de745 | 4395 | "resource temporarily unavailable (jukebox)", 0); |
b0d623f7 | 4396 | } |
fe8ab488 | 4397 | if ((NMFLAG(nmp, SOFT) || (req->r_flags & R_SOFT)) && (req->r_delay == 30) && |
0a7de745 | 4398 | !(req->r_flags & R_NOINTR)) { |
b0d623f7 | 4399 | /* for soft mounts, just give up after a short while */ |
316670eb | 4400 | OSAddAtomic64(1, &nfsstats.rpctimeouts); |
b0d623f7 A |
4401 | nfs_softterm(req); |
4402 | error = req->r_error; | |
4403 | goto nfsmout; | |
2d21ac55 A |
4404 | } |
4405 | delay = req->r_delay; | |
4406 | if (req->r_callback.rcb_func) { | |
4407 | struct timeval now; | |
4408 | microuptime(&now); | |
4409 | req->r_resendtime = now.tv_sec + delay; | |
e5568f75 | 4410 | } else { |
2d21ac55 | 4411 | do { |
0a7de745 | 4412 | if ((error = nfs_sigintr(req->r_nmp, req, req->r_thread, 0))) { |
b0d623f7 | 4413 | goto nfsmout; |
0a7de745 A |
4414 | } |
4415 | tsleep(nfs_request_finish, PSOCK | slpflag, "nfs_jukebox_trylater", hz); | |
6d2010ae | 4416 | slpflag = 0; |
2d21ac55 | 4417 | } while (--delay > 0); |
e5568f75 | 4418 | } |
0a7de745 | 4419 | req->r_xid = 0; // get a new XID |
2d21ac55 A |
4420 | req->r_flags |= R_RESTART; |
4421 | req->r_start = 0; | |
4422 | FSDBG(273, R_XID32(req->r_xid), nmp, req, NFSERR_TRYLATER); | |
0a7de745 | 4423 | return 0; |
1c79356b A |
4424 | } |
4425 | ||
b0d623f7 A |
4426 | if (req->r_flags & R_JBTPRINTFMSG) { |
4427 | req->r_flags &= ~R_JBTPRINTFMSG; | |
4428 | lck_mtx_lock(&nmp->nm_lock); | |
4429 | nmp->nm_jbreqs--; | |
4430 | clearjbtimeo = (nmp->nm_jbreqs == 0) ? NFSSTA_JUKEBOXTIMEO : 0; | |
4431 | lck_mtx_unlock(&nmp->nm_lock); | |
4432 | nfs_up(nmp, req->r_thread, clearjbtimeo, "resource available again"); | |
4433 | } | |
2d21ac55 | 4434 | |
6d2010ae A |
4435 | if ((nmp->nm_vers >= NFS_VER4) && (*status == NFSERR_WRONGSEC)) { |
4436 | /* | |
4437 | * Hmmm... we need to try a different security flavor. | |
4438 | * The first time a request hits this, we will allocate an array | |
4439 | * to track flavors to try. We fill the array with the mount's | |
4440 | * preferred flavors or the server's preferred flavors or just the | |
4441 | * flavors we support. | |
4442 | */ | |
4443 | uint32_t srvflavors[NX_MAX_SEC_FLAVORS]; | |
4444 | int srvcount, i, j; | |
4445 | ||
4446 | /* Call SECINFO to try to get list of flavors from server. */ | |
4447 | srvcount = NX_MAX_SEC_FLAVORS; | |
4448 | nfs4_secinfo_rpc(nmp, &req->r_secinfo, req->r_cred, srvflavors, &srvcount); | |
4449 | ||
4450 | if (!req->r_wrongsec) { | |
4451 | /* first time... set up flavor array */ | |
0a7de745 | 4452 | MALLOC(req->r_wrongsec, uint32_t*, NX_MAX_SEC_FLAVORS * sizeof(uint32_t), M_TEMP, M_WAITOK); |
6d2010ae A |
4453 | if (!req->r_wrongsec) { |
4454 | error = EACCES; | |
4455 | goto nfsmout; | |
4456 | } | |
0a7de745 | 4457 | i = 0; |
6d2010ae | 4458 | if (nmp->nm_sec.count) { /* use the mount's preferred list of flavors */ |
0a7de745 | 4459 | for (; i < nmp->nm_sec.count; i++) { |
6d2010ae | 4460 | req->r_wrongsec[i] = nmp->nm_sec.flavors[i]; |
0a7de745 | 4461 | } |
6d2010ae | 4462 | } else if (srvcount) { /* otherwise use the server's list of flavors */ |
0a7de745 | 4463 | for (; i < srvcount; i++) { |
6d2010ae | 4464 | req->r_wrongsec[i] = srvflavors[i]; |
0a7de745 | 4465 | } |
6d2010ae A |
4466 | } else { /* otherwise, just try the flavors we support. */ |
4467 | req->r_wrongsec[i++] = RPCAUTH_KRB5P; | |
4468 | req->r_wrongsec[i++] = RPCAUTH_KRB5I; | |
4469 | req->r_wrongsec[i++] = RPCAUTH_KRB5; | |
4470 | req->r_wrongsec[i++] = RPCAUTH_SYS; | |
4471 | req->r_wrongsec[i++] = RPCAUTH_NONE; | |
4472 | } | |
0a7de745 | 4473 | for (; i < NX_MAX_SEC_FLAVORS; i++) { /* invalidate any remaining slots */ |
6d2010ae | 4474 | req->r_wrongsec[i] = RPCAUTH_INVALID; |
0a7de745 | 4475 | } |
6d2010ae A |
4476 | } |
4477 | ||
4478 | /* clear the current flavor from the list */ | |
0a7de745 A |
4479 | for (i = 0; i < NX_MAX_SEC_FLAVORS; i++) { |
4480 | if (req->r_wrongsec[i] == req->r_auth) { | |
6d2010ae | 4481 | req->r_wrongsec[i] = RPCAUTH_INVALID; |
0a7de745 A |
4482 | } |
4483 | } | |
6d2010ae A |
4484 | |
4485 | /* find the next flavor to try */ | |
0a7de745 | 4486 | for (i = 0; i < NX_MAX_SEC_FLAVORS; i++) { |
6d2010ae | 4487 | if (req->r_wrongsec[i] != RPCAUTH_INVALID) { |
0a7de745 | 4488 | if (!srvcount) { /* no server list, just try it */ |
6d2010ae | 4489 | break; |
0a7de745 | 4490 | } |
6d2010ae | 4491 | /* check that it's in the server's list */ |
0a7de745 A |
4492 | for (j = 0; j < srvcount; j++) { |
4493 | if (req->r_wrongsec[i] == srvflavors[j]) { | |
6d2010ae | 4494 | break; |
0a7de745 A |
4495 | } |
4496 | } | |
4497 | if (j < srvcount) { /* found */ | |
6d2010ae | 4498 | break; |
0a7de745 | 4499 | } |
6d2010ae A |
4500 | /* not found in server list */ |
4501 | req->r_wrongsec[i] = RPCAUTH_INVALID; | |
4502 | } | |
0a7de745 | 4503 | } |
6d2010ae A |
4504 | if (i == NX_MAX_SEC_FLAVORS) { |
4505 | /* nothing left to try! */ | |
4506 | error = EACCES; | |
4507 | goto nfsmout; | |
4508 | } | |
4509 | ||
4510 | /* retry with the next auth flavor */ | |
4511 | req->r_auth = req->r_wrongsec[i]; | |
0a7de745 | 4512 | req->r_xid = 0; // get a new XID |
6d2010ae A |
4513 | req->r_flags |= R_RESTART; |
4514 | req->r_start = 0; | |
4515 | FSDBG(273, R_XID32(req->r_xid), nmp, req, NFSERR_WRONGSEC); | |
0a7de745 | 4516 | return 0; |
6d2010ae A |
4517 | } |
4518 | if ((nmp->nm_vers >= NFS_VER4) && req->r_wrongsec) { | |
4519 | /* | |
4520 | * We renegotiated security for this request; so update the | |
4521 | * default security flavor for the associated node. | |
4522 | */ | |
0a7de745 | 4523 | if (req->r_np) { |
6d2010ae | 4524 | req->r_np->n_auth = req->r_auth; |
0a7de745 | 4525 | } |
6d2010ae A |
4526 | } |
4527 | ||
2d21ac55 A |
4528 | if (*status == NFS_OK) { |
4529 | /* | |
4530 | * Successful NFS request | |
4531 | */ | |
4532 | *nmrepp = nmrep; | |
4533 | req->r_nmrep.nmc_mhead = NULL; | |
4534 | break; | |
4535 | } | |
4536 | /* Got an NFS error of some kind */ | |
4537 | ||
4538 | /* | |
4539 | * If the File Handle was stale, invalidate the | |
4540 | * lookup cache, just in case. | |
4541 | */ | |
6d2010ae | 4542 | if ((*status == ESTALE) && req->r_np) { |
2d21ac55 | 4543 | cache_purge(NFSTOV(req->r_np)); |
6d2010ae | 4544 | /* if monitored, also send delete event */ |
0a7de745 A |
4545 | if (vnode_ismonitored(NFSTOV(req->r_np))) { |
4546 | nfs_vnode_notify(req->r_np, (VNODE_EVENT_ATTRIB | VNODE_EVENT_DELETE)); | |
4547 | } | |
6d2010ae | 4548 | } |
0a7de745 | 4549 | if (nmp->nm_vers == NFS_VER2) { |
2d21ac55 | 4550 | mbuf_freem(mrep); |
0a7de745 | 4551 | } else { |
2d21ac55 | 4552 | *nmrepp = nmrep; |
0a7de745 | 4553 | } |
2d21ac55 A |
4554 | req->r_nmrep.nmc_mhead = NULL; |
4555 | error = 0; | |
4556 | break; | |
4557 | case RPC_PROGUNAVAIL: | |
4558 | error = EPROGUNAVAIL; | |
4559 | break; | |
4560 | case RPC_PROGMISMATCH: | |
4561 | error = ERPCMISMATCH; | |
4562 | break; | |
4563 | case RPC_PROCUNAVAIL: | |
4564 | error = EPROCUNAVAIL; | |
4565 | break; | |
4566 | case RPC_GARBAGE: | |
4567 | error = EBADRPC; | |
4568 | break; | |
4569 | case RPC_SYSTEM_ERR: | |
4570 | default: | |
4571 | error = EIO; | |
4572 | break; | |
1c79356b | 4573 | } |
1c79356b | 4574 | nfsmout: |
b0d623f7 A |
4575 | if (req->r_flags & R_JBTPRINTFMSG) { |
4576 | req->r_flags &= ~R_JBTPRINTFMSG; | |
4577 | lck_mtx_lock(&nmp->nm_lock); | |
4578 | nmp->nm_jbreqs--; | |
4579 | clearjbtimeo = (nmp->nm_jbreqs == 0) ? NFSSTA_JUKEBOXTIMEO : 0; | |
4580 | lck_mtx_unlock(&nmp->nm_lock); | |
0a7de745 | 4581 | if (clearjbtimeo) { |
b0d623f7 | 4582 | nfs_up(nmp, req->r_thread, clearjbtimeo, NULL); |
0a7de745 | 4583 | } |
b0d623f7 | 4584 | } |
2d21ac55 | 4585 | FSDBG(273, R_XID32(req->r_xid), nmp, req, |
0a7de745 A |
4586 | (!error && (*status == NFS_OK)) ? 0xf0f0f0f0 : error); |
4587 | return error; | |
1c79356b A |
4588 | } |
4589 | ||
6d2010ae A |
4590 | /* |
4591 | * NFS request using a GSS/Kerberos security flavor? | |
4592 | */ | |
4593 | int | |
4594 | nfs_request_using_gss(struct nfsreq *req) | |
4595 | { | |
0a7de745 A |
4596 | if (!req->r_gss_ctx) { |
4597 | return 0; | |
4598 | } | |
6d2010ae | 4599 | switch (req->r_auth) { |
0a7de745 A |
4600 | case RPCAUTH_KRB5: |
4601 | case RPCAUTH_KRB5I: | |
4602 | case RPCAUTH_KRB5P: | |
4603 | return 1; | |
6d2010ae | 4604 | } |
0a7de745 | 4605 | return 0; |
6d2010ae | 4606 | } |
2d21ac55 | 4607 | |
1c79356b | 4608 | /* |
2d21ac55 | 4609 | * Perform an NFS request synchronously. |
1c79356b | 4610 | */ |
2d21ac55 | 4611 | |
1c79356b | 4612 | int |
2d21ac55 A |
4613 | nfs_request( |
4614 | nfsnode_t np, | |
0a7de745 | 4615 | mount_t mp, /* used only if !np */ |
2d21ac55 A |
4616 | struct nfsm_chain *nmrest, |
4617 | int procnum, | |
4618 | vfs_context_t ctx, | |
6d2010ae | 4619 | struct nfsreq_secinfo_args *si, |
2d21ac55 A |
4620 | struct nfsm_chain *nmrepp, |
4621 | u_int64_t *xidp, | |
4622 | int *status) | |
1c79356b | 4623 | { |
2d21ac55 | 4624 | return nfs_request2(np, mp, nmrest, procnum, |
0a7de745 A |
4625 | vfs_context_thread(ctx), vfs_context_ucred(ctx), |
4626 | si, 0, nmrepp, xidp, status); | |
2d21ac55 | 4627 | } |
1c79356b | 4628 | |
2d21ac55 A |
4629 | int |
4630 | nfs_request2( | |
4631 | nfsnode_t np, | |
0a7de745 | 4632 | mount_t mp, /* used only if !np */ |
2d21ac55 A |
4633 | struct nfsm_chain *nmrest, |
4634 | int procnum, | |
4635 | thread_t thd, | |
4636 | kauth_cred_t cred, | |
6d2010ae | 4637 | struct nfsreq_secinfo_args *si, |
2d21ac55 A |
4638 | int flags, |
4639 | struct nfsm_chain *nmrepp, | |
4640 | u_int64_t *xidp, | |
4641 | int *status) | |
4642 | { | |
4643 | struct nfsreq rq, *req = &rq; | |
4644 | int error; | |
1c79356b | 4645 | |
0a7de745 A |
4646 | if ((error = nfs_request_create(np, mp, nmrest, procnum, thd, cred, &req))) { |
4647 | return error; | |
4648 | } | |
fe8ab488 | 4649 | req->r_flags |= (flags & (R_OPTMASK | R_SOFT)); |
0a7de745 | 4650 | if (si) { |
6d2010ae | 4651 | req->r_secinfo = *si; |
0a7de745 | 4652 | } |
1c79356b | 4653 | |
2d21ac55 A |
4654 | FSDBG_TOP(273, R_XID32(req->r_xid), np, procnum, 0); |
4655 | do { | |
4656 | req->r_error = 0; | |
4657 | req->r_flags &= ~R_RESTART; | |
0a7de745 | 4658 | if ((error = nfs_request_add_header(req))) { |
1c79356b | 4659 | break; |
0a7de745 A |
4660 | } |
4661 | if (xidp) { | |
2d21ac55 | 4662 | *xidp = req->r_xid; |
0a7de745 A |
4663 | } |
4664 | if ((error = nfs_request_send(req, 1))) { | |
1c79356b | 4665 | break; |
0a7de745 | 4666 | } |
2d21ac55 | 4667 | nfs_request_wait(req); |
0a7de745 | 4668 | if ((error = nfs_request_finish(req, nmrepp, status))) { |
1c79356b | 4669 | break; |
0a7de745 | 4670 | } |
2d21ac55 | 4671 | } while (req->r_flags & R_RESTART); |
91447636 | 4672 | |
2d21ac55 A |
4673 | FSDBG_BOT(273, R_XID32(req->r_xid), np, procnum, error); |
4674 | nfs_request_rele(req); | |
0a7de745 | 4675 | return error; |
1c79356b A |
4676 | } |
4677 | ||
b0d623f7 A |
4678 | |
4679 | /* | |
4680 | * Set up a new null proc request to exchange GSS context tokens with the | |
4681 | * server. Associate the context that we are setting up with the request that we | |
4682 | * are sending. | |
4683 | */ | |
316670eb | 4684 | |
b0d623f7 A |
4685 | int |
4686 | nfs_request_gss( | |
0a7de745 A |
4687 | mount_t mp, |
4688 | struct nfsm_chain *nmrest, | |
4689 | thread_t thd, | |
4690 | kauth_cred_t cred, | |
4691 | int flags, | |
4692 | struct nfs_gss_clnt_ctx *cp, /* Set to gss context to renew or setup */ | |
4693 | struct nfsm_chain *nmrepp, | |
4694 | int *status) | |
b0d623f7 A |
4695 | { |
4696 | struct nfsreq rq, *req = &rq; | |
fe8ab488 | 4697 | int error, wait = 1; |
b0d623f7 | 4698 | |
0a7de745 A |
4699 | if ((error = nfs_request_create(NULL, mp, nmrest, NFSPROC_NULL, thd, cred, &req))) { |
4700 | return error; | |
4701 | } | |
b0d623f7 | 4702 | req->r_flags |= (flags & R_OPTMASK); |
316670eb | 4703 | |
b0d623f7 A |
4704 | if (cp == NULL) { |
4705 | printf("nfs_request_gss request has no context\n"); | |
4706 | nfs_request_rele(req); | |
0a7de745 | 4707 | return NFSERR_EAUTH; |
b0d623f7 A |
4708 | } |
4709 | nfs_gss_clnt_ctx_ref(req, cp); | |
4710 | ||
fe8ab488 A |
4711 | /* |
4712 | * Don't wait for a reply to a context destroy advisory | |
4713 | * to avoid hanging on a dead server. | |
4714 | */ | |
0a7de745 | 4715 | if (cp->gss_clnt_proc == RPCSEC_GSS_DESTROY) { |
fe8ab488 | 4716 | wait = 0; |
0a7de745 | 4717 | } |
fe8ab488 | 4718 | |
b0d623f7 A |
4719 | FSDBG_TOP(273, R_XID32(req->r_xid), NULL, NFSPROC_NULL, 0); |
4720 | do { | |
4721 | req->r_error = 0; | |
4722 | req->r_flags &= ~R_RESTART; | |
0a7de745 | 4723 | if ((error = nfs_request_add_header(req))) { |
b0d623f7 | 4724 | break; |
0a7de745 | 4725 | } |
b0d623f7 | 4726 | |
0a7de745 | 4727 | if ((error = nfs_request_send(req, wait))) { |
b0d623f7 | 4728 | break; |
0a7de745 A |
4729 | } |
4730 | if (!wait) { | |
fe8ab488 | 4731 | break; |
0a7de745 | 4732 | } |
fe8ab488 | 4733 | |
b0d623f7 | 4734 | nfs_request_wait(req); |
0a7de745 | 4735 | if ((error = nfs_request_finish(req, nmrepp, status))) { |
b0d623f7 | 4736 | break; |
0a7de745 | 4737 | } |
b0d623f7 A |
4738 | } while (req->r_flags & R_RESTART); |
4739 | ||
4740 | FSDBG_BOT(273, R_XID32(req->r_xid), NULL, NFSPROC_NULL, error); | |
fe8ab488 A |
4741 | |
4742 | nfs_gss_clnt_ctx_unref(req); | |
b0d623f7 | 4743 | nfs_request_rele(req); |
fe8ab488 | 4744 | |
0a7de745 | 4745 | return error; |
b0d623f7 | 4746 | } |
316670eb | 4747 | |
2d21ac55 A |
4748 | /* |
4749 | * Create and start an asynchronous NFS request. | |
4750 | */ | |
4751 | int | |
4752 | nfs_request_async( | |
4753 | nfsnode_t np, | |
0a7de745 | 4754 | mount_t mp, /* used only if !np */ |
2d21ac55 A |
4755 | struct nfsm_chain *nmrest, |
4756 | int procnum, | |
4757 | thread_t thd, | |
4758 | kauth_cred_t cred, | |
6d2010ae A |
4759 | struct nfsreq_secinfo_args *si, |
4760 | int flags, | |
2d21ac55 A |
4761 | struct nfsreq_cbinfo *cb, |
4762 | struct nfsreq **reqp) | |
4763 | { | |
4764 | struct nfsreq *req; | |
6d2010ae | 4765 | struct nfsmount *nmp; |
2d21ac55 | 4766 | int error, sent; |
1c79356b | 4767 | |
2d21ac55 A |
4768 | error = nfs_request_create(np, mp, nmrest, procnum, thd, cred, reqp); |
4769 | req = *reqp; | |
4770 | FSDBG(274, (req ? R_XID32(req->r_xid) : 0), np, procnum, error); | |
0a7de745 A |
4771 | if (error) { |
4772 | return error; | |
4773 | } | |
6d2010ae | 4774 | req->r_flags |= (flags & R_OPTMASK); |
2d21ac55 | 4775 | req->r_flags |= R_ASYNC; |
0a7de745 | 4776 | if (si) { |
6d2010ae | 4777 | req->r_secinfo = *si; |
0a7de745 A |
4778 | } |
4779 | if (cb) { | |
2d21ac55 | 4780 | req->r_callback = *cb; |
0a7de745 | 4781 | } |
2d21ac55 A |
4782 | error = nfs_request_add_header(req); |
4783 | if (!error) { | |
4784 | req->r_flags |= R_WAITSENT; | |
0a7de745 | 4785 | if (req->r_callback.rcb_func) { |
2d21ac55 | 4786 | nfs_request_ref(req, 0); |
0a7de745 | 4787 | } |
2d21ac55 A |
4788 | error = nfs_request_send(req, 1); |
4789 | lck_mtx_lock(&req->r_mtx); | |
4790 | if (!error && !(req->r_flags & R_SENT) && req->r_callback.rcb_func) { | |
4791 | /* make sure to wait until this async I/O request gets sent */ | |
6d2010ae | 4792 | int slpflag = (req->r_nmp && NMFLAG(req->r_nmp, INTR) && req->r_thread && !(req->r_flags & R_NOINTR)) ? PCATCH : 0; |
2d21ac55 | 4793 | struct timespec ts = { 2, 0 }; |
36401178 | 4794 | while (!(req->r_flags & R_SENT)) { |
fe8ab488 A |
4795 | nmp = req->r_nmp; |
4796 | if ((req->r_flags & R_RESENDQ) && !nfs_mount_gone(nmp)) { | |
6d2010ae A |
4797 | lck_mtx_lock(&nmp->nm_lock); |
4798 | if ((nmp->nm_state & NFSSTA_RECOVER) && (req->r_rchain.tqe_next != NFSREQNOLIST)) { | |
4799 | /* | |
4800 | * It's not going to get off the resend queue if we're in recovery. | |
4801 | * So, just take it off ourselves. We could be holding mount state | |
4802 | * busy and thus holding up the start of recovery. | |
4803 | */ | |
4804 | TAILQ_REMOVE(&nmp->nm_resendq, req, r_rchain); | |
4805 | req->r_rchain.tqe_next = NFSREQNOLIST; | |
0a7de745 | 4806 | if (req->r_flags & R_RESENDQ) { |
6d2010ae | 4807 | req->r_flags &= ~R_RESENDQ; |
0a7de745 | 4808 | } |
6d2010ae A |
4809 | lck_mtx_unlock(&nmp->nm_lock); |
4810 | req->r_flags |= R_SENDING; | |
4811 | lck_mtx_unlock(&req->r_mtx); | |
4812 | error = nfs_send(req, 1); | |
3e170ce0 A |
4813 | /* Remove the R_RESENDQ reference */ |
4814 | nfs_request_rele(req); | |
6d2010ae | 4815 | lck_mtx_lock(&req->r_mtx); |
0a7de745 | 4816 | if (error) { |
6d2010ae | 4817 | break; |
0a7de745 | 4818 | } |
6d2010ae A |
4819 | continue; |
4820 | } | |
4821 | lck_mtx_unlock(&nmp->nm_lock); | |
4822 | } | |
0a7de745 | 4823 | if ((error = nfs_sigintr(req->r_nmp, req, req->r_thread, 0))) { |
2d21ac55 | 4824 | break; |
0a7de745 | 4825 | } |
36401178 A |
4826 | msleep(req, &req->r_mtx, slpflag | (PZERO - 1), "nfswaitsent", &ts); |
4827 | slpflag = 0; | |
2d21ac55 A |
4828 | } |
4829 | } | |
4830 | sent = req->r_flags & R_SENT; | |
4831 | lck_mtx_unlock(&req->r_mtx); | |
fe8ab488 | 4832 | if (error && req->r_callback.rcb_func && !sent) { |
2d21ac55 | 4833 | nfs_request_rele(req); |
fe8ab488 | 4834 | } |
2d21ac55 A |
4835 | } |
4836 | FSDBG(274, R_XID32(req->r_xid), np, procnum, error); | |
0a7de745 | 4837 | if (error || req->r_callback.rcb_func) { |
2d21ac55 | 4838 | nfs_request_rele(req); |
0a7de745 | 4839 | } |
fe8ab488 | 4840 | |
0a7de745 | 4841 | return error; |
2d21ac55 | 4842 | } |
1c79356b A |
4843 | |
4844 | /* | |
2d21ac55 | 4845 | * Wait for and finish an asynchronous NFS request. |
1c79356b | 4846 | */ |
2d21ac55 A |
4847 | int |
4848 | nfs_request_async_finish( | |
4849 | struct nfsreq *req, | |
4850 | struct nfsm_chain *nmrepp, | |
4851 | u_int64_t *xidp, | |
4852 | int *status) | |
1c79356b | 4853 | { |
cf7d32b8 | 4854 | int error = 0, asyncio = req->r_callback.rcb_func ? 1 : 0; |
6d2010ae | 4855 | struct nfsmount *nmp; |
2d21ac55 A |
4856 | |
4857 | lck_mtx_lock(&req->r_mtx); | |
0a7de745 | 4858 | if (!asyncio) { |
2d21ac55 | 4859 | req->r_flags |= R_ASYNCWAIT; |
0a7de745 | 4860 | } |
cf7d32b8 A |
4861 | while (req->r_flags & R_RESENDQ) { /* wait until the request is off the resend queue */ |
4862 | struct timespec ts = { 2, 0 }; | |
fe8ab488 | 4863 | |
6d2010ae A |
4864 | if ((nmp = req->r_nmp)) { |
4865 | lck_mtx_lock(&nmp->nm_lock); | |
4866 | if ((nmp->nm_state & NFSSTA_RECOVER) && (req->r_rchain.tqe_next != NFSREQNOLIST)) { | |
4867 | /* | |
4868 | * It's not going to get off the resend queue if we're in recovery. | |
4869 | * So, just take it off ourselves. We could be holding mount state | |
4870 | * busy and thus holding up the start of recovery. | |
4871 | */ | |
4872 | TAILQ_REMOVE(&nmp->nm_resendq, req, r_rchain); | |
4873 | req->r_rchain.tqe_next = NFSREQNOLIST; | |
0a7de745 | 4874 | if (req->r_flags & R_RESENDQ) { |
6d2010ae | 4875 | req->r_flags &= ~R_RESENDQ; |
0a7de745 | 4876 | } |
3e170ce0 A |
4877 | /* Remove the R_RESENDQ reference */ |
4878 | assert(req->r_refs > 0); | |
4879 | req->r_refs--; | |
6d2010ae A |
4880 | lck_mtx_unlock(&nmp->nm_lock); |
4881 | break; | |
4882 | } | |
4883 | lck_mtx_unlock(&nmp->nm_lock); | |
4884 | } | |
0a7de745 | 4885 | if ((error = nfs_sigintr(req->r_nmp, req, req->r_thread, 0))) { |
cf7d32b8 | 4886 | break; |
0a7de745 A |
4887 | } |
4888 | msleep(req, &req->r_mtx, PZERO - 1, "nfsresendqwait", &ts); | |
cf7d32b8 | 4889 | } |
2d21ac55 A |
4890 | lck_mtx_unlock(&req->r_mtx); |
4891 | ||
cf7d32b8 A |
4892 | if (!error) { |
4893 | nfs_request_wait(req); | |
4894 | error = nfs_request_finish(req, nmrepp, status); | |
4895 | } | |
2d21ac55 A |
4896 | |
4897 | while (!error && (req->r_flags & R_RESTART)) { | |
3e170ce0 A |
4898 | if (asyncio) { |
4899 | assert(req->r_achain.tqe_next == NFSREQNOLIST); | |
2d21ac55 | 4900 | lck_mtx_lock(&req->r_mtx); |
3e170ce0 A |
4901 | req->r_flags &= ~R_IOD; |
4902 | if (req->r_resendtime) { /* send later */ | |
4903 | nfs_asyncio_resend(req); | |
4904 | lck_mtx_unlock(&req->r_mtx); | |
0a7de745 | 4905 | return EINPROGRESS; |
3e170ce0 | 4906 | } |
2d21ac55 | 4907 | lck_mtx_unlock(&req->r_mtx); |
2d21ac55 A |
4908 | } |
4909 | req->r_error = 0; | |
4910 | req->r_flags &= ~R_RESTART; | |
0a7de745 | 4911 | if ((error = nfs_request_add_header(req))) { |
2d21ac55 | 4912 | break; |
0a7de745 A |
4913 | } |
4914 | if ((error = nfs_request_send(req, !asyncio))) { | |
2d21ac55 | 4915 | break; |
0a7de745 A |
4916 | } |
4917 | if (asyncio) { | |
4918 | return EINPROGRESS; | |
4919 | } | |
2d21ac55 | 4920 | nfs_request_wait(req); |
0a7de745 | 4921 | if ((error = nfs_request_finish(req, nmrepp, status))) { |
2d21ac55 | 4922 | break; |
0a7de745 | 4923 | } |
1c79356b | 4924 | } |
0a7de745 | 4925 | if (xidp) { |
2d21ac55 | 4926 | *xidp = req->r_xid; |
0a7de745 | 4927 | } |
2d21ac55 A |
4928 | |
4929 | FSDBG(275, R_XID32(req->r_xid), req->r_np, req->r_procnum, error); | |
4930 | nfs_request_rele(req); | |
0a7de745 | 4931 | return error; |
1c79356b A |
4932 | } |
4933 | ||
2d21ac55 A |
4934 | /* |
4935 | * Cancel a pending asynchronous NFS request. | |
4936 | */ | |
1c79356b | 4937 | void |
2d21ac55 | 4938 | nfs_request_async_cancel(struct nfsreq *req) |
1c79356b | 4939 | { |
2d21ac55 A |
4940 | FSDBG(275, R_XID32(req->r_xid), req->r_np, req->r_procnum, 0xD1ED1E); |
4941 | nfs_request_rele(req); | |
1c79356b A |
4942 | } |
4943 | ||
55e303ae | 4944 | /* |
2d21ac55 | 4945 | * Flag a request as being terminated. |
55e303ae | 4946 | */ |
b0d623f7 | 4947 | void |
2d21ac55 | 4948 | nfs_softterm(struct nfsreq *req) |
55e303ae | 4949 | { |
2d21ac55 A |
4950 | struct nfsmount *nmp = req->r_nmp; |
4951 | req->r_flags |= R_SOFTTERM; | |
4952 | req->r_error = ETIMEDOUT; | |
0a7de745 | 4953 | if (!(req->r_flags & R_CWND) || nfs_mount_gone(nmp)) { |
2d21ac55 | 4954 | return; |
0a7de745 | 4955 | } |
2d21ac55 A |
4956 | /* update congestion window */ |
4957 | req->r_flags &= ~R_CWND; | |
4958 | lck_mtx_lock(&nmp->nm_lock); | |
4959 | FSDBG(532, R_XID32(req->r_xid), req, nmp->nm_sent, nmp->nm_cwnd); | |
4960 | nmp->nm_sent -= NFS_CWNDSCALE; | |
4961 | if ((nmp->nm_sent < nmp->nm_cwnd) && !TAILQ_EMPTY(&nmp->nm_cwndq)) { | |
4962 | /* congestion window is open, poke the cwnd queue */ | |
4963 | struct nfsreq *req2 = TAILQ_FIRST(&nmp->nm_cwndq); | |
4964 | TAILQ_REMOVE(&nmp->nm_cwndq, req2, r_cchain); | |
4965 | req2->r_cchain.tqe_next = NFSREQNOLIST; | |
4966 | wakeup(req2); | |
4967 | } | |
4968 | lck_mtx_unlock(&nmp->nm_lock); | |
4969 | } | |
55e303ae | 4970 | |
2d21ac55 A |
4971 | /* |
4972 | * Ensure req isn't in use by the timer, then dequeue it. | |
4973 | */ | |
b0d623f7 | 4974 | void |
2d21ac55 A |
4975 | nfs_reqdequeue(struct nfsreq *req) |
4976 | { | |
4977 | lck_mtx_lock(nfs_request_mutex); | |
4978 | while (req->r_lflags & RL_BUSY) { | |
4979 | req->r_lflags |= RL_WAITING; | |
4980 | msleep(&req->r_lflags, nfs_request_mutex, PSOCK, "reqdeq", NULL); | |
4981 | } | |
4982 | if (req->r_lflags & RL_QUEUED) { | |
4983 | TAILQ_REMOVE(&nfs_reqq, req, r_chain); | |
4984 | req->r_lflags &= ~RL_QUEUED; | |
55e303ae | 4985 | } |
2d21ac55 | 4986 | lck_mtx_unlock(nfs_request_mutex); |
55e303ae A |
4987 | } |
4988 | ||
4989 | /* | |
4990 | * Busy (lock) a nfsreq, used by the nfs timer to make sure it's not | |
4991 | * free()'d out from under it. | |
4992 | */ | |
b0d623f7 | 4993 | void |
2d21ac55 | 4994 | nfs_reqbusy(struct nfsreq *req) |
55e303ae | 4995 | { |
0a7de745 | 4996 | if (req->r_lflags & RL_BUSY) { |
2d21ac55 | 4997 | panic("req locked"); |
0a7de745 | 4998 | } |
2d21ac55 | 4999 | req->r_lflags |= RL_BUSY; |
55e303ae A |
5000 | } |
5001 | ||
5002 | /* | |
5003 | * Unbusy the nfsreq passed in, return the next nfsreq in the chain busied. | |
5004 | */ | |
b0d623f7 | 5005 | struct nfsreq * |
2d21ac55 | 5006 | nfs_reqnext(struct nfsreq *req) |
55e303ae | 5007 | { |
2d21ac55 | 5008 | struct nfsreq * nextreq; |
55e303ae | 5009 | |
0a7de745 A |
5010 | if (req == NULL) { |
5011 | return NULL; | |
5012 | } | |
55e303ae A |
5013 | /* |
5014 | * We need to get and busy the next req before signalling the | |
5015 | * current one, otherwise wakeup() may block us and we'll race to | |
5016 | * grab the next req. | |
5017 | */ | |
2d21ac55 | 5018 | nextreq = TAILQ_NEXT(req, r_chain); |
0a7de745 | 5019 | if (nextreq != NULL) { |
2d21ac55 | 5020 | nfs_reqbusy(nextreq); |
0a7de745 | 5021 | } |
55e303ae | 5022 | /* unbusy and signal. */ |
2d21ac55 A |
5023 | req->r_lflags &= ~RL_BUSY; |
5024 | if (req->r_lflags & RL_WAITING) { | |
5025 | req->r_lflags &= ~RL_WAITING; | |
5026 | wakeup(&req->r_lflags); | |
55e303ae | 5027 | } |
0a7de745 | 5028 | return nextreq; |
55e303ae A |
5029 | } |
5030 | ||
1c79356b | 5031 | /* |
2d21ac55 A |
5032 | * NFS request queue timer routine |
5033 | * | |
5034 | * Scan the NFS request queue for any requests that have timed out. | |
5035 | * | |
5036 | * Alert the system of unresponsive servers. | |
5037 | * Mark expired requests on soft mounts as terminated. | |
5038 | * For UDP, mark/signal requests for retransmission. | |
1c79356b A |
5039 | */ |
5040 | void | |
2d21ac55 | 5041 | nfs_request_timer(__unused void *param0, __unused void *param1) |
1c79356b | 5042 | { |
2d21ac55 | 5043 | struct nfsreq *req; |
91447636 | 5044 | struct nfsmount *nmp; |
2d21ac55 | 5045 | int timeo, maxtime, finish_asyncio, error; |
55e303ae | 5046 | struct timeval now; |
2d21ac55 | 5047 | TAILQ_HEAD(nfs_mount_pokeq, nfsmount) nfs_mount_poke_queue; |
d9a64523 | 5048 | TAILQ_INIT(&nfs_mount_poke_queue); |
2d21ac55 | 5049 | |
fe8ab488 | 5050 | restart: |
2d21ac55 A |
5051 | lck_mtx_lock(nfs_request_mutex); |
5052 | req = TAILQ_FIRST(&nfs_reqq); | |
0a7de745 | 5053 | if (req == NULL) { /* no requests - turn timer off */ |
2d21ac55 A |
5054 | nfs_request_timer_on = 0; |
5055 | lck_mtx_unlock(nfs_request_mutex); | |
5056 | return; | |
5057 | } | |
5058 | ||
5059 | nfs_reqbusy(req); | |
1c79356b | 5060 | |
55e303ae | 5061 | microuptime(&now); |
0a7de745 | 5062 | for (; req != NULL; req = nfs_reqnext(req)) { |
2d21ac55 | 5063 | nmp = req->r_nmp; |
fe8ab488 A |
5064 | if (nmp == NULL) { |
5065 | NFS_SOCK_DBG("Found a request with out a mount!\n"); | |
1c79356b | 5066 | continue; |
fe8ab488 | 5067 | } |
0a7de745 | 5068 | if (req->r_error || req->r_nmrep.nmc_mhead) { |
1c79356b | 5069 | continue; |
0a7de745 | 5070 | } |
2d21ac55 A |
5071 | if ((error = nfs_sigintr(nmp, req, req->r_thread, 0))) { |
5072 | if (req->r_callback.rcb_func != NULL) { | |
5073 | /* async I/O RPC needs to be finished */ | |
5074 | lck_mtx_lock(&req->r_mtx); | |
5075 | req->r_error = error; | |
5076 | finish_asyncio = !(req->r_flags & R_WAITSENT); | |
5077 | wakeup(req); | |
5078 | lck_mtx_unlock(&req->r_mtx); | |
0a7de745 | 5079 | if (finish_asyncio) { |
2d21ac55 | 5080 | nfs_asyncio_finish(req); |
0a7de745 | 5081 | } |
2d21ac55 A |
5082 | } |
5083 | continue; | |
5084 | } | |
5085 | ||
5086 | lck_mtx_lock(&req->r_mtx); | |
5087 | ||
5088 | if (nmp->nm_tprintf_initial_delay && | |
5089 | ((req->r_rexmit > 2) || (req->r_flags & R_RESENDERR)) && | |
5090 | ((req->r_lastmsg + nmp->nm_tprintf_delay) < now.tv_sec)) { | |
5091 | req->r_lastmsg = now.tv_sec; | |
5092 | nfs_down(req->r_nmp, req->r_thread, 0, NFSSTA_TIMEO, | |
0a7de745 | 5093 | "not responding", 1); |
2d21ac55 A |
5094 | req->r_flags |= R_TPRINTFMSG; |
5095 | lck_mtx_lock(&nmp->nm_lock); | |
4a249263 | 5096 | if (!(nmp->nm_state & NFSSTA_MOUNTED)) { |
2d21ac55 | 5097 | lck_mtx_unlock(&nmp->nm_lock); |
4a249263 A |
5098 | /* we're not yet completely mounted and */ |
5099 | /* we can't complete an RPC, so we fail */ | |
316670eb | 5100 | OSAddAtomic64(1, &nfsstats.rpctimeouts); |
2d21ac55 A |
5101 | nfs_softterm(req); |
5102 | finish_asyncio = ((req->r_callback.rcb_func != NULL) && !(req->r_flags & R_WAITSENT)); | |
5103 | wakeup(req); | |
5104 | lck_mtx_unlock(&req->r_mtx); | |
0a7de745 | 5105 | if (finish_asyncio) { |
2d21ac55 | 5106 | nfs_asyncio_finish(req); |
0a7de745 | 5107 | } |
4a249263 A |
5108 | continue; |
5109 | } | |
2d21ac55 | 5110 | lck_mtx_unlock(&nmp->nm_lock); |
1c79356b | 5111 | } |
2d21ac55 | 5112 | |
1c79356b | 5113 | /* |
2d21ac55 A |
5114 | * Put a reasonable limit on the maximum timeout, |
5115 | * and reduce that limit when soft mounts get timeouts or are in reconnect. | |
1c79356b | 5116 | */ |
0a7de745 | 5117 | if (!(NMFLAG(nmp, SOFT) || (req->r_flags & R_SOFT)) && !nfs_can_squish(nmp)) { |
2d21ac55 | 5118 | maxtime = NFS_MAXTIMEO; |
0a7de745 A |
5119 | } else if ((req->r_flags & (R_SETUP | R_RECOVER)) || |
5120 | ((nmp->nm_reconnect_start <= 0) || ((now.tv_sec - nmp->nm_reconnect_start) < 8))) { | |
5121 | maxtime = (NFS_MAXTIMEO / (nmp->nm_timeouts + 1)) / 2; | |
5122 | } else { | |
5123 | maxtime = NFS_MINTIMEO / 4; | |
5124 | } | |
1c79356b A |
5125 | |
5126 | /* | |
2d21ac55 | 5127 | * Check for request timeout. |
1c79356b | 5128 | */ |
2d21ac55 A |
5129 | if (req->r_rtt >= 0) { |
5130 | req->r_rtt++; | |
5131 | lck_mtx_lock(&nmp->nm_lock); | |
5132 | if (req->r_flags & R_RESENDERR) { | |
5133 | /* with resend errors, retry every few seconds */ | |
0a7de745 | 5134 | timeo = 4 * hz; |
1c79356b | 5135 | } else { |
0a7de745 | 5136 | if (req->r_procnum == NFSPROC_NULL && req->r_gss_ctx != NULL) { |
2d21ac55 | 5137 | timeo = NFS_MINIDEMTIMEO; // gss context setup |
0a7de745 | 5138 | } else if (NMFLAG(nmp, DUMBTIMER)) { |
2d21ac55 | 5139 | timeo = nmp->nm_timeo; |
0a7de745 | 5140 | } else { |
2d21ac55 | 5141 | timeo = NFS_RTO(nmp, proct[req->r_procnum]); |
0a7de745 | 5142 | } |
1c79356b | 5143 | |
2d21ac55 | 5144 | /* ensure 62.5 ms floor */ |
0a7de745 | 5145 | while (16 * timeo < hz) { |
2d21ac55 | 5146 | timeo *= 2; |
0a7de745 A |
5147 | } |
5148 | if (nmp->nm_timeouts > 0) { | |
2d21ac55 | 5149 | timeo *= nfs_backoff[nmp->nm_timeouts - 1]; |
0a7de745 | 5150 | } |
2d21ac55 A |
5151 | } |
5152 | /* limit timeout to max */ | |
0a7de745 | 5153 | if (timeo > maxtime) { |
2d21ac55 | 5154 | timeo = maxtime; |
0a7de745 | 5155 | } |
2d21ac55 | 5156 | if (req->r_rtt <= timeo) { |
fe8ab488 | 5157 | NFS_SOCK_DBG("nfs timeout: req time %d and timeo is %d continue\n", req->r_rtt, timeo); |
2d21ac55 A |
5158 | lck_mtx_unlock(&nmp->nm_lock); |
5159 | lck_mtx_unlock(&req->r_mtx); | |
5160 | continue; | |
91447636 | 5161 | } |
2d21ac55 | 5162 | /* The request has timed out */ |
39236c6e | 5163 | NFS_SOCK_DBG("nfs timeout: proc %d %d xid %llx rtt %d to %d # %d, t %ld/%d\n", |
0a7de745 A |
5164 | req->r_procnum, proct[req->r_procnum], |
5165 | req->r_xid, req->r_rtt, timeo, nmp->nm_timeouts, | |
5166 | (now.tv_sec - req->r_start) * NFS_HZ, maxtime); | |
5167 | if (nmp->nm_timeouts < 8) { | |
2d21ac55 | 5168 | nmp->nm_timeouts++; |
0a7de745 | 5169 | } |
fe8ab488 A |
5170 | if (nfs_mount_check_dead_timeout(nmp)) { |
5171 | /* Unbusy this request */ | |
5172 | req->r_lflags &= ~RL_BUSY; | |
5173 | if (req->r_lflags & RL_WAITING) { | |
5174 | req->r_lflags &= ~RL_WAITING; | |
5175 | wakeup(&req->r_lflags); | |
5176 | } | |
5177 | lck_mtx_unlock(&req->r_mtx); | |
5178 | ||
0a7de745 | 5179 | /* No need to poke this mount */ |
fe8ab488 A |
5180 | if (nmp->nm_sockflags & NMSOCK_POKE) { |
5181 | nmp->nm_sockflags &= ~NMSOCK_POKE; | |
5182 | TAILQ_REMOVE(&nfs_mount_poke_queue, nmp, nm_pokeq); | |
5183 | } | |
5184 | /* Release our lock state, so we can become a zombie */ | |
5185 | lck_mtx_unlock(nfs_request_mutex); | |
5186 | ||
5187 | /* | |
5188 | * Note nfs_mount_make zombie(nmp) must be | |
5189 | * called with nm_lock held. After doing some | |
5190 | * work we release nm_lock in | |
5191 | * nfs_make_mount_zombie with out acquiring any | |
5192 | * other locks. (Later, in nfs_mount_zombie we | |
5193 | * will acquire nfs_request_mutex, r_mtx, | |
5194 | * nm_lock in that order). So we should not be | |
5195 | * introducing deadlock here. We take a reference | |
5196 | * on the mount so that its still there when we | |
5197 | * release the lock. | |
5198 | */ | |
5199 | nmp->nm_ref++; | |
5200 | nfs_mount_make_zombie(nmp); | |
5201 | lck_mtx_unlock(&nmp->nm_lock); | |
5202 | nfs_mount_rele(nmp); | |
5203 | ||
5204 | /* | |
5205 | * All the request for this mount have now been | |
5206 | * removed from the request queue. Restart to | |
5207 | * process the remaining mounts | |
5208 | */ | |
5209 | goto restart; | |
5210 | } | |
0a7de745 | 5211 | |
2d21ac55 A |
5212 | /* if it's been a few seconds, try poking the socket */ |
5213 | if ((nmp->nm_sotype == SOCK_STREAM) && | |
5214 | ((now.tv_sec - req->r_start) >= 3) && | |
0a7de745 | 5215 | !(nmp->nm_sockflags & (NMSOCK_POKE | NMSOCK_UNMOUNT)) && |
6d2010ae | 5216 | (nmp->nm_sockflags & NMSOCK_READY)) { |
2d21ac55 | 5217 | nmp->nm_sockflags |= NMSOCK_POKE; |
39037602 A |
5218 | /* |
5219 | * We take a ref on the mount so that we know the mount will still be there | |
5220 | * when we process the nfs_mount_poke_queue. An unmount request will block | |
5221 | * in nfs_mount_drain_and_cleanup until after the poke is finished. We release | |
5222 | * the reference after calling nfs_sock_poke below; | |
5223 | */ | |
5224 | nmp->nm_ref++; | |
2d21ac55 A |
5225 | TAILQ_INSERT_TAIL(&nfs_mount_poke_queue, nmp, nm_pokeq); |
5226 | } | |
5227 | lck_mtx_unlock(&nmp->nm_lock); | |
5228 | } | |
1c79356b | 5229 | |
b0d623f7 | 5230 | /* For soft mounts (& SETUPs/RECOVERs), check for too many retransmits/timeout. */ |
0a7de745 | 5231 | if ((NMFLAG(nmp, SOFT) || (req->r_flags & (R_SETUP | R_RECOVER | R_SOFT))) && |
2d21ac55 | 5232 | ((req->r_rexmit >= req->r_retry) || /* too many */ |
0a7de745 | 5233 | ((now.tv_sec - req->r_start) * NFS_HZ > maxtime))) { /* too long */ |
316670eb | 5234 | OSAddAtomic64(1, &nfsstats.rpctimeouts); |
2d21ac55 A |
5235 | lck_mtx_lock(&nmp->nm_lock); |
5236 | if (!(nmp->nm_state & NFSSTA_TIMEO)) { | |
5237 | lck_mtx_unlock(&nmp->nm_lock); | |
5238 | /* make sure we note the unresponsive server */ | |
5239 | /* (maxtime may be less than tprintf delay) */ | |
5240 | nfs_down(req->r_nmp, req->r_thread, 0, NFSSTA_TIMEO, | |
0a7de745 | 5241 | "not responding", 1); |
2d21ac55 A |
5242 | req->r_lastmsg = now.tv_sec; |
5243 | req->r_flags |= R_TPRINTFMSG; | |
5244 | } else { | |
5245 | lck_mtx_unlock(&nmp->nm_lock); | |
5246 | } | |
6d2010ae A |
5247 | if (req->r_flags & R_NOINTR) { |
5248 | /* don't terminate nointr requests on timeout */ | |
5249 | lck_mtx_unlock(&req->r_mtx); | |
5250 | continue; | |
5251 | } | |
39236c6e | 5252 | NFS_SOCK_DBG("nfs timer TERMINATE: p %d x 0x%llx f 0x%x rtt %d t %ld\n", |
0a7de745 A |
5253 | req->r_procnum, req->r_xid, req->r_flags, req->r_rtt, |
5254 | now.tv_sec - req->r_start); | |
2d21ac55 A |
5255 | nfs_softterm(req); |
5256 | finish_asyncio = ((req->r_callback.rcb_func != NULL) && !(req->r_flags & R_WAITSENT)); | |
5257 | wakeup(req); | |
5258 | lck_mtx_unlock(&req->r_mtx); | |
0a7de745 | 5259 | if (finish_asyncio) { |
2d21ac55 | 5260 | nfs_asyncio_finish(req); |
0a7de745 | 5261 | } |
2d21ac55 A |
5262 | continue; |
5263 | } | |
1c79356b | 5264 | |
2d21ac55 A |
5265 | /* for TCP, only resend if explicitly requested */ |
5266 | if ((nmp->nm_sotype == SOCK_STREAM) && !(req->r_flags & R_MUSTRESEND)) { | |
0a7de745 | 5267 | if (++req->r_rexmit > NFS_MAXREXMIT) { |
2d21ac55 | 5268 | req->r_rexmit = NFS_MAXREXMIT; |
0a7de745 | 5269 | } |
2d21ac55 A |
5270 | req->r_rtt = 0; |
5271 | lck_mtx_unlock(&req->r_mtx); | |
5272 | continue; | |
1c79356b | 5273 | } |
483a1d10 | 5274 | |
483a1d10 | 5275 | /* |
2d21ac55 A |
5276 | * The request needs to be (re)sent. Kick the requester to resend it. |
5277 | * (unless it's already marked as needing a resend) | |
483a1d10 | 5278 | */ |
2d21ac55 A |
5279 | if ((req->r_flags & R_MUSTRESEND) && (req->r_rtt == -1)) { |
5280 | lck_mtx_unlock(&req->r_mtx); | |
5281 | continue; | |
5282 | } | |
39236c6e | 5283 | NFS_SOCK_DBG("nfs timer mark resend: p %d x 0x%llx f 0x%x rtt %d\n", |
0a7de745 | 5284 | req->r_procnum, req->r_xid, req->r_flags, req->r_rtt); |
2d21ac55 A |
5285 | req->r_flags |= R_MUSTRESEND; |
5286 | req->r_rtt = -1; | |
5287 | wakeup(req); | |
0a7de745 | 5288 | if ((req->r_flags & (R_IOD | R_ASYNC | R_ASYNCWAIT | R_SENDING)) == R_ASYNC) { |
2d21ac55 | 5289 | nfs_asyncio_resend(req); |
0a7de745 | 5290 | } |
2d21ac55 A |
5291 | lck_mtx_unlock(&req->r_mtx); |
5292 | } | |
5293 | ||
5294 | lck_mtx_unlock(nfs_request_mutex); | |
5295 | ||
5296 | /* poke any sockets */ | |
5297 | while ((nmp = TAILQ_FIRST(&nfs_mount_poke_queue))) { | |
5298 | TAILQ_REMOVE(&nfs_mount_poke_queue, nmp, nm_pokeq); | |
5299 | nfs_sock_poke(nmp); | |
39037602 | 5300 | nfs_mount_rele(nmp); |
2d21ac55 A |
5301 | } |
5302 | ||
5303 | nfs_interval_timer_start(nfs_request_timer_call, NFS_REQUESTDELAY); | |
1c79356b A |
5304 | } |
5305 | ||
2d21ac55 A |
5306 | /* |
5307 | * check a thread's proc for the "noremotehang" flag. | |
5308 | */ | |
5309 | int | |
5310 | nfs_noremotehang(thread_t thd) | |
5311 | { | |
5312 | proc_t p = thd ? get_bsdthreadtask_info(thd) : NULL; | |
0a7de745 | 5313 | return p && proc_noremotehang(p); |
2d21ac55 | 5314 | } |
1c79356b A |
5315 | |
5316 | /* | |
5317 | * Test for a termination condition pending on the process. | |
55e303ae | 5318 | * This is used to determine if we need to bail on a mount. |
2d21ac55 | 5319 | * ETIMEDOUT is returned if there has been a soft timeout. |
55e303ae A |
5320 | * EINTR is returned if there is a signal pending that is not being ignored |
5321 | * and the mount is interruptable, or if we are a thread that is in the process | |
5322 | * of cancellation (also SIGKILL posted). | |
1c79356b | 5323 | */ |
0a7de745 | 5324 | extern int sigprop[NSIG + 1]; |
1c79356b | 5325 | int |
2d21ac55 | 5326 | nfs_sigintr(struct nfsmount *nmp, struct nfsreq *req, thread_t thd, int nmplocked) |
1c79356b | 5327 | { |
0b4c1975 | 5328 | proc_t p; |
2d21ac55 | 5329 | int error = 0; |
55e303ae | 5330 | |
0a7de745 A |
5331 | if (!nmp) { |
5332 | return ENXIO; | |
5333 | } | |
2d21ac55 | 5334 | |
0a7de745 A |
5335 | if (req && (req->r_flags & R_SOFTTERM)) { |
5336 | return ETIMEDOUT; /* request has been terminated. */ | |
5337 | } | |
5338 | if (req && (req->r_flags & R_NOINTR)) { | |
6d2010ae | 5339 | thd = NULL; /* don't check for signal on R_NOINTR */ |
0a7de745 A |
5340 | } |
5341 | if (!nmplocked) { | |
2d21ac55 | 5342 | lck_mtx_lock(&nmp->nm_lock); |
0a7de745 | 5343 | } |
6d2010ae A |
5344 | if (nmp->nm_state & NFSSTA_FORCE) { |
5345 | /* If a force unmount is in progress then fail. */ | |
2d21ac55 | 5346 | error = EIO; |
fe8ab488 | 5347 | } else if (vfs_isforce(nmp->nm_mountp)) { |
55e303ae | 5348 | /* Someone is unmounting us, go soft and mark it. */ |
6d2010ae | 5349 | NFS_BITMAP_SET(nmp->nm_flags, NFS_MFLAG_SOFT); |
2d21ac55 | 5350 | nmp->nm_state |= NFSSTA_FORCE; |
55e303ae | 5351 | } |
2d21ac55 | 5352 | |
b0d623f7 | 5353 | /* Check if the mount is marked dead. */ |
0a7de745 | 5354 | if (!error && (nmp->nm_state & NFSSTA_DEAD)) { |
b0d623f7 | 5355 | error = ENXIO; |
0a7de745 | 5356 | } |
b0d623f7 | 5357 | |
2d21ac55 A |
5358 | /* |
5359 | * If the mount is hung and we've requested not to hang | |
5360 | * on remote filesystems, then bail now. | |
5361 | */ | |
39236c6e | 5362 | if (current_proc() != kernproc && |
0a7de745 | 5363 | !error && (nmp->nm_state & NFSSTA_TIMEO) && nfs_noremotehang(thd)) { |
2d21ac55 | 5364 | error = EIO; |
0a7de745 | 5365 | } |
2d21ac55 | 5366 | |
0a7de745 | 5367 | if (!nmplocked) { |
2d21ac55 | 5368 | lck_mtx_unlock(&nmp->nm_lock); |
0a7de745 A |
5369 | } |
5370 | if (error) { | |
5371 | return error; | |
5372 | } | |
2d21ac55 A |
5373 | |
5374 | /* may not have a thread for async I/O */ | |
0a7de745 A |
5375 | if (thd == NULL || current_proc() == kernproc) { |
5376 | return 0; | |
5377 | } | |
1c79356b | 5378 | |
6d2010ae A |
5379 | /* |
5380 | * Check if the process is aborted, but don't interrupt if we | |
5381 | * were killed by a signal and this is the exiting thread which | |
5382 | * is attempting to dump core. | |
5383 | */ | |
5384 | if (((p = current_proc()) != kernproc) && current_thread_aborted() && | |
5385 | (!(p->p_acflag & AXSIG) || (p->exit_thread != current_thread()) || | |
0a7de745 A |
5386 | (p->p_sigacts == NULL) || |
5387 | (p->p_sigacts->ps_sig < 1) || (p->p_sigacts->ps_sig > NSIG) || | |
5388 | !(sigprop[p->p_sigacts->ps_sig] & SA_CORE))) { | |
5389 | return EINTR; | |
5390 | } | |
91447636 | 5391 | |
2d21ac55 | 5392 | /* mask off thread and process blocked signals. */ |
6d2010ae | 5393 | if (NMFLAG(nmp, INTR) && ((p = get_bsdthreadtask_info(thd))) && |
0a7de745 A |
5394 | proc_pendingsignals(p, NFSINT_SIGMASK)) { |
5395 | return EINTR; | |
5396 | } | |
5397 | return 0; | |
1c79356b A |
5398 | } |
5399 | ||
5400 | /* | |
5401 | * Lock a socket against others. | |
5402 | * Necessary for STREAM sockets to ensure you get an entire rpc request/reply | |
5403 | * and also to avoid race conditions between the processes with nfs requests | |
5404 | * in progress when a reconnect is necessary. | |
5405 | */ | |
5406 | int | |
2d21ac55 | 5407 | nfs_sndlock(struct nfsreq *req) |
1c79356b | 5408 | { |
2d21ac55 | 5409 | struct nfsmount *nmp = req->r_nmp; |
91447636 | 5410 | int *statep; |
2d21ac55 A |
5411 | int error = 0, slpflag = 0; |
5412 | struct timespec ts = { 0, 0 }; | |
1c79356b | 5413 | |
0a7de745 A |
5414 | if (nfs_mount_gone(nmp)) { |
5415 | return ENXIO; | |
5416 | } | |
55e303ae | 5417 | |
2d21ac55 A |
5418 | lck_mtx_lock(&nmp->nm_lock); |
5419 | statep = &nmp->nm_state; | |
5420 | ||
0a7de745 | 5421 | if (NMFLAG(nmp, INTR) && req->r_thread && !(req->r_flags & R_NOINTR)) { |
55e303ae | 5422 | slpflag = PCATCH; |
0a7de745 | 5423 | } |
36401178 | 5424 | while (*statep & NFSSTA_SNDLOCK) { |
0a7de745 | 5425 | if ((error = nfs_sigintr(nmp, req, req->r_thread, 1))) { |
2d21ac55 | 5426 | break; |
0a7de745 | 5427 | } |
55e303ae | 5428 | *statep |= NFSSTA_WANTSND; |
0a7de745 | 5429 | if (nfs_noremotehang(req->r_thread)) { |
2d21ac55 | 5430 | ts.tv_sec = 1; |
0a7de745 | 5431 | } |
36401178 | 5432 | msleep(statep, &nmp->nm_lock, slpflag | (PZERO - 1), "nfsndlck", &ts); |
1c79356b A |
5433 | if (slpflag == PCATCH) { |
5434 | slpflag = 0; | |
2d21ac55 | 5435 | ts.tv_sec = 2; |
1c79356b A |
5436 | } |
5437 | } | |
0a7de745 | 5438 | if (!error) { |
2d21ac55 | 5439 | *statep |= NFSSTA_SNDLOCK; |
0a7de745 | 5440 | } |
2d21ac55 | 5441 | lck_mtx_unlock(&nmp->nm_lock); |
0a7de745 | 5442 | return error; |
1c79356b A |
5443 | } |
5444 | ||
5445 | /* | |
5446 | * Unlock the stream socket for others. | |
5447 | */ | |
5448 | void | |
2d21ac55 | 5449 | nfs_sndunlock(struct nfsreq *req) |
1c79356b | 5450 | { |
2d21ac55 A |
5451 | struct nfsmount *nmp = req->r_nmp; |
5452 | int *statep, wake = 0; | |
1c79356b | 5453 | |
0a7de745 | 5454 | if (!nmp) { |
55e303ae | 5455 | return; |
0a7de745 | 5456 | } |
2d21ac55 A |
5457 | lck_mtx_lock(&nmp->nm_lock); |
5458 | statep = &nmp->nm_state; | |
0a7de745 | 5459 | if ((*statep & NFSSTA_SNDLOCK) == 0) { |
1c79356b | 5460 | panic("nfs sndunlock"); |
0a7de745 A |
5461 | } |
5462 | *statep &= ~(NFSSTA_SNDLOCK | NFSSTA_SENDING); | |
55e303ae A |
5463 | if (*statep & NFSSTA_WANTSND) { |
5464 | *statep &= ~NFSSTA_WANTSND; | |
2d21ac55 | 5465 | wake = 1; |
1c79356b | 5466 | } |
2d21ac55 | 5467 | lck_mtx_unlock(&nmp->nm_lock); |
0a7de745 | 5468 | if (wake) { |
2d21ac55 | 5469 | wakeup(statep); |
0a7de745 | 5470 | } |
1c79356b A |
5471 | } |
5472 | ||
b0d623f7 A |
5473 | int |
5474 | nfs_aux_request( | |
5475 | struct nfsmount *nmp, | |
5476 | thread_t thd, | |
6d2010ae A |
5477 | struct sockaddr *saddr, |
5478 | socket_t so, | |
5479 | int sotype, | |
b0d623f7 A |
5480 | mbuf_t mreq, |
5481 | uint32_t xid, | |
5482 | int bindresv, | |
5483 | int timeo, | |
5484 | struct nfsm_chain *nmrep) | |
5485 | { | |
6d2010ae A |
5486 | int error = 0, on = 1, try, sendat = 2, soproto, recv, optlen, restoreto = 0; |
5487 | socket_t newso = NULL; | |
5488 | struct sockaddr_storage ss; | |
5489 | struct timeval orig_rcvto, orig_sndto, tv = { 1, 0 }; | |
b0d623f7 A |
5490 | mbuf_t m, mrep = NULL; |
5491 | struct msghdr msg; | |
5492 | uint32_t rxid = 0, reply = 0, reply_status, rejected_status; | |
5493 | uint32_t verf_type, verf_len, accepted_status; | |
6d2010ae A |
5494 | size_t readlen, sentlen; |
5495 | struct nfs_rpc_record_state nrrs; | |
b0d623f7 | 5496 | |
6d2010ae A |
5497 | if (!so) { |
5498 | /* create socket and set options */ | |
5499 | soproto = (sotype == SOCK_DGRAM) ? IPPROTO_UDP : IPPROTO_TCP; | |
0a7de745 | 5500 | if ((error = sock_socket(saddr->sa_family, sotype, soproto, NULL, NULL, &newso))) { |
6d2010ae | 5501 | goto nfsmout; |
0a7de745 | 5502 | } |
6d2010ae A |
5503 | |
5504 | if (bindresv) { | |
5505 | int level = (saddr->sa_family == AF_INET) ? IPPROTO_IP : IPPROTO_IPV6; | |
5506 | int optname = (saddr->sa_family == AF_INET) ? IP_PORTRANGE : IPV6_PORTRANGE; | |
5507 | int portrange = IP_PORTRANGE_LOW; | |
5508 | error = sock_setsockopt(newso, level, optname, &portrange, sizeof(portrange)); | |
5509 | nfsmout_if(error); | |
5510 | ss.ss_len = saddr->sa_len; | |
5511 | ss.ss_family = saddr->sa_family; | |
5512 | if (ss.ss_family == AF_INET) { | |
5513 | ((struct sockaddr_in*)&ss)->sin_addr.s_addr = INADDR_ANY; | |
5514 | ((struct sockaddr_in*)&ss)->sin_port = htons(0); | |
5515 | } else if (ss.ss_family == AF_INET6) { | |
5516 | ((struct sockaddr_in6*)&ss)->sin6_addr = in6addr_any; | |
5517 | ((struct sockaddr_in6*)&ss)->sin6_port = htons(0); | |
5518 | } else { | |
5519 | error = EINVAL; | |
5520 | } | |
0a7de745 | 5521 | if (!error) { |
6d2010ae | 5522 | error = sock_bind(newso, (struct sockaddr *)&ss); |
0a7de745 | 5523 | } |
6d2010ae A |
5524 | nfsmout_if(error); |
5525 | } | |
5526 | ||
5527 | if (sotype == SOCK_STREAM) { | |
0a7de745 | 5528 | # define NFS_AUX_CONNECTION_TIMEOUT 4 /* 4 second timeout for connections */ |
fe8ab488 | 5529 | int count = 0; |
0a7de745 | 5530 | |
fe8ab488 | 5531 | error = sock_connect(newso, saddr, MSG_DONTWAIT); |
0a7de745 | 5532 | if (error == EINPROGRESS) { |
fe8ab488 | 5533 | error = 0; |
0a7de745 | 5534 | } |
fe8ab488 A |
5535 | nfsmout_if(error); |
5536 | ||
5537 | while ((error = sock_connectwait(newso, &tv)) == EINPROGRESS) { | |
5538 | /* After NFS_AUX_CONNECTION_TIMEOUT bail */ | |
5539 | if (++count >= NFS_AUX_CONNECTION_TIMEOUT) { | |
5540 | error = ETIMEDOUT; | |
5541 | break; | |
5542 | } | |
5543 | } | |
6d2010ae A |
5544 | nfsmout_if(error); |
5545 | } | |
5546 | if (((error = sock_setsockopt(newso, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)))) || | |
5547 | ((error = sock_setsockopt(newso, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)))) || | |
0a7de745 | 5548 | ((error = sock_setsockopt(newso, SOL_SOCKET, SO_NOADDRERR, &on, sizeof(on))))) { |
6d2010ae | 5549 | goto nfsmout; |
0a7de745 | 5550 | } |
6d2010ae A |
5551 | so = newso; |
5552 | } else { | |
5553 | /* make sure socket is using a one second timeout in this function */ | |
5554 | optlen = sizeof(orig_rcvto); | |
5555 | error = sock_getsockopt(so, SOL_SOCKET, SO_RCVTIMEO, &orig_rcvto, &optlen); | |
5556 | if (!error) { | |
5557 | optlen = sizeof(orig_sndto); | |
5558 | error = sock_getsockopt(so, SOL_SOCKET, SO_SNDTIMEO, &orig_sndto, &optlen); | |
5559 | } | |
5560 | if (!error) { | |
5561 | sock_setsockopt(so, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); | |
5562 | sock_setsockopt(so, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); | |
5563 | restoreto = 1; | |
5564 | } | |
5565 | } | |
5566 | ||
5567 | if (sotype == SOCK_STREAM) { | |
5568 | sendat = 0; /* we only resend the request for UDP */ | |
5569 | nfs_rpc_record_state_init(&nrrs); | |
b0d623f7 A |
5570 | } |
5571 | ||
0a7de745 A |
5572 | for (try = 0; try < timeo; try++) { |
5573 | if ((error = nfs_sigintr(nmp, NULL, !try ? NULL : thd, 0))) { | |
b0d623f7 | 5574 | break; |
0a7de745 | 5575 | } |
b0d623f7 | 5576 | if (!try || (try == sendat)) { |
6d2010ae | 5577 | /* send the request (resending periodically for UDP) */ |
0a7de745 | 5578 | if ((error = mbuf_copym(mreq, 0, MBUF_COPYALL, MBUF_WAITOK, &m))) { |
b0d623f7 | 5579 | goto nfsmout; |
0a7de745 | 5580 | } |
b0d623f7 | 5581 | bzero(&msg, sizeof(msg)); |
6d2010ae A |
5582 | if ((sotype == SOCK_DGRAM) && !sock_isconnected(so)) { |
5583 | msg.msg_name = saddr; | |
5584 | msg.msg_namelen = saddr->sa_len; | |
5585 | } | |
0a7de745 | 5586 | if ((error = sock_sendmbuf(so, &msg, m, 0, &sentlen))) { |
b0d623f7 | 5587 | goto nfsmout; |
0a7de745 | 5588 | } |
b0d623f7 | 5589 | sendat *= 2; |
0a7de745 | 5590 | if (sendat > 30) { |
b0d623f7 | 5591 | sendat = 30; |
0a7de745 | 5592 | } |
b0d623f7 A |
5593 | } |
5594 | /* wait for the response */ | |
6d2010ae A |
5595 | if (sotype == SOCK_STREAM) { |
5596 | /* try to read (more of) record */ | |
5597 | error = nfs_rpc_record_read(so, &nrrs, 0, &recv, &mrep); | |
5598 | /* if we don't have the whole record yet, we'll keep trying */ | |
5599 | } else { | |
0a7de745 | 5600 | readlen = 1 << 18; |
6d2010ae A |
5601 | bzero(&msg, sizeof(msg)); |
5602 | error = sock_receivembuf(so, &msg, &mrep, 0, &readlen); | |
5603 | } | |
0a7de745 | 5604 | if (error == EWOULDBLOCK) { |
b0d623f7 | 5605 | continue; |
0a7de745 | 5606 | } |
b0d623f7 A |
5607 | nfsmout_if(error); |
5608 | /* parse the response */ | |
5609 | nfsm_chain_dissect_init(error, nmrep, mrep); | |
5610 | nfsm_chain_get_32(error, nmrep, rxid); | |
5611 | nfsm_chain_get_32(error, nmrep, reply); | |
5612 | nfsmout_if(error); | |
0a7de745 | 5613 | if ((rxid != xid) || (reply != RPC_REPLY)) { |
b0d623f7 | 5614 | error = EBADRPC; |
0a7de745 | 5615 | } |
b0d623f7 A |
5616 | nfsm_chain_get_32(error, nmrep, reply_status); |
5617 | nfsmout_if(error); | |
5618 | if (reply_status == RPC_MSGDENIED) { | |
5619 | nfsm_chain_get_32(error, nmrep, rejected_status); | |
5620 | nfsmout_if(error); | |
6d2010ae | 5621 | error = (rejected_status == RPC_MISMATCH) ? ERPCMISMATCH : EACCES; |
b0d623f7 A |
5622 | goto nfsmout; |
5623 | } | |
5624 | nfsm_chain_get_32(error, nmrep, verf_type); /* verifier flavor */ | |
5625 | nfsm_chain_get_32(error, nmrep, verf_len); /* verifier length */ | |
5626 | nfsmout_if(error); | |
0a7de745 | 5627 | if (verf_len) { |
b0d623f7 | 5628 | nfsm_chain_adv(error, nmrep, nfsm_rndup(verf_len)); |
0a7de745 | 5629 | } |
b0d623f7 | 5630 | nfsm_chain_get_32(error, nmrep, accepted_status); |
6d2010ae A |
5631 | nfsmout_if(error); |
5632 | switch (accepted_status) { | |
5633 | case RPC_SUCCESS: | |
5634 | error = 0; | |
5635 | break; | |
5636 | case RPC_PROGUNAVAIL: | |
5637 | error = EPROGUNAVAIL; | |
5638 | break; | |
5639 | case RPC_PROGMISMATCH: | |
5640 | error = EPROGMISMATCH; | |
5641 | break; | |
5642 | case RPC_PROCUNAVAIL: | |
5643 | error = EPROCUNAVAIL; | |
5644 | break; | |
5645 | case RPC_GARBAGE: | |
5646 | error = EBADRPC; | |
5647 | break; | |
5648 | case RPC_SYSTEM_ERR: | |
5649 | default: | |
5650 | error = EIO; | |
5651 | break; | |
5652 | } | |
b0d623f7 A |
5653 | break; |
5654 | } | |
5655 | nfsmout: | |
6d2010ae A |
5656 | if (restoreto) { |
5657 | sock_setsockopt(so, SOL_SOCKET, SO_RCVTIMEO, &orig_rcvto, sizeof(tv)); | |
5658 | sock_setsockopt(so, SOL_SOCKET, SO_SNDTIMEO, &orig_sndto, sizeof(tv)); | |
5659 | } | |
5660 | if (newso) { | |
5661 | sock_shutdown(newso, SHUT_RDWR); | |
5662 | sock_close(newso); | |
b0d623f7 A |
5663 | } |
5664 | mbuf_freem(mreq); | |
0a7de745 | 5665 | return error; |
b0d623f7 A |
5666 | } |
5667 | ||
6d2010ae A |
5668 | int |
5669 | nfs_portmap_lookup( | |
5670 | struct nfsmount *nmp, | |
5671 | vfs_context_t ctx, | |
5672 | struct sockaddr *sa, | |
5673 | socket_t so, | |
5674 | uint32_t protocol, | |
5675 | uint32_t vers, | |
5676 | uint32_t ipproto, | |
5677 | int timeo) | |
5678 | { | |
5679 | thread_t thd = vfs_context_thread(ctx); | |
5680 | kauth_cred_t cred = vfs_context_ucred(ctx); | |
5681 | struct sockaddr_storage ss; | |
5682 | struct sockaddr *saddr = (struct sockaddr*)&ss; | |
5683 | struct nfsm_chain nmreq, nmrep; | |
5684 | mbuf_t mreq; | |
d26ffc64 A |
5685 | int error = 0, ip, pmprog, pmvers, pmproc; |
5686 | uint32_t ualen = 0; | |
6d2010ae A |
5687 | uint32_t port; |
5688 | uint64_t xid = 0; | |
0a7de745 | 5689 | char uaddr[MAX_IPv6_STR_LEN + 16]; |
6d2010ae A |
5690 | |
5691 | bcopy(sa, saddr, min(sizeof(ss), sa->sa_len)); | |
5692 | if (saddr->sa_family == AF_INET) { | |
5693 | ip = 4; | |
5694 | pmprog = PMAPPROG; | |
5695 | pmvers = PMAPVERS; | |
5696 | pmproc = PMAPPROC_GETPORT; | |
5697 | } else if (saddr->sa_family == AF_INET6) { | |
5698 | ip = 6; | |
5699 | pmprog = RPCBPROG; | |
5700 | pmvers = RPCBVERS4; | |
5701 | pmproc = RPCBPROC_GETVERSADDR; | |
5702 | } else { | |
0a7de745 | 5703 | return EINVAL; |
6d2010ae A |
5704 | } |
5705 | nfsm_chain_null(&nmreq); | |
5706 | nfsm_chain_null(&nmrep); | |
5707 | ||
5708 | tryagain: | |
5709 | /* send portmapper request to get port/uaddr */ | |
0a7de745 | 5710 | if (ip == 4) { |
6d2010ae | 5711 | ((struct sockaddr_in*)saddr)->sin_port = htons(PMAPPORT); |
0a7de745 | 5712 | } else { |
6d2010ae | 5713 | ((struct sockaddr_in6*)saddr)->sin6_port = htons(PMAPPORT); |
0a7de745 A |
5714 | } |
5715 | nfsm_chain_build_alloc_init(error, &nmreq, 8 * NFSX_UNSIGNED); | |
6d2010ae A |
5716 | nfsm_chain_add_32(error, &nmreq, protocol); |
5717 | nfsm_chain_add_32(error, &nmreq, vers); | |
5718 | if (ip == 4) { | |
5719 | nfsm_chain_add_32(error, &nmreq, ipproto); | |
5720 | nfsm_chain_add_32(error, &nmreq, 0); | |
5721 | } else { | |
0a7de745 | 5722 | if (ipproto == IPPROTO_TCP) { |
6d2010ae | 5723 | nfsm_chain_add_string(error, &nmreq, "tcp6", 4); |
0a7de745 | 5724 | } else { |
6d2010ae | 5725 | nfsm_chain_add_string(error, &nmreq, "udp6", 4); |
0a7de745 | 5726 | } |
6d2010ae A |
5727 | nfsm_chain_add_string(error, &nmreq, "", 0); /* uaddr */ |
5728 | nfsm_chain_add_string(error, &nmreq, "", 0); /* owner */ | |
5729 | } | |
5730 | nfsm_chain_build_done(error, &nmreq); | |
5731 | nfsmout_if(error); | |
5732 | error = nfsm_rpchead2(nmp, (ipproto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM, | |
0a7de745 A |
5733 | pmprog, pmvers, pmproc, RPCAUTH_SYS, cred, NULL, nmreq.nmc_mhead, |
5734 | &xid, &mreq); | |
6d2010ae A |
5735 | nfsmout_if(error); |
5736 | nmreq.nmc_mhead = NULL; | |
5737 | error = nfs_aux_request(nmp, thd, saddr, so, (ipproto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM, | |
0a7de745 | 5738 | mreq, R_XID32(xid), 0, timeo, &nmrep); |
6d2010ae A |
5739 | |
5740 | /* grab port from portmap response */ | |
5741 | if (ip == 4) { | |
5742 | nfsm_chain_get_32(error, &nmrep, port); | |
0a7de745 | 5743 | if (!error) { |
6d2010ae | 5744 | ((struct sockaddr_in*)sa)->sin_port = htons(port); |
0a7de745 | 5745 | } |
6d2010ae A |
5746 | } else { |
5747 | /* get uaddr string and convert to sockaddr */ | |
5748 | nfsm_chain_get_32(error, &nmrep, ualen); | |
5749 | if (!error) { | |
0a7de745 | 5750 | if (ualen > (sizeof(uaddr) - 1)) { |
6d2010ae | 5751 | error = EIO; |
0a7de745 | 5752 | } |
6d2010ae A |
5753 | if (ualen < 1) { |
5754 | /* program is not available, just return a zero port */ | |
5755 | bcopy(sa, saddr, min(sizeof(ss), sa->sa_len)); | |
5756 | ((struct sockaddr_in6*)saddr)->sin6_port = htons(0); | |
5757 | } else { | |
5758 | nfsm_chain_get_opaque(error, &nmrep, ualen, uaddr); | |
5759 | if (!error) { | |
5760 | uaddr[ualen] = '\0'; | |
0a7de745 | 5761 | if (!nfs_uaddr2sockaddr(uaddr, saddr)) { |
6d2010ae | 5762 | error = EIO; |
0a7de745 | 5763 | } |
6d2010ae A |
5764 | } |
5765 | } | |
5766 | } | |
5767 | if ((error == EPROGMISMATCH) || (error == EPROCUNAVAIL) || (error == EIO) || (error == EBADRPC)) { | |
5768 | /* remote doesn't support rpcbind version or proc (or we couldn't parse uaddr) */ | |
5769 | if (pmvers == RPCBVERS4) { | |
5770 | /* fall back to v3 and GETADDR */ | |
5771 | pmvers = RPCBVERS3; | |
5772 | pmproc = RPCBPROC_GETADDR; | |
5773 | nfsm_chain_cleanup(&nmreq); | |
5774 | nfsm_chain_cleanup(&nmrep); | |
5775 | bcopy(sa, saddr, min(sizeof(ss), sa->sa_len)); | |
5776 | xid = 0; | |
5777 | error = 0; | |
5778 | goto tryagain; | |
5779 | } | |
5780 | } | |
0a7de745 | 5781 | if (!error) { |
6d2010ae | 5782 | bcopy(saddr, sa, min(saddr->sa_len, sa->sa_len)); |
0a7de745 | 5783 | } |
6d2010ae A |
5784 | } |
5785 | nfsmout: | |
5786 | nfsm_chain_cleanup(&nmreq); | |
5787 | nfsm_chain_cleanup(&nmrep); | |
0a7de745 | 5788 | return error; |
6d2010ae A |
5789 | } |
5790 | ||
b0d623f7 A |
5791 | int |
5792 | nfs_msg(thread_t thd, | |
0a7de745 A |
5793 | const char *server, |
5794 | const char *msg, | |
5795 | int error) | |
b0d623f7 A |
5796 | { |
5797 | proc_t p = thd ? get_bsdthreadtask_info(thd) : NULL; | |
5798 | tpr_t tpr; | |
5799 | ||
0a7de745 | 5800 | if (p) { |
b0d623f7 | 5801 | tpr = tprintf_open(p); |
0a7de745 | 5802 | } else { |
b0d623f7 | 5803 | tpr = NULL; |
0a7de745 A |
5804 | } |
5805 | if (error) { | |
b0d623f7 | 5806 | tprintf(tpr, "nfs server %s: %s, error %d\n", server, msg, error); |
0a7de745 | 5807 | } else { |
b0d623f7 | 5808 | tprintf(tpr, "nfs server %s: %s\n", server, msg); |
0a7de745 | 5809 | } |
b0d623f7 | 5810 | tprintf_close(tpr); |
0a7de745 | 5811 | return 0; |
b0d623f7 A |
5812 | } |
5813 | ||
0a7de745 A |
5814 | #define NFS_SQUISH_MOBILE_ONLY 0x0001 /* Squish mounts only on mobile machines */ |
5815 | #define NFS_SQUISH_AUTOMOUNTED_ONLY 0x0002 /* Squish mounts only if the are automounted */ | |
5816 | #define NFS_SQUISH_SOFT 0x0004 /* Treat all soft mounts as though they were on a mobile machine */ | |
5817 | #define NFS_SQUISH_QUICK 0x0008 /* Try to squish mounts more quickly. */ | |
5818 | #define NFS_SQUISH_SHUTDOWN 0x1000 /* Squish all mounts on shutdown. Currently not implemented */ | |
316670eb A |
5819 | |
5820 | uint32_t nfs_squishy_flags = NFS_SQUISH_MOBILE_ONLY | NFS_SQUISH_AUTOMOUNTED_ONLY | NFS_SQUISH_QUICK; | |
5821 | int32_t nfs_is_mobile; | |
5822 | ||
0a7de745 A |
5823 | #define NFS_SQUISHY_DEADTIMEOUT 8 /* Dead time out for squishy mounts */ |
5824 | #define NFS_SQUISHY_QUICKTIMEOUT 4 /* Quicker dead time out when nfs_squish_flags NFS_SQUISH_QUICK bit is set*/ | |
316670eb A |
5825 | |
5826 | /* | |
5827 | * Could this mount be squished? | |
5828 | */ | |
5829 | int | |
5830 | nfs_can_squish(struct nfsmount *nmp) | |
5831 | { | |
5832 | uint64_t flags = vfs_flags(nmp->nm_mountp); | |
5833 | int softsquish = ((nfs_squishy_flags & NFS_SQUISH_SOFT) & NMFLAG(nmp, SOFT)); | |
5834 | ||
0a7de745 A |
5835 | if (!softsquish && (nfs_squishy_flags & NFS_SQUISH_MOBILE_ONLY) && nfs_is_mobile == 0) { |
5836 | return 0; | |
5837 | } | |
316670eb | 5838 | |
0a7de745 A |
5839 | if ((nfs_squishy_flags & NFS_SQUISH_AUTOMOUNTED_ONLY) && (flags & MNT_AUTOMOUNTED) == 0) { |
5840 | return 0; | |
5841 | } | |
316670eb | 5842 | |
0a7de745 | 5843 | return 1; |
316670eb A |
5844 | } |
5845 | ||
5846 | /* | |
5847 | * NFS mounts default to "rw,hard" - but frequently on mobile clients | |
5848 | * the mount may become "not responding". It's desirable to be able | |
5849 | * to unmount these dead mounts, but only if there is no risk of | |
5850 | * losing data or crashing applications. A "squishy" NFS mount is one | |
5851 | * that can be force unmounted with little risk of harm. | |
5852 | * | |
5853 | * nfs_is_squishy checks if a mount is in a squishy state. A mount is | |
5854 | * in a squishy state iff it is allowed to be squishy and there are no | |
5855 | * dirty pages and there are no mmapped files and there are no files | |
5856 | * open for write. Mounts are allowed to be squishy is controlled by | |
5857 | * the settings of the nfs_squishy_flags and its mobility state. These | |
5858 | * flags can be set by sysctls. | |
5859 | * | |
5860 | * If nfs_is_squishy determines that we are in a squishy state we will | |
5861 | * update the current dead timeout to at least NFS_SQUISHY_DEADTIMEOUT | |
5862 | * (or NFS_SQUISHY_QUICKTIMEOUT if NFS_SQUISH_QUICK is set) (see | |
5863 | * above) or 1/8th of the mount's nm_deadtimeout value, otherwise we just | |
5864 | * update the current dead timeout with the mount's nm_deadtimeout | |
5865 | * value set at mount time. | |
5866 | * | |
5867 | * Assumes that nm_lock is held. | |
5868 | * | |
5869 | * Note this routine is racey, but its effects on setting the | |
5870 | * dead timeout only have effects when we're in trouble and are likely | |
5871 | * to stay that way. Since by default its only for automounted | |
5872 | * volumes on mobile machines; this is a reasonable trade off between | |
5873 | * data integrity and user experience. It can be disabled or set via | |
5874 | * nfs.conf file. | |
5875 | */ | |
5876 | ||
5877 | int | |
5878 | nfs_is_squishy(struct nfsmount *nmp) | |
5879 | { | |
5880 | mount_t mp = nmp->nm_mountp; | |
5881 | int squishy = 0; | |
5882 | int timeo = (nfs_squishy_flags & NFS_SQUISH_QUICK) ? NFS_SQUISHY_QUICKTIMEOUT : NFS_SQUISHY_DEADTIMEOUT; | |
5883 | ||
fe8ab488 | 5884 | NFS_SOCK_DBG("%s: nm_curdeadtimeout = %d, nfs_is_mobile = %d\n", |
0a7de745 | 5885 | vfs_statfs(mp)->f_mntfromname, nmp->nm_curdeadtimeout, nfs_is_mobile); |
316670eb | 5886 | |
0a7de745 | 5887 | if (!nfs_can_squish(nmp)) { |
316670eb | 5888 | goto out; |
0a7de745 | 5889 | } |
316670eb | 5890 | |
0a7de745 | 5891 | timeo = (nmp->nm_deadtimeout > timeo) ? max(nmp->nm_deadtimeout / 8, timeo) : timeo; |
39236c6e | 5892 | NFS_SOCK_DBG("nm_writers = %d nm_mappers = %d timeo = %d\n", nmp->nm_writers, nmp->nm_mappers, timeo); |
316670eb A |
5893 | |
5894 | if (nmp->nm_writers == 0 && nmp->nm_mappers == 0) { | |
5895 | uint64_t flags = mp ? vfs_flags(mp) : 0; | |
5896 | squishy = 1; | |
0a7de745 A |
5897 | |
5898 | /* | |
5899 | * Walk the nfs nodes and check for dirty buffers it we're not | |
316670eb A |
5900 | * RDONLY and we've not already been declared as squishy since |
5901 | * this can be a bit expensive. | |
5902 | */ | |
0a7de745 | 5903 | if (!(flags & MNT_RDONLY) && !(nmp->nm_state & NFSSTA_SQUISHY)) { |
316670eb | 5904 | squishy = !nfs_mount_is_dirty(mp); |
0a7de745 | 5905 | } |
316670eb A |
5906 | } |
5907 | ||
5908 | out: | |
0a7de745 | 5909 | if (squishy) { |
316670eb | 5910 | nmp->nm_state |= NFSSTA_SQUISHY; |
0a7de745 | 5911 | } else { |
316670eb | 5912 | nmp->nm_state &= ~NFSSTA_SQUISHY; |
0a7de745 | 5913 | } |
316670eb A |
5914 | |
5915 | nmp->nm_curdeadtimeout = squishy ? timeo : nmp->nm_deadtimeout; | |
0a7de745 | 5916 | |
39236c6e | 5917 | NFS_SOCK_DBG("nm_curdeadtimeout = %d\n", nmp->nm_curdeadtimeout); |
316670eb | 5918 | |
0a7de745 | 5919 | return squishy; |
316670eb A |
5920 | } |
5921 | ||
5922 | /* | |
5923 | * On a send operation, if we can't reach the server and we've got only one server to talk to | |
5924 | * and NFS_SQUISH_QUICK flag is set and we are in a squishy state then mark the mount as dead | |
5925 | * and ask to be forcibly unmounted. Return 1 if we're dead and 0 otherwise. | |
5926 | */ | |
fe8ab488 A |
5927 | int |
5928 | nfs_is_dead(int error, struct nfsmount *nmp) | |
316670eb | 5929 | { |
fe8ab488 A |
5930 | fsid_t fsid; |
5931 | ||
5932 | lck_mtx_lock(&nmp->nm_lock); | |
5933 | if (nmp->nm_state & NFSSTA_DEAD) { | |
5934 | lck_mtx_unlock(&nmp->nm_lock); | |
0a7de745 | 5935 | return 1; |
fe8ab488 | 5936 | } |
316670eb | 5937 | |
39236c6e | 5938 | if ((error != ENETUNREACH && error != EHOSTUNREACH && error != EADDRNOTAVAIL) || |
fe8ab488 A |
5939 | !(nmp->nm_locations.nl_numlocs == 1 && nmp->nm_locations.nl_locations[0]->nl_servcount == 1)) { |
5940 | lck_mtx_unlock(&nmp->nm_lock); | |
0a7de745 | 5941 | return 0; |
fe8ab488 | 5942 | } |
39236c6e | 5943 | |
316670eb A |
5944 | if ((nfs_squishy_flags & NFS_SQUISH_QUICK) && nfs_is_squishy(nmp)) { |
5945 | printf("nfs_is_dead: nfs server %s: unreachable. Squished dead\n", vfs_statfs(nmp->nm_mountp)->f_mntfromname); | |
fe8ab488 A |
5946 | fsid = vfs_statfs(nmp->nm_mountp)->f_fsid; |
5947 | lck_mtx_unlock(&nmp->nm_lock); | |
5948 | nfs_mount_zombie(nmp, NFSSTA_DEAD); | |
5949 | vfs_event_signal(&fsid, VQ_DEAD, 0); | |
0a7de745 | 5950 | return 1; |
316670eb | 5951 | } |
fe8ab488 | 5952 | lck_mtx_unlock(&nmp->nm_lock); |
0a7de745 | 5953 | return 0; |
316670eb A |
5954 | } |
5955 | ||
fe8ab488 A |
5956 | /* |
5957 | * If we've experienced timeouts and we're not really a | |
5958 | * classic hard mount, then just return cached data to | |
5959 | * the caller instead of likely hanging on an RPC. | |
5960 | */ | |
316670eb | 5961 | int |
fe8ab488 | 5962 | nfs_use_cache(struct nfsmount *nmp) |
316670eb | 5963 | { |
fe8ab488 A |
5964 | /* |
5965 | *%%% We always let mobile users goto the cache, | |
5966 | * perhaps we should not even require them to have | |
5967 | * a timeout? | |
5968 | */ | |
5969 | int cache_ok = (nfs_is_mobile || NMFLAG(nmp, SOFT) || | |
0a7de745 | 5970 | nfs_can_squish(nmp) || nmp->nm_deadtimeout); |
316670eb | 5971 | |
fe8ab488 A |
5972 | int timeoutmask = NFSSTA_TIMEO | NFSSTA_LOCKTIMEO | NFSSTA_JUKEBOXTIMEO; |
5973 | ||
5974 | /* | |
5975 | * So if we have a timeout and we're not really a hard hard-mount, | |
5976 | * return 1 to not get things out of the cache. | |
5977 | */ | |
316670eb | 5978 | |
0a7de745 | 5979 | return (nmp->nm_state & timeoutmask) && cache_ok; |
316670eb A |
5980 | } |
5981 | ||
fe8ab488 A |
5982 | /* |
5983 | * Log a message that nfs or lockd server is unresponsive. Check if we | |
5984 | * can be squished and if we can, or that our dead timeout has | |
5985 | * expired, and we're not holding state, set our mount as dead, remove | |
5986 | * our mount state and ask to be unmounted. If we are holding state | |
5987 | * we're being called from the nfs_request_timer and will soon detect | |
5988 | * that we need to unmount. | |
5989 | */ | |
b0d623f7 | 5990 | void |
fe8ab488 | 5991 | nfs_down(struct nfsmount *nmp, thread_t thd, int error, int flags, const char *msg, int holding_state) |
b0d623f7 A |
5992 | { |
5993 | int timeoutmask, wasunresponsive, unresponsive, softnobrowse; | |
fe8ab488 | 5994 | uint32_t do_vfs_signal = 0; |
b0d623f7 A |
5995 | struct timeval now; |
5996 | ||
0a7de745 | 5997 | if (nfs_mount_gone(nmp)) { |
b0d623f7 | 5998 | return; |
0a7de745 | 5999 | } |
b0d623f7 A |
6000 | |
6001 | lck_mtx_lock(&nmp->nm_lock); | |
6002 | ||
6003 | timeoutmask = NFSSTA_TIMEO | NFSSTA_LOCKTIMEO | NFSSTA_JUKEBOXTIMEO; | |
0a7de745 A |
6004 | if (NMFLAG(nmp, MUTEJUKEBOX)) { /* jukebox timeouts don't count as unresponsive if muted */ |
6005 | timeoutmask &= ~NFSSTA_JUKEBOXTIMEO; | |
6006 | } | |
b0d623f7 A |
6007 | wasunresponsive = (nmp->nm_state & timeoutmask); |
6008 | ||
6009 | /* XXX don't allow users to know about/disconnect unresponsive, soft, nobrowse mounts */ | |
6d2010ae | 6010 | softnobrowse = (NMFLAG(nmp, SOFT) && (vfs_flags(nmp->nm_mountp) & MNT_DONTBROWSE)); |
b0d623f7 | 6011 | |
0a7de745 | 6012 | if ((flags & NFSSTA_TIMEO) && !(nmp->nm_state & NFSSTA_TIMEO)) { |
b0d623f7 | 6013 | nmp->nm_state |= NFSSTA_TIMEO; |
0a7de745 A |
6014 | } |
6015 | if ((flags & NFSSTA_LOCKTIMEO) && !(nmp->nm_state & NFSSTA_LOCKTIMEO)) { | |
b0d623f7 | 6016 | nmp->nm_state |= NFSSTA_LOCKTIMEO; |
0a7de745 A |
6017 | } |
6018 | if ((flags & NFSSTA_JUKEBOXTIMEO) && !(nmp->nm_state & NFSSTA_JUKEBOXTIMEO)) { | |
b0d623f7 | 6019 | nmp->nm_state |= NFSSTA_JUKEBOXTIMEO; |
0a7de745 | 6020 | } |
b0d623f7 A |
6021 | |
6022 | unresponsive = (nmp->nm_state & timeoutmask); | |
6023 | ||
316670eb A |
6024 | nfs_is_squishy(nmp); |
6025 | ||
6026 | if (unresponsive && (nmp->nm_curdeadtimeout > 0)) { | |
b0d623f7 A |
6027 | microuptime(&now); |
6028 | if (!wasunresponsive) { | |
6029 | nmp->nm_deadto_start = now.tv_sec; | |
6030 | nfs_mount_sock_thread_wake(nmp); | |
fe8ab488 | 6031 | } else if ((now.tv_sec - nmp->nm_deadto_start) > nmp->nm_curdeadtimeout && !holding_state) { |
0a7de745 | 6032 | if (!(nmp->nm_state & NFSSTA_DEAD)) { |
316670eb | 6033 | printf("nfs server %s: %sdead\n", vfs_statfs(nmp->nm_mountp)->f_mntfromname, |
0a7de745 A |
6034 | (nmp->nm_curdeadtimeout != nmp->nm_deadtimeout) ? "squished " : ""); |
6035 | } | |
fe8ab488 | 6036 | do_vfs_signal = VQ_DEAD; |
b0d623f7 A |
6037 | } |
6038 | } | |
6039 | lck_mtx_unlock(&nmp->nm_lock); | |
6040 | ||
0a7de745 | 6041 | if (do_vfs_signal == VQ_DEAD && !(nmp->nm_state & NFSSTA_DEAD)) { |
fe8ab488 | 6042 | nfs_mount_zombie(nmp, NFSSTA_DEAD); |
0a7de745 | 6043 | } else if (softnobrowse || wasunresponsive || !unresponsive) { |
b0d623f7 | 6044 | do_vfs_signal = 0; |
0a7de745 | 6045 | } else { |
b0d623f7 | 6046 | do_vfs_signal = VQ_NOTRESP; |
0a7de745 A |
6047 | } |
6048 | if (do_vfs_signal) { | |
b0d623f7 | 6049 | vfs_event_signal(&vfs_statfs(nmp->nm_mountp)->f_fsid, do_vfs_signal, 0); |
0a7de745 | 6050 | } |
b0d623f7 A |
6051 | |
6052 | nfs_msg(thd, vfs_statfs(nmp->nm_mountp)->f_mntfromname, msg, error); | |
6053 | } | |
6054 | ||
6055 | void | |
6056 | nfs_up(struct nfsmount *nmp, thread_t thd, int flags, const char *msg) | |
6057 | { | |
6058 | int timeoutmask, wasunresponsive, unresponsive, softnobrowse; | |
6059 | int do_vfs_signal; | |
6060 | ||
0a7de745 | 6061 | if (nfs_mount_gone(nmp)) { |
b0d623f7 | 6062 | return; |
0a7de745 | 6063 | } |
b0d623f7 | 6064 | |
0a7de745 | 6065 | if (msg) { |
b0d623f7 | 6066 | nfs_msg(thd, vfs_statfs(nmp->nm_mountp)->f_mntfromname, msg, 0); |
0a7de745 | 6067 | } |
b0d623f7 A |
6068 | |
6069 | lck_mtx_lock(&nmp->nm_lock); | |
6070 | ||
6071 | timeoutmask = NFSSTA_TIMEO | NFSSTA_LOCKTIMEO | NFSSTA_JUKEBOXTIMEO; | |
0a7de745 A |
6072 | if (NMFLAG(nmp, MUTEJUKEBOX)) { /* jukebox timeouts don't count as unresponsive if muted */ |
6073 | timeoutmask &= ~NFSSTA_JUKEBOXTIMEO; | |
6074 | } | |
b0d623f7 A |
6075 | wasunresponsive = (nmp->nm_state & timeoutmask); |
6076 | ||
6077 | /* XXX don't allow users to know about/disconnect unresponsive, soft, nobrowse mounts */ | |
6d2010ae | 6078 | softnobrowse = (NMFLAG(nmp, SOFT) && (vfs_flags(nmp->nm_mountp) & MNT_DONTBROWSE)); |
b0d623f7 | 6079 | |
0a7de745 | 6080 | if ((flags & NFSSTA_TIMEO) && (nmp->nm_state & NFSSTA_TIMEO)) { |
b0d623f7 | 6081 | nmp->nm_state &= ~NFSSTA_TIMEO; |
0a7de745 A |
6082 | } |
6083 | if ((flags & NFSSTA_LOCKTIMEO) && (nmp->nm_state & NFSSTA_LOCKTIMEO)) { | |
b0d623f7 | 6084 | nmp->nm_state &= ~NFSSTA_LOCKTIMEO; |
0a7de745 A |
6085 | } |
6086 | if ((flags & NFSSTA_JUKEBOXTIMEO) && (nmp->nm_state & NFSSTA_JUKEBOXTIMEO)) { | |
b0d623f7 | 6087 | nmp->nm_state &= ~NFSSTA_JUKEBOXTIMEO; |
0a7de745 | 6088 | } |
b0d623f7 A |
6089 | |
6090 | unresponsive = (nmp->nm_state & timeoutmask); | |
6091 | ||
316670eb A |
6092 | nmp->nm_deadto_start = 0; |
6093 | nmp->nm_curdeadtimeout = nmp->nm_deadtimeout; | |
6094 | nmp->nm_state &= ~NFSSTA_SQUISHY; | |
b0d623f7 A |
6095 | lck_mtx_unlock(&nmp->nm_lock); |
6096 | ||
0a7de745 | 6097 | if (softnobrowse) { |
b0d623f7 | 6098 | do_vfs_signal = 0; |
0a7de745 | 6099 | } else { |
b0d623f7 | 6100 | do_vfs_signal = (wasunresponsive && !unresponsive); |
0a7de745 A |
6101 | } |
6102 | if (do_vfs_signal) { | |
b0d623f7 | 6103 | vfs_event_signal(&vfs_statfs(nmp->nm_mountp)->f_fsid, VQ_NOTRESP, 1); |
0a7de745 | 6104 | } |
b0d623f7 A |
6105 | } |
6106 | ||
6107 | ||
2d21ac55 A |
6108 | #endif /* NFSCLIENT */ |
6109 | ||
6110 | #if NFSSERVER | |
6111 | ||
6112 | /* | |
6113 | * Generate the rpc reply header | |
6114 | * siz arg. is used to decide if adding a cluster is worthwhile | |
6115 | */ | |
6116 | int | |
6117 | nfsrv_rephead( | |
6118 | struct nfsrv_descript *nd, | |
6119 | __unused struct nfsrv_sock *slp, | |
6120 | struct nfsm_chain *nmrepp, | |
6121 | size_t siz) | |
1c79356b | 6122 | { |
2d21ac55 | 6123 | mbuf_t mrep; |
b0d623f7 | 6124 | u_int32_t *tl; |
2d21ac55 A |
6125 | struct nfsm_chain nmrep; |
6126 | int err, error; | |
1c79356b | 6127 | |
2d21ac55 | 6128 | err = nd->nd_repstat; |
0a7de745 | 6129 | if (err && (nd->nd_vers == NFS_VER2)) { |
2d21ac55 | 6130 | siz = 0; |
0a7de745 | 6131 | } |
d12e1678 | 6132 | |
2d21ac55 A |
6133 | /* |
6134 | * If this is a big reply, use a cluster else | |
6135 | * try and leave leading space for the lower level headers. | |
6136 | */ | |
6137 | siz += RPC_REPLYSIZ; | |
6138 | if (siz >= nfs_mbuf_minclsize) { | |
6139 | error = mbuf_getpacket(MBUF_WAITOK, &mrep); | |
6140 | } else { | |
6141 | error = mbuf_gethdr(MBUF_WAITOK, MBUF_TYPE_DATA, &mrep); | |
6142 | } | |
6143 | if (error) { | |
6144 | /* unable to allocate packet */ | |
6145 | /* XXX should we keep statistics for these errors? */ | |
0a7de745 | 6146 | return error; |
2d21ac55 A |
6147 | } |
6148 | if (siz < nfs_mbuf_minclsize) { | |
6149 | /* leave space for lower level headers */ | |
6150 | tl = mbuf_data(mrep); | |
0a7de745 | 6151 | tl += 80 / sizeof(*tl); /* XXX max_hdr? XXX */ |
2d21ac55 A |
6152 | mbuf_setdata(mrep, tl, 6 * NFSX_UNSIGNED); |
6153 | } | |
6154 | nfsm_chain_init(&nmrep, mrep); | |
6155 | nfsm_chain_add_32(error, &nmrep, nd->nd_retxid); | |
6156 | nfsm_chain_add_32(error, &nmrep, RPC_REPLY); | |
6157 | if (err == ERPCMISMATCH || (err & NFSERR_AUTHERR)) { | |
6158 | nfsm_chain_add_32(error, &nmrep, RPC_MSGDENIED); | |
6159 | if (err & NFSERR_AUTHERR) { | |
6160 | nfsm_chain_add_32(error, &nmrep, RPC_AUTHERR); | |
6161 | nfsm_chain_add_32(error, &nmrep, (err & ~NFSERR_AUTHERR)); | |
6162 | } else { | |
6163 | nfsm_chain_add_32(error, &nmrep, RPC_MISMATCH); | |
6164 | nfsm_chain_add_32(error, &nmrep, RPC_VER2); | |
6165 | nfsm_chain_add_32(error, &nmrep, RPC_VER2); | |
1c79356b | 6166 | } |
2d21ac55 A |
6167 | } else { |
6168 | /* reply status */ | |
6169 | nfsm_chain_add_32(error, &nmrep, RPC_MSGACCEPTED); | |
6170 | if (nd->nd_gss_context != NULL) { | |
6171 | /* RPCSEC_GSS verifier */ | |
6172 | error = nfs_gss_svc_verf_put(nd, &nmrep); | |
6173 | if (error) { | |
6174 | nfsm_chain_add_32(error, &nmrep, RPC_SYSTEM_ERR); | |
6175 | goto done; | |
6176 | } | |
6177 | } else { | |
6178 | /* RPCAUTH_NULL verifier */ | |
6179 | nfsm_chain_add_32(error, &nmrep, RPCAUTH_NULL); | |
6180 | nfsm_chain_add_32(error, &nmrep, 0); | |
1c79356b | 6181 | } |
2d21ac55 A |
6182 | /* accepted status */ |
6183 | switch (err) { | |
6184 | case EPROGUNAVAIL: | |
6185 | nfsm_chain_add_32(error, &nmrep, RPC_PROGUNAVAIL); | |
6186 | break; | |
6187 | case EPROGMISMATCH: | |
6188 | nfsm_chain_add_32(error, &nmrep, RPC_PROGMISMATCH); | |
6189 | /* XXX hard coded versions? */ | |
6190 | nfsm_chain_add_32(error, &nmrep, NFS_VER2); | |
6191 | nfsm_chain_add_32(error, &nmrep, NFS_VER3); | |
6192 | break; | |
6193 | case EPROCUNAVAIL: | |
6194 | nfsm_chain_add_32(error, &nmrep, RPC_PROCUNAVAIL); | |
6195 | break; | |
6196 | case EBADRPC: | |
6197 | nfsm_chain_add_32(error, &nmrep, RPC_GARBAGE); | |
6198 | break; | |
6199 | default: | |
6200 | nfsm_chain_add_32(error, &nmrep, RPC_SUCCESS); | |
0a7de745 | 6201 | if (nd->nd_gss_context != NULL) { |
2d21ac55 | 6202 | error = nfs_gss_svc_prepare_reply(nd, &nmrep); |
0a7de745 A |
6203 | } |
6204 | if (err != NFSERR_RETVOID) { | |
2d21ac55 | 6205 | nfsm_chain_add_32(error, &nmrep, |
0a7de745 A |
6206 | (err ? nfsrv_errmap(nd, err) : 0)); |
6207 | } | |
2d21ac55 | 6208 | break; |
fa4905b1 | 6209 | } |
1c79356b | 6210 | } |
2d21ac55 A |
6211 | |
6212 | done: | |
6213 | nfsm_chain_build_done(error, &nmrep); | |
6214 | if (error) { | |
6215 | /* error composing reply header */ | |
6216 | /* XXX should we keep statistics for these errors? */ | |
6217 | mbuf_freem(mrep); | |
0a7de745 | 6218 | return error; |
2d21ac55 A |
6219 | } |
6220 | ||
6221 | *nmrepp = nmrep; | |
0a7de745 | 6222 | if ((err != 0) && (err != NFSERR_RETVOID)) { |
316670eb | 6223 | OSAddAtomic64(1, &nfsstats.srvrpc_errs); |
0a7de745 A |
6224 | } |
6225 | return 0; | |
1c79356b A |
6226 | } |
6227 | ||
6228 | /* | |
2d21ac55 A |
6229 | * The nfs server send routine. |
6230 | * | |
6231 | * - return EINTR or ERESTART if interrupted by a signal | |
6232 | * - return EPIPE if a connection is lost for connection based sockets (TCP...) | |
6233 | * - do any cleanup required by recoverable socket errors (???) | |
1c79356b | 6234 | */ |
2d21ac55 A |
6235 | int |
6236 | nfsrv_send(struct nfsrv_sock *slp, mbuf_t nam, mbuf_t top) | |
1c79356b | 6237 | { |
2d21ac55 A |
6238 | int error; |
6239 | socket_t so = slp->ns_so; | |
6240 | struct sockaddr *sendnam; | |
6241 | struct msghdr msg; | |
1c79356b | 6242 | |
2d21ac55 A |
6243 | bzero(&msg, sizeof(msg)); |
6244 | if (nam && !sock_isconnected(so) && (slp->ns_sotype != SOCK_STREAM)) { | |
6245 | if ((sendnam = mbuf_data(nam))) { | |
6246 | msg.msg_name = (caddr_t)sendnam; | |
6247 | msg.msg_namelen = sendnam->sa_len; | |
6248 | } | |
1c79356b | 6249 | } |
2d21ac55 | 6250 | error = sock_sendmbuf(so, &msg, top, 0, NULL); |
0a7de745 A |
6251 | if (!error) { |
6252 | return 0; | |
6253 | } | |
2d21ac55 A |
6254 | log(LOG_INFO, "nfsd send error %d\n", error); |
6255 | ||
0a7de745 | 6256 | if ((error == EWOULDBLOCK) && (slp->ns_sotype == SOCK_STREAM)) { |
2d21ac55 | 6257 | error = EPIPE; /* zap TCP sockets if they time out on send */ |
0a7de745 | 6258 | } |
2d21ac55 A |
6259 | /* Handle any recoverable (soft) socket errors here. (???) */ |
6260 | if (error != EINTR && error != ERESTART && error != EIO && | |
0a7de745 | 6261 | error != EWOULDBLOCK && error != EPIPE) { |
2d21ac55 | 6262 | error = 0; |
0a7de745 | 6263 | } |
2d21ac55 | 6264 | |
0a7de745 | 6265 | return error; |
2d21ac55 | 6266 | } |
1c79356b | 6267 | |
1c79356b A |
6268 | /* |
6269 | * Socket upcall routine for the nfsd sockets. | |
2d21ac55 | 6270 | * The caddr_t arg is a pointer to the "struct nfsrv_sock". |
1c79356b | 6271 | * Essentially do as much as possible non-blocking, else punt and it will |
91447636 | 6272 | * be called with MBUF_WAITOK from an nfsd. |
1c79356b A |
6273 | */ |
6274 | void | |
6d2010ae | 6275 | nfsrv_rcv(socket_t so, void *arg, int waitflag) |
1c79356b | 6276 | { |
6d2010ae | 6277 | struct nfsrv_sock *slp = arg; |
1c79356b | 6278 | |
0a7de745 | 6279 | if (!nfsd_thread_count || !(slp->ns_flag & SLP_VALID)) { |
1c79356b | 6280 | return; |
0a7de745 | 6281 | } |
91447636 A |
6282 | |
6283 | lck_rw_lock_exclusive(&slp->ns_rwlock); | |
6284 | nfsrv_rcv_locked(so, slp, waitflag); | |
6285 | /* Note: ns_rwlock gets dropped when called with MBUF_DONTWAIT */ | |
6286 | } | |
6287 | void | |
2d21ac55 | 6288 | nfsrv_rcv_locked(socket_t so, struct nfsrv_sock *slp, int waitflag) |
91447636 A |
6289 | { |
6290 | mbuf_t m, mp, mhck, m2; | |
0a7de745 A |
6291 | int ns_flag = 0, error; |
6292 | struct msghdr msg; | |
91447636 A |
6293 | size_t bytes_read; |
6294 | ||
6295 | if ((slp->ns_flag & SLP_VALID) == 0) { | |
0a7de745 | 6296 | if (waitflag == MBUF_DONTWAIT) { |
91447636 | 6297 | lck_rw_done(&slp->ns_rwlock); |
0a7de745 | 6298 | } |
91447636 A |
6299 | return; |
6300 | } | |
6301 | ||
1c79356b A |
6302 | #ifdef notdef |
6303 | /* | |
6304 | * Define this to test for nfsds handling this under heavy load. | |
6305 | */ | |
91447636 A |
6306 | if (waitflag == MBUF_DONTWAIT) { |
6307 | ns_flag = SLP_NEEDQ; | |
55e303ae | 6308 | goto dorecs; |
1c79356b A |
6309 | } |
6310 | #endif | |
91447636 | 6311 | if (slp->ns_sotype == SOCK_STREAM) { |
1c79356b A |
6312 | /* |
6313 | * If there are already records on the queue, defer soreceive() | |
b7266188 | 6314 | * to an(other) nfsd so that there is feedback to the TCP layer that |
1c79356b A |
6315 | * the nfs servers are heavily loaded. |
6316 | */ | |
b7266188 | 6317 | if (slp->ns_rec) { |
91447636 | 6318 | ns_flag = SLP_NEEDQ; |
1c79356b A |
6319 | goto dorecs; |
6320 | } | |
6321 | ||
6322 | /* | |
6323 | * Do soreceive(). | |
6324 | */ | |
91447636 A |
6325 | bytes_read = 1000000000; |
6326 | error = sock_receivembuf(so, NULL, &mp, MSG_DONTWAIT, &bytes_read); | |
6327 | if (error || mp == NULL) { | |
0a7de745 | 6328 | if (error == EWOULDBLOCK) { |
2d21ac55 | 6329 | ns_flag = (waitflag == MBUF_DONTWAIT) ? SLP_NEEDQ : 0; |
0a7de745 | 6330 | } else { |
91447636 | 6331 | ns_flag = SLP_DISCONN; |
0a7de745 | 6332 | } |
1c79356b A |
6333 | goto dorecs; |
6334 | } | |
6335 | m = mp; | |
6336 | if (slp->ns_rawend) { | |
0a7de745 | 6337 | if ((error = mbuf_setnext(slp->ns_rawend, m))) { |
91447636 | 6338 | panic("nfsrv_rcv: mbuf_setnext failed %d\n", error); |
0a7de745 | 6339 | } |
91447636 | 6340 | slp->ns_cc += bytes_read; |
1c79356b A |
6341 | } else { |
6342 | slp->ns_raw = m; | |
91447636 | 6343 | slp->ns_cc = bytes_read; |
1c79356b | 6344 | } |
0a7de745 | 6345 | while ((m2 = mbuf_next(m))) { |
91447636 | 6346 | m = m2; |
0a7de745 | 6347 | } |
1c79356b A |
6348 | slp->ns_rawend = m; |
6349 | ||
6350 | /* | |
6351 | * Now try and parse record(s) out of the raw stream data. | |
6352 | */ | |
6353 | error = nfsrv_getstream(slp, waitflag); | |
6354 | if (error) { | |
0a7de745 | 6355 | if (error == EPERM) { |
91447636 | 6356 | ns_flag = SLP_DISCONN; |
0a7de745 | 6357 | } else { |
91447636 | 6358 | ns_flag = SLP_NEEDQ; |
0a7de745 | 6359 | } |
1c79356b A |
6360 | } |
6361 | } else { | |
0a7de745 | 6362 | struct sockaddr_storage nam; |
2d21ac55 A |
6363 | |
6364 | if (slp->ns_reccnt >= nfsrv_sock_max_rec_queue_length) { | |
6365 | /* already have max # RPC records queued on this socket */ | |
6366 | ns_flag = SLP_NEEDQ; | |
6367 | goto dorecs; | |
6368 | } | |
316670eb | 6369 | |
91447636 A |
6370 | bzero(&msg, sizeof(msg)); |
6371 | msg.msg_name = (caddr_t)&nam; | |
6372 | msg.msg_namelen = sizeof(nam); | |
316670eb | 6373 | |
1c79356b | 6374 | do { |
91447636 A |
6375 | bytes_read = 1000000000; |
6376 | error = sock_receivembuf(so, &msg, &mp, MSG_DONTWAIT | MSG_NEEDSA, &bytes_read); | |
1c79356b | 6377 | if (mp) { |
91447636 A |
6378 | if (msg.msg_name && (mbuf_get(MBUF_WAITOK, MBUF_TYPE_SONAME, &mhck) == 0)) { |
6379 | mbuf_setlen(mhck, nam.ss_len); | |
6380 | bcopy(&nam, mbuf_data(mhck), nam.ss_len); | |
1c79356b | 6381 | m = mhck; |
91447636 A |
6382 | if (mbuf_setnext(m, mp)) { |
6383 | /* trouble... just drop it */ | |
6384 | printf("nfsrv_rcv: mbuf_setnext failed\n"); | |
6385 | mbuf_free(mhck); | |
6386 | m = mp; | |
6387 | } | |
6388 | } else { | |
1c79356b | 6389 | m = mp; |
91447636 | 6390 | } |
0a7de745 | 6391 | if (slp->ns_recend) { |
91447636 | 6392 | mbuf_setnextpkt(slp->ns_recend, m); |
0a7de745 | 6393 | } else { |
1c79356b | 6394 | slp->ns_rec = m; |
2d21ac55 A |
6395 | slp->ns_flag |= SLP_DOREC; |
6396 | } | |
1c79356b | 6397 | slp->ns_recend = m; |
91447636 | 6398 | mbuf_setnextpkt(m, NULL); |
2d21ac55 | 6399 | slp->ns_reccnt++; |
4a249263 | 6400 | } |
1c79356b A |
6401 | } while (mp); |
6402 | } | |
6403 | ||
6404 | /* | |
6405 | * Now try and process the request records, non-blocking. | |
6406 | */ | |
6407 | dorecs: | |
0a7de745 | 6408 | if (ns_flag) { |
91447636 | 6409 | slp->ns_flag |= ns_flag; |
0a7de745 | 6410 | } |
91447636 | 6411 | if (waitflag == MBUF_DONTWAIT) { |
2d21ac55 | 6412 | int wake = (slp->ns_flag & SLP_WORKTODO); |
91447636 | 6413 | lck_rw_done(&slp->ns_rwlock); |
2d21ac55 | 6414 | if (wake && nfsd_thread_count) { |
91447636 A |
6415 | lck_mtx_lock(nfsd_mutex); |
6416 | nfsrv_wakenfsd(slp); | |
6417 | lck_mtx_unlock(nfsd_mutex); | |
6418 | } | |
1c79356b A |
6419 | } |
6420 | } | |
6421 | ||
6422 | /* | |
6423 | * Try and extract an RPC request from the mbuf data list received on a | |
6424 | * stream socket. The "waitflag" argument indicates whether or not it | |
6425 | * can sleep. | |
6426 | */ | |
b0d623f7 | 6427 | int |
2d21ac55 | 6428 | nfsrv_getstream(struct nfsrv_sock *slp, int waitflag) |
1c79356b | 6429 | { |
91447636 A |
6430 | mbuf_t m; |
6431 | char *cp1, *cp2, *mdata; | |
6432 | int len, mlen, error; | |
6433 | mbuf_t om, m2, recm; | |
b0d623f7 | 6434 | u_int32_t recmark; |
1c79356b | 6435 | |
0a7de745 | 6436 | if (slp->ns_flag & SLP_GETSTREAM) { |
1c79356b | 6437 | panic("nfs getstream"); |
0a7de745 | 6438 | } |
91447636 | 6439 | slp->ns_flag |= SLP_GETSTREAM; |
1c79356b | 6440 | for (;;) { |
0a7de745 A |
6441 | if (slp->ns_reclen == 0) { |
6442 | if (slp->ns_cc < NFSX_UNSIGNED) { | |
6443 | slp->ns_flag &= ~SLP_GETSTREAM; | |
6444 | return 0; | |
6445 | } | |
6446 | m = slp->ns_raw; | |
6447 | mdata = mbuf_data(m); | |
6448 | mlen = mbuf_len(m); | |
6449 | if (mlen >= NFSX_UNSIGNED) { | |
6450 | bcopy(mdata, (caddr_t)&recmark, NFSX_UNSIGNED); | |
6451 | mdata += NFSX_UNSIGNED; | |
6452 | mlen -= NFSX_UNSIGNED; | |
6453 | mbuf_setdata(m, mdata, mlen); | |
6454 | } else { | |
6455 | cp1 = (caddr_t)&recmark; | |
6456 | cp2 = mdata; | |
6457 | while (cp1 < ((caddr_t)&recmark) + NFSX_UNSIGNED) { | |
6458 | while (mlen == 0) { | |
6459 | m = mbuf_next(m); | |
6460 | cp2 = mbuf_data(m); | |
6461 | mlen = mbuf_len(m); | |
6462 | } | |
6463 | *cp1++ = *cp2++; | |
6464 | mlen--; | |
6465 | mbuf_setdata(m, cp2, mlen); | |
1c79356b | 6466 | } |
0a7de745 A |
6467 | } |
6468 | slp->ns_cc -= NFSX_UNSIGNED; | |
6469 | recmark = ntohl(recmark); | |
6470 | slp->ns_reclen = recmark & ~0x80000000; | |
6471 | if (recmark & 0x80000000) { | |
6472 | slp->ns_flag |= SLP_LASTFRAG; | |
6473 | } else { | |
6474 | slp->ns_flag &= ~SLP_LASTFRAG; | |
6475 | } | |
6476 | if (slp->ns_reclen <= 0 || slp->ns_reclen > NFS_MAXPACKET) { | |
6477 | slp->ns_flag &= ~SLP_GETSTREAM; | |
6478 | return EPERM; | |
6479 | } | |
6480 | } | |
6481 | ||
6482 | /* | |
6483 | * Now get the record part. | |
6484 | * | |
6485 | * Note that slp->ns_reclen may be 0. Linux sometimes | |
6486 | * generates 0-length RPCs | |
6487 | */ | |
6488 | recm = NULL; | |
6489 | if (slp->ns_cc == slp->ns_reclen) { | |
6490 | recm = slp->ns_raw; | |
6491 | slp->ns_raw = slp->ns_rawend = NULL; | |
6492 | slp->ns_cc = slp->ns_reclen = 0; | |
6493 | } else if (slp->ns_cc > slp->ns_reclen) { | |
6494 | len = 0; | |
6495 | m = slp->ns_raw; | |
6496 | mlen = mbuf_len(m); | |
6497 | mdata = mbuf_data(m); | |
6498 | om = NULL; | |
6499 | while (len < slp->ns_reclen) { | |
6500 | if ((len + mlen) > slp->ns_reclen) { | |
6501 | if (mbuf_copym(m, 0, slp->ns_reclen - len, waitflag, &m2)) { | |
91447636 | 6502 | slp->ns_flag &= ~SLP_GETSTREAM; |
0a7de745 A |
6503 | return EWOULDBLOCK; |
6504 | } | |
6505 | if (om) { | |
6506 | if (mbuf_setnext(om, m2)) { | |
6507 | /* trouble... just drop it */ | |
6508 | printf("nfsrv_getstream: mbuf_setnext failed\n"); | |
6509 | mbuf_freem(m2); | |
6510 | slp->ns_flag &= ~SLP_GETSTREAM; | |
6511 | return EWOULDBLOCK; | |
6512 | } | |
6513 | recm = slp->ns_raw; | |
6514 | } else { | |
6515 | recm = m2; | |
91447636 | 6516 | } |
0a7de745 A |
6517 | mdata += slp->ns_reclen - len; |
6518 | mlen -= slp->ns_reclen - len; | |
6519 | mbuf_setdata(m, mdata, mlen); | |
6520 | len = slp->ns_reclen; | |
6521 | } else if ((len + mlen) == slp->ns_reclen) { | |
6522 | om = m; | |
6523 | len += mlen; | |
6524 | m = mbuf_next(m); | |
91447636 | 6525 | recm = slp->ns_raw; |
0a7de745 A |
6526 | if (mbuf_setnext(om, NULL)) { |
6527 | printf("nfsrv_getstream: mbuf_setnext failed 2\n"); | |
6528 | slp->ns_flag &= ~SLP_GETSTREAM; | |
6529 | return EWOULDBLOCK; | |
6530 | } | |
6531 | mlen = mbuf_len(m); | |
6532 | mdata = mbuf_data(m); | |
91447636 | 6533 | } else { |
0a7de745 A |
6534 | om = m; |
6535 | len += mlen; | |
6536 | m = mbuf_next(m); | |
6537 | mlen = mbuf_len(m); | |
6538 | mdata = mbuf_data(m); | |
91447636 | 6539 | } |
0a7de745 A |
6540 | } |
6541 | slp->ns_raw = m; | |
6542 | slp->ns_cc -= len; | |
6543 | slp->ns_reclen = 0; | |
6544 | } else { | |
6545 | slp->ns_flag &= ~SLP_GETSTREAM; | |
6546 | return 0; | |
6547 | } | |
6548 | ||
6549 | /* | |
6550 | * Accumulate the fragments into a record. | |
6551 | */ | |
6552 | if (slp->ns_frag == NULL) { | |
6553 | slp->ns_frag = recm; | |
6554 | } else { | |
6555 | m = slp->ns_frag; | |
6556 | while ((m2 = mbuf_next(m))) { | |
6557 | m = m2; | |
6558 | } | |
6559 | if ((error = mbuf_setnext(m, recm))) { | |
6560 | panic("nfsrv_getstream: mbuf_setnext failed 3, %d\n", error); | |
6561 | } | |
6562 | } | |
6563 | if (slp->ns_flag & SLP_LASTFRAG) { | |
6564 | if (slp->ns_recend) { | |
6565 | mbuf_setnextpkt(slp->ns_recend, slp->ns_frag); | |
1c79356b | 6566 | } else { |
0a7de745 A |
6567 | slp->ns_rec = slp->ns_frag; |
6568 | slp->ns_flag |= SLP_DOREC; | |
6569 | } | |
6570 | slp->ns_recend = slp->ns_frag; | |
6571 | slp->ns_frag = NULL; | |
6572 | } | |
1c79356b A |
6573 | } |
6574 | } | |
6575 | ||
6576 | /* | |
6577 | * Parse an RPC header. | |
6578 | */ | |
6579 | int | |
2d21ac55 A |
6580 | nfsrv_dorec( |
6581 | struct nfsrv_sock *slp, | |
6582 | struct nfsd *nfsd, | |
6583 | struct nfsrv_descript **ndp) | |
1c79356b | 6584 | { |
91447636 A |
6585 | mbuf_t m; |
6586 | mbuf_t nam; | |
6587 | struct nfsrv_descript *nd; | |
2d21ac55 | 6588 | int error = 0; |
1c79356b A |
6589 | |
6590 | *ndp = NULL; | |
0a7de745 A |
6591 | if (!(slp->ns_flag & (SLP_VALID | SLP_DOREC)) || (slp->ns_rec == NULL)) { |
6592 | return ENOBUFS; | |
6593 | } | |
91447636 | 6594 | MALLOC_ZONE(nd, struct nfsrv_descript *, |
0a7de745 A |
6595 | sizeof(struct nfsrv_descript), M_NFSRVDESC, M_WAITOK); |
6596 | if (!nd) { | |
6597 | return ENOMEM; | |
6598 | } | |
91447636 A |
6599 | m = slp->ns_rec; |
6600 | slp->ns_rec = mbuf_nextpkt(m); | |
0a7de745 | 6601 | if (slp->ns_rec) { |
91447636 | 6602 | mbuf_setnextpkt(m, NULL); |
0a7de745 | 6603 | } else { |
2d21ac55 | 6604 | slp->ns_flag &= ~SLP_DOREC; |
91447636 | 6605 | slp->ns_recend = NULL; |
2d21ac55 A |
6606 | } |
6607 | slp->ns_reccnt--; | |
91447636 | 6608 | if (mbuf_type(m) == MBUF_TYPE_SONAME) { |
1c79356b | 6609 | nam = m; |
91447636 | 6610 | m = mbuf_next(m); |
0a7de745 | 6611 | if ((error = mbuf_setnext(nam, NULL))) { |
91447636 | 6612 | panic("nfsrv_dorec: mbuf_setnext failed %d\n", error); |
0a7de745 A |
6613 | } |
6614 | } else { | |
1c79356b | 6615 | nam = NULL; |
0a7de745 | 6616 | } |
1c79356b | 6617 | nd->nd_nam2 = nam; |
2d21ac55 | 6618 | nfsm_chain_dissect_init(error, &nd->nd_nmreq, m); |
0a7de745 | 6619 | if (!error) { |
2d21ac55 | 6620 | error = nfsrv_getreq(nd); |
0a7de745 | 6621 | } |
1c79356b | 6622 | if (error) { |
0a7de745 | 6623 | if (nam) { |
91447636 | 6624 | mbuf_freem(nam); |
0a7de745 A |
6625 | } |
6626 | if (nd->nd_gss_context) { | |
6d2010ae | 6627 | nfs_gss_svc_ctx_deref(nd->nd_gss_context); |
0a7de745 | 6628 | } |
2d21ac55 | 6629 | FREE_ZONE(nd, sizeof(*nd), M_NFSRVDESC); |
0a7de745 | 6630 | return error; |
1c79356b | 6631 | } |
2d21ac55 | 6632 | nd->nd_mrep = NULL; |
1c79356b A |
6633 | *ndp = nd; |
6634 | nfsd->nfsd_nd = nd; | |
0a7de745 | 6635 | return 0; |
1c79356b A |
6636 | } |
6637 | ||
6638 | /* | |
6639 | * Parse an RPC request | |
6640 | * - verify it | |
6641 | * - fill in the cred struct. | |
6642 | */ | |
b0d623f7 | 6643 | int |
2d21ac55 | 6644 | nfsrv_getreq(struct nfsrv_descript *nd) |
1c79356b | 6645 | { |
2d21ac55 | 6646 | struct nfsm_chain *nmreq; |
91447636 | 6647 | int len, i; |
b0d623f7 | 6648 | u_int32_t nfsvers, auth_type; |
2d21ac55 | 6649 | int error = 0; |
91447636 A |
6650 | uid_t user_id; |
6651 | gid_t group_id; | |
6652 | int ngroups; | |
2d21ac55 | 6653 | uint32_t val; |
1c79356b | 6654 | |
91447636 | 6655 | nd->nd_cr = NULL; |
2d21ac55 A |
6656 | nd->nd_gss_context = NULL; |
6657 | nd->nd_gss_seqnum = 0; | |
6658 | nd->nd_gss_mb = NULL; | |
6659 | ||
6660 | user_id = group_id = -2; | |
6661 | val = auth_type = len = 0; | |
6662 | ||
6663 | nmreq = &nd->nd_nmreq; | |
0a7de745 A |
6664 | nfsm_chain_get_32(error, nmreq, nd->nd_retxid); // XID |
6665 | nfsm_chain_get_32(error, nmreq, val); // RPC Call | |
6666 | if (!error && (val != RPC_CALL)) { | |
2d21ac55 | 6667 | error = EBADRPC; |
0a7de745 | 6668 | } |
2d21ac55 | 6669 | nfsmout_if(error); |
1c79356b | 6670 | nd->nd_repstat = 0; |
0a7de745 | 6671 | nfsm_chain_get_32(error, nmreq, val); // RPC Version |
2d21ac55 A |
6672 | nfsmout_if(error); |
6673 | if (val != RPC_VER2) { | |
1c79356b A |
6674 | nd->nd_repstat = ERPCMISMATCH; |
6675 | nd->nd_procnum = NFSPROC_NOOP; | |
0a7de745 | 6676 | return 0; |
1c79356b | 6677 | } |
0a7de745 | 6678 | nfsm_chain_get_32(error, nmreq, val); // RPC Program Number |
2d21ac55 A |
6679 | nfsmout_if(error); |
6680 | if (val != NFS_PROG) { | |
91447636 A |
6681 | nd->nd_repstat = EPROGUNAVAIL; |
6682 | nd->nd_procnum = NFSPROC_NOOP; | |
0a7de745 | 6683 | return 0; |
1c79356b | 6684 | } |
2d21ac55 A |
6685 | nfsm_chain_get_32(error, nmreq, nfsvers);// NFS Version Number |
6686 | nfsmout_if(error); | |
91447636 | 6687 | if ((nfsvers < NFS_VER2) || (nfsvers > NFS_VER3)) { |
1c79356b A |
6688 | nd->nd_repstat = EPROGMISMATCH; |
6689 | nd->nd_procnum = NFSPROC_NOOP; | |
0a7de745 | 6690 | return 0; |
1c79356b | 6691 | } |
2d21ac55 A |
6692 | nd->nd_vers = nfsvers; |
6693 | nfsm_chain_get_32(error, nmreq, nd->nd_procnum);// NFS Procedure Number | |
6694 | nfsmout_if(error); | |
91447636 | 6695 | if ((nd->nd_procnum >= NFS_NPROCS) || |
0a7de745 | 6696 | ((nd->nd_vers == NFS_VER2) && (nd->nd_procnum > NFSV2PROC_STATFS))) { |
1c79356b A |
6697 | nd->nd_repstat = EPROCUNAVAIL; |
6698 | nd->nd_procnum = NFSPROC_NOOP; | |
0a7de745 | 6699 | return 0; |
1c79356b | 6700 | } |
0a7de745 | 6701 | if (nfsvers != NFS_VER3) { |
1c79356b | 6702 | nd->nd_procnum = nfsv3_procid[nd->nd_procnum]; |
0a7de745 A |
6703 | } |
6704 | nfsm_chain_get_32(error, nmreq, auth_type); // Auth Flavor | |
6705 | nfsm_chain_get_32(error, nmreq, len); // Auth Length | |
6706 | if (!error && (len < 0 || len > RPCAUTH_MAXSIZ)) { | |
2d21ac55 | 6707 | error = EBADRPC; |
0a7de745 | 6708 | } |
2d21ac55 A |
6709 | nfsmout_if(error); |
6710 | ||
6711 | /* Handle authentication */ | |
6d2010ae A |
6712 | if (auth_type == RPCAUTH_SYS) { |
6713 | struct posix_cred temp_pcred; | |
0a7de745 A |
6714 | if (nd->nd_procnum == NFSPROC_NULL) { |
6715 | return 0; | |
6716 | } | |
6d2010ae | 6717 | nd->nd_sec = RPCAUTH_SYS; |
0a7de745 A |
6718 | nfsm_chain_adv(error, nmreq, NFSX_UNSIGNED); // skip stamp |
6719 | nfsm_chain_get_32(error, nmreq, len); // hostname length | |
6720 | if (len < 0 || len > NFS_MAXNAMLEN) { | |
2d21ac55 | 6721 | error = EBADRPC; |
0a7de745 A |
6722 | } |
6723 | nfsm_chain_adv(error, nmreq, nfsm_rndup(len)); // skip hostname | |
2d21ac55 A |
6724 | nfsmout_if(error); |
6725 | ||
6726 | /* create a temporary credential using the bits from the wire */ | |
6d2010ae | 6727 | bzero(&temp_pcred, sizeof(temp_pcred)); |
2d21ac55 A |
6728 | nfsm_chain_get_32(error, nmreq, user_id); |
6729 | nfsm_chain_get_32(error, nmreq, group_id); | |
6d2010ae | 6730 | temp_pcred.cr_groups[0] = group_id; |
0a7de745 A |
6731 | nfsm_chain_get_32(error, nmreq, len); // extra GID count |
6732 | if ((len < 0) || (len > RPCAUTH_UNIXGIDS)) { | |
2d21ac55 | 6733 | error = EBADRPC; |
0a7de745 | 6734 | } |
2d21ac55 | 6735 | nfsmout_if(error); |
0a7de745 A |
6736 | for (i = 1; i <= len; i++) { |
6737 | if (i < NGROUPS) { | |
6d2010ae | 6738 | nfsm_chain_get_32(error, nmreq, temp_pcred.cr_groups[i]); |
0a7de745 | 6739 | } else { |
2d21ac55 | 6740 | nfsm_chain_adv(error, nmreq, NFSX_UNSIGNED); |
0a7de745 A |
6741 | } |
6742 | } | |
2d21ac55 | 6743 | nfsmout_if(error); |
91447636 | 6744 | ngroups = (len >= NGROUPS) ? NGROUPS : (len + 1); |
0a7de745 | 6745 | if (ngroups > 1) { |
6d2010ae | 6746 | nfsrv_group_sort(&temp_pcred.cr_groups[0], ngroups); |
0a7de745 A |
6747 | } |
6748 | nfsm_chain_adv(error, nmreq, NFSX_UNSIGNED); // verifier flavor (should be AUTH_NONE) | |
6749 | nfsm_chain_get_32(error, nmreq, len); // verifier length | |
6750 | if (len < 0 || len > RPCAUTH_MAXSIZ) { | |
2d21ac55 | 6751 | error = EBADRPC; |
0a7de745 A |
6752 | } |
6753 | if (len > 0) { | |
2d21ac55 | 6754 | nfsm_chain_adv(error, nmreq, nfsm_rndup(len)); |
0a7de745 | 6755 | } |
2d21ac55 A |
6756 | |
6757 | /* request creation of a real credential */ | |
6d2010ae A |
6758 | temp_pcred.cr_uid = user_id; |
6759 | temp_pcred.cr_ngroups = ngroups; | |
6760 | nd->nd_cr = posix_cred_create(&temp_pcred); | |
91447636 A |
6761 | if (nd->nd_cr == NULL) { |
6762 | nd->nd_repstat = ENOMEM; | |
6763 | nd->nd_procnum = NFSPROC_NOOP; | |
0a7de745 | 6764 | return 0; |
91447636 | 6765 | } |
2d21ac55 A |
6766 | } else if (auth_type == RPCSEC_GSS) { |
6767 | error = nfs_gss_svc_cred_get(nd, nmreq); | |
6768 | if (error) { | |
0a7de745 A |
6769 | if (error == EINVAL) { |
6770 | goto nfsmout; // drop the request | |
6771 | } | |
2d21ac55 A |
6772 | nd->nd_repstat = error; |
6773 | nd->nd_procnum = NFSPROC_NOOP; | |
0a7de745 | 6774 | return 0; |
2d21ac55 | 6775 | } |
1c79356b | 6776 | } else { |
0a7de745 A |
6777 | if (nd->nd_procnum == NFSPROC_NULL) { // assume it's AUTH_NONE |
6778 | return 0; | |
6779 | } | |
1c79356b A |
6780 | nd->nd_repstat = (NFSERR_AUTHERR | AUTH_REJECTCRED); |
6781 | nd->nd_procnum = NFSPROC_NOOP; | |
0a7de745 | 6782 | return 0; |
1c79356b | 6783 | } |
0a7de745 | 6784 | return 0; |
1c79356b | 6785 | nfsmout: |
0a7de745 | 6786 | if (IS_VALID_CRED(nd->nd_cr)) { |
0c530ab8 | 6787 | kauth_cred_unref(&nd->nd_cr); |
0a7de745 | 6788 | } |
2d21ac55 | 6789 | nfsm_chain_cleanup(nmreq); |
0a7de745 | 6790 | return error; |
1c79356b A |
6791 | } |
6792 | ||
6793 | /* | |
6794 | * Search for a sleeping nfsd and wake it up. | |
2d21ac55 A |
6795 | * SIDE EFFECT: If none found, make sure the socket is queued up so that one |
6796 | * of the running nfsds will go look for the work in the nfsrv_sockwait list. | |
91447636 | 6797 | * Note: Must be called with nfsd_mutex held. |
1c79356b A |
6798 | */ |
6799 | void | |
2d21ac55 | 6800 | nfsrv_wakenfsd(struct nfsrv_sock *slp) |
1c79356b | 6801 | { |
91447636 | 6802 | struct nfsd *nd; |
1c79356b | 6803 | |
0a7de745 | 6804 | if ((slp->ns_flag & SLP_VALID) == 0) { |
1c79356b | 6805 | return; |
0a7de745 | 6806 | } |
91447636 A |
6807 | |
6808 | lck_rw_lock_exclusive(&slp->ns_rwlock); | |
2d21ac55 A |
6809 | /* if there's work to do on this socket, make sure it's queued up */ |
6810 | if ((slp->ns_flag & SLP_WORKTODO) && !(slp->ns_flag & SLP_QUEUED)) { | |
6811 | TAILQ_INSERT_TAIL(&nfsrv_sockwait, slp, ns_svcq); | |
6812 | slp->ns_flag |= SLP_WAITQ; | |
1c79356b | 6813 | } |
91447636 A |
6814 | lck_rw_done(&slp->ns_rwlock); |
6815 | ||
2d21ac55 A |
6816 | /* wake up a waiting nfsd, if possible */ |
6817 | nd = TAILQ_FIRST(&nfsd_queue); | |
0a7de745 | 6818 | if (!nd) { |
2d21ac55 | 6819 | return; |
0a7de745 | 6820 | } |
2d21ac55 A |
6821 | |
6822 | TAILQ_REMOVE(&nfsd_queue, nd, nfsd_queue); | |
6823 | nd->nfsd_flag &= ~NFSD_WAITING; | |
6824 | wakeup(nd); | |
1c79356b | 6825 | } |
2d21ac55 A |
6826 | |
6827 | #endif /* NFSSERVER */ |