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