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
37 * $FreeBSD: src/sys/nfs/nfs_syscalls.c,v 1.58.2.1 2000/11/26 02:30:06 dillon Exp $
38 * $DragonFly: src/sys/vfs/nfs/nfs_syscalls.c,v 1.9 2003/08/20 09:56:33 rob Exp $
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/sysproto.h>
44 #include <sys/kernel.h>
45 #include <sys/sysctl.h>
47 #include <sys/filedesc.h>
48 #include <sys/vnode.h>
49 #include <sys/malloc.h>
50 #include <sys/mount.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56 #include <sys/domain.h>
57 #include <sys/protosw.h>
58 #include <sys/namei.h>
59 #include <vm/vm_zone.h>
61 #include <netinet/in.h>
62 #include <netinet/tcp.h>
67 #include "nfsm_subs.h"
68 #include "nfsrvcache.h"
74 static MALLOC_DEFINE(M_NFSSVC, "NFS srvsock", "Nfs server structure");
77 extern int32_t (*nfsrv3_procs[NFS_NPROCS]) (struct nfsrv_descript *nd,
78 struct nfssvc_sock *slp,
81 extern int nfs_numasync;
82 extern time_t nqnfsstarttime;
83 extern int nqsrv_writeslack;
85 extern struct nfsstats nfsstats;
86 extern int nfsrvw_procrastinate;
87 extern int nfsrvw_procrastinate_v3;
88 static int nuidhash_max = NFS_MAXUIDHASH;
91 static void nfsrv_zapsock (struct nfssvc_sock *slp);
93 static int nfssvc_iod (struct thread *);
98 static int nfs_asyncdaemon[NFS_MAXASYNCDAEMON];
100 SYSCTL_DECL(_vfs_nfs);
103 int nfsd_waiting = 0;
104 static struct nfsdrt nfsdrt;
105 static int nfs_numnfsd = 0;
106 static int notstarted = 1;
107 static int modify_flag = 0;
108 static void nfsd_rt (int sotype, struct nfsrv_descript *nd,
110 static int nfssvc_addsock (struct file *, struct sockaddr *,
112 static int nfssvc_nfsd (struct nfsd_srvargs *,caddr_t,struct thread *);
114 static int nfs_privport = 0;
115 SYSCTL_INT(_vfs_nfs, NFS_NFSPRIVPORT, nfs_privport, CTLFLAG_RW, &nfs_privport, 0, "");
116 SYSCTL_INT(_vfs_nfs, OID_AUTO, gatherdelay, CTLFLAG_RW, &nfsrvw_procrastinate, 0, "");
117 SYSCTL_INT(_vfs_nfs, OID_AUTO, gatherdelay_v3, CTLFLAG_RW, &nfsrvw_procrastinate_v3, 0, "");
120 * NFS server system calls
123 #endif /* NFS_NOSERVER */
125 * nfssvc_args(int flag, caddr_t argp)
127 * Nfs server psuedo system call for the nfsd's
128 * Based on the flag value it either:
129 * - adds a socket to the selection list
130 * - remains in the kernel as an nfsd
131 * - remains in the kernel as an nfsiod
134 nfssvc(struct nfssvc_args *uap)
139 struct sockaddr *nam;
140 struct nfsd_args nfsdarg;
141 struct nfsd_srvargs nfsd_srvargs, *nsd = &nfsd_srvargs;
142 struct nfsd_cargs ncd;
144 struct nfssvc_sock *slp;
145 struct nfsuid *nuidp;
146 struct nfsmount *nmp;
147 #endif /* NFS_NOSERVER */
149 struct thread *td = curthread;
157 KKASSERT(td->td_proc); /* for ucred and p_fd */
158 while (nfssvc_sockhead_flag & SLP_INIT) {
159 nfssvc_sockhead_flag |= SLP_WANTINIT;
160 (void) tsleep((caddr_t)&nfssvc_sockhead, 0, "nfsd init", 0);
162 if (uap->flag & NFSSVC_BIOD)
163 error = nfssvc_iod(td);
167 #else /* !NFS_NOSERVER */
168 else if (uap->flag & NFSSVC_MNTD) {
169 error = copyin(uap->argp, (caddr_t)&ncd, sizeof (ncd));
172 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
177 NDFREE(&nd, NDF_ONLY_PNBUF);
178 if ((nd.ni_vp->v_flag & VROOT) == 0)
180 nmp = VFSTONFS(nd.ni_vp->v_mount);
184 if ((nmp->nm_state & NFSSTA_MNTD) &&
185 (uap->flag & NFSSVC_GOTAUTH) == 0)
187 nmp->nm_state |= NFSSTA_MNTD;
188 error = nqnfs_clientd(nmp, td->td_proc->p_ucred, &ncd, uap->flag,
190 } else if (uap->flag & NFSSVC_ADDSOCK) {
191 error = copyin(uap->argp, (caddr_t)&nfsdarg, sizeof(nfsdarg));
194 error = holdsock(td->td_proc->p_fd, nfsdarg.sock, &fp);
198 * Get the client address for connected sockets.
200 if (nfsdarg.name == NULL || nfsdarg.namelen == 0)
201 nam = (struct sockaddr *)0;
203 error = getsockaddr(&nam, nfsdarg.name,
210 error = nfssvc_addsock(fp, nam, td);
213 error = copyin(uap->argp, (caddr_t)nsd, sizeof (*nsd));
216 if ((uap->flag & NFSSVC_AUTHIN) &&
217 ((nfsd = nsd->nsd_nfsd)) != NULL &&
218 (nfsd->nfsd_slp->ns_flag & SLP_VALID)) {
219 slp = nfsd->nfsd_slp;
222 * First check to see if another nfsd has already
223 * added this credential.
225 for (nuidp = NUIDHASH(slp,nsd->nsd_cr.cr_uid)->lh_first;
226 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
227 if (nuidp->nu_cr.cr_uid == nsd->nsd_cr.cr_uid &&
228 (!nfsd->nfsd_nd->nd_nam2 ||
229 netaddr_match(NU_NETFAM(nuidp),
230 &nuidp->nu_haddr, nfsd->nfsd_nd->nd_nam2)))
234 nfsrv_setcred(&nuidp->nu_cr,&nfsd->nfsd_nd->nd_cr);
235 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
240 if (slp->ns_numuids < nuidhash_max) {
242 nuidp = (struct nfsuid *)
243 malloc(sizeof (struct nfsuid), M_NFSUID,
246 nuidp = (struct nfsuid *)0;
247 if ((slp->ns_flag & SLP_VALID) == 0) {
249 free((caddr_t)nuidp, M_NFSUID);
251 if (nuidp == (struct nfsuid *)0) {
252 nuidp = slp->ns_uidlruhead.tqh_first;
253 LIST_REMOVE(nuidp, nu_hash);
254 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp,
256 if (nuidp->nu_flag & NU_NAM)
257 FREE(nuidp->nu_nam, M_SONAME);
260 nuidp->nu_cr = nsd->nsd_cr;
261 if (nuidp->nu_cr.cr_ngroups > NGROUPS)
262 nuidp->nu_cr.cr_ngroups = NGROUPS;
263 nuidp->nu_cr.cr_ref = 1;
264 nuidp->nu_timestamp = nsd->nsd_timestamp;
265 nuidp->nu_expire = time_second + nsd->nsd_ttl;
267 * and save the session key in nu_key.
269 bcopy(nsd->nsd_key, nuidp->nu_key,
270 sizeof (nsd->nsd_key));
271 if (nfsd->nfsd_nd->nd_nam2) {
272 struct sockaddr_in *saddr;
274 saddr = (struct sockaddr_in *)
275 nfsd->nfsd_nd->nd_nam2;
276 switch (saddr->sin_family) {
278 nuidp->nu_flag |= NU_INETADDR;
280 saddr->sin_addr.s_addr;
284 nuidp->nu_flag |= NU_NAM;
286 dup_sockaddr(nfsd->nfsd_nd->
291 TAILQ_INSERT_TAIL(&slp->ns_uidlruhead, nuidp,
293 LIST_INSERT_HEAD(NUIDHASH(slp, nsd->nsd_uid),
295 nfsrv_setcred(&nuidp->nu_cr,
296 &nfsd->nfsd_nd->nd_cr);
297 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
301 if ((uap->flag & NFSSVC_AUTHINFAIL) && (nfsd = nsd->nsd_nfsd))
302 nfsd->nfsd_flag |= NFSD_AUTHFAIL;
303 error = nfssvc_nfsd(nsd, uap->argp, td);
305 #endif /* NFS_NOSERVER */
306 if (error == EINTR || error == ERESTART)
313 * Adds a socket to the list for servicing by nfsds.
316 nfssvc_addsock(struct file *fp, struct sockaddr *mynam, struct thread *td)
319 struct nfssvc_sock *slp;
323 so = (struct socket *)fp->f_data;
325 tslp = (struct nfssvc_sock *)0;
327 * Add it to the list, as required.
329 if (so->so_proto->pr_protocol == IPPROTO_UDP) {
331 if (tslp->ns_flag & SLP_VALID) {
333 FREE(mynam, M_SONAME);
338 if (so->so_type == SOCK_STREAM)
339 siz = NFS_MAXPACKET + sizeof (u_long);
342 error = soreserve(so, siz, siz);
345 FREE(mynam, M_SONAME);
350 * Set protocol specific options { for now TCP only } and
351 * reserve some space. For datagram sockets, this can get called
352 * repeatedly for the same socket, but that isn't harmful.
354 if (so->so_type == SOCK_STREAM) {
358 bzero(&sopt, sizeof sopt);
359 sopt.sopt_level = SOL_SOCKET;
360 sopt.sopt_name = SO_KEEPALIVE;
361 sopt.sopt_val = &val;
362 sopt.sopt_valsize = sizeof val;
366 if (so->so_proto->pr_domain->dom_family == AF_INET &&
367 so->so_proto->pr_protocol == IPPROTO_TCP) {
371 bzero(&sopt, sizeof sopt);
372 sopt.sopt_level = IPPROTO_TCP;
373 sopt.sopt_name = TCP_NODELAY;
374 sopt.sopt_val = &val;
375 sopt.sopt_valsize = sizeof val;
379 so->so_rcv.sb_flags &= ~SB_NOINTR;
380 so->so_rcv.sb_timeo = 0;
381 so->so_snd.sb_flags &= ~SB_NOINTR;
382 so->so_snd.sb_timeo = 0;
384 slp = (struct nfssvc_sock *)
385 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
386 bzero((caddr_t)slp, sizeof (struct nfssvc_sock));
387 STAILQ_INIT(&slp->ns_rec);
388 TAILQ_INIT(&slp->ns_uidlruhead);
389 TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
396 so->so_upcallarg = (caddr_t)slp;
397 so->so_upcall = nfsrv_rcv;
398 so->so_rcv.sb_flags |= SB_UPCALL;
399 slp->ns_flag = (SLP_VALID | SLP_NEEDQ);
406 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
407 * until it is killed by a signal.
410 nfssvc_nfsd(struct nfsd_srvargs *nsd, caddr_t argp, struct thread *td)
413 struct nfssvc_sock *slp;
414 struct nfsd *nfsd = nsd->nsd_nfsd;
415 struct nfsrv_descript *nd = NULL;
416 struct mbuf *m, *mreq;
417 int error = 0, cacherep, s, sotype, writes_todo;
425 if (nfsd == (struct nfsd *)0) {
426 nsd->nsd_nfsd = nfsd = (struct nfsd *)
427 malloc(sizeof (struct nfsd), M_NFSD, M_WAITOK);
428 bzero((caddr_t)nfsd, sizeof (struct nfsd));
431 TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain);
437 * Loop getting rpc requests until SIGKILL.
440 if ((nfsd->nfsd_flag & NFSD_REQINPROG) == 0) {
441 while (nfsd->nfsd_slp == (struct nfssvc_sock *)0 &&
442 (nfsd_head_flag & NFSD_CHECKSLP) == 0) {
443 nfsd->nfsd_flag |= NFSD_WAITING;
445 error = tsleep((caddr_t)nfsd, PCATCH, "nfsd", 0);
450 if (nfsd->nfsd_slp == (struct nfssvc_sock *)0 &&
451 (nfsd_head_flag & NFSD_CHECKSLP) != 0) {
452 for (slp = nfssvc_sockhead.tqh_first; slp != 0;
453 slp = slp->ns_chain.tqe_next) {
454 if ((slp->ns_flag & (SLP_VALID | SLP_DOREC))
455 == (SLP_VALID | SLP_DOREC)) {
456 slp->ns_flag &= ~SLP_DOREC;
458 nfsd->nfsd_slp = slp;
463 nfsd_head_flag &= ~NFSD_CHECKSLP;
465 if ((slp = nfsd->nfsd_slp) == (struct nfssvc_sock *)0)
467 if (slp->ns_flag & SLP_VALID) {
468 if (slp->ns_flag & SLP_DISCONN)
470 else if (slp->ns_flag & SLP_NEEDQ) {
471 slp->ns_flag &= ~SLP_NEEDQ;
472 (void) nfs_slplock(slp, 1);
473 nfsrv_rcv(slp->ns_so, (caddr_t)slp,
477 error = nfsrv_dorec(slp, nfsd, &nd);
478 cur_usec = nfs_curusec();
479 if (error && slp->ns_tq.lh_first &&
480 slp->ns_tq.lh_first->nd_time <= cur_usec) {
486 nfsd->nfsd_flag |= NFSD_REQINPROG;
490 slp = nfsd->nfsd_slp;
492 if (error || (slp->ns_flag & SLP_VALID) == 0) {
494 free((caddr_t)nd, M_NFSRVDESC);
497 nfsd->nfsd_slp = (struct nfssvc_sock *)0;
498 nfsd->nfsd_flag &= ~NFSD_REQINPROG;
503 sotype = slp->ns_so->so_type;
505 getmicrotime(&nd->nd_starttime);
507 nd->nd_nam = nd->nd_nam2;
509 nd->nd_nam = slp->ns_nam;
512 * Check to see if authorization is needed.
514 if (nfsd->nfsd_flag & NFSD_NEEDAUTH) {
515 nfsd->nfsd_flag &= ~NFSD_NEEDAUTH;
517 ((struct sockaddr_in *)
518 nd->nd_nam)->sin_addr.s_addr;
519 nsd->nsd_authlen = nfsd->nfsd_authlen;
520 nsd->nsd_verflen = nfsd->nfsd_verflen;
521 if (!copyout(nfsd->nfsd_authstr,nsd->nsd_authstr,
522 nfsd->nfsd_authlen) &&
523 !copyout(nfsd->nfsd_verfstr, nsd->nsd_verfstr,
524 nfsd->nfsd_verflen) &&
525 !copyout((caddr_t)nsd, argp, sizeof (*nsd)))
527 cacherep = RC_DROPIT;
529 cacherep = nfsrv_getcache(nd, slp, &mreq);
532 * Check for just starting up for NQNFS and send
533 * fake "try again later" replies to the NQNFS clients.
535 if (notstarted && nqnfsstarttime <= time_second) {
537 nqnfsstarttime = time_second + nqsrv_writeslack;
543 if ((nd->nd_flag & ND_NQNFS) == 0)
544 cacherep = RC_DROPIT;
545 else if (nd->nd_procnum != NFSPROC_WRITE) {
546 nd->nd_procnum = NFSPROC_NOOP;
547 nd->nd_repstat = NQNFS_TRYLATER;
551 } else if (nfsd->nfsd_flag & NFSD_AUTHFAIL) {
552 nfsd->nfsd_flag &= ~NFSD_AUTHFAIL;
553 nd->nd_procnum = NFSPROC_NOOP;
554 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
556 } else if (nfs_privport) {
557 /* Check if source port is privileged */
559 struct sockaddr *nam = nd->nd_nam;
560 struct sockaddr_in *sin;
562 sin = (struct sockaddr_in *)nam;
563 port = ntohs(sin->sin_port);
564 if (port >= IPPORT_RESERVED &&
565 nd->nd_procnum != NFSPROC_NULL) {
566 nd->nd_procnum = NFSPROC_NOOP;
567 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
569 printf("NFS request from unprivileged port (%s:%d)\n",
570 inet_ntoa(sin->sin_addr), port);
577 * Loop to get all the write rpc relies that have been
583 if (nd && (nd->nd_flag & ND_NFSV3))
584 procrastinate = nfsrvw_procrastinate_v3;
586 procrastinate = nfsrvw_procrastinate;
587 if (writes_todo || (nd->nd_procnum == NFSPROC_WRITE &&
588 procrastinate > 0 && !notstarted))
589 error = nfsrv_writegather(&nd, slp,
590 nfsd->nfsd_td, &mreq);
592 error = (*(nfsrv3_procs[nd->nd_procnum]))(nd,
593 slp, nfsd->nfsd_td, &mreq);
596 if (error != 0 && error != NFSERR_RETVOID) {
597 if (nd->nd_procnum != NQNFSPROC_VACATED)
599 nfsrv_updatecache(nd, FALSE, mreq);
601 FREE(nd->nd_nam2, M_SONAME);
604 nfsstats.srvrpccnt[nd->nd_procnum]++;
605 nfsrv_updatecache(nd, TRUE, mreq);
606 nd->nd_mrep = (struct mbuf *)0;
614 if (siz <= 0 || siz > NFS_MAXPACKET) {
615 printf("mbuf siz=%d\n",siz);
616 panic("Bad nfs svc reply");
619 m->m_pkthdr.len = siz;
620 m->m_pkthdr.rcvif = (struct ifnet *)0;
622 * For stream protocols, prepend a Sun RPC
625 if (sotype == SOCK_STREAM) {
626 M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
627 *mtod(m, u_int32_t *) = htonl(0x80000000 | siz);
629 if (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED)
630 (void) nfs_slplock(slp, 1);
631 if (slp->ns_flag & SLP_VALID)
632 error = nfs_send(slp->ns_so, nd->nd_nam2, m, NULL);
638 nfsd_rt(sotype, nd, cacherep);
640 FREE(nd->nd_nam2, M_SONAME);
642 m_freem(nd->nd_mrep);
645 if (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED)
647 if (error == EINTR || error == ERESTART) {
648 free((caddr_t)nd, M_NFSRVDESC);
656 nfsd_rt(sotype, nd, cacherep);
657 m_freem(nd->nd_mrep);
659 FREE(nd->nd_nam2, M_SONAME);
663 FREE((caddr_t)nd, M_NFSRVDESC);
668 * Check to see if there are outstanding writes that
669 * need to be serviced.
671 cur_usec = nfs_curusec();
673 if (slp->ns_tq.lh_first &&
674 slp->ns_tq.lh_first->nd_time <= cur_usec) {
680 } while (writes_todo);
682 if (nfsrv_dorec(slp, nfsd, &nd)) {
683 nfsd->nfsd_flag &= ~NFSD_REQINPROG;
684 nfsd->nfsd_slp = NULL;
689 TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
691 free((caddr_t)nfsd, M_NFSD);
692 nsd->nsd_nfsd = (struct nfsd *)0;
693 if (--nfs_numnfsd == 0)
694 nfsrv_init(TRUE); /* Reinitialize everything */
699 * Shut down a socket associated with an nfssvc_sock structure.
700 * Should be called with the send lock set, if required.
701 * The trick here is to increment the sref at the start, so that the nfsds
702 * will stop using it and clear ns_flag at the end so that it will not be
703 * reassigned during cleanup.
707 struct nfssvc_sock *slp;
709 struct nfsuid *nuidp, *nnuidp;
710 struct nfsrv_descript *nwp, *nnwp;
713 struct nfsrv_rec *rec;
716 slp->ns_flag &= ~SLP_ALLFLAGS;
719 slp->ns_fp = (struct file *)0;
721 so->so_rcv.sb_flags &= ~SB_UPCALL;
722 so->so_upcall = NULL;
723 so->so_upcallarg = NULL;
727 FREE(slp->ns_nam, M_SONAME);
728 m_freem(slp->ns_raw);
729 while ((rec = STAILQ_FIRST(&slp->ns_rec)) != NULL) {
730 STAILQ_REMOVE_HEAD(&slp->ns_rec, nr_link);
732 FREE(rec->nr_address, M_SONAME);
733 m_freem(rec->nr_packet);
734 free(rec, M_NFSRVDESC);
736 for (nuidp = slp->ns_uidlruhead.tqh_first; nuidp != 0;
738 nnuidp = nuidp->nu_lru.tqe_next;
739 LIST_REMOVE(nuidp, nu_hash);
740 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru);
741 if (nuidp->nu_flag & NU_NAM)
742 FREE(nuidp->nu_nam, M_SONAME);
743 free((caddr_t)nuidp, M_NFSUID);
746 for (nwp = slp->ns_tq.lh_first; nwp; nwp = nnwp) {
747 nnwp = nwp->nd_tq.le_next;
748 LIST_REMOVE(nwp, nd_tq);
749 free((caddr_t)nwp, M_NFSRVDESC);
751 LIST_INIT(&slp->ns_tq);
757 * Derefence a server socket structure. If it has no more references and
758 * is no longer valid, you can throw it away.
762 struct nfssvc_sock *slp;
764 if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) {
765 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
766 free((caddr_t)slp, M_NFSSVC);
771 * Lock a socket against others.
774 nfs_slplock(slp, wait)
775 struct nfssvc_sock *slp;
778 int *statep = &slp->ns_solock;
780 if (!wait && (*statep & NFSSTA_SNDLOCK))
781 return(0); /* already locked, fail */
782 while (*statep & NFSSTA_SNDLOCK) {
783 *statep |= NFSSTA_WANTSND;
784 (void) tsleep((caddr_t)statep, 0, "nfsslplck", 0);
786 *statep |= NFSSTA_SNDLOCK;
791 * Unlock the stream socket for others.
795 struct nfssvc_sock *slp;
797 int *statep = &slp->ns_solock;
799 if ((*statep & NFSSTA_SNDLOCK) == 0)
800 panic("nfs slpunlock");
801 *statep &= ~NFSSTA_SNDLOCK;
802 if (*statep & NFSSTA_WANTSND) {
803 *statep &= ~NFSSTA_WANTSND;
804 wakeup((caddr_t)statep);
809 * Initialize the data structures for the server.
810 * Handshake with any new nfsds starting up to avoid any chance of
814 nfsrv_init(terminating)
817 struct nfssvc_sock *slp, *nslp;
819 if (nfssvc_sockhead_flag & SLP_INIT)
821 nfssvc_sockhead_flag |= SLP_INIT;
823 for (slp = nfssvc_sockhead.tqh_first; slp != 0; slp = nslp) {
824 nslp = slp->ns_chain.tqe_next;
825 if (slp->ns_flag & SLP_VALID)
827 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
828 free((caddr_t)slp, M_NFSSVC);
830 nfsrv_cleancache(); /* And clear out server cache */
832 nfs_pub.np_valid = 0;
834 TAILQ_INIT(&nfssvc_sockhead);
835 nfssvc_sockhead_flag &= ~SLP_INIT;
836 if (nfssvc_sockhead_flag & SLP_WANTINIT) {
837 nfssvc_sockhead_flag &= ~SLP_WANTINIT;
838 wakeup((caddr_t)&nfssvc_sockhead);
841 TAILQ_INIT(&nfsd_head);
842 nfsd_head_flag &= ~NFSD_CHECKSLP;
845 nfs_udpsock = (struct nfssvc_sock *)
846 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
847 bzero((caddr_t)nfs_udpsock, sizeof (struct nfssvc_sock));
848 STAILQ_INIT(&nfs_udpsock->ns_rec);
849 TAILQ_INIT(&nfs_udpsock->ns_uidlruhead);
850 TAILQ_INSERT_HEAD(&nfssvc_sockhead, nfs_udpsock, ns_chain);
852 nfs_cltpsock = (struct nfssvc_sock *)
853 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
854 bzero((caddr_t)nfs_cltpsock, sizeof (struct nfssvc_sock));
855 STAILQ_INIT(&nfs_cltpsock->ns_rec);
856 TAILQ_INIT(&nfs_cltpsock->ns_uidlruhead);
857 TAILQ_INSERT_TAIL(&nfssvc_sockhead, nfs_cltpsock, ns_chain);
862 * Add entries to the server monitor log.
865 nfsd_rt(sotype, nd, cacherep)
867 struct nfsrv_descript *nd;
872 rt = &nfsdrt.drt[nfsdrt.pos];
873 if (cacherep == RC_DOIT)
875 else if (cacherep == RC_REPLY)
876 rt->flag = DRT_CACHEREPLY;
878 rt->flag = DRT_CACHEDROP;
879 if (sotype == SOCK_STREAM)
881 if (nd->nd_flag & ND_NQNFS)
882 rt->flag |= DRT_NQNFS;
883 else if (nd->nd_flag & ND_NFSV3)
884 rt->flag |= DRT_NFSV3;
885 rt->proc = nd->nd_procnum;
886 if (nd->nd_nam->sa_family == AF_INET)
887 rt->ipadr = ((struct sockaddr_in *)nd->nd_nam)->sin_addr.s_addr;
889 rt->ipadr = INADDR_ANY;
890 rt->resptime = nfs_curusec() - (nd->nd_starttime.tv_sec * 1000000 + nd->nd_starttime.tv_usec);
891 getmicrotime(&rt->tstamp);
892 nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ;
894 #endif /* NFS_NOSERVER */
896 static int nfs_defect = 0;
897 SYSCTL_INT(_vfs_nfs, OID_AUTO, defect, CTLFLAG_RW, &nfs_defect, 0, "");
900 * Asynchronous I/O daemons for client nfs.
901 * They do read-ahead and write-behind operations on the block I/O cache.
902 * Never returns unless it fails or gets killed.
905 nfssvc_iod(struct thread *td)
909 struct nfsmount *nmp;
913 * Assign my position or return error if too many already running
916 for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
917 if (nfs_asyncdaemon[i] == 0) {
918 nfs_asyncdaemon[i]++;
926 * Just loop around doin our stuff until SIGKILL
929 while (((nmp = nfs_iodmount[myiod]) == NULL
930 || nmp->nm_bufq.tqh_first == NULL)
934 nfs_iodwant[myiod] = td;
935 nfs_iodmount[myiod] = NULL;
936 error = tsleep((caddr_t)&nfs_iodwant[myiod],
937 PCATCH, "nfsidl", 0);
940 nfs_asyncdaemon[myiod] = 0;
943 nfs_iodwant[myiod] = NULL;
944 nfs_iodmount[myiod] = NULL;
948 while ((bp = nmp->nm_bufq.tqh_first) != NULL) {
949 /* Take one off the front of the list */
950 TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist);
952 if (nmp->nm_bufqwant && nmp->nm_bufqlen <= nfs_numasync) {
953 nmp->nm_bufqwant = FALSE;
954 wakeup(&nmp->nm_bufq);
956 (void) nfs_doio(bp, NULL);
958 * If there are more than one iod on this mount, then defect
959 * so that the iods can be shared out fairly between the mounts
961 if (nfs_defect && nmp->nm_bufqiods > 1) {
963 ("nfssvc_iod: iod %d defecting from mount %p\n",
965 nfs_iodmount[myiod] = NULL;
975 * Get an authorization string for the uid by having the mount_nfs sitting
976 * on this mount point porpous out of the kernel and do it.
979 nfs_getauth(nmp, rep, cred, auth_str, auth_len, verf_str, verf_len, key)
980 struct nfsmount *nmp;
987 NFSKERBKEY_T key; /* return session key */
991 while ((nmp->nm_state & NFSSTA_WAITAUTH) == 0) {
992 nmp->nm_state |= NFSSTA_WANTAUTH;
993 (void) tsleep((caddr_t)&nmp->nm_authtype, 0,
995 error = nfs_sigintr(nmp, rep, rep->r_td);
997 nmp->nm_state &= ~NFSSTA_WANTAUTH;
1001 nmp->nm_state &= ~(NFSSTA_WAITAUTH | NFSSTA_WANTAUTH);
1002 nmp->nm_authstr = *auth_str = (char *)malloc(RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK);
1003 nmp->nm_authlen = RPCAUTH_MAXSIZ;
1004 nmp->nm_verfstr = verf_str;
1005 nmp->nm_verflen = *verf_len;
1006 nmp->nm_authuid = cred->cr_uid;
1007 wakeup((caddr_t)&nmp->nm_authstr);
1010 * And wait for mount_nfs to do its stuff.
1012 while ((nmp->nm_state & NFSSTA_HASAUTH) == 0 && error == 0) {
1013 (void) tsleep((caddr_t)&nmp->nm_authlen, 0,
1014 "nfsauth2", 2 * hz);
1015 error = nfs_sigintr(nmp, rep, rep->r_td);
1017 if (nmp->nm_state & NFSSTA_AUTHERR) {
1018 nmp->nm_state &= ~NFSSTA_AUTHERR;
1022 free((caddr_t)*auth_str, M_TEMP);
1024 *auth_len = nmp->nm_authlen;
1025 *verf_len = nmp->nm_verflen;
1026 bcopy((caddr_t)nmp->nm_key, (caddr_t)key, sizeof (key));
1028 nmp->nm_state &= ~NFSSTA_HASAUTH;
1029 nmp->nm_state |= NFSSTA_WAITAUTH;
1030 if (nmp->nm_state & NFSSTA_WANTAUTH) {
1031 nmp->nm_state &= ~NFSSTA_WANTAUTH;
1032 wakeup((caddr_t)&nmp->nm_authtype);
1038 * Get a nickname authenticator and verifier.
1041 nfs_getnickauth(nmp, cred, auth_str, auth_len, verf_str, verf_len)
1042 struct nfsmount *nmp;
1049 struct nfsuid *nuidp;
1050 u_int32_t *nickp, *verfp;
1051 struct timeval ktvin, ktvout;
1054 if (verf_len < (4 * NFSX_UNSIGNED))
1055 panic("nfs_getnickauth verf too small");
1057 for (nuidp = NMUIDHASH(nmp, cred->cr_uid)->lh_first;
1058 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
1059 if (nuidp->nu_cr.cr_uid == cred->cr_uid)
1062 if (!nuidp || nuidp->nu_expire < time_second)
1066 * Move to the end of the lru list (end of lru == most recently used).
1068 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
1069 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, nu_lru);
1071 nickp = (u_int32_t *)malloc(2 * NFSX_UNSIGNED, M_TEMP, M_WAITOK);
1072 *nickp++ = txdr_unsigned(RPCAKN_NICKNAME);
1073 *nickp = txdr_unsigned(nuidp->nu_nickname);
1074 *auth_str = (char *)nickp;
1075 *auth_len = 2 * NFSX_UNSIGNED;
1078 * Now we must encrypt the verifier and package it up.
1080 verfp = (u_int32_t *)verf_str;
1081 *verfp++ = txdr_unsigned(RPCAKN_NICKNAME);
1082 if (time_second > nuidp->nu_timestamp.tv_sec ||
1083 (time_second == nuidp->nu_timestamp.tv_sec &&
1084 time_second > nuidp->nu_timestamp.tv_usec))
1085 getmicrotime(&nuidp->nu_timestamp);
1087 nuidp->nu_timestamp.tv_usec++;
1088 ktvin.tv_sec = txdr_unsigned(nuidp->nu_timestamp.tv_sec);
1089 ktvin.tv_usec = txdr_unsigned(nuidp->nu_timestamp.tv_usec);
1092 * Now encrypt the timestamp verifier in ecb mode using the session
1099 *verfp++ = ktvout.tv_sec;
1100 *verfp++ = ktvout.tv_usec;
1106 * Save the current nickname in a hash list entry on the mount point.
1109 nfs_savenickauth(nmp, cred, len, key, mdp, dposp, mrep)
1110 struct nfsmount *nmp;
1118 struct nfsuid *nuidp;
1121 struct mbuf *md = *mdp;
1122 struct timeval ktvin, ktvout;
1124 char *dpos = *dposp, *cp2;
1125 int deltasec, error = 0;
1127 if (len == (3 * NFSX_UNSIGNED)) {
1128 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1129 ktvin.tv_sec = *tl++;
1130 ktvin.tv_usec = *tl++;
1131 nick = fxdr_unsigned(u_int32_t, *tl);
1134 * Decrypt the timestamp in ecb mode.
1139 ktvout.tv_sec = fxdr_unsigned(long, ktvout.tv_sec);
1140 ktvout.tv_usec = fxdr_unsigned(long, ktvout.tv_usec);
1141 deltasec = time_second - ktvout.tv_sec;
1143 deltasec = -deltasec;
1145 * If ok, add it to the hash list for the mount point.
1147 if (deltasec <= NFS_KERBCLOCKSKEW) {
1148 if (nmp->nm_numuids < nuidhash_max) {
1150 nuidp = (struct nfsuid *)
1151 malloc(sizeof (struct nfsuid), M_NFSUID,
1154 nuidp = nmp->nm_uidlruhead.tqh_first;
1155 LIST_REMOVE(nuidp, nu_hash);
1156 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp,
1160 nuidp->nu_cr.cr_uid = cred->cr_uid;
1161 nuidp->nu_expire = time_second + NFS_KERBTTL;
1162 nuidp->nu_timestamp = ktvout;
1163 nuidp->nu_nickname = nick;
1164 bcopy(key, nuidp->nu_key, sizeof (key));
1165 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp,
1167 LIST_INSERT_HEAD(NMUIDHASH(nmp, cred->cr_uid),
1171 nfsm_adv(nfsm_rndup(len));