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