Home | History | Annotate | Line # | Download | only in nfs
nfs_syscalls.c revision 1.69
      1  1.68   darrenr /*	$NetBSD: nfs_syscalls.c,v 1.69 2003/06/29 22:32:18 fvdl 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.19      fvdl  *	@(#)nfs_syscalls.c	8.5 (Berkeley) 3/30/95
     39   1.1       cgd  */
     40  1.49     lukem 
     41  1.49     lukem #include <sys/cdefs.h>
     42  1.68   darrenr __KERNEL_RCSID(0, "$NetBSD: nfs_syscalls.c,v 1.69 2003/06/29 22:32:18 fvdl Exp $");
     43  1.28   thorpej 
     44  1.28   thorpej #include "fs_nfs.h"
     45  1.43     bjh21 #include "opt_nfs.h"
     46  1.30   thorpej #include "opt_nfsserver.h"
     47  1.31  jonathan #include "opt_iso.h"
     48  1.41      fvdl #include "opt_inet.h"
     49  1.39    tsarna #include "opt_compat_netbsd.h"
     50   1.1       cgd 
     51   1.7   mycroft #include <sys/param.h>
     52   1.7   mycroft #include <sys/systm.h>
     53   1.7   mycroft #include <sys/kernel.h>
     54   1.7   mycroft #include <sys/file.h>
     55   1.7   mycroft #include <sys/stat.h>
     56   1.7   mycroft #include <sys/vnode.h>
     57   1.7   mycroft #include <sys/mount.h>
     58   1.7   mycroft #include <sys/proc.h>
     59  1.10   mycroft #include <sys/uio.h>
     60   1.7   mycroft #include <sys/malloc.h>
     61   1.7   mycroft #include <sys/buf.h>
     62   1.7   mycroft #include <sys/mbuf.h>
     63   1.7   mycroft #include <sys/socket.h>
     64   1.7   mycroft #include <sys/socketvar.h>
     65  1.39    tsarna #include <sys/signalvar.h>
     66   1.7   mycroft #include <sys/domain.h>
     67   1.7   mycroft #include <sys/protosw.h>
     68  1.10   mycroft #include <sys/namei.h>
     69  1.10   mycroft #include <sys/syslog.h>
     70  1.18  christos #include <sys/filedesc.h>
     71  1.39    tsarna #include <sys/kthread.h>
     72   1.1       cgd 
     73  1.53   thorpej #include <sys/sa.h>
     74  1.13       cgd #include <sys/syscallargs.h>
     75  1.13       cgd 
     76   1.7   mycroft #include <netinet/in.h>
     77   1.7   mycroft #include <netinet/tcp.h>
     78  1.10   mycroft #ifdef ISO
     79  1.10   mycroft #include <netiso/iso.h>
     80  1.10   mycroft #endif
     81  1.19      fvdl #include <nfs/xdr_subs.h>
     82  1.10   mycroft #include <nfs/rpcv2.h>
     83  1.19      fvdl #include <nfs/nfsproto.h>
     84   1.7   mycroft #include <nfs/nfs.h>
     85  1.19      fvdl #include <nfs/nfsm_subs.h>
     86   1.7   mycroft #include <nfs/nfsrvcache.h>
     87  1.10   mycroft #include <nfs/nfsmount.h>
     88  1.10   mycroft #include <nfs/nfsnode.h>
     89  1.10   mycroft #include <nfs/nqnfs.h>
     90  1.10   mycroft #include <nfs/nfsrtt.h>
     91  1.18  christos #include <nfs/nfs_var.h>
     92   1.1       cgd 
     93   1.1       cgd /* Global defs. */
     94  1.19      fvdl extern int32_t (*nfsrv3_procs[NFS_NPROCS]) __P((struct nfsrv_descript *,
     95  1.19      fvdl 						struct nfssvc_sock *,
     96  1.69      fvdl 						struct proc *, struct mbuf **));
     97  1.10   mycroft extern time_t nqnfsstarttime;
     98  1.19      fvdl extern int nfsrvw_procrastinate;
     99  1.51      matt 
    100  1.47       chs struct nfssvc_sock *nfs_udpsock;
    101  1.47       chs #ifdef ISO
    102  1.47       chs struct nfssvc_sock *nfs_cltpsock;
    103  1.47       chs #endif
    104  1.41      fvdl #ifdef INET6
    105  1.41      fvdl struct nfssvc_sock *nfs_udp6sock;
    106  1.41      fvdl #endif
    107  1.10   mycroft int nuidhash_max = NFS_MAXUIDHASH;
    108  1.18  christos int nfsd_waiting = 0;
    109  1.18  christos #ifdef NFSSERVER
    110  1.10   mycroft static int nfs_numnfsd = 0;
    111  1.10   mycroft static int notstarted = 1;
    112  1.10   mycroft static int modify_flag = 0;
    113  1.10   mycroft static struct nfsdrt nfsdrt;
    114  1.18  christos #endif
    115  1.51      matt 
    116  1.66      yamt #ifdef NFSSERVER
    117  1.66      yamt struct simplelock nfsd_slock = SIMPLELOCK_INITIALIZER;
    118  1.51      matt struct nfssvc_sockhead nfssvc_sockhead;
    119  1.66      yamt struct nfssvc_sockhead nfssvc_sockpending;
    120  1.51      matt struct nfsdhead nfsd_head;
    121  1.66      yamt struct nfsdidlehead nfsd_idle_head;
    122  1.51      matt 
    123  1.51      matt int nfssvc_sockhead_flag;
    124  1.51      matt int nfsd_head_flag;
    125  1.66      yamt #endif
    126   1.1       cgd 
    127  1.54   thorpej MALLOC_DEFINE(M_NFSUID, "NFS uid", "Nfs uid mapping structure");
    128   1.1       cgd 
    129  1.24   thorpej #ifdef NFS
    130  1.58      yamt struct nfs_iod nfs_asyncdaemon[NFS_MAXASYNCDAEMON];
    131  1.40    tsarna int nfs_niothreads = -1; /* == "0, and has never been set" */
    132  1.23   thorpej #endif
    133  1.19      fvdl 
    134  1.18  christos #ifdef NFSSERVER
    135  1.19      fvdl static void nfsd_rt __P((int, struct nfsrv_descript *, int));
    136  1.65      yamt static struct nfssvc_sock *nfsrv_sockalloc __P((void));
    137  1.18  christos #endif
    138  1.19      fvdl 
    139   1.1       cgd /*
    140   1.1       cgd  * NFS server system calls
    141   1.1       cgd  */
    142   1.1       cgd 
    143   1.1       cgd 
    144   1.1       cgd /*
    145  1.19      fvdl  * Nfs server pseudo system call for the nfsd's
    146  1.10   mycroft  * Based on the flag value it either:
    147  1.10   mycroft  * - adds a socket to the selection list
    148  1.10   mycroft  * - remains in the kernel as an nfsd
    149  1.10   mycroft  * - remains in the kernel as an nfsiod
    150   1.1       cgd  */
    151  1.18  christos int
    152  1.53   thorpej sys_nfssvc(l, v, retval)
    153  1.53   thorpej 	struct lwp *l;
    154  1.15   thorpej 	void *v;
    155  1.15   thorpej 	register_t *retval;
    156  1.15   thorpej {
    157  1.38  augustss 	struct sys_nfssvc_args /* {
    158  1.13       cgd 		syscallarg(int) flag;
    159  1.13       cgd 		syscallarg(caddr_t) argp;
    160  1.15   thorpej 	} */ *uap = v;
    161  1.53   thorpej 	struct proc *p = l->l_proc;
    162  1.23   thorpej 	int error;
    163  1.24   thorpej #ifdef NFS
    164  1.10   mycroft 	struct nameidata nd;
    165  1.18  christos 	struct nfsmount *nmp;
    166  1.19      fvdl 	struct nfsd_cargs ncd;
    167  1.19      fvdl #endif
    168  1.18  christos #ifdef NFSSERVER
    169  1.67      yamt 	int s;
    170   1.1       cgd 	struct file *fp;
    171  1.10   mycroft 	struct mbuf *nam;
    172  1.10   mycroft 	struct nfsd_args nfsdarg;
    173  1.10   mycroft 	struct nfsd_srvargs nfsd_srvargs, *nsd = &nfsd_srvargs;
    174  1.10   mycroft 	struct nfsd *nfsd;
    175  1.10   mycroft 	struct nfssvc_sock *slp;
    176  1.18  christos 	struct nfsuid *nuidp;
    177  1.18  christos #endif
    178   1.1       cgd 
    179   1.1       cgd 	/*
    180   1.1       cgd 	 * Must be super user
    181   1.1       cgd 	 */
    182  1.19      fvdl 	error = suser(p->p_ucred, &p->p_acflag);
    183  1.19      fvdl 	if(error)
    184   1.1       cgd 		return (error);
    185  1.67      yamt #ifdef NFSSERVER
    186  1.66      yamt 	s = splsoftnet();
    187  1.66      yamt 	simple_lock(&nfsd_slock);
    188  1.12   mycroft 	while (nfssvc_sockhead_flag & SLP_INIT) {
    189  1.19      fvdl 		 nfssvc_sockhead_flag |= SLP_WANTINIT;
    190  1.66      yamt 		(void) ltsleep(&nfssvc_sockhead, PSOCK, "nfsd init", 0,
    191  1.66      yamt 		    &nfsd_slock);
    192  1.10   mycroft 	}
    193  1.66      yamt 	simple_unlock(&nfsd_slock);
    194  1.66      yamt 	splx(s);
    195  1.67      yamt #endif
    196  1.13       cgd 	if (SCARG(uap, flag) & NFSSVC_BIOD) {
    197  1.39    tsarna #if defined(NFS) && defined(COMPAT_14)
    198  1.53   thorpej 		error = nfssvc_iod(l);
    199  1.19      fvdl #else
    200  1.10   mycroft 		error = ENOSYS;
    201  1.19      fvdl #endif
    202  1.13       cgd 	} else if (SCARG(uap, flag) & NFSSVC_MNTD) {
    203  1.24   thorpej #ifndef NFS
    204  1.10   mycroft 		error = ENOSYS;
    205  1.19      fvdl #else
    206  1.18  christos 		error = copyin(SCARG(uap, argp), (caddr_t)&ncd, sizeof (ncd));
    207  1.18  christos 		if (error)
    208  1.10   mycroft 			return (error);
    209  1.10   mycroft 		NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
    210  1.69      fvdl 			ncd.ncd_dirp, p);
    211  1.19      fvdl 		error = namei(&nd);
    212  1.19      fvdl 		if (error)
    213  1.10   mycroft 			return (error);
    214  1.10   mycroft 		if ((nd.ni_vp->v_flag & VROOT) == 0)
    215  1.10   mycroft 			error = EINVAL;
    216  1.10   mycroft 		nmp = VFSTONFS(nd.ni_vp->v_mount);
    217  1.10   mycroft 		vput(nd.ni_vp);
    218  1.10   mycroft 		if (error)
    219  1.10   mycroft 			return (error);
    220  1.27      fvdl 		if ((nmp->nm_iflag & NFSMNT_MNTD) &&
    221  1.19      fvdl 			(SCARG(uap, flag) & NFSSVC_GOTAUTH) == 0)
    222  1.10   mycroft 			return (0);
    223  1.27      fvdl 		nmp->nm_iflag |= NFSMNT_MNTD;
    224  1.13       cgd 		error = nqnfs_clientd(nmp, p->p_ucred, &ncd, SCARG(uap, flag),
    225  1.53   thorpej 			SCARG(uap, argp), l);
    226  1.24   thorpej #endif /* NFS */
    227  1.13       cgd 	} else if (SCARG(uap, flag) & NFSSVC_ADDSOCK) {
    228  1.10   mycroft #ifndef NFSSERVER
    229  1.10   mycroft 		error = ENOSYS;
    230  1.19      fvdl #else
    231  1.18  christos 		error = copyin(SCARG(uap, argp), (caddr_t)&nfsdarg,
    232  1.19      fvdl 		    sizeof(nfsdarg));
    233  1.18  christos 		if (error)
    234  1.10   mycroft 			return (error);
    235  1.35   thorpej 		/* getsock() will use the descriptor for us */
    236  1.19      fvdl 		error = getsock(p->p_fd, nfsdarg.sock, &fp);
    237  1.19      fvdl 		if (error)
    238  1.10   mycroft 			return (error);
    239  1.10   mycroft 		/*
    240  1.10   mycroft 		 * Get the client address for connected sockets.
    241  1.10   mycroft 		 */
    242  1.10   mycroft 		if (nfsdarg.name == NULL || nfsdarg.namelen == 0)
    243  1.10   mycroft 			nam = (struct mbuf *)0;
    244  1.18  christos 		else {
    245  1.18  christos 			error = sockargs(&nam, nfsdarg.name, nfsdarg.namelen,
    246  1.19      fvdl 				MT_SONAME);
    247  1.35   thorpej 			if (error) {
    248  1.35   thorpej 				FILE_UNUSE(fp, NULL);
    249  1.18  christos 				return (error);
    250  1.35   thorpej 			}
    251  1.18  christos 		}
    252  1.10   mycroft 		error = nfssvc_addsock(fp, nam);
    253  1.35   thorpej 		FILE_UNUSE(fp, NULL);
    254  1.10   mycroft #endif /* !NFSSERVER */
    255  1.10   mycroft 	} else {
    256  1.10   mycroft #ifndef NFSSERVER
    257  1.10   mycroft 		error = ENOSYS;
    258  1.19      fvdl #else
    259  1.18  christos 		error = copyin(SCARG(uap, argp), (caddr_t)nsd, sizeof (*nsd));
    260  1.18  christos 		if (error)
    261  1.10   mycroft 			return (error);
    262  1.13       cgd 		if ((SCARG(uap, flag) & NFSSVC_AUTHIN) &&
    263  1.19      fvdl 		    ((nfsd = nsd->nsd_nfsd)) != NULL &&
    264  1.19      fvdl 		    (nfsd->nfsd_slp->ns_flag & SLP_VALID)) {
    265  1.19      fvdl 			slp = nfsd->nfsd_slp;
    266  1.10   mycroft 
    267  1.10   mycroft 			/*
    268  1.10   mycroft 			 * First check to see if another nfsd has already
    269  1.10   mycroft 			 * added this credential.
    270  1.10   mycroft 			 */
    271  1.57      yamt 			LIST_FOREACH(nuidp, NUIDHASH(slp,nsd->nsd_cr.cr_uid),
    272  1.57      yamt 			    nu_hash) {
    273  1.19      fvdl 				if (nuidp->nu_cr.cr_uid == nsd->nsd_cr.cr_uid &&
    274  1.19      fvdl 				    (!nfsd->nfsd_nd->nd_nam2 ||
    275  1.19      fvdl 				     netaddr_match(NU_NETFAM(nuidp),
    276  1.19      fvdl 				     &nuidp->nu_haddr, nfsd->nfsd_nd->nd_nam2)))
    277  1.10   mycroft 					break;
    278  1.10   mycroft 			}
    279  1.19      fvdl 			if (nuidp) {
    280  1.19      fvdl 			    nfsrv_setcred(&nuidp->nu_cr,&nfsd->nfsd_nd->nd_cr);
    281  1.19      fvdl 			    nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
    282  1.19      fvdl 			} else {
    283  1.10   mycroft 			    /*
    284  1.10   mycroft 			     * Nope, so we will.
    285  1.10   mycroft 			     */
    286  1.10   mycroft 			    if (slp->ns_numuids < nuidhash_max) {
    287  1.10   mycroft 				slp->ns_numuids++;
    288  1.10   mycroft 				nuidp = (struct nfsuid *)
    289  1.10   mycroft 				   malloc(sizeof (struct nfsuid), M_NFSUID,
    290  1.10   mycroft 					M_WAITOK);
    291  1.10   mycroft 			    } else
    292  1.10   mycroft 				nuidp = (struct nfsuid *)0;
    293  1.10   mycroft 			    if ((slp->ns_flag & SLP_VALID) == 0) {
    294  1.10   mycroft 				if (nuidp)
    295  1.10   mycroft 				    free((caddr_t)nuidp, M_NFSUID);
    296  1.10   mycroft 			    } else {
    297  1.10   mycroft 				if (nuidp == (struct nfsuid *)0) {
    298  1.57      yamt 				    nuidp = TAILQ_FIRST(&slp->ns_uidlruhead);
    299  1.12   mycroft 				    LIST_REMOVE(nuidp, nu_hash);
    300  1.12   mycroft 				    TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp,
    301  1.12   mycroft 					nu_lru);
    302  1.19      fvdl 				    if (nuidp->nu_flag & NU_NAM)
    303  1.19      fvdl 					m_freem(nuidp->nu_nam);
    304  1.10   mycroft 			        }
    305  1.19      fvdl 				nuidp->nu_flag = 0;
    306  1.50  christos 				crcvt(&nuidp->nu_cr, &nsd->nsd_cr);
    307  1.10   mycroft 				if (nuidp->nu_cr.cr_ngroups > NGROUPS)
    308  1.19      fvdl 				    nuidp->nu_cr.cr_ngroups = NGROUPS;
    309  1.10   mycroft 				nuidp->nu_cr.cr_ref = 1;
    310  1.19      fvdl 				nuidp->nu_timestamp = nsd->nsd_timestamp;
    311  1.19      fvdl 				nuidp->nu_expire = time.tv_sec + nsd->nsd_ttl;
    312  1.19      fvdl 				/*
    313  1.19      fvdl 				 * and save the session key in nu_key.
    314  1.19      fvdl 				 */
    315  1.32     perry 				memcpy(nuidp->nu_key, nsd->nsd_key,
    316  1.32     perry 				    sizeof(nsd->nsd_key));
    317  1.19      fvdl 				if (nfsd->nfsd_nd->nd_nam2) {
    318  1.19      fvdl 				    struct sockaddr_in *saddr;
    319  1.19      fvdl 
    320  1.19      fvdl 				    saddr = mtod(nfsd->nfsd_nd->nd_nam2,
    321  1.19      fvdl 					 struct sockaddr_in *);
    322  1.19      fvdl 				    switch (saddr->sin_family) {
    323  1.19      fvdl 				    case AF_INET:
    324  1.19      fvdl 					nuidp->nu_flag |= NU_INETADDR;
    325  1.19      fvdl 					nuidp->nu_inetaddr =
    326  1.19      fvdl 					     saddr->sin_addr.s_addr;
    327  1.19      fvdl 					break;
    328  1.19      fvdl 				    case AF_ISO:
    329  1.19      fvdl 				    default:
    330  1.19      fvdl 					nuidp->nu_flag |= NU_NAM;
    331  1.19      fvdl 					nuidp->nu_nam = m_copym(
    332  1.19      fvdl 					    nfsd->nfsd_nd->nd_nam2, 0,
    333  1.19      fvdl 					     M_COPYALL, M_WAIT);
    334  1.19      fvdl 					break;
    335  1.19      fvdl 				    };
    336  1.19      fvdl 				}
    337  1.12   mycroft 				TAILQ_INSERT_TAIL(&slp->ns_uidlruhead, nuidp,
    338  1.19      fvdl 					nu_lru);
    339  1.12   mycroft 				LIST_INSERT_HEAD(NUIDHASH(slp, nsd->nsd_uid),
    340  1.19      fvdl 					nuidp, nu_hash);
    341  1.19      fvdl 				nfsrv_setcred(&nuidp->nu_cr,
    342  1.19      fvdl 				    &nfsd->nfsd_nd->nd_cr);
    343  1.19      fvdl 				nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
    344  1.10   mycroft 			    }
    345  1.10   mycroft 			}
    346  1.10   mycroft 		}
    347  1.13       cgd 		if ((SCARG(uap, flag) & NFSSVC_AUTHINFAIL) &&
    348  1.13       cgd 		    (nfsd = nsd->nsd_nfsd))
    349  1.19      fvdl 			nfsd->nfsd_flag |= NFSD_AUTHFAIL;
    350  1.53   thorpej 		error = nfssvc_nfsd(nsd, SCARG(uap, argp), l);
    351  1.10   mycroft #endif /* !NFSSERVER */
    352  1.10   mycroft 	}
    353  1.10   mycroft 	if (error == EINTR || error == ERESTART)
    354  1.10   mycroft 		error = 0;
    355  1.10   mycroft 	return (error);
    356  1.10   mycroft }
    357  1.10   mycroft 
    358  1.10   mycroft #ifdef NFSSERVER
    359  1.54   thorpej MALLOC_DEFINE(M_NFSD, "NFS daemon", "Nfs server daemon structure");
    360  1.54   thorpej MALLOC_DEFINE(M_NFSSVC, "NFS srvsock", "Nfs server structure");
    361  1.64      yamt struct pool nfs_srvdesc_pool;
    362  1.54   thorpej 
    363  1.65      yamt static struct nfssvc_sock *
    364  1.65      yamt nfsrv_sockalloc()
    365  1.65      yamt {
    366  1.65      yamt 	struct nfssvc_sock *slp;
    367  1.66      yamt 	int s;
    368  1.65      yamt 
    369  1.65      yamt 	slp = (struct nfssvc_sock *)
    370  1.65      yamt 	    malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
    371  1.65      yamt 	memset(slp, 0, sizeof (struct nfssvc_sock));
    372  1.65      yamt 	TAILQ_INIT(&slp->ns_uidlruhead);
    373  1.66      yamt 	s = splsoftnet();
    374  1.66      yamt 	simple_lock(&nfsd_slock);
    375  1.65      yamt 	TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
    376  1.66      yamt 	simple_unlock(&nfsd_slock);
    377  1.66      yamt 	splx(s);
    378  1.65      yamt 
    379  1.65      yamt 	return slp;
    380  1.65      yamt }
    381  1.65      yamt 
    382  1.10   mycroft /*
    383  1.10   mycroft  * Adds a socket to the list for servicing by nfsds.
    384  1.10   mycroft  */
    385  1.18  christos int
    386  1.10   mycroft nfssvc_addsock(fp, mynam)
    387  1.10   mycroft 	struct file *fp;
    388  1.10   mycroft 	struct mbuf *mynam;
    389  1.10   mycroft {
    390  1.38  augustss 	struct mbuf *m;
    391  1.38  augustss 	int siz;
    392  1.38  augustss 	struct nfssvc_sock *slp;
    393  1.38  augustss 	struct socket *so;
    394  1.10   mycroft 	struct nfssvc_sock *tslp;
    395  1.10   mycroft 	int error, s;
    396  1.10   mycroft 
    397   1.1       cgd 	so = (struct socket *)fp->f_data;
    398  1.10   mycroft 	tslp = (struct nfssvc_sock *)0;
    399  1.10   mycroft 	/*
    400  1.10   mycroft 	 * Add it to the list, as required.
    401  1.10   mycroft 	 */
    402  1.10   mycroft 	if (so->so_proto->pr_protocol == IPPROTO_UDP) {
    403  1.41      fvdl #ifdef INET6
    404  1.41      fvdl 		if (so->so_proto->pr_domain->dom_family == AF_INET6)
    405  1.41      fvdl 			tslp = nfs_udp6sock;
    406  1.41      fvdl 		else
    407  1.41      fvdl #endif
    408  1.10   mycroft 		tslp = nfs_udpsock;
    409  1.10   mycroft 		if (tslp->ns_flag & SLP_VALID) {
    410  1.10   mycroft 			m_freem(mynam);
    411  1.10   mycroft 			return (EPERM);
    412  1.10   mycroft 		}
    413  1.10   mycroft #ifdef ISO
    414  1.10   mycroft 	} else if (so->so_proto->pr_protocol == ISOPROTO_CLTP) {
    415  1.10   mycroft 		tslp = nfs_cltpsock;
    416  1.10   mycroft 		if (tslp->ns_flag & SLP_VALID) {
    417  1.10   mycroft 			m_freem(mynam);
    418  1.10   mycroft 			return (EPERM);
    419  1.10   mycroft 		}
    420  1.10   mycroft #endif /* ISO */
    421  1.10   mycroft 	}
    422  1.10   mycroft 	if (so->so_type == SOCK_STREAM)
    423  1.19      fvdl 		siz = NFS_MAXPACKET + sizeof (u_long);
    424  1.10   mycroft 	else
    425   1.1       cgd 		siz = NFS_MAXPACKET;
    426  1.19      fvdl 	error = soreserve(so, siz, siz);
    427  1.19      fvdl 	if (error) {
    428  1.10   mycroft 		m_freem(mynam);
    429  1.10   mycroft 		return (error);
    430  1.10   mycroft 	}
    431   1.1       cgd 
    432   1.1       cgd 	/*
    433   1.1       cgd 	 * Set protocol specific options { for now TCP only } and
    434   1.1       cgd 	 * reserve some space. For datagram sockets, this can get called
    435   1.1       cgd 	 * repeatedly for the same socket, but that isn't harmful.
    436   1.1       cgd 	 */
    437  1.10   mycroft 	if (so->so_type == SOCK_STREAM) {
    438  1.55      matt 		m = m_get(M_WAIT, MT_SOOPTS);
    439  1.55      matt 		MCLAIM(m, &nfs_mowner);
    440  1.19      fvdl 		*mtod(m, int32_t *) = 1;
    441  1.19      fvdl 		m->m_len = sizeof(int32_t);
    442   1.1       cgd 		sosetopt(so, SOL_SOCKET, SO_KEEPALIVE, m);
    443   1.1       cgd 	}
    444  1.41      fvdl 	if ((so->so_proto->pr_domain->dom_family == AF_INET
    445  1.41      fvdl #ifdef INET6
    446  1.41      fvdl 	    || so->so_proto->pr_domain->dom_family == AF_INET6
    447  1.41      fvdl #endif
    448  1.41      fvdl 	    ) &&
    449  1.10   mycroft 	    so->so_proto->pr_protocol == IPPROTO_TCP) {
    450  1.55      matt 		m = m_get(M_WAIT, MT_SOOPTS);
    451  1.55      matt 		MCLAIM(m, &nfs_mowner);
    452  1.19      fvdl 		*mtod(m, int32_t *) = 1;
    453  1.19      fvdl 		m->m_len = sizeof(int32_t);
    454   1.1       cgd 		sosetopt(so, IPPROTO_TCP, TCP_NODELAY, m);
    455   1.1       cgd 	}
    456   1.1       cgd 	so->so_rcv.sb_flags &= ~SB_NOINTR;
    457   1.1       cgd 	so->so_rcv.sb_timeo = 0;
    458   1.1       cgd 	so->so_snd.sb_flags &= ~SB_NOINTR;
    459   1.1       cgd 	so->so_snd.sb_timeo = 0;
    460  1.10   mycroft 	if (tslp)
    461  1.10   mycroft 		slp = tslp;
    462  1.10   mycroft 	else {
    463  1.65      yamt 		slp = nfsrv_sockalloc();
    464  1.10   mycroft 	}
    465  1.10   mycroft 	slp->ns_so = so;
    466  1.10   mycroft 	slp->ns_nam = mynam;
    467  1.10   mycroft 	fp->f_count++;
    468  1.10   mycroft 	slp->ns_fp = fp;
    469  1.14   mycroft 	s = splsoftnet();
    470  1.10   mycroft 	so->so_upcallarg = (caddr_t)slp;
    471  1.10   mycroft 	so->so_upcall = nfsrv_rcv;
    472  1.29      matt 	so->so_rcv.sb_flags |= SB_UPCALL;
    473  1.10   mycroft 	slp->ns_flag = (SLP_VALID | SLP_NEEDQ);
    474  1.10   mycroft 	nfsrv_wakenfsd(slp);
    475  1.10   mycroft 	splx(s);
    476  1.10   mycroft 	return (0);
    477  1.10   mycroft }
    478   1.1       cgd 
    479  1.10   mycroft /*
    480  1.10   mycroft  * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
    481  1.10   mycroft  * until it is killed by a signal.
    482  1.10   mycroft  */
    483  1.18  christos int
    484  1.53   thorpej nfssvc_nfsd(nsd, argp, l)
    485  1.10   mycroft 	struct nfsd_srvargs *nsd;
    486  1.10   mycroft 	caddr_t argp;
    487  1.53   thorpej 	struct lwp *l;
    488  1.10   mycroft {
    489  1.38  augustss 	struct mbuf *m;
    490  1.38  augustss 	int siz;
    491  1.38  augustss 	struct nfssvc_sock *slp;
    492  1.38  augustss 	struct socket *so;
    493  1.38  augustss 	int *solockp;
    494  1.19      fvdl 	struct nfsd *nfsd = nsd->nsd_nfsd;
    495  1.19      fvdl 	struct nfsrv_descript *nd = NULL;
    496  1.19      fvdl 	struct mbuf *mreq;
    497  1.19      fvdl 	int error = 0, cacherep, s, sotype, writes_todo;
    498  1.19      fvdl 	u_quad_t cur_usec;
    499  1.53   thorpej 	struct proc *p = l->l_proc;
    500  1.19      fvdl 
    501  1.19      fvdl #ifndef nolint
    502  1.19      fvdl 	cacherep = RC_DOIT;
    503  1.19      fvdl 	writes_todo = 0;
    504  1.19      fvdl #endif
    505  1.14   mycroft 	s = splsoftnet();
    506  1.19      fvdl 	if (nfsd == (struct nfsd *)0) {
    507  1.19      fvdl 		nsd->nsd_nfsd = nfsd = (struct nfsd *)
    508  1.10   mycroft 			malloc(sizeof (struct nfsd), M_NFSD, M_WAITOK);
    509  1.32     perry 		memset((caddr_t)nfsd, 0, sizeof (struct nfsd));
    510  1.19      fvdl 		nfsd->nfsd_procp = p;
    511  1.66      yamt 		simple_lock(&nfsd_slock);
    512  1.19      fvdl 		TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain);
    513  1.10   mycroft 		nfs_numnfsd++;
    514  1.66      yamt 		simple_unlock(&nfsd_slock);
    515  1.10   mycroft 	}
    516  1.53   thorpej 	PHOLD(l);
    517   1.1       cgd 	/*
    518  1.10   mycroft 	 * Loop getting rpc requests until SIGKILL.
    519   1.1       cgd 	 */
    520   1.1       cgd 	for (;;) {
    521  1.19      fvdl 		if ((nfsd->nfsd_flag & NFSD_REQINPROG) == 0) {
    522  1.66      yamt 			simple_lock(&nfsd_slock);
    523  1.19      fvdl 			while (nfsd->nfsd_slp == (struct nfssvc_sock *)0 &&
    524  1.12   mycroft 			    (nfsd_head_flag & NFSD_CHECKSLP) == 0) {
    525  1.66      yamt 				SLIST_INSERT_HEAD(&nfsd_idle_head, nfsd,
    526  1.66      yamt 				    nfsd_idle);
    527  1.19      fvdl 				nfsd->nfsd_flag |= NFSD_WAITING;
    528  1.10   mycroft 				nfsd_waiting++;
    529  1.66      yamt 				error = ltsleep(nfsd, PSOCK | PCATCH, "nfsd",
    530  1.66      yamt 				    0, &nfsd_slock);
    531  1.10   mycroft 				nfsd_waiting--;
    532  1.66      yamt 				if (error) {
    533  1.66      yamt 					slp = nfsd->nfsd_slp;
    534  1.66      yamt 					nfsd->nfsd_slp = NULL;
    535  1.66      yamt 					if (!slp)
    536  1.66      yamt 						SLIST_REMOVE(&nfsd_idle_head,
    537  1.66      yamt 						    nfsd, nfsd, nfsd_idle);
    538  1.66      yamt 					simple_unlock(&nfsd_slock);
    539  1.66      yamt 					if (slp) {
    540  1.66      yamt 						nfsrv_wakenfsd(slp);
    541  1.66      yamt 						nfsrv_slpderef(slp);
    542  1.66      yamt 					}
    543  1.10   mycroft 					goto done;
    544  1.66      yamt 				}
    545  1.10   mycroft 			}
    546  1.19      fvdl 			if (nfsd->nfsd_slp == (struct nfssvc_sock *)0 &&
    547  1.12   mycroft 			    (nfsd_head_flag & NFSD_CHECKSLP) != 0) {
    548  1.66      yamt 				slp = TAILQ_FIRST(&nfssvc_sockpending);
    549  1.66      yamt 				if (slp) {
    550  1.66      yamt 					KASSERT((slp->ns_flag &
    551  1.63      yamt 					    (SLP_VALID | SLP_DOREC))
    552  1.66      yamt 					    == (SLP_VALID | SLP_DOREC));
    553  1.66      yamt 					TAILQ_REMOVE(&nfssvc_sockpending, slp,
    554  1.66      yamt 					    ns_pending);
    555  1.66      yamt 					slp->ns_flag &= ~SLP_DOREC;
    556  1.66      yamt 					slp->ns_sref++;
    557  1.66      yamt 					nfsd->nfsd_slp = slp;
    558  1.66      yamt 				} else
    559  1.12   mycroft 					nfsd_head_flag &= ~NFSD_CHECKSLP;
    560  1.10   mycroft 			}
    561  1.66      yamt 			simple_unlock(&nfsd_slock);
    562  1.19      fvdl 			if ((slp = nfsd->nfsd_slp) == (struct nfssvc_sock *)0)
    563  1.10   mycroft 				continue;
    564  1.10   mycroft 			if (slp->ns_flag & SLP_VALID) {
    565  1.10   mycroft 				if (slp->ns_flag & SLP_DISCONN)
    566  1.10   mycroft 					nfsrv_zapsock(slp);
    567  1.10   mycroft 				else if (slp->ns_flag & SLP_NEEDQ) {
    568  1.10   mycroft 					slp->ns_flag &= ~SLP_NEEDQ;
    569  1.10   mycroft 					(void) nfs_sndlock(&slp->ns_solock,
    570  1.10   mycroft 						(struct nfsreq *)0);
    571  1.10   mycroft 					nfsrv_rcv(slp->ns_so, (caddr_t)slp,
    572  1.10   mycroft 						M_WAIT);
    573  1.10   mycroft 					nfs_sndunlock(&slp->ns_solock);
    574  1.10   mycroft 				}
    575  1.19      fvdl 				error = nfsrv_dorec(slp, nfsd, &nd);
    576  1.19      fvdl 				cur_usec = (u_quad_t)time.tv_sec * 1000000 +
    577  1.19      fvdl 					(u_quad_t)time.tv_usec;
    578  1.57      yamt 				if (error && LIST_FIRST(&slp->ns_tq) &&
    579  1.57      yamt 				    LIST_FIRST(&slp->ns_tq)->nd_time <=
    580  1.57      yamt 				    cur_usec) {
    581  1.19      fvdl 					error = 0;
    582  1.19      fvdl 					cacherep = RC_DOIT;
    583  1.19      fvdl 					writes_todo = 1;
    584  1.19      fvdl 				} else
    585  1.19      fvdl 					writes_todo = 0;
    586  1.19      fvdl 				nfsd->nfsd_flag |= NFSD_REQINPROG;
    587   1.1       cgd 			}
    588  1.10   mycroft 		} else {
    589  1.10   mycroft 			error = 0;
    590  1.19      fvdl 			slp = nfsd->nfsd_slp;
    591  1.10   mycroft 		}
    592  1.10   mycroft 		if (error || (slp->ns_flag & SLP_VALID) == 0) {
    593  1.19      fvdl 			if (nd) {
    594  1.64      yamt 				pool_put(&nfs_srvdesc_pool, nd);
    595  1.19      fvdl 				nd = NULL;
    596  1.19      fvdl 			}
    597  1.19      fvdl 			nfsd->nfsd_slp = (struct nfssvc_sock *)0;
    598  1.19      fvdl 			nfsd->nfsd_flag &= ~NFSD_REQINPROG;
    599  1.10   mycroft 			nfsrv_slpderef(slp);
    600   1.1       cgd 			continue;
    601   1.1       cgd 		}
    602  1.10   mycroft 		splx(s);
    603  1.10   mycroft 		so = slp->ns_so;
    604  1.10   mycroft 		sotype = so->so_type;
    605  1.10   mycroft 		if (so->so_proto->pr_flags & PR_CONNREQUIRED)
    606  1.10   mycroft 			solockp = &slp->ns_solock;
    607  1.10   mycroft 		else
    608  1.10   mycroft 			solockp = (int *)0;
    609  1.19      fvdl 		if (nd) {
    610  1.63      yamt 			nd->nd_starttime = time;
    611  1.63      yamt 			if (nd->nd_nam2)
    612  1.63      yamt 				nd->nd_nam = nd->nd_nam2;
    613  1.63      yamt 			else
    614  1.63      yamt 				nd->nd_nam = slp->ns_nam;
    615  1.63      yamt 
    616  1.63      yamt 			/*
    617  1.63      yamt 			 * Check to see if authorization is needed.
    618  1.63      yamt 			 */
    619  1.63      yamt 			if (nfsd->nfsd_flag & NFSD_NEEDAUTH) {
    620  1.63      yamt 				nfsd->nfsd_flag &= ~NFSD_NEEDAUTH;
    621  1.63      yamt 				nsd->nsd_haddr = mtod(nd->nd_nam,
    622  1.63      yamt 				    struct sockaddr_in *)->sin_addr.s_addr;
    623  1.63      yamt 				nsd->nsd_authlen = nfsd->nfsd_authlen;
    624  1.63      yamt 				nsd->nsd_verflen = nfsd->nfsd_verflen;
    625  1.63      yamt 				if (!copyout(nfsd->nfsd_authstr,
    626  1.63      yamt 				    nsd->nsd_authstr, nfsd->nfsd_authlen) &&
    627  1.63      yamt 				    !copyout(nfsd->nfsd_verfstr,
    628  1.63      yamt 				    nsd->nsd_verfstr, nfsd->nfsd_verflen) &&
    629  1.63      yamt 				    !copyout(nsd, argp, sizeof (*nsd))) {
    630  1.63      yamt 					PRELE(l);
    631  1.63      yamt 					return (ENEEDAUTH);
    632  1.63      yamt 				}
    633  1.63      yamt 				cacherep = RC_DROPIT;
    634  1.10   mycroft 			} else
    635  1.63      yamt 				cacherep = nfsrv_getcache(nd, slp, &mreq);
    636  1.63      yamt 
    637  1.63      yamt 			/*
    638  1.63      yamt 			 * Check for just starting up for NQNFS and send
    639  1.63      yamt 			 * fake "try again later" replies to the NQNFS clients.
    640  1.63      yamt 			 */
    641  1.63      yamt 			if (notstarted && nqnfsstarttime <= time.tv_sec) {
    642  1.63      yamt 				if (modify_flag) {
    643  1.63      yamt 					nqnfsstarttime =
    644  1.63      yamt 					    time.tv_sec + nqsrv_writeslack;
    645  1.63      yamt 					modify_flag = 0;
    646  1.63      yamt 				} else
    647  1.63      yamt 					notstarted = 0;
    648  1.63      yamt 			}
    649  1.63      yamt 			if (notstarted) {
    650  1.63      yamt 				if ((nd->nd_flag & ND_NQNFS) == 0)
    651  1.63      yamt 					cacherep = RC_DROPIT;
    652  1.63      yamt 				else if (nd->nd_procnum != NFSPROC_WRITE) {
    653  1.63      yamt 					nd->nd_procnum = NFSPROC_NOOP;
    654  1.63      yamt 					nd->nd_repstat = NQNFS_TRYLATER;
    655  1.63      yamt 					cacherep = RC_DOIT;
    656  1.63      yamt 				} else
    657  1.63      yamt 					modify_flag = 1;
    658  1.63      yamt 			} else if (nfsd->nfsd_flag & NFSD_AUTHFAIL) {
    659  1.63      yamt 				nfsd->nfsd_flag &= ~NFSD_AUTHFAIL;
    660  1.10   mycroft 				nd->nd_procnum = NFSPROC_NOOP;
    661  1.63      yamt 				nd->nd_repstat =
    662  1.63      yamt 				    (NFSERR_AUTHERR | AUTH_TOOWEAK);
    663  1.10   mycroft 				cacherep = RC_DOIT;
    664  1.63      yamt 			}
    665  1.10   mycroft 		}
    666  1.10   mycroft 
    667  1.19      fvdl 		/*
    668  1.19      fvdl 		 * Loop to get all the write rpc relies that have been
    669  1.19      fvdl 		 * gathered together.
    670  1.19      fvdl 		 */
    671  1.19      fvdl 		do {
    672  1.34  sommerfe #ifdef DIAGNOSTIC
    673  1.63      yamt 			int lockcount;
    674  1.34  sommerfe #endif
    675  1.63      yamt 			switch (cacherep) {
    676  1.63      yamt 			case RC_DOIT:
    677  1.34  sommerfe #ifdef DIAGNOSTIC
    678  1.63      yamt 				/*
    679  1.63      yamt 				 * NFS server procs should neither release
    680  1.63      yamt 				 * locks already held, nor leave things
    681  1.63      yamt 				 * locked.  Catch this sooner, rather than
    682  1.63      yamt 				 * later (when we try to relock something we
    683  1.63      yamt 				 * already have locked).  Careful inspection
    684  1.63      yamt 				 * of the failing routine usually turns up the
    685  1.63      yamt 				 * lock leak.. once we know what it is..
    686  1.63      yamt 				 */
    687  1.63      yamt 				lockcount = l->l_locks;
    688  1.34  sommerfe #endif
    689  1.63      yamt 				if (writes_todo || (!(nd->nd_flag & ND_NFSV3) &&
    690  1.63      yamt 				     nd->nd_procnum == NFSPROC_WRITE &&
    691  1.63      yamt 				     nfsrvw_procrastinate > 0 && !notstarted))
    692  1.63      yamt 					error = nfsrv_writegather(&nd, slp,
    693  1.69      fvdl 					    nfsd->nfsd_procp, &mreq);
    694  1.63      yamt 				else
    695  1.63      yamt 					error =
    696  1.63      yamt 					    (*(nfsrv3_procs[nd->nd_procnum]))
    697  1.69      fvdl 					    (nd, slp, nfsd->nfsd_procp, &mreq);
    698  1.34  sommerfe #ifdef DIAGNOSTIC
    699  1.63      yamt 				if (l->l_locks != lockcount) {
    700  1.63      yamt 					/*
    701  1.63      yamt 					 * If you see this panic, audit
    702  1.63      yamt 					 * nfsrv3_procs[nd->nd_procnum] for
    703  1.63      yamt 					 * vnode locking errors (usually, it's
    704  1.63      yamt 					 * due to forgetting to vput()
    705  1.63      yamt 					 * something).
    706  1.63      yamt 					 */
    707  1.46      matt #ifdef DEBUG
    708  1.63      yamt 					extern void printlockedvnodes(void);
    709  1.63      yamt 					printlockedvnodes();
    710  1.46      matt #endif
    711  1.63      yamt 					printf("nfsd: locking botch in op %d"
    712  1.63      yamt 					    " (before %d, after %d)\n",
    713  1.63      yamt 					    nd ? nd->nd_procnum : -1,
    714  1.63      yamt 					    lockcount, l->l_locks);
    715  1.63      yamt 				}
    716  1.34  sommerfe #endif
    717  1.63      yamt 				if (mreq == NULL)
    718  1.63      yamt 					break;
    719  1.63      yamt 				if (error) {
    720  1.63      yamt 					if (nd->nd_procnum != NQNFSPROC_VACATED)
    721  1.63      yamt 						nfsstats.srv_errs++;
    722  1.63      yamt 					nfsrv_updatecache(nd, FALSE, mreq);
    723  1.63      yamt 					if (nd->nd_nam2)
    724  1.63      yamt 						m_freem(nd->nd_nam2);
    725  1.63      yamt 					break;
    726  1.63      yamt 				}
    727  1.63      yamt 				nfsstats.srvrpccnt[nd->nd_procnum]++;
    728  1.63      yamt 				nfsrv_updatecache(nd, TRUE, mreq);
    729  1.63      yamt 				nd->nd_mrep = (struct mbuf *)0;
    730  1.63      yamt 			case RC_REPLY:
    731  1.63      yamt 				m = mreq;
    732  1.63      yamt 				siz = 0;
    733  1.63      yamt 				while (m) {
    734  1.63      yamt 					siz += m->m_len;
    735  1.63      yamt 					m = m->m_next;
    736  1.63      yamt 				}
    737  1.63      yamt 				if (siz <= 0 || siz > NFS_MAXPACKET) {
    738  1.63      yamt 					printf("mbuf siz=%d\n",siz);
    739  1.63      yamt 					panic("Bad nfs svc reply");
    740  1.63      yamt 				}
    741  1.63      yamt 				m = mreq;
    742  1.63      yamt 				m->m_pkthdr.len = siz;
    743  1.63      yamt 				m->m_pkthdr.rcvif = (struct ifnet *)0;
    744  1.63      yamt 				/*
    745  1.63      yamt 				 * For stream protocols, prepend a Sun RPC
    746  1.63      yamt 				 * Record Mark.
    747  1.63      yamt 				 */
    748  1.63      yamt 				if (sotype == SOCK_STREAM) {
    749  1.63      yamt 					M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
    750  1.63      yamt 					*mtod(m, u_int32_t *) =
    751  1.63      yamt 					    htonl(0x80000000 | siz);
    752  1.63      yamt 				}
    753  1.63      yamt 				if (solockp)
    754  1.63      yamt 					(void) nfs_sndlock(solockp, NULL);
    755  1.63      yamt 				if (slp->ns_flag & SLP_VALID) {
    756  1.63      yamt 					error =
    757  1.63      yamt 					    nfs_send(so, nd->nd_nam2, m, NULL);
    758  1.63      yamt 				} else {
    759  1.63      yamt 					error = EPIPE;
    760  1.63      yamt 					m_freem(m);
    761  1.63      yamt 				}
    762  1.63      yamt 				if (nfsrtton)
    763  1.63      yamt 					nfsd_rt(sotype, nd, cacherep);
    764  1.63      yamt 				if (nd->nd_nam2)
    765  1.63      yamt 					m_free(nd->nd_nam2);
    766  1.63      yamt 				if (nd->nd_mrep)
    767  1.63      yamt 					m_freem(nd->nd_mrep);
    768  1.63      yamt 				if (error == EPIPE)
    769  1.63      yamt 					nfsrv_zapsock(slp);
    770  1.63      yamt 				if (solockp)
    771  1.63      yamt 					nfs_sndunlock(solockp);
    772  1.63      yamt 				if (error == EINTR || error == ERESTART) {
    773  1.64      yamt 					pool_put(&nfs_srvdesc_pool, nd);
    774  1.63      yamt 					nfsrv_slpderef(slp);
    775  1.63      yamt 					s = splsoftnet();
    776  1.63      yamt 					goto done;
    777  1.63      yamt 				}
    778  1.19      fvdl 				break;
    779  1.63      yamt 			case RC_DROPIT:
    780  1.63      yamt 				if (nfsrtton)
    781  1.63      yamt 					nfsd_rt(sotype, nd, cacherep);
    782  1.63      yamt 				m_freem(nd->nd_mrep);
    783  1.63      yamt 				m_freem(nd->nd_nam2);
    784   1.1       cgd 				break;
    785   1.1       cgd 			}
    786  1.63      yamt 			if (nd) {
    787  1.64      yamt 				pool_put(&nfs_srvdesc_pool, nd);
    788  1.63      yamt 				nd = NULL;
    789  1.63      yamt 			}
    790  1.63      yamt 
    791   1.1       cgd 			/*
    792  1.63      yamt 			 * Check to see if there are outstanding writes that
    793  1.63      yamt 			 * need to be serviced.
    794   1.1       cgd 			 */
    795  1.63      yamt 			cur_usec = (u_quad_t)time.tv_sec * 1000000 +
    796  1.63      yamt 			    (u_quad_t)time.tv_usec;
    797  1.63      yamt 			s = splsoftclock();
    798  1.63      yamt 			if (LIST_FIRST(&slp->ns_tq) &&
    799  1.63      yamt 			    LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec) {
    800  1.63      yamt 				cacherep = RC_DOIT;
    801  1.63      yamt 				writes_todo = 1;
    802  1.63      yamt 			} else
    803  1.63      yamt 				writes_todo = 0;
    804  1.63      yamt 			splx(s);
    805  1.19      fvdl 		} while (writes_todo);
    806  1.14   mycroft 		s = splsoftnet();
    807  1.19      fvdl 		if (nfsrv_dorec(slp, nfsd, &nd)) {
    808  1.19      fvdl 			nfsd->nfsd_flag &= ~NFSD_REQINPROG;
    809  1.19      fvdl 			nfsd->nfsd_slp = NULL;
    810  1.10   mycroft 			nfsrv_slpderef(slp);
    811  1.10   mycroft 		}
    812   1.1       cgd 	}
    813  1.10   mycroft done:
    814  1.53   thorpej 	PRELE(l);
    815  1.66      yamt 	simple_lock(&nfsd_slock);
    816  1.19      fvdl 	TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
    817  1.66      yamt 	simple_unlock(&nfsd_slock);
    818  1.10   mycroft 	splx(s);
    819  1.19      fvdl 	free((caddr_t)nfsd, M_NFSD);
    820  1.10   mycroft 	nsd->nsd_nfsd = (struct nfsd *)0;
    821  1.10   mycroft 	if (--nfs_numnfsd == 0)
    822  1.10   mycroft 		nfsrv_init(TRUE);	/* Reinitialize everything */
    823   1.1       cgd 	return (error);
    824   1.1       cgd }
    825   1.1       cgd 
    826  1.10   mycroft /*
    827  1.10   mycroft  * Shut down a socket associated with an nfssvc_sock structure.
    828  1.10   mycroft  * Should be called with the send lock set, if required.
    829  1.10   mycroft  * The trick here is to increment the sref at the start, so that the nfsds
    830  1.10   mycroft  * will stop using it and clear ns_flag at the end so that it will not be
    831  1.10   mycroft  * reassigned during cleanup.
    832  1.10   mycroft  */
    833  1.18  christos void
    834  1.10   mycroft nfsrv_zapsock(slp)
    835  1.38  augustss 	struct nfssvc_sock *slp;
    836  1.10   mycroft {
    837  1.38  augustss 	struct nfsuid *nuidp, *nnuidp;
    838  1.38  augustss 	struct nfsrv_descript *nwp, *nnwp;
    839  1.10   mycroft 	struct socket *so;
    840  1.10   mycroft 	struct file *fp;
    841  1.19      fvdl 	int s;
    842  1.10   mycroft 
    843  1.10   mycroft 	slp->ns_flag &= ~SLP_ALLFLAGS;
    844  1.19      fvdl 	fp = slp->ns_fp;
    845  1.19      fvdl 	if (fp) {
    846  1.56      yamt 		simple_lock(&fp->f_slock);
    847  1.35   thorpej 		FILE_USE(fp);
    848  1.10   mycroft 		slp->ns_fp = (struct file *)0;
    849  1.10   mycroft 		so = slp->ns_so;
    850  1.10   mycroft 		so->so_upcall = NULL;
    851  1.29      matt 		so->so_upcallarg = NULL;
    852  1.29      matt 		so->so_rcv.sb_flags &= ~SB_UPCALL;
    853  1.10   mycroft 		soshutdown(so, 2);
    854  1.69      fvdl 		closef(fp, (struct proc *)0);
    855  1.10   mycroft 		if (slp->ns_nam)
    856  1.55      matt 			m_free(slp->ns_nam);
    857  1.10   mycroft 		m_freem(slp->ns_raw);
    858  1.10   mycroft 		m_freem(slp->ns_rec);
    859  1.57      yamt 		for (nuidp = TAILQ_FIRST(&slp->ns_uidlruhead); nuidp != 0;
    860  1.12   mycroft 		    nuidp = nnuidp) {
    861  1.57      yamt 			nnuidp = TAILQ_NEXT(nuidp, nu_lru);
    862  1.12   mycroft 			LIST_REMOVE(nuidp, nu_hash);
    863  1.12   mycroft 			TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru);
    864  1.19      fvdl 			if (nuidp->nu_flag & NU_NAM)
    865  1.19      fvdl 				m_freem(nuidp->nu_nam);
    866  1.12   mycroft 			free((caddr_t)nuidp, M_NFSUID);
    867  1.12   mycroft 		}
    868  1.19      fvdl 		s = splsoftclock();
    869  1.57      yamt 		for (nwp = LIST_FIRST(&slp->ns_tq); nwp; nwp = nnwp) {
    870  1.57      yamt 			nnwp = LIST_NEXT(nwp, nd_tq);
    871  1.19      fvdl 			LIST_REMOVE(nwp, nd_tq);
    872  1.64      yamt 			pool_put(&nfs_srvdesc_pool, nwp);
    873  1.19      fvdl 		}
    874  1.19      fvdl 		LIST_INIT(&slp->ns_tq);
    875  1.19      fvdl 		splx(s);
    876  1.10   mycroft 	}
    877  1.10   mycroft }
    878  1.10   mycroft 
    879  1.10   mycroft /*
    880  1.10   mycroft  * Derefence a server socket structure. If it has no more references and
    881  1.10   mycroft  * is no longer valid, you can throw it away.
    882  1.10   mycroft  */
    883  1.10   mycroft void
    884  1.10   mycroft nfsrv_slpderef(slp)
    885  1.38  augustss 	struct nfssvc_sock *slp;
    886  1.10   mycroft {
    887  1.66      yamt 	LOCK_ASSERT(!simple_lock_held(&nfsd_slock));
    888  1.66      yamt 
    889  1.10   mycroft 	if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) {
    890  1.66      yamt 		int s = splsoftnet();
    891  1.66      yamt 		simple_lock(&nfsd_slock);
    892  1.12   mycroft 		TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
    893  1.66      yamt 		simple_unlock(&nfsd_slock);
    894  1.66      yamt 		splx(s);
    895  1.66      yamt 		free(slp, M_NFSSVC);
    896  1.10   mycroft 	}
    897  1.10   mycroft }
    898  1.10   mycroft 
    899  1.10   mycroft /*
    900  1.10   mycroft  * Initialize the data structures for the server.
    901  1.10   mycroft  * Handshake with any new nfsds starting up to avoid any chance of
    902  1.10   mycroft  * corruption.
    903  1.10   mycroft  */
    904  1.10   mycroft void
    905  1.10   mycroft nfsrv_init(terminating)
    906  1.10   mycroft 	int terminating;
    907  1.10   mycroft {
    908  1.66      yamt 	struct nfssvc_sock *slp;
    909  1.66      yamt 	int s;
    910  1.10   mycroft 
    911  1.66      yamt 	s = splsoftnet();
    912  1.66      yamt 	simple_lock(&nfsd_slock);
    913  1.12   mycroft 	if (nfssvc_sockhead_flag & SLP_INIT)
    914  1.10   mycroft 		panic("nfsd init");
    915  1.12   mycroft 	nfssvc_sockhead_flag |= SLP_INIT;
    916  1.66      yamt 
    917  1.10   mycroft 	if (terminating) {
    918  1.66      yamt 		while ((slp = TAILQ_FIRST(&nfssvc_sockhead)) != NULL) {
    919  1.66      yamt 			TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
    920  1.66      yamt 			simple_unlock(&nfsd_slock);
    921  1.10   mycroft 			if (slp->ns_flag & SLP_VALID)
    922  1.10   mycroft 				nfsrv_zapsock(slp);
    923  1.66      yamt 			free(slp, M_NFSSVC);
    924  1.66      yamt 			simple_lock(&nfsd_slock);
    925  1.10   mycroft 		}
    926  1.66      yamt 		simple_unlock(&nfsd_slock);
    927  1.66      yamt 		splx(s);
    928  1.10   mycroft 		nfsrv_cleancache();	/* And clear out server cache */
    929  1.66      yamt 	} else {
    930  1.66      yamt 		simple_unlock(&nfsd_slock);
    931  1.66      yamt 		splx(s);
    932  1.26      fvdl 		nfs_pub.np_valid = 0;
    933  1.66      yamt 	}
    934  1.12   mycroft 
    935  1.12   mycroft 	TAILQ_INIT(&nfssvc_sockhead);
    936  1.66      yamt 	TAILQ_INIT(&nfssvc_sockpending);
    937  1.12   mycroft 	nfssvc_sockhead_flag &= ~SLP_INIT;
    938  1.12   mycroft 
    939  1.12   mycroft 	TAILQ_INIT(&nfsd_head);
    940  1.66      yamt 	SLIST_INIT(&nfsd_idle_head);
    941  1.12   mycroft 	nfsd_head_flag &= ~NFSD_CHECKSLP;
    942  1.12   mycroft 
    943  1.65      yamt 	nfs_udpsock = nfsrv_sockalloc();
    944  1.41      fvdl 
    945  1.41      fvdl #ifdef INET6
    946  1.65      yamt 	nfs_udp6sock = nfsrv_sockalloc();
    947  1.41      fvdl #endif
    948  1.12   mycroft 
    949  1.47       chs #ifdef ISO
    950  1.65      yamt 	nfs_cltpsock = nfsrv_sockalloc();
    951  1.47       chs #endif
    952  1.66      yamt 
    953  1.66      yamt 	simple_lock(&nfsd_slock);
    954  1.66      yamt 	if (nfssvc_sockhead_flag & SLP_WANTINIT) {
    955  1.66      yamt 		nfssvc_sockhead_flag &= ~SLP_WANTINIT;
    956  1.66      yamt 		wakeup(&nfssvc_sockhead);
    957  1.66      yamt 	}
    958  1.66      yamt 	simple_unlock(&nfsd_slock);
    959  1.66      yamt 	splx(s);
    960  1.10   mycroft }
    961  1.10   mycroft 
    962  1.10   mycroft /*
    963  1.10   mycroft  * Add entries to the server monitor log.
    964  1.10   mycroft  */
    965  1.10   mycroft static void
    966  1.19      fvdl nfsd_rt(sotype, nd, cacherep)
    967  1.10   mycroft 	int sotype;
    968  1.38  augustss 	struct nfsrv_descript *nd;
    969  1.10   mycroft 	int cacherep;
    970  1.10   mycroft {
    971  1.38  augustss 	struct drt *rt;
    972  1.10   mycroft 
    973  1.10   mycroft 	rt = &nfsdrt.drt[nfsdrt.pos];
    974  1.10   mycroft 	if (cacherep == RC_DOIT)
    975  1.10   mycroft 		rt->flag = 0;
    976  1.10   mycroft 	else if (cacherep == RC_REPLY)
    977  1.10   mycroft 		rt->flag = DRT_CACHEREPLY;
    978  1.10   mycroft 	else
    979  1.10   mycroft 		rt->flag = DRT_CACHEDROP;
    980  1.10   mycroft 	if (sotype == SOCK_STREAM)
    981  1.10   mycroft 		rt->flag |= DRT_TCP;
    982  1.19      fvdl 	if (nd->nd_flag & ND_NQNFS)
    983  1.10   mycroft 		rt->flag |= DRT_NQNFS;
    984  1.19      fvdl 	else if (nd->nd_flag & ND_NFSV3)
    985  1.19      fvdl 		rt->flag |= DRT_NFSV3;
    986  1.10   mycroft 	rt->proc = nd->nd_procnum;
    987  1.19      fvdl 	if (mtod(nd->nd_nam, struct sockaddr *)->sa_family == AF_INET)
    988  1.19      fvdl 	    rt->ipadr = mtod(nd->nd_nam, struct sockaddr_in *)->sin_addr.s_addr;
    989  1.10   mycroft 	else
    990  1.19      fvdl 	    rt->ipadr = INADDR_ANY;
    991  1.19      fvdl 	rt->resptime = ((time.tv_sec - nd->nd_starttime.tv_sec) * 1000000) +
    992  1.19      fvdl 		(time.tv_usec - nd->nd_starttime.tv_usec);
    993  1.10   mycroft 	rt->tstamp = time;
    994  1.10   mycroft 	nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ;
    995  1.10   mycroft }
    996   1.3     glass #endif /* NFSSERVER */
    997   1.3     glass 
    998  1.24   thorpej #ifdef NFS
    999  1.22   thorpej 
   1000  1.22   thorpej int nfs_defect = 0;
   1001   1.1       cgd /*
   1002  1.39    tsarna  * Asynchronous I/O threads for client nfs.
   1003  1.10   mycroft  * They do read-ahead and write-behind operations on the block I/O cache.
   1004  1.10   mycroft  * Never returns unless it fails or gets killed.
   1005   1.1       cgd  */
   1006  1.39    tsarna 
   1007  1.18  christos int
   1008  1.53   thorpej nfssvc_iod(l)
   1009  1.53   thorpej 	struct lwp *l;
   1010   1.1       cgd {
   1011  1.38  augustss 	struct buf *bp;
   1012  1.58      yamt 	int i;
   1013  1.58      yamt 	struct nfs_iod *myiod;
   1014  1.22   thorpej 	struct nfsmount *nmp;
   1015  1.22   thorpej 	int error = 0;
   1016  1.69      fvdl 	struct proc *p = l->l_proc;
   1017   1.1       cgd 
   1018   1.1       cgd 	/*
   1019   1.1       cgd 	 * Assign my position or return error if too many already running
   1020   1.1       cgd 	 */
   1021  1.58      yamt 	myiod = NULL;
   1022   1.1       cgd 	for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
   1023  1.58      yamt 		if (nfs_asyncdaemon[i].nid_proc == NULL) {
   1024  1.58      yamt 			myiod = &nfs_asyncdaemon[i];
   1025   1.1       cgd 			break;
   1026   1.1       cgd 		}
   1027  1.58      yamt 	if (myiod == NULL)
   1028   1.1       cgd 		return (EBUSY);
   1029  1.58      yamt 	myiod->nid_proc = p;
   1030   1.1       cgd 	nfs_numasync++;
   1031  1.53   thorpej 	PHOLD(l);
   1032   1.1       cgd 	/*
   1033  1.42   nathanw 	 * Just loop around doing our stuff until SIGKILL
   1034   1.1       cgd 	 */
   1035   1.1       cgd 	for (;;) {
   1036  1.60      yamt 		while (/*CONSTCOND*/ TRUE) {
   1037  1.60      yamt 			simple_lock(&myiod->nid_slock);
   1038  1.60      yamt 			nmp = myiod->nid_mount;
   1039  1.60      yamt 			if (nmp) {
   1040  1.60      yamt 				simple_lock(&nmp->nm_slock);
   1041  1.60      yamt 				if (!TAILQ_EMPTY(&nmp->nm_bufq)) {
   1042  1.60      yamt 					simple_unlock(&myiod->nid_slock);
   1043  1.60      yamt 					break;
   1044  1.60      yamt 				}
   1045  1.59      yamt 				nmp->nm_bufqiods--;
   1046  1.60      yamt 				simple_unlock(&nmp->nm_slock);
   1047  1.60      yamt 			}
   1048  1.59      yamt 			myiod->nid_want = p;
   1049  1.59      yamt 			myiod->nid_mount = NULL;
   1050  1.60      yamt 			error = ltsleep(&myiod->nid_want,
   1051  1.60      yamt 			    PWAIT | PCATCH | PNORELOCK, "nfsidl", 0,
   1052  1.60      yamt 			    &myiod->nid_slock);
   1053  1.60      yamt 			if (error)
   1054  1.60      yamt 				goto quit;
   1055  1.59      yamt 		}
   1056  1.60      yamt 
   1057  1.60      yamt 		while ((bp = TAILQ_FIRST(&nmp->nm_bufq)) != NULL) {
   1058  1.59      yamt 			/* Take one off the front of the list */
   1059  1.59      yamt 			TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist);
   1060  1.59      yamt 			nmp->nm_bufqlen--;
   1061  1.59      yamt 			if (nmp->nm_bufqwant &&
   1062  1.59      yamt 			    nmp->nm_bufqlen < 2 * nfs_numasync) {
   1063  1.59      yamt 				nmp->nm_bufqwant = FALSE;
   1064  1.59      yamt 				wakeup(&nmp->nm_bufq);
   1065  1.59      yamt 			}
   1066  1.60      yamt 			simple_unlock(&nmp->nm_slock);
   1067  1.59      yamt 			(void) nfs_doio(bp, NULL);
   1068  1.60      yamt 			simple_lock(&nmp->nm_slock);
   1069  1.59      yamt 			/*
   1070  1.59      yamt 			 * If there are more than one iod on this mount, then defect
   1071  1.59      yamt 			 * so that the iods can be shared out fairly between the mounts
   1072  1.59      yamt 			 */
   1073  1.59      yamt 			if (nfs_defect && nmp->nm_bufqiods > 1) {
   1074  1.59      yamt 				myiod->nid_mount = NULL;
   1075  1.59      yamt 				nmp->nm_bufqiods--;
   1076  1.59      yamt 				break;
   1077  1.59      yamt 			}
   1078  1.59      yamt 		}
   1079  1.60      yamt 		simple_unlock(&nmp->nm_slock);
   1080   1.1       cgd 	}
   1081  1.60      yamt quit:
   1082  1.53   thorpej 	PRELE(l);
   1083  1.45       chs 	if (nmp)
   1084  1.45       chs 		nmp->nm_bufqiods--;
   1085  1.58      yamt 	myiod->nid_want = NULL;
   1086  1.58      yamt 	myiod->nid_mount = NULL;
   1087  1.58      yamt 	myiod->nid_proc = NULL;
   1088  1.33   mycroft 	nfs_numasync--;
   1089  1.39    tsarna 
   1090  1.60      yamt 	return error;
   1091  1.60      yamt }
   1092  1.60      yamt 
   1093  1.60      yamt void
   1094  1.60      yamt nfs_iodinit()
   1095  1.60      yamt {
   1096  1.60      yamt 	int i;
   1097  1.60      yamt 
   1098  1.60      yamt 	for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
   1099  1.60      yamt 		simple_lock_init(&nfs_asyncdaemon[i].nid_slock);
   1100   1.1       cgd }
   1101   1.3     glass 
   1102  1.39    tsarna void
   1103  1.39    tsarna start_nfsio(arg)
   1104  1.39    tsarna 	void *arg;
   1105  1.39    tsarna {
   1106  1.53   thorpej 	nfssvc_iod(curlwp);
   1107  1.39    tsarna 
   1108  1.39    tsarna 	kthread_exit(0);
   1109  1.39    tsarna }
   1110  1.39    tsarna 
   1111  1.39    tsarna void
   1112  1.39    tsarna nfs_getset_niothreads(set)
   1113  1.39    tsarna 	int set;
   1114  1.39    tsarna {
   1115  1.39    tsarna 	int i, have, start;
   1116  1.39    tsarna 
   1117  1.39    tsarna 	for (have = 0, i = 0; i < NFS_MAXASYNCDAEMON; i++)
   1118  1.58      yamt 		if (nfs_asyncdaemon[i].nid_proc != NULL)
   1119  1.39    tsarna 			have++;
   1120  1.39    tsarna 
   1121  1.39    tsarna 	if (set) {
   1122  1.39    tsarna 		/* clamp to sane range */
   1123  1.39    tsarna 		nfs_niothreads = max(0, min(nfs_niothreads, NFS_MAXASYNCDAEMON));
   1124  1.39    tsarna 
   1125  1.39    tsarna 		start = nfs_niothreads - have;
   1126  1.39    tsarna 
   1127  1.39    tsarna 		while (start > 0) {
   1128  1.39    tsarna 			kthread_create1(start_nfsio, NULL, NULL, "nfsio");
   1129  1.39    tsarna 			start--;
   1130  1.39    tsarna 		}
   1131  1.39    tsarna 
   1132  1.39    tsarna 		for (i = 0; (start < 0) && (i < NFS_MAXASYNCDAEMON); i++)
   1133  1.58      yamt 			if (nfs_asyncdaemon[i].nid_proc != NULL) {
   1134  1.58      yamt 				psignal(nfs_asyncdaemon[i].nid_proc, SIGKILL);
   1135  1.39    tsarna 				start++;
   1136  1.39    tsarna 			}
   1137  1.39    tsarna 	} else {
   1138  1.40    tsarna 		if (nfs_niothreads >= 0)
   1139  1.40    tsarna 			nfs_niothreads = have;
   1140  1.39    tsarna 	}
   1141  1.39    tsarna }
   1142  1.19      fvdl 
   1143  1.10   mycroft /*
   1144  1.10   mycroft  * Get an authorization string for the uid by having the mount_nfs sitting
   1145  1.10   mycroft  * on this mount point porpous out of the kernel and do it.
   1146  1.10   mycroft  */
   1147  1.18  christos int
   1148  1.19      fvdl nfs_getauth(nmp, rep, cred, auth_str, auth_len, verf_str, verf_len, key)
   1149  1.38  augustss 	struct nfsmount *nmp;
   1150  1.10   mycroft 	struct nfsreq *rep;
   1151  1.10   mycroft 	struct ucred *cred;
   1152  1.10   mycroft 	char **auth_str;
   1153  1.10   mycroft 	int *auth_len;
   1154  1.19      fvdl 	char *verf_str;
   1155  1.19      fvdl 	int *verf_len;
   1156  1.19      fvdl 	NFSKERBKEY_T key;		/* return session key */
   1157  1.10   mycroft {
   1158  1.10   mycroft 	int error = 0;
   1159  1.10   mycroft 
   1160  1.27      fvdl 	while ((nmp->nm_iflag & NFSMNT_WAITAUTH) == 0) {
   1161  1.27      fvdl 		nmp->nm_iflag |= NFSMNT_WANTAUTH;
   1162  1.10   mycroft 		(void) tsleep((caddr_t)&nmp->nm_authtype, PSOCK,
   1163  1.10   mycroft 			"nfsauth1", 2 * hz);
   1164  1.69      fvdl 		error = nfs_sigintr(nmp, rep, rep->r_procp);
   1165  1.18  christos 		if (error) {
   1166  1.27      fvdl 			nmp->nm_iflag &= ~NFSMNT_WANTAUTH;
   1167  1.10   mycroft 			return (error);
   1168  1.10   mycroft 		}
   1169  1.10   mycroft 	}
   1170  1.27      fvdl 	nmp->nm_iflag &= ~(NFSMNT_WAITAUTH | NFSMNT_WANTAUTH);
   1171  1.10   mycroft 	nmp->nm_authstr = *auth_str = (char *)malloc(RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK);
   1172  1.19      fvdl 	nmp->nm_authlen = RPCAUTH_MAXSIZ;
   1173  1.19      fvdl 	nmp->nm_verfstr = verf_str;
   1174  1.19      fvdl 	nmp->nm_verflen = *verf_len;
   1175  1.10   mycroft 	nmp->nm_authuid = cred->cr_uid;
   1176  1.10   mycroft 	wakeup((caddr_t)&nmp->nm_authstr);
   1177   1.3     glass 
   1178  1.10   mycroft 	/*
   1179  1.10   mycroft 	 * And wait for mount_nfs to do its stuff.
   1180  1.10   mycroft 	 */
   1181  1.27      fvdl 	while ((nmp->nm_iflag & NFSMNT_HASAUTH) == 0 && error == 0) {
   1182  1.10   mycroft 		(void) tsleep((caddr_t)&nmp->nm_authlen, PSOCK,
   1183  1.10   mycroft 			"nfsauth2", 2 * hz);
   1184  1.69      fvdl 		error = nfs_sigintr(nmp, rep, rep->r_procp);
   1185  1.10   mycroft 	}
   1186  1.27      fvdl 	if (nmp->nm_iflag & NFSMNT_AUTHERR) {
   1187  1.27      fvdl 		nmp->nm_iflag &= ~NFSMNT_AUTHERR;
   1188  1.10   mycroft 		error = EAUTH;
   1189  1.10   mycroft 	}
   1190  1.10   mycroft 	if (error)
   1191  1.10   mycroft 		free((caddr_t)*auth_str, M_TEMP);
   1192  1.10   mycroft 	else {
   1193  1.10   mycroft 		*auth_len = nmp->nm_authlen;
   1194  1.19      fvdl 		*verf_len = nmp->nm_verflen;
   1195  1.52       chs 		memcpy(key, nmp->nm_key, sizeof (NFSKERBKEY_T));
   1196  1.10   mycroft 	}
   1197  1.27      fvdl 	nmp->nm_iflag &= ~NFSMNT_HASAUTH;
   1198  1.27      fvdl 	nmp->nm_iflag |= NFSMNT_WAITAUTH;
   1199  1.27      fvdl 	if (nmp->nm_iflag & NFSMNT_WANTAUTH) {
   1200  1.27      fvdl 		nmp->nm_iflag &= ~NFSMNT_WANTAUTH;
   1201  1.10   mycroft 		wakeup((caddr_t)&nmp->nm_authtype);
   1202  1.10   mycroft 	}
   1203  1.19      fvdl 	return (error);
   1204  1.19      fvdl }
   1205  1.19      fvdl 
   1206  1.19      fvdl /*
   1207  1.19      fvdl  * Get a nickname authenticator and verifier.
   1208  1.19      fvdl  */
   1209  1.19      fvdl int
   1210  1.19      fvdl nfs_getnickauth(nmp, cred, auth_str, auth_len, verf_str, verf_len)
   1211  1.19      fvdl 	struct nfsmount *nmp;
   1212  1.19      fvdl 	struct ucred *cred;
   1213  1.19      fvdl 	char **auth_str;
   1214  1.19      fvdl 	int *auth_len;
   1215  1.19      fvdl 	char *verf_str;
   1216  1.19      fvdl 	int verf_len;
   1217  1.19      fvdl {
   1218  1.38  augustss 	struct nfsuid *nuidp;
   1219  1.38  augustss 	u_int32_t *nickp, *verfp;
   1220  1.19      fvdl 	struct timeval ktvin, ktvout;
   1221  1.19      fvdl 
   1222  1.19      fvdl #ifdef DIAGNOSTIC
   1223  1.19      fvdl 	if (verf_len < (4 * NFSX_UNSIGNED))
   1224  1.19      fvdl 		panic("nfs_getnickauth verf too small");
   1225  1.19      fvdl #endif
   1226  1.57      yamt 	LIST_FOREACH(nuidp, NMUIDHASH(nmp, cred->cr_uid), nu_hash) {
   1227  1.19      fvdl 		if (nuidp->nu_cr.cr_uid == cred->cr_uid)
   1228  1.19      fvdl 			break;
   1229  1.19      fvdl 	}
   1230  1.19      fvdl 	if (!nuidp || nuidp->nu_expire < time.tv_sec)
   1231  1.19      fvdl 		return (EACCES);
   1232  1.19      fvdl 
   1233  1.19      fvdl 	/*
   1234  1.19      fvdl 	 * Move to the end of the lru list (end of lru == most recently used).
   1235  1.19      fvdl 	 */
   1236  1.19      fvdl 	TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
   1237  1.19      fvdl 	TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, nu_lru);
   1238  1.19      fvdl 
   1239  1.19      fvdl 	nickp = (u_int32_t *)malloc(2 * NFSX_UNSIGNED, M_TEMP, M_WAITOK);
   1240  1.19      fvdl 	*nickp++ = txdr_unsigned(RPCAKN_NICKNAME);
   1241  1.19      fvdl 	*nickp = txdr_unsigned(nuidp->nu_nickname);
   1242  1.19      fvdl 	*auth_str = (char *)nickp;
   1243  1.19      fvdl 	*auth_len = 2 * NFSX_UNSIGNED;
   1244  1.19      fvdl 
   1245  1.19      fvdl 	/*
   1246  1.19      fvdl 	 * Now we must encrypt the verifier and package it up.
   1247  1.19      fvdl 	 */
   1248  1.19      fvdl 	verfp = (u_int32_t *)verf_str;
   1249  1.19      fvdl 	*verfp++ = txdr_unsigned(RPCAKN_NICKNAME);
   1250  1.19      fvdl 	if (time.tv_sec > nuidp->nu_timestamp.tv_sec ||
   1251  1.19      fvdl 	    (time.tv_sec == nuidp->nu_timestamp.tv_sec &&
   1252  1.19      fvdl 	     time.tv_usec > nuidp->nu_timestamp.tv_usec))
   1253  1.19      fvdl 		nuidp->nu_timestamp = time;
   1254  1.19      fvdl 	else
   1255  1.19      fvdl 		nuidp->nu_timestamp.tv_usec++;
   1256  1.19      fvdl 	ktvin.tv_sec = txdr_unsigned(nuidp->nu_timestamp.tv_sec);
   1257  1.19      fvdl 	ktvin.tv_usec = txdr_unsigned(nuidp->nu_timestamp.tv_usec);
   1258  1.19      fvdl 
   1259  1.19      fvdl 	/*
   1260  1.19      fvdl 	 * Now encrypt the timestamp verifier in ecb mode using the session
   1261  1.19      fvdl 	 * key.
   1262  1.19      fvdl 	 */
   1263  1.19      fvdl #ifdef NFSKERB
   1264  1.19      fvdl 	XXX
   1265  1.19      fvdl #endif
   1266  1.19      fvdl 
   1267  1.19      fvdl 	*verfp++ = ktvout.tv_sec;
   1268  1.19      fvdl 	*verfp++ = ktvout.tv_usec;
   1269  1.19      fvdl 	*verfp = 0;
   1270  1.19      fvdl 	return (0);
   1271  1.19      fvdl }
   1272  1.19      fvdl 
   1273  1.19      fvdl /*
   1274  1.19      fvdl  * Save the current nickname in a hash list entry on the mount point.
   1275  1.19      fvdl  */
   1276  1.19      fvdl int
   1277  1.19      fvdl nfs_savenickauth(nmp, cred, len, key, mdp, dposp, mrep)
   1278  1.38  augustss 	struct nfsmount *nmp;
   1279  1.19      fvdl 	struct ucred *cred;
   1280  1.19      fvdl 	int len;
   1281  1.19      fvdl 	NFSKERBKEY_T key;
   1282  1.19      fvdl 	struct mbuf **mdp;
   1283  1.19      fvdl 	char **dposp;
   1284  1.19      fvdl 	struct mbuf *mrep;
   1285  1.19      fvdl {
   1286  1.38  augustss 	struct nfsuid *nuidp;
   1287  1.38  augustss 	u_int32_t *tl;
   1288  1.38  augustss 	int32_t t1;
   1289  1.19      fvdl 	struct mbuf *md = *mdp;
   1290  1.19      fvdl 	struct timeval ktvin, ktvout;
   1291  1.19      fvdl 	u_int32_t nick;
   1292  1.19      fvdl 	char *dpos = *dposp, *cp2;
   1293  1.19      fvdl 	int deltasec, error = 0;
   1294  1.19      fvdl 
   1295  1.19      fvdl 	if (len == (3 * NFSX_UNSIGNED)) {
   1296  1.19      fvdl 		nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   1297  1.19      fvdl 		ktvin.tv_sec = *tl++;
   1298  1.19      fvdl 		ktvin.tv_usec = *tl++;
   1299  1.19      fvdl 		nick = fxdr_unsigned(u_int32_t, *tl);
   1300  1.19      fvdl 
   1301  1.19      fvdl 		/*
   1302  1.19      fvdl 		 * Decrypt the timestamp in ecb mode.
   1303  1.19      fvdl 		 */
   1304  1.19      fvdl #ifdef NFSKERB
   1305  1.19      fvdl 		XXX
   1306  1.19      fvdl #endif
   1307  1.19      fvdl 		ktvout.tv_sec = fxdr_unsigned(long, ktvout.tv_sec);
   1308  1.19      fvdl 		ktvout.tv_usec = fxdr_unsigned(long, ktvout.tv_usec);
   1309  1.19      fvdl 		deltasec = time.tv_sec - ktvout.tv_sec;
   1310  1.19      fvdl 		if (deltasec < 0)
   1311  1.19      fvdl 			deltasec = -deltasec;
   1312  1.19      fvdl 		/*
   1313  1.19      fvdl 		 * If ok, add it to the hash list for the mount point.
   1314  1.19      fvdl 		 */
   1315  1.19      fvdl 		if (deltasec <= NFS_KERBCLOCKSKEW) {
   1316  1.19      fvdl 			if (nmp->nm_numuids < nuidhash_max) {
   1317  1.19      fvdl 				nmp->nm_numuids++;
   1318  1.19      fvdl 				nuidp = (struct nfsuid *)
   1319  1.19      fvdl 				   malloc(sizeof (struct nfsuid), M_NFSUID,
   1320  1.19      fvdl 					M_WAITOK);
   1321  1.19      fvdl 			} else {
   1322  1.57      yamt 				nuidp = TAILQ_FIRST(&nmp->nm_uidlruhead);
   1323  1.19      fvdl 				LIST_REMOVE(nuidp, nu_hash);
   1324  1.19      fvdl 				TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp,
   1325  1.19      fvdl 					nu_lru);
   1326  1.19      fvdl 			}
   1327  1.19      fvdl 			nuidp->nu_flag = 0;
   1328  1.19      fvdl 			nuidp->nu_cr.cr_uid = cred->cr_uid;
   1329  1.19      fvdl 			nuidp->nu_expire = time.tv_sec + NFS_KERBTTL;
   1330  1.19      fvdl 			nuidp->nu_timestamp = ktvout;
   1331  1.19      fvdl 			nuidp->nu_nickname = nick;
   1332  1.52       chs 			memcpy(nuidp->nu_key, key, sizeof (NFSKERBKEY_T));
   1333  1.19      fvdl 			TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp,
   1334  1.19      fvdl 				nu_lru);
   1335  1.19      fvdl 			LIST_INSERT_HEAD(NMUIDHASH(nmp, cred->cr_uid),
   1336  1.19      fvdl 				nuidp, nu_hash);
   1337  1.19      fvdl 		}
   1338  1.19      fvdl 	} else
   1339  1.19      fvdl 		nfsm_adv(nfsm_rndup(len));
   1340  1.19      fvdl nfsmout:
   1341  1.19      fvdl 	*mdp = md;
   1342  1.19      fvdl 	*dposp = dpos;
   1343  1.10   mycroft 	return (error);
   1344  1.10   mycroft }
   1345  1.24   thorpej #endif /* NFS */
   1346