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 $
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>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
55 #include <sys/domain.h>
56 #include <sys/protosw.h>
57 #include <sys/namei.h>
58 #include <vm/vm_zone.h>
60 #include <netinet/in.h>
61 #include <netinet/tcp.h>
62 #include <nfs/xdr_subs.h>
63 #include <nfs/rpcv2.h>
64 #include <nfs/nfsproto.h>
66 #include <nfs/nfsm_subs.h>
67 #include <nfs/nfsrvcache.h>
68 #include <nfs/nfsmount.h>
69 #include <nfs/nfsnode.h>
70 #include <nfs/nqnfs.h>
71 #include <nfs/nfsrtt.h>
73 static MALLOC_DEFINE(M_NFSSVC, "NFS srvsock", "Nfs server structure");
76 extern int32_t (*nfsrv3_procs[NFS_NPROCS]) __P((struct nfsrv_descript *nd,
77 struct nfssvc_sock *slp,
79 struct mbuf **mreqp));
80 extern int nfs_numasync;
81 extern time_t nqnfsstarttime;
82 extern int nqsrv_writeslack;
84 extern struct nfsstats nfsstats;
85 extern int nfsrvw_procrastinate;
86 extern int nfsrvw_procrastinate_v3;
87 static int nuidhash_max = NFS_MAXUIDHASH;
90 static void nfsrv_zapsock __P((struct nfssvc_sock *slp));
92 static int nfssvc_iod __P((struct proc *));
97 static int nfs_asyncdaemon[NFS_MAXASYNCDAEMON];
99 SYSCTL_DECL(_vfs_nfs);
102 int nfsd_waiting = 0;
103 static struct nfsdrt nfsdrt;
104 static int nfs_numnfsd = 0;
105 static int notstarted = 1;
106 static int modify_flag = 0;
107 static void nfsd_rt __P((int sotype, struct nfsrv_descript *nd,
109 static int nfssvc_addsock __P((struct file *, struct sockaddr *,
111 static int nfssvc_nfsd __P((struct nfsd_srvargs *,caddr_t,struct proc *));
113 static int nfs_privport = 0;
114 SYSCTL_INT(_vfs_nfs, NFS_NFSPRIVPORT, nfs_privport, CTLFLAG_RW, &nfs_privport, 0, "");
115 SYSCTL_INT(_vfs_nfs, OID_AUTO, gatherdelay, CTLFLAG_RW, &nfsrvw_procrastinate, 0, "");
116 SYSCTL_INT(_vfs_nfs, OID_AUTO, gatherdelay_v3, CTLFLAG_RW, &nfsrvw_procrastinate_v3, 0, "");
119 * NFS server system calls
122 #endif /* NFS_NOSERVER */
124 * Nfs server psuedo system call for the nfsd's
125 * Based on the flag value it either:
126 * - adds a socket to the selection list
127 * - remains in the kernel as an nfsd
128 * - remains in the kernel as an nfsiod
130 #ifndef _SYS_SYSPROTO_H_
139 register struct nfssvc_args *uap;
144 struct sockaddr *nam;
145 struct nfsd_args nfsdarg;
146 struct nfsd_srvargs nfsd_srvargs, *nsd = &nfsd_srvargs;
147 struct nfsd_cargs ncd;
149 struct nfssvc_sock *slp;
150 struct nfsuid *nuidp;
151 struct nfsmount *nmp;
152 #endif /* NFS_NOSERVER */
161 while (nfssvc_sockhead_flag & SLP_INIT) {
162 nfssvc_sockhead_flag |= SLP_WANTINIT;
163 (void) tsleep((caddr_t)&nfssvc_sockhead, PSOCK, "nfsd init", 0);
165 if (uap->flag & NFSSVC_BIOD)
166 error = nfssvc_iod(p);
170 #else /* !NFS_NOSERVER */
171 else if (uap->flag & NFSSVC_MNTD) {
172 error = copyin(uap->argp, (caddr_t)&ncd, sizeof (ncd));
175 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
180 NDFREE(&nd, NDF_ONLY_PNBUF);
181 if ((nd.ni_vp->v_flag & VROOT) == 0)
183 nmp = VFSTONFS(nd.ni_vp->v_mount);
187 if ((nmp->nm_state & NFSSTA_MNTD) &&
188 (uap->flag & NFSSVC_GOTAUTH) == 0)
190 nmp->nm_state |= NFSSTA_MNTD;
191 error = nqnfs_clientd(nmp, p->p_ucred, &ncd, uap->flag,
193 } else if (uap->flag & NFSSVC_ADDSOCK) {
194 error = copyin(uap->argp, (caddr_t)&nfsdarg, sizeof(nfsdarg));
197 error = holdsock(p->p_fd, nfsdarg.sock, &fp);
201 * Get the client address for connected sockets.
203 if (nfsdarg.name == NULL || nfsdarg.namelen == 0)
204 nam = (struct sockaddr *)0;
206 error = getsockaddr(&nam, nfsdarg.name,
213 error = nfssvc_addsock(fp, nam, p);
216 error = copyin(uap->argp, (caddr_t)nsd, sizeof (*nsd));
219 if ((uap->flag & NFSSVC_AUTHIN) &&
220 ((nfsd = nsd->nsd_nfsd)) != NULL &&
221 (nfsd->nfsd_slp->ns_flag & SLP_VALID)) {
222 slp = nfsd->nfsd_slp;
225 * First check to see if another nfsd has already
226 * added this credential.
228 for (nuidp = NUIDHASH(slp,nsd->nsd_cr.cr_uid)->lh_first;
229 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
230 if (nuidp->nu_cr.cr_uid == nsd->nsd_cr.cr_uid &&
231 (!nfsd->nfsd_nd->nd_nam2 ||
232 netaddr_match(NU_NETFAM(nuidp),
233 &nuidp->nu_haddr, nfsd->nfsd_nd->nd_nam2)))
237 nfsrv_setcred(&nuidp->nu_cr,&nfsd->nfsd_nd->nd_cr);
238 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
243 if (slp->ns_numuids < nuidhash_max) {
245 nuidp = (struct nfsuid *)
246 malloc(sizeof (struct nfsuid), M_NFSUID,
249 nuidp = (struct nfsuid *)0;
250 if ((slp->ns_flag & SLP_VALID) == 0) {
252 free((caddr_t)nuidp, M_NFSUID);
254 if (nuidp == (struct nfsuid *)0) {
255 nuidp = slp->ns_uidlruhead.tqh_first;
256 LIST_REMOVE(nuidp, nu_hash);
257 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp,
259 if (nuidp->nu_flag & NU_NAM)
260 FREE(nuidp->nu_nam, M_SONAME);
263 nuidp->nu_cr = nsd->nsd_cr;
264 if (nuidp->nu_cr.cr_ngroups > NGROUPS)
265 nuidp->nu_cr.cr_ngroups = NGROUPS;
266 nuidp->nu_cr.cr_ref = 1;
267 nuidp->nu_timestamp = nsd->nsd_timestamp;
268 nuidp->nu_expire = time_second + nsd->nsd_ttl;
270 * and save the session key in nu_key.
272 bcopy(nsd->nsd_key, nuidp->nu_key,
273 sizeof (nsd->nsd_key));
274 if (nfsd->nfsd_nd->nd_nam2) {
275 struct sockaddr_in *saddr;
277 saddr = (struct sockaddr_in *)
278 nfsd->nfsd_nd->nd_nam2;
279 switch (saddr->sin_family) {
281 nuidp->nu_flag |= NU_INETADDR;
283 saddr->sin_addr.s_addr;
287 nuidp->nu_flag |= NU_NAM;
289 dup_sockaddr(nfsd->nfsd_nd->
294 TAILQ_INSERT_TAIL(&slp->ns_uidlruhead, nuidp,
296 LIST_INSERT_HEAD(NUIDHASH(slp, nsd->nsd_uid),
298 nfsrv_setcred(&nuidp->nu_cr,
299 &nfsd->nfsd_nd->nd_cr);
300 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
304 if ((uap->flag & NFSSVC_AUTHINFAIL) && (nfsd = nsd->nsd_nfsd))
305 nfsd->nfsd_flag |= NFSD_AUTHFAIL;
306 error = nfssvc_nfsd(nsd, uap->argp, p);
308 #endif /* NFS_NOSERVER */
309 if (error == EINTR || error == ERESTART)
316 * Adds a socket to the list for servicing by nfsds.
319 nfssvc_addsock(fp, mynam, p)
321 struct sockaddr *mynam;
325 register struct nfssvc_sock *slp;
326 register struct socket *so;
329 so = (struct socket *)fp->f_data;
331 tslp = (struct nfssvc_sock *)0;
333 * Add it to the list, as required.
335 if (so->so_proto->pr_protocol == IPPROTO_UDP) {
337 if (tslp->ns_flag & SLP_VALID) {
339 FREE(mynam, M_SONAME);
344 if (so->so_type == SOCK_STREAM)
345 siz = NFS_MAXPACKET + sizeof (u_long);
348 error = soreserve(so, siz, siz);
351 FREE(mynam, M_SONAME);
356 * Set protocol specific options { for now TCP only } and
357 * reserve some space. For datagram sockets, this can get called
358 * repeatedly for the same socket, but that isn't harmful.
360 if (so->so_type == SOCK_STREAM) {
364 bzero(&sopt, sizeof sopt);
365 sopt.sopt_level = SOL_SOCKET;
366 sopt.sopt_name = SO_KEEPALIVE;
367 sopt.sopt_val = &val;
368 sopt.sopt_valsize = sizeof val;
372 if (so->so_proto->pr_domain->dom_family == AF_INET &&
373 so->so_proto->pr_protocol == IPPROTO_TCP) {
377 bzero(&sopt, sizeof sopt);
378 sopt.sopt_level = IPPROTO_TCP;
379 sopt.sopt_name = TCP_NODELAY;
380 sopt.sopt_val = &val;
381 sopt.sopt_valsize = sizeof val;
385 so->so_rcv.sb_flags &= ~SB_NOINTR;
386 so->so_rcv.sb_timeo = 0;
387 so->so_snd.sb_flags &= ~SB_NOINTR;
388 so->so_snd.sb_timeo = 0;
390 slp = (struct nfssvc_sock *)
391 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
392 bzero((caddr_t)slp, sizeof (struct nfssvc_sock));
393 STAILQ_INIT(&slp->ns_rec);
394 TAILQ_INIT(&slp->ns_uidlruhead);
395 TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
402 so->so_upcallarg = (caddr_t)slp;
403 so->so_upcall = nfsrv_rcv;
404 so->so_rcv.sb_flags |= SB_UPCALL;
405 slp->ns_flag = (SLP_VALID | SLP_NEEDQ);
412 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
413 * until it is killed by a signal.
416 nfssvc_nfsd(nsd, argp, p)
417 struct nfsd_srvargs *nsd;
422 register struct nfssvc_sock *slp;
423 struct nfsd *nfsd = nsd->nsd_nfsd;
424 struct nfsrv_descript *nd = NULL;
425 struct mbuf *m, *mreq;
426 int error = 0, cacherep, s, sotype, writes_todo;
434 if (nfsd == (struct nfsd *)0) {
435 nsd->nsd_nfsd = nfsd = (struct nfsd *)
436 malloc(sizeof (struct nfsd), M_NFSD, M_WAITOK);
437 bzero((caddr_t)nfsd, sizeof (struct nfsd));
439 nfsd->nfsd_procp = p;
440 TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain);
446 * Loop getting rpc requests until SIGKILL.
449 if ((nfsd->nfsd_flag & NFSD_REQINPROG) == 0) {
450 while (nfsd->nfsd_slp == (struct nfssvc_sock *)0 &&
451 (nfsd_head_flag & NFSD_CHECKSLP) == 0) {
452 nfsd->nfsd_flag |= NFSD_WAITING;
454 error = tsleep((caddr_t)nfsd, PSOCK | PCATCH,
460 if (nfsd->nfsd_slp == (struct nfssvc_sock *)0 &&
461 (nfsd_head_flag & NFSD_CHECKSLP) != 0) {
462 for (slp = nfssvc_sockhead.tqh_first; slp != 0;
463 slp = slp->ns_chain.tqe_next) {
464 if ((slp->ns_flag & (SLP_VALID | SLP_DOREC))
465 == (SLP_VALID | SLP_DOREC)) {
466 slp->ns_flag &= ~SLP_DOREC;
468 nfsd->nfsd_slp = slp;
473 nfsd_head_flag &= ~NFSD_CHECKSLP;
475 if ((slp = nfsd->nfsd_slp) == (struct nfssvc_sock *)0)
477 if (slp->ns_flag & SLP_VALID) {
478 if (slp->ns_flag & SLP_DISCONN)
480 else if (slp->ns_flag & SLP_NEEDQ) {
481 slp->ns_flag &= ~SLP_NEEDQ;
482 (void) nfs_slplock(slp, 1);
483 nfsrv_rcv(slp->ns_so, (caddr_t)slp,
487 error = nfsrv_dorec(slp, nfsd, &nd);
488 cur_usec = nfs_curusec();
489 if (error && slp->ns_tq.lh_first &&
490 slp->ns_tq.lh_first->nd_time <= cur_usec) {
496 nfsd->nfsd_flag |= NFSD_REQINPROG;
500 slp = nfsd->nfsd_slp;
502 if (error || (slp->ns_flag & SLP_VALID) == 0) {
504 free((caddr_t)nd, M_NFSRVDESC);
507 nfsd->nfsd_slp = (struct nfssvc_sock *)0;
508 nfsd->nfsd_flag &= ~NFSD_REQINPROG;
513 sotype = slp->ns_so->so_type;
515 getmicrotime(&nd->nd_starttime);
517 nd->nd_nam = nd->nd_nam2;
519 nd->nd_nam = slp->ns_nam;
522 * Check to see if authorization is needed.
524 if (nfsd->nfsd_flag & NFSD_NEEDAUTH) {
525 nfsd->nfsd_flag &= ~NFSD_NEEDAUTH;
527 ((struct sockaddr_in *)
528 nd->nd_nam)->sin_addr.s_addr;
529 nsd->nsd_authlen = nfsd->nfsd_authlen;
530 nsd->nsd_verflen = nfsd->nfsd_verflen;
531 if (!copyout(nfsd->nfsd_authstr,nsd->nsd_authstr,
532 nfsd->nfsd_authlen) &&
533 !copyout(nfsd->nfsd_verfstr, nsd->nsd_verfstr,
534 nfsd->nfsd_verflen) &&
535 !copyout((caddr_t)nsd, argp, sizeof (*nsd)))
537 cacherep = RC_DROPIT;
539 cacherep = nfsrv_getcache(nd, slp, &mreq);
542 * Check for just starting up for NQNFS and send
543 * fake "try again later" replies to the NQNFS clients.
545 if (notstarted && nqnfsstarttime <= time_second) {
547 nqnfsstarttime = time_second + nqsrv_writeslack;
553 if ((nd->nd_flag & ND_NQNFS) == 0)
554 cacherep = RC_DROPIT;
555 else if (nd->nd_procnum != NFSPROC_WRITE) {
556 nd->nd_procnum = NFSPROC_NOOP;
557 nd->nd_repstat = NQNFS_TRYLATER;
561 } else if (nfsd->nfsd_flag & NFSD_AUTHFAIL) {
562 nfsd->nfsd_flag &= ~NFSD_AUTHFAIL;
563 nd->nd_procnum = NFSPROC_NOOP;
564 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
566 } else if (nfs_privport) {
567 /* Check if source port is privileged */
569 struct sockaddr *nam = nd->nd_nam;
570 struct sockaddr_in *sin;
572 sin = (struct sockaddr_in *)nam;
573 port = ntohs(sin->sin_port);
574 if (port >= IPPORT_RESERVED &&
575 nd->nd_procnum != NFSPROC_NULL) {
576 nd->nd_procnum = NFSPROC_NOOP;
577 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
579 printf("NFS request from unprivileged port (%s:%d)\n",
580 inet_ntoa(sin->sin_addr), port);
587 * Loop to get all the write rpc relies that have been
593 if (nd && (nd->nd_flag & ND_NFSV3))
594 procrastinate = nfsrvw_procrastinate_v3;
596 procrastinate = nfsrvw_procrastinate;
597 if (writes_todo || (nd->nd_procnum == NFSPROC_WRITE &&
598 procrastinate > 0 && !notstarted))
599 error = nfsrv_writegather(&nd, slp,
600 nfsd->nfsd_procp, &mreq);
602 error = (*(nfsrv3_procs[nd->nd_procnum]))(nd,
603 slp, nfsd->nfsd_procp, &mreq);
606 if (error != 0 && error != NFSERR_RETVOID) {
607 if (nd->nd_procnum != NQNFSPROC_VACATED)
609 nfsrv_updatecache(nd, FALSE, mreq);
611 FREE(nd->nd_nam2, M_SONAME);
614 nfsstats.srvrpccnt[nd->nd_procnum]++;
615 nfsrv_updatecache(nd, TRUE, mreq);
616 nd->nd_mrep = (struct mbuf *)0;
624 if (siz <= 0 || siz > NFS_MAXPACKET) {
625 printf("mbuf siz=%d\n",siz);
626 panic("Bad nfs svc reply");
629 m->m_pkthdr.len = siz;
630 m->m_pkthdr.rcvif = (struct ifnet *)0;
632 * For stream protocols, prepend a Sun RPC
635 if (sotype == SOCK_STREAM) {
636 M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
637 *mtod(m, u_int32_t *) = htonl(0x80000000 | siz);
639 if (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED)
640 (void) nfs_slplock(slp, 1);
641 if (slp->ns_flag & SLP_VALID)
642 error = nfs_send(slp->ns_so, nd->nd_nam2, m, NULL);
648 nfsd_rt(sotype, nd, cacherep);
650 FREE(nd->nd_nam2, M_SONAME);
652 m_freem(nd->nd_mrep);
655 if (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED)
657 if (error == EINTR || error == ERESTART) {
658 free((caddr_t)nd, M_NFSRVDESC);
666 nfsd_rt(sotype, nd, cacherep);
667 m_freem(nd->nd_mrep);
669 FREE(nd->nd_nam2, M_SONAME);
673 FREE((caddr_t)nd, M_NFSRVDESC);
678 * Check to see if there are outstanding writes that
679 * need to be serviced.
681 cur_usec = nfs_curusec();
683 if (slp->ns_tq.lh_first &&
684 slp->ns_tq.lh_first->nd_time <= cur_usec) {
690 } while (writes_todo);
692 if (nfsrv_dorec(slp, nfsd, &nd)) {
693 nfsd->nfsd_flag &= ~NFSD_REQINPROG;
694 nfsd->nfsd_slp = NULL;
699 TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
701 free((caddr_t)nfsd, M_NFSD);
702 nsd->nsd_nfsd = (struct nfsd *)0;
703 if (--nfs_numnfsd == 0)
704 nfsrv_init(TRUE); /* Reinitialize everything */
709 * Shut down a socket associated with an nfssvc_sock structure.
710 * Should be called with the send lock set, if required.
711 * The trick here is to increment the sref at the start, so that the nfsds
712 * will stop using it and clear ns_flag at the end so that it will not be
713 * reassigned during cleanup.
717 register struct nfssvc_sock *slp;
719 register struct nfsuid *nuidp, *nnuidp;
720 register struct nfsrv_descript *nwp, *nnwp;
723 struct nfsrv_rec *rec;
726 slp->ns_flag &= ~SLP_ALLFLAGS;
729 slp->ns_fp = (struct file *)0;
731 so->so_rcv.sb_flags &= ~SB_UPCALL;
732 so->so_upcall = NULL;
733 so->so_upcallarg = NULL;
735 closef(fp, (struct proc *)0);
737 FREE(slp->ns_nam, M_SONAME);
738 m_freem(slp->ns_raw);
739 while ((rec = STAILQ_FIRST(&slp->ns_rec)) != NULL) {
740 STAILQ_REMOVE_HEAD(&slp->ns_rec, nr_link);
742 FREE(rec->nr_address, M_SONAME);
743 m_freem(rec->nr_packet);
744 free(rec, M_NFSRVDESC);
746 for (nuidp = slp->ns_uidlruhead.tqh_first; nuidp != 0;
748 nnuidp = nuidp->nu_lru.tqe_next;
749 LIST_REMOVE(nuidp, nu_hash);
750 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru);
751 if (nuidp->nu_flag & NU_NAM)
752 FREE(nuidp->nu_nam, M_SONAME);
753 free((caddr_t)nuidp, M_NFSUID);
756 for (nwp = slp->ns_tq.lh_first; nwp; nwp = nnwp) {
757 nnwp = nwp->nd_tq.le_next;
758 LIST_REMOVE(nwp, nd_tq);
759 free((caddr_t)nwp, M_NFSRVDESC);
761 LIST_INIT(&slp->ns_tq);
767 * Derefence a server socket structure. If it has no more references and
768 * is no longer valid, you can throw it away.
772 register struct nfssvc_sock *slp;
774 if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) {
775 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
776 free((caddr_t)slp, M_NFSSVC);
781 * Lock a socket against others.
784 nfs_slplock(slp, wait)
785 register struct nfssvc_sock *slp;
788 int *statep = &slp->ns_solock;
790 if (!wait && (*statep & NFSSTA_SNDLOCK))
791 return(0); /* already locked, fail */
792 while (*statep & NFSSTA_SNDLOCK) {
793 *statep |= NFSSTA_WANTSND;
794 (void) tsleep((caddr_t)statep, PZERO - 1, "nfsslplck", 0);
796 *statep |= NFSSTA_SNDLOCK;
801 * Unlock the stream socket for others.
805 register struct nfssvc_sock *slp;
807 int *statep = &slp->ns_solock;
809 if ((*statep & NFSSTA_SNDLOCK) == 0)
810 panic("nfs slpunlock");
811 *statep &= ~NFSSTA_SNDLOCK;
812 if (*statep & NFSSTA_WANTSND) {
813 *statep &= ~NFSSTA_WANTSND;
814 wakeup((caddr_t)statep);
819 * Initialize the data structures for the server.
820 * Handshake with any new nfsds starting up to avoid any chance of
824 nfsrv_init(terminating)
827 register struct nfssvc_sock *slp, *nslp;
829 if (nfssvc_sockhead_flag & SLP_INIT)
831 nfssvc_sockhead_flag |= SLP_INIT;
833 for (slp = nfssvc_sockhead.tqh_first; slp != 0; slp = nslp) {
834 nslp = slp->ns_chain.tqe_next;
835 if (slp->ns_flag & SLP_VALID)
837 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
838 free((caddr_t)slp, M_NFSSVC);
840 nfsrv_cleancache(); /* And clear out server cache */
842 nfs_pub.np_valid = 0;
844 TAILQ_INIT(&nfssvc_sockhead);
845 nfssvc_sockhead_flag &= ~SLP_INIT;
846 if (nfssvc_sockhead_flag & SLP_WANTINIT) {
847 nfssvc_sockhead_flag &= ~SLP_WANTINIT;
848 wakeup((caddr_t)&nfssvc_sockhead);
851 TAILQ_INIT(&nfsd_head);
852 nfsd_head_flag &= ~NFSD_CHECKSLP;
855 nfs_udpsock = (struct nfssvc_sock *)
856 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
857 bzero((caddr_t)nfs_udpsock, sizeof (struct nfssvc_sock));
858 STAILQ_INIT(&nfs_udpsock->ns_rec);
859 TAILQ_INIT(&nfs_udpsock->ns_uidlruhead);
860 TAILQ_INSERT_HEAD(&nfssvc_sockhead, nfs_udpsock, ns_chain);
862 nfs_cltpsock = (struct nfssvc_sock *)
863 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
864 bzero((caddr_t)nfs_cltpsock, sizeof (struct nfssvc_sock));
865 STAILQ_INIT(&nfs_cltpsock->ns_rec);
866 TAILQ_INIT(&nfs_cltpsock->ns_uidlruhead);
867 TAILQ_INSERT_TAIL(&nfssvc_sockhead, nfs_cltpsock, ns_chain);
872 * Add entries to the server monitor log.
875 nfsd_rt(sotype, nd, cacherep)
877 register struct nfsrv_descript *nd;
880 register struct drt *rt;
882 rt = &nfsdrt.drt[nfsdrt.pos];
883 if (cacherep == RC_DOIT)
885 else if (cacherep == RC_REPLY)
886 rt->flag = DRT_CACHEREPLY;
888 rt->flag = DRT_CACHEDROP;
889 if (sotype == SOCK_STREAM)
891 if (nd->nd_flag & ND_NQNFS)
892 rt->flag |= DRT_NQNFS;
893 else if (nd->nd_flag & ND_NFSV3)
894 rt->flag |= DRT_NFSV3;
895 rt->proc = nd->nd_procnum;
896 if (nd->nd_nam->sa_family == AF_INET)
897 rt->ipadr = ((struct sockaddr_in *)nd->nd_nam)->sin_addr.s_addr;
899 rt->ipadr = INADDR_ANY;
900 rt->resptime = nfs_curusec() - (nd->nd_starttime.tv_sec * 1000000 + nd->nd_starttime.tv_usec);
901 getmicrotime(&rt->tstamp);
902 nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ;
904 #endif /* NFS_NOSERVER */
906 static int nfs_defect = 0;
907 SYSCTL_INT(_vfs_nfs, OID_AUTO, defect, CTLFLAG_RW, &nfs_defect, 0, "");
910 * Asynchronous I/O daemons for client nfs.
911 * They do read-ahead and write-behind operations on the block I/O cache.
912 * Never returns unless it fails or gets killed.
918 register struct buf *bp;
919 register int i, myiod;
920 struct nfsmount *nmp;
924 * Assign my position or return error if too many already running
927 for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
928 if (nfs_asyncdaemon[i] == 0) {
929 nfs_asyncdaemon[i]++;
937 * Just loop around doin our stuff until SIGKILL
940 while (((nmp = nfs_iodmount[myiod]) == NULL
941 || nmp->nm_bufq.tqh_first == NULL)
945 nfs_iodwant[myiod] = p;
946 nfs_iodmount[myiod] = NULL;
947 error = tsleep((caddr_t)&nfs_iodwant[myiod],
948 PWAIT | PCATCH, "nfsidl", 0);
951 nfs_asyncdaemon[myiod] = 0;
954 nfs_iodwant[myiod] = NULL;
955 nfs_iodmount[myiod] = NULL;
959 while ((bp = nmp->nm_bufq.tqh_first) != NULL) {
960 /* Take one off the front of the list */
961 TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist);
963 if (nmp->nm_bufqwant && nmp->nm_bufqlen <= nfs_numasync) {
964 nmp->nm_bufqwant = FALSE;
965 wakeup(&nmp->nm_bufq);
967 if (bp->b_flags & B_READ)
968 (void) nfs_doio(bp, bp->b_rcred, (struct proc *)0);
970 (void) nfs_doio(bp, bp->b_wcred, (struct proc *)0);
972 * If there are more than one iod on this mount, then defect
973 * so that the iods can be shared out fairly between the mounts
975 if (nfs_defect && nmp->nm_bufqiods > 1) {
977 ("nfssvc_iod: iod %d defecting from mount %p\n",
979 nfs_iodmount[myiod] = NULL;
989 * Get an authorization string for the uid by having the mount_nfs sitting
990 * on this mount point porpous out of the kernel and do it.
993 nfs_getauth(nmp, rep, cred, auth_str, auth_len, verf_str, verf_len, key)
994 register struct nfsmount *nmp;
1001 NFSKERBKEY_T key; /* return session key */
1005 while ((nmp->nm_state & NFSSTA_WAITAUTH) == 0) {
1006 nmp->nm_state |= NFSSTA_WANTAUTH;
1007 (void) tsleep((caddr_t)&nmp->nm_authtype, PSOCK,
1008 "nfsauth1", 2 * hz);
1009 error = nfs_sigintr(nmp, rep, rep->r_procp);
1011 nmp->nm_state &= ~NFSSTA_WANTAUTH;
1015 nmp->nm_state &= ~(NFSSTA_WAITAUTH | NFSSTA_WANTAUTH);
1016 nmp->nm_authstr = *auth_str = (char *)malloc(RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK);
1017 nmp->nm_authlen = RPCAUTH_MAXSIZ;
1018 nmp->nm_verfstr = verf_str;
1019 nmp->nm_verflen = *verf_len;
1020 nmp->nm_authuid = cred->cr_uid;
1021 wakeup((caddr_t)&nmp->nm_authstr);
1024 * And wait for mount_nfs to do its stuff.
1026 while ((nmp->nm_state & NFSSTA_HASAUTH) == 0 && error == 0) {
1027 (void) tsleep((caddr_t)&nmp->nm_authlen, PSOCK,
1028 "nfsauth2", 2 * hz);
1029 error = nfs_sigintr(nmp, rep, rep->r_procp);
1031 if (nmp->nm_state & NFSSTA_AUTHERR) {
1032 nmp->nm_state &= ~NFSSTA_AUTHERR;
1036 free((caddr_t)*auth_str, M_TEMP);
1038 *auth_len = nmp->nm_authlen;
1039 *verf_len = nmp->nm_verflen;
1040 bcopy((caddr_t)nmp->nm_key, (caddr_t)key, sizeof (key));
1042 nmp->nm_state &= ~NFSSTA_HASAUTH;
1043 nmp->nm_state |= NFSSTA_WAITAUTH;
1044 if (nmp->nm_state & NFSSTA_WANTAUTH) {
1045 nmp->nm_state &= ~NFSSTA_WANTAUTH;
1046 wakeup((caddr_t)&nmp->nm_authtype);
1052 * Get a nickname authenticator and verifier.
1055 nfs_getnickauth(nmp, cred, auth_str, auth_len, verf_str, verf_len)
1056 struct nfsmount *nmp;
1063 register struct nfsuid *nuidp;
1064 register u_int32_t *nickp, *verfp;
1065 struct timeval ktvin, ktvout;
1068 if (verf_len < (4 * NFSX_UNSIGNED))
1069 panic("nfs_getnickauth verf too small");
1071 for (nuidp = NMUIDHASH(nmp, cred->cr_uid)->lh_first;
1072 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
1073 if (nuidp->nu_cr.cr_uid == cred->cr_uid)
1076 if (!nuidp || nuidp->nu_expire < time_second)
1080 * Move to the end of the lru list (end of lru == most recently used).
1082 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
1083 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, nu_lru);
1085 nickp = (u_int32_t *)malloc(2 * NFSX_UNSIGNED, M_TEMP, M_WAITOK);
1086 *nickp++ = txdr_unsigned(RPCAKN_NICKNAME);
1087 *nickp = txdr_unsigned(nuidp->nu_nickname);
1088 *auth_str = (char *)nickp;
1089 *auth_len = 2 * NFSX_UNSIGNED;
1092 * Now we must encrypt the verifier and package it up.
1094 verfp = (u_int32_t *)verf_str;
1095 *verfp++ = txdr_unsigned(RPCAKN_NICKNAME);
1096 if (time_second > nuidp->nu_timestamp.tv_sec ||
1097 (time_second == nuidp->nu_timestamp.tv_sec &&
1098 time_second > nuidp->nu_timestamp.tv_usec))
1099 getmicrotime(&nuidp->nu_timestamp);
1101 nuidp->nu_timestamp.tv_usec++;
1102 ktvin.tv_sec = txdr_unsigned(nuidp->nu_timestamp.tv_sec);
1103 ktvin.tv_usec = txdr_unsigned(nuidp->nu_timestamp.tv_usec);
1106 * Now encrypt the timestamp verifier in ecb mode using the session
1113 *verfp++ = ktvout.tv_sec;
1114 *verfp++ = ktvout.tv_usec;
1120 * Save the current nickname in a hash list entry on the mount point.
1123 nfs_savenickauth(nmp, cred, len, key, mdp, dposp, mrep)
1124 register struct nfsmount *nmp;
1132 register struct nfsuid *nuidp;
1133 register u_int32_t *tl;
1134 register int32_t t1;
1135 struct mbuf *md = *mdp;
1136 struct timeval ktvin, ktvout;
1138 char *dpos = *dposp, *cp2;
1139 int deltasec, error = 0;
1141 if (len == (3 * NFSX_UNSIGNED)) {
1142 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1143 ktvin.tv_sec = *tl++;
1144 ktvin.tv_usec = *tl++;
1145 nick = fxdr_unsigned(u_int32_t, *tl);
1148 * Decrypt the timestamp in ecb mode.
1153 ktvout.tv_sec = fxdr_unsigned(long, ktvout.tv_sec);
1154 ktvout.tv_usec = fxdr_unsigned(long, ktvout.tv_usec);
1155 deltasec = time_second - ktvout.tv_sec;
1157 deltasec = -deltasec;
1159 * If ok, add it to the hash list for the mount point.
1161 if (deltasec <= NFS_KERBCLOCKSKEW) {
1162 if (nmp->nm_numuids < nuidhash_max) {
1164 nuidp = (struct nfsuid *)
1165 malloc(sizeof (struct nfsuid), M_NFSUID,
1168 nuidp = nmp->nm_uidlruhead.tqh_first;
1169 LIST_REMOVE(nuidp, nu_hash);
1170 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp,
1174 nuidp->nu_cr.cr_uid = cred->cr_uid;
1175 nuidp->nu_expire = time_second + NFS_KERBTTL;
1176 nuidp->nu_timestamp = ktvout;
1177 nuidp->nu_nickname = nick;
1178 bcopy(key, nuidp->nu_key, sizeof (key));
1179 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp,
1181 LIST_INSERT_HEAD(NMUIDHASH(nmp, cred->cr_uid),
1185 nfsm_adv(nfsm_rndup(len));