2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)nfs_syscalls.c 8.5 (Berkeley) 3/30/95
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include "opt_inet6.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/sysproto.h>
43 #include <sys/kernel.h>
44 #include <sys/sysctl.h>
46 #include <sys/filedesc.h>
47 #include <sys/vnode.h>
48 #include <sys/malloc.h>
49 #include <sys/mount.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
57 #include <sys/domain.h>
58 #include <sys/protosw.h>
59 #include <sys/namei.h>
60 #include <sys/fcntl.h>
61 #include <sys/lockf.h>
63 #include <netinet/in.h>
64 #include <netinet/tcp.h>
67 #include <netinet6/in6_var.h>
69 #include <nfs/xdr_subs.h>
70 #include <nfs/rpcv2.h>
71 #include <nfs/nfsproto.h>
72 #include <nfsserver/nfs.h>
73 #include <nfsserver/nfsm_subs.h>
74 #include <nfsserver/nfsrvcache.h>
76 static MALLOC_DEFINE(M_NFSSVC, "nfss_srvsock", "Nfs server structure");
78 MALLOC_DEFINE(M_NFSRVDESC, "nfss_srvdesc", "NFS server socket descriptor");
79 MALLOC_DEFINE(M_NFSD, "nfss_daemon", "Nfs server daemon structure");
84 SYSCTL_DECL(_vfs_nfsrv);
87 int nfsrv_numnfsd = 0;
88 static int notstarted = 1;
90 static int nfs_privport = 0;
91 SYSCTL_INT(_vfs_nfsrv, NFS_NFSPRIVPORT, nfs_privport, CTLFLAG_RW,
92 &nfs_privport, 0, "");
93 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, gatherdelay, CTLFLAG_RW,
94 &nfsrvw_procrastinate, 0, "");
95 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, gatherdelay_v3, CTLFLAG_RW,
96 &nfsrvw_procrastinate_v3, 0, "");
98 static int nfssvc_addsock(struct file *, struct sockaddr *,
100 static void nfsrv_zapsock(struct nfssvc_sock *slp);
101 static int nfssvc_nfsd(struct thread *);
103 extern u_long sb_max_adj;
106 * NFS server system calls
110 * Nfs server psuedo system call for the nfsd's
111 * Based on the flag value it either:
112 * - adds a socket to the selection list
113 * - remains in the kernel as an nfsd
114 * - remains in the kernel as an nfsiod
115 * For INET6 we suppose that nfsd provides only IN6P_IPV6_V6ONLY sockets
116 * and that mountd provides
117 * - sockaddr with no IPv4-mapped addresses
118 * - mask for both INET and INET6 families if there is IPv4-mapped overlap
120 #ifndef _SYS_SYSPROTO_H_
127 nfssvc(struct thread *td, struct nfssvc_args *uap)
130 struct sockaddr *nam;
131 struct nfsd_args nfsdarg;
134 KASSERT(!mtx_owned(&Giant), ("nfssvc(): called with Giant"));
136 error = priv_check(td, PRIV_NFS_DAEMON);
140 while (nfssvc_sockhead_flag & SLP_INIT) {
141 nfssvc_sockhead_flag |= SLP_WANTINIT;
142 (void) msleep(&nfssvc_sockhead, &nfsd_mtx, PSOCK,
146 if (uap->flag & NFSSVC_ADDSOCK) {
147 error = copyin(uap->argp, (caddr_t)&nfsdarg, sizeof(nfsdarg));
150 if ((error = fget(td, nfsdarg.sock, &fp)) != 0)
152 if (fp->f_type != DTYPE_SOCKET) {
154 return (error); /* XXXRW: Should be EINVAL? */
157 * Get the client address for connected sockets.
159 if (nfsdarg.name == NULL || nfsdarg.namelen == 0)
162 error = getsockaddr(&nam, nfsdarg.name,
169 error = nfssvc_addsock(fp, nam, td);
171 } else if (uap->flag & NFSSVC_NFSD) {
172 error = nfssvc_nfsd(td);
176 if (error == EINTR || error == ERESTART)
182 * Adds a socket to the list for servicing by nfsds.
185 nfssvc_addsock(struct file *fp, struct sockaddr *mynam, struct thread *td)
188 struct nfssvc_sock *slp;
195 * XXXRW: If this code is ever enabled, there's a race when running
200 * Add it to the list, as required.
202 if (so->so_proto->pr_protocol == IPPROTO_UDP) {
204 if (tslp->ns_flag & SLP_VALID) {
206 FREE(mynam, M_SONAME);
212 error = soreserve(so, siz, siz);
215 FREE(mynam, M_SONAME);
220 * Set protocol specific options { for now TCP only } and
221 * reserve some space. For datagram sockets, this can get called
222 * repeatedly for the same socket, but that isn't harmful.
224 if (so->so_type == SOCK_STREAM) {
228 bzero(&sopt, sizeof sopt);
229 sopt.sopt_dir = SOPT_SET;
230 sopt.sopt_level = SOL_SOCKET;
231 sopt.sopt_name = SO_KEEPALIVE;
232 sopt.sopt_val = &val;
233 sopt.sopt_valsize = sizeof val;
237 if (so->so_proto->pr_protocol == IPPROTO_TCP) {
241 bzero(&sopt, sizeof sopt);
242 sopt.sopt_dir = SOPT_SET;
243 sopt.sopt_level = IPPROTO_TCP;
244 sopt.sopt_name = TCP_NODELAY;
245 sopt.sopt_val = &val;
246 sopt.sopt_valsize = sizeof val;
250 SOCKBUF_LOCK(&so->so_rcv);
251 so->so_rcv.sb_flags &= ~SB_NOINTR;
252 so->so_rcv.sb_timeo = 0;
253 SOCKBUF_UNLOCK(&so->so_rcv);
254 SOCKBUF_LOCK(&so->so_snd);
255 so->so_snd.sb_flags &= ~SB_NOINTR;
256 so->so_snd.sb_timeo = 0;
257 SOCKBUF_UNLOCK(&so->so_snd);
259 slp = (struct nfssvc_sock *)
260 malloc(sizeof (struct nfssvc_sock), M_NFSSVC,
262 STAILQ_INIT(&slp->ns_rec);
264 TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
271 * XXXRW: Socket locking here?
274 so->so_upcallarg = (caddr_t)slp;
275 so->so_upcall = nfsrv_rcv;
276 SOCKBUF_LOCK(&so->so_rcv);
277 so->so_rcv.sb_flags |= SB_UPCALL;
278 SOCKBUF_UNLOCK(&so->so_rcv);
279 slp->ns_flag = (SLP_VALID | SLP_NEEDQ);
287 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
288 * until it is killed by a signal.
291 nfssvc_nfsd(struct thread *td)
294 struct nfssvc_sock *slp;
296 struct nfsrv_descript *nd = NULL;
297 struct mbuf *m, *mreq;
298 int error = 0, cacherep, s, sotype, writes_todo;
306 nfsd = (struct nfsd *)
307 malloc(sizeof (struct nfsd), M_NFSD, M_WAITOK | M_ZERO);
312 TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain);
316 * Loop getting rpc requests until SIGKILL.
319 if ((nfsd->nfsd_flag & NFSD_REQINPROG) == 0) {
320 while (nfsd->nfsd_slp == NULL &&
321 (nfsd_head_flag & NFSD_CHECKSLP) == 0) {
322 nfsd->nfsd_flag |= NFSD_WAITING;
324 error = msleep(nfsd, &nfsd_mtx,
325 PSOCK | PCATCH, "-", 0);
330 if (nfsd->nfsd_slp == NULL &&
331 (nfsd_head_flag & NFSD_CHECKSLP) != 0) {
332 TAILQ_FOREACH(slp, &nfssvc_sockhead, ns_chain) {
333 if ((slp->ns_flag & (SLP_VALID | SLP_DOREC))
334 == (SLP_VALID | SLP_DOREC)) {
335 slp->ns_flag &= ~SLP_DOREC;
337 nfsd->nfsd_slp = slp;
342 nfsd_head_flag &= ~NFSD_CHECKSLP;
344 if ((slp = nfsd->nfsd_slp) == NULL)
346 if (slp->ns_flag & SLP_VALID) {
347 if (slp->ns_flag & SLP_DISCONN)
349 else if (slp->ns_flag & SLP_NEEDQ) {
350 slp->ns_flag &= ~SLP_NEEDQ;
351 (void) nfs_slplock(slp, 1);
353 nfsrv_rcv(slp->ns_so, (caddr_t)slp,
358 error = nfsrv_dorec(slp, nfsd, &nd);
359 cur_usec = nfs_curusec();
360 if (error && LIST_FIRST(&slp->ns_tq) &&
361 LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec) {
367 nfsd->nfsd_flag |= NFSD_REQINPROG;
371 slp = nfsd->nfsd_slp;
373 if (error || (slp->ns_flag & SLP_VALID) == 0) {
375 if (nd->nd_cr != NULL)
377 free((caddr_t)nd, M_NFSRVDESC);
380 nfsd->nfsd_slp = NULL;
381 nfsd->nfsd_flag &= ~NFSD_REQINPROG;
386 sotype = slp->ns_so->so_type;
388 getmicrotime(&nd->nd_starttime);
390 nd->nd_nam = nd->nd_nam2;
392 nd->nd_nam = slp->ns_nam;
395 * Check to see if authorization is needed.
397 cacherep = nfsrv_getcache(nd, &mreq);
400 /* Check if source port is privileged */
402 struct sockaddr *nam = nd->nd_nam;
403 struct sockaddr_in *sin;
405 sin = (struct sockaddr_in *)nam;
407 * INET/INET6 - same code:
408 * sin_port and sin6_port are at same offset
410 port = ntohs(sin->sin_port);
411 if (port >= IPPORT_RESERVED &&
412 nd->nd_procnum != NFSPROC_NULL) {
414 char b6[INET6_ADDRSTRLEN];
415 #if defined(KLD_MODULE)
416 /* Do not use ip6_sprintf: the nfs module should work without INET6. */
417 #define ip6_sprintf(buf, a) \
418 (sprintf((buf), "%x:%x:%x:%x:%x:%x:%x:%x", \
419 (a)->s6_addr16[0], (a)->s6_addr16[1], \
420 (a)->s6_addr16[2], (a)->s6_addr16[3], \
421 (a)->s6_addr16[4], (a)->s6_addr16[5], \
422 (a)->s6_addr16[6], (a)->s6_addr16[7]), \
426 nd->nd_procnum = NFSPROC_NOOP;
427 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
429 printf("NFS request from unprivileged port (%s:%d)\n",
431 sin->sin_family == AF_INET6 ?
432 ip6_sprintf(b6, &satosin6(sin)->sin6_addr) :
433 #if defined(KLD_MODULE)
437 inet_ntoa(sin->sin_addr), port);
444 * Loop to get all the write rpc relies that have been
450 if (nd && (nd->nd_flag & ND_NFSV3))
451 procrastinate = nfsrvw_procrastinate_v3;
453 procrastinate = nfsrvw_procrastinate;
455 if (writes_todo || (!(nd->nd_flag & ND_NFSV3) &&
456 nd->nd_procnum == NFSPROC_WRITE &&
457 procrastinate > 0 && !notstarted))
458 error = nfsrv_writegather(&nd, slp,
459 nfsd->nfsd_td, &mreq);
461 error = (*(nfsrv3_procs[nd->nd_procnum]))(nd,
462 slp, nfsd->nfsd_td, &mreq);
466 if (error != 0 && error != NFSERR_RETVOID) {
467 nfsrvstats.srv_errs++;
468 nfsrv_updatecache(nd, FALSE, mreq);
470 FREE(nd->nd_nam2, M_SONAME);
473 nfsrvstats.srvrpccnt[nd->nd_procnum]++;
474 nfsrv_updatecache(nd, TRUE, mreq);
479 siz = m_length(mreq, NULL);
480 if (siz <= 0 || siz > NFS_MAXPACKET) {
481 printf("mbuf siz=%d\n",siz);
482 panic("Bad nfs svc reply");
485 m->m_pkthdr.len = siz;
486 m->m_pkthdr.rcvif = NULL;
488 * For stream protocols, prepend a Sun RPC
491 if (sotype == SOCK_STREAM) {
492 M_PREPEND(m, NFSX_UNSIGNED, M_TRYWAIT);
493 *mtod(m, u_int32_t *) = htonl(0x80000000 | siz);
496 if (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED)
497 (void) nfs_slplock(slp, 1);
498 if (slp->ns_flag & SLP_VALID) {
500 error = nfsrv_send(slp->ns_so, nd->nd_nam2, m);
507 FREE(nd->nd_nam2, M_SONAME);
509 m_freem(nd->nd_mrep);
512 if (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED)
514 if (error == EINTR || error == ERESTART) {
515 if (nd->nd_cr != NULL)
517 free((caddr_t)nd, M_NFSRVDESC);
524 m_freem(nd->nd_mrep);
526 FREE(nd->nd_nam2, M_SONAME);
530 if (nd->nd_cr != NULL)
532 FREE((caddr_t)nd, M_NFSRVDESC);
537 * Check to see if there are outstanding writes that
538 * need to be serviced.
540 cur_usec = nfs_curusec();
542 if (LIST_FIRST(&slp->ns_tq) &&
543 LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec) {
549 } while (writes_todo);
551 if (nfsrv_dorec(slp, nfsd, &nd)) {
552 nfsd->nfsd_flag &= ~NFSD_REQINPROG;
553 nfsd->nfsd_slp = NULL;
556 mtx_assert(&Giant, MA_NOTOWNED);
559 mtx_assert(&Giant, MA_NOTOWNED);
560 TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
562 free((caddr_t)nfsd, M_NFSD);
563 if (--nfsrv_numnfsd == 0)
564 nfsrv_init(TRUE); /* Reinitialize everything */
570 * Shut down a socket associated with an nfssvc_sock structure.
571 * Should be called with the send lock set, if required.
572 * The trick here is to increment the sref at the start, so that the nfsds
573 * will stop using it and clear ns_flag at the end so that it will not be
574 * reassigned during cleanup.
577 nfsrv_zapsock(struct nfssvc_sock *slp)
579 struct nfsrv_descript *nwp, *nnwp;
582 struct nfsrv_rec *rec;
588 * XXXRW: By clearing all flags, other threads/etc should ignore
589 * this slp and we can safely release nfsd_mtx so we can clean
592 slp->ns_flag &= ~SLP_ALLFLAGS;
598 SOCKBUF_LOCK(&so->so_rcv);
599 so->so_rcv.sb_flags &= ~SB_UPCALL;
600 SOCKBUF_UNLOCK(&so->so_rcv);
601 so->so_upcall = NULL;
602 so->so_upcallarg = NULL;
603 soshutdown(so, SHUT_RDWR);
607 FREE(slp->ns_nam, M_SONAME);
608 m_freem(slp->ns_raw);
609 while ((rec = STAILQ_FIRST(&slp->ns_rec)) != NULL) {
610 STAILQ_REMOVE_HEAD(&slp->ns_rec, nr_link);
612 FREE(rec->nr_address, M_SONAME);
613 m_freem(rec->nr_packet);
614 free(rec, M_NFSRVDESC);
617 for (nwp = LIST_FIRST(&slp->ns_tq); nwp; nwp = nnwp) {
618 nnwp = LIST_NEXT(nwp, nd_tq);
619 LIST_REMOVE(nwp, nd_tq);
620 if (nwp->nd_cr != NULL)
622 free((caddr_t)nwp, M_NFSRVDESC);
624 LIST_INIT(&slp->ns_tq);
630 * Derefence a server socket structure. If it has no more references and
631 * is no longer valid, you can throw it away.
634 nfsrv_slpderef(struct nfssvc_sock *slp)
639 if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) {
640 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
641 free((caddr_t)slp, M_NFSSVC);
646 * Lock a socket against others.
648 * XXXRW: Wait argument is always 1 in the caller. Replace with a real
652 nfs_slplock(struct nfssvc_sock *slp, int wait)
654 int *statep = &slp->ns_solock;
658 if (!wait && (*statep & NFSRV_SNDLOCK))
659 return(0); /* already locked, fail */
660 while (*statep & NFSRV_SNDLOCK) {
661 *statep |= NFSRV_WANTSND;
662 (void) msleep(statep, &nfsd_mtx, PZERO - 1, "nfsslplck", 0);
664 *statep |= NFSRV_SNDLOCK;
669 * Unlock the stream socket for others.
672 nfs_slpunlock(struct nfssvc_sock *slp)
674 int *statep = &slp->ns_solock;
678 if ((*statep & NFSRV_SNDLOCK) == 0)
679 panic("nfs slpunlock");
680 *statep &= ~NFSRV_SNDLOCK;
681 if (*statep & NFSRV_WANTSND) {
682 *statep &= ~NFSRV_WANTSND;
688 * Initialize the data structures for the server.
689 * Handshake with any new nfsds starting up to avoid any chance of
693 nfsrv_init(int terminating)
695 struct nfssvc_sock *slp, *nslp;
699 if (nfssvc_sockhead_flag & SLP_INIT)
701 nfssvc_sockhead_flag |= SLP_INIT;
703 TAILQ_FOREACH_SAFE(slp, &nfssvc_sockhead, ns_chain, nslp) {
704 if (slp->ns_flag & SLP_VALID)
706 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
707 free((caddr_t)slp, M_NFSSVC);
709 nfsrv_cleancache(); /* And clear out server cache */
711 nfs_pub.np_valid = 0;
713 TAILQ_INIT(&nfssvc_sockhead);
714 nfssvc_sockhead_flag &= ~SLP_INIT;
715 if (nfssvc_sockhead_flag & SLP_WANTINIT) {
716 nfssvc_sockhead_flag &= ~SLP_WANTINIT;
717 wakeup(&nfssvc_sockhead);
720 TAILQ_INIT(&nfsd_head);
721 nfsd_head_flag &= ~NFSD_CHECKSLP;
724 nfs_udpsock = (struct nfssvc_sock *)
725 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK | M_ZERO);
726 STAILQ_INIT(&nfs_udpsock->ns_rec);
727 TAILQ_INSERT_HEAD(&nfssvc_sockhead, nfs_udpsock, ns_chain);
729 nfs_cltpsock = (struct nfssvc_sock *)
730 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK | M_ZERO);
731 STAILQ_INIT(&nfs_cltpsock->ns_rec);
732 TAILQ_INSERT_TAIL(&nfssvc_sockhead, nfs_cltpsock, ns_chain);