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.28 2006/12/23 00:41:29 swildner 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/resourcevar.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
57 #include <sys/domain.h>
58 #include <sys/protosw.h>
59 #include <sys/nlookup.h>
60 #include <vm/vm_zone.h>
62 #include <netinet/in.h>
63 #include <netinet/tcp.h>
68 #include "nfsm_subs.h"
69 #include "nfsrvcache.h"
74 #include <sys/thread2.h>
76 static MALLOC_DEFINE(M_NFSSVC, "NFS srvsock", "Nfs server structure");
79 extern int32_t (*nfsrv3_procs[NFS_NPROCS]) (struct nfsrv_descript *nd,
80 struct nfssvc_sock *slp,
83 extern int nfs_numasync;
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 void nfsd_rt (int sotype, struct nfsrv_descript *nd,
108 static int nfssvc_addsock (struct file *, struct sockaddr *,
110 static int nfssvc_nfsd (struct nfsd_srvargs *,caddr_t,struct thread *);
112 static int nfs_privport = 0;
113 SYSCTL_INT(_vfs_nfs, NFS_NFSPRIVPORT, nfs_privport, CTLFLAG_RW, &nfs_privport, 0, "");
114 SYSCTL_INT(_vfs_nfs, OID_AUTO, gatherdelay, CTLFLAG_RW, &nfsrvw_procrastinate, 0, "");
115 SYSCTL_INT(_vfs_nfs, OID_AUTO, gatherdelay_v3, CTLFLAG_RW, &nfsrvw_procrastinate_v3, 0, "");
116 static int nfs_soreserve = 65535;
117 SYSCTL_INT(_vfs_nfs, OID_AUTO, soreserve, CTLFLAG_RW, &nfs_soreserve, 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 sys_nfssvc(struct nfssvc_args *uap)
137 struct nlookupdata nd;
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;
148 #endif /* NFS_NOSERVER */
150 struct thread *td = curthread;
158 KKASSERT(td->td_proc); /* for ucred and p_fd */
159 while (nfssvc_sockhead_flag & SLP_INIT) {
160 nfssvc_sockhead_flag |= SLP_WANTINIT;
161 (void) tsleep((caddr_t)&nfssvc_sockhead, 0, "nfsd init", 0);
163 if (uap->flag & NFSSVC_BIOD)
164 error = nfssvc_iod(td);
168 #else /* !NFS_NOSERVER */
169 else if (uap->flag & NFSSVC_MNTD) {
170 error = copyin(uap->argp, (caddr_t)&ncd, sizeof (ncd));
174 error = nlookup_init(&nd, ncd.ncd_dirp, UIO_USERSPACE,
177 error = nlookup(&nd);
179 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
184 if ((vp->v_flag & VROOT) == 0)
186 nmp = VFSTONFS(vp->v_mount);
190 if ((nmp->nm_state & NFSSTA_MNTD) &&
191 (uap->flag & NFSSVC_GOTAUTH) == 0)
193 nmp->nm_state |= NFSSTA_MNTD;
194 error = nfs_clientd(nmp, td->td_proc->p_ucred, &ncd, uap->flag,
196 } else if (uap->flag & NFSSVC_ADDSOCK) {
197 error = copyin(uap->argp, (caddr_t)&nfsdarg, sizeof(nfsdarg));
200 error = holdsock(td->td_proc->p_fd, nfsdarg.sock, &fp);
204 * Get the client address for connected sockets.
206 if (nfsdarg.name == NULL || nfsdarg.namelen == 0)
207 nam = (struct sockaddr *)0;
209 error = getsockaddr(&nam, nfsdarg.name,
216 error = nfssvc_addsock(fp, nam, td);
219 error = copyin(uap->argp, (caddr_t)nsd, sizeof (*nsd));
222 if ((uap->flag & NFSSVC_AUTHIN) &&
223 ((nfsd = nsd->nsd_nfsd)) != NULL &&
224 (nfsd->nfsd_slp->ns_flag & SLP_VALID)) {
225 slp = nfsd->nfsd_slp;
228 * First check to see if another nfsd has already
229 * added this credential.
231 for (nuidp = NUIDHASH(slp,nsd->nsd_cr.cr_uid)->lh_first;
232 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
233 if (nuidp->nu_cr.cr_uid == nsd->nsd_cr.cr_uid &&
234 (!nfsd->nfsd_nd->nd_nam2 ||
235 netaddr_match(NU_NETFAM(nuidp),
236 &nuidp->nu_haddr, nfsd->nfsd_nd->nd_nam2)))
240 nfsrv_setcred(&nuidp->nu_cr,&nfsd->nfsd_nd->nd_cr);
241 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
246 if (slp->ns_numuids < nuidhash_max) {
248 nuidp = (struct nfsuid *)
249 kmalloc(sizeof (struct nfsuid), M_NFSUID,
252 nuidp = (struct nfsuid *)0;
253 if ((slp->ns_flag & SLP_VALID) == 0) {
255 kfree((caddr_t)nuidp, M_NFSUID);
257 if (nuidp == (struct nfsuid *)0) {
258 nuidp = TAILQ_FIRST(&slp->ns_uidlruhead);
259 LIST_REMOVE(nuidp, nu_hash);
260 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp,
262 if (nuidp->nu_flag & NU_NAM)
263 FREE(nuidp->nu_nam, M_SONAME);
266 nuidp->nu_cr = nsd->nsd_cr;
267 if (nuidp->nu_cr.cr_ngroups > NGROUPS)
268 nuidp->nu_cr.cr_ngroups = NGROUPS;
269 nuidp->nu_cr.cr_ref = 1;
270 nuidp->nu_timestamp = nsd->nsd_timestamp;
271 nuidp->nu_expire = time_second + nsd->nsd_ttl;
273 * and save the session key in nu_key.
275 bcopy(nsd->nsd_key, nuidp->nu_key,
276 sizeof (nsd->nsd_key));
277 if (nfsd->nfsd_nd->nd_nam2) {
278 struct sockaddr_in *saddr;
280 saddr = (struct sockaddr_in *)
281 nfsd->nfsd_nd->nd_nam2;
282 switch (saddr->sin_family) {
284 nuidp->nu_flag |= NU_INETADDR;
286 saddr->sin_addr.s_addr;
290 nuidp->nu_flag |= NU_NAM;
292 dup_sockaddr(nfsd->nfsd_nd->nd_nam2);
296 TAILQ_INSERT_TAIL(&slp->ns_uidlruhead, nuidp,
298 LIST_INSERT_HEAD(NUIDHASH(slp, nsd->nsd_uid),
300 nfsrv_setcred(&nuidp->nu_cr,
301 &nfsd->nfsd_nd->nd_cr);
302 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
306 if ((uap->flag & NFSSVC_AUTHINFAIL) && (nfsd = nsd->nsd_nfsd))
307 nfsd->nfsd_flag |= NFSD_AUTHFAIL;
308 error = nfssvc_nfsd(nsd, uap->argp, td);
310 #endif /* NFS_NOSERVER */
311 if (error == EINTR || error == ERESTART)
318 * Adds a socket to the list for servicing by nfsds.
321 nfssvc_addsock(struct file *fp, struct sockaddr *mynam, struct thread *td)
324 struct nfssvc_sock *slp;
328 so = (struct socket *)fp->f_data;
330 tslp = (struct nfssvc_sock *)0;
332 * Add it to the list, as required.
334 if (so->so_proto->pr_protocol == IPPROTO_UDP) {
336 if (tslp->ns_flag & SLP_VALID) {
338 FREE(mynam, M_SONAME);
344 * Reserve buffer space in the socket. Note that due to bugs in
345 * Linux's delayed-ack code, serious performance degredation may
346 * occur with linux hosts if the minimum is used.
348 if (so->so_type == SOCK_STREAM)
349 siz = NFS_MAXPACKET + sizeof (u_long);
352 if (siz < nfs_soreserve)
354 if (siz > sb_max_adj) {
355 kprintf("Warning: vfs.nfs.soreserve (%d) "
356 "limited to adjusted sb_max (%ld)\n",
357 nfs_soreserve, sb_max_adj);
361 error = soreserve(so, siz, siz, &td->td_proc->p_rlimit[RLIMIT_SBSIZE]);
364 FREE(mynam, M_SONAME);
369 * Set protocol specific options { for now TCP only } and
370 * reserve some space. For datagram sockets, this can get called
371 * repeatedly for the same socket, but that isn't harmful.
373 if (so->so_type == SOCK_STREAM) {
377 bzero(&sopt, sizeof sopt);
378 sopt.sopt_level = SOL_SOCKET;
379 sopt.sopt_name = SO_KEEPALIVE;
380 sopt.sopt_val = &val;
381 sopt.sopt_valsize = sizeof val;
385 if (so->so_proto->pr_domain->dom_family == AF_INET &&
386 so->so_proto->pr_protocol == IPPROTO_TCP) {
390 bzero(&sopt, sizeof sopt);
391 sopt.sopt_level = IPPROTO_TCP;
392 sopt.sopt_name = TCP_NODELAY;
393 sopt.sopt_val = &val;
394 sopt.sopt_valsize = sizeof val;
398 so->so_rcv.sb_flags &= ~SB_NOINTR;
399 so->so_rcv.sb_timeo = 0;
400 so->so_snd.sb_flags &= ~SB_NOINTR;
401 so->so_snd.sb_timeo = 0;
403 slp = (struct nfssvc_sock *)
404 kmalloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
405 bzero((caddr_t)slp, sizeof (struct nfssvc_sock));
406 STAILQ_INIT(&slp->ns_rec);
407 TAILQ_INIT(&slp->ns_uidlruhead);
408 TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
415 so->so_upcallarg = (caddr_t)slp;
416 so->so_upcall = nfsrv_rcv;
417 so->so_rcv.sb_flags |= SB_UPCALL;
418 slp->ns_flag = (SLP_VALID | SLP_NEEDQ);
419 nfsrv_wakenfsd(slp, 1);
425 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
426 * until it is killed by a signal.
429 nfssvc_nfsd(struct nfsd_srvargs *nsd, caddr_t argp, struct thread *td)
432 struct nfssvc_sock *slp;
433 struct nfsd *nfsd = nsd->nsd_nfsd;
434 struct nfsrv_descript *nd = NULL;
435 struct mbuf *m, *mreq;
436 int error = 0, cacherep, sotype, writes_todo;
444 if (nfsd == (struct nfsd *)0) {
445 nsd->nsd_nfsd = nfsd = (struct nfsd *)
446 kmalloc(sizeof (struct nfsd), M_NFSD, M_WAITOK);
447 bzero((caddr_t)nfsd, sizeof (struct nfsd));
450 TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain);
456 * Loop getting rpc requests until SIGKILL.
459 if ((nfsd->nfsd_flag & NFSD_REQINPROG) == 0) {
460 while (nfsd->nfsd_slp == NULL &&
461 (nfsd_head_flag & NFSD_CHECKSLP) == 0) {
462 nfsd->nfsd_flag |= NFSD_WAITING;
464 error = tsleep((caddr_t)nfsd, PCATCH, "nfsd", 0);
469 if (nfsd->nfsd_slp == NULL &&
470 (nfsd_head_flag & NFSD_CHECKSLP) != 0) {
471 TAILQ_FOREACH(slp, &nfssvc_sockhead, ns_chain) {
472 if ((slp->ns_flag & (SLP_VALID | SLP_DOREC))
473 == (SLP_VALID | SLP_DOREC)) {
474 slp->ns_flag &= ~SLP_DOREC;
476 nfsd->nfsd_slp = slp;
481 nfsd_head_flag &= ~NFSD_CHECKSLP;
483 if ((slp = nfsd->nfsd_slp) == (struct nfssvc_sock *)0)
485 if (slp->ns_flag & SLP_VALID) {
486 if (slp->ns_flag & SLP_DISCONN)
488 else if (slp->ns_flag & SLP_NEEDQ) {
489 slp->ns_flag &= ~SLP_NEEDQ;
490 (void) nfs_slplock(slp, 1);
491 nfsrv_rcv(slp->ns_so, (caddr_t)slp,
495 error = nfsrv_dorec(slp, nfsd, &nd);
496 cur_usec = nfs_curusec();
497 if (error && slp->ns_tq.lh_first &&
498 slp->ns_tq.lh_first->nd_time <= cur_usec) {
504 nfsd->nfsd_flag |= NFSD_REQINPROG;
508 slp = nfsd->nfsd_slp;
510 if (error || (slp->ns_flag & SLP_VALID) == 0) {
512 kfree((caddr_t)nd, M_NFSRVDESC);
515 nfsd->nfsd_slp = (struct nfssvc_sock *)0;
516 nfsd->nfsd_flag &= ~NFSD_REQINPROG;
521 sotype = slp->ns_so->so_type;
523 getmicrotime(&nd->nd_starttime);
525 nd->nd_nam = nd->nd_nam2;
527 nd->nd_nam = slp->ns_nam;
530 * Check to see if authorization is needed.
532 if (nfsd->nfsd_flag & NFSD_NEEDAUTH) {
533 nfsd->nfsd_flag &= ~NFSD_NEEDAUTH;
535 ((struct sockaddr_in *)
536 nd->nd_nam)->sin_addr.s_addr;
537 nsd->nsd_authlen = nfsd->nfsd_authlen;
538 nsd->nsd_verflen = nfsd->nfsd_verflen;
539 if (!copyout(nfsd->nfsd_authstr,nsd->nsd_authstr,
540 nfsd->nfsd_authlen) &&
541 !copyout(nfsd->nfsd_verfstr, nsd->nsd_verfstr,
542 nfsd->nfsd_verflen) &&
543 !copyout((caddr_t)nsd, argp, sizeof (*nsd)))
545 cacherep = RC_DROPIT;
547 cacherep = nfsrv_getcache(nd, slp, &mreq);
550 if (nfsd->nfsd_flag & NFSD_AUTHFAIL) {
551 nfsd->nfsd_flag &= ~NFSD_AUTHFAIL;
552 nd->nd_procnum = NFSPROC_NOOP;
553 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
555 } else if (nfs_privport) {
556 /* Check if source port is privileged */
558 struct sockaddr *nam = nd->nd_nam;
559 struct sockaddr_in *sin;
561 sin = (struct sockaddr_in *)nam;
562 port = ntohs(sin->sin_port);
563 if (port >= IPPORT_RESERVED &&
564 nd->nd_procnum != NFSPROC_NULL) {
565 nd->nd_procnum = NFSPROC_NOOP;
566 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
568 kprintf("NFS request from unprivileged port (%s:%d)\n",
569 inet_ntoa(sin->sin_addr), port);
576 * Loop to get all the write rpc relies that have been
582 if (nd && (nd->nd_flag & ND_NFSV3))
583 procrastinate = nfsrvw_procrastinate_v3;
585 procrastinate = nfsrvw_procrastinate;
586 if (writes_todo || (nd->nd_procnum == NFSPROC_WRITE &&
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);
597 if (error != 0 && error != NFSERR_RETVOID) {
598 if (nd->nd_procnum != NQNFSPROC_VACATED)
600 nfsrv_updatecache(nd, FALSE, mreq);
602 FREE(nd->nd_nam2, M_SONAME);
605 nfsstats.srvrpccnt[nd->nd_procnum]++;
606 nfsrv_updatecache(nd, TRUE, mreq);
607 nd->nd_mrep = (struct mbuf *)0;
615 if (siz <= 0 || siz > NFS_MAXPACKET) {
616 kprintf("mbuf siz=%d\n",siz);
617 panic("Bad nfs svc reply");
620 m->m_pkthdr.len = siz;
621 m->m_pkthdr.rcvif = (struct ifnet *)0;
623 * For stream protocols, prepend a Sun RPC
626 if (sotype == SOCK_STREAM) {
627 M_PREPEND(m, NFSX_UNSIGNED, MB_WAIT);
630 *mtod(m, u_int32_t *) = htonl(0x80000000 | siz);
632 if (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED)
633 (void) nfs_slplock(slp, 1);
634 if (slp->ns_flag & SLP_VALID)
635 error = nfs_send(slp->ns_so, nd->nd_nam2, m, NULL);
641 nfsd_rt(sotype, nd, cacherep);
643 FREE(nd->nd_nam2, M_SONAME);
645 m_freem(nd->nd_mrep);
648 if (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED)
650 if (error == EINTR || error == ERESTART) {
651 kfree((caddr_t)nd, M_NFSRVDESC);
659 nfsd_rt(sotype, nd, cacherep);
660 m_freem(nd->nd_mrep);
662 FREE(nd->nd_nam2, M_SONAME);
666 FREE((caddr_t)nd, M_NFSRVDESC);
671 * Check to see if there are outstanding writes that
672 * need to be serviced.
674 cur_usec = nfs_curusec();
676 if (slp->ns_tq.lh_first &&
677 slp->ns_tq.lh_first->nd_time <= cur_usec) {
683 } while (writes_todo);
685 if (nfsrv_dorec(slp, nfsd, &nd)) {
686 nfsd->nfsd_flag &= ~NFSD_REQINPROG;
687 nfsd->nfsd_slp = NULL;
692 TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
694 kfree((caddr_t)nfsd, M_NFSD);
695 nsd->nsd_nfsd = (struct nfsd *)0;
696 if (--nfs_numnfsd == 0)
697 nfsrv_init(TRUE); /* Reinitialize everything */
702 * Shut down a socket associated with an nfssvc_sock structure.
703 * Should be called with the send lock set, if required.
704 * The trick here is to increment the sref at the start, so that the nfsds
705 * will stop using it and clear ns_flag at the end so that it will not be
706 * reassigned during cleanup.
709 nfsrv_zapsock(struct nfssvc_sock *slp)
711 struct nfsuid *nuidp, *nnuidp;
712 struct nfsrv_descript *nwp, *nnwp;
715 struct nfsrv_rec *rec;
717 slp->ns_flag &= ~SLP_ALLFLAGS;
720 slp->ns_fp = (struct file *)0;
722 so->so_rcv.sb_flags &= ~SB_UPCALL;
723 so->so_upcall = NULL;
724 so->so_upcallarg = NULL;
728 FREE(slp->ns_nam, M_SONAME);
729 m_freem(slp->ns_raw);
730 while ((rec = STAILQ_FIRST(&slp->ns_rec)) != NULL) {
732 STAILQ_REMOVE_HEAD(&slp->ns_rec, nr_link);
734 FREE(rec->nr_address, M_SONAME);
735 m_freem(rec->nr_packet);
736 kfree(rec, M_NFSRVDESC);
738 TAILQ_FOREACH_MUTABLE(nuidp, &slp->ns_uidlruhead, nu_lru,
740 LIST_REMOVE(nuidp, nu_hash);
741 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru);
742 if (nuidp->nu_flag & NU_NAM)
743 FREE(nuidp->nu_nam, M_SONAME);
744 kfree((caddr_t)nuidp, M_NFSUID);
747 for (nwp = slp->ns_tq.lh_first; nwp; nwp = nnwp) {
748 nnwp = nwp->nd_tq.le_next;
749 LIST_REMOVE(nwp, nd_tq);
750 kfree((caddr_t)nwp, M_NFSRVDESC);
752 LIST_INIT(&slp->ns_tq);
758 * Derefence a server socket structure. If it has no more references and
759 * is no longer valid, you can throw it away.
762 nfsrv_slpderef(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 kfree((caddr_t)slp, M_NFSSVC);
771 * Lock a socket against others.
774 nfs_slplock(struct nfssvc_sock *slp, int wait)
776 int *statep = &slp->ns_solock;
778 if (!wait && (*statep & NFSSTA_SNDLOCK))
779 return(0); /* already locked, fail */
780 while (*statep & NFSSTA_SNDLOCK) {
781 *statep |= NFSSTA_WANTSND;
782 (void) tsleep((caddr_t)statep, 0, "nfsslplck", 0);
784 *statep |= NFSSTA_SNDLOCK;
789 * Unlock the stream socket for others.
792 nfs_slpunlock(struct nfssvc_sock *slp)
794 int *statep = &slp->ns_solock;
796 if ((*statep & NFSSTA_SNDLOCK) == 0)
797 panic("nfs slpunlock");
798 *statep &= ~NFSSTA_SNDLOCK;
799 if (*statep & NFSSTA_WANTSND) {
800 *statep &= ~NFSSTA_WANTSND;
801 wakeup((caddr_t)statep);
806 * Initialize the data structures for the server.
807 * Handshake with any new nfsds starting up to avoid any chance of
811 nfsrv_init(int terminating)
813 struct nfssvc_sock *slp, *nslp;
815 if (nfssvc_sockhead_flag & SLP_INIT)
817 nfssvc_sockhead_flag |= SLP_INIT;
819 TAILQ_FOREACH_MUTABLE(slp, &nfssvc_sockhead, ns_chain, nslp) {
820 if (slp->ns_flag & SLP_VALID)
822 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
823 kfree((caddr_t)slp, M_NFSSVC);
825 nfsrv_cleancache(); /* And clear out server cache */
827 nfs_pub.np_valid = 0;
829 TAILQ_INIT(&nfssvc_sockhead);
830 nfssvc_sockhead_flag &= ~SLP_INIT;
831 if (nfssvc_sockhead_flag & SLP_WANTINIT) {
832 nfssvc_sockhead_flag &= ~SLP_WANTINIT;
833 wakeup((caddr_t)&nfssvc_sockhead);
836 TAILQ_INIT(&nfsd_head);
837 nfsd_head_flag &= ~NFSD_CHECKSLP;
840 nfs_udpsock = (struct nfssvc_sock *)
841 kmalloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
842 bzero((caddr_t)nfs_udpsock, sizeof (struct nfssvc_sock));
843 STAILQ_INIT(&nfs_udpsock->ns_rec);
844 TAILQ_INIT(&nfs_udpsock->ns_uidlruhead);
845 TAILQ_INSERT_HEAD(&nfssvc_sockhead, nfs_udpsock, ns_chain);
847 nfs_cltpsock = (struct nfssvc_sock *)
848 kmalloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
849 bzero((caddr_t)nfs_cltpsock, sizeof (struct nfssvc_sock));
850 STAILQ_INIT(&nfs_cltpsock->ns_rec);
851 TAILQ_INIT(&nfs_cltpsock->ns_uidlruhead);
852 TAILQ_INSERT_TAIL(&nfssvc_sockhead, nfs_cltpsock, ns_chain);
857 * Add entries to the server monitor log.
860 nfsd_rt(int sotype, struct nfsrv_descript *nd, int cacherep)
864 rt = &nfsdrt.drt[nfsdrt.pos];
865 if (cacherep == RC_DOIT)
867 else if (cacherep == RC_REPLY)
868 rt->flag = DRT_CACHEREPLY;
870 rt->flag = DRT_CACHEDROP;
871 if (sotype == SOCK_STREAM)
873 if (nd->nd_flag & ND_NFSV3)
874 rt->flag |= DRT_NFSV3;
875 rt->proc = nd->nd_procnum;
876 if (nd->nd_nam->sa_family == AF_INET)
877 rt->ipadr = ((struct sockaddr_in *)nd->nd_nam)->sin_addr.s_addr;
879 rt->ipadr = INADDR_ANY;
880 rt->resptime = nfs_curusec() - (nd->nd_starttime.tv_sec * 1000000 + nd->nd_starttime.tv_usec);
881 getmicrotime(&rt->tstamp);
882 nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ;
884 #endif /* NFS_NOSERVER */
886 static int nfs_defect = 0;
887 SYSCTL_INT(_vfs_nfs, OID_AUTO, defect, CTLFLAG_RW, &nfs_defect, 0, "");
890 * Asynchronous I/O daemons for client nfs.
891 * They do read-ahead and write-behind operations on the block I/O cache.
892 * Never returns unless it fails or gets killed.
895 nfssvc_iod(struct thread *td)
899 struct nfsmount *nmp;
903 * Assign my position or return error if too many already running
906 for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
907 if (nfs_asyncdaemon[i] == 0) {
908 nfs_asyncdaemon[i]++;
916 * Just loop around doin our stuff until SIGKILL
919 while (((nmp = nfs_iodmount[myiod]) == NULL
920 || TAILQ_EMPTY(&nmp->nm_bioq))
924 nfs_iodwant[myiod] = td;
925 nfs_iodmount[myiod] = NULL;
926 error = tsleep((caddr_t)&nfs_iodwant[myiod],
927 PCATCH, "nfsidl", 0);
930 nfs_asyncdaemon[myiod] = 0;
933 nfs_iodwant[myiod] = NULL;
934 nfs_iodmount[myiod] = NULL;
938 while ((bio = TAILQ_FIRST(&nmp->nm_bioq)) != NULL) {
940 * Take one off the front of the list. The BIO's
941 * block number is normalized for DEV_BSIZE.
943 TAILQ_REMOVE(&nmp->nm_bioq, bio, bio_act);
945 if (nmp->nm_bioqwant && nmp->nm_bioqlen <= nfs_numasync) {
946 nmp->nm_bioqwant = FALSE;
947 wakeup(&nmp->nm_bioq);
949 nfs_doio((struct vnode *)bio->bio_driver_info, bio, NULL);
951 * If there are more than one iod on this mount, then defect
952 * so that the iods can be shared out fairly between the mounts
954 if (nfs_defect && nmp->nm_bioqiods > 1) {
956 ("nfssvc_iod: iod %d defecting from mount %p\n",
958 nfs_iodmount[myiod] = NULL;
968 * Get an authorization string for the uid by having the mount_nfs sitting
969 * on this mount point porpous out of the kernel and do it.
972 nfs_getauth(struct nfsmount *nmp, struct nfsreq *rep,
973 struct ucred *cred, char **auth_str, int *auth_len, char *verf_str,
974 int *verf_len, NFSKERBKEY_T key /* return session key */)
978 while ((nmp->nm_state & NFSSTA_WAITAUTH) == 0) {
979 nmp->nm_state |= NFSSTA_WANTAUTH;
980 (void) tsleep((caddr_t)&nmp->nm_authtype, 0,
982 error = nfs_sigintr(nmp, rep, rep->r_td);
984 nmp->nm_state &= ~NFSSTA_WANTAUTH;
988 nmp->nm_state &= ~(NFSSTA_WAITAUTH | NFSSTA_WANTAUTH);
989 nmp->nm_authstr = *auth_str = (char *)kmalloc(RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK);
990 nmp->nm_authlen = RPCAUTH_MAXSIZ;
991 nmp->nm_verfstr = verf_str;
992 nmp->nm_verflen = *verf_len;
993 nmp->nm_authuid = cred->cr_uid;
994 wakeup((caddr_t)&nmp->nm_authstr);
997 * And wait for mount_nfs to do its stuff.
999 while ((nmp->nm_state & NFSSTA_HASAUTH) == 0 && error == 0) {
1000 (void) tsleep((caddr_t)&nmp->nm_authlen, 0,
1001 "nfsauth2", 2 * hz);
1002 error = nfs_sigintr(nmp, rep, rep->r_td);
1004 if (nmp->nm_state & NFSSTA_AUTHERR) {
1005 nmp->nm_state &= ~NFSSTA_AUTHERR;
1009 kfree((caddr_t)*auth_str, M_TEMP);
1011 *auth_len = nmp->nm_authlen;
1012 *verf_len = nmp->nm_verflen;
1013 bcopy((caddr_t)nmp->nm_key, (caddr_t)key, sizeof (key));
1015 nmp->nm_state &= ~NFSSTA_HASAUTH;
1016 nmp->nm_state |= NFSSTA_WAITAUTH;
1017 if (nmp->nm_state & NFSSTA_WANTAUTH) {
1018 nmp->nm_state &= ~NFSSTA_WANTAUTH;
1019 wakeup((caddr_t)&nmp->nm_authtype);
1025 * Get a nickname authenticator and verifier.
1028 nfs_getnickauth(struct nfsmount *nmp, struct ucred *cred, char **auth_str,
1029 int *auth_len, char *verf_str, int verf_len)
1031 struct nfsuid *nuidp;
1032 u_int32_t *nickp, *verfp;
1033 struct timeval ktvin, ktvout;
1036 if (verf_len < (4 * NFSX_UNSIGNED))
1037 panic("nfs_getnickauth verf too small");
1039 for (nuidp = NMUIDHASH(nmp, cred->cr_uid)->lh_first;
1040 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
1041 if (nuidp->nu_cr.cr_uid == cred->cr_uid)
1044 if (!nuidp || nuidp->nu_expire < time_second)
1048 * Move to the end of the lru list (end of lru == most recently used).
1050 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
1051 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, nu_lru);
1053 nickp = (u_int32_t *)kmalloc(2 * NFSX_UNSIGNED, M_TEMP, M_WAITOK);
1054 *nickp++ = txdr_unsigned(RPCAKN_NICKNAME);
1055 *nickp = txdr_unsigned(nuidp->nu_nickname);
1056 *auth_str = (char *)nickp;
1057 *auth_len = 2 * NFSX_UNSIGNED;
1060 * Now we must encrypt the verifier and package it up.
1062 verfp = (u_int32_t *)verf_str;
1063 *verfp++ = txdr_unsigned(RPCAKN_NICKNAME);
1064 if (time_second > nuidp->nu_timestamp.tv_sec ||
1065 (time_second == nuidp->nu_timestamp.tv_sec &&
1066 time_second > nuidp->nu_timestamp.tv_usec))
1067 getmicrotime(&nuidp->nu_timestamp);
1069 nuidp->nu_timestamp.tv_usec++;
1070 ktvin.tv_sec = txdr_unsigned(nuidp->nu_timestamp.tv_sec);
1071 ktvin.tv_usec = txdr_unsigned(nuidp->nu_timestamp.tv_usec);
1074 * Now encrypt the timestamp verifier in ecb mode using the session
1081 *verfp++ = ktvout.tv_sec;
1082 *verfp++ = ktvout.tv_usec;
1088 * Save the current nickname in a hash list entry on the mount point.
1091 nfs_savenickauth(struct nfsmount *nmp, struct ucred *cred, int len,
1092 NFSKERBKEY_T key, struct mbuf **mdp, char **dposp,
1095 struct nfsuid *nuidp;
1098 struct mbuf *md = *mdp;
1099 struct timeval ktvin, ktvout;
1101 char *dpos = *dposp, *cp2;
1102 int deltasec, error = 0;
1104 if (len == (3 * NFSX_UNSIGNED)) {
1105 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1106 ktvin.tv_sec = *tl++;
1107 ktvin.tv_usec = *tl++;
1108 nick = fxdr_unsigned(u_int32_t, *tl);
1111 * Decrypt the timestamp in ecb mode.
1116 ktvout.tv_sec = fxdr_unsigned(long, ktvout.tv_sec);
1117 ktvout.tv_usec = fxdr_unsigned(long, ktvout.tv_usec);
1118 deltasec = time_second - ktvout.tv_sec;
1120 deltasec = -deltasec;
1122 * If ok, add it to the hash list for the mount point.
1124 if (deltasec <= NFS_KERBCLOCKSKEW) {
1125 if (nmp->nm_numuids < nuidhash_max) {
1127 nuidp = (struct nfsuid *)
1128 kmalloc(sizeof (struct nfsuid), M_NFSUID,
1131 nuidp = TAILQ_FIRST(&nmp->nm_uidlruhead);
1132 LIST_REMOVE(nuidp, nu_hash);
1133 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp,
1137 nuidp->nu_cr.cr_uid = cred->cr_uid;
1138 nuidp->nu_expire = time_second + NFS_KERBTTL;
1139 nuidp->nu_timestamp = ktvout;
1140 nuidp->nu_nickname = nick;
1141 bcopy(key, nuidp->nu_key, sizeof (key));
1142 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp,
1144 LIST_INSERT_HEAD(NMUIDHASH(nmp, cred->cr_uid),
1148 nfsm_adv(nfsm_rndup(len));