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.20 2005/03/27 23:51:42 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/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 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)
140 struct nlookupdata nd;
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;
151 #endif /* NFS_NOSERVER */
153 struct thread *td = curthread;
161 KKASSERT(td->td_proc); /* for ucred and p_fd */
162 while (nfssvc_sockhead_flag & SLP_INIT) {
163 nfssvc_sockhead_flag |= SLP_WANTINIT;
164 (void) tsleep((caddr_t)&nfssvc_sockhead, 0, "nfsd init", 0);
166 if (uap->flag & NFSSVC_BIOD)
167 error = nfssvc_iod(td);
171 #else /* !NFS_NOSERVER */
172 else if (uap->flag & NFSSVC_MNTD) {
173 error = copyin(uap->argp, (caddr_t)&ncd, sizeof (ncd));
177 error = nlookup_init(&nd, ncd.ncd_dirp, UIO_USERSPACE,
180 error = nlookup(&nd);
182 error = cache_vget(nd.nl_ncp, nd.nl_cred, LK_EXCLUSIVE, &vp);
187 if ((vp->v_flag & VROOT) == 0)
189 nmp = VFSTONFS(vp->v_mount);
193 if ((nmp->nm_state & NFSSTA_MNTD) &&
194 (uap->flag & NFSSVC_GOTAUTH) == 0)
196 nmp->nm_state |= NFSSTA_MNTD;
197 error = nqnfs_clientd(nmp, td->td_proc->p_ucred, &ncd, uap->flag,
199 } else if (uap->flag & NFSSVC_ADDSOCK) {
200 error = copyin(uap->argp, (caddr_t)&nfsdarg, sizeof(nfsdarg));
203 error = holdsock(td->td_proc->p_fd, nfsdarg.sock, &fp);
207 * Get the client address for connected sockets.
209 if (nfsdarg.name == NULL || nfsdarg.namelen == 0)
210 nam = (struct sockaddr *)0;
212 error = getsockaddr(&nam, nfsdarg.name,
219 error = nfssvc_addsock(fp, nam, td);
222 error = copyin(uap->argp, (caddr_t)nsd, sizeof (*nsd));
225 if ((uap->flag & NFSSVC_AUTHIN) &&
226 ((nfsd = nsd->nsd_nfsd)) != NULL &&
227 (nfsd->nfsd_slp->ns_flag & SLP_VALID)) {
228 slp = nfsd->nfsd_slp;
231 * First check to see if another nfsd has already
232 * added this credential.
234 for (nuidp = NUIDHASH(slp,nsd->nsd_cr.cr_uid)->lh_first;
235 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
236 if (nuidp->nu_cr.cr_uid == nsd->nsd_cr.cr_uid &&
237 (!nfsd->nfsd_nd->nd_nam2 ||
238 netaddr_match(NU_NETFAM(nuidp),
239 &nuidp->nu_haddr, nfsd->nfsd_nd->nd_nam2)))
243 nfsrv_setcred(&nuidp->nu_cr,&nfsd->nfsd_nd->nd_cr);
244 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
249 if (slp->ns_numuids < nuidhash_max) {
251 nuidp = (struct nfsuid *)
252 malloc(sizeof (struct nfsuid), M_NFSUID,
255 nuidp = (struct nfsuid *)0;
256 if ((slp->ns_flag & SLP_VALID) == 0) {
258 free((caddr_t)nuidp, M_NFSUID);
260 if (nuidp == (struct nfsuid *)0) {
261 nuidp = TAILQ_FIRST(&slp->ns_uidlruhead);
262 LIST_REMOVE(nuidp, nu_hash);
263 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp,
265 if (nuidp->nu_flag & NU_NAM)
266 FREE(nuidp->nu_nam, M_SONAME);
269 nuidp->nu_cr = nsd->nsd_cr;
270 if (nuidp->nu_cr.cr_ngroups > NGROUPS)
271 nuidp->nu_cr.cr_ngroups = NGROUPS;
272 nuidp->nu_cr.cr_ref = 1;
273 nuidp->nu_timestamp = nsd->nsd_timestamp;
274 nuidp->nu_expire = time_second + nsd->nsd_ttl;
276 * and save the session key in nu_key.
278 bcopy(nsd->nsd_key, nuidp->nu_key,
279 sizeof (nsd->nsd_key));
280 if (nfsd->nfsd_nd->nd_nam2) {
281 struct sockaddr_in *saddr;
283 saddr = (struct sockaddr_in *)
284 nfsd->nfsd_nd->nd_nam2;
285 switch (saddr->sin_family) {
287 nuidp->nu_flag |= NU_INETADDR;
289 saddr->sin_addr.s_addr;
293 nuidp->nu_flag |= NU_NAM;
295 dup_sockaddr(nfsd->nfsd_nd->nd_nam2);
299 TAILQ_INSERT_TAIL(&slp->ns_uidlruhead, nuidp,
301 LIST_INSERT_HEAD(NUIDHASH(slp, nsd->nsd_uid),
303 nfsrv_setcred(&nuidp->nu_cr,
304 &nfsd->nfsd_nd->nd_cr);
305 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
309 if ((uap->flag & NFSSVC_AUTHINFAIL) && (nfsd = nsd->nsd_nfsd))
310 nfsd->nfsd_flag |= NFSD_AUTHFAIL;
311 error = nfssvc_nfsd(nsd, uap->argp, td);
313 #endif /* NFS_NOSERVER */
314 if (error == EINTR || error == ERESTART)
321 * Adds a socket to the list for servicing by nfsds.
324 nfssvc_addsock(struct file *fp, struct sockaddr *mynam, struct thread *td)
327 struct nfssvc_sock *slp;
331 so = (struct socket *)fp->f_data;
333 tslp = (struct nfssvc_sock *)0;
335 * Add it to the list, as required.
337 if (so->so_proto->pr_protocol == IPPROTO_UDP) {
339 if (tslp->ns_flag & SLP_VALID) {
341 FREE(mynam, M_SONAME);
347 * Reserve buffer space in the socket. Note that due to bugs in
348 * Linux's delayed-ack code, serious performance degredation may
349 * occur with linux hosts if the minimum is used.
351 if (so->so_type == SOCK_STREAM)
352 siz = NFS_MAXPACKET + sizeof (u_long);
355 if (siz < nfs_soreserve)
357 if (siz > sb_max_adj) {
358 printf("Warning: vfs.nfs.soreserve (%d) "
359 "limited to adjusted sb_max (%ld)\n",
360 nfs_soreserve, sb_max_adj);
364 error = soreserve(so, siz, siz, &td->td_proc->p_rlimit[RLIMIT_SBSIZE]);
367 FREE(mynam, M_SONAME);
372 * Set protocol specific options { for now TCP only } and
373 * reserve some space. For datagram sockets, this can get called
374 * repeatedly for the same socket, but that isn't harmful.
376 if (so->so_type == SOCK_STREAM) {
380 bzero(&sopt, sizeof sopt);
381 sopt.sopt_level = SOL_SOCKET;
382 sopt.sopt_name = SO_KEEPALIVE;
383 sopt.sopt_val = &val;
384 sopt.sopt_valsize = sizeof val;
388 if (so->so_proto->pr_domain->dom_family == AF_INET &&
389 so->so_proto->pr_protocol == IPPROTO_TCP) {
393 bzero(&sopt, sizeof sopt);
394 sopt.sopt_level = IPPROTO_TCP;
395 sopt.sopt_name = TCP_NODELAY;
396 sopt.sopt_val = &val;
397 sopt.sopt_valsize = sizeof val;
401 so->so_rcv.sb_flags &= ~SB_NOINTR;
402 so->so_rcv.sb_timeo = 0;
403 so->so_snd.sb_flags &= ~SB_NOINTR;
404 so->so_snd.sb_timeo = 0;
406 slp = (struct nfssvc_sock *)
407 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
408 bzero((caddr_t)slp, sizeof (struct nfssvc_sock));
409 STAILQ_INIT(&slp->ns_rec);
410 TAILQ_INIT(&slp->ns_uidlruhead);
411 TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
418 so->so_upcallarg = (caddr_t)slp;
419 so->so_upcall = nfsrv_rcv;
420 so->so_rcv.sb_flags |= SB_UPCALL;
421 slp->ns_flag = (SLP_VALID | SLP_NEEDQ);
422 nfsrv_wakenfsd(slp, 1);
428 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
429 * until it is killed by a signal.
432 nfssvc_nfsd(struct nfsd_srvargs *nsd, caddr_t argp, struct thread *td)
435 struct nfssvc_sock *slp;
436 struct nfsd *nfsd = nsd->nsd_nfsd;
437 struct nfsrv_descript *nd = NULL;
438 struct mbuf *m, *mreq;
439 int error = 0, cacherep, s, sotype, writes_todo;
447 if (nfsd == (struct nfsd *)0) {
448 nsd->nsd_nfsd = nfsd = (struct nfsd *)
449 malloc(sizeof (struct nfsd), M_NFSD, M_WAITOK);
450 bzero((caddr_t)nfsd, sizeof (struct nfsd));
453 TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain);
459 * Loop getting rpc requests until SIGKILL.
462 if ((nfsd->nfsd_flag & NFSD_REQINPROG) == 0) {
463 while (nfsd->nfsd_slp == NULL &&
464 (nfsd_head_flag & NFSD_CHECKSLP) == 0) {
465 nfsd->nfsd_flag |= NFSD_WAITING;
467 error = tsleep((caddr_t)nfsd, PCATCH, "nfsd", 0);
472 if (nfsd->nfsd_slp == NULL &&
473 (nfsd_head_flag & NFSD_CHECKSLP) != 0) {
474 TAILQ_FOREACH(slp, &nfssvc_sockhead, ns_chain) {
475 if ((slp->ns_flag & (SLP_VALID | SLP_DOREC))
476 == (SLP_VALID | SLP_DOREC)) {
477 slp->ns_flag &= ~SLP_DOREC;
479 nfsd->nfsd_slp = slp;
484 nfsd_head_flag &= ~NFSD_CHECKSLP;
486 if ((slp = nfsd->nfsd_slp) == (struct nfssvc_sock *)0)
488 if (slp->ns_flag & SLP_VALID) {
489 if (slp->ns_flag & SLP_DISCONN)
491 else if (slp->ns_flag & SLP_NEEDQ) {
492 slp->ns_flag &= ~SLP_NEEDQ;
493 (void) nfs_slplock(slp, 1);
494 nfsrv_rcv(slp->ns_so, (caddr_t)slp,
498 error = nfsrv_dorec(slp, nfsd, &nd);
499 cur_usec = nfs_curusec();
500 if (error && slp->ns_tq.lh_first &&
501 slp->ns_tq.lh_first->nd_time <= cur_usec) {
507 nfsd->nfsd_flag |= NFSD_REQINPROG;
511 slp = nfsd->nfsd_slp;
513 if (error || (slp->ns_flag & SLP_VALID) == 0) {
515 free((caddr_t)nd, M_NFSRVDESC);
518 nfsd->nfsd_slp = (struct nfssvc_sock *)0;
519 nfsd->nfsd_flag &= ~NFSD_REQINPROG;
524 sotype = slp->ns_so->so_type;
526 getmicrotime(&nd->nd_starttime);
528 nd->nd_nam = nd->nd_nam2;
530 nd->nd_nam = slp->ns_nam;
533 * Check to see if authorization is needed.
535 if (nfsd->nfsd_flag & NFSD_NEEDAUTH) {
536 nfsd->nfsd_flag &= ~NFSD_NEEDAUTH;
538 ((struct sockaddr_in *)
539 nd->nd_nam)->sin_addr.s_addr;
540 nsd->nsd_authlen = nfsd->nfsd_authlen;
541 nsd->nsd_verflen = nfsd->nfsd_verflen;
542 if (!copyout(nfsd->nfsd_authstr,nsd->nsd_authstr,
543 nfsd->nfsd_authlen) &&
544 !copyout(nfsd->nfsd_verfstr, nsd->nsd_verfstr,
545 nfsd->nfsd_verflen) &&
546 !copyout((caddr_t)nsd, argp, sizeof (*nsd)))
548 cacherep = RC_DROPIT;
550 cacherep = nfsrv_getcache(nd, slp, &mreq);
553 * Check for just starting up for NQNFS and send
554 * fake "try again later" replies to the NQNFS clients.
556 if (notstarted && nqnfsstarttime <= time_second) {
558 nqnfsstarttime = time_second + nqsrv_writeslack;
564 if ((nd->nd_flag & ND_NQNFS) == 0)
565 cacherep = RC_DROPIT;
566 else if (nd->nd_procnum != NFSPROC_WRITE) {
567 nd->nd_procnum = NFSPROC_NOOP;
568 nd->nd_repstat = NQNFS_TRYLATER;
572 } else if (nfsd->nfsd_flag & NFSD_AUTHFAIL) {
573 nfsd->nfsd_flag &= ~NFSD_AUTHFAIL;
574 nd->nd_procnum = NFSPROC_NOOP;
575 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
577 } else if (nfs_privport) {
578 /* Check if source port is privileged */
580 struct sockaddr *nam = nd->nd_nam;
581 struct sockaddr_in *sin;
583 sin = (struct sockaddr_in *)nam;
584 port = ntohs(sin->sin_port);
585 if (port >= IPPORT_RESERVED &&
586 nd->nd_procnum != NFSPROC_NULL) {
587 nd->nd_procnum = NFSPROC_NOOP;
588 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
590 printf("NFS request from unprivileged port (%s:%d)\n",
591 inet_ntoa(sin->sin_addr), port);
598 * Loop to get all the write rpc relies that have been
604 if (nd && (nd->nd_flag & ND_NFSV3))
605 procrastinate = nfsrvw_procrastinate_v3;
607 procrastinate = nfsrvw_procrastinate;
608 if (writes_todo || (nd->nd_procnum == NFSPROC_WRITE &&
609 procrastinate > 0 && !notstarted))
610 error = nfsrv_writegather(&nd, slp,
611 nfsd->nfsd_td, &mreq);
613 error = (*(nfsrv3_procs[nd->nd_procnum]))(nd,
614 slp, nfsd->nfsd_td, &mreq);
617 if (error != 0 && error != NFSERR_RETVOID) {
618 if (nd->nd_procnum != NQNFSPROC_VACATED)
620 nfsrv_updatecache(nd, FALSE, mreq);
622 FREE(nd->nd_nam2, M_SONAME);
625 nfsstats.srvrpccnt[nd->nd_procnum]++;
626 nfsrv_updatecache(nd, TRUE, mreq);
627 nd->nd_mrep = (struct mbuf *)0;
635 if (siz <= 0 || siz > NFS_MAXPACKET) {
636 printf("mbuf siz=%d\n",siz);
637 panic("Bad nfs svc reply");
640 m->m_pkthdr.len = siz;
641 m->m_pkthdr.rcvif = (struct ifnet *)0;
643 * For stream protocols, prepend a Sun RPC
646 if (sotype == SOCK_STREAM) {
647 M_PREPEND(m, NFSX_UNSIGNED, MB_WAIT);
650 *mtod(m, u_int32_t *) = htonl(0x80000000 | siz);
652 if (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED)
653 (void) nfs_slplock(slp, 1);
654 if (slp->ns_flag & SLP_VALID)
655 error = nfs_send(slp->ns_so, nd->nd_nam2, m, NULL);
661 nfsd_rt(sotype, nd, cacherep);
663 FREE(nd->nd_nam2, M_SONAME);
665 m_freem(nd->nd_mrep);
668 if (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED)
670 if (error == EINTR || error == ERESTART) {
671 free((caddr_t)nd, M_NFSRVDESC);
679 nfsd_rt(sotype, nd, cacherep);
680 m_freem(nd->nd_mrep);
682 FREE(nd->nd_nam2, M_SONAME);
686 FREE((caddr_t)nd, M_NFSRVDESC);
691 * Check to see if there are outstanding writes that
692 * need to be serviced.
694 cur_usec = nfs_curusec();
696 if (slp->ns_tq.lh_first &&
697 slp->ns_tq.lh_first->nd_time <= cur_usec) {
703 } while (writes_todo);
705 if (nfsrv_dorec(slp, nfsd, &nd)) {
706 nfsd->nfsd_flag &= ~NFSD_REQINPROG;
707 nfsd->nfsd_slp = NULL;
712 TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
714 free((caddr_t)nfsd, M_NFSD);
715 nsd->nsd_nfsd = (struct nfsd *)0;
716 if (--nfs_numnfsd == 0)
717 nfsrv_init(TRUE); /* Reinitialize everything */
722 * Shut down a socket associated with an nfssvc_sock structure.
723 * Should be called with the send lock set, if required.
724 * The trick here is to increment the sref at the start, so that the nfsds
725 * will stop using it and clear ns_flag at the end so that it will not be
726 * reassigned during cleanup.
729 nfsrv_zapsock(struct nfssvc_sock *slp)
731 struct nfsuid *nuidp, *nnuidp;
732 struct nfsrv_descript *nwp, *nnwp;
735 struct nfsrv_rec *rec;
738 slp->ns_flag &= ~SLP_ALLFLAGS;
741 slp->ns_fp = (struct file *)0;
743 so->so_rcv.sb_flags &= ~SB_UPCALL;
744 so->so_upcall = NULL;
745 so->so_upcallarg = NULL;
749 FREE(slp->ns_nam, M_SONAME);
750 m_freem(slp->ns_raw);
751 while ((rec = STAILQ_FIRST(&slp->ns_rec)) != NULL) {
753 STAILQ_REMOVE_HEAD(&slp->ns_rec, nr_link);
755 FREE(rec->nr_address, M_SONAME);
756 m_freem(rec->nr_packet);
757 free(rec, M_NFSRVDESC);
759 TAILQ_FOREACH_MUTABLE(nuidp, &slp->ns_uidlruhead, nu_lru,
761 LIST_REMOVE(nuidp, nu_hash);
762 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru);
763 if (nuidp->nu_flag & NU_NAM)
764 FREE(nuidp->nu_nam, M_SONAME);
765 free((caddr_t)nuidp, M_NFSUID);
768 for (nwp = slp->ns_tq.lh_first; nwp; nwp = nnwp) {
769 nnwp = nwp->nd_tq.le_next;
770 LIST_REMOVE(nwp, nd_tq);
771 free((caddr_t)nwp, M_NFSRVDESC);
773 LIST_INIT(&slp->ns_tq);
779 * Derefence a server socket structure. If it has no more references and
780 * is no longer valid, you can throw it away.
783 nfsrv_slpderef(struct nfssvc_sock *slp)
785 if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) {
786 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
787 free((caddr_t)slp, M_NFSSVC);
792 * Lock a socket against others.
795 nfs_slplock(struct nfssvc_sock *slp, int wait)
797 int *statep = &slp->ns_solock;
799 if (!wait && (*statep & NFSSTA_SNDLOCK))
800 return(0); /* already locked, fail */
801 while (*statep & NFSSTA_SNDLOCK) {
802 *statep |= NFSSTA_WANTSND;
803 (void) tsleep((caddr_t)statep, 0, "nfsslplck", 0);
805 *statep |= NFSSTA_SNDLOCK;
810 * Unlock the stream socket for others.
813 nfs_slpunlock(struct nfssvc_sock *slp)
815 int *statep = &slp->ns_solock;
817 if ((*statep & NFSSTA_SNDLOCK) == 0)
818 panic("nfs slpunlock");
819 *statep &= ~NFSSTA_SNDLOCK;
820 if (*statep & NFSSTA_WANTSND) {
821 *statep &= ~NFSSTA_WANTSND;
822 wakeup((caddr_t)statep);
827 * Initialize the data structures for the server.
828 * Handshake with any new nfsds starting up to avoid any chance of
832 nfsrv_init(int terminating)
834 struct nfssvc_sock *slp, *nslp;
836 if (nfssvc_sockhead_flag & SLP_INIT)
838 nfssvc_sockhead_flag |= SLP_INIT;
840 TAILQ_FOREACH_MUTABLE(slp, &nfssvc_sockhead, ns_chain, nslp) {
841 if (slp->ns_flag & SLP_VALID)
843 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
844 free((caddr_t)slp, M_NFSSVC);
846 nfsrv_cleancache(); /* And clear out server cache */
848 nfs_pub.np_valid = 0;
850 TAILQ_INIT(&nfssvc_sockhead);
851 nfssvc_sockhead_flag &= ~SLP_INIT;
852 if (nfssvc_sockhead_flag & SLP_WANTINIT) {
853 nfssvc_sockhead_flag &= ~SLP_WANTINIT;
854 wakeup((caddr_t)&nfssvc_sockhead);
857 TAILQ_INIT(&nfsd_head);
858 nfsd_head_flag &= ~NFSD_CHECKSLP;
861 nfs_udpsock = (struct nfssvc_sock *)
862 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
863 bzero((caddr_t)nfs_udpsock, sizeof (struct nfssvc_sock));
864 STAILQ_INIT(&nfs_udpsock->ns_rec);
865 TAILQ_INIT(&nfs_udpsock->ns_uidlruhead);
866 TAILQ_INSERT_HEAD(&nfssvc_sockhead, nfs_udpsock, ns_chain);
868 nfs_cltpsock = (struct nfssvc_sock *)
869 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
870 bzero((caddr_t)nfs_cltpsock, sizeof (struct nfssvc_sock));
871 STAILQ_INIT(&nfs_cltpsock->ns_rec);
872 TAILQ_INIT(&nfs_cltpsock->ns_uidlruhead);
873 TAILQ_INSERT_TAIL(&nfssvc_sockhead, nfs_cltpsock, ns_chain);
878 * Add entries to the server monitor log.
881 nfsd_rt(int sotype, struct nfsrv_descript *nd, int cacherep)
885 rt = &nfsdrt.drt[nfsdrt.pos];
886 if (cacherep == RC_DOIT)
888 else if (cacherep == RC_REPLY)
889 rt->flag = DRT_CACHEREPLY;
891 rt->flag = DRT_CACHEDROP;
892 if (sotype == SOCK_STREAM)
894 if (nd->nd_flag & ND_NQNFS)
895 rt->flag |= DRT_NQNFS;
896 else if (nd->nd_flag & ND_NFSV3)
897 rt->flag |= DRT_NFSV3;
898 rt->proc = nd->nd_procnum;
899 if (nd->nd_nam->sa_family == AF_INET)
900 rt->ipadr = ((struct sockaddr_in *)nd->nd_nam)->sin_addr.s_addr;
902 rt->ipadr = INADDR_ANY;
903 rt->resptime = nfs_curusec() - (nd->nd_starttime.tv_sec * 1000000 + nd->nd_starttime.tv_usec);
904 getmicrotime(&rt->tstamp);
905 nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ;
907 #endif /* NFS_NOSERVER */
909 static int nfs_defect = 0;
910 SYSCTL_INT(_vfs_nfs, OID_AUTO, defect, CTLFLAG_RW, &nfs_defect, 0, "");
913 * Asynchronous I/O daemons for client nfs.
914 * They do read-ahead and write-behind operations on the block I/O cache.
915 * Never returns unless it fails or gets killed.
918 nfssvc_iod(struct thread *td)
922 struct nfsmount *nmp;
926 * Assign my position or return error if too many already running
929 for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
930 if (nfs_asyncdaemon[i] == 0) {
931 nfs_asyncdaemon[i]++;
939 * Just loop around doin our stuff until SIGKILL
942 while (((nmp = nfs_iodmount[myiod]) == NULL
943 || TAILQ_EMPTY(&nmp->nm_bufq))
947 nfs_iodwant[myiod] = td;
948 nfs_iodmount[myiod] = NULL;
949 error = tsleep((caddr_t)&nfs_iodwant[myiod],
950 PCATCH, "nfsidl", 0);
953 nfs_asyncdaemon[myiod] = 0;
956 nfs_iodwant[myiod] = NULL;
957 nfs_iodmount[myiod] = NULL;
961 while ((bp = TAILQ_FIRST(&nmp->nm_bufq)) != NULL) {
962 /* Take one off the front of the list */
963 TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist);
965 if (nmp->nm_bufqwant && nmp->nm_bufqlen <= nfs_numasync) {
966 nmp->nm_bufqwant = FALSE;
967 wakeup(&nmp->nm_bufq);
969 (void) nfs_doio(bp, NULL);
971 * If there are more than one iod on this mount, then defect
972 * so that the iods can be shared out fairly between the mounts
974 if (nfs_defect && nmp->nm_bufqiods > 1) {
976 ("nfssvc_iod: iod %d defecting from mount %p\n",
978 nfs_iodmount[myiod] = NULL;
988 * Get an authorization string for the uid by having the mount_nfs sitting
989 * on this mount point porpous out of the kernel and do it.
992 nfs_getauth(struct nfsmount *nmp, struct nfsreq *rep,
993 struct ucred *cred, char **auth_str, int *auth_len, char *verf_str,
994 int *verf_len, NFSKERBKEY_T key /* return session key */)
998 while ((nmp->nm_state & NFSSTA_WAITAUTH) == 0) {
999 nmp->nm_state |= NFSSTA_WANTAUTH;
1000 (void) tsleep((caddr_t)&nmp->nm_authtype, 0,
1001 "nfsauth1", 2 * hz);
1002 error = nfs_sigintr(nmp, rep, rep->r_td);
1004 nmp->nm_state &= ~NFSSTA_WANTAUTH;
1008 nmp->nm_state &= ~(NFSSTA_WAITAUTH | NFSSTA_WANTAUTH);
1009 nmp->nm_authstr = *auth_str = (char *)malloc(RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK);
1010 nmp->nm_authlen = RPCAUTH_MAXSIZ;
1011 nmp->nm_verfstr = verf_str;
1012 nmp->nm_verflen = *verf_len;
1013 nmp->nm_authuid = cred->cr_uid;
1014 wakeup((caddr_t)&nmp->nm_authstr);
1017 * And wait for mount_nfs to do its stuff.
1019 while ((nmp->nm_state & NFSSTA_HASAUTH) == 0 && error == 0) {
1020 (void) tsleep((caddr_t)&nmp->nm_authlen, 0,
1021 "nfsauth2", 2 * hz);
1022 error = nfs_sigintr(nmp, rep, rep->r_td);
1024 if (nmp->nm_state & NFSSTA_AUTHERR) {
1025 nmp->nm_state &= ~NFSSTA_AUTHERR;
1029 free((caddr_t)*auth_str, M_TEMP);
1031 *auth_len = nmp->nm_authlen;
1032 *verf_len = nmp->nm_verflen;
1033 bcopy((caddr_t)nmp->nm_key, (caddr_t)key, sizeof (key));
1035 nmp->nm_state &= ~NFSSTA_HASAUTH;
1036 nmp->nm_state |= NFSSTA_WAITAUTH;
1037 if (nmp->nm_state & NFSSTA_WANTAUTH) {
1038 nmp->nm_state &= ~NFSSTA_WANTAUTH;
1039 wakeup((caddr_t)&nmp->nm_authtype);
1045 * Get a nickname authenticator and verifier.
1048 nfs_getnickauth(struct nfsmount *nmp, struct ucred *cred, char **auth_str,
1049 int *auth_len, char *verf_str, int verf_len)
1051 struct nfsuid *nuidp;
1052 u_int32_t *nickp, *verfp;
1053 struct timeval ktvin, ktvout;
1056 if (verf_len < (4 * NFSX_UNSIGNED))
1057 panic("nfs_getnickauth verf too small");
1059 for (nuidp = NMUIDHASH(nmp, cred->cr_uid)->lh_first;
1060 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
1061 if (nuidp->nu_cr.cr_uid == cred->cr_uid)
1064 if (!nuidp || nuidp->nu_expire < time_second)
1068 * Move to the end of the lru list (end of lru == most recently used).
1070 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
1071 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, nu_lru);
1073 nickp = (u_int32_t *)malloc(2 * NFSX_UNSIGNED, M_TEMP, M_WAITOK);
1074 *nickp++ = txdr_unsigned(RPCAKN_NICKNAME);
1075 *nickp = txdr_unsigned(nuidp->nu_nickname);
1076 *auth_str = (char *)nickp;
1077 *auth_len = 2 * NFSX_UNSIGNED;
1080 * Now we must encrypt the verifier and package it up.
1082 verfp = (u_int32_t *)verf_str;
1083 *verfp++ = txdr_unsigned(RPCAKN_NICKNAME);
1084 if (time_second > nuidp->nu_timestamp.tv_sec ||
1085 (time_second == nuidp->nu_timestamp.tv_sec &&
1086 time_second > nuidp->nu_timestamp.tv_usec))
1087 getmicrotime(&nuidp->nu_timestamp);
1089 nuidp->nu_timestamp.tv_usec++;
1090 ktvin.tv_sec = txdr_unsigned(nuidp->nu_timestamp.tv_sec);
1091 ktvin.tv_usec = txdr_unsigned(nuidp->nu_timestamp.tv_usec);
1094 * Now encrypt the timestamp verifier in ecb mode using the session
1101 *verfp++ = ktvout.tv_sec;
1102 *verfp++ = ktvout.tv_usec;
1108 * Save the current nickname in a hash list entry on the mount point.
1111 nfs_savenickauth(struct nfsmount *nmp, struct ucred *cred, int len,
1112 NFSKERBKEY_T key, struct mbuf **mdp, char **dposp,
1115 struct nfsuid *nuidp;
1118 struct mbuf *md = *mdp;
1119 struct timeval ktvin, ktvout;
1121 char *dpos = *dposp, *cp2;
1122 int deltasec, error = 0;
1124 if (len == (3 * NFSX_UNSIGNED)) {
1125 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1126 ktvin.tv_sec = *tl++;
1127 ktvin.tv_usec = *tl++;
1128 nick = fxdr_unsigned(u_int32_t, *tl);
1131 * Decrypt the timestamp in ecb mode.
1136 ktvout.tv_sec = fxdr_unsigned(long, ktvout.tv_sec);
1137 ktvout.tv_usec = fxdr_unsigned(long, ktvout.tv_usec);
1138 deltasec = time_second - ktvout.tv_sec;
1140 deltasec = -deltasec;
1142 * If ok, add it to the hash list for the mount point.
1144 if (deltasec <= NFS_KERBCLOCKSKEW) {
1145 if (nmp->nm_numuids < nuidhash_max) {
1147 nuidp = (struct nfsuid *)
1148 malloc(sizeof (struct nfsuid), M_NFSUID,
1151 nuidp = TAILQ_FIRST(&nmp->nm_uidlruhead);
1152 LIST_REMOVE(nuidp, nu_hash);
1153 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp,
1157 nuidp->nu_cr.cr_uid = cred->cr_uid;
1158 nuidp->nu_expire = time_second + NFS_KERBTTL;
1159 nuidp->nu_timestamp = ktvout;
1160 nuidp->nu_nickname = nick;
1161 bcopy(key, nuidp->nu_key, sizeof (key));
1162 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp,
1164 LIST_INSERT_HEAD(NMUIDHASH(nmp, cred->cr_uid),
1168 nfsm_adv(nfsm_rndup(len));