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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)nfs_syscalls.c 8.5 (Berkeley) 3/30/95
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/sysproto.h>
46 #include <sys/kernel.h>
47 #include <sys/sysctl.h>
49 #include <sys/filedesc.h>
50 #include <sys/vnode.h>
51 #include <sys/malloc.h>
52 #include <sys/mount.h>
57 #include <sys/socket.h>
58 #include <sys/socketvar.h>
59 #include <sys/domain.h>
60 #include <sys/protosw.h>
61 #include <sys/namei.h>
62 #include <sys/fcntl.h>
63 #include <sys/lockf.h>
65 #include <netinet/in.h>
66 #include <netinet/tcp.h>
67 #include <nfs/xdr_subs.h>
68 #include <nfs/rpcv2.h>
69 #include <nfs/nfsproto.h>
70 #include <nfsserver/nfs.h>
71 #include <nfsserver/nfsm_subs.h>
72 #include <nfsserver/nfsrvcache.h>
74 static MALLOC_DEFINE(M_NFSSVC, "NFS srvsock", "Nfs server structure");
76 MALLOC_DEFINE(M_NFSRVDESC, "NFSV3 srvdesc", "NFS server socket descriptor");
77 MALLOC_DEFINE(M_NFSD, "NFS daemon", "Nfs server daemon structure");
83 SYSCTL_DECL(_vfs_nfsrv);
86 static int nfs_numnfsd = 0;
87 static int notstarted = 1;
89 static int nfs_privport = 0;
90 SYSCTL_INT(_vfs_nfsrv, NFS_NFSPRIVPORT, nfs_privport, CTLFLAG_RW,
91 &nfs_privport, 0, "");
92 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, gatherdelay, CTLFLAG_RW,
93 &nfsrvw_procrastinate, 0, "");
94 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, gatherdelay_v3, CTLFLAG_RW,
95 &nfsrvw_procrastinate_v3, 0, "");
97 static int nfssvc_addsock(struct file *, struct sockaddr *,
99 static void nfsrv_zapsock(struct nfssvc_sock *slp);
100 static int nfssvc_nfsd(struct nfsd_srvargs *, caddr_t, struct thread *);
103 * NFS server system calls
107 * Nfs server psuedo system call for the nfsd's
108 * Based on the flag value it either:
109 * - adds a socket to the selection list
110 * - remains in the kernel as an nfsd
111 * - remains in the kernel as an nfsiod
113 #ifndef _SYS_SYSPROTO_H_
123 nfssvc(struct thread *td, struct nfssvc_args *uap)
126 struct sockaddr *nam;
127 struct nfsd_args nfsdarg;
128 struct nfsd_srvargs nfsd_srvargs, *nsd = &nfsd_srvargs;
135 while (nfssvc_sockhead_flag & SLP_INIT) {
136 nfssvc_sockhead_flag |= SLP_WANTINIT;
137 (void) tsleep((caddr_t)&nfssvc_sockhead, PSOCK, "nfsd init", 0);
139 if (uap->flag & NFSSVC_ADDSOCK) {
140 error = copyin(uap->argp, (caddr_t)&nfsdarg, sizeof(nfsdarg));
143 if ((error = fget(td, nfsdarg.sock, &fp)) != 0)
145 if (fp->f_type != DTYPE_SOCKET) {
150 * Get the client address for connected sockets.
152 if (nfsdarg.name == NULL || nfsdarg.namelen == 0)
153 nam = (struct sockaddr *)0;
155 error = getsockaddr(&nam, nfsdarg.name,
162 error = nfssvc_addsock(fp, nam, td);
164 } else if (uap->flag & NFSSVC_NFSD) {
165 error = copyin(uap->argp, (caddr_t)nsd, sizeof (*nsd));
168 error = nfssvc_nfsd(nsd, uap->argp, td);
172 if (error == EINTR || error == ERESTART)
180 * Adds a socket to the list for servicing by nfsds.
183 nfssvc_addsock(struct file *fp, struct sockaddr *mynam, struct thread *td)
186 struct nfssvc_sock *slp;
190 so = (struct socket *)fp->f_data;
192 tslp = (struct nfssvc_sock *)0;
194 * Add it to the list, as required.
196 if (so->so_proto->pr_protocol == IPPROTO_UDP) {
198 if (tslp->ns_flag & SLP_VALID) {
200 FREE(mynam, M_SONAME);
205 if (so->so_type == SOCK_STREAM)
206 siz = NFS_MAXPACKET + sizeof (u_long);
209 error = soreserve(so, siz, siz);
212 FREE(mynam, M_SONAME);
217 * Set protocol specific options { for now TCP only } and
218 * reserve some space. For datagram sockets, this can get called
219 * repeatedly for the same socket, but that isn't harmful.
221 if (so->so_type == SOCK_STREAM) {
225 bzero(&sopt, sizeof sopt);
226 sopt.sopt_level = SOL_SOCKET;
227 sopt.sopt_name = SO_KEEPALIVE;
228 sopt.sopt_val = &val;
229 sopt.sopt_valsize = sizeof val;
233 if (so->so_proto->pr_domain->dom_family == AF_INET &&
234 so->so_proto->pr_protocol == IPPROTO_TCP) {
238 bzero(&sopt, sizeof sopt);
239 sopt.sopt_level = IPPROTO_TCP;
240 sopt.sopt_name = TCP_NODELAY;
241 sopt.sopt_val = &val;
242 sopt.sopt_valsize = sizeof val;
246 so->so_rcv.sb_flags &= ~SB_NOINTR;
247 so->so_rcv.sb_timeo = 0;
248 so->so_snd.sb_flags &= ~SB_NOINTR;
249 so->so_snd.sb_timeo = 0;
251 slp = (struct nfssvc_sock *)
252 malloc(sizeof (struct nfssvc_sock), M_NFSSVC,
254 STAILQ_INIT(&slp->ns_rec);
255 TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
262 so->so_upcallarg = (caddr_t)slp;
263 so->so_upcall = nfsrv_rcv;
264 so->so_rcv.sb_flags |= SB_UPCALL;
265 slp->ns_flag = (SLP_VALID | SLP_NEEDQ);
272 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
273 * until it is killed by a signal.
276 nfssvc_nfsd(struct nfsd_srvargs *nsd, caddr_t argp, struct thread *td)
279 struct nfssvc_sock *slp;
280 struct nfsd *nfsd = nsd->nsd_nfsd;
281 struct nfsrv_descript *nd = NULL;
282 struct mbuf *m, *mreq;
283 int error = 0, cacherep, s, sotype, writes_todo;
291 if (nfsd == (struct nfsd *)0) {
292 nsd->nsd_nfsd = nfsd = (struct nfsd *)
293 malloc(sizeof (struct nfsd), M_NFSD, M_WAITOK | M_ZERO);
296 TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain);
302 * Loop getting rpc requests until SIGKILL.
305 if ((nfsd->nfsd_flag & NFSD_REQINPROG) == 0) {
306 while (nfsd->nfsd_slp == (struct nfssvc_sock *)0 &&
307 (nfsd_head_flag & NFSD_CHECKSLP) == 0) {
308 nfsd->nfsd_flag |= NFSD_WAITING;
310 error = tsleep((caddr_t)nfsd, PSOCK | PCATCH,
316 if (nfsd->nfsd_slp == (struct nfssvc_sock *)0 &&
317 (nfsd_head_flag & NFSD_CHECKSLP) != 0) {
318 TAILQ_FOREACH(slp, &nfssvc_sockhead, ns_chain) {
319 if ((slp->ns_flag & (SLP_VALID | SLP_DOREC))
320 == (SLP_VALID | SLP_DOREC)) {
321 slp->ns_flag &= ~SLP_DOREC;
323 nfsd->nfsd_slp = slp;
328 nfsd_head_flag &= ~NFSD_CHECKSLP;
330 if ((slp = nfsd->nfsd_slp) == (struct nfssvc_sock *)0)
332 if (slp->ns_flag & SLP_VALID) {
333 if (slp->ns_flag & SLP_DISCONN)
335 else if (slp->ns_flag & SLP_NEEDQ) {
336 slp->ns_flag &= ~SLP_NEEDQ;
337 (void) nfs_slplock(slp, 1);
338 nfsrv_rcv(slp->ns_so, (caddr_t)slp,
342 error = nfsrv_dorec(slp, nfsd, &nd);
343 cur_usec = nfs_curusec();
344 if (error && LIST_FIRST(&slp->ns_tq) &&
345 LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec) {
351 nfsd->nfsd_flag |= NFSD_REQINPROG;
355 slp = nfsd->nfsd_slp;
357 if (error || (slp->ns_flag & SLP_VALID) == 0) {
359 free((caddr_t)nd, M_NFSRVDESC);
362 nfsd->nfsd_slp = (struct nfssvc_sock *)0;
363 nfsd->nfsd_flag &= ~NFSD_REQINPROG;
368 sotype = slp->ns_so->so_type;
370 getmicrotime(&nd->nd_starttime);
372 nd->nd_nam = nd->nd_nam2;
374 nd->nd_nam = slp->ns_nam;
377 * Check to see if authorization is needed.
379 cacherep = nfsrv_getcache(nd, &mreq);
382 /* Check if source port is privileged */
384 struct sockaddr *nam = nd->nd_nam;
385 struct sockaddr_in *sin;
387 sin = (struct sockaddr_in *)nam;
388 port = ntohs(sin->sin_port);
389 if (port >= IPPORT_RESERVED &&
390 nd->nd_procnum != NFSPROC_NULL) {
391 nd->nd_procnum = NFSPROC_NOOP;
392 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
394 printf("NFS request from unprivileged port (%s:%d)\n",
395 inet_ntoa(sin->sin_addr), port);
402 * Loop to get all the write rpc relies that have been
408 if (nd && (nd->nd_flag & ND_NFSV3))
409 procrastinate = nfsrvw_procrastinate_v3;
411 procrastinate = nfsrvw_procrastinate;
412 if (writes_todo || (nd->nd_procnum == NFSPROC_WRITE &&
413 procrastinate > 0 && !notstarted))
414 error = nfsrv_writegather(&nd, slp,
415 nfsd->nfsd_td, &mreq);
417 error = (*(nfsrv3_procs[nd->nd_procnum]))(nd,
418 slp, nfsd->nfsd_td, &mreq);
421 if (error != 0 && error != NFSERR_RETVOID) {
422 nfsrvstats.srv_errs++;
423 nfsrv_updatecache(nd, FALSE, mreq);
425 FREE(nd->nd_nam2, M_SONAME);
428 nfsrvstats.srvrpccnt[nd->nd_procnum]++;
429 nfsrv_updatecache(nd, TRUE, mreq);
430 nd->nd_mrep = (struct mbuf *)0;
438 if (siz <= 0 || siz > NFS_MAXPACKET) {
439 printf("mbuf siz=%d\n",siz);
440 panic("Bad nfs svc reply");
443 m->m_pkthdr.len = siz;
444 m->m_pkthdr.rcvif = (struct ifnet *)0;
446 * For stream protocols, prepend a Sun RPC
449 if (sotype == SOCK_STREAM) {
450 M_PREPEND(m, NFSX_UNSIGNED, M_TRYWAIT);
451 *mtod(m, u_int32_t *) = htonl(0x80000000 | siz);
453 if (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED)
454 (void) nfs_slplock(slp, 1);
455 if (slp->ns_flag & SLP_VALID)
456 error = nfsrv_send(slp->ns_so, nd->nd_nam2, m);
462 FREE(nd->nd_nam2, M_SONAME);
464 m_freem(nd->nd_mrep);
467 if (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED)
469 if (error == EINTR || error == ERESTART) {
470 free((caddr_t)nd, M_NFSRVDESC);
477 m_freem(nd->nd_mrep);
479 FREE(nd->nd_nam2, M_SONAME);
483 FREE((caddr_t)nd, M_NFSRVDESC);
488 * Check to see if there are outstanding writes that
489 * need to be serviced.
491 cur_usec = nfs_curusec();
493 if (LIST_FIRST(&slp->ns_tq) &&
494 LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec) {
500 } while (writes_todo);
502 if (nfsrv_dorec(slp, nfsd, &nd)) {
503 nfsd->nfsd_flag &= ~NFSD_REQINPROG;
504 nfsd->nfsd_slp = NULL;
509 TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
511 free((caddr_t)nfsd, M_NFSD);
512 nsd->nsd_nfsd = (struct nfsd *)0;
513 if (--nfs_numnfsd == 0)
514 nfsrv_init(TRUE); /* Reinitialize everything */
519 * Shut down a socket associated with an nfssvc_sock structure.
520 * Should be called with the send lock set, if required.
521 * The trick here is to increment the sref at the start, so that the nfsds
522 * will stop using it and clear ns_flag at the end so that it will not be
523 * reassigned during cleanup.
526 nfsrv_zapsock(struct nfssvc_sock *slp)
528 struct nfsrv_descript *nwp, *nnwp;
531 struct nfsrv_rec *rec;
534 slp->ns_flag &= ~SLP_ALLFLAGS;
537 slp->ns_fp = (struct file *)0;
539 so->so_rcv.sb_flags &= ~SB_UPCALL;
540 so->so_upcall = NULL;
541 so->so_upcallarg = NULL;
543 closef(fp, (struct thread *)0);
545 FREE(slp->ns_nam, M_SONAME);
546 m_freem(slp->ns_raw);
547 while ((rec = STAILQ_FIRST(&slp->ns_rec)) != NULL) {
548 STAILQ_REMOVE_HEAD(&slp->ns_rec, nr_link);
550 FREE(rec->nr_address, M_SONAME);
551 m_freem(rec->nr_packet);
552 free(rec, M_NFSRVDESC);
555 for (nwp = LIST_FIRST(&slp->ns_tq); nwp; nwp = nnwp) {
556 nnwp = LIST_NEXT(nwp, nd_tq);
557 LIST_REMOVE(nwp, nd_tq);
558 free((caddr_t)nwp, M_NFSRVDESC);
560 LIST_INIT(&slp->ns_tq);
566 * Derefence a server socket structure. If it has no more references and
567 * is no longer valid, you can throw it away.
570 nfsrv_slpderef(struct nfssvc_sock *slp)
573 if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) {
574 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
575 free((caddr_t)slp, M_NFSSVC);
580 * Lock a socket against others.
583 nfs_slplock(struct nfssvc_sock *slp, int wait)
585 int *statep = &slp->ns_solock;
587 if (!wait && (*statep & NFSRV_SNDLOCK))
588 return(0); /* already locked, fail */
589 while (*statep & NFSRV_SNDLOCK) {
590 *statep |= NFSRV_WANTSND;
591 (void) tsleep((caddr_t)statep, PZERO - 1, "nfsslplck", 0);
593 *statep |= NFSRV_SNDLOCK;
598 * Unlock the stream socket for others.
601 nfs_slpunlock(struct nfssvc_sock *slp)
603 int *statep = &slp->ns_solock;
605 if ((*statep & NFSRV_SNDLOCK) == 0)
606 panic("nfs slpunlock");
607 *statep &= ~NFSRV_SNDLOCK;
608 if (*statep & NFSRV_WANTSND) {
609 *statep &= ~NFSRV_WANTSND;
610 wakeup((caddr_t)statep);
615 * Initialize the data structures for the server.
616 * Handshake with any new nfsds starting up to avoid any chance of
620 nfsrv_init(int terminating)
622 struct nfssvc_sock *slp, *nslp;
624 if (nfssvc_sockhead_flag & SLP_INIT)
626 nfssvc_sockhead_flag |= SLP_INIT;
628 for (slp = TAILQ_FIRST(&nfssvc_sockhead); slp != 0; slp = nslp){
629 nslp = TAILQ_NEXT(slp, ns_chain);
630 if (slp->ns_flag & SLP_VALID)
632 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
633 free((caddr_t)slp, M_NFSSVC);
635 nfsrv_cleancache(); /* And clear out server cache */
637 nfs_pub.np_valid = 0;
639 TAILQ_INIT(&nfssvc_sockhead);
640 nfssvc_sockhead_flag &= ~SLP_INIT;
641 if (nfssvc_sockhead_flag & SLP_WANTINIT) {
642 nfssvc_sockhead_flag &= ~SLP_WANTINIT;
643 wakeup((caddr_t)&nfssvc_sockhead);
646 TAILQ_INIT(&nfsd_head);
647 nfsd_head_flag &= ~NFSD_CHECKSLP;
650 nfs_udpsock = (struct nfssvc_sock *)
651 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK | M_ZERO);
652 STAILQ_INIT(&nfs_udpsock->ns_rec);
653 TAILQ_INSERT_HEAD(&nfssvc_sockhead, nfs_udpsock, ns_chain);
655 nfs_cltpsock = (struct nfssvc_sock *)
656 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK | M_ZERO);
657 STAILQ_INIT(&nfs_cltpsock->ns_rec);
658 TAILQ_INSERT_TAIL(&nfssvc_sockhead, nfs_cltpsock, ns_chain);