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