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