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