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