xref: /original-bsd/sys/nfs/nfs_syscalls.c (revision 3705696b)
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  * %sccs.include.redist.c%
9  *
10  *	@(#)nfs_syscalls.c	8.1 (Berkeley) 06/10/93
11  */
12 
13 #include <sys/param.h>
14 #include <sys/systm.h>
15 #include <sys/kernel.h>
16 #include <sys/file.h>
17 #include <sys/stat.h>
18 #include <sys/vnode.h>
19 #include <sys/mount.h>
20 #include <sys/proc.h>
21 #include <sys/uio.h>
22 #include <sys/malloc.h>
23 #include <sys/buf.h>
24 #include <sys/mbuf.h>
25 #include <sys/socket.h>
26 #include <sys/socketvar.h>
27 #include <sys/domain.h>
28 #include <sys/protosw.h>
29 #include <sys/namei.h>
30 #include <sys/syslog.h>
31 
32 #include <netinet/in.h>
33 #include <netinet/tcp.h>
34 #ifdef ISO
35 #include <netiso/iso.h>
36 #endif
37 #include <nfs/rpcv2.h>
38 #include <nfs/nfsv2.h>
39 #include <nfs/nfs.h>
40 #include <nfs/nfsrvcache.h>
41 #include <nfs/nfsmount.h>
42 #include <nfs/nqnfs.h>
43 #include <nfs/nfsrtt.h>
44 
45 /* Global defs. */
46 extern u_long nfs_prog, nfs_vers;
47 extern int (*nfsrv_procs[NFS_NPROCS])();
48 extern struct queue_entry nfs_bufq;
49 extern struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON];
50 extern int nfs_numasync;
51 extern time_t nqnfsstarttime;
52 extern struct nfsrv_req nsrvq_head;
53 extern struct nfsd nfsd_head;
54 extern int nqsrv_writeslack;
55 extern int nfsrtton;
56 struct nfssvc_sock *nfs_udpsock, *nfs_cltpsock;
57 int nuidhash_max = NFS_MAXUIDHASH;
58 static int nfs_numnfsd = 0;
59 int nfsd_waiting = 0;
60 static int notstarted = 1;
61 static int modify_flag = 0;
62 static struct nfsdrt nfsdrt;
63 void nfsrv_cleancache(), nfsrv_rcv(), nfsrv_wakenfsd(), nfs_sndunlock();
64 static void nfsd_rt();
65 void nfsrv_slpderef(), nfsrv_init();
66 
67 #define	TRUE	1
68 #define	FALSE	0
69 
70 static int nfs_asyncdaemon[NFS_MAXASYNCDAEMON];
71 /*
72  * NFS server system calls
73  * getfh() lives here too, but maybe should move to kern/vfs_syscalls.c
74  */
75 
76 /*
77  * Get file handle system call
78  */
79 struct getfh_args {
80 	char	*fname;
81 	fhandle_t *fhp;
82 };
83 getfh(p, uap, retval)
84 	struct proc *p;
85 	register struct getfh_args *uap;
86 	int *retval;
87 {
88 	register struct vnode *vp;
89 	fhandle_t fh;
90 	int error;
91 	struct nameidata nd;
92 
93 	/*
94 	 * Must be super user
95 	 */
96 	if (error = suser(p->p_ucred, &p->p_acflag))
97 		return (error);
98 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, uap->fname, p);
99 	if (error = namei(&nd))
100 		return (error);
101 	vp = nd.ni_vp;
102 	bzero((caddr_t)&fh, sizeof(fh));
103 	fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
104 	error = VFS_VPTOFH(vp, &fh.fh_fid);
105 	vput(vp);
106 	if (error)
107 		return (error);
108 	error = copyout((caddr_t)&fh, (caddr_t)uap->fhp, sizeof (fh));
109 	return (error);
110 }
111 
112 static struct nfssvc_sock nfssvc_sockhead;
113 
114 /*
115  * Nfs server psuedo system call for the nfsd's
116  * Based on the flag value it either:
117  * - adds a socket to the selection list
118  * - remains in the kernel as an nfsd
119  * - remains in the kernel as an nfsiod
120  */
121 struct nfssvc_args {
122 	int flag;
123 	caddr_t argp;
124 };
125 nfssvc(p, uap, retval)
126 	struct proc *p;
127 	register struct nfssvc_args *uap;
128 	int *retval;
129 {
130 	struct nameidata nd;
131 	struct file *fp;
132 	struct mbuf *nam;
133 	struct nfsd_args nfsdarg;
134 	struct nfsd_srvargs nfsd_srvargs, *nsd = &nfsd_srvargs;
135 	struct nfsd_cargs ncd;
136 	struct nfsd *nfsd;
137 	struct nfssvc_sock *slp;
138 	struct nfsuid *nuidp, **nuh;
139 	struct nfsmount *nmp;
140 	int error;
141 
142 	/*
143 	 * Must be super user
144 	 */
145 	if (error = suser(p->p_ucred, &p->p_acflag))
146 		return (error);
147 	while (nfssvc_sockhead.ns_flag & SLP_INIT) {
148 		nfssvc_sockhead.ns_flag |= SLP_WANTINIT;
149 		(void) tsleep((caddr_t)&nfssvc_sockhead, PSOCK, "nfsd init", 0);
150 	}
151 	if (uap->flag & NFSSVC_BIOD)
152 		error = nfssvc_iod(p);
153 	else if (uap->flag & NFSSVC_MNTD) {
154 		if (error = copyin(uap->argp, (caddr_t)&ncd, sizeof (ncd)))
155 			return (error);
156 		NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
157 			ncd.ncd_dirp, p);
158 		if (error = namei(&nd))
159 			return (error);
160 		if ((nd.ni_vp->v_flag & VROOT) == 0)
161 			error = EINVAL;
162 		nmp = VFSTONFS(nd.ni_vp->v_mount);
163 		vput(nd.ni_vp);
164 		if (error)
165 			return (error);
166 		if ((nmp->nm_flag & NFSMNT_MNTD) &&
167 			(uap->flag & NFSSVC_GOTAUTH) == 0)
168 			return (0);
169 		nmp->nm_flag |= NFSMNT_MNTD;
170 		error = nqnfs_clientd(nmp, p->p_ucred, &ncd, uap->flag,
171 			uap->argp, p);
172 	} else if (uap->flag & NFSSVC_ADDSOCK) {
173 		if (error = copyin(uap->argp, (caddr_t)&nfsdarg,
174 		    sizeof(nfsdarg)))
175 			return (error);
176 		if (error = getsock(p->p_fd, nfsdarg.sock, &fp))
177 			return (error);
178 		/*
179 		 * Get the client address for connected sockets.
180 		 */
181 		if (nfsdarg.name == NULL || nfsdarg.namelen == 0)
182 			nam = (struct mbuf *)0;
183 		else if (error = sockargs(&nam, nfsdarg.name, nfsdarg.namelen,
184 			MT_SONAME))
185 			return (error);
186 		error = nfssvc_addsock(fp, nam);
187 	} else {
188 		if (error = copyin(uap->argp, (caddr_t)nsd, sizeof (*nsd)))
189 			return (error);
190 		if ((uap->flag & NFSSVC_AUTHIN) && (nfsd = nsd->nsd_nfsd) &&
191 			(nfsd->nd_slp->ns_flag & SLP_VALID)) {
192 			slp = nfsd->nd_slp;
193 
194 			/*
195 			 * First check to see if another nfsd has already
196 			 * added this credential.
197 			 */
198 			nuidp = slp->ns_uidh[NUIDHASH(nsd->nsd_uid)];
199 			while (nuidp) {
200 				if (nuidp->nu_uid == nsd->nsd_uid)
201 					break;
202 				nuidp = nuidp->nu_hnext;
203 			}
204 			if (!nuidp) {
205 			    /*
206 			     * Nope, so we will.
207 			     */
208 			    if (slp->ns_numuids < nuidhash_max) {
209 				slp->ns_numuids++;
210 				nuidp = (struct nfsuid *)
211 				   malloc(sizeof (struct nfsuid), M_NFSUID,
212 					M_WAITOK);
213 			    } else
214 				nuidp = (struct nfsuid *)0;
215 			    if ((slp->ns_flag & SLP_VALID) == 0) {
216 				if (nuidp)
217 				    free((caddr_t)nuidp, M_NFSUID);
218 			    } else {
219 				if (nuidp == (struct nfsuid *)0) {
220 				    nuidp = slp->ns_lruprev;
221 				    remque(nuidp);
222 				    if (nuidp->nu_hprev)
223 					nuidp->nu_hprev->nu_hnext =
224 					    nuidp->nu_hnext;
225 				    if (nuidp->nu_hnext)
226 					nuidp->nu_hnext->nu_hprev =
227 					    nuidp->nu_hprev;
228 			        }
229 				nuidp->nu_cr = nsd->nsd_cr;
230 				if (nuidp->nu_cr.cr_ngroups > NGROUPS)
231 					nuidp->nu_cr.cr_ngroups = NGROUPS;
232 				nuidp->nu_cr.cr_ref = 1;
233 				nuidp->nu_uid = nsd->nsd_uid;
234 				insque(nuidp, (struct nfsuid *)slp);
235 				nuh = &slp->ns_uidh[NUIDHASH(nsd->nsd_uid)];
236 				if (nuidp->nu_hnext = *nuh)
237 				    nuidp->nu_hnext->nu_hprev = nuidp;
238 				nuidp->nu_hprev = (struct nfsuid *)0;
239 				*nuh = nuidp;
240 			    }
241 			}
242 		}
243 		if ((uap->flag & NFSSVC_AUTHINFAIL) && (nfsd = nsd->nsd_nfsd))
244 			nfsd->nd_flag |= NFSD_AUTHFAIL;
245 		error = nfssvc_nfsd(nsd, uap->argp, p);
246 	}
247 	if (error == EINTR || error == ERESTART)
248 		error = 0;
249 	return (error);
250 }
251 
252 /*
253  * Adds a socket to the list for servicing by nfsds.
254  */
255 nfssvc_addsock(fp, mynam)
256 	struct file *fp;
257 	struct mbuf *mynam;
258 {
259 	register struct mbuf *m;
260 	register int siz;
261 	register struct nfssvc_sock *slp;
262 	register struct socket *so;
263 	struct nfssvc_sock *tslp;
264 	int error, s;
265 
266 	so = (struct socket *)fp->f_data;
267 	tslp = (struct nfssvc_sock *)0;
268 	/*
269 	 * Add it to the list, as required.
270 	 */
271 	if (so->so_proto->pr_protocol == IPPROTO_UDP) {
272 		tslp = nfs_udpsock;
273 		if (tslp->ns_flag & SLP_VALID) {
274 			m_freem(mynam);
275 			return (EPERM);
276 		}
277 #ifdef ISO
278 	} else if (so->so_proto->pr_protocol == ISOPROTO_CLTP) {
279 		tslp = nfs_cltpsock;
280 		if (tslp->ns_flag & SLP_VALID) {
281 			m_freem(mynam);
282 			return (EPERM);
283 		}
284 #endif /* ISO */
285 	}
286 	if (so->so_type == SOCK_STREAM)
287 		siz = NFS_MAXPACKET + sizeof (u_long);
288 	else
289 		siz = NFS_MAXPACKET;
290 	if (error = soreserve(so, siz, siz)) {
291 		m_freem(mynam);
292 		return (error);
293 	}
294 
295 	/*
296 	 * Set protocol specific options { for now TCP only } and
297 	 * reserve some space. For datagram sockets, this can get called
298 	 * repeatedly for the same socket, but that isn't harmful.
299 	 */
300 	if (so->so_type == SOCK_STREAM) {
301 		MGET(m, M_WAIT, MT_SOOPTS);
302 		*mtod(m, int *) = 1;
303 		m->m_len = sizeof(int);
304 		sosetopt(so, SOL_SOCKET, SO_KEEPALIVE, m);
305 	}
306 	if (so->so_proto->pr_domain->dom_family == AF_INET &&
307 	    so->so_proto->pr_protocol == IPPROTO_TCP) {
308 		MGET(m, M_WAIT, MT_SOOPTS);
309 		*mtod(m, int *) = 1;
310 		m->m_len = sizeof(int);
311 		sosetopt(so, IPPROTO_TCP, TCP_NODELAY, m);
312 	}
313 	so->so_rcv.sb_flags &= ~SB_NOINTR;
314 	so->so_rcv.sb_timeo = 0;
315 	so->so_snd.sb_flags &= ~SB_NOINTR;
316 	so->so_snd.sb_timeo = 0;
317 	if (tslp)
318 		slp = tslp;
319 	else {
320 		slp = (struct nfssvc_sock *)
321 			malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
322 		bzero((caddr_t)slp, sizeof (struct nfssvc_sock));
323 		slp->ns_prev = nfssvc_sockhead.ns_prev;
324 		slp->ns_prev->ns_next = slp;
325 		slp->ns_next = &nfssvc_sockhead;
326 		nfssvc_sockhead.ns_prev = slp;
327 		slp->ns_lrunext = slp->ns_lruprev = (struct nfsuid *)slp;
328 	}
329 	slp->ns_so = so;
330 	slp->ns_nam = mynam;
331 	fp->f_count++;
332 	slp->ns_fp = fp;
333 	s = splnet();
334 	so->so_upcallarg = (caddr_t)slp;
335 	so->so_upcall = nfsrv_rcv;
336 	slp->ns_flag = (SLP_VALID | SLP_NEEDQ);
337 	nfsrv_wakenfsd(slp);
338 	splx(s);
339 	return (0);
340 }
341 
342 /*
343  * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
344  * until it is killed by a signal.
345  */
346 nfssvc_nfsd(nsd, argp, p)
347 	struct nfsd_srvargs *nsd;
348 	caddr_t argp;
349 	struct proc *p;
350 {
351 	register struct mbuf *m, *nam2;
352 	register int siz;
353 	register struct nfssvc_sock *slp;
354 	register struct socket *so;
355 	register int *solockp;
356 	struct nfssvc_sock *oslp;
357 	struct nfsd *nd = nsd->nsd_nfsd;
358 	struct mbuf *mreq, *nam;
359 	struct timeval starttime;
360 	struct nfsuid *uidp;
361 	int error, cacherep, s;
362 	int sotype;
363 
364 	s = splnet();
365 	if (nd == (struct nfsd *)0) {
366 		nsd->nsd_nfsd = nd = (struct nfsd *)
367 			malloc(sizeof (struct nfsd), M_NFSD, M_WAITOK);
368 		bzero((caddr_t)nd, sizeof (struct nfsd));
369 		nd->nd_procp = p;
370 		nd->nd_cr.cr_ref = 1;
371 		insque(nd, &nfsd_head);
372 		nd->nd_nqlflag = NQL_NOVAL;
373 		nfs_numnfsd++;
374 	}
375 	/*
376 	 * Loop getting rpc requests until SIGKILL.
377 	 */
378 	for (;;) {
379 		if ((nd->nd_flag & NFSD_REQINPROG) == 0) {
380 			while (nd->nd_slp == (struct nfssvc_sock *)0 &&
381 				 (nfsd_head.nd_flag & NFSD_CHECKSLP) == 0) {
382 				nd->nd_flag |= NFSD_WAITING;
383 				nfsd_waiting++;
384 				error = tsleep((caddr_t)nd, PSOCK | PCATCH, "nfsd", 0);
385 				nfsd_waiting--;
386 				if (error)
387 					goto done;
388 			}
389 			if (nd->nd_slp == (struct nfssvc_sock *)0 &&
390 				(nfsd_head.nd_flag & NFSD_CHECKSLP)) {
391 				slp = nfssvc_sockhead.ns_next;
392 				while (slp != &nfssvc_sockhead) {
393 				    if ((slp->ns_flag & (SLP_VALID | SLP_DOREC))
394 					== (SLP_VALID | SLP_DOREC)) {
395 					    slp->ns_flag &= ~SLP_DOREC;
396 					    slp->ns_sref++;
397 					    nd->nd_slp = slp;
398 					    break;
399 				    }
400 				    slp = slp->ns_next;
401 				}
402 				if (slp == &nfssvc_sockhead)
403 					nfsd_head.nd_flag &= ~NFSD_CHECKSLP;
404 			}
405 			if ((slp = nd->nd_slp) == (struct nfssvc_sock *)0)
406 				continue;
407 			if (slp->ns_flag & SLP_VALID) {
408 				if (slp->ns_flag & SLP_DISCONN)
409 					nfsrv_zapsock(slp);
410 				else if (slp->ns_flag & SLP_NEEDQ) {
411 					slp->ns_flag &= ~SLP_NEEDQ;
412 					(void) nfs_sndlock(&slp->ns_solock,
413 						(struct nfsreq *)0);
414 					nfsrv_rcv(slp->ns_so, (caddr_t)slp,
415 						M_WAIT);
416 					nfs_sndunlock(&slp->ns_solock);
417 				}
418 				error = nfsrv_dorec(slp, nd);
419 				nd->nd_flag |= NFSD_REQINPROG;
420 			}
421 		} else {
422 			error = 0;
423 			slp = nd->nd_slp;
424 		}
425 		if (error || (slp->ns_flag & SLP_VALID) == 0) {
426 			nd->nd_slp = (struct nfssvc_sock *)0;
427 			nd->nd_flag &= ~NFSD_REQINPROG;
428 			nfsrv_slpderef(slp);
429 			continue;
430 		}
431 		splx(s);
432 		so = slp->ns_so;
433 		sotype = so->so_type;
434 		starttime = time;
435 		if (so->so_proto->pr_flags & PR_CONNREQUIRED)
436 			solockp = &slp->ns_solock;
437 		else
438 			solockp = (int *)0;
439 		/*
440 		 * nam == nam2 for connectionless protocols such as UDP
441 		 * nam2 == NULL for connection based protocols to disable
442 		 *    recent request caching.
443 		 */
444 		if (nam2 = nd->nd_nam) {
445 			nam = nam2;
446 			cacherep = RC_CHECKIT;
447 		} else {
448 			nam = slp->ns_nam;
449 			cacherep = RC_DOIT;
450 		}
451 
452 		/*
453 		 * Check to see if authorization is needed.
454 		 */
455 		if (nd->nd_flag & NFSD_NEEDAUTH) {
456 			static int logauth = 0;
457 
458 			nd->nd_flag &= ~NFSD_NEEDAUTH;
459 			/*
460 			 * Check for a mapping already installed.
461 			 */
462 			uidp = slp->ns_uidh[NUIDHASH(nd->nd_cr.cr_uid)];
463 			while (uidp) {
464 				if (uidp->nu_uid == nd->nd_cr.cr_uid)
465 					break;
466 				uidp = uidp->nu_hnext;
467 			}
468 			if (!uidp) {
469 			    nsd->nsd_uid = nd->nd_cr.cr_uid;
470 			    if (nam2 && logauth++ == 0)
471 				log(LOG_WARNING, "Kerberized NFS using UDP\n");
472 			    nsd->nsd_haddr =
473 			      mtod(nam, struct sockaddr_in *)->sin_addr.s_addr;
474 			    nsd->nsd_authlen = nd->nd_authlen;
475 			    if (copyout(nd->nd_authstr, nsd->nsd_authstr,
476 				nd->nd_authlen) == 0 &&
477 				copyout((caddr_t)nsd, argp, sizeof (*nsd)) == 0)
478 				return (ENEEDAUTH);
479 			    cacherep = RC_DROPIT;
480 			}
481 		}
482 		if (cacherep == RC_CHECKIT)
483 			cacherep = nfsrv_getcache(nam2, nd, &mreq);
484 
485 		/*
486 		 * Check for just starting up for NQNFS and send
487 		 * fake "try again later" replies to the NQNFS clients.
488 		 */
489 		if (notstarted && nqnfsstarttime <= time.tv_sec) {
490 			if (modify_flag) {
491 				nqnfsstarttime = time.tv_sec + nqsrv_writeslack;
492 				modify_flag = 0;
493 			} else
494 				notstarted = 0;
495 		}
496 		if (notstarted) {
497 			if (nd->nd_nqlflag == NQL_NOVAL)
498 				cacherep = RC_DROPIT;
499 			else if (nd->nd_procnum != NFSPROC_WRITE) {
500 				nd->nd_procnum = NFSPROC_NOOP;
501 				nd->nd_repstat = NQNFS_TRYLATER;
502 				cacherep = RC_DOIT;
503 			} else
504 				modify_flag = 1;
505 		} else if (nd->nd_flag & NFSD_AUTHFAIL) {
506 			nd->nd_flag &= ~NFSD_AUTHFAIL;
507 			nd->nd_procnum = NFSPROC_NOOP;
508 			nd->nd_repstat = NQNFS_AUTHERR;
509 			cacherep = RC_DOIT;
510 		}
511 
512 		switch (cacherep) {
513 		case RC_DOIT:
514 			error = (*(nfsrv_procs[nd->nd_procnum]))(nd,
515 				nd->nd_mrep, nd->nd_md, nd->nd_dpos, &nd->nd_cr,
516 				nam, &mreq);
517 			if (nd->nd_cr.cr_ref != 1) {
518 				printf("nfssvc cref=%d\n", nd->nd_cr.cr_ref);
519 				panic("nfssvc cref");
520 			}
521 			if (error) {
522 				if (nd->nd_procnum != NQNFSPROC_VACATED)
523 					nfsstats.srv_errs++;
524 				if (nam2) {
525 					nfsrv_updatecache(nam2, nd, FALSE, mreq);
526 					m_freem(nam2);
527 				}
528 				break;
529 			}
530 			nfsstats.srvrpccnt[nd->nd_procnum]++;
531 			if (nam2)
532 				nfsrv_updatecache(nam2, nd, TRUE, mreq);
533 			nd->nd_mrep = (struct mbuf *)0;
534 		case RC_REPLY:
535 			m = mreq;
536 			siz = 0;
537 			while (m) {
538 				siz += m->m_len;
539 				m = m->m_next;
540 			}
541 			if (siz <= 0 || siz > NFS_MAXPACKET) {
542 				printf("mbuf siz=%d\n",siz);
543 				panic("Bad nfs svc reply");
544 			}
545 			m = mreq;
546 			m->m_pkthdr.len = siz;
547 			m->m_pkthdr.rcvif = (struct ifnet *)0;
548 			/*
549 			 * For stream protocols, prepend a Sun RPC
550 			 * Record Mark.
551 			 */
552 			if (sotype == SOCK_STREAM) {
553 				M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
554 				*mtod(m, u_long *) = htonl(0x80000000 | siz);
555 			}
556 			if (solockp)
557 				(void) nfs_sndlock(solockp, (struct nfsreq *)0);
558 			if (slp->ns_flag & SLP_VALID)
559 			    error = nfs_send(so, nam2, m, (struct nfsreq *)0);
560 			else {
561 			    error = EPIPE;
562 			    m_freem(m);
563 			}
564 			if (nfsrtton)
565 				nfsd_rt(&starttime, sotype, nd, nam, cacherep);
566 			if (nam2)
567 				MFREE(nam2, m);
568 			if (nd->nd_mrep)
569 				m_freem(nd->nd_mrep);
570 			if (error == EPIPE)
571 				nfsrv_zapsock(slp);
572 			if (solockp)
573 				nfs_sndunlock(solockp);
574 			if (error == EINTR || error == ERESTART) {
575 				nfsrv_slpderef(slp);
576 				s = splnet();
577 				goto done;
578 			}
579 			break;
580 		case RC_DROPIT:
581 			if (nfsrtton)
582 				nfsd_rt(&starttime, sotype, nd, nam, cacherep);
583 			m_freem(nd->nd_mrep);
584 			m_freem(nam2);
585 			break;
586 		};
587 		s = splnet();
588 		if (nfsrv_dorec(slp, nd)) {
589 			nd->nd_flag &= ~NFSD_REQINPROG;
590 			nd->nd_slp = (struct nfssvc_sock *)0;
591 			nfsrv_slpderef(slp);
592 		}
593 	}
594 done:
595 	remque(nd);
596 	splx(s);
597 	free((caddr_t)nd, M_NFSD);
598 	nsd->nsd_nfsd = (struct nfsd *)0;
599 	if (--nfs_numnfsd == 0)
600 		nfsrv_init(TRUE);	/* Reinitialize everything */
601 	return (error);
602 }
603 
604 /*
605  * Asynchronous I/O daemons for client nfs.
606  * They do read-ahead and write-behind operations on the block I/O cache.
607  * Never returns unless it fails or gets killed.
608  */
609 nfssvc_iod(p)
610 	struct proc *p;
611 {
612 	register struct buf *bp, *dp;
613 	register int i, myiod;
614 	int error = 0;
615 
616 	/*
617 	 * Assign my position or return error if too many already running
618 	 */
619 	myiod = -1;
620 	for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
621 		if (nfs_asyncdaemon[i] == 0) {
622 			nfs_asyncdaemon[i]++;
623 			myiod = i;
624 			break;
625 		}
626 	if (myiod == -1)
627 		return (EBUSY);
628 	nfs_numasync++;
629 	/*
630 	 * Just loop around doin our stuff until SIGKILL
631 	 */
632 	for (;;) {
633 		while (nfs_bufq.qe_next == NULL && error == 0) {
634 			nfs_iodwant[myiod] = p;
635 			error = tsleep((caddr_t)&nfs_iodwant[myiod],
636 				PWAIT | PCATCH, "nfsidl", 0);
637 		}
638 		while ((bp = nfs_bufq.qe_next) != NULL) {
639 			/* Take one off the front of the list */
640 			queue_remove(&nfs_bufq, bp, struct buf *, b_freelist);
641 			if (bp->b_flags & B_READ)
642 			    (void) nfs_doio(bp, bp->b_rcred, (struct proc *)0);
643 			else
644 			    (void) nfs_doio(bp, bp->b_wcred, (struct proc *)0);
645 		}
646 		if (error) {
647 			nfs_asyncdaemon[myiod] = 0;
648 			nfs_numasync--;
649 			return (error);
650 		}
651 	}
652 }
653 
654 /*
655  * Shut down a socket associated with an nfssvc_sock structure.
656  * Should be called with the send lock set, if required.
657  * The trick here is to increment the sref at the start, so that the nfsds
658  * will stop using it and clear ns_flag at the end so that it will not be
659  * reassigned during cleanup.
660  */
661 nfsrv_zapsock(slp)
662 	register struct nfssvc_sock *slp;
663 {
664 	register struct nfsuid *nuidp, *onuidp;
665 	register int i;
666 	struct socket *so;
667 	struct file *fp;
668 	struct mbuf *m;
669 
670 	slp->ns_flag &= ~SLP_ALLFLAGS;
671 	if (fp = slp->ns_fp) {
672 		slp->ns_fp = (struct file *)0;
673 		so = slp->ns_so;
674 		so->so_upcall = NULL;
675 		soshutdown(so, 2);
676 		closef(fp, (struct proc *)0);
677 		if (slp->ns_nam)
678 			MFREE(slp->ns_nam, m);
679 		m_freem(slp->ns_raw);
680 		m_freem(slp->ns_rec);
681 		nuidp = slp->ns_lrunext;
682 		while (nuidp != (struct nfsuid *)slp) {
683 			onuidp = nuidp;
684 			nuidp = nuidp->nu_lrunext;
685 			free((caddr_t)onuidp, M_NFSUID);
686 		}
687 		slp->ns_lrunext = slp->ns_lruprev = (struct nfsuid *)slp;
688 		for (i = 0; i < NUIDHASHSIZ; i++)
689 			slp->ns_uidh[i] = (struct nfsuid *)0;
690 	}
691 }
692 
693 /*
694  * Get an authorization string for the uid by having the mount_nfs sitting
695  * on this mount point porpous out of the kernel and do it.
696  */
697 nfs_getauth(nmp, rep, cred, auth_type, auth_str, auth_len)
698 	register struct nfsmount *nmp;
699 	struct nfsreq *rep;
700 	struct ucred *cred;
701 	int *auth_type;
702 	char **auth_str;
703 	int *auth_len;
704 {
705 	int error = 0;
706 
707 	while ((nmp->nm_flag & NFSMNT_WAITAUTH) == 0) {
708 		nmp->nm_flag |= NFSMNT_WANTAUTH;
709 		(void) tsleep((caddr_t)&nmp->nm_authtype, PSOCK,
710 			"nfsauth1", 2 * hz);
711 		if (error = nfs_sigintr(nmp, rep, rep->r_procp)) {
712 			nmp->nm_flag &= ~NFSMNT_WANTAUTH;
713 			return (error);
714 		}
715 	}
716 	nmp->nm_flag &= ~(NFSMNT_WAITAUTH | NFSMNT_WANTAUTH);
717 	nmp->nm_authstr = *auth_str = (char *)malloc(RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK);
718 	nmp->nm_authuid = cred->cr_uid;
719 	wakeup((caddr_t)&nmp->nm_authstr);
720 
721 	/*
722 	 * And wait for mount_nfs to do its stuff.
723 	 */
724 	while ((nmp->nm_flag & NFSMNT_HASAUTH) == 0 && error == 0) {
725 		(void) tsleep((caddr_t)&nmp->nm_authlen, PSOCK,
726 			"nfsauth2", 2 * hz);
727 		error = nfs_sigintr(nmp, rep, rep->r_procp);
728 	}
729 	if (nmp->nm_flag & NFSMNT_AUTHERR) {
730 		nmp->nm_flag &= ~NFSMNT_AUTHERR;
731 		error = EAUTH;
732 	}
733 	if (error)
734 		free((caddr_t)*auth_str, M_TEMP);
735 	else {
736 		*auth_type = nmp->nm_authtype;
737 		*auth_len = nmp->nm_authlen;
738 	}
739 	nmp->nm_flag &= ~NFSMNT_HASAUTH;
740 	nmp->nm_flag |= NFSMNT_WAITAUTH;
741 	if (nmp->nm_flag & NFSMNT_WANTAUTH) {
742 		nmp->nm_flag &= ~NFSMNT_WANTAUTH;
743 		wakeup((caddr_t)&nmp->nm_authtype);
744 	}
745 	return (error);
746 }
747 
748 /*
749  * Derefence a server socket structure. If it has no more references and
750  * is no longer valid, you can throw it away.
751  */
752 void
753 nfsrv_slpderef(slp)
754 	register struct nfssvc_sock *slp;
755 {
756 	if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) {
757 		slp->ns_prev->ns_next = slp->ns_next;
758 		slp->ns_next->ns_prev = slp->ns_prev;
759 		free((caddr_t)slp, M_NFSSVC);
760 	}
761 }
762 
763 /*
764  * Initialize the data structures for the server.
765  * Handshake with any new nfsds starting up to avoid any chance of
766  * corruption.
767  */
768 void
769 nfsrv_init(terminating)
770 	int terminating;
771 {
772 	register struct nfssvc_sock *slp;
773 	struct nfssvc_sock *oslp;
774 
775 	if (nfssvc_sockhead.ns_flag & SLP_INIT)
776 		panic("nfsd init");
777 	nfssvc_sockhead.ns_flag |= SLP_INIT;
778 	if (terminating) {
779 		slp = nfssvc_sockhead.ns_next;
780 		while (slp != &nfssvc_sockhead) {
781 			if (slp->ns_flag & SLP_VALID)
782 				nfsrv_zapsock(slp);
783 			slp->ns_next->ns_prev = slp->ns_prev;
784 			slp->ns_prev->ns_next = slp->ns_next;
785 			oslp = slp;
786 			slp = slp->ns_next;
787 			free((caddr_t)oslp, M_NFSSVC);
788 		}
789 		nfsrv_cleancache();	/* And clear out server cache */
790 	}
791 	nfs_udpsock = (struct nfssvc_sock *)
792 	    malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
793 	bzero((caddr_t)nfs_udpsock, sizeof (struct nfssvc_sock));
794 	nfs_cltpsock = (struct nfssvc_sock *)
795 	    malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
796 	bzero((caddr_t)nfs_cltpsock, sizeof (struct nfssvc_sock));
797 	nfssvc_sockhead.ns_next = nfs_udpsock;
798 	nfs_udpsock->ns_next = nfs_cltpsock;
799 	nfs_cltpsock->ns_next = &nfssvc_sockhead;
800 	nfssvc_sockhead.ns_prev = nfs_cltpsock;
801 	nfs_cltpsock->ns_prev = nfs_udpsock;
802 	nfs_udpsock->ns_prev = &nfssvc_sockhead;
803 	nfs_udpsock->ns_lrunext = nfs_udpsock->ns_lruprev =
804 		(struct nfsuid *)nfs_udpsock;
805 	nfs_cltpsock->ns_lrunext = nfs_cltpsock->ns_lruprev =
806 		(struct nfsuid *)nfs_cltpsock;
807 	nfsd_head.nd_next = nfsd_head.nd_prev = &nfsd_head;
808 	nfsd_head.nd_flag = 0;
809 	nfssvc_sockhead.ns_flag &= ~SLP_INIT;
810 	if (nfssvc_sockhead.ns_flag & SLP_WANTINIT) {
811 		nfssvc_sockhead.ns_flag &= ~SLP_WANTINIT;
812 		wakeup((caddr_t)&nfssvc_sockhead);
813 	}
814 }
815 
816 /*
817  * Add entries to the server monitor log.
818  */
819 static void
820 nfsd_rt(startp, sotype, nd, nam, cacherep)
821 	struct timeval *startp;
822 	int sotype;
823 	register struct nfsd *nd;
824 	struct mbuf *nam;
825 	int cacherep;
826 {
827 	register struct drt *rt;
828 
829 	rt = &nfsdrt.drt[nfsdrt.pos];
830 	if (cacherep == RC_DOIT)
831 		rt->flag = 0;
832 	else if (cacherep == RC_REPLY)
833 		rt->flag = DRT_CACHEREPLY;
834 	else
835 		rt->flag = DRT_CACHEDROP;
836 	if (sotype == SOCK_STREAM)
837 		rt->flag |= DRT_TCP;
838 	if (nd->nd_nqlflag != NQL_NOVAL)
839 		rt->flag |= DRT_NQNFS;
840 	rt->proc = nd->nd_procnum;
841 	if (mtod(nam, struct sockaddr *)->sa_family == AF_INET)
842 		rt->ipadr = mtod(nam, struct sockaddr_in *)->sin_addr.s_addr;
843 	else
844 		rt->ipadr = INADDR_ANY;
845 	rt->resptime = ((time.tv_sec - startp->tv_sec) * 1000000) +
846 		(time.tv_usec - startp->tv_usec);
847 	rt->tstamp = time;
848 	nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ;
849 }
850