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.21 2005/06/06 15:09:38 drhodus 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"
75 #include <sys/thread2.h>
77 static MALLOC_DEFINE(M_NFSSVC, "NFS srvsock", "Nfs server structure");
80 extern int32_t (*nfsrv3_procs[NFS_NPROCS]) (struct nfsrv_descript *nd,
81 struct nfssvc_sock *slp,
84 extern int nfs_numasync;
85 extern time_t nqnfsstarttime;
86 extern int nqsrv_writeslack;
88 extern struct nfsstats nfsstats;
89 extern int nfsrvw_procrastinate;
90 extern int nfsrvw_procrastinate_v3;
91 static int nuidhash_max = NFS_MAXUIDHASH;
94 static void nfsrv_zapsock (struct nfssvc_sock *slp);
96 static int nfssvc_iod (struct thread *);
101 static int nfs_asyncdaemon[NFS_MAXASYNCDAEMON];
103 SYSCTL_DECL(_vfs_nfs);
106 int nfsd_waiting = 0;
107 static struct nfsdrt nfsdrt;
108 static int nfs_numnfsd = 0;
109 static int notstarted = 1;
110 static int modify_flag = 0;
111 static void nfsd_rt (int sotype, struct nfsrv_descript *nd,
113 static int nfssvc_addsock (struct file *, struct sockaddr *,
115 static int nfssvc_nfsd (struct nfsd_srvargs *,caddr_t,struct thread *);
117 static int nfs_privport = 0;
118 SYSCTL_INT(_vfs_nfs, NFS_NFSPRIVPORT, nfs_privport, CTLFLAG_RW, &nfs_privport, 0, "");
119 SYSCTL_INT(_vfs_nfs, OID_AUTO, gatherdelay, CTLFLAG_RW, &nfsrvw_procrastinate, 0, "");
120 SYSCTL_INT(_vfs_nfs, OID_AUTO, gatherdelay_v3, CTLFLAG_RW, &nfsrvw_procrastinate_v3, 0, "");
121 static int nfs_soreserve = 65535;
122 SYSCTL_INT(_vfs_nfs, OID_AUTO, soreserve, CTLFLAG_RW, &nfs_soreserve, 0, "");
125 * NFS server system calls
128 #endif /* NFS_NOSERVER */
130 * nfssvc_args(int flag, caddr_t argp)
132 * Nfs server psuedo system call for the nfsd's
133 * Based on the flag value it either:
134 * - adds a socket to the selection list
135 * - remains in the kernel as an nfsd
136 * - remains in the kernel as an nfsiod
139 nfssvc(struct nfssvc_args *uap)
142 struct nlookupdata nd;
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;
153 #endif /* NFS_NOSERVER */
155 struct thread *td = curthread;
163 KKASSERT(td->td_proc); /* for ucred and p_fd */
164 while (nfssvc_sockhead_flag & SLP_INIT) {
165 nfssvc_sockhead_flag |= SLP_WANTINIT;
166 (void) tsleep((caddr_t)&nfssvc_sockhead, 0, "nfsd init", 0);
168 if (uap->flag & NFSSVC_BIOD)
169 error = nfssvc_iod(td);
173 #else /* !NFS_NOSERVER */
174 else if (uap->flag & NFSSVC_MNTD) {
175 error = copyin(uap->argp, (caddr_t)&ncd, sizeof (ncd));
179 error = nlookup_init(&nd, ncd.ncd_dirp, UIO_USERSPACE,
182 error = nlookup(&nd);
184 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
189 if ((vp->v_flag & VROOT) == 0)
191 nmp = VFSTONFS(vp->v_mount);
195 if ((nmp->nm_state & NFSSTA_MNTD) &&
196 (uap->flag & NFSSVC_GOTAUTH) == 0)
198 nmp->nm_state |= NFSSTA_MNTD;
199 error = nqnfs_clientd(nmp, td->td_proc->p_ucred, &ncd, uap->flag,
201 } else if (uap->flag & NFSSVC_ADDSOCK) {
202 error = copyin(uap->argp, (caddr_t)&nfsdarg, sizeof(nfsdarg));
205 error = holdsock(td->td_proc->p_fd, nfsdarg.sock, &fp);
209 * Get the client address for connected sockets.
211 if (nfsdarg.name == NULL || nfsdarg.namelen == 0)
212 nam = (struct sockaddr *)0;
214 error = getsockaddr(&nam, nfsdarg.name,
221 error = nfssvc_addsock(fp, nam, td);
224 error = copyin(uap->argp, (caddr_t)nsd, sizeof (*nsd));
227 if ((uap->flag & NFSSVC_AUTHIN) &&
228 ((nfsd = nsd->nsd_nfsd)) != NULL &&
229 (nfsd->nfsd_slp->ns_flag & SLP_VALID)) {
230 slp = nfsd->nfsd_slp;
233 * First check to see if another nfsd has already
234 * added this credential.
236 for (nuidp = NUIDHASH(slp,nsd->nsd_cr.cr_uid)->lh_first;
237 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
238 if (nuidp->nu_cr.cr_uid == nsd->nsd_cr.cr_uid &&
239 (!nfsd->nfsd_nd->nd_nam2 ||
240 netaddr_match(NU_NETFAM(nuidp),
241 &nuidp->nu_haddr, nfsd->nfsd_nd->nd_nam2)))
245 nfsrv_setcred(&nuidp->nu_cr,&nfsd->nfsd_nd->nd_cr);
246 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
251 if (slp->ns_numuids < nuidhash_max) {
253 nuidp = (struct nfsuid *)
254 malloc(sizeof (struct nfsuid), M_NFSUID,
257 nuidp = (struct nfsuid *)0;
258 if ((slp->ns_flag & SLP_VALID) == 0) {
260 free((caddr_t)nuidp, M_NFSUID);
262 if (nuidp == (struct nfsuid *)0) {
263 nuidp = TAILQ_FIRST(&slp->ns_uidlruhead);
264 LIST_REMOVE(nuidp, nu_hash);
265 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp,
267 if (nuidp->nu_flag & NU_NAM)
268 FREE(nuidp->nu_nam, M_SONAME);
271 nuidp->nu_cr = nsd->nsd_cr;
272 if (nuidp->nu_cr.cr_ngroups > NGROUPS)
273 nuidp->nu_cr.cr_ngroups = NGROUPS;
274 nuidp->nu_cr.cr_ref = 1;
275 nuidp->nu_timestamp = nsd->nsd_timestamp;
276 nuidp->nu_expire = time_second + nsd->nsd_ttl;
278 * and save the session key in nu_key.
280 bcopy(nsd->nsd_key, nuidp->nu_key,
281 sizeof (nsd->nsd_key));
282 if (nfsd->nfsd_nd->nd_nam2) {
283 struct sockaddr_in *saddr;
285 saddr = (struct sockaddr_in *)
286 nfsd->nfsd_nd->nd_nam2;
287 switch (saddr->sin_family) {
289 nuidp->nu_flag |= NU_INETADDR;
291 saddr->sin_addr.s_addr;
295 nuidp->nu_flag |= NU_NAM;
297 dup_sockaddr(nfsd->nfsd_nd->nd_nam2);
301 TAILQ_INSERT_TAIL(&slp->ns_uidlruhead, nuidp,
303 LIST_INSERT_HEAD(NUIDHASH(slp, nsd->nsd_uid),
305 nfsrv_setcred(&nuidp->nu_cr,
306 &nfsd->nfsd_nd->nd_cr);
307 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
311 if ((uap->flag & NFSSVC_AUTHINFAIL) && (nfsd = nsd->nsd_nfsd))
312 nfsd->nfsd_flag |= NFSD_AUTHFAIL;
313 error = nfssvc_nfsd(nsd, uap->argp, td);
315 #endif /* NFS_NOSERVER */
316 if (error == EINTR || error == ERESTART)
323 * Adds a socket to the list for servicing by nfsds.
326 nfssvc_addsock(struct file *fp, struct sockaddr *mynam, struct thread *td)
329 struct nfssvc_sock *slp;
333 so = (struct socket *)fp->f_data;
335 tslp = (struct nfssvc_sock *)0;
337 * Add it to the list, as required.
339 if (so->so_proto->pr_protocol == IPPROTO_UDP) {
341 if (tslp->ns_flag & SLP_VALID) {
343 FREE(mynam, M_SONAME);
349 * Reserve buffer space in the socket. Note that due to bugs in
350 * Linux's delayed-ack code, serious performance degredation may
351 * occur with linux hosts if the minimum is used.
353 if (so->so_type == SOCK_STREAM)
354 siz = NFS_MAXPACKET + sizeof (u_long);
357 if (siz < nfs_soreserve)
359 if (siz > sb_max_adj) {
360 printf("Warning: vfs.nfs.soreserve (%d) "
361 "limited to adjusted sb_max (%ld)\n",
362 nfs_soreserve, sb_max_adj);
366 error = soreserve(so, siz, siz, &td->td_proc->p_rlimit[RLIMIT_SBSIZE]);
369 FREE(mynam, M_SONAME);
374 * Set protocol specific options { for now TCP only } and
375 * reserve some space. For datagram sockets, this can get called
376 * repeatedly for the same socket, but that isn't harmful.
378 if (so->so_type == SOCK_STREAM) {
382 bzero(&sopt, sizeof sopt);
383 sopt.sopt_level = SOL_SOCKET;
384 sopt.sopt_name = SO_KEEPALIVE;
385 sopt.sopt_val = &val;
386 sopt.sopt_valsize = sizeof val;
390 if (so->so_proto->pr_domain->dom_family == AF_INET &&
391 so->so_proto->pr_protocol == IPPROTO_TCP) {
395 bzero(&sopt, sizeof sopt);
396 sopt.sopt_level = IPPROTO_TCP;
397 sopt.sopt_name = TCP_NODELAY;
398 sopt.sopt_val = &val;
399 sopt.sopt_valsize = sizeof val;
403 so->so_rcv.sb_flags &= ~SB_NOINTR;
404 so->so_rcv.sb_timeo = 0;
405 so->so_snd.sb_flags &= ~SB_NOINTR;
406 so->so_snd.sb_timeo = 0;
408 slp = (struct nfssvc_sock *)
409 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
410 bzero((caddr_t)slp, sizeof (struct nfssvc_sock));
411 STAILQ_INIT(&slp->ns_rec);
412 TAILQ_INIT(&slp->ns_uidlruhead);
413 TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
420 so->so_upcallarg = (caddr_t)slp;
421 so->so_upcall = nfsrv_rcv;
422 so->so_rcv.sb_flags |= SB_UPCALL;
423 slp->ns_flag = (SLP_VALID | SLP_NEEDQ);
424 nfsrv_wakenfsd(slp, 1);
430 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
431 * until it is killed by a signal.
434 nfssvc_nfsd(struct nfsd_srvargs *nsd, caddr_t argp, struct thread *td)
437 struct nfssvc_sock *slp;
438 struct nfsd *nfsd = nsd->nsd_nfsd;
439 struct nfsrv_descript *nd = NULL;
440 struct mbuf *m, *mreq;
441 int error = 0, cacherep, sotype, writes_todo;
449 if (nfsd == (struct nfsd *)0) {
450 nsd->nsd_nfsd = nfsd = (struct nfsd *)
451 malloc(sizeof (struct nfsd), M_NFSD, M_WAITOK);
452 bzero((caddr_t)nfsd, sizeof (struct nfsd));
455 TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain);
461 * Loop getting rpc requests until SIGKILL.
464 if ((nfsd->nfsd_flag & NFSD_REQINPROG) == 0) {
465 while (nfsd->nfsd_slp == NULL &&
466 (nfsd_head_flag & NFSD_CHECKSLP) == 0) {
467 nfsd->nfsd_flag |= NFSD_WAITING;
469 error = tsleep((caddr_t)nfsd, PCATCH, "nfsd", 0);
474 if (nfsd->nfsd_slp == NULL &&
475 (nfsd_head_flag & NFSD_CHECKSLP) != 0) {
476 TAILQ_FOREACH(slp, &nfssvc_sockhead, ns_chain) {
477 if ((slp->ns_flag & (SLP_VALID | SLP_DOREC))
478 == (SLP_VALID | SLP_DOREC)) {
479 slp->ns_flag &= ~SLP_DOREC;
481 nfsd->nfsd_slp = slp;
486 nfsd_head_flag &= ~NFSD_CHECKSLP;
488 if ((slp = nfsd->nfsd_slp) == (struct nfssvc_sock *)0)
490 if (slp->ns_flag & SLP_VALID) {
491 if (slp->ns_flag & SLP_DISCONN)
493 else if (slp->ns_flag & SLP_NEEDQ) {
494 slp->ns_flag &= ~SLP_NEEDQ;
495 (void) nfs_slplock(slp, 1);
496 nfsrv_rcv(slp->ns_so, (caddr_t)slp,
500 error = nfsrv_dorec(slp, nfsd, &nd);
501 cur_usec = nfs_curusec();
502 if (error && slp->ns_tq.lh_first &&
503 slp->ns_tq.lh_first->nd_time <= cur_usec) {
509 nfsd->nfsd_flag |= NFSD_REQINPROG;
513 slp = nfsd->nfsd_slp;
515 if (error || (slp->ns_flag & SLP_VALID) == 0) {
517 free((caddr_t)nd, M_NFSRVDESC);
520 nfsd->nfsd_slp = (struct nfssvc_sock *)0;
521 nfsd->nfsd_flag &= ~NFSD_REQINPROG;
526 sotype = slp->ns_so->so_type;
528 getmicrotime(&nd->nd_starttime);
530 nd->nd_nam = nd->nd_nam2;
532 nd->nd_nam = slp->ns_nam;
535 * Check to see if authorization is needed.
537 if (nfsd->nfsd_flag & NFSD_NEEDAUTH) {
538 nfsd->nfsd_flag &= ~NFSD_NEEDAUTH;
540 ((struct sockaddr_in *)
541 nd->nd_nam)->sin_addr.s_addr;
542 nsd->nsd_authlen = nfsd->nfsd_authlen;
543 nsd->nsd_verflen = nfsd->nfsd_verflen;
544 if (!copyout(nfsd->nfsd_authstr,nsd->nsd_authstr,
545 nfsd->nfsd_authlen) &&
546 !copyout(nfsd->nfsd_verfstr, nsd->nsd_verfstr,
547 nfsd->nfsd_verflen) &&
548 !copyout((caddr_t)nsd, argp, sizeof (*nsd)))
550 cacherep = RC_DROPIT;
552 cacherep = nfsrv_getcache(nd, slp, &mreq);
555 * Check for just starting up for NQNFS and send
556 * fake "try again later" replies to the NQNFS clients.
558 if (notstarted && nqnfsstarttime <= time_second) {
560 nqnfsstarttime = time_second + nqsrv_writeslack;
566 if ((nd->nd_flag & ND_NQNFS) == 0)
567 cacherep = RC_DROPIT;
568 else if (nd->nd_procnum != NFSPROC_WRITE) {
569 nd->nd_procnum = NFSPROC_NOOP;
570 nd->nd_repstat = NQNFS_TRYLATER;
574 } else if (nfsd->nfsd_flag & NFSD_AUTHFAIL) {
575 nfsd->nfsd_flag &= ~NFSD_AUTHFAIL;
576 nd->nd_procnum = NFSPROC_NOOP;
577 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
579 } else if (nfs_privport) {
580 /* Check if source port is privileged */
582 struct sockaddr *nam = nd->nd_nam;
583 struct sockaddr_in *sin;
585 sin = (struct sockaddr_in *)nam;
586 port = ntohs(sin->sin_port);
587 if (port >= IPPORT_RESERVED &&
588 nd->nd_procnum != NFSPROC_NULL) {
589 nd->nd_procnum = NFSPROC_NOOP;
590 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
592 printf("NFS request from unprivileged port (%s:%d)\n",
593 inet_ntoa(sin->sin_addr), port);
600 * Loop to get all the write rpc relies that have been
606 if (nd && (nd->nd_flag & ND_NFSV3))
607 procrastinate = nfsrvw_procrastinate_v3;
609 procrastinate = nfsrvw_procrastinate;
610 if (writes_todo || (nd->nd_procnum == NFSPROC_WRITE &&
611 procrastinate > 0 && !notstarted))
612 error = nfsrv_writegather(&nd, slp,
613 nfsd->nfsd_td, &mreq);
615 error = (*(nfsrv3_procs[nd->nd_procnum]))(nd,
616 slp, nfsd->nfsd_td, &mreq);
619 if (error != 0 && error != NFSERR_RETVOID) {
620 if (nd->nd_procnum != NQNFSPROC_VACATED)
622 nfsrv_updatecache(nd, FALSE, mreq);
624 FREE(nd->nd_nam2, M_SONAME);
627 nfsstats.srvrpccnt[nd->nd_procnum]++;
628 nfsrv_updatecache(nd, TRUE, mreq);
629 nd->nd_mrep = (struct mbuf *)0;
637 if (siz <= 0 || siz > NFS_MAXPACKET) {
638 printf("mbuf siz=%d\n",siz);
639 panic("Bad nfs svc reply");
642 m->m_pkthdr.len = siz;
643 m->m_pkthdr.rcvif = (struct ifnet *)0;
645 * For stream protocols, prepend a Sun RPC
648 if (sotype == SOCK_STREAM) {
649 M_PREPEND(m, NFSX_UNSIGNED, MB_WAIT);
652 *mtod(m, u_int32_t *) = htonl(0x80000000 | siz);
654 if (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED)
655 (void) nfs_slplock(slp, 1);
656 if (slp->ns_flag & SLP_VALID)
657 error = nfs_send(slp->ns_so, nd->nd_nam2, m, NULL);
663 nfsd_rt(sotype, nd, cacherep);
665 FREE(nd->nd_nam2, M_SONAME);
667 m_freem(nd->nd_mrep);
670 if (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED)
672 if (error == EINTR || error == ERESTART) {
673 free((caddr_t)nd, M_NFSRVDESC);
681 nfsd_rt(sotype, nd, cacherep);
682 m_freem(nd->nd_mrep);
684 FREE(nd->nd_nam2, M_SONAME);
688 FREE((caddr_t)nd, M_NFSRVDESC);
693 * Check to see if there are outstanding writes that
694 * need to be serviced.
696 cur_usec = nfs_curusec();
698 if (slp->ns_tq.lh_first &&
699 slp->ns_tq.lh_first->nd_time <= cur_usec) {
705 } while (writes_todo);
707 if (nfsrv_dorec(slp, nfsd, &nd)) {
708 nfsd->nfsd_flag &= ~NFSD_REQINPROG;
709 nfsd->nfsd_slp = NULL;
714 TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
716 free((caddr_t)nfsd, M_NFSD);
717 nsd->nsd_nfsd = (struct nfsd *)0;
718 if (--nfs_numnfsd == 0)
719 nfsrv_init(TRUE); /* Reinitialize everything */
724 * Shut down a socket associated with an nfssvc_sock structure.
725 * Should be called with the send lock set, if required.
726 * The trick here is to increment the sref at the start, so that the nfsds
727 * will stop using it and clear ns_flag at the end so that it will not be
728 * reassigned during cleanup.
731 nfsrv_zapsock(struct nfssvc_sock *slp)
733 struct nfsuid *nuidp, *nnuidp;
734 struct nfsrv_descript *nwp, *nnwp;
737 struct nfsrv_rec *rec;
739 slp->ns_flag &= ~SLP_ALLFLAGS;
742 slp->ns_fp = (struct file *)0;
744 so->so_rcv.sb_flags &= ~SB_UPCALL;
745 so->so_upcall = NULL;
746 so->so_upcallarg = NULL;
750 FREE(slp->ns_nam, M_SONAME);
751 m_freem(slp->ns_raw);
752 while ((rec = STAILQ_FIRST(&slp->ns_rec)) != NULL) {
754 STAILQ_REMOVE_HEAD(&slp->ns_rec, nr_link);
756 FREE(rec->nr_address, M_SONAME);
757 m_freem(rec->nr_packet);
758 free(rec, M_NFSRVDESC);
760 TAILQ_FOREACH_MUTABLE(nuidp, &slp->ns_uidlruhead, nu_lru,
762 LIST_REMOVE(nuidp, nu_hash);
763 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru);
764 if (nuidp->nu_flag & NU_NAM)
765 FREE(nuidp->nu_nam, M_SONAME);
766 free((caddr_t)nuidp, M_NFSUID);
769 for (nwp = slp->ns_tq.lh_first; nwp; nwp = nnwp) {
770 nnwp = nwp->nd_tq.le_next;
771 LIST_REMOVE(nwp, nd_tq);
772 free((caddr_t)nwp, M_NFSRVDESC);
774 LIST_INIT(&slp->ns_tq);
780 * Derefence a server socket structure. If it has no more references and
781 * is no longer valid, you can throw it away.
784 nfsrv_slpderef(struct nfssvc_sock *slp)
786 if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) {
787 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
788 free((caddr_t)slp, M_NFSSVC);
793 * Lock a socket against others.
796 nfs_slplock(struct nfssvc_sock *slp, int wait)
798 int *statep = &slp->ns_solock;
800 if (!wait && (*statep & NFSSTA_SNDLOCK))
801 return(0); /* already locked, fail */
802 while (*statep & NFSSTA_SNDLOCK) {
803 *statep |= NFSSTA_WANTSND;
804 (void) tsleep((caddr_t)statep, 0, "nfsslplck", 0);
806 *statep |= NFSSTA_SNDLOCK;
811 * Unlock the stream socket for others.
814 nfs_slpunlock(struct nfssvc_sock *slp)
816 int *statep = &slp->ns_solock;
818 if ((*statep & NFSSTA_SNDLOCK) == 0)
819 panic("nfs slpunlock");
820 *statep &= ~NFSSTA_SNDLOCK;
821 if (*statep & NFSSTA_WANTSND) {
822 *statep &= ~NFSSTA_WANTSND;
823 wakeup((caddr_t)statep);
828 * Initialize the data structures for the server.
829 * Handshake with any new nfsds starting up to avoid any chance of
833 nfsrv_init(int terminating)
835 struct nfssvc_sock *slp, *nslp;
837 if (nfssvc_sockhead_flag & SLP_INIT)
839 nfssvc_sockhead_flag |= SLP_INIT;
841 TAILQ_FOREACH_MUTABLE(slp, &nfssvc_sockhead, ns_chain, nslp) {
842 if (slp->ns_flag & SLP_VALID)
844 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
845 free((caddr_t)slp, M_NFSSVC);
847 nfsrv_cleancache(); /* And clear out server cache */
849 nfs_pub.np_valid = 0;
851 TAILQ_INIT(&nfssvc_sockhead);
852 nfssvc_sockhead_flag &= ~SLP_INIT;
853 if (nfssvc_sockhead_flag & SLP_WANTINIT) {
854 nfssvc_sockhead_flag &= ~SLP_WANTINIT;
855 wakeup((caddr_t)&nfssvc_sockhead);
858 TAILQ_INIT(&nfsd_head);
859 nfsd_head_flag &= ~NFSD_CHECKSLP;
862 nfs_udpsock = (struct nfssvc_sock *)
863 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
864 bzero((caddr_t)nfs_udpsock, sizeof (struct nfssvc_sock));
865 STAILQ_INIT(&nfs_udpsock->ns_rec);
866 TAILQ_INIT(&nfs_udpsock->ns_uidlruhead);
867 TAILQ_INSERT_HEAD(&nfssvc_sockhead, nfs_udpsock, ns_chain);
869 nfs_cltpsock = (struct nfssvc_sock *)
870 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
871 bzero((caddr_t)nfs_cltpsock, sizeof (struct nfssvc_sock));
872 STAILQ_INIT(&nfs_cltpsock->ns_rec);
873 TAILQ_INIT(&nfs_cltpsock->ns_uidlruhead);
874 TAILQ_INSERT_TAIL(&nfssvc_sockhead, nfs_cltpsock, ns_chain);
879 * Add entries to the server monitor log.
882 nfsd_rt(int sotype, struct nfsrv_descript *nd, int cacherep)
886 rt = &nfsdrt.drt[nfsdrt.pos];
887 if (cacherep == RC_DOIT)
889 else if (cacherep == RC_REPLY)
890 rt->flag = DRT_CACHEREPLY;
892 rt->flag = DRT_CACHEDROP;
893 if (sotype == SOCK_STREAM)
895 if (nd->nd_flag & ND_NQNFS)
896 rt->flag |= DRT_NQNFS;
897 else if (nd->nd_flag & ND_NFSV3)
898 rt->flag |= DRT_NFSV3;
899 rt->proc = nd->nd_procnum;
900 if (nd->nd_nam->sa_family == AF_INET)
901 rt->ipadr = ((struct sockaddr_in *)nd->nd_nam)->sin_addr.s_addr;
903 rt->ipadr = INADDR_ANY;
904 rt->resptime = nfs_curusec() - (nd->nd_starttime.tv_sec * 1000000 + nd->nd_starttime.tv_usec);
905 getmicrotime(&rt->tstamp);
906 nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ;
908 #endif /* NFS_NOSERVER */
910 static int nfs_defect = 0;
911 SYSCTL_INT(_vfs_nfs, OID_AUTO, defect, CTLFLAG_RW, &nfs_defect, 0, "");
914 * Asynchronous I/O daemons for client nfs.
915 * They do read-ahead and write-behind operations on the block I/O cache.
916 * Never returns unless it fails or gets killed.
919 nfssvc_iod(struct thread *td)
923 struct nfsmount *nmp;
927 * Assign my position or return error if too many already running
930 for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
931 if (nfs_asyncdaemon[i] == 0) {
932 nfs_asyncdaemon[i]++;
940 * Just loop around doin our stuff until SIGKILL
943 while (((nmp = nfs_iodmount[myiod]) == NULL
944 || TAILQ_EMPTY(&nmp->nm_bufq))
948 nfs_iodwant[myiod] = td;
949 nfs_iodmount[myiod] = NULL;
950 error = tsleep((caddr_t)&nfs_iodwant[myiod],
951 PCATCH, "nfsidl", 0);
954 nfs_asyncdaemon[myiod] = 0;
957 nfs_iodwant[myiod] = NULL;
958 nfs_iodmount[myiod] = NULL;
962 while ((bp = TAILQ_FIRST(&nmp->nm_bufq)) != NULL) {
963 /* Take one off the front of the list */
964 TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist);
966 if (nmp->nm_bufqwant && nmp->nm_bufqlen <= nfs_numasync) {
967 nmp->nm_bufqwant = FALSE;
968 wakeup(&nmp->nm_bufq);
970 (void) nfs_doio(bp, NULL);
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(struct nfsmount *nmp, struct nfsreq *rep,
994 struct ucred *cred, char **auth_str, int *auth_len, char *verf_str,
995 int *verf_len, NFSKERBKEY_T key /* return session key */)
999 while ((nmp->nm_state & NFSSTA_WAITAUTH) == 0) {
1000 nmp->nm_state |= NFSSTA_WANTAUTH;
1001 (void) tsleep((caddr_t)&nmp->nm_authtype, 0,
1002 "nfsauth1", 2 * hz);
1003 error = nfs_sigintr(nmp, rep, rep->r_td);
1005 nmp->nm_state &= ~NFSSTA_WANTAUTH;
1009 nmp->nm_state &= ~(NFSSTA_WAITAUTH | NFSSTA_WANTAUTH);
1010 nmp->nm_authstr = *auth_str = (char *)malloc(RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK);
1011 nmp->nm_authlen = RPCAUTH_MAXSIZ;
1012 nmp->nm_verfstr = verf_str;
1013 nmp->nm_verflen = *verf_len;
1014 nmp->nm_authuid = cred->cr_uid;
1015 wakeup((caddr_t)&nmp->nm_authstr);
1018 * And wait for mount_nfs to do its stuff.
1020 while ((nmp->nm_state & NFSSTA_HASAUTH) == 0 && error == 0) {
1021 (void) tsleep((caddr_t)&nmp->nm_authlen, 0,
1022 "nfsauth2", 2 * hz);
1023 error = nfs_sigintr(nmp, rep, rep->r_td);
1025 if (nmp->nm_state & NFSSTA_AUTHERR) {
1026 nmp->nm_state &= ~NFSSTA_AUTHERR;
1030 free((caddr_t)*auth_str, M_TEMP);
1032 *auth_len = nmp->nm_authlen;
1033 *verf_len = nmp->nm_verflen;
1034 bcopy((caddr_t)nmp->nm_key, (caddr_t)key, sizeof (key));
1036 nmp->nm_state &= ~NFSSTA_HASAUTH;
1037 nmp->nm_state |= NFSSTA_WAITAUTH;
1038 if (nmp->nm_state & NFSSTA_WANTAUTH) {
1039 nmp->nm_state &= ~NFSSTA_WANTAUTH;
1040 wakeup((caddr_t)&nmp->nm_authtype);
1046 * Get a nickname authenticator and verifier.
1049 nfs_getnickauth(struct nfsmount *nmp, struct ucred *cred, char **auth_str,
1050 int *auth_len, char *verf_str, int verf_len)
1052 struct nfsuid *nuidp;
1053 u_int32_t *nickp, *verfp;
1054 struct timeval ktvin, ktvout;
1057 if (verf_len < (4 * NFSX_UNSIGNED))
1058 panic("nfs_getnickauth verf too small");
1060 for (nuidp = NMUIDHASH(nmp, cred->cr_uid)->lh_first;
1061 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
1062 if (nuidp->nu_cr.cr_uid == cred->cr_uid)
1065 if (!nuidp || nuidp->nu_expire < time_second)
1069 * Move to the end of the lru list (end of lru == most recently used).
1071 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
1072 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, nu_lru);
1074 nickp = (u_int32_t *)malloc(2 * NFSX_UNSIGNED, M_TEMP, M_WAITOK);
1075 *nickp++ = txdr_unsigned(RPCAKN_NICKNAME);
1076 *nickp = txdr_unsigned(nuidp->nu_nickname);
1077 *auth_str = (char *)nickp;
1078 *auth_len = 2 * NFSX_UNSIGNED;
1081 * Now we must encrypt the verifier and package it up.
1083 verfp = (u_int32_t *)verf_str;
1084 *verfp++ = txdr_unsigned(RPCAKN_NICKNAME);
1085 if (time_second > nuidp->nu_timestamp.tv_sec ||
1086 (time_second == nuidp->nu_timestamp.tv_sec &&
1087 time_second > nuidp->nu_timestamp.tv_usec))
1088 getmicrotime(&nuidp->nu_timestamp);
1090 nuidp->nu_timestamp.tv_usec++;
1091 ktvin.tv_sec = txdr_unsigned(nuidp->nu_timestamp.tv_sec);
1092 ktvin.tv_usec = txdr_unsigned(nuidp->nu_timestamp.tv_usec);
1095 * Now encrypt the timestamp verifier in ecb mode using the session
1102 *verfp++ = ktvout.tv_sec;
1103 *verfp++ = ktvout.tv_usec;
1109 * Save the current nickname in a hash list entry on the mount point.
1112 nfs_savenickauth(struct nfsmount *nmp, struct ucred *cred, int len,
1113 NFSKERBKEY_T key, struct mbuf **mdp, char **dposp,
1116 struct nfsuid *nuidp;
1119 struct mbuf *md = *mdp;
1120 struct timeval ktvin, ktvout;
1122 char *dpos = *dposp, *cp2;
1123 int deltasec, error = 0;
1125 if (len == (3 * NFSX_UNSIGNED)) {
1126 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1127 ktvin.tv_sec = *tl++;
1128 ktvin.tv_usec = *tl++;
1129 nick = fxdr_unsigned(u_int32_t, *tl);
1132 * Decrypt the timestamp in ecb mode.
1137 ktvout.tv_sec = fxdr_unsigned(long, ktvout.tv_sec);
1138 ktvout.tv_usec = fxdr_unsigned(long, ktvout.tv_usec);
1139 deltasec = time_second - ktvout.tv_sec;
1141 deltasec = -deltasec;
1143 * If ok, add it to the hash list for the mount point.
1145 if (deltasec <= NFS_KERBCLOCKSKEW) {
1146 if (nmp->nm_numuids < nuidhash_max) {
1148 nuidp = (struct nfsuid *)
1149 malloc(sizeof (struct nfsuid), M_NFSUID,
1152 nuidp = TAILQ_FIRST(&nmp->nm_uidlruhead);
1153 LIST_REMOVE(nuidp, nu_hash);
1154 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp,
1158 nuidp->nu_cr.cr_uid = cred->cr_uid;
1159 nuidp->nu_expire = time_second + NFS_KERBTTL;
1160 nuidp->nu_timestamp = ktvout;
1161 nuidp->nu_nickname = nick;
1162 bcopy(key, nuidp->nu_key, sizeof (key));
1163 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp,
1165 LIST_INSERT_HEAD(NMUIDHASH(nmp, cred->cr_uid),
1169 nfsm_adv(nfsm_rndup(len));