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.17 2004/06/06 19:16:11 dillon 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/namei.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 static MALLOC_DEFINE(M_NFSSVC, "NFS srvsock", "Nfs server structure");
78 extern int32_t (*nfsrv3_procs[NFS_NPROCS]) (struct nfsrv_descript *nd,
79 struct nfssvc_sock *slp,
82 extern int nfs_numasync;
83 extern time_t nqnfsstarttime;
84 extern int nqsrv_writeslack;
86 extern struct nfsstats nfsstats;
87 extern int nfsrvw_procrastinate;
88 extern int nfsrvw_procrastinate_v3;
89 static int nuidhash_max = NFS_MAXUIDHASH;
92 static void nfsrv_zapsock (struct nfssvc_sock *slp);
94 static int nfssvc_iod (struct thread *);
99 static int nfs_asyncdaemon[NFS_MAXASYNCDAEMON];
101 SYSCTL_DECL(_vfs_nfs);
104 int nfsd_waiting = 0;
105 static struct nfsdrt nfsdrt;
106 static int nfs_numnfsd = 0;
107 static int notstarted = 1;
108 static int modify_flag = 0;
109 static void nfsd_rt (int sotype, struct nfsrv_descript *nd,
111 static int nfssvc_addsock (struct file *, struct sockaddr *,
113 static int nfssvc_nfsd (struct nfsd_srvargs *,caddr_t,struct thread *);
115 static int nfs_privport = 0;
116 SYSCTL_INT(_vfs_nfs, NFS_NFSPRIVPORT, nfs_privport, CTLFLAG_RW, &nfs_privport, 0, "");
117 SYSCTL_INT(_vfs_nfs, OID_AUTO, gatherdelay, CTLFLAG_RW, &nfsrvw_procrastinate, 0, "");
118 SYSCTL_INT(_vfs_nfs, OID_AUTO, gatherdelay_v3, CTLFLAG_RW, &nfsrvw_procrastinate_v3, 0, "");
119 static int nfs_soreserve = 65535;
120 SYSCTL_INT(_vfs_nfs, OID_AUTO, soreserve, CTLFLAG_RW, &nfs_soreserve, 0, "");
123 * NFS server system calls
126 #endif /* NFS_NOSERVER */
128 * nfssvc_args(int flag, caddr_t argp)
130 * Nfs server psuedo system call for the nfsd's
131 * Based on the flag value it either:
132 * - adds a socket to the selection list
133 * - remains in the kernel as an nfsd
134 * - remains in the kernel as an nfsiod
137 nfssvc(struct nfssvc_args *uap)
142 struct sockaddr *nam;
143 struct nfsd_args nfsdarg;
144 struct nfsd_srvargs nfsd_srvargs, *nsd = &nfsd_srvargs;
145 struct nfsd_cargs ncd;
147 struct nfssvc_sock *slp;
148 struct nfsuid *nuidp;
149 struct nfsmount *nmp;
150 #endif /* NFS_NOSERVER */
152 struct thread *td = curthread;
160 KKASSERT(td->td_proc); /* for ucred and p_fd */
161 while (nfssvc_sockhead_flag & SLP_INIT) {
162 nfssvc_sockhead_flag |= SLP_WANTINIT;
163 (void) tsleep((caddr_t)&nfssvc_sockhead, 0, "nfsd init", 0);
165 if (uap->flag & NFSSVC_BIOD)
166 error = nfssvc_iod(td);
170 #else /* !NFS_NOSERVER */
171 else if (uap->flag & NFSSVC_MNTD) {
172 error = copyin(uap->argp, (caddr_t)&ncd, sizeof (ncd));
175 NDINIT(&nd, NAMEI_LOOKUP, CNP_FOLLOW | CNP_LOCKLEAF,
176 UIO_USERSPACE, ncd.ncd_dirp, td);
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, td->td_proc->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(td->td_proc->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, td);
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->nd_nam2);
293 TAILQ_INSERT_TAIL(&slp->ns_uidlruhead, nuidp,
295 LIST_INSERT_HEAD(NUIDHASH(slp, nsd->nsd_uid),
297 nfsrv_setcred(&nuidp->nu_cr,
298 &nfsd->nfsd_nd->nd_cr);
299 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
303 if ((uap->flag & NFSSVC_AUTHINFAIL) && (nfsd = nsd->nsd_nfsd))
304 nfsd->nfsd_flag |= NFSD_AUTHFAIL;
305 error = nfssvc_nfsd(nsd, uap->argp, td);
307 #endif /* NFS_NOSERVER */
308 if (error == EINTR || error == ERESTART)
315 * Adds a socket to the list for servicing by nfsds.
318 nfssvc_addsock(struct file *fp, struct sockaddr *mynam, struct thread *td)
321 struct nfssvc_sock *slp;
325 so = (struct socket *)fp->f_data;
327 tslp = (struct nfssvc_sock *)0;
329 * Add it to the list, as required.
331 if (so->so_proto->pr_protocol == IPPROTO_UDP) {
333 if (tslp->ns_flag & SLP_VALID) {
335 FREE(mynam, M_SONAME);
341 * Reserve buffer space in the socket. Note that due to bugs in
342 * Linux's delayed-ack code, serious performance degredation may
343 * occur with linux hosts if the minimum is used.
345 if (so->so_type == SOCK_STREAM)
346 siz = NFS_MAXPACKET + sizeof (u_long);
349 if (siz < nfs_soreserve)
351 if (siz > sb_max_adj) {
352 printf("Warning: vfs.nfs.soreserve (%d) "
353 "limited to adjusted sb_max (%ld)\n",
354 nfs_soreserve, sb_max_adj);
358 error = soreserve(so, siz, siz, &td->td_proc->p_rlimit[RLIMIT_SBSIZE]);
361 FREE(mynam, M_SONAME);
366 * Set protocol specific options { for now TCP only } and
367 * reserve some space. For datagram sockets, this can get called
368 * repeatedly for the same socket, but that isn't harmful.
370 if (so->so_type == SOCK_STREAM) {
374 bzero(&sopt, sizeof sopt);
375 sopt.sopt_level = SOL_SOCKET;
376 sopt.sopt_name = SO_KEEPALIVE;
377 sopt.sopt_val = &val;
378 sopt.sopt_valsize = sizeof val;
382 if (so->so_proto->pr_domain->dom_family == AF_INET &&
383 so->so_proto->pr_protocol == IPPROTO_TCP) {
387 bzero(&sopt, sizeof sopt);
388 sopt.sopt_level = IPPROTO_TCP;
389 sopt.sopt_name = TCP_NODELAY;
390 sopt.sopt_val = &val;
391 sopt.sopt_valsize = sizeof val;
395 so->so_rcv.sb_flags &= ~SB_NOINTR;
396 so->so_rcv.sb_timeo = 0;
397 so->so_snd.sb_flags &= ~SB_NOINTR;
398 so->so_snd.sb_timeo = 0;
400 slp = (struct nfssvc_sock *)
401 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
402 bzero((caddr_t)slp, sizeof (struct nfssvc_sock));
403 STAILQ_INIT(&slp->ns_rec);
404 TAILQ_INIT(&slp->ns_uidlruhead);
405 TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
412 so->so_upcallarg = (caddr_t)slp;
413 so->so_upcall = nfsrv_rcv;
414 so->so_rcv.sb_flags |= SB_UPCALL;
415 slp->ns_flag = (SLP_VALID | SLP_NEEDQ);
422 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
423 * until it is killed by a signal.
426 nfssvc_nfsd(struct nfsd_srvargs *nsd, caddr_t argp, struct thread *td)
429 struct nfssvc_sock *slp;
430 struct nfsd *nfsd = nsd->nsd_nfsd;
431 struct nfsrv_descript *nd = NULL;
432 struct mbuf *m, *mreq;
433 int error = 0, cacherep, s, sotype, writes_todo;
441 if (nfsd == (struct nfsd *)0) {
442 nsd->nsd_nfsd = nfsd = (struct nfsd *)
443 malloc(sizeof (struct nfsd), M_NFSD, M_WAITOK);
444 bzero((caddr_t)nfsd, sizeof (struct nfsd));
447 TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain);
453 * Loop getting rpc requests until SIGKILL.
456 if ((nfsd->nfsd_flag & NFSD_REQINPROG) == 0) {
457 while (nfsd->nfsd_slp == (struct nfssvc_sock *)0 &&
458 (nfsd_head_flag & NFSD_CHECKSLP) == 0) {
459 nfsd->nfsd_flag |= NFSD_WAITING;
461 error = tsleep((caddr_t)nfsd, PCATCH, "nfsd", 0);
466 if (nfsd->nfsd_slp == (struct nfssvc_sock *)0 &&
467 (nfsd_head_flag & NFSD_CHECKSLP) != 0) {
468 for (slp = nfssvc_sockhead.tqh_first; slp != 0;
469 slp = slp->ns_chain.tqe_next) {
470 if ((slp->ns_flag & (SLP_VALID | SLP_DOREC))
471 == (SLP_VALID | SLP_DOREC)) {
472 slp->ns_flag &= ~SLP_DOREC;
474 nfsd->nfsd_slp = slp;
479 nfsd_head_flag &= ~NFSD_CHECKSLP;
481 if ((slp = nfsd->nfsd_slp) == (struct nfssvc_sock *)0)
483 if (slp->ns_flag & SLP_VALID) {
484 if (slp->ns_flag & SLP_DISCONN)
486 else if (slp->ns_flag & SLP_NEEDQ) {
487 slp->ns_flag &= ~SLP_NEEDQ;
488 (void) nfs_slplock(slp, 1);
489 nfsrv_rcv(slp->ns_so, (caddr_t)slp,
493 error = nfsrv_dorec(slp, nfsd, &nd);
494 cur_usec = nfs_curusec();
495 if (error && slp->ns_tq.lh_first &&
496 slp->ns_tq.lh_first->nd_time <= cur_usec) {
502 nfsd->nfsd_flag |= NFSD_REQINPROG;
506 slp = nfsd->nfsd_slp;
508 if (error || (slp->ns_flag & SLP_VALID) == 0) {
510 free((caddr_t)nd, M_NFSRVDESC);
513 nfsd->nfsd_slp = (struct nfssvc_sock *)0;
514 nfsd->nfsd_flag &= ~NFSD_REQINPROG;
519 sotype = slp->ns_so->so_type;
521 getmicrotime(&nd->nd_starttime);
523 nd->nd_nam = nd->nd_nam2;
525 nd->nd_nam = slp->ns_nam;
528 * Check to see if authorization is needed.
530 if (nfsd->nfsd_flag & NFSD_NEEDAUTH) {
531 nfsd->nfsd_flag &= ~NFSD_NEEDAUTH;
533 ((struct sockaddr_in *)
534 nd->nd_nam)->sin_addr.s_addr;
535 nsd->nsd_authlen = nfsd->nfsd_authlen;
536 nsd->nsd_verflen = nfsd->nfsd_verflen;
537 if (!copyout(nfsd->nfsd_authstr,nsd->nsd_authstr,
538 nfsd->nfsd_authlen) &&
539 !copyout(nfsd->nfsd_verfstr, nsd->nsd_verfstr,
540 nfsd->nfsd_verflen) &&
541 !copyout((caddr_t)nsd, argp, sizeof (*nsd)))
543 cacherep = RC_DROPIT;
545 cacherep = nfsrv_getcache(nd, slp, &mreq);
548 * Check for just starting up for NQNFS and send
549 * fake "try again later" replies to the NQNFS clients.
551 if (notstarted && nqnfsstarttime <= time_second) {
553 nqnfsstarttime = time_second + nqsrv_writeslack;
559 if ((nd->nd_flag & ND_NQNFS) == 0)
560 cacherep = RC_DROPIT;
561 else if (nd->nd_procnum != NFSPROC_WRITE) {
562 nd->nd_procnum = NFSPROC_NOOP;
563 nd->nd_repstat = NQNFS_TRYLATER;
567 } else if (nfsd->nfsd_flag & NFSD_AUTHFAIL) {
568 nfsd->nfsd_flag &= ~NFSD_AUTHFAIL;
569 nd->nd_procnum = NFSPROC_NOOP;
570 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
572 } else if (nfs_privport) {
573 /* Check if source port is privileged */
575 struct sockaddr *nam = nd->nd_nam;
576 struct sockaddr_in *sin;
578 sin = (struct sockaddr_in *)nam;
579 port = ntohs(sin->sin_port);
580 if (port >= IPPORT_RESERVED &&
581 nd->nd_procnum != NFSPROC_NULL) {
582 nd->nd_procnum = NFSPROC_NOOP;
583 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
585 printf("NFS request from unprivileged port (%s:%d)\n",
586 inet_ntoa(sin->sin_addr), port);
593 * Loop to get all the write rpc relies that have been
599 if (nd && (nd->nd_flag & ND_NFSV3))
600 procrastinate = nfsrvw_procrastinate_v3;
602 procrastinate = nfsrvw_procrastinate;
603 if (writes_todo || (nd->nd_procnum == NFSPROC_WRITE &&
604 procrastinate > 0 && !notstarted))
605 error = nfsrv_writegather(&nd, slp,
606 nfsd->nfsd_td, &mreq);
608 error = (*(nfsrv3_procs[nd->nd_procnum]))(nd,
609 slp, nfsd->nfsd_td, &mreq);
612 if (error != 0 && error != NFSERR_RETVOID) {
613 if (nd->nd_procnum != NQNFSPROC_VACATED)
615 nfsrv_updatecache(nd, FALSE, mreq);
617 FREE(nd->nd_nam2, M_SONAME);
620 nfsstats.srvrpccnt[nd->nd_procnum]++;
621 nfsrv_updatecache(nd, TRUE, mreq);
622 nd->nd_mrep = (struct mbuf *)0;
630 if (siz <= 0 || siz > NFS_MAXPACKET) {
631 printf("mbuf siz=%d\n",siz);
632 panic("Bad nfs svc reply");
635 m->m_pkthdr.len = siz;
636 m->m_pkthdr.rcvif = (struct ifnet *)0;
638 * For stream protocols, prepend a Sun RPC
641 if (sotype == SOCK_STREAM) {
642 M_PREPEND(m, NFSX_UNSIGNED, MB_WAIT);
645 *mtod(m, u_int32_t *) = htonl(0x80000000 | siz);
647 if (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED)
648 (void) nfs_slplock(slp, 1);
649 if (slp->ns_flag & SLP_VALID)
650 error = nfs_send(slp->ns_so, nd->nd_nam2, m, NULL);
656 nfsd_rt(sotype, nd, cacherep);
658 FREE(nd->nd_nam2, M_SONAME);
660 m_freem(nd->nd_mrep);
663 if (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED)
665 if (error == EINTR || error == ERESTART) {
666 free((caddr_t)nd, M_NFSRVDESC);
674 nfsd_rt(sotype, nd, cacherep);
675 m_freem(nd->nd_mrep);
677 FREE(nd->nd_nam2, M_SONAME);
681 FREE((caddr_t)nd, M_NFSRVDESC);
686 * Check to see if there are outstanding writes that
687 * need to be serviced.
689 cur_usec = nfs_curusec();
691 if (slp->ns_tq.lh_first &&
692 slp->ns_tq.lh_first->nd_time <= cur_usec) {
698 } while (writes_todo);
700 if (nfsrv_dorec(slp, nfsd, &nd)) {
701 nfsd->nfsd_flag &= ~NFSD_REQINPROG;
702 nfsd->nfsd_slp = NULL;
707 TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
709 free((caddr_t)nfsd, M_NFSD);
710 nsd->nsd_nfsd = (struct nfsd *)0;
711 if (--nfs_numnfsd == 0)
712 nfsrv_init(TRUE); /* Reinitialize everything */
717 * Shut down a socket associated with an nfssvc_sock structure.
718 * Should be called with the send lock set, if required.
719 * The trick here is to increment the sref at the start, so that the nfsds
720 * will stop using it and clear ns_flag at the end so that it will not be
721 * reassigned during cleanup.
724 nfsrv_zapsock(struct nfssvc_sock *slp)
726 struct nfsuid *nuidp, *nnuidp;
727 struct nfsrv_descript *nwp, *nnwp;
730 struct nfsrv_rec *rec;
733 slp->ns_flag &= ~SLP_ALLFLAGS;
736 slp->ns_fp = (struct file *)0;
738 so->so_rcv.sb_flags &= ~SB_UPCALL;
739 so->so_upcall = NULL;
740 so->so_upcallarg = NULL;
744 FREE(slp->ns_nam, M_SONAME);
745 m_freem(slp->ns_raw);
746 while ((rec = STAILQ_FIRST(&slp->ns_rec)) != NULL) {
747 STAILQ_REMOVE_HEAD(&slp->ns_rec, nr_link);
749 FREE(rec->nr_address, M_SONAME);
750 m_freem(rec->nr_packet);
751 free(rec, M_NFSRVDESC);
753 for (nuidp = slp->ns_uidlruhead.tqh_first; nuidp != 0;
755 nnuidp = nuidp->nu_lru.tqe_next;
756 LIST_REMOVE(nuidp, nu_hash);
757 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru);
758 if (nuidp->nu_flag & NU_NAM)
759 FREE(nuidp->nu_nam, M_SONAME);
760 free((caddr_t)nuidp, M_NFSUID);
763 for (nwp = slp->ns_tq.lh_first; nwp; nwp = nnwp) {
764 nnwp = nwp->nd_tq.le_next;
765 LIST_REMOVE(nwp, nd_tq);
766 free((caddr_t)nwp, M_NFSRVDESC);
768 LIST_INIT(&slp->ns_tq);
774 * Derefence a server socket structure. If it has no more references and
775 * is no longer valid, you can throw it away.
778 nfsrv_slpderef(struct nfssvc_sock *slp)
780 if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) {
781 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
782 free((caddr_t)slp, M_NFSSVC);
787 * Lock a socket against others.
790 nfs_slplock(struct nfssvc_sock *slp, int wait)
792 int *statep = &slp->ns_solock;
794 if (!wait && (*statep & NFSSTA_SNDLOCK))
795 return(0); /* already locked, fail */
796 while (*statep & NFSSTA_SNDLOCK) {
797 *statep |= NFSSTA_WANTSND;
798 (void) tsleep((caddr_t)statep, 0, "nfsslplck", 0);
800 *statep |= NFSSTA_SNDLOCK;
805 * Unlock the stream socket for others.
808 nfs_slpunlock(struct nfssvc_sock *slp)
810 int *statep = &slp->ns_solock;
812 if ((*statep & NFSSTA_SNDLOCK) == 0)
813 panic("nfs slpunlock");
814 *statep &= ~NFSSTA_SNDLOCK;
815 if (*statep & NFSSTA_WANTSND) {
816 *statep &= ~NFSSTA_WANTSND;
817 wakeup((caddr_t)statep);
822 * Initialize the data structures for the server.
823 * Handshake with any new nfsds starting up to avoid any chance of
827 nfsrv_init(int terminating)
829 struct nfssvc_sock *slp, *nslp;
831 if (nfssvc_sockhead_flag & SLP_INIT)
833 nfssvc_sockhead_flag |= SLP_INIT;
835 for (slp = nfssvc_sockhead.tqh_first; slp != 0; slp = nslp) {
836 nslp = slp->ns_chain.tqe_next;
837 if (slp->ns_flag & SLP_VALID)
839 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
840 free((caddr_t)slp, M_NFSSVC);
842 nfsrv_cleancache(); /* And clear out server cache */
844 nfs_pub.np_valid = 0;
846 TAILQ_INIT(&nfssvc_sockhead);
847 nfssvc_sockhead_flag &= ~SLP_INIT;
848 if (nfssvc_sockhead_flag & SLP_WANTINIT) {
849 nfssvc_sockhead_flag &= ~SLP_WANTINIT;
850 wakeup((caddr_t)&nfssvc_sockhead);
853 TAILQ_INIT(&nfsd_head);
854 nfsd_head_flag &= ~NFSD_CHECKSLP;
857 nfs_udpsock = (struct nfssvc_sock *)
858 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
859 bzero((caddr_t)nfs_udpsock, sizeof (struct nfssvc_sock));
860 STAILQ_INIT(&nfs_udpsock->ns_rec);
861 TAILQ_INIT(&nfs_udpsock->ns_uidlruhead);
862 TAILQ_INSERT_HEAD(&nfssvc_sockhead, nfs_udpsock, ns_chain);
864 nfs_cltpsock = (struct nfssvc_sock *)
865 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
866 bzero((caddr_t)nfs_cltpsock, sizeof (struct nfssvc_sock));
867 STAILQ_INIT(&nfs_cltpsock->ns_rec);
868 TAILQ_INIT(&nfs_cltpsock->ns_uidlruhead);
869 TAILQ_INSERT_TAIL(&nfssvc_sockhead, nfs_cltpsock, ns_chain);
874 * Add entries to the server monitor log.
877 nfsd_rt(int sotype, struct nfsrv_descript *nd, int cacherep)
881 rt = &nfsdrt.drt[nfsdrt.pos];
882 if (cacherep == RC_DOIT)
884 else if (cacherep == RC_REPLY)
885 rt->flag = DRT_CACHEREPLY;
887 rt->flag = DRT_CACHEDROP;
888 if (sotype == SOCK_STREAM)
890 if (nd->nd_flag & ND_NQNFS)
891 rt->flag |= DRT_NQNFS;
892 else if (nd->nd_flag & ND_NFSV3)
893 rt->flag |= DRT_NFSV3;
894 rt->proc = nd->nd_procnum;
895 if (nd->nd_nam->sa_family == AF_INET)
896 rt->ipadr = ((struct sockaddr_in *)nd->nd_nam)->sin_addr.s_addr;
898 rt->ipadr = INADDR_ANY;
899 rt->resptime = nfs_curusec() - (nd->nd_starttime.tv_sec * 1000000 + nd->nd_starttime.tv_usec);
900 getmicrotime(&rt->tstamp);
901 nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ;
903 #endif /* NFS_NOSERVER */
905 static int nfs_defect = 0;
906 SYSCTL_INT(_vfs_nfs, OID_AUTO, defect, CTLFLAG_RW, &nfs_defect, 0, "");
909 * Asynchronous I/O daemons for client nfs.
910 * They do read-ahead and write-behind operations on the block I/O cache.
911 * Never returns unless it fails or gets killed.
914 nfssvc_iod(struct thread *td)
918 struct nfsmount *nmp;
922 * Assign my position or return error if too many already running
925 for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
926 if (nfs_asyncdaemon[i] == 0) {
927 nfs_asyncdaemon[i]++;
935 * Just loop around doin our stuff until SIGKILL
938 while (((nmp = nfs_iodmount[myiod]) == NULL
939 || nmp->nm_bufq.tqh_first == NULL)
943 nfs_iodwant[myiod] = td;
944 nfs_iodmount[myiod] = NULL;
945 error = tsleep((caddr_t)&nfs_iodwant[myiod],
946 PCATCH, "nfsidl", 0);
949 nfs_asyncdaemon[myiod] = 0;
952 nfs_iodwant[myiod] = NULL;
953 nfs_iodmount[myiod] = NULL;
957 while ((bp = nmp->nm_bufq.tqh_first) != NULL) {
958 /* Take one off the front of the list */
959 TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist);
961 if (nmp->nm_bufqwant && nmp->nm_bufqlen <= nfs_numasync) {
962 nmp->nm_bufqwant = FALSE;
963 wakeup(&nmp->nm_bufq);
965 (void) nfs_doio(bp, NULL);
967 * If there are more than one iod on this mount, then defect
968 * so that the iods can be shared out fairly between the mounts
970 if (nfs_defect && nmp->nm_bufqiods > 1) {
972 ("nfssvc_iod: iod %d defecting from mount %p\n",
974 nfs_iodmount[myiod] = NULL;
984 * Get an authorization string for the uid by having the mount_nfs sitting
985 * on this mount point porpous out of the kernel and do it.
988 nfs_getauth(struct nfsmount *nmp, struct nfsreq *rep,
989 struct ucred *cred, char **auth_str, int *auth_len, char *verf_str,
990 int *verf_len, NFSKERBKEY_T key /* return session key */)
994 while ((nmp->nm_state & NFSSTA_WAITAUTH) == 0) {
995 nmp->nm_state |= NFSSTA_WANTAUTH;
996 (void) tsleep((caddr_t)&nmp->nm_authtype, 0,
998 error = nfs_sigintr(nmp, rep, rep->r_td);
1000 nmp->nm_state &= ~NFSSTA_WANTAUTH;
1004 nmp->nm_state &= ~(NFSSTA_WAITAUTH | NFSSTA_WANTAUTH);
1005 nmp->nm_authstr = *auth_str = (char *)malloc(RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK);
1006 nmp->nm_authlen = RPCAUTH_MAXSIZ;
1007 nmp->nm_verfstr = verf_str;
1008 nmp->nm_verflen = *verf_len;
1009 nmp->nm_authuid = cred->cr_uid;
1010 wakeup((caddr_t)&nmp->nm_authstr);
1013 * And wait for mount_nfs to do its stuff.
1015 while ((nmp->nm_state & NFSSTA_HASAUTH) == 0 && error == 0) {
1016 (void) tsleep((caddr_t)&nmp->nm_authlen, 0,
1017 "nfsauth2", 2 * hz);
1018 error = nfs_sigintr(nmp, rep, rep->r_td);
1020 if (nmp->nm_state & NFSSTA_AUTHERR) {
1021 nmp->nm_state &= ~NFSSTA_AUTHERR;
1025 free((caddr_t)*auth_str, M_TEMP);
1027 *auth_len = nmp->nm_authlen;
1028 *verf_len = nmp->nm_verflen;
1029 bcopy((caddr_t)nmp->nm_key, (caddr_t)key, sizeof (key));
1031 nmp->nm_state &= ~NFSSTA_HASAUTH;
1032 nmp->nm_state |= NFSSTA_WAITAUTH;
1033 if (nmp->nm_state & NFSSTA_WANTAUTH) {
1034 nmp->nm_state &= ~NFSSTA_WANTAUTH;
1035 wakeup((caddr_t)&nmp->nm_authtype);
1041 * Get a nickname authenticator and verifier.
1044 nfs_getnickauth(struct nfsmount *nmp, struct ucred *cred, char **auth_str,
1045 int *auth_len, char *verf_str, int verf_len)
1047 struct nfsuid *nuidp;
1048 u_int32_t *nickp, *verfp;
1049 struct timeval ktvin, ktvout;
1052 if (verf_len < (4 * NFSX_UNSIGNED))
1053 panic("nfs_getnickauth verf too small");
1055 for (nuidp = NMUIDHASH(nmp, cred->cr_uid)->lh_first;
1056 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
1057 if (nuidp->nu_cr.cr_uid == cred->cr_uid)
1060 if (!nuidp || nuidp->nu_expire < time_second)
1064 * Move to the end of the lru list (end of lru == most recently used).
1066 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
1067 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, nu_lru);
1069 nickp = (u_int32_t *)malloc(2 * NFSX_UNSIGNED, M_TEMP, M_WAITOK);
1070 *nickp++ = txdr_unsigned(RPCAKN_NICKNAME);
1071 *nickp = txdr_unsigned(nuidp->nu_nickname);
1072 *auth_str = (char *)nickp;
1073 *auth_len = 2 * NFSX_UNSIGNED;
1076 * Now we must encrypt the verifier and package it up.
1078 verfp = (u_int32_t *)verf_str;
1079 *verfp++ = txdr_unsigned(RPCAKN_NICKNAME);
1080 if (time_second > nuidp->nu_timestamp.tv_sec ||
1081 (time_second == nuidp->nu_timestamp.tv_sec &&
1082 time_second > nuidp->nu_timestamp.tv_usec))
1083 getmicrotime(&nuidp->nu_timestamp);
1085 nuidp->nu_timestamp.tv_usec++;
1086 ktvin.tv_sec = txdr_unsigned(nuidp->nu_timestamp.tv_sec);
1087 ktvin.tv_usec = txdr_unsigned(nuidp->nu_timestamp.tv_usec);
1090 * Now encrypt the timestamp verifier in ecb mode using the session
1097 *verfp++ = ktvout.tv_sec;
1098 *verfp++ = ktvout.tv_usec;
1104 * Save the current nickname in a hash list entry on the mount point.
1107 nfs_savenickauth(struct nfsmount *nmp, struct ucred *cred, int len,
1108 NFSKERBKEY_T key, struct mbuf **mdp, char **dposp,
1111 struct nfsuid *nuidp;
1114 struct mbuf *md = *mdp;
1115 struct timeval ktvin, ktvout;
1117 char *dpos = *dposp, *cp2;
1118 int deltasec, error = 0;
1120 if (len == (3 * NFSX_UNSIGNED)) {
1121 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1122 ktvin.tv_sec = *tl++;
1123 ktvin.tv_usec = *tl++;
1124 nick = fxdr_unsigned(u_int32_t, *tl);
1127 * Decrypt the timestamp in ecb mode.
1132 ktvout.tv_sec = fxdr_unsigned(long, ktvout.tv_sec);
1133 ktvout.tv_usec = fxdr_unsigned(long, ktvout.tv_usec);
1134 deltasec = time_second - ktvout.tv_sec;
1136 deltasec = -deltasec;
1138 * If ok, add it to the hash list for the mount point.
1140 if (deltasec <= NFS_KERBCLOCKSKEW) {
1141 if (nmp->nm_numuids < nuidhash_max) {
1143 nuidp = (struct nfsuid *)
1144 malloc(sizeof (struct nfsuid), M_NFSUID,
1147 nuidp = nmp->nm_uidlruhead.tqh_first;
1148 LIST_REMOVE(nuidp, nu_hash);
1149 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp,
1153 nuidp->nu_cr.cr_uid = cred->cr_uid;
1154 nuidp->nu_expire = time_second + NFS_KERBTTL;
1155 nuidp->nu_timestamp = ktvout;
1156 nuidp->nu_nickname = nick;
1157 bcopy(key, nuidp->nu_key, sizeof (key));
1158 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp,
1160 LIST_INSERT_HEAD(NMUIDHASH(nmp, cred->cr_uid),
1164 nfsm_adv(nfsm_rndup(len));