]> git.saurik.com Git - apple/xnu.git/blob - bsd/nfs/nfs_syscalls.c
78d83c9519360381ec63a719ea5bece6eee0a5d0
[apple/xnu.git] / bsd / nfs / nfs_syscalls.c
1 /*
2 * Copyright (c) 2000-2018 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29 /*
30 * Copyright (c) 1989, 1993
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_syscalls.c 8.5 (Berkeley) 3/30/95
65 * FreeBSD-Id: nfs_syscalls.c,v 1.32 1997/11/07 08:53:25 phk Exp $
66 */
67 /*
68 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
69 * support for mandatory and extensible security protections. This notice
70 * is included in support of clause 2.2 (b) of the Apple Public License,
71 * Version 2.0.
72 */
73
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/kernel.h>
77 #include <sys/file_internal.h>
78 #include <sys/filedesc.h>
79 #include <sys/stat.h>
80 #include <sys/vnode_internal.h>
81 #include <sys/mount_internal.h>
82 #include <sys/proc_internal.h> /* for fdflags */
83 #include <sys/kauth.h>
84 #include <sys/sysctl.h>
85 #include <sys/ubc.h>
86 #include <sys/uio.h>
87 #include <sys/malloc.h>
88 #include <sys/kpi_mbuf.h>
89 #include <sys/socket.h>
90 #include <sys/socketvar.h>
91 #include <sys/domain.h>
92 #include <sys/protosw.h>
93 #include <sys/fcntl.h>
94 #include <sys/lockf.h>
95 #include <sys/syslog.h>
96 #include <sys/user.h>
97 #include <sys/sysproto.h>
98 #include <sys/kpi_socket.h>
99 #include <sys/fsevents.h>
100 #include <libkern/OSAtomic.h>
101 #include <kern/thread_call.h>
102 #include <kern/task.h>
103
104 #include <security/audit/audit.h>
105
106 #include <netinet/in.h>
107 #include <netinet/tcp.h>
108 #include <nfs/xdr_subs.h>
109 #include <nfs/rpcv2.h>
110 #include <nfs/nfsproto.h>
111 #include <nfs/nfs.h>
112 #include <nfs/nfsm_subs.h>
113 #include <nfs/nfsrvcache.h>
114 #include <nfs/nfs_gss.h>
115 #include <nfs/nfsmount.h>
116 #include <nfs/nfsnode.h>
117 #include <nfs/nfs_lock.h>
118 #if CONFIG_MACF
119 #include <security/mac_framework.h>
120 #endif
121
122 kern_return_t thread_terminate(thread_t); /* XXX */
123
124 #if NFSSERVER
125
126 extern const nfsrv_proc_t nfsrv_procs[NFS_NPROCS];
127
128 extern int nfsrv_wg_delay;
129 extern int nfsrv_wg_delay_v3;
130
131 static int nfsrv_require_resv_port = 0;
132 static time_t nfsrv_idlesock_timer_on = 0;
133 static int nfsrv_sock_tcp_cnt = 0;
134 #define NFSD_MIN_IDLE_TIMEOUT 30
135 static int nfsrv_sock_idle_timeout = 3600; /* One hour */
136
137 int nfssvc_export(user_addr_t argp);
138 int nfssvc_nfsd(void);
139 int nfssvc_addsock(socket_t, mbuf_t);
140 void nfsrv_zapsock(struct nfsrv_sock *);
141 void nfsrv_slpderef(struct nfsrv_sock *);
142 void nfsrv_slpfree(struct nfsrv_sock *);
143
144 #endif /* NFSSERVER */
145
146 /*
147 * sysctl stuff
148 */
149 SYSCTL_DECL(_vfs_generic);
150 SYSCTL_NODE(_vfs_generic, OID_AUTO, nfs, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "nfs hinge");
151
152 #if NFSCLIENT
153 SYSCTL_NODE(_vfs_generic_nfs, OID_AUTO, client, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "nfs client hinge");
154 SYSCTL_INT(_vfs_generic_nfs_client, OID_AUTO, initialdowndelay, CTLFLAG_RW | CTLFLAG_LOCKED, &nfs_tprintf_initial_delay, 0, "");
155 SYSCTL_INT(_vfs_generic_nfs_client, OID_AUTO, nextdowndelay, CTLFLAG_RW | CTLFLAG_LOCKED, &nfs_tprintf_delay, 0, "");
156 SYSCTL_INT(_vfs_generic_nfs_client, OID_AUTO, iosize, CTLFLAG_RW | CTLFLAG_LOCKED, &nfs_iosize, 0, "");
157 SYSCTL_INT(_vfs_generic_nfs_client, OID_AUTO, access_cache_timeout, CTLFLAG_RW | CTLFLAG_LOCKED, &nfs_access_cache_timeout, 0, "");
158 SYSCTL_INT(_vfs_generic_nfs_client, OID_AUTO, allow_async, CTLFLAG_RW | CTLFLAG_LOCKED, &nfs_allow_async, 0, "");
159 SYSCTL_INT(_vfs_generic_nfs_client, OID_AUTO, statfs_rate_limit, CTLFLAG_RW | CTLFLAG_LOCKED, &nfs_statfs_rate_limit, 0, "");
160 SYSCTL_INT(_vfs_generic_nfs_client, OID_AUTO, nfsiod_thread_max, CTLFLAG_RW | CTLFLAG_LOCKED, &nfsiod_thread_max, 0, "");
161 SYSCTL_INT(_vfs_generic_nfs_client, OID_AUTO, nfsiod_thread_count, CTLFLAG_RD | CTLFLAG_LOCKED, &nfsiod_thread_count, 0, "");
162 SYSCTL_INT(_vfs_generic_nfs_client, OID_AUTO, lockd_mounts, CTLFLAG_RD | CTLFLAG_LOCKED, &nfs_lockd_mounts, 0, "");
163 SYSCTL_INT(_vfs_generic_nfs_client, OID_AUTO, max_async_writes, CTLFLAG_RW | CTLFLAG_LOCKED, &nfs_max_async_writes, 0, "");
164 SYSCTL_INT(_vfs_generic_nfs_client, OID_AUTO, access_delete, CTLFLAG_RW | CTLFLAG_LOCKED, &nfs_access_delete, 0, "");
165 SYSCTL_INT(_vfs_generic_nfs_client, OID_AUTO, access_dotzfs, CTLFLAG_RW | CTLFLAG_LOCKED, &nfs_access_dotzfs, 0, "");
166 SYSCTL_INT(_vfs_generic_nfs_client, OID_AUTO, access_for_getattr, CTLFLAG_RW | CTLFLAG_LOCKED, &nfs_access_for_getattr, 0, "");
167 SYSCTL_INT(_vfs_generic_nfs_client, OID_AUTO, idmap_ctrl, CTLFLAG_RW | CTLFLAG_LOCKED, &nfs_idmap_ctrl, 0, "");
168 SYSCTL_INT(_vfs_generic_nfs_client, OID_AUTO, callback_port, CTLFLAG_RW | CTLFLAG_LOCKED, &nfs_callback_port, 0, "");
169 SYSCTL_INT(_vfs_generic_nfs_client, OID_AUTO, is_mobile, CTLFLAG_RW | CTLFLAG_LOCKED, &nfs_is_mobile, 0, "");
170 SYSCTL_INT(_vfs_generic_nfs_client, OID_AUTO, squishy_flags, CTLFLAG_RW | CTLFLAG_LOCKED, &nfs_squishy_flags, 0, "");
171 SYSCTL_UINT(_vfs_generic_nfs_client, OID_AUTO, debug_ctl, CTLFLAG_RW | CTLFLAG_LOCKED, &nfs_debug_ctl, 0, "");
172 SYSCTL_INT(_vfs_generic_nfs_client, OID_AUTO, readlink_nocache, CTLFLAG_RW | CTLFLAG_LOCKED, &nfs_readlink_nocache, 0, "");
173 #if CONFIG_NFS_GSS
174 SYSCTL_INT(_vfs_generic_nfs_client, OID_AUTO, root_steals_gss_context, CTLFLAG_RW | CTLFLAG_LOCKED, &nfs_root_steals_ctx, 0, "");
175 #endif
176 #if CONFIG_NFS4
177 SYSCTL_STRING(_vfs_generic_nfs_client, OID_AUTO, default_nfs4domain, CTLFLAG_RW | CTLFLAG_LOCKED, nfs4_default_domain, sizeof(nfs4_default_domain), "");
178 #endif
179 #endif /* NFSCLIENT */
180
181 #if NFSSERVER
182 SYSCTL_NODE(_vfs_generic_nfs, OID_AUTO, server, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "nfs server hinge");
183 SYSCTL_INT(_vfs_generic_nfs_server, OID_AUTO, wg_delay, CTLFLAG_RW | CTLFLAG_LOCKED, &nfsrv_wg_delay, 0, "");
184 SYSCTL_INT(_vfs_generic_nfs_server, OID_AUTO, wg_delay_v3, CTLFLAG_RW | CTLFLAG_LOCKED, &nfsrv_wg_delay_v3, 0, "");
185 SYSCTL_INT(_vfs_generic_nfs_server, OID_AUTO, require_resv_port, CTLFLAG_RW | CTLFLAG_LOCKED, &nfsrv_require_resv_port, 0, "");
186 SYSCTL_INT(_vfs_generic_nfs_server, OID_AUTO, async, CTLFLAG_RW | CTLFLAG_LOCKED, &nfsrv_async, 0, "");
187 SYSCTL_INT(_vfs_generic_nfs_server, OID_AUTO, export_hash_size, CTLFLAG_RW | CTLFLAG_LOCKED, &nfsrv_export_hash_size, 0, "");
188 SYSCTL_INT(_vfs_generic_nfs_server, OID_AUTO, reqcache_size, CTLFLAG_RW | CTLFLAG_LOCKED, &nfsrv_reqcache_size, 0, "");
189 SYSCTL_INT(_vfs_generic_nfs_server, OID_AUTO, request_queue_length, CTLFLAG_RW | CTLFLAG_LOCKED, &nfsrv_sock_max_rec_queue_length, 0, "");
190 SYSCTL_INT(_vfs_generic_nfs_server, OID_AUTO, user_stats, CTLFLAG_RW | CTLFLAG_LOCKED, &nfsrv_user_stat_enabled, 0, "");
191 SYSCTL_UINT(_vfs_generic_nfs_server, OID_AUTO, gss_context_ttl, CTLFLAG_RW | CTLFLAG_LOCKED, &nfsrv_gss_context_ttl, 0, "");
192 #if CONFIG_FSE
193 SYSCTL_INT(_vfs_generic_nfs_server, OID_AUTO, fsevents, CTLFLAG_RW | CTLFLAG_LOCKED, &nfsrv_fsevents_enabled, 0, "");
194 #endif
195 SYSCTL_INT(_vfs_generic_nfs_server, OID_AUTO, nfsd_thread_max, CTLFLAG_RW | CTLFLAG_LOCKED, &nfsd_thread_max, 0, "");
196 SYSCTL_INT(_vfs_generic_nfs_server, OID_AUTO, nfsd_thread_count, CTLFLAG_RD | CTLFLAG_LOCKED, &nfsd_thread_count, 0, "");
197 SYSCTL_INT(_vfs_generic_nfs_server, OID_AUTO, nfsd_sock_idle_timeout, CTLFLAG_RW | CTLFLAG_LOCKED, &nfsrv_sock_idle_timeout, 0, "");
198 SYSCTL_INT(_vfs_generic_nfs_server, OID_AUTO, nfsd_tcp_connections, CTLFLAG_RD | CTLFLAG_LOCKED, &nfsrv_sock_tcp_cnt, 0, "");
199 #ifdef NFS_UC_Q_DEBUG
200 SYSCTL_INT(_vfs_generic_nfs_server, OID_AUTO, use_upcall_svc, CTLFLAG_RW | CTLFLAG_LOCKED, &nfsrv_uc_use_proxy, 0, "");
201 SYSCTL_INT(_vfs_generic_nfs_server, OID_AUTO, upcall_queue_limit, CTLFLAG_RW | CTLFLAG_LOCKED, &nfsrv_uc_queue_limit, 0, "");
202 SYSCTL_INT(_vfs_generic_nfs_server, OID_AUTO, upcall_queue_max_seen, CTLFLAG_RW | CTLFLAG_LOCKED, &nfsrv_uc_queue_max_seen, 0, "");
203 SYSCTL_INT(_vfs_generic_nfs_server, OID_AUTO, upcall_queue_count, CTLFLAG_RD | CTLFLAG_LOCKED, __DECONST(int *, &nfsrv_uc_queue_count), 0, "");
204 #endif
205 #endif /* NFSSERVER */
206
207
208 #if NFSCLIENT
209
210 #if CONFIG_NFS4
211 static int
212 mapname2id(struct nfs_testmapid *map)
213 {
214 int error;
215 error = nfs4_id2guid(map->ntm_name, &map->ntm_guid, map->ntm_grpflag);
216 if (error) {
217 return error;
218 }
219
220 if (map->ntm_grpflag) {
221 error = kauth_cred_guid2gid(&map->ntm_guid, (gid_t *)&map->ntm_id);
222 } else {
223 error = kauth_cred_guid2uid(&map->ntm_guid, (uid_t *)&map->ntm_id);
224 }
225
226 return error;
227 }
228
229 static int
230 mapid2name(struct nfs_testmapid *map)
231 {
232 int error;
233 size_t len = sizeof(map->ntm_name);
234
235 if (map->ntm_grpflag) {
236 error = kauth_cred_gid2guid((gid_t)map->ntm_id, &map->ntm_guid);
237 } else {
238 error = kauth_cred_uid2guid((uid_t)map->ntm_id, &map->ntm_guid);
239 }
240
241 if (error) {
242 return error;
243 }
244
245 error = nfs4_guid2id(&map->ntm_guid, map->ntm_name, &len, map->ntm_grpflag);
246
247 return error;
248 }
249
250 static int
251 nfsclnt_testidmap(proc_t p, user_addr_t argp)
252 {
253 struct nfs_testmapid mapid;
254 int error, coerror;
255 size_t len = sizeof(mapid.ntm_name);
256
257 /* Let root make this call. */
258 error = proc_suser(p);
259 if (error) {
260 return error;
261 }
262
263 error = copyin(argp, &mapid, sizeof(mapid));
264 mapid.ntm_name[MAXIDNAMELEN - 1] = '\0';
265
266 if (error) {
267 return error;
268 }
269 switch (mapid.ntm_lookup) {
270 case NTM_NAME2ID:
271 error = mapname2id(&mapid);
272 break;
273 case NTM_ID2NAME:
274 error = mapid2name(&mapid);
275 break;
276 case NTM_NAME2GUID:
277 error = nfs4_id2guid(mapid.ntm_name, &mapid.ntm_guid, mapid.ntm_grpflag);
278 break;
279 case NTM_GUID2NAME:
280 error = nfs4_guid2id(&mapid.ntm_guid, mapid.ntm_name, &len, mapid.ntm_grpflag);
281 break;
282 default:
283 return EINVAL;
284 }
285
286 coerror = copyout(&mapid, argp, sizeof(mapid));
287
288 return error ? error : coerror;
289 }
290 #endif
291
292 int
293 nfsclnt(proc_t p, struct nfsclnt_args *uap, __unused int *retval)
294 {
295 struct lockd_ans la;
296 int error;
297
298 switch (uap->flag) {
299 case NFSCLNT_LOCKDANS:
300 error = copyin(uap->argp, &la, sizeof(la));
301 if (!error) {
302 error = nfslockdans(p, &la);
303 }
304 break;
305 case NFSCLNT_LOCKDNOTIFY:
306 error = nfslockdnotify(p, uap->argp);
307 break;
308 #if CONFIG_NFS4
309 case NFSCLNT_TESTIDMAP:
310 error = nfsclnt_testidmap(p, uap->argp);
311 break;
312 #endif
313 default:
314 error = EINVAL;
315 }
316 return error;
317 }
318
319
320 /*
321 * Asynchronous I/O threads for client NFS.
322 * They do read-ahead and write-behind operations on the block I/O cache.
323 *
324 * The pool of up to nfsiod_thread_max threads is launched on demand and exit
325 * when unused for a while. There are as many nfsiod structs as there are
326 * nfsiod threads; however there's no strict tie between a thread and a struct.
327 * Each thread puts an nfsiod on the free list and sleeps on it. When it wakes
328 * up, it removes the next struct nfsiod from the queue and services it. Then
329 * it will put the struct at the head of free list and sleep on it.
330 * Async requests will pull the next struct nfsiod from the head of the free list,
331 * put it on the work queue, and wake whatever thread is waiting on that struct.
332 */
333
334 /*
335 * nfsiod thread exit routine
336 *
337 * Must be called with nfsiod_mutex held so that the
338 * decision to terminate is atomic with the termination.
339 */
340 void
341 nfsiod_terminate(struct nfsiod *niod)
342 {
343 nfsiod_thread_count--;
344 lck_mtx_unlock(nfsiod_mutex);
345 if (niod) {
346 FREE(niod, M_TEMP);
347 } else {
348 printf("nfsiod: terminating without niod\n");
349 }
350 thread_terminate(current_thread());
351 /*NOTREACHED*/
352 }
353
354 /* nfsiod thread startup routine */
355 void
356 nfsiod_thread(void)
357 {
358 struct nfsiod *niod;
359 int error;
360
361 MALLOC(niod, struct nfsiod *, sizeof(struct nfsiod), M_TEMP, M_WAITOK);
362 if (!niod) {
363 lck_mtx_lock(nfsiod_mutex);
364 nfsiod_thread_count--;
365 wakeup(current_thread());
366 lck_mtx_unlock(nfsiod_mutex);
367 thread_terminate(current_thread());
368 /*NOTREACHED*/
369 }
370 bzero(niod, sizeof(*niod));
371 lck_mtx_lock(nfsiod_mutex);
372 TAILQ_INSERT_HEAD(&nfsiodfree, niod, niod_link);
373 wakeup(current_thread());
374 error = msleep0(niod, nfsiod_mutex, PWAIT | PDROP, "nfsiod", NFS_ASYNCTHREADMAXIDLE * hz, nfsiod_continue);
375 /* shouldn't return... so we have an error */
376 /* remove an old nfsiod struct and terminate */
377 lck_mtx_lock(nfsiod_mutex);
378 if ((niod = TAILQ_LAST(&nfsiodfree, nfsiodlist))) {
379 TAILQ_REMOVE(&nfsiodfree, niod, niod_link);
380 }
381 nfsiod_terminate(niod);
382 /*NOTREACHED*/
383 }
384
385 /*
386 * Start up another nfsiod thread.
387 * (unless we're already maxed out and there are nfsiods running)
388 */
389 int
390 nfsiod_start(void)
391 {
392 thread_t thd = THREAD_NULL;
393
394 lck_mtx_lock(nfsiod_mutex);
395 if ((nfsiod_thread_count >= NFSIOD_MAX) && (nfsiod_thread_count > 0)) {
396 lck_mtx_unlock(nfsiod_mutex);
397 return EBUSY;
398 }
399 nfsiod_thread_count++;
400 if (kernel_thread_start((thread_continue_t)nfsiod_thread, NULL, &thd) != KERN_SUCCESS) {
401 lck_mtx_unlock(nfsiod_mutex);
402 return EBUSY;
403 }
404 /* wait for the thread to complete startup */
405 msleep(thd, nfsiod_mutex, PWAIT | PDROP, "nfsiodw", NULL);
406 thread_deallocate(thd);
407 return 0;
408 }
409
410 /*
411 * Continuation for Asynchronous I/O threads for NFS client.
412 *
413 * Grab an nfsiod struct to work on, do some work, then drop it
414 */
415 int
416 nfsiod_continue(int error)
417 {
418 struct nfsiod *niod;
419 struct nfsmount *nmp;
420 struct nfsreq *req, *treq;
421 struct nfs_reqqhead iodq;
422 int morework;
423
424 lck_mtx_lock(nfsiod_mutex);
425 niod = TAILQ_FIRST(&nfsiodwork);
426 if (!niod) {
427 /* there's no work queued up */
428 /* remove an old nfsiod struct and terminate */
429 if ((niod = TAILQ_LAST(&nfsiodfree, nfsiodlist))) {
430 TAILQ_REMOVE(&nfsiodfree, niod, niod_link);
431 }
432 nfsiod_terminate(niod);
433 /*NOTREACHED*/
434 }
435 TAILQ_REMOVE(&nfsiodwork, niod, niod_link);
436
437 worktodo:
438 while ((nmp = niod->niod_nmp)) {
439 if (nmp == NULL) {
440 niod->niod_nmp = NULL;
441 break;
442 }
443
444 /*
445 * Service this mount's async I/O queue.
446 *
447 * In order to ensure some level of fairness between mounts,
448 * we grab all the work up front before processing it so any
449 * new work that arrives will be serviced on a subsequent
450 * iteration - and we have a chance to see if other work needs
451 * to be done (e.g. the delayed write queue needs to be pushed
452 * or other mounts are waiting for an nfsiod).
453 */
454 /* grab the current contents of the queue */
455 TAILQ_INIT(&iodq);
456 TAILQ_CONCAT(&iodq, &nmp->nm_iodq, r_achain);
457 /* Mark each iod request as being managed by an iod */
458 TAILQ_FOREACH(req, &iodq, r_achain) {
459 lck_mtx_lock(&req->r_mtx);
460 assert(!(req->r_flags & R_IOD));
461 req->r_flags |= R_IOD;
462 lck_mtx_unlock(&req->r_mtx);
463 }
464 lck_mtx_unlock(nfsiod_mutex);
465
466 /* process the queue */
467 TAILQ_FOREACH_SAFE(req, &iodq, r_achain, treq) {
468 TAILQ_REMOVE(&iodq, req, r_achain);
469 req->r_achain.tqe_next = NFSREQNOLIST;
470 req->r_callback.rcb_func(req);
471 }
472
473 /* now check if there's more/other work to be done */
474 lck_mtx_lock(nfsiod_mutex);
475 morework = !TAILQ_EMPTY(&nmp->nm_iodq);
476 if (!morework || !TAILQ_EMPTY(&nfsiodmounts)) {
477 /*
478 * we're going to stop working on this mount but if the
479 * mount still needs more work so queue it up
480 */
481 if (morework && nmp->nm_iodlink.tqe_next == NFSNOLIST) {
482 TAILQ_INSERT_TAIL(&nfsiodmounts, nmp, nm_iodlink);
483 }
484 nmp->nm_niod = NULL;
485 niod->niod_nmp = NULL;
486 }
487 }
488
489 /* loop if there's still a mount to work on */
490 if (!niod->niod_nmp && !TAILQ_EMPTY(&nfsiodmounts)) {
491 niod->niod_nmp = TAILQ_FIRST(&nfsiodmounts);
492 TAILQ_REMOVE(&nfsiodmounts, niod->niod_nmp, nm_iodlink);
493 niod->niod_nmp->nm_iodlink.tqe_next = NFSNOLIST;
494 }
495 if (niod->niod_nmp) {
496 goto worktodo;
497 }
498
499 /* queue ourselves back up - if there aren't too many threads running */
500 if (nfsiod_thread_count <= NFSIOD_MAX) {
501 TAILQ_INSERT_HEAD(&nfsiodfree, niod, niod_link);
502 error = msleep0(niod, nfsiod_mutex, PWAIT | PDROP, "nfsiod", NFS_ASYNCTHREADMAXIDLE * hz, nfsiod_continue);
503 /* shouldn't return... so we have an error */
504 /* remove an old nfsiod struct and terminate */
505 lck_mtx_lock(nfsiod_mutex);
506 if ((niod = TAILQ_LAST(&nfsiodfree, nfsiodlist))) {
507 TAILQ_REMOVE(&nfsiodfree, niod, niod_link);
508 }
509 }
510 nfsiod_terminate(niod);
511 /*NOTREACHED*/
512 return 0;
513 }
514
515 #endif /* NFSCLIENT */
516
517
518 #if NFSSERVER
519
520 /*
521 * NFS server system calls
522 * getfh() lives here too, but maybe should move to kern/vfs_syscalls.c
523 */
524
525 /*
526 * Get file handle system call
527 */
528 int
529 getfh(proc_t p, struct getfh_args *uap, __unused int *retval)
530 {
531 vnode_t vp;
532 struct nfs_filehandle nfh;
533 int error, fhlen, fidlen;
534 struct nameidata nd;
535 char path[MAXPATHLEN], *ptr;
536 size_t pathlen;
537 struct nfs_exportfs *nxfs;
538 struct nfs_export *nx;
539
540 /*
541 * Must be super user
542 */
543 error = proc_suser(p);
544 if (error) {
545 return error;
546 }
547
548 error = copyinstr(uap->fname, path, MAXPATHLEN, &pathlen);
549 if (!error) {
550 error = copyin(uap->fhp, &fhlen, sizeof(fhlen));
551 }
552 if (error) {
553 return error;
554 }
555 /* limit fh size to length specified (or v3 size by default) */
556 if ((fhlen != NFSV2_MAX_FH_SIZE) && (fhlen != NFSV3_MAX_FH_SIZE)) {
557 fhlen = NFSV3_MAX_FH_SIZE;
558 }
559 fidlen = fhlen - sizeof(struct nfs_exphandle);
560
561 if (!nfsrv_is_initialized()) {
562 return EINVAL;
563 }
564
565 NDINIT(&nd, LOOKUP, OP_LOOKUP, FOLLOW | LOCKLEAF | AUDITVNPATH1,
566 UIO_SYSSPACE, CAST_USER_ADDR_T(path), vfs_context_current());
567 error = namei(&nd);
568 if (error) {
569 return error;
570 }
571 nameidone(&nd);
572
573 vp = nd.ni_vp;
574
575 // find exportfs that matches f_mntonname
576 lck_rw_lock_shared(&nfsrv_export_rwlock);
577 ptr = vnode_mount(vp)->mnt_vfsstat.f_mntonname;
578 LIST_FOREACH(nxfs, &nfsrv_exports, nxfs_next) {
579 if (!strncmp(nxfs->nxfs_path, ptr, MAXPATHLEN)) {
580 break;
581 }
582 }
583 if (!nxfs || strncmp(nxfs->nxfs_path, path, strlen(nxfs->nxfs_path))) {
584 error = EINVAL;
585 goto out;
586 }
587 // find export that best matches remainder of path
588 ptr = path + strlen(nxfs->nxfs_path);
589 while (*ptr && (*ptr == '/')) {
590 ptr++;
591 }
592 LIST_FOREACH(nx, &nxfs->nxfs_exports, nx_next) {
593 int len = strlen(nx->nx_path);
594 if (len == 0) { // we've hit the export entry for the root directory
595 break;
596 }
597 if (!strncmp(nx->nx_path, ptr, len)) {
598 break;
599 }
600 }
601 if (!nx) {
602 error = EINVAL;
603 goto out;
604 }
605
606 bzero(&nfh, sizeof(nfh));
607 nfh.nfh_xh.nxh_version = htonl(NFS_FH_VERSION);
608 nfh.nfh_xh.nxh_fsid = htonl(nxfs->nxfs_id);
609 nfh.nfh_xh.nxh_expid = htonl(nx->nx_id);
610 nfh.nfh_xh.nxh_flags = 0;
611 nfh.nfh_xh.nxh_reserved = 0;
612 nfh.nfh_len = fidlen;
613 error = VFS_VPTOFH(vp, (int*)&nfh.nfh_len, &nfh.nfh_fid[0], NULL);
614 if (nfh.nfh_len > (uint32_t)fidlen) {
615 error = EOVERFLOW;
616 }
617 nfh.nfh_xh.nxh_fidlen = nfh.nfh_len;
618 nfh.nfh_len += sizeof(nfh.nfh_xh);
619 nfh.nfh_fhp = (u_char*)&nfh.nfh_xh;
620
621 out:
622 lck_rw_done(&nfsrv_export_rwlock);
623 vnode_put(vp);
624 if (error) {
625 return error;
626 }
627 /*
628 * At first blush, this may appear to leak a kernel stack
629 * address, but the copyout() never reaches &nfh.nfh_fhp
630 * (sizeof(fhandle_t) < sizeof(nfh)).
631 */
632 error = copyout((caddr_t)&nfh, uap->fhp, sizeof(fhandle_t));
633 return error;
634 }
635
636 extern const struct fileops vnops;
637
638 /*
639 * syscall for the rpc.lockd to use to translate a NFS file handle into
640 * an open descriptor.
641 *
642 * warning: do not remove the suser() call or this becomes one giant
643 * security hole.
644 */
645 int
646 fhopen( proc_t p,
647 struct fhopen_args *uap,
648 int32_t *retval)
649 {
650 vnode_t vp;
651 struct nfs_filehandle nfh;
652 struct nfs_export *nx;
653 struct nfs_export_options *nxo;
654 struct flock lf;
655 struct fileproc *fp, *nfp;
656 int fmode, error, type;
657 int indx;
658 vfs_context_t ctx = vfs_context_current();
659 kauth_action_t action;
660
661 /*
662 * Must be super user
663 */
664 error = suser(vfs_context_ucred(ctx), 0);
665 if (error) {
666 return error;
667 }
668
669 if (!nfsrv_is_initialized()) {
670 return EINVAL;
671 }
672
673 fmode = FFLAGS(uap->flags);
674 /* why not allow a non-read/write open for our lockd? */
675 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT)) {
676 return EINVAL;
677 }
678
679 error = copyin(uap->u_fhp, &nfh.nfh_len, sizeof(nfh.nfh_len));
680 if (error) {
681 return error;
682 }
683 if ((nfh.nfh_len < (int)sizeof(struct nfs_exphandle)) ||
684 (nfh.nfh_len > (int)NFSV3_MAX_FH_SIZE)) {
685 return EINVAL;
686 }
687 error = copyin(uap->u_fhp, &nfh, sizeof(nfh.nfh_len) + nfh.nfh_len);
688 if (error) {
689 return error;
690 }
691 nfh.nfh_fhp = (u_char*)&nfh.nfh_xh;
692
693 lck_rw_lock_shared(&nfsrv_export_rwlock);
694 /* now give me my vnode, it gets returned to me with a reference */
695 error = nfsrv_fhtovp(&nfh, NULL, &vp, &nx, &nxo);
696 lck_rw_done(&nfsrv_export_rwlock);
697 if (error) {
698 if (error == NFSERR_TRYLATER) {
699 error = EAGAIN; // XXX EBUSY? Or just leave as TRYLATER?
700 }
701 return error;
702 }
703
704 /*
705 * From now on we have to make sure not
706 * to forget about the vnode.
707 * Any error that causes an abort must vnode_put(vp).
708 * Just set error = err and 'goto bad;'.
709 */
710
711 /*
712 * from vn_open
713 */
714 if (vnode_vtype(vp) == VSOCK) {
715 error = EOPNOTSUPP;
716 goto bad;
717 }
718
719 /* disallow write operations on directories */
720 if (vnode_isdir(vp) && (fmode & (FWRITE | O_TRUNC))) {
721 error = EISDIR;
722 goto bad;
723 }
724
725 #if CONFIG_MACF
726 if ((error = mac_vnode_check_open(ctx, vp, fmode))) {
727 goto bad;
728 }
729 #endif
730
731 /* compute action to be authorized */
732 action = 0;
733 if (fmode & FREAD) {
734 action |= KAUTH_VNODE_READ_DATA;
735 }
736 if (fmode & (FWRITE | O_TRUNC)) {
737 action |= KAUTH_VNODE_WRITE_DATA;
738 }
739 if ((error = vnode_authorize(vp, NULL, action, ctx)) != 0) {
740 goto bad;
741 }
742
743 if ((error = VNOP_OPEN(vp, fmode, ctx))) {
744 goto bad;
745 }
746 if ((error = vnode_ref_ext(vp, fmode, 0))) {
747 goto bad;
748 }
749
750 /*
751 * end of vn_open code
752 */
753
754 // starting here... error paths should call vn_close/vnode_put
755 if ((error = falloc(p, &nfp, &indx, ctx)) != 0) {
756 vn_close(vp, fmode & FMASK, ctx);
757 goto bad;
758 }
759 fp = nfp;
760
761 fp->f_fglob->fg_flag = fmode & FMASK;
762 fp->f_fglob->fg_ops = &vnops;
763 fp->f_fglob->fg_data = (caddr_t)vp;
764
765 // XXX do we really need to support this with fhopen()?
766 if (fmode & (O_EXLOCK | O_SHLOCK)) {
767 lf.l_whence = SEEK_SET;
768 lf.l_start = 0;
769 lf.l_len = 0;
770 if (fmode & O_EXLOCK) {
771 lf.l_type = F_WRLCK;
772 } else {
773 lf.l_type = F_RDLCK;
774 }
775 type = F_FLOCK;
776 if ((fmode & FNONBLOCK) == 0) {
777 type |= F_WAIT;
778 }
779 if ((error = VNOP_ADVLOCK(vp, (caddr_t)fp->f_fglob, F_SETLK, &lf, type, ctx, NULL))) {
780 struct vfs_context context = *vfs_context_current();
781 /* Modify local copy (to not damage thread copy) */
782 context.vc_ucred = fp->f_fglob->fg_cred;
783
784 vn_close(vp, fp->f_fglob->fg_flag, &context);
785 fp_free(p, indx, fp);
786 return error;
787 }
788 fp->f_fglob->fg_flag |= FHASLOCK;
789 }
790
791 vnode_put(vp);
792
793 proc_fdlock(p);
794 procfdtbl_releasefd(p, indx, NULL);
795 fp_drop(p, indx, fp, 1);
796 proc_fdunlock(p);
797
798 *retval = indx;
799 return 0;
800
801 bad:
802 vnode_put(vp);
803 return error;
804 }
805
806 /*
807 * NFS server pseudo system call
808 */
809 int
810 nfssvc(proc_t p, struct nfssvc_args *uap, __unused int *retval)
811 {
812 mbuf_t nam;
813 struct user_nfsd_args user_nfsdarg;
814 socket_t so;
815 int error;
816
817 AUDIT_ARG(cmd, uap->flag);
818
819 /*
820 * Must be super user for most operations (export ops checked later).
821 */
822 if ((uap->flag != NFSSVC_EXPORT) && ((error = proc_suser(p)))) {
823 return error;
824 }
825 #if CONFIG_MACF
826 error = mac_system_check_nfsd(kauth_cred_get());
827 if (error) {
828 return error;
829 }
830 #endif
831
832 /* make sure NFS server data structures have been initialized */
833 nfsrv_init();
834
835 if (uap->flag & NFSSVC_ADDSOCK) {
836 if (IS_64BIT_PROCESS(p)) {
837 error = copyin(uap->argp, (caddr_t)&user_nfsdarg, sizeof(user_nfsdarg));
838 } else {
839 struct nfsd_args tmp_args;
840 error = copyin(uap->argp, (caddr_t)&tmp_args, sizeof(tmp_args));
841 if (error == 0) {
842 user_nfsdarg.sock = tmp_args.sock;
843 user_nfsdarg.name = CAST_USER_ADDR_T(tmp_args.name);
844 user_nfsdarg.namelen = tmp_args.namelen;
845 }
846 }
847 if (error) {
848 return error;
849 }
850 /* get the socket */
851 error = file_socket(user_nfsdarg.sock, &so);
852 if (error) {
853 return error;
854 }
855 /* Get the client address for connected sockets. */
856 if (user_nfsdarg.name == USER_ADDR_NULL || user_nfsdarg.namelen == 0) {
857 nam = NULL;
858 } else {
859 error = sockargs(&nam, user_nfsdarg.name, user_nfsdarg.namelen, MBUF_TYPE_SONAME);
860 if (error) {
861 /* drop the iocount file_socket() grabbed on the file descriptor */
862 file_drop(user_nfsdarg.sock);
863 return error;
864 }
865 }
866 /*
867 * nfssvc_addsock() will grab a retain count on the socket
868 * to keep the socket from being closed when nfsd closes its
869 * file descriptor for it.
870 */
871 error = nfssvc_addsock(so, nam);
872 /* drop the iocount file_socket() grabbed on the file descriptor */
873 file_drop(user_nfsdarg.sock);
874 } else if (uap->flag & NFSSVC_NFSD) {
875 error = nfssvc_nfsd();
876 } else if (uap->flag & NFSSVC_EXPORT) {
877 error = nfssvc_export(uap->argp);
878 } else {
879 error = EINVAL;
880 }
881 if (error == EINTR || error == ERESTART) {
882 error = 0;
883 }
884 return error;
885 }
886
887 /*
888 * Adds a socket to the list for servicing by nfsds.
889 */
890 int
891 nfssvc_addsock(socket_t so, mbuf_t mynam)
892 {
893 struct nfsrv_sock *slp;
894 int error = 0, sodomain, sotype, soprotocol, on = 1;
895 int first;
896 struct timeval timeo;
897
898 /* make sure mbuf constants are set up */
899 if (!nfs_mbuf_mhlen) {
900 nfs_mbuf_init();
901 }
902
903 sock_gettype(so, &sodomain, &sotype, &soprotocol);
904
905 /* There should be only one UDP socket for each of IPv4 and IPv6 */
906 if ((sodomain == AF_INET) && (soprotocol == IPPROTO_UDP) && nfsrv_udpsock) {
907 mbuf_freem(mynam);
908 return EEXIST;
909 }
910 if ((sodomain == AF_INET6) && (soprotocol == IPPROTO_UDP) && nfsrv_udp6sock) {
911 mbuf_freem(mynam);
912 return EEXIST;
913 }
914
915 /* Set protocol options and reserve some space (for UDP). */
916 if (sotype == SOCK_STREAM) {
917 error = nfsrv_check_exports_allow_address(mynam);
918 if (error) {
919 log(LOG_INFO, "nfsvc_addsock:: nfsrv_check_exports_allow_address(myname) returned %d\n", error);
920 mbuf_freem(mynam);
921 return error;
922 }
923 sock_setsockopt(so, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on));
924 }
925 if ((sodomain == AF_INET) && (soprotocol == IPPROTO_TCP)) {
926 sock_setsockopt(so, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
927 }
928 if (sotype == SOCK_DGRAM || sodomain == AF_LOCAL) { /* set socket buffer sizes for UDP */
929 int reserve = (sotype == SOCK_DGRAM) ? NFS_UDPSOCKBUF : (2 * 1024 * 1024);
930 error |= sock_setsockopt(so, SOL_SOCKET, SO_SNDBUF, &reserve, sizeof(reserve));
931 error |= sock_setsockopt(so, SOL_SOCKET, SO_RCVBUF, &reserve, sizeof(reserve));
932 if (error) {
933 log(LOG_INFO, "nfssvc_addsock: UDP socket buffer setting error(s) %d\n", error);
934 error = 0;
935 }
936 }
937 sock_nointerrupt(so, 0);
938
939 /*
940 * Set socket send/receive timeouts.
941 * Receive timeout shouldn't matter, but setting the send timeout
942 * will make sure that an unresponsive client can't hang the server.
943 */
944 timeo.tv_usec = 0;
945 timeo.tv_sec = 1;
946 error |= sock_setsockopt(so, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo));
947 timeo.tv_sec = 30;
948 error |= sock_setsockopt(so, SOL_SOCKET, SO_SNDTIMEO, &timeo, sizeof(timeo));
949 if (error) {
950 log(LOG_INFO, "nfssvc_addsock: socket timeout setting error(s) %d\n", error);
951 error = 0;
952 }
953
954 MALLOC(slp, struct nfsrv_sock *, sizeof(struct nfsrv_sock), M_NFSSVC, M_WAITOK);
955 if (!slp) {
956 mbuf_freem(mynam);
957 return ENOMEM;
958 }
959 bzero((caddr_t)slp, sizeof(struct nfsrv_sock));
960 lck_rw_init(&slp->ns_rwlock, nfsrv_slp_rwlock_group, LCK_ATTR_NULL);
961 lck_mtx_init(&slp->ns_wgmutex, nfsrv_slp_mutex_group, LCK_ATTR_NULL);
962
963 lck_mtx_lock(nfsd_mutex);
964
965 if (soprotocol == IPPROTO_UDP) {
966 if (sodomain == AF_INET) {
967 /* There should be only one UDP/IPv4 socket */
968 if (nfsrv_udpsock) {
969 lck_mtx_unlock(nfsd_mutex);
970 nfsrv_slpfree(slp);
971 mbuf_freem(mynam);
972 return EEXIST;
973 }
974 nfsrv_udpsock = slp;
975 }
976 if (sodomain == AF_INET6) {
977 /* There should be only one UDP/IPv6 socket */
978 if (nfsrv_udp6sock) {
979 lck_mtx_unlock(nfsd_mutex);
980 nfsrv_slpfree(slp);
981 mbuf_freem(mynam);
982 return EEXIST;
983 }
984 nfsrv_udp6sock = slp;
985 }
986 }
987
988 /* add the socket to the list */
989 first = TAILQ_EMPTY(&nfsrv_socklist);
990 TAILQ_INSERT_TAIL(&nfsrv_socklist, slp, ns_chain);
991 if (sotype == SOCK_STREAM) {
992 nfsrv_sock_tcp_cnt++;
993 if (nfsrv_sock_idle_timeout < 0) {
994 nfsrv_sock_idle_timeout = 0;
995 }
996 if (nfsrv_sock_idle_timeout && (nfsrv_sock_idle_timeout < NFSD_MIN_IDLE_TIMEOUT)) {
997 nfsrv_sock_idle_timeout = NFSD_MIN_IDLE_TIMEOUT;
998 }
999 /*
1000 * Possibly start or stop the idle timer. We only start the idle timer when
1001 * we have more than 2 * nfsd_thread_max connections. If the idle timer is
1002 * on then we may need to turn it off based on the nvsrv_sock_idle_timeout or
1003 * the number of connections.
1004 */
1005 if ((nfsrv_sock_tcp_cnt > 2 * nfsd_thread_max) || nfsrv_idlesock_timer_on) {
1006 if (nfsrv_sock_idle_timeout == 0 || nfsrv_sock_tcp_cnt <= 2 * nfsd_thread_max) {
1007 if (nfsrv_idlesock_timer_on) {
1008 thread_call_cancel(nfsrv_idlesock_timer_call);
1009 nfsrv_idlesock_timer_on = 0;
1010 }
1011 } else {
1012 struct nfsrv_sock *old_slp;
1013 struct timeval now;
1014 time_t time_to_wait = nfsrv_sock_idle_timeout;
1015 /*
1016 * Get the oldest tcp socket and calculate the
1017 * earliest time for the next idle timer to fire
1018 * based on the possibly updated nfsrv_sock_idle_timeout
1019 */
1020 TAILQ_FOREACH(old_slp, &nfsrv_socklist, ns_chain) {
1021 if (old_slp->ns_sotype == SOCK_STREAM) {
1022 microuptime(&now);
1023 time_to_wait -= now.tv_sec - old_slp->ns_timestamp;
1024 if (time_to_wait < 1) {
1025 time_to_wait = 1;
1026 }
1027 break;
1028 }
1029 }
1030 /*
1031 * If we have a timer scheduled, but if its going to fire too late,
1032 * turn it off.
1033 */
1034 if (nfsrv_idlesock_timer_on > now.tv_sec + time_to_wait) {
1035 thread_call_cancel(nfsrv_idlesock_timer_call);
1036 nfsrv_idlesock_timer_on = 0;
1037 }
1038 /* Schedule the idle thread if it isn't already */
1039 if (!nfsrv_idlesock_timer_on) {
1040 nfs_interval_timer_start(nfsrv_idlesock_timer_call, time_to_wait * 1000);
1041 nfsrv_idlesock_timer_on = now.tv_sec + time_to_wait;
1042 }
1043 }
1044 }
1045 }
1046
1047 sock_retain(so); /* grab a retain count on the socket */
1048 slp->ns_so = so;
1049 slp->ns_sotype = sotype;
1050 slp->ns_nam = mynam;
1051
1052 /* set up the socket up-call */
1053 nfsrv_uc_addsock(slp, first);
1054
1055 /* mark that the socket is not in the nfsrv_sockwg list */
1056 slp->ns_wgq.tqe_next = SLPNOLIST;
1057
1058 slp->ns_flag = SLP_VALID | SLP_NEEDQ;
1059
1060 nfsrv_wakenfsd(slp);
1061 lck_mtx_unlock(nfsd_mutex);
1062
1063 return 0;
1064 }
1065
1066 /*
1067 * nfssvc_nfsd()
1068 *
1069 * nfsd theory of operation:
1070 *
1071 * The first nfsd thread stays in user mode accepting new TCP connections
1072 * which are then added via the "addsock" call. The rest of the nfsd threads
1073 * simply call into the kernel and remain there in a loop handling NFS
1074 * requests until killed by a signal.
1075 *
1076 * There's a list of nfsd threads (nfsd_head).
1077 * There's an nfsd queue that contains only those nfsds that are
1078 * waiting for work to do (nfsd_queue).
1079 *
1080 * There's a list of all NFS sockets (nfsrv_socklist) and two queues for
1081 * managing the work on the sockets:
1082 * nfsrv_sockwait - sockets w/new data waiting to be worked on
1083 * nfsrv_sockwork - sockets being worked on which may have more work to do
1084 * nfsrv_sockwg -- sockets which have pending write gather data
1085 * When a socket receives data, if it is not currently queued, it
1086 * will be placed at the end of the "wait" queue.
1087 * Whenever a socket needs servicing we make sure it is queued and
1088 * wake up a waiting nfsd (if there is one).
1089 *
1090 * nfsds will service at most 8 requests from the same socket before
1091 * defecting to work on another socket.
1092 * nfsds will defect immediately if there are any sockets in the "wait" queue
1093 * nfsds looking for a socket to work on check the "wait" queue first and
1094 * then check the "work" queue.
1095 * When an nfsd starts working on a socket, it removes it from the head of
1096 * the queue it's currently on and moves it to the end of the "work" queue.
1097 * When nfsds are checking the queues for work, any sockets found not to
1098 * have any work are simply dropped from the queue.
1099 *
1100 */
1101 int
1102 nfssvc_nfsd(void)
1103 {
1104 mbuf_t m, mrep;
1105 struct nfsrv_sock *slp;
1106 struct nfsd *nfsd;
1107 struct nfsrv_descript *nd = NULL;
1108 int error = 0, cacherep, writes_todo;
1109 int siz, procrastinate, opcnt = 0;
1110 u_quad_t cur_usec;
1111 struct timeval now;
1112 struct vfs_context context;
1113 struct timespec to;
1114
1115 #ifndef nolint
1116 cacherep = RC_DOIT;
1117 writes_todo = 0;
1118 #endif
1119
1120 MALLOC(nfsd, struct nfsd *, sizeof(struct nfsd), M_NFSD, M_WAITOK);
1121 if (!nfsd) {
1122 return ENOMEM;
1123 }
1124 bzero(nfsd, sizeof(struct nfsd));
1125 lck_mtx_lock(nfsd_mutex);
1126 if (nfsd_thread_count++ == 0) {
1127 nfsrv_initcache(); /* Init the server request cache */
1128 }
1129 TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain);
1130 lck_mtx_unlock(nfsd_mutex);
1131
1132 context.vc_thread = current_thread();
1133
1134 /* Set time out so that nfsd threads can wake up a see if they are still needed. */
1135 to.tv_sec = 5;
1136 to.tv_nsec = 0;
1137
1138 /*
1139 * Loop getting rpc requests until SIGKILL.
1140 */
1141 for (;;) {
1142 if (nfsd_thread_max <= 0) {
1143 /* NFS server shutting down, get out ASAP */
1144 error = EINTR;
1145 slp = nfsd->nfsd_slp;
1146 } else if (nfsd->nfsd_flag & NFSD_REQINPROG) {
1147 /* already have some work to do */
1148 error = 0;
1149 slp = nfsd->nfsd_slp;
1150 } else {
1151 /* need to find work to do */
1152 error = 0;
1153 lck_mtx_lock(nfsd_mutex);
1154 while (!nfsd->nfsd_slp && TAILQ_EMPTY(&nfsrv_sockwait) && TAILQ_EMPTY(&nfsrv_sockwork)) {
1155 if (nfsd_thread_count > nfsd_thread_max) {
1156 /*
1157 * If we have no socket and there are more
1158 * nfsd threads than configured, let's exit.
1159 */
1160 error = 0;
1161 goto done;
1162 }
1163 nfsd->nfsd_flag |= NFSD_WAITING;
1164 TAILQ_INSERT_HEAD(&nfsd_queue, nfsd, nfsd_queue);
1165 error = msleep(nfsd, nfsd_mutex, PSOCK | PCATCH, "nfsd", &to);
1166 if (error) {
1167 if (nfsd->nfsd_flag & NFSD_WAITING) {
1168 TAILQ_REMOVE(&nfsd_queue, nfsd, nfsd_queue);
1169 nfsd->nfsd_flag &= ~NFSD_WAITING;
1170 }
1171 if (error == EWOULDBLOCK) {
1172 continue;
1173 }
1174 goto done;
1175 }
1176 }
1177 slp = nfsd->nfsd_slp;
1178 if (!slp && !TAILQ_EMPTY(&nfsrv_sockwait)) {
1179 /* look for a socket to work on in the wait queue */
1180 while ((slp = TAILQ_FIRST(&nfsrv_sockwait))) {
1181 lck_rw_lock_exclusive(&slp->ns_rwlock);
1182 /* remove from the head of the queue */
1183 TAILQ_REMOVE(&nfsrv_sockwait, slp, ns_svcq);
1184 slp->ns_flag &= ~SLP_WAITQ;
1185 if ((slp->ns_flag & SLP_VALID) && (slp->ns_flag & SLP_WORKTODO)) {
1186 break;
1187 }
1188 /* nothing to do, so skip this socket */
1189 lck_rw_done(&slp->ns_rwlock);
1190 }
1191 }
1192 if (!slp && !TAILQ_EMPTY(&nfsrv_sockwork)) {
1193 /* look for a socket to work on in the work queue */
1194 while ((slp = TAILQ_FIRST(&nfsrv_sockwork))) {
1195 lck_rw_lock_exclusive(&slp->ns_rwlock);
1196 /* remove from the head of the queue */
1197 TAILQ_REMOVE(&nfsrv_sockwork, slp, ns_svcq);
1198 slp->ns_flag &= ~SLP_WORKQ;
1199 if ((slp->ns_flag & SLP_VALID) && (slp->ns_flag & SLP_WORKTODO)) {
1200 break;
1201 }
1202 /* nothing to do, so skip this socket */
1203 lck_rw_done(&slp->ns_rwlock);
1204 }
1205 }
1206 if (!nfsd->nfsd_slp && slp) {
1207 /* we found a socket to work on, grab a reference */
1208 slp->ns_sref++;
1209 microuptime(&now);
1210 slp->ns_timestamp = now.tv_sec;
1211 /* We keep the socket list in least recently used order for reaping idle sockets */
1212 TAILQ_REMOVE(&nfsrv_socklist, slp, ns_chain);
1213 TAILQ_INSERT_TAIL(&nfsrv_socklist, slp, ns_chain);
1214 nfsd->nfsd_slp = slp;
1215 opcnt = 0;
1216 /* and put it at the back of the work queue */
1217 TAILQ_INSERT_TAIL(&nfsrv_sockwork, slp, ns_svcq);
1218 slp->ns_flag |= SLP_WORKQ;
1219 lck_rw_done(&slp->ns_rwlock);
1220 }
1221 lck_mtx_unlock(nfsd_mutex);
1222 if (!slp) {
1223 continue;
1224 }
1225 lck_rw_lock_exclusive(&slp->ns_rwlock);
1226 if (slp->ns_flag & SLP_VALID) {
1227 if ((slp->ns_flag & (SLP_NEEDQ | SLP_DISCONN)) == SLP_NEEDQ) {
1228 slp->ns_flag &= ~SLP_NEEDQ;
1229 nfsrv_rcv_locked(slp->ns_so, slp, MBUF_WAITOK);
1230 }
1231 if (slp->ns_flag & SLP_DISCONN) {
1232 nfsrv_zapsock(slp);
1233 }
1234 error = nfsrv_dorec(slp, nfsd, &nd);
1235 if (error == EINVAL) { // RPCSEC_GSS drop
1236 if (slp->ns_sotype == SOCK_STREAM) {
1237 nfsrv_zapsock(slp); // drop connection
1238 }
1239 }
1240 writes_todo = 0;
1241 if (error && (slp->ns_wgtime || (slp->ns_flag & SLP_DOWRITES))) {
1242 microuptime(&now);
1243 cur_usec = (u_quad_t)now.tv_sec * 1000000 +
1244 (u_quad_t)now.tv_usec;
1245 if (slp->ns_wgtime <= cur_usec) {
1246 error = 0;
1247 cacherep = RC_DOIT;
1248 writes_todo = 1;
1249 }
1250 slp->ns_flag &= ~SLP_DOWRITES;
1251 }
1252 nfsd->nfsd_flag |= NFSD_REQINPROG;
1253 }
1254 lck_rw_done(&slp->ns_rwlock);
1255 }
1256 if (error || (slp && !(slp->ns_flag & SLP_VALID))) {
1257 if (nd) {
1258 nfsm_chain_cleanup(&nd->nd_nmreq);
1259 if (nd->nd_nam2) {
1260 mbuf_freem(nd->nd_nam2);
1261 }
1262 if (IS_VALID_CRED(nd->nd_cr)) {
1263 kauth_cred_unref(&nd->nd_cr);
1264 }
1265 if (nd->nd_gss_context) {
1266 nfs_gss_svc_ctx_deref(nd->nd_gss_context);
1267 }
1268 FREE_ZONE(nd, sizeof(*nd), M_NFSRVDESC);
1269 nd = NULL;
1270 }
1271 nfsd->nfsd_slp = NULL;
1272 nfsd->nfsd_flag &= ~NFSD_REQINPROG;
1273 if (slp) {
1274 nfsrv_slpderef(slp);
1275 }
1276 if (nfsd_thread_max <= 0) {
1277 break;
1278 }
1279 continue;
1280 }
1281 if (nd) {
1282 microuptime(&nd->nd_starttime);
1283 if (nd->nd_nam2) {
1284 nd->nd_nam = nd->nd_nam2;
1285 } else {
1286 nd->nd_nam = slp->ns_nam;
1287 }
1288
1289 cacherep = nfsrv_getcache(nd, slp, &mrep);
1290
1291 if (nfsrv_require_resv_port) {
1292 /* Check if source port is a reserved port */
1293 in_port_t port = 0;
1294 struct sockaddr *saddr = mbuf_data(nd->nd_nam);
1295
1296 if (saddr->sa_family == AF_INET) {
1297 port = ntohs(((struct sockaddr_in*)saddr)->sin_port);
1298 } else if (saddr->sa_family == AF_INET6) {
1299 port = ntohs(((struct sockaddr_in6*)saddr)->sin6_port);
1300 }
1301 if ((port >= IPPORT_RESERVED) && (nd->nd_procnum != NFSPROC_NULL)) {
1302 nd->nd_procnum = NFSPROC_NOOP;
1303 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
1304 cacherep = RC_DOIT;
1305 }
1306 }
1307 }
1308
1309 /*
1310 * Loop to get all the write RPC replies that have been
1311 * gathered together.
1312 */
1313 do {
1314 switch (cacherep) {
1315 case RC_DOIT:
1316 if (nd && (nd->nd_vers == NFS_VER3)) {
1317 procrastinate = nfsrv_wg_delay_v3;
1318 } else {
1319 procrastinate = nfsrv_wg_delay;
1320 }
1321 lck_rw_lock_shared(&nfsrv_export_rwlock);
1322 context.vc_ucred = NULL;
1323 if (writes_todo || ((nd->nd_procnum == NFSPROC_WRITE) && (procrastinate > 0))) {
1324 error = nfsrv_writegather(&nd, slp, &context, &mrep);
1325 } else {
1326 error = (*(nfsrv_procs[nd->nd_procnum]))(nd, slp, &context, &mrep);
1327 }
1328 lck_rw_done(&nfsrv_export_rwlock);
1329 if (mrep == NULL) {
1330 /*
1331 * If this is a stream socket and we are not going
1332 * to send a reply we better close the connection
1333 * so the client doesn't hang.
1334 */
1335 if (error && slp->ns_sotype == SOCK_STREAM) {
1336 lck_rw_lock_exclusive(&slp->ns_rwlock);
1337 nfsrv_zapsock(slp);
1338 lck_rw_done(&slp->ns_rwlock);
1339 printf("NFS server: NULL reply from proc = %d error = %d\n",
1340 nd->nd_procnum, error);
1341 }
1342 break;
1343 }
1344 if (error) {
1345 OSAddAtomic64(1, &nfsstats.srv_errs);
1346 nfsrv_updatecache(nd, FALSE, mrep);
1347 if (nd->nd_nam2) {
1348 mbuf_freem(nd->nd_nam2);
1349 nd->nd_nam2 = NULL;
1350 }
1351 break;
1352 }
1353 OSAddAtomic64(1, &nfsstats.srvrpccnt[nd->nd_procnum]);
1354 nfsrv_updatecache(nd, TRUE, mrep);
1355 /* FALLTHRU */
1356
1357 case RC_REPLY:
1358 if (nd->nd_gss_mb != NULL) { // It's RPCSEC_GSS
1359 /*
1360 * Need to checksum or encrypt the reply
1361 */
1362 error = nfs_gss_svc_protect_reply(nd, mrep);
1363 if (error) {
1364 mbuf_freem(mrep);
1365 break;
1366 }
1367 }
1368
1369 /*
1370 * Get the total size of the reply
1371 */
1372 m = mrep;
1373 siz = 0;
1374 while (m) {
1375 siz += mbuf_len(m);
1376 m = mbuf_next(m);
1377 }
1378 if (siz <= 0 || siz > NFS_MAXPACKET) {
1379 printf("mbuf siz=%d\n", siz);
1380 panic("Bad nfs svc reply");
1381 }
1382 m = mrep;
1383 mbuf_pkthdr_setlen(m, siz);
1384 error = mbuf_pkthdr_setrcvif(m, NULL);
1385 if (error) {
1386 panic("nfsd setrcvif failed: %d", error);
1387 }
1388 /*
1389 * For stream protocols, prepend a Sun RPC
1390 * Record Mark.
1391 */
1392 if (slp->ns_sotype == SOCK_STREAM) {
1393 error = mbuf_prepend(&m, NFSX_UNSIGNED, MBUF_WAITOK);
1394 if (!error) {
1395 *(u_int32_t*)mbuf_data(m) = htonl(0x80000000 | siz);
1396 }
1397 }
1398 if (!error) {
1399 if (slp->ns_flag & SLP_VALID) {
1400 error = nfsrv_send(slp, nd->nd_nam2, m);
1401 } else {
1402 error = EPIPE;
1403 mbuf_freem(m);
1404 }
1405 } else {
1406 mbuf_freem(m);
1407 }
1408 mrep = NULL;
1409 if (nd->nd_nam2) {
1410 mbuf_freem(nd->nd_nam2);
1411 nd->nd_nam2 = NULL;
1412 }
1413 if (error == EPIPE) {
1414 lck_rw_lock_exclusive(&slp->ns_rwlock);
1415 nfsrv_zapsock(slp);
1416 lck_rw_done(&slp->ns_rwlock);
1417 }
1418 if (error == EINTR || error == ERESTART) {
1419 nfsm_chain_cleanup(&nd->nd_nmreq);
1420 if (IS_VALID_CRED(nd->nd_cr)) {
1421 kauth_cred_unref(&nd->nd_cr);
1422 }
1423 if (nd->nd_gss_context) {
1424 nfs_gss_svc_ctx_deref(nd->nd_gss_context);
1425 }
1426 FREE_ZONE(nd, sizeof(*nd), M_NFSRVDESC);
1427 nfsrv_slpderef(slp);
1428 lck_mtx_lock(nfsd_mutex);
1429 goto done;
1430 }
1431 break;
1432 case RC_DROPIT:
1433 mbuf_freem(nd->nd_nam2);
1434 nd->nd_nam2 = NULL;
1435 break;
1436 }
1437 ;
1438 opcnt++;
1439 if (nd) {
1440 nfsm_chain_cleanup(&nd->nd_nmreq);
1441 if (nd->nd_nam2) {
1442 mbuf_freem(nd->nd_nam2);
1443 }
1444 if (IS_VALID_CRED(nd->nd_cr)) {
1445 kauth_cred_unref(&nd->nd_cr);
1446 }
1447 if (nd->nd_gss_context) {
1448 nfs_gss_svc_ctx_deref(nd->nd_gss_context);
1449 }
1450 FREE_ZONE(nd, sizeof(*nd), M_NFSRVDESC);
1451 nd = NULL;
1452 }
1453
1454 /*
1455 * Check to see if there are outstanding writes that
1456 * need to be serviced.
1457 */
1458 writes_todo = 0;
1459 if (slp->ns_wgtime) {
1460 microuptime(&now);
1461 cur_usec = (u_quad_t)now.tv_sec * 1000000 +
1462 (u_quad_t)now.tv_usec;
1463 if (slp->ns_wgtime <= cur_usec) {
1464 cacherep = RC_DOIT;
1465 writes_todo = 1;
1466 }
1467 }
1468 } while (writes_todo);
1469
1470 nd = NULL;
1471 if (TAILQ_EMPTY(&nfsrv_sockwait) && (opcnt < 8)) {
1472 lck_rw_lock_exclusive(&slp->ns_rwlock);
1473 error = nfsrv_dorec(slp, nfsd, &nd);
1474 if (error == EINVAL) { // RPCSEC_GSS drop
1475 if (slp->ns_sotype == SOCK_STREAM) {
1476 nfsrv_zapsock(slp); // drop connection
1477 }
1478 }
1479 lck_rw_done(&slp->ns_rwlock);
1480 }
1481 if (!nd) {
1482 /* drop our reference on the socket */
1483 nfsd->nfsd_flag &= ~NFSD_REQINPROG;
1484 nfsd->nfsd_slp = NULL;
1485 nfsrv_slpderef(slp);
1486 }
1487 }
1488 lck_mtx_lock(nfsd_mutex);
1489 done:
1490 TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
1491 FREE(nfsd, M_NFSD);
1492 if (--nfsd_thread_count == 0) {
1493 nfsrv_cleanup();
1494 }
1495 lck_mtx_unlock(nfsd_mutex);
1496 return error;
1497 }
1498
1499 int
1500 nfssvc_export(user_addr_t argp)
1501 {
1502 int error = 0, is_64bit;
1503 struct user_nfs_export_args unxa;
1504 vfs_context_t ctx = vfs_context_current();
1505
1506 is_64bit = IS_64BIT_PROCESS(vfs_context_proc(ctx));
1507
1508 /* copy in pointers to path and export args */
1509 if (is_64bit) {
1510 error = copyin(argp, (caddr_t)&unxa, sizeof(unxa));
1511 } else {
1512 struct nfs_export_args tnxa;
1513 error = copyin(argp, (caddr_t)&tnxa, sizeof(tnxa));
1514 if (error == 0) {
1515 /* munge into LP64 version of nfs_export_args structure */
1516 unxa.nxa_fsid = tnxa.nxa_fsid;
1517 unxa.nxa_expid = tnxa.nxa_expid;
1518 unxa.nxa_fspath = CAST_USER_ADDR_T(tnxa.nxa_fspath);
1519 unxa.nxa_exppath = CAST_USER_ADDR_T(tnxa.nxa_exppath);
1520 unxa.nxa_flags = tnxa.nxa_flags;
1521 unxa.nxa_netcount = tnxa.nxa_netcount;
1522 unxa.nxa_nets = CAST_USER_ADDR_T(tnxa.nxa_nets);
1523 }
1524 }
1525 if (error) {
1526 return error;
1527 }
1528
1529 error = nfsrv_export(&unxa, ctx);
1530
1531 return error;
1532 }
1533
1534 /*
1535 * Shut down a socket associated with an nfsrv_sock structure.
1536 * Should be called with the send lock set, if required.
1537 * The trick here is to increment the sref at the start, so that the nfsds
1538 * will stop using it and clear ns_flag at the end so that it will not be
1539 * reassigned during cleanup.
1540 */
1541 void
1542 nfsrv_zapsock(struct nfsrv_sock *slp)
1543 {
1544 socket_t so;
1545
1546 if ((slp->ns_flag & SLP_VALID) == 0) {
1547 return;
1548 }
1549 slp->ns_flag &= ~SLP_ALLFLAGS;
1550
1551 so = slp->ns_so;
1552 if (so == NULL) {
1553 return;
1554 }
1555
1556 sock_setupcall(so, NULL, NULL);
1557 sock_shutdown(so, SHUT_RDWR);
1558
1559 /*
1560 * Remove from the up-call queue
1561 */
1562 nfsrv_uc_dequeue(slp);
1563 }
1564
1565 /*
1566 * cleanup and release a server socket structure.
1567 */
1568 void
1569 nfsrv_slpfree(struct nfsrv_sock *slp)
1570 {
1571 struct nfsrv_descript *nwp, *nnwp;
1572
1573 if (slp->ns_so) {
1574 sock_release(slp->ns_so);
1575 slp->ns_so = NULL;
1576 }
1577 if (slp->ns_nam) {
1578 mbuf_free(slp->ns_nam);
1579 }
1580 if (slp->ns_raw) {
1581 mbuf_freem(slp->ns_raw);
1582 }
1583 if (slp->ns_rec) {
1584 mbuf_freem(slp->ns_rec);
1585 }
1586 if (slp->ns_frag) {
1587 mbuf_freem(slp->ns_frag);
1588 }
1589 slp->ns_nam = slp->ns_raw = slp->ns_rec = slp->ns_frag = NULL;
1590 slp->ns_reccnt = 0;
1591
1592 for (nwp = slp->ns_tq.lh_first; nwp; nwp = nnwp) {
1593 nnwp = nwp->nd_tq.le_next;
1594 LIST_REMOVE(nwp, nd_tq);
1595 nfsm_chain_cleanup(&nwp->nd_nmreq);
1596 if (nwp->nd_mrep) {
1597 mbuf_freem(nwp->nd_mrep);
1598 }
1599 if (nwp->nd_nam2) {
1600 mbuf_freem(nwp->nd_nam2);
1601 }
1602 if (IS_VALID_CRED(nwp->nd_cr)) {
1603 kauth_cred_unref(&nwp->nd_cr);
1604 }
1605 if (nwp->nd_gss_context) {
1606 nfs_gss_svc_ctx_deref(nwp->nd_gss_context);
1607 }
1608 FREE_ZONE(nwp, sizeof(*nwp), M_NFSRVDESC);
1609 }
1610 LIST_INIT(&slp->ns_tq);
1611
1612 lck_rw_destroy(&slp->ns_rwlock, nfsrv_slp_rwlock_group);
1613 lck_mtx_destroy(&slp->ns_wgmutex, nfsrv_slp_mutex_group);
1614 FREE(slp, M_NFSSVC);
1615 }
1616
1617 /*
1618 * Derefence a server socket structure. If it has no more references and
1619 * is no longer valid, you can throw it away.
1620 */
1621 static void
1622 nfsrv_slpderef_locked(struct nfsrv_sock *slp)
1623 {
1624 lck_rw_lock_exclusive(&slp->ns_rwlock);
1625 slp->ns_sref--;
1626
1627 if (slp->ns_sref || (slp->ns_flag & SLP_VALID)) {
1628 if ((slp->ns_flag & SLP_QUEUED) && !(slp->ns_flag & SLP_WORKTODO)) {
1629 /* remove socket from queue since there's no work */
1630 if (slp->ns_flag & SLP_WAITQ) {
1631 TAILQ_REMOVE(&nfsrv_sockwait, slp, ns_svcq);
1632 } else {
1633 TAILQ_REMOVE(&nfsrv_sockwork, slp, ns_svcq);
1634 }
1635 slp->ns_flag &= ~SLP_QUEUED;
1636 }
1637 lck_rw_done(&slp->ns_rwlock);
1638 return;
1639 }
1640
1641 /* This socket is no longer valid, so we'll get rid of it */
1642
1643 if (slp->ns_flag & SLP_QUEUED) {
1644 if (slp->ns_flag & SLP_WAITQ) {
1645 TAILQ_REMOVE(&nfsrv_sockwait, slp, ns_svcq);
1646 } else {
1647 TAILQ_REMOVE(&nfsrv_sockwork, slp, ns_svcq);
1648 }
1649 slp->ns_flag &= ~SLP_QUEUED;
1650 }
1651 lck_rw_done(&slp->ns_rwlock);
1652
1653 TAILQ_REMOVE(&nfsrv_socklist, slp, ns_chain);
1654 if (slp->ns_sotype == SOCK_STREAM) {
1655 nfsrv_sock_tcp_cnt--;
1656 }
1657
1658 /* now remove from the write gather socket list */
1659 if (slp->ns_wgq.tqe_next != SLPNOLIST) {
1660 TAILQ_REMOVE(&nfsrv_sockwg, slp, ns_wgq);
1661 slp->ns_wgq.tqe_next = SLPNOLIST;
1662 }
1663 nfsrv_slpfree(slp);
1664 }
1665
1666 void
1667 nfsrv_slpderef(struct nfsrv_sock *slp)
1668 {
1669 lck_mtx_lock(nfsd_mutex);
1670 nfsrv_slpderef_locked(slp);
1671 lck_mtx_unlock(nfsd_mutex);
1672 }
1673
1674 /*
1675 * Check periodically for idle sockest if needed and
1676 * zap them.
1677 */
1678 void
1679 nfsrv_idlesock_timer(__unused void *param0, __unused void *param1)
1680 {
1681 struct nfsrv_sock *slp, *tslp;
1682 struct timeval now;
1683 time_t time_to_wait = nfsrv_sock_idle_timeout;
1684
1685 microuptime(&now);
1686 lck_mtx_lock(nfsd_mutex);
1687
1688 /* Turn off the timer if we're suppose to and get out */
1689 if (nfsrv_sock_idle_timeout < NFSD_MIN_IDLE_TIMEOUT) {
1690 nfsrv_sock_idle_timeout = 0;
1691 }
1692 if ((nfsrv_sock_tcp_cnt <= 2 * nfsd_thread_max) || (nfsrv_sock_idle_timeout == 0)) {
1693 nfsrv_idlesock_timer_on = 0;
1694 lck_mtx_unlock(nfsd_mutex);
1695 return;
1696 }
1697
1698 TAILQ_FOREACH_SAFE(slp, &nfsrv_socklist, ns_chain, tslp) {
1699 lck_rw_lock_exclusive(&slp->ns_rwlock);
1700 /* Skip udp and referenced sockets */
1701 if (slp->ns_sotype == SOCK_DGRAM || slp->ns_sref) {
1702 lck_rw_done(&slp->ns_rwlock);
1703 continue;
1704 }
1705 /*
1706 * If this is the first non-referenced socket that hasn't idle out,
1707 * use its time stamp to calculate the earlist time in the future
1708 * to start the next invocation of the timer. Since the nfsrv_socklist
1709 * is sorted oldest access to newest. Once we find the first one,
1710 * we're done and break out of the loop.
1711 */
1712 if (((slp->ns_timestamp + nfsrv_sock_idle_timeout) > now.tv_sec) ||
1713 nfsrv_sock_tcp_cnt <= 2 * nfsd_thread_max) {
1714 time_to_wait -= now.tv_sec - slp->ns_timestamp;
1715 if (time_to_wait < 1) {
1716 time_to_wait = 1;
1717 }
1718 lck_rw_done(&slp->ns_rwlock);
1719 break;
1720 }
1721 /*
1722 * Bump the ref count. nfsrv_slpderef below will destroy
1723 * the socket, since nfsrv_zapsock has closed it.
1724 */
1725 slp->ns_sref++;
1726 nfsrv_zapsock(slp);
1727 lck_rw_done(&slp->ns_rwlock);
1728 nfsrv_slpderef_locked(slp);
1729 }
1730
1731 /* Start ourself back up */
1732 nfs_interval_timer_start(nfsrv_idlesock_timer_call, time_to_wait * 1000);
1733 /* Remember when the next timer will fire for nfssvc_addsock. */
1734 nfsrv_idlesock_timer_on = now.tv_sec + time_to_wait;
1735 lck_mtx_unlock(nfsd_mutex);
1736 }
1737
1738 /*
1739 * Clean up the data structures for the server.
1740 */
1741 void
1742 nfsrv_cleanup(void)
1743 {
1744 struct nfsrv_sock *slp, *nslp;
1745 struct timeval now;
1746 #if CONFIG_FSE
1747 struct nfsrv_fmod *fp, *nfp;
1748 int i;
1749 #endif
1750
1751 microuptime(&now);
1752 for (slp = TAILQ_FIRST(&nfsrv_socklist); slp != 0; slp = nslp) {
1753 nslp = TAILQ_NEXT(slp, ns_chain);
1754 lck_rw_lock_exclusive(&slp->ns_rwlock);
1755 slp->ns_sref++;
1756 if (slp->ns_flag & SLP_VALID) {
1757 nfsrv_zapsock(slp);
1758 }
1759 lck_rw_done(&slp->ns_rwlock);
1760 nfsrv_slpderef_locked(slp);
1761 }
1762 #
1763 #if CONFIG_FSE
1764 /*
1765 * Flush pending file write fsevents
1766 */
1767 lck_mtx_lock(nfsrv_fmod_mutex);
1768 for (i = 0; i < NFSRVFMODHASHSZ; i++) {
1769 for (fp = LIST_FIRST(&nfsrv_fmod_hashtbl[i]); fp; fp = nfp) {
1770 /*
1771 * Fire off the content modified fsevent for each
1772 * entry, remove it from the list, and free it.
1773 */
1774 if (nfsrv_fsevents_enabled) {
1775 fp->fm_context.vc_thread = current_thread();
1776 add_fsevent(FSE_CONTENT_MODIFIED, &fp->fm_context,
1777 FSE_ARG_VNODE, fp->fm_vp,
1778 FSE_ARG_DONE);
1779 }
1780 vnode_put(fp->fm_vp);
1781 kauth_cred_unref(&fp->fm_context.vc_ucred);
1782 nfp = LIST_NEXT(fp, fm_link);
1783 LIST_REMOVE(fp, fm_link);
1784 FREE(fp, M_TEMP);
1785 }
1786 }
1787 nfsrv_fmod_pending = 0;
1788 lck_mtx_unlock(nfsrv_fmod_mutex);
1789 #endif
1790
1791 nfsrv_uc_cleanup(); /* Stop nfs socket up-call threads */
1792
1793 nfs_gss_svc_cleanup(); /* Remove any RPCSEC_GSS contexts */
1794
1795 nfsrv_cleancache(); /* And clear out server cache */
1796
1797 nfsrv_udpsock = NULL;
1798 nfsrv_udp6sock = NULL;
1799 }
1800
1801 #endif /* NFS_NOSERVER */