nfs_nfsiod.c revision 59794
1/*
2 * Copyright (c) 1989, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
23 *
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
34 * SUCH DAMAGE.
35 *
36 *	@(#)nfs_syscalls.c	8.5 (Berkeley) 3/30/95
37 * $FreeBSD: head/sys/nfsclient/nfs_nfsiod.c 59794 2000-04-30 18:52:11Z phk $
38 */
39
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>
45#include <sys/file.h>
46#include <sys/filedesc.h>
47#include <sys/vnode.h>
48#include <sys/malloc.h>
49#include <sys/mount.h>
50#include <sys/proc.h>
51#include <sys/buf.h>
52#include <sys/mbuf.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
59#include <netinet/in.h>
60#include <netinet/tcp.h>
61#include <nfs/xdr_subs.h>
62#include <nfs/rpcv2.h>
63#include <nfs/nfsproto.h>
64#include <nfs/nfs.h>
65#include <nfs/nfsm_subs.h>
66#include <nfs/nfsrvcache.h>
67#include <nfs/nfsmount.h>
68#include <nfs/nfsnode.h>
69#include <nfs/nqnfs.h>
70#include <nfs/nfsrtt.h>
71
72static MALLOC_DEFINE(M_NFSSVC, "NFS srvsock", "Nfs server structure");
73
74/* Global defs. */
75extern int32_t (*nfsrv3_procs[NFS_NPROCS]) __P((struct nfsrv_descript *nd,
76					    struct nfssvc_sock *slp,
77					    struct proc *procp,
78					    struct mbuf **mreqp));
79extern int nfs_numasync;
80extern time_t nqnfsstarttime;
81extern int nqsrv_writeslack;
82extern int nfsrtton;
83extern struct nfsstats nfsstats;
84extern int nfsrvw_procrastinate;
85extern int nfsrvw_procrastinate_v3;
86static int nuidhash_max = NFS_MAXUIDHASH;
87
88#ifndef NFS_NOSERVER
89static void	nfsrv_zapsock __P((struct nfssvc_sock *slp));
90#endif
91static int	nfssvc_iod __P((struct proc *));
92
93#define	TRUE	1
94#define	FALSE	0
95
96static int nfs_asyncdaemon[NFS_MAXASYNCDAEMON];
97
98SYSCTL_DECL(_vfs_nfs);
99
100#ifndef NFS_NOSERVER
101int nfsd_waiting = 0;
102static struct nfsdrt nfsdrt;
103static int nfs_numnfsd = 0;
104static int notstarted = 1;
105static int modify_flag = 0;
106static void	nfsd_rt __P((int sotype, struct nfsrv_descript *nd,
107			     int cacherep));
108static int	nfssvc_addsock __P((struct file *, struct sockaddr *,
109				    struct proc *));
110static int	nfssvc_nfsd __P((struct nfsd_srvargs *,caddr_t,struct proc *));
111
112static int nfs_privport = 0;
113SYSCTL_INT(_vfs_nfs, NFS_NFSPRIVPORT, nfs_privport, CTLFLAG_RW, &nfs_privport, 0, "");
114SYSCTL_INT(_vfs_nfs, OID_AUTO, gatherdelay, CTLFLAG_RW, &nfsrvw_procrastinate, 0, "");
115SYSCTL_INT(_vfs_nfs, OID_AUTO, gatherdelay_v3, CTLFLAG_RW, &nfsrvw_procrastinate_v3, 0, "");
116
117/*
118 * NFS server system calls
119 */
120
121#endif /* NFS_NOSERVER */
122/*
123 * Nfs server psuedo system call for the nfsd's
124 * Based on the flag value it either:
125 * - adds a socket to the selection list
126 * - remains in the kernel as an nfsd
127 * - remains in the kernel as an nfsiod
128 */
129#ifndef _SYS_SYSPROTO_H_
130struct nfssvc_args {
131	int flag;
132	caddr_t argp;
133};
134#endif
135int
136nfssvc(p, uap)
137	struct proc *p;
138	register struct nfssvc_args *uap;
139{
140#ifndef NFS_NOSERVER
141	struct nameidata nd;
142	struct file *fp;
143	struct sockaddr *nam;
144	struct nfsd_args nfsdarg;
145	struct nfsd_srvargs nfsd_srvargs, *nsd = &nfsd_srvargs;
146	struct nfsd_cargs ncd;
147	struct nfsd *nfsd;
148	struct nfssvc_sock *slp;
149	struct nfsuid *nuidp;
150	struct nfsmount *nmp;
151#endif /* NFS_NOSERVER */
152	int error;
153
154	/*
155	 * Must be super user
156	 */
157	error = suser(p);
158	if(error)
159		return (error);
160	while (nfssvc_sockhead_flag & SLP_INIT) {
161		 nfssvc_sockhead_flag |= SLP_WANTINIT;
162		(void) tsleep((caddr_t)&nfssvc_sockhead, PSOCK, "nfsd init", 0);
163	}
164	if (uap->flag & NFSSVC_BIOD)
165		error = nfssvc_iod(p);
166#ifdef NFS_NOSERVER
167	else
168		error = ENXIO;
169#else /* !NFS_NOSERVER */
170	else if (uap->flag & NFSSVC_MNTD) {
171		error = copyin(uap->argp, (caddr_t)&ncd, sizeof (ncd));
172		if (error)
173			return (error);
174		NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
175			ncd.ncd_dirp, p);
176		error = namei(&nd);
177		if (error)
178			return (error);
179		NDFREE(&nd, NDF_ONLY_PNBUF);
180		if ((nd.ni_vp->v_flag & VROOT) == 0)
181			error = EINVAL;
182		nmp = VFSTONFS(nd.ni_vp->v_mount);
183		vput(nd.ni_vp);
184		if (error)
185			return (error);
186		if ((nmp->nm_state & NFSSTA_MNTD) &&
187			(uap->flag & NFSSVC_GOTAUTH) == 0)
188			return (0);
189		nmp->nm_state |= NFSSTA_MNTD;
190		error = nqnfs_clientd(nmp, p->p_ucred, &ncd, uap->flag,
191			uap->argp, p);
192	} else if (uap->flag & NFSSVC_ADDSOCK) {
193		error = copyin(uap->argp, (caddr_t)&nfsdarg, sizeof(nfsdarg));
194		if (error)
195			return (error);
196		error = getsock(p->p_fd, nfsdarg.sock, &fp);
197		if (error)
198			return (error);
199		/*
200		 * Get the client address for connected sockets.
201		 */
202		if (nfsdarg.name == NULL || nfsdarg.namelen == 0)
203			nam = (struct sockaddr *)0;
204		else {
205			error = getsockaddr(&nam, nfsdarg.name,
206					    nfsdarg.namelen);
207			if (error)
208				return (error);
209		}
210		error = nfssvc_addsock(fp, nam, p);
211	} else {
212		error = copyin(uap->argp, (caddr_t)nsd, sizeof (*nsd));
213		if (error)
214			return (error);
215		if ((uap->flag & NFSSVC_AUTHIN) &&
216		    ((nfsd = nsd->nsd_nfsd)) != NULL &&
217		    (nfsd->nfsd_slp->ns_flag & SLP_VALID)) {
218			slp = nfsd->nfsd_slp;
219
220			/*
221			 * First check to see if another nfsd has already
222			 * added this credential.
223			 */
224			for (nuidp = NUIDHASH(slp,nsd->nsd_cr.cr_uid)->lh_first;
225			    nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
226				if (nuidp->nu_cr.cr_uid == nsd->nsd_cr.cr_uid &&
227				    (!nfsd->nfsd_nd->nd_nam2 ||
228				     netaddr_match(NU_NETFAM(nuidp),
229				     &nuidp->nu_haddr, nfsd->nfsd_nd->nd_nam2)))
230					break;
231			}
232			if (nuidp) {
233			    nfsrv_setcred(&nuidp->nu_cr,&nfsd->nfsd_nd->nd_cr);
234			    nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
235			} else {
236			    /*
237			     * Nope, so we will.
238			     */
239			    if (slp->ns_numuids < nuidhash_max) {
240				slp->ns_numuids++;
241				nuidp = (struct nfsuid *)
242				   malloc(sizeof (struct nfsuid), M_NFSUID,
243					M_WAITOK);
244			    } else
245				nuidp = (struct nfsuid *)0;
246			    if ((slp->ns_flag & SLP_VALID) == 0) {
247				if (nuidp)
248				    free((caddr_t)nuidp, M_NFSUID);
249			    } else {
250				if (nuidp == (struct nfsuid *)0) {
251				    nuidp = slp->ns_uidlruhead.tqh_first;
252				    LIST_REMOVE(nuidp, nu_hash);
253				    TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp,
254					nu_lru);
255				    if (nuidp->nu_flag & NU_NAM)
256					FREE(nuidp->nu_nam, M_SONAME);
257			        }
258				nuidp->nu_flag = 0;
259				nuidp->nu_cr = nsd->nsd_cr;
260				if (nuidp->nu_cr.cr_ngroups > NGROUPS)
261				    nuidp->nu_cr.cr_ngroups = NGROUPS;
262				nuidp->nu_cr.cr_ref = 1;
263				nuidp->nu_timestamp = nsd->nsd_timestamp;
264				nuidp->nu_expire = time_second + nsd->nsd_ttl;
265				/*
266				 * and save the session key in nu_key.
267				 */
268				bcopy(nsd->nsd_key, nuidp->nu_key,
269				    sizeof (nsd->nsd_key));
270				if (nfsd->nfsd_nd->nd_nam2) {
271				    struct sockaddr_in *saddr;
272
273				    saddr = (struct sockaddr_in *)
274					    nfsd->nfsd_nd->nd_nam2;
275				    switch (saddr->sin_family) {
276				    case AF_INET:
277					nuidp->nu_flag |= NU_INETADDR;
278					nuidp->nu_inetaddr =
279					     saddr->sin_addr.s_addr;
280					break;
281				    case AF_ISO:
282				    default:
283					nuidp->nu_flag |= NU_NAM;
284					nuidp->nu_nam =
285						dup_sockaddr(nfsd->nfsd_nd->
286							     nd_nam2, 1);
287					break;
288				    };
289				}
290				TAILQ_INSERT_TAIL(&slp->ns_uidlruhead, nuidp,
291					nu_lru);
292				LIST_INSERT_HEAD(NUIDHASH(slp, nsd->nsd_uid),
293					nuidp, nu_hash);
294				nfsrv_setcred(&nuidp->nu_cr,
295				    &nfsd->nfsd_nd->nd_cr);
296				nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
297			    }
298			}
299		}
300		if ((uap->flag & NFSSVC_AUTHINFAIL) && (nfsd = nsd->nsd_nfsd))
301			nfsd->nfsd_flag |= NFSD_AUTHFAIL;
302		error = nfssvc_nfsd(nsd, uap->argp, p);
303	}
304#endif /* NFS_NOSERVER */
305	if (error == EINTR || error == ERESTART)
306		error = 0;
307	return (error);
308}
309
310#ifndef NFS_NOSERVER
311/*
312 * Adds a socket to the list for servicing by nfsds.
313 */
314static int
315nfssvc_addsock(fp, mynam, p)
316	struct file *fp;
317	struct sockaddr *mynam;
318	struct proc *p;
319{
320	register int siz;
321	register struct nfssvc_sock *slp;
322	register struct socket *so;
323	int error, s;
324
325	so = (struct socket *)fp->f_data;
326#if 0
327	tslp = (struct nfssvc_sock *)0;
328	/*
329	 * Add it to the list, as required.
330	 */
331	if (so->so_proto->pr_protocol == IPPROTO_UDP) {
332		tslp = nfs_udpsock;
333		if (tslp->ns_flag & SLP_VALID) {
334			if (mynam != NULL)
335				FREE(mynam, M_SONAME);
336			return (EPERM);
337		}
338	}
339#endif
340	if (so->so_type == SOCK_STREAM)
341		siz = NFS_MAXPACKET + sizeof (u_long);
342	else
343		siz = NFS_MAXPACKET;
344	error = soreserve(so, siz, siz);
345	if (error) {
346		if (mynam != NULL)
347			FREE(mynam, M_SONAME);
348		return (error);
349	}
350
351	/*
352	 * Set protocol specific options { for now TCP only } and
353	 * reserve some space. For datagram sockets, this can get called
354	 * repeatedly for the same socket, but that isn't harmful.
355	 */
356	if (so->so_type == SOCK_STREAM) {
357		struct sockopt sopt;
358		int val;
359
360		bzero(&sopt, sizeof sopt);
361		sopt.sopt_level = SOL_SOCKET;
362		sopt.sopt_name = SO_KEEPALIVE;
363		sopt.sopt_val = &val;
364		sopt.sopt_valsize = sizeof val;
365		val = 1;
366		sosetopt(so, &sopt);
367	}
368	if (so->so_proto->pr_domain->dom_family == AF_INET &&
369	    so->so_proto->pr_protocol == IPPROTO_TCP) {
370		struct sockopt sopt;
371		int val;
372
373		bzero(&sopt, sizeof sopt);
374		sopt.sopt_level = IPPROTO_TCP;
375		sopt.sopt_name = TCP_NODELAY;
376		sopt.sopt_val = &val;
377		sopt.sopt_valsize = sizeof val;
378		val = 1;
379		sosetopt(so, &sopt);
380	}
381	so->so_rcv.sb_flags &= ~SB_NOINTR;
382	so->so_rcv.sb_timeo = 0;
383	so->so_snd.sb_flags &= ~SB_NOINTR;
384	so->so_snd.sb_timeo = 0;
385
386	slp = (struct nfssvc_sock *)
387		malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
388	bzero((caddr_t)slp, sizeof (struct nfssvc_sock));
389	STAILQ_INIT(&slp->ns_rec);
390	TAILQ_INIT(&slp->ns_uidlruhead);
391	TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
392
393	slp->ns_so = so;
394	slp->ns_nam = mynam;
395	fp->f_count++;
396	slp->ns_fp = fp;
397	s = splnet();
398	so->so_upcallarg = (caddr_t)slp;
399	so->so_upcall = nfsrv_rcv;
400	so->so_rcv.sb_flags |= SB_UPCALL;
401	slp->ns_flag = (SLP_VALID | SLP_NEEDQ);
402	nfsrv_wakenfsd(slp);
403	splx(s);
404	return (0);
405}
406
407/*
408 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
409 * until it is killed by a signal.
410 */
411static int
412nfssvc_nfsd(nsd, argp, p)
413	struct nfsd_srvargs *nsd;
414	caddr_t argp;
415	struct proc *p;
416{
417	register int siz;
418	register struct nfssvc_sock *slp;
419	struct nfsd *nfsd = nsd->nsd_nfsd;
420	struct nfsrv_descript *nd = NULL;
421	struct mbuf *m, *mreq;
422	int error = 0, cacherep, s, sotype, writes_todo;
423	int procrastinate;
424	u_quad_t cur_usec;
425
426#ifndef nolint
427	cacherep = RC_DOIT;
428	writes_todo = 0;
429#endif
430	if (nfsd == (struct nfsd *)0) {
431		nsd->nsd_nfsd = nfsd = (struct nfsd *)
432			malloc(sizeof (struct nfsd), M_NFSD, M_WAITOK);
433		bzero((caddr_t)nfsd, sizeof (struct nfsd));
434		s = splnet();
435		nfsd->nfsd_procp = p;
436		TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain);
437		nfs_numnfsd++;
438	} else
439		s = splnet();
440
441	/*
442	 * Loop getting rpc requests until SIGKILL.
443	 */
444	for (;;) {
445		if ((nfsd->nfsd_flag & NFSD_REQINPROG) == 0) {
446			while (nfsd->nfsd_slp == (struct nfssvc_sock *)0 &&
447			    (nfsd_head_flag & NFSD_CHECKSLP) == 0) {
448				nfsd->nfsd_flag |= NFSD_WAITING;
449				nfsd_waiting++;
450				error = tsleep((caddr_t)nfsd, PSOCK | PCATCH,
451				    "nfsd", 0);
452				nfsd_waiting--;
453				if (error)
454					goto done;
455			}
456			if (nfsd->nfsd_slp == (struct nfssvc_sock *)0 &&
457			    (nfsd_head_flag & NFSD_CHECKSLP) != 0) {
458				for (slp = nfssvc_sockhead.tqh_first; slp != 0;
459				    slp = slp->ns_chain.tqe_next) {
460				    if ((slp->ns_flag & (SLP_VALID | SLP_DOREC))
461					== (SLP_VALID | SLP_DOREC)) {
462					    slp->ns_flag &= ~SLP_DOREC;
463					    slp->ns_sref++;
464					    nfsd->nfsd_slp = slp;
465					    break;
466				    }
467				}
468				if (slp == 0)
469					nfsd_head_flag &= ~NFSD_CHECKSLP;
470			}
471			if ((slp = nfsd->nfsd_slp) == (struct nfssvc_sock *)0)
472				continue;
473			if (slp->ns_flag & SLP_VALID) {
474				if (slp->ns_flag & SLP_DISCONN)
475					nfsrv_zapsock(slp);
476				else if (slp->ns_flag & SLP_NEEDQ) {
477					slp->ns_flag &= ~SLP_NEEDQ;
478					(void) nfs_slplock(slp, 1);
479					nfsrv_rcv(slp->ns_so, (caddr_t)slp,
480						M_WAIT);
481					nfs_slpunlock(slp);
482				}
483				error = nfsrv_dorec(slp, nfsd, &nd);
484				cur_usec = nfs_curusec();
485				if (error && slp->ns_tq.lh_first &&
486				    slp->ns_tq.lh_first->nd_time <= cur_usec) {
487					error = 0;
488					cacherep = RC_DOIT;
489					writes_todo = 1;
490				} else
491					writes_todo = 0;
492				nfsd->nfsd_flag |= NFSD_REQINPROG;
493			}
494		} else {
495			error = 0;
496			slp = nfsd->nfsd_slp;
497		}
498		if (error || (slp->ns_flag & SLP_VALID) == 0) {
499			if (nd) {
500				free((caddr_t)nd, M_NFSRVDESC);
501				nd = NULL;
502			}
503			nfsd->nfsd_slp = (struct nfssvc_sock *)0;
504			nfsd->nfsd_flag &= ~NFSD_REQINPROG;
505			nfsrv_slpderef(slp);
506			continue;
507		}
508		splx(s);
509		sotype = slp->ns_so->so_type;
510		if (nd) {
511		    getmicrotime(&nd->nd_starttime);
512		    if (nd->nd_nam2)
513			nd->nd_nam = nd->nd_nam2;
514		    else
515			nd->nd_nam = slp->ns_nam;
516
517		    /*
518		     * Check to see if authorization is needed.
519		     */
520		    if (nfsd->nfsd_flag & NFSD_NEEDAUTH) {
521			nfsd->nfsd_flag &= ~NFSD_NEEDAUTH;
522			nsd->nsd_haddr =
523				((struct sockaddr_in *)
524				 nd->nd_nam)->sin_addr.s_addr;
525			nsd->nsd_authlen = nfsd->nfsd_authlen;
526			nsd->nsd_verflen = nfsd->nfsd_verflen;
527			if (!copyout(nfsd->nfsd_authstr,nsd->nsd_authstr,
528				nfsd->nfsd_authlen) &&
529			    !copyout(nfsd->nfsd_verfstr, nsd->nsd_verfstr,
530				nfsd->nfsd_verflen) &&
531			    !copyout((caddr_t)nsd, argp, sizeof (*nsd)))
532			    return (ENEEDAUTH);
533			cacherep = RC_DROPIT;
534		    } else
535			cacherep = nfsrv_getcache(nd, slp, &mreq);
536
537		    /*
538		     * Check for just starting up for NQNFS and send
539		     * fake "try again later" replies to the NQNFS clients.
540		     */
541		    if (notstarted && nqnfsstarttime <= time_second) {
542			if (modify_flag) {
543				nqnfsstarttime = time_second + nqsrv_writeslack;
544				modify_flag = 0;
545			} else
546				notstarted = 0;
547		    }
548		    if (notstarted) {
549			if ((nd->nd_flag & ND_NQNFS) == 0)
550				cacherep = RC_DROPIT;
551			else if (nd->nd_procnum != NFSPROC_WRITE) {
552				nd->nd_procnum = NFSPROC_NOOP;
553				nd->nd_repstat = NQNFS_TRYLATER;
554				cacherep = RC_DOIT;
555			} else
556				modify_flag = 1;
557		    } else if (nfsd->nfsd_flag & NFSD_AUTHFAIL) {
558			nfsd->nfsd_flag &= ~NFSD_AUTHFAIL;
559			nd->nd_procnum = NFSPROC_NOOP;
560			nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
561			cacherep = RC_DOIT;
562		    } else if (nfs_privport) {
563			/* Check if source port is privileged */
564			u_short port;
565			struct sockaddr *nam = nd->nd_nam;
566			struct sockaddr_in *sin;
567
568			sin = (struct sockaddr_in *)nam;
569			port = ntohs(sin->sin_port);
570			if (port >= IPPORT_RESERVED &&
571			    nd->nd_procnum != NFSPROC_NULL) {
572			    nd->nd_procnum = NFSPROC_NOOP;
573			    nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
574			    cacherep = RC_DOIT;
575			    printf("NFS request from unprivileged port (%s:%d)\n",
576				   inet_ntoa(sin->sin_addr), port);
577			}
578		    }
579
580		}
581
582		/*
583		 * Loop to get all the write rpc relies that have been
584		 * gathered together.
585		 */
586		do {
587		    switch (cacherep) {
588		    case RC_DOIT:
589			if (nd && (nd->nd_flag & ND_NFSV3))
590			    procrastinate = nfsrvw_procrastinate_v3;
591			else
592			    procrastinate = nfsrvw_procrastinate;
593			if (writes_todo || (nd->nd_procnum == NFSPROC_WRITE &&
594			    procrastinate > 0 && !notstarted))
595			    error = nfsrv_writegather(&nd, slp,
596				nfsd->nfsd_procp, &mreq);
597			else
598			    error = (*(nfsrv3_procs[nd->nd_procnum]))(nd,
599				slp, nfsd->nfsd_procp, &mreq);
600			if (mreq == NULL)
601				break;
602			if (error != 0 && error != NFSERR_RETVOID) {
603				if (nd->nd_procnum != NQNFSPROC_VACATED)
604					nfsstats.srv_errs++;
605				nfsrv_updatecache(nd, FALSE, mreq);
606				if (nd->nd_nam2)
607					FREE(nd->nd_nam2, M_SONAME);
608				break;
609			}
610			nfsstats.srvrpccnt[nd->nd_procnum]++;
611			nfsrv_updatecache(nd, TRUE, mreq);
612			nd->nd_mrep = (struct mbuf *)0;
613		    case RC_REPLY:
614			m = mreq;
615			siz = 0;
616			while (m) {
617				siz += m->m_len;
618				m = m->m_next;
619			}
620			if (siz <= 0 || siz > NFS_MAXPACKET) {
621				printf("mbuf siz=%d\n",siz);
622				panic("Bad nfs svc reply");
623			}
624			m = mreq;
625			m->m_pkthdr.len = siz;
626			m->m_pkthdr.rcvif = (struct ifnet *)0;
627			/*
628			 * For stream protocols, prepend a Sun RPC
629			 * Record Mark.
630			 */
631			if (sotype == SOCK_STREAM) {
632				M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
633				*mtod(m, u_int32_t *) = htonl(0x80000000 | siz);
634			}
635			if (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED)
636				(void) nfs_slplock(slp, 1);
637			if (slp->ns_flag & SLP_VALID)
638			    error = nfs_send(slp->ns_so, nd->nd_nam2, m, NULL);
639			else {
640			    error = EPIPE;
641			    m_freem(m);
642			}
643			if (nfsrtton)
644				nfsd_rt(sotype, nd, cacherep);
645			if (nd->nd_nam2)
646				FREE(nd->nd_nam2, M_SONAME);
647			if (nd->nd_mrep)
648				m_freem(nd->nd_mrep);
649			if (error == EPIPE)
650				nfsrv_zapsock(slp);
651			if (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED)
652				nfs_slpunlock(slp);
653			if (error == EINTR || error == ERESTART) {
654				free((caddr_t)nd, M_NFSRVDESC);
655				nfsrv_slpderef(slp);
656				s = splnet();
657				goto done;
658			}
659			break;
660		    case RC_DROPIT:
661			if (nfsrtton)
662				nfsd_rt(sotype, nd, cacherep);
663			m_freem(nd->nd_mrep);
664			if (nd->nd_nam2)
665				FREE(nd->nd_nam2, M_SONAME);
666			break;
667		    };
668		    if (nd) {
669			FREE((caddr_t)nd, M_NFSRVDESC);
670			nd = NULL;
671		    }
672
673		    /*
674		     * Check to see if there are outstanding writes that
675		     * need to be serviced.
676		     */
677		    cur_usec = nfs_curusec();
678		    s = splsoftclock();
679		    if (slp->ns_tq.lh_first &&
680			slp->ns_tq.lh_first->nd_time <= cur_usec) {
681			cacherep = RC_DOIT;
682			writes_todo = 1;
683		    } else
684			writes_todo = 0;
685		    splx(s);
686		} while (writes_todo);
687		s = splnet();
688		if (nfsrv_dorec(slp, nfsd, &nd)) {
689			nfsd->nfsd_flag &= ~NFSD_REQINPROG;
690			nfsd->nfsd_slp = NULL;
691			nfsrv_slpderef(slp);
692		}
693	}
694done:
695	TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
696	splx(s);
697	free((caddr_t)nfsd, M_NFSD);
698	nsd->nsd_nfsd = (struct nfsd *)0;
699	if (--nfs_numnfsd == 0)
700		nfsrv_init(TRUE);	/* Reinitialize everything */
701	return (error);
702}
703
704/*
705 * Shut down a socket associated with an nfssvc_sock structure.
706 * Should be called with the send lock set, if required.
707 * The trick here is to increment the sref at the start, so that the nfsds
708 * will stop using it and clear ns_flag at the end so that it will not be
709 * reassigned during cleanup.
710 */
711static void
712nfsrv_zapsock(slp)
713	register struct nfssvc_sock *slp;
714{
715	register struct nfsuid *nuidp, *nnuidp;
716	register struct nfsrv_descript *nwp, *nnwp;
717	struct socket *so;
718	struct file *fp;
719	struct nfsrv_rec *rec;
720	int s;
721
722	slp->ns_flag &= ~SLP_ALLFLAGS;
723	fp = slp->ns_fp;
724	if (fp) {
725		slp->ns_fp = (struct file *)0;
726		so = slp->ns_so;
727		so->so_rcv.sb_flags &= ~SB_UPCALL;
728		so->so_upcall = NULL;
729		so->so_upcallarg = NULL;
730		soshutdown(so, 2);
731		closef(fp, (struct proc *)0);
732		if (slp->ns_nam)
733			FREE(slp->ns_nam, M_SONAME);
734		m_freem(slp->ns_raw);
735		while ((rec = STAILQ_FIRST(&slp->ns_rec)) != NULL) {
736			STAILQ_REMOVE_HEAD(&slp->ns_rec, nr_link);
737			if (rec->nr_address)
738				FREE(rec->nr_address, M_SONAME);
739			m_freem(rec->nr_packet);
740			free(rec, M_NFSRVDESC);
741		}
742		for (nuidp = slp->ns_uidlruhead.tqh_first; nuidp != 0;
743		    nuidp = nnuidp) {
744			nnuidp = nuidp->nu_lru.tqe_next;
745			LIST_REMOVE(nuidp, nu_hash);
746			TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru);
747			if (nuidp->nu_flag & NU_NAM)
748				FREE(nuidp->nu_nam, M_SONAME);
749			free((caddr_t)nuidp, M_NFSUID);
750		}
751		s = splsoftclock();
752		for (nwp = slp->ns_tq.lh_first; nwp; nwp = nnwp) {
753			nnwp = nwp->nd_tq.le_next;
754			LIST_REMOVE(nwp, nd_tq);
755			free((caddr_t)nwp, M_NFSRVDESC);
756		}
757		LIST_INIT(&slp->ns_tq);
758		splx(s);
759	}
760}
761
762/*
763 * Derefence a server socket structure. If it has no more references and
764 * is no longer valid, you can throw it away.
765 */
766void
767nfsrv_slpderef(slp)
768	register struct nfssvc_sock *slp;
769{
770	if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) {
771		TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
772		free((caddr_t)slp, M_NFSSVC);
773	}
774}
775
776/*
777 * Lock a socket against others.
778 */
779int
780nfs_slplock(slp, wait)
781	register struct nfssvc_sock *slp;
782	int wait;
783{
784	int *statep = &slp->ns_solock;
785
786	if (!wait && (*statep & NFSSTA_SNDLOCK))
787		return(0);	/* already locked, fail */
788	while (*statep & NFSSTA_SNDLOCK) {
789		*statep |= NFSSTA_WANTSND;
790		(void) tsleep((caddr_t)statep, PZERO - 1, "nfsslplck", 0);
791	}
792	*statep |= NFSSTA_SNDLOCK;
793	return (1);
794}
795
796/*
797 * Unlock the stream socket for others.
798 */
799void
800nfs_slpunlock(slp)
801	register struct nfssvc_sock *slp;
802{
803	int *statep = &slp->ns_solock;
804
805	if ((*statep & NFSSTA_SNDLOCK) == 0)
806		panic("nfs slpunlock");
807	*statep &= ~NFSSTA_SNDLOCK;
808	if (*statep & NFSSTA_WANTSND) {
809		*statep &= ~NFSSTA_WANTSND;
810		wakeup((caddr_t)statep);
811	}
812}
813
814/*
815 * Initialize the data structures for the server.
816 * Handshake with any new nfsds starting up to avoid any chance of
817 * corruption.
818 */
819void
820nfsrv_init(terminating)
821	int terminating;
822{
823	register struct nfssvc_sock *slp, *nslp;
824
825	if (nfssvc_sockhead_flag & SLP_INIT)
826		panic("nfsd init");
827	nfssvc_sockhead_flag |= SLP_INIT;
828	if (terminating) {
829		for (slp = nfssvc_sockhead.tqh_first; slp != 0; slp = nslp) {
830			nslp = slp->ns_chain.tqe_next;
831			if (slp->ns_flag & SLP_VALID)
832				nfsrv_zapsock(slp);
833			TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
834			free((caddr_t)slp, M_NFSSVC);
835		}
836		nfsrv_cleancache();	/* And clear out server cache */
837	} else
838		nfs_pub.np_valid = 0;
839
840	TAILQ_INIT(&nfssvc_sockhead);
841	nfssvc_sockhead_flag &= ~SLP_INIT;
842	if (nfssvc_sockhead_flag & SLP_WANTINIT) {
843		nfssvc_sockhead_flag &= ~SLP_WANTINIT;
844		wakeup((caddr_t)&nfssvc_sockhead);
845	}
846
847	TAILQ_INIT(&nfsd_head);
848	nfsd_head_flag &= ~NFSD_CHECKSLP;
849
850#if 0
851	nfs_udpsock = (struct nfssvc_sock *)
852	    malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
853	bzero((caddr_t)nfs_udpsock, sizeof (struct nfssvc_sock));
854	STAILQ_INIT(&nfs_udpsock->ns_rec);
855	TAILQ_INIT(&nfs_udpsock->ns_uidlruhead);
856	TAILQ_INSERT_HEAD(&nfssvc_sockhead, nfs_udpsock, ns_chain);
857
858	nfs_cltpsock = (struct nfssvc_sock *)
859	    malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
860	bzero((caddr_t)nfs_cltpsock, sizeof (struct nfssvc_sock));
861	STAILQ_INIT(&nfs_cltpsock->ns_rec);
862	TAILQ_INIT(&nfs_cltpsock->ns_uidlruhead);
863	TAILQ_INSERT_TAIL(&nfssvc_sockhead, nfs_cltpsock, ns_chain);
864#endif
865}
866
867/*
868 * Add entries to the server monitor log.
869 */
870static void
871nfsd_rt(sotype, nd, cacherep)
872	int sotype;
873	register struct nfsrv_descript *nd;
874	int cacherep;
875{
876	register struct drt *rt;
877
878	rt = &nfsdrt.drt[nfsdrt.pos];
879	if (cacherep == RC_DOIT)
880		rt->flag = 0;
881	else if (cacherep == RC_REPLY)
882		rt->flag = DRT_CACHEREPLY;
883	else
884		rt->flag = DRT_CACHEDROP;
885	if (sotype == SOCK_STREAM)
886		rt->flag |= DRT_TCP;
887	if (nd->nd_flag & ND_NQNFS)
888		rt->flag |= DRT_NQNFS;
889	else if (nd->nd_flag & ND_NFSV3)
890		rt->flag |= DRT_NFSV3;
891	rt->proc = nd->nd_procnum;
892	if (nd->nd_nam->sa_family == AF_INET)
893	    rt->ipadr = ((struct sockaddr_in *)nd->nd_nam)->sin_addr.s_addr;
894	else
895	    rt->ipadr = INADDR_ANY;
896	rt->resptime = nfs_curusec() - (nd->nd_starttime.tv_sec * 1000000 + nd->nd_starttime.tv_usec);
897	getmicrotime(&rt->tstamp);
898	nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ;
899}
900#endif /* NFS_NOSERVER */
901
902static int nfs_defect = 0;
903SYSCTL_INT(_vfs_nfs, OID_AUTO, defect, CTLFLAG_RW, &nfs_defect, 0, "");
904
905/*
906 * Asynchronous I/O daemons for client nfs.
907 * They do read-ahead and write-behind operations on the block I/O cache.
908 * Never returns unless it fails or gets killed.
909 */
910static int
911nfssvc_iod(p)
912	struct proc *p;
913{
914	register struct buf *bp;
915	register int i, myiod;
916	struct nfsmount *nmp;
917	int error = 0;
918
919	/*
920	 * Assign my position or return error if too many already running
921	 */
922	myiod = -1;
923	for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
924		if (nfs_asyncdaemon[i] == 0) {
925			nfs_asyncdaemon[i]++;
926			myiod = i;
927			break;
928		}
929	if (myiod == -1)
930		return (EBUSY);
931	nfs_numasync++;
932	/*
933	 * Just loop around doin our stuff until SIGKILL
934	 */
935	for (;;) {
936	    while (((nmp = nfs_iodmount[myiod]) == NULL
937		    || nmp->nm_bufq.tqh_first == NULL)
938		   && error == 0) {
939		if (nmp)
940		    nmp->nm_bufqiods--;
941		nfs_iodwant[myiod] = p;
942		nfs_iodmount[myiod] = NULL;
943		error = tsleep((caddr_t)&nfs_iodwant[myiod],
944			PWAIT | PCATCH, "nfsidl", 0);
945	    }
946	    if (error) {
947		nfs_asyncdaemon[myiod] = 0;
948		if (nmp)
949		    nmp->nm_bufqiods--;
950		nfs_iodwant[myiod] = NULL;
951		nfs_iodmount[myiod] = NULL;
952		nfs_numasync--;
953		return (error);
954	    }
955	    while ((bp = nmp->nm_bufq.tqh_first) != NULL) {
956		/* Take one off the front of the list */
957		TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist);
958		nmp->nm_bufqlen--;
959		if (nmp->nm_bufqwant && nmp->nm_bufqlen <= nfs_numasync) {
960		    nmp->nm_bufqwant = FALSE;
961		    wakeup(&nmp->nm_bufq);
962		}
963		if (bp->b_iocmd == BIO_READ)
964		    (void) nfs_doio(bp, bp->b_rcred, (struct proc *)0);
965		else
966		    (void) nfs_doio(bp, bp->b_wcred, (struct proc *)0);
967		/*
968		 * If there are more than one iod on this mount, then defect
969		 * so that the iods can be shared out fairly between the mounts
970		 */
971		if (nfs_defect && nmp->nm_bufqiods > 1) {
972		    NFS_DPF(ASYNCIO,
973			    ("nfssvc_iod: iod %d defecting from mount %p\n",
974			     myiod, nmp));
975		    nfs_iodmount[myiod] = NULL;
976		    nmp->nm_bufqiods--;
977		    break;
978		}
979	    }
980	}
981}
982
983
984/*
985 * Get an authorization string for the uid by having the mount_nfs sitting
986 * on this mount point porpous out of the kernel and do it.
987 */
988int
989nfs_getauth(nmp, rep, cred, auth_str, auth_len, verf_str, verf_len, key)
990	register struct nfsmount *nmp;
991	struct nfsreq *rep;
992	struct ucred *cred;
993	char **auth_str;
994	int *auth_len;
995	char *verf_str;
996	int *verf_len;
997	NFSKERBKEY_T key;		/* return session key */
998{
999	int error = 0;
1000
1001	while ((nmp->nm_state & NFSSTA_WAITAUTH) == 0) {
1002		nmp->nm_state |= NFSSTA_WANTAUTH;
1003		(void) tsleep((caddr_t)&nmp->nm_authtype, PSOCK,
1004			"nfsauth1", 2 * hz);
1005		error = nfs_sigintr(nmp, rep, rep->r_procp);
1006		if (error) {
1007			nmp->nm_state &= ~NFSSTA_WANTAUTH;
1008			return (error);
1009		}
1010	}
1011	nmp->nm_state &= ~(NFSSTA_WAITAUTH | NFSSTA_WANTAUTH);
1012	nmp->nm_authstr = *auth_str = (char *)malloc(RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK);
1013	nmp->nm_authlen = RPCAUTH_MAXSIZ;
1014	nmp->nm_verfstr = verf_str;
1015	nmp->nm_verflen = *verf_len;
1016	nmp->nm_authuid = cred->cr_uid;
1017	wakeup((caddr_t)&nmp->nm_authstr);
1018
1019	/*
1020	 * And wait for mount_nfs to do its stuff.
1021	 */
1022	while ((nmp->nm_state & NFSSTA_HASAUTH) == 0 && error == 0) {
1023		(void) tsleep((caddr_t)&nmp->nm_authlen, PSOCK,
1024			"nfsauth2", 2 * hz);
1025		error = nfs_sigintr(nmp, rep, rep->r_procp);
1026	}
1027	if (nmp->nm_state & NFSSTA_AUTHERR) {
1028		nmp->nm_state &= ~NFSSTA_AUTHERR;
1029		error = EAUTH;
1030	}
1031	if (error)
1032		free((caddr_t)*auth_str, M_TEMP);
1033	else {
1034		*auth_len = nmp->nm_authlen;
1035		*verf_len = nmp->nm_verflen;
1036		bcopy((caddr_t)nmp->nm_key, (caddr_t)key, sizeof (key));
1037	}
1038	nmp->nm_state &= ~NFSSTA_HASAUTH;
1039	nmp->nm_state |= NFSSTA_WAITAUTH;
1040	if (nmp->nm_state & NFSSTA_WANTAUTH) {
1041		nmp->nm_state &= ~NFSSTA_WANTAUTH;
1042		wakeup((caddr_t)&nmp->nm_authtype);
1043	}
1044	return (error);
1045}
1046
1047/*
1048 * Get a nickname authenticator and verifier.
1049 */
1050int
1051nfs_getnickauth(nmp, cred, auth_str, auth_len, verf_str, verf_len)
1052	struct nfsmount *nmp;
1053	struct ucred *cred;
1054	char **auth_str;
1055	int *auth_len;
1056	char *verf_str;
1057	int verf_len;
1058{
1059	register struct nfsuid *nuidp;
1060	register u_int32_t *nickp, *verfp;
1061	struct timeval ktvin, ktvout;
1062
1063#ifdef DIAGNOSTIC
1064	if (verf_len < (4 * NFSX_UNSIGNED))
1065		panic("nfs_getnickauth verf too small");
1066#endif
1067	for (nuidp = NMUIDHASH(nmp, cred->cr_uid)->lh_first;
1068	    nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
1069		if (nuidp->nu_cr.cr_uid == cred->cr_uid)
1070			break;
1071	}
1072	if (!nuidp || nuidp->nu_expire < time_second)
1073		return (EACCES);
1074
1075	/*
1076	 * Move to the end of the lru list (end of lru == most recently used).
1077	 */
1078	TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
1079	TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, nu_lru);
1080
1081	nickp = (u_int32_t *)malloc(2 * NFSX_UNSIGNED, M_TEMP, M_WAITOK);
1082	*nickp++ = txdr_unsigned(RPCAKN_NICKNAME);
1083	*nickp = txdr_unsigned(nuidp->nu_nickname);
1084	*auth_str = (char *)nickp;
1085	*auth_len = 2 * NFSX_UNSIGNED;
1086
1087	/*
1088	 * Now we must encrypt the verifier and package it up.
1089	 */
1090	verfp = (u_int32_t *)verf_str;
1091	*verfp++ = txdr_unsigned(RPCAKN_NICKNAME);
1092	if (time_second > nuidp->nu_timestamp.tv_sec ||
1093	    (time_second == nuidp->nu_timestamp.tv_sec &&
1094	     time_second > nuidp->nu_timestamp.tv_usec))
1095		getmicrotime(&nuidp->nu_timestamp);
1096	else
1097		nuidp->nu_timestamp.tv_usec++;
1098	ktvin.tv_sec = txdr_unsigned(nuidp->nu_timestamp.tv_sec);
1099	ktvin.tv_usec = txdr_unsigned(nuidp->nu_timestamp.tv_usec);
1100
1101	/*
1102	 * Now encrypt the timestamp verifier in ecb mode using the session
1103	 * key.
1104	 */
1105#ifdef NFSKERB
1106	XXX
1107#endif
1108
1109	*verfp++ = ktvout.tv_sec;
1110	*verfp++ = ktvout.tv_usec;
1111	*verfp = 0;
1112	return (0);
1113}
1114
1115/*
1116 * Save the current nickname in a hash list entry on the mount point.
1117 */
1118int
1119nfs_savenickauth(nmp, cred, len, key, mdp, dposp, mrep)
1120	register struct nfsmount *nmp;
1121	struct ucred *cred;
1122	int len;
1123	NFSKERBKEY_T key;
1124	struct mbuf **mdp;
1125	char **dposp;
1126	struct mbuf *mrep;
1127{
1128	register struct nfsuid *nuidp;
1129	register u_int32_t *tl;
1130	register int32_t t1;
1131	struct mbuf *md = *mdp;
1132	struct timeval ktvin, ktvout;
1133	u_int32_t nick;
1134	char *dpos = *dposp, *cp2;
1135	int deltasec, error = 0;
1136
1137	if (len == (3 * NFSX_UNSIGNED)) {
1138		nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1139		ktvin.tv_sec = *tl++;
1140		ktvin.tv_usec = *tl++;
1141		nick = fxdr_unsigned(u_int32_t, *tl);
1142
1143		/*
1144		 * Decrypt the timestamp in ecb mode.
1145		 */
1146#ifdef NFSKERB
1147		XXX
1148#endif
1149		ktvout.tv_sec = fxdr_unsigned(long, ktvout.tv_sec);
1150		ktvout.tv_usec = fxdr_unsigned(long, ktvout.tv_usec);
1151		deltasec = time_second - ktvout.tv_sec;
1152		if (deltasec < 0)
1153			deltasec = -deltasec;
1154		/*
1155		 * If ok, add it to the hash list for the mount point.
1156		 */
1157		if (deltasec <= NFS_KERBCLOCKSKEW) {
1158			if (nmp->nm_numuids < nuidhash_max) {
1159				nmp->nm_numuids++;
1160				nuidp = (struct nfsuid *)
1161				   malloc(sizeof (struct nfsuid), M_NFSUID,
1162					M_WAITOK);
1163			} else {
1164				nuidp = nmp->nm_uidlruhead.tqh_first;
1165				LIST_REMOVE(nuidp, nu_hash);
1166				TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp,
1167					nu_lru);
1168			}
1169			nuidp->nu_flag = 0;
1170			nuidp->nu_cr.cr_uid = cred->cr_uid;
1171			nuidp->nu_expire = time_second + NFS_KERBTTL;
1172			nuidp->nu_timestamp = ktvout;
1173			nuidp->nu_nickname = nick;
1174			bcopy(key, nuidp->nu_key, sizeof (key));
1175			TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp,
1176				nu_lru);
1177			LIST_INSERT_HEAD(NMUIDHASH(nmp, cred->cr_uid),
1178				nuidp, nu_hash);
1179		}
1180	} else
1181		nfsm_adv(nfsm_rndup(len));
1182nfsmout:
1183	*mdp = md;
1184	*dposp = dpos;
1185	return (error);
1186}
1187