Home | History | Annotate | Line # | Download | only in nfs
nfs_syscalls.c revision 1.143
      1  1.143     pooka /*	$NetBSD: nfs_syscalls.c,v 1.143 2008/11/28 06:47:08 pooka 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.70       agc  * 3. Neither the name of the University nor the names of its contributors
     19    1.1       cgd  *    may be used to endorse or promote products derived from this software
     20    1.1       cgd  *    without specific prior written permission.
     21    1.1       cgd  *
     22    1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23    1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24    1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25    1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26    1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27    1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28    1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29    1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30    1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31    1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32    1.1       cgd  * SUCH DAMAGE.
     33    1.1       cgd  *
     34   1.19      fvdl  *	@(#)nfs_syscalls.c	8.5 (Berkeley) 3/30/95
     35    1.1       cgd  */
     36   1.49     lukem 
     37   1.49     lukem #include <sys/cdefs.h>
     38  1.143     pooka __KERNEL_RCSID(0, "$NetBSD: nfs_syscalls.c,v 1.143 2008/11/28 06:47:08 pooka Exp $");
     39    1.1       cgd 
     40    1.7   mycroft #include <sys/param.h>
     41    1.7   mycroft #include <sys/systm.h>
     42    1.7   mycroft #include <sys/kernel.h>
     43    1.7   mycroft #include <sys/file.h>
     44    1.7   mycroft #include <sys/stat.h>
     45    1.7   mycroft #include <sys/vnode.h>
     46    1.7   mycroft #include <sys/mount.h>
     47    1.7   mycroft #include <sys/proc.h>
     48   1.10   mycroft #include <sys/uio.h>
     49    1.7   mycroft #include <sys/malloc.h>
     50  1.124      yamt #include <sys/kmem.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.39    tsarna #include <sys/signalvar.h>
     56    1.7   mycroft #include <sys/domain.h>
     57    1.7   mycroft #include <sys/protosw.h>
     58   1.10   mycroft #include <sys/namei.h>
     59   1.10   mycroft #include <sys/syslog.h>
     60   1.18  christos #include <sys/filedesc.h>
     61   1.39    tsarna #include <sys/kthread.h>
     62   1.92      elad #include <sys/kauth.h>
     63   1.13       cgd #include <sys/syscallargs.h>
     64   1.13       cgd 
     65    1.7   mycroft #include <netinet/in.h>
     66    1.7   mycroft #include <netinet/tcp.h>
     67   1.19      fvdl #include <nfs/xdr_subs.h>
     68   1.10   mycroft #include <nfs/rpcv2.h>
     69   1.19      fvdl #include <nfs/nfsproto.h>
     70    1.7   mycroft #include <nfs/nfs.h>
     71   1.19      fvdl #include <nfs/nfsm_subs.h>
     72    1.7   mycroft #include <nfs/nfsrvcache.h>
     73   1.10   mycroft #include <nfs/nfsmount.h>
     74   1.10   mycroft #include <nfs/nfsnode.h>
     75   1.10   mycroft #include <nfs/nfsrtt.h>
     76   1.18  christos #include <nfs/nfs_var.h>
     77    1.1       cgd 
     78   1.19      fvdl extern int32_t (*nfsrv3_procs[NFS_NPROCS]) __P((struct nfsrv_descript *,
     79   1.19      fvdl 						struct nfssvc_sock *,
     80   1.84  christos 						struct lwp *, struct mbuf **));
     81   1.19      fvdl extern int nfsrvw_procrastinate;
     82  1.142        ad extern int nuidhash_max;
     83   1.51      matt 
     84   1.10   mycroft static int nfs_numnfsd = 0;
     85   1.10   mycroft static struct nfsdrt nfsdrt;
     86  1.111      yamt kmutex_t nfsd_lock;
     87   1.51      matt struct nfssvc_sockhead nfssvc_sockhead;
     88  1.111      yamt kcondvar_t nfsd_initcv;
     89   1.66      yamt struct nfssvc_sockhead nfssvc_sockpending;
     90   1.51      matt struct nfsdhead nfsd_head;
     91   1.66      yamt struct nfsdidlehead nfsd_idle_head;
     92   1.51      matt 
     93   1.51      matt int nfssvc_sockhead_flag;
     94   1.51      matt int nfsd_head_flag;
     95    1.1       cgd 
     96  1.142        ad struct nfssvc_sock *nfs_udpsock;
     97  1.142        ad struct nfssvc_sock *nfs_udp6sock;
     98   1.19      fvdl 
     99   1.65      yamt static struct nfssvc_sock *nfsrv_sockalloc __P((void));
    100   1.77      yamt static void nfsrv_sockfree __P((struct nfssvc_sock *));
    101   1.86      yamt static void nfsd_rt __P((int, struct nfsrv_descript *, int));
    102   1.19      fvdl 
    103    1.1       cgd /*
    104    1.1       cgd  * NFS server system calls
    105    1.1       cgd  */
    106    1.1       cgd 
    107    1.1       cgd 
    108    1.1       cgd /*
    109   1.19      fvdl  * Nfs server pseudo system call for the nfsd's
    110   1.10   mycroft  * Based on the flag value it either:
    111   1.10   mycroft  * - adds a socket to the selection list
    112   1.10   mycroft  * - remains in the kernel as an nfsd
    113   1.10   mycroft  * - remains in the kernel as an nfsiod
    114    1.1       cgd  */
    115   1.18  christos int
    116  1.128       dsl sys_nfssvc(struct lwp *l, const struct sys_nfssvc_args *uap, register_t *retval)
    117   1.15   thorpej {
    118  1.128       dsl 	/* {
    119   1.13       cgd 		syscallarg(int) flag;
    120  1.107  christos 		syscallarg(void *) argp;
    121  1.128       dsl 	} */
    122   1.23   thorpej 	int error;
    123  1.132        ad 	file_t *fp;
    124   1.10   mycroft 	struct mbuf *nam;
    125   1.10   mycroft 	struct nfsd_args nfsdarg;
    126   1.10   mycroft 	struct nfsd_srvargs nfsd_srvargs, *nsd = &nfsd_srvargs;
    127   1.10   mycroft 	struct nfsd *nfsd;
    128   1.10   mycroft 	struct nfssvc_sock *slp;
    129   1.18  christos 	struct nfsuid *nuidp;
    130    1.1       cgd 
    131    1.1       cgd 	/*
    132    1.1       cgd 	 * Must be super user
    133    1.1       cgd 	 */
    134  1.131      elad 	error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_NFS,
    135  1.131      elad 	    KAUTH_REQ_NETWORK_NFS_SVC, NULL, NULL, NULL);
    136   1.71   thorpej 	if (error)
    137    1.1       cgd 		return (error);
    138   1.83   thorpej 
    139  1.111      yamt 	mutex_enter(&nfsd_lock);
    140   1.12   mycroft 	while (nfssvc_sockhead_flag & SLP_INIT) {
    141  1.111      yamt 		cv_wait(&nfsd_initcv, &nfsd_lock);
    142   1.10   mycroft 	}
    143  1.111      yamt 	mutex_exit(&nfsd_lock);
    144  1.142        ad 
    145   1.13       cgd 	if (SCARG(uap, flag) & NFSSVC_BIOD) {
    146  1.141        ad 		/* Dummy implementation of nfsios for 1.4 and earlier. */
    147  1.141        ad 		error = kpause("nfsbiod", true, 0, NULL);
    148   1.13       cgd 	} else if (SCARG(uap, flag) & NFSSVC_MNTD) {
    149   1.10   mycroft 		error = ENOSYS;
    150   1.13       cgd 	} else if (SCARG(uap, flag) & NFSSVC_ADDSOCK) {
    151  1.107  christos 		error = copyin(SCARG(uap, argp), (void *)&nfsdarg,
    152   1.19      fvdl 		    sizeof(nfsdarg));
    153   1.18  christos 		if (error)
    154   1.10   mycroft 			return (error);
    155   1.35   thorpej 		/* getsock() will use the descriptor for us */
    156  1.137        ad 		if ((fp = fd_getfile(nfsdarg.sock)) == NULL)
    157  1.137        ad 			return (EBADF);
    158  1.137        ad 		if (fp->f_type != DTYPE_SOCKET) {
    159  1.137        ad 			fd_putfile(nfsdarg.sock);
    160  1.137        ad 			return (ENOTSOCK);
    161  1.137        ad 		}
    162   1.19      fvdl 		if (error)
    163   1.10   mycroft 			return (error);
    164   1.10   mycroft 		/*
    165   1.10   mycroft 		 * Get the client address for connected sockets.
    166   1.10   mycroft 		 */
    167   1.10   mycroft 		if (nfsdarg.name == NULL || nfsdarg.namelen == 0)
    168   1.10   mycroft 			nam = (struct mbuf *)0;
    169   1.18  christos 		else {
    170   1.18  christos 			error = sockargs(&nam, nfsdarg.name, nfsdarg.namelen,
    171   1.19      fvdl 				MT_SONAME);
    172   1.35   thorpej 			if (error) {
    173  1.132        ad 				fd_putfile(nfsdarg.sock);
    174   1.18  christos 				return (error);
    175   1.35   thorpej 			}
    176   1.18  christos 		}
    177   1.10   mycroft 		error = nfssvc_addsock(fp, nam);
    178  1.132        ad 		fd_putfile(nfsdarg.sock);
    179   1.82      jmmv 	} else if (SCARG(uap, flag) & NFSSVC_SETEXPORTSLIST) {
    180   1.82      jmmv 		struct export_args *args;
    181   1.82      jmmv 		struct mountd_exports_list mel;
    182   1.82      jmmv 
    183   1.82      jmmv 		error = copyin(SCARG(uap, argp), &mel, sizeof(mel));
    184   1.82      jmmv 		if (error != 0)
    185   1.82      jmmv 			return error;
    186   1.82      jmmv 
    187   1.82      jmmv 		args = (struct export_args *)malloc(mel.mel_nexports *
    188   1.82      jmmv 		    sizeof(struct export_args), M_TEMP, M_WAITOK);
    189   1.82      jmmv 		error = copyin(mel.mel_exports, args, mel.mel_nexports *
    190   1.82      jmmv 		    sizeof(struct export_args));
    191   1.82      jmmv 		if (error != 0) {
    192   1.82      jmmv 			free(args, M_TEMP);
    193   1.82      jmmv 			return error;
    194   1.82      jmmv 		}
    195   1.82      jmmv 		mel.mel_exports = args;
    196   1.82      jmmv 
    197   1.84  christos 		error = mountd_set_exports_list(&mel, l);
    198   1.82      jmmv 
    199   1.82      jmmv 		free(args, M_TEMP);
    200   1.10   mycroft 	} else {
    201  1.107  christos 		error = copyin(SCARG(uap, argp), (void *)nsd, sizeof (*nsd));
    202   1.18  christos 		if (error)
    203   1.10   mycroft 			return (error);
    204   1.13       cgd 		if ((SCARG(uap, flag) & NFSSVC_AUTHIN) &&
    205   1.19      fvdl 		    ((nfsd = nsd->nsd_nfsd)) != NULL &&
    206  1.127      yamt 		    (nfsd->nfsd_slp->ns_flags & SLP_VALID)) {
    207   1.19      fvdl 			slp = nfsd->nfsd_slp;
    208   1.10   mycroft 
    209   1.10   mycroft 			/*
    210   1.10   mycroft 			 * First check to see if another nfsd has already
    211   1.10   mycroft 			 * added this credential.
    212   1.10   mycroft 			 */
    213   1.92      elad 			LIST_FOREACH(nuidp, NUIDHASH(slp, nsd->nsd_cr.cr_uid),
    214   1.57      yamt 			    nu_hash) {
    215   1.93      yamt 				if (kauth_cred_geteuid(nuidp->nu_cr) ==
    216   1.93      yamt 				    nsd->nsd_cr.cr_uid &&
    217   1.19      fvdl 				    (!nfsd->nfsd_nd->nd_nam2 ||
    218   1.19      fvdl 				     netaddr_match(NU_NETFAM(nuidp),
    219   1.19      fvdl 				     &nuidp->nu_haddr, nfsd->nfsd_nd->nd_nam2)))
    220   1.10   mycroft 					break;
    221   1.10   mycroft 			}
    222   1.19      fvdl 			if (nuidp) {
    223   1.93      yamt 			    kauth_cred_hold(nuidp->nu_cr);
    224   1.93      yamt 			    nfsd->nfsd_nd->nd_cr = nuidp->nu_cr;
    225   1.19      fvdl 			    nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
    226   1.19      fvdl 			} else {
    227   1.10   mycroft 			    /*
    228   1.10   mycroft 			     * Nope, so we will.
    229   1.10   mycroft 			     */
    230   1.10   mycroft 			    if (slp->ns_numuids < nuidhash_max) {
    231   1.10   mycroft 				slp->ns_numuids++;
    232  1.130      yamt 				nuidp = kmem_alloc(sizeof(*nuidp), KM_SLEEP);
    233   1.10   mycroft 			    } else
    234   1.10   mycroft 				nuidp = (struct nfsuid *)0;
    235  1.127      yamt 			    if ((slp->ns_flags & SLP_VALID) == 0) {
    236   1.10   mycroft 				if (nuidp)
    237  1.130      yamt 				    kmem_free(nuidp, sizeof(*nuidp));
    238   1.10   mycroft 			    } else {
    239   1.10   mycroft 				if (nuidp == (struct nfsuid *)0) {
    240   1.57      yamt 				    nuidp = TAILQ_FIRST(&slp->ns_uidlruhead);
    241   1.12   mycroft 				    LIST_REMOVE(nuidp, nu_hash);
    242   1.12   mycroft 				    TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp,
    243   1.12   mycroft 					nu_lru);
    244   1.19      fvdl 				    if (nuidp->nu_flag & NU_NAM)
    245   1.19      fvdl 					m_freem(nuidp->nu_nam);
    246   1.10   mycroft 			        }
    247   1.19      fvdl 				nuidp->nu_flag = 0;
    248   1.98     pooka 				kauth_uucred_to_cred(nuidp->nu_cr,
    249   1.98     pooka 				    &nsd->nsd_cr);
    250   1.19      fvdl 				nuidp->nu_timestamp = nsd->nsd_timestamp;
    251   1.95    kardel 				nuidp->nu_expire = time_second + nsd->nsd_ttl;
    252   1.19      fvdl 				/*
    253   1.19      fvdl 				 * and save the session key in nu_key.
    254   1.19      fvdl 				 */
    255   1.32     perry 				memcpy(nuidp->nu_key, nsd->nsd_key,
    256   1.32     perry 				    sizeof(nsd->nsd_key));
    257   1.19      fvdl 				if (nfsd->nfsd_nd->nd_nam2) {
    258   1.19      fvdl 				    struct sockaddr_in *saddr;
    259   1.19      fvdl 
    260   1.19      fvdl 				    saddr = mtod(nfsd->nfsd_nd->nd_nam2,
    261   1.19      fvdl 					 struct sockaddr_in *);
    262   1.19      fvdl 				    switch (saddr->sin_family) {
    263   1.19      fvdl 				    case AF_INET:
    264   1.19      fvdl 					nuidp->nu_flag |= NU_INETADDR;
    265   1.19      fvdl 					nuidp->nu_inetaddr =
    266   1.19      fvdl 					     saddr->sin_addr.s_addr;
    267   1.19      fvdl 					break;
    268  1.140  christos 				    case AF_INET6:
    269   1.19      fvdl 					nuidp->nu_flag |= NU_NAM;
    270   1.19      fvdl 					nuidp->nu_nam = m_copym(
    271   1.19      fvdl 					    nfsd->nfsd_nd->nd_nam2, 0,
    272   1.19      fvdl 					     M_COPYALL, M_WAIT);
    273   1.19      fvdl 					break;
    274  1.140  christos 				    default:
    275  1.140  christos 					return EAFNOSUPPORT;
    276   1.19      fvdl 				    };
    277   1.19      fvdl 				}
    278   1.12   mycroft 				TAILQ_INSERT_TAIL(&slp->ns_uidlruhead, nuidp,
    279   1.19      fvdl 					nu_lru);
    280   1.12   mycroft 				LIST_INSERT_HEAD(NUIDHASH(slp, nsd->nsd_uid),
    281   1.19      fvdl 					nuidp, nu_hash);
    282   1.93      yamt 				kauth_cred_hold(nuidp->nu_cr);
    283   1.93      yamt 				nfsd->nfsd_nd->nd_cr = nuidp->nu_cr;
    284   1.19      fvdl 				nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
    285   1.10   mycroft 			    }
    286   1.10   mycroft 			}
    287   1.10   mycroft 		}
    288   1.13       cgd 		if ((SCARG(uap, flag) & NFSSVC_AUTHINFAIL) &&
    289   1.13       cgd 		    (nfsd = nsd->nsd_nfsd))
    290   1.19      fvdl 			nfsd->nfsd_flag |= NFSD_AUTHFAIL;
    291   1.53   thorpej 		error = nfssvc_nfsd(nsd, SCARG(uap, argp), l);
    292   1.10   mycroft 	}
    293   1.10   mycroft 	if (error == EINTR || error == ERESTART)
    294   1.10   mycroft 		error = 0;
    295   1.10   mycroft 	return (error);
    296   1.10   mycroft }
    297   1.10   mycroft 
    298   1.65      yamt static struct nfssvc_sock *
    299   1.65      yamt nfsrv_sockalloc()
    300   1.65      yamt {
    301   1.65      yamt 	struct nfssvc_sock *slp;
    302   1.78     perry 
    303  1.130      yamt 	slp = kmem_alloc(sizeof(*slp), KM_SLEEP);
    304   1.65      yamt 	memset(slp, 0, sizeof (struct nfssvc_sock));
    305  1.135      yamt 	mutex_init(&slp->ns_lock, MUTEX_DRIVER, IPL_SOFTNET);
    306  1.135      yamt 	mutex_init(&slp->ns_alock, MUTEX_DRIVER, IPL_SOFTNET);
    307  1.111      yamt 	cv_init(&slp->ns_cv, "nfsdsock");
    308   1.65      yamt 	TAILQ_INIT(&slp->ns_uidlruhead);
    309   1.86      yamt 	LIST_INIT(&slp->ns_tq);
    310   1.86      yamt 	SIMPLEQ_INIT(&slp->ns_sendq);
    311  1.111      yamt 	mutex_enter(&nfsd_lock);
    312   1.65      yamt 	TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
    313  1.111      yamt 	mutex_exit(&nfsd_lock);
    314   1.65      yamt 
    315   1.65      yamt 	return slp;
    316   1.65      yamt }
    317   1.65      yamt 
    318   1.77      yamt static void
    319   1.77      yamt nfsrv_sockfree(struct nfssvc_sock *slp)
    320   1.77      yamt {
    321   1.77      yamt 
    322   1.77      yamt 	KASSERT(slp->ns_so == NULL);
    323   1.77      yamt 	KASSERT(slp->ns_fp == NULL);
    324  1.127      yamt 	KASSERT((slp->ns_flags & SLP_VALID) == 0);
    325  1.111      yamt 	mutex_destroy(&slp->ns_lock);
    326  1.127      yamt 	mutex_destroy(&slp->ns_alock);
    327  1.111      yamt 	cv_destroy(&slp->ns_cv);
    328  1.130      yamt 	kmem_free(slp, sizeof(*slp));
    329   1.77      yamt }
    330   1.77      yamt 
    331   1.10   mycroft /*
    332   1.10   mycroft  * Adds a socket to the list for servicing by nfsds.
    333   1.10   mycroft  */
    334   1.18  christos int
    335   1.10   mycroft nfssvc_addsock(fp, mynam)
    336  1.132        ad 	file_t *fp;
    337   1.10   mycroft 	struct mbuf *mynam;
    338   1.10   mycroft {
    339   1.38  augustss 	int siz;
    340   1.38  augustss 	struct nfssvc_sock *slp;
    341   1.38  augustss 	struct socket *so;
    342   1.10   mycroft 	struct nfssvc_sock *tslp;
    343  1.134        ad 	int error;
    344  1.138    plunky 	int val;
    345   1.10   mycroft 
    346    1.1       cgd 	so = (struct socket *)fp->f_data;
    347   1.10   mycroft 	tslp = (struct nfssvc_sock *)0;
    348   1.10   mycroft 	/*
    349   1.10   mycroft 	 * Add it to the list, as required.
    350   1.10   mycroft 	 */
    351   1.10   mycroft 	if (so->so_proto->pr_protocol == IPPROTO_UDP) {
    352   1.41      fvdl 		if (so->so_proto->pr_domain->dom_family == AF_INET6)
    353   1.41      fvdl 			tslp = nfs_udp6sock;
    354  1.142        ad 		else {
    355  1.142        ad 			tslp = nfs_udpsock;
    356  1.142        ad 			if (tslp->ns_flags & SLP_VALID) {
    357  1.142        ad 				m_freem(mynam);
    358  1.142        ad 				return (EPERM);
    359  1.142        ad 			}
    360   1.10   mycroft 		}
    361   1.10   mycroft 	}
    362   1.10   mycroft 	if (so->so_type == SOCK_STREAM)
    363   1.19      fvdl 		siz = NFS_MAXPACKET + sizeof (u_long);
    364   1.10   mycroft 	else
    365    1.1       cgd 		siz = NFS_MAXPACKET;
    366  1.134        ad 	solock(so);
    367   1.78     perry 	error = soreserve(so, siz, siz);
    368  1.134        ad 	sounlock(so);
    369   1.19      fvdl 	if (error) {
    370   1.10   mycroft 		m_freem(mynam);
    371   1.10   mycroft 		return (error);
    372   1.10   mycroft 	}
    373    1.1       cgd 
    374    1.1       cgd 	/*
    375    1.1       cgd 	 * Set protocol specific options { for now TCP only } and
    376    1.1       cgd 	 * reserve some space. For datagram sockets, this can get called
    377    1.1       cgd 	 * repeatedly for the same socket, but that isn't harmful.
    378    1.1       cgd 	 */
    379   1.10   mycroft 	if (so->so_type == SOCK_STREAM) {
    380  1.138    plunky 		val = 1;
    381  1.138    plunky 		so_setsockopt(NULL, so, SOL_SOCKET, SO_KEEPALIVE, &val,
    382  1.138    plunky 		    sizeof(val));
    383    1.1       cgd 	}
    384  1.142        ad 	if ((so->so_proto->pr_domain->dom_family == AF_INET ||
    385  1.142        ad 	    so->so_proto->pr_domain->dom_family == AF_INET6) &&
    386   1.10   mycroft 	    so->so_proto->pr_protocol == IPPROTO_TCP) {
    387  1.138    plunky 		val = 1;
    388  1.138    plunky 		so_setsockopt(NULL, so, IPPROTO_TCP, TCP_NODELAY, &val,
    389  1.138    plunky 		    sizeof(val));
    390    1.1       cgd 	}
    391  1.134        ad 	solock(so);
    392    1.1       cgd 	so->so_rcv.sb_flags &= ~SB_NOINTR;
    393    1.1       cgd 	so->so_rcv.sb_timeo = 0;
    394    1.1       cgd 	so->so_snd.sb_flags &= ~SB_NOINTR;
    395    1.1       cgd 	so->so_snd.sb_timeo = 0;
    396  1.134        ad 	sounlock(so);
    397  1.111      yamt 	if (tslp) {
    398   1.10   mycroft 		slp = tslp;
    399  1.111      yamt 	} else {
    400   1.65      yamt 		slp = nfsrv_sockalloc();
    401   1.10   mycroft 	}
    402   1.10   mycroft 	slp->ns_so = so;
    403   1.10   mycroft 	slp->ns_nam = mynam;
    404  1.127      yamt 	mutex_enter(&fp->f_lock);
    405   1.10   mycroft 	fp->f_count++;
    406  1.127      yamt 	mutex_exit(&fp->f_lock);
    407   1.10   mycroft 	slp->ns_fp = fp;
    408  1.127      yamt 	slp->ns_flags = SLP_VALID;
    409  1.127      yamt 	slp->ns_aflags = SLP_A_NEEDQ;
    410  1.127      yamt 	slp->ns_gflags = 0;
    411  1.127      yamt 	slp->ns_sflags = 0;
    412  1.134        ad 	solock(so);
    413  1.107  christos 	so->so_upcallarg = (void *)slp;
    414  1.127      yamt 	so->so_upcall = nfsrv_soupcall;
    415   1.29      matt 	so->so_rcv.sb_flags |= SB_UPCALL;
    416  1.134        ad 	sounlock(so);
    417   1.10   mycroft 	nfsrv_wakenfsd(slp);
    418   1.10   mycroft 	return (0);
    419   1.10   mycroft }
    420    1.1       cgd 
    421   1.10   mycroft /*
    422   1.10   mycroft  * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
    423   1.10   mycroft  * until it is killed by a signal.
    424   1.10   mycroft  */
    425   1.18  christos int
    426   1.53   thorpej nfssvc_nfsd(nsd, argp, l)
    427   1.10   mycroft 	struct nfsd_srvargs *nsd;
    428  1.107  christos 	void *argp;
    429   1.53   thorpej 	struct lwp *l;
    430   1.10   mycroft {
    431   1.95    kardel 	struct timeval tv;
    432   1.38  augustss 	struct mbuf *m;
    433   1.38  augustss 	struct nfssvc_sock *slp;
    434   1.19      fvdl 	struct nfsd *nfsd = nsd->nsd_nfsd;
    435   1.19      fvdl 	struct nfsrv_descript *nd = NULL;
    436   1.19      fvdl 	struct mbuf *mreq;
    437   1.19      fvdl 	u_quad_t cur_usec;
    438  1.111      yamt 	int error = 0, cacherep, siz, sotype, writes_todo;
    439   1.53   thorpej 	struct proc *p = l->l_proc;
    440  1.111      yamt 	int s;
    441  1.127      yamt 	bool doreinit;
    442   1.19      fvdl 
    443   1.19      fvdl #ifndef nolint
    444   1.19      fvdl 	cacherep = RC_DOIT;
    445   1.19      fvdl 	writes_todo = 0;
    446   1.19      fvdl #endif
    447  1.117        ad 	uvm_lwp_hold(l);
    448  1.111      yamt 	if (nfsd == NULL) {
    449  1.130      yamt 		nsd->nsd_nfsd = nfsd = kmem_alloc(sizeof(*nfsd), KM_SLEEP);
    450  1.111      yamt 		memset(nfsd, 0, sizeof (struct nfsd));
    451  1.111      yamt 		cv_init(&nfsd->nfsd_cv, "nfsd");
    452   1.19      fvdl 		nfsd->nfsd_procp = p;
    453  1.111      yamt 		mutex_enter(&nfsd_lock);
    454  1.127      yamt 		while ((nfssvc_sockhead_flag & SLP_INIT) != 0) {
    455  1.127      yamt 			KASSERT(nfs_numnfsd == 0);
    456  1.127      yamt 			cv_wait(&nfsd_initcv, &nfsd_lock);
    457  1.127      yamt 		}
    458   1.19      fvdl 		TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain);
    459   1.10   mycroft 		nfs_numnfsd++;
    460  1.111      yamt 		mutex_exit(&nfsd_lock);
    461   1.10   mycroft 	}
    462    1.1       cgd 	/*
    463   1.10   mycroft 	 * Loop getting rpc requests until SIGKILL.
    464    1.1       cgd 	 */
    465    1.1       cgd 	for (;;) {
    466  1.127      yamt 		bool dummy;
    467  1.127      yamt 
    468  1.113      yamt 		if ((curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
    469  1.113      yamt 		    != 0) {
    470  1.113      yamt 			preempt();
    471  1.113      yamt 		}
    472   1.85      yamt 		if (nfsd->nfsd_slp == NULL) {
    473  1.111      yamt 			mutex_enter(&nfsd_lock);
    474   1.85      yamt 			while (nfsd->nfsd_slp == NULL &&
    475   1.12   mycroft 			    (nfsd_head_flag & NFSD_CHECKSLP) == 0) {
    476   1.66      yamt 				SLIST_INSERT_HEAD(&nfsd_idle_head, nfsd,
    477   1.66      yamt 				    nfsd_idle);
    478  1.111      yamt 				error = cv_wait_sig(&nfsd->nfsd_cv, &nfsd_lock);
    479   1.66      yamt 				if (error) {
    480   1.66      yamt 					slp = nfsd->nfsd_slp;
    481   1.66      yamt 					nfsd->nfsd_slp = NULL;
    482   1.66      yamt 					if (!slp)
    483   1.66      yamt 						SLIST_REMOVE(&nfsd_idle_head,
    484   1.66      yamt 						    nfsd, nfsd, nfsd_idle);
    485  1.111      yamt 					mutex_exit(&nfsd_lock);
    486   1.66      yamt 					if (slp) {
    487   1.66      yamt 						nfsrv_wakenfsd(slp);
    488   1.66      yamt 						nfsrv_slpderef(slp);
    489   1.66      yamt 					}
    490   1.10   mycroft 					goto done;
    491   1.66      yamt 				}
    492   1.10   mycroft 			}
    493   1.85      yamt 			if (nfsd->nfsd_slp == NULL &&
    494   1.12   mycroft 			    (nfsd_head_flag & NFSD_CHECKSLP) != 0) {
    495   1.66      yamt 				slp = TAILQ_FIRST(&nfssvc_sockpending);
    496   1.66      yamt 				if (slp) {
    497  1.127      yamt 					KASSERT((slp->ns_gflags & SLP_G_DOREC)
    498  1.121      yamt 					    != 0);
    499   1.66      yamt 					TAILQ_REMOVE(&nfssvc_sockpending, slp,
    500   1.66      yamt 					    ns_pending);
    501  1.127      yamt 					slp->ns_gflags &= ~SLP_G_DOREC;
    502   1.66      yamt 					slp->ns_sref++;
    503   1.66      yamt 					nfsd->nfsd_slp = slp;
    504   1.66      yamt 				} else
    505   1.12   mycroft 					nfsd_head_flag &= ~NFSD_CHECKSLP;
    506   1.10   mycroft 			}
    507  1.116      yamt 			KASSERT(nfsd->nfsd_slp == NULL ||
    508  1.116      yamt 			    nfsd->nfsd_slp->ns_sref > 0);
    509  1.111      yamt 			mutex_exit(&nfsd_lock);
    510   1.85      yamt 			if ((slp = nfsd->nfsd_slp) == NULL)
    511   1.10   mycroft 				continue;
    512  1.127      yamt 			if (slp->ns_flags & SLP_VALID) {
    513  1.127      yamt 				bool more;
    514  1.127      yamt 
    515  1.127      yamt 				if (nfsdsock_testbits(slp, SLP_A_NEEDQ)) {
    516  1.127      yamt 					nfsrv_rcv(slp);
    517   1.10   mycroft 				}
    518  1.127      yamt 				if (nfsdsock_testbits(slp, SLP_A_DISCONN)) {
    519  1.119      yamt 					nfsrv_zapsock(slp);
    520  1.119      yamt 				}
    521  1.127      yamt 				error = nfsrv_dorec(slp, nfsd, &nd, &more);
    522   1.95    kardel 				getmicrotime(&tv);
    523   1.95    kardel 				cur_usec = (u_quad_t)tv.tv_sec * 1000000 +
    524   1.95    kardel 					(u_quad_t)tv.tv_usec;
    525  1.127      yamt 				writes_todo = 0;
    526  1.127      yamt 				if (error) {
    527  1.127      yamt 					struct nfsrv_descript *nd2;
    528  1.127      yamt 
    529  1.127      yamt 					mutex_enter(&nfsd_lock);
    530  1.127      yamt 					nd2 = LIST_FIRST(&slp->ns_tq);
    531  1.127      yamt 					if (nd2 != NULL &&
    532  1.127      yamt 					    nd2->nd_time <= cur_usec) {
    533  1.127      yamt 						error = 0;
    534  1.127      yamt 						cacherep = RC_DOIT;
    535  1.127      yamt 						writes_todo = 1;
    536  1.127      yamt 					}
    537  1.127      yamt 					mutex_exit(&nfsd_lock);
    538  1.127      yamt 				}
    539  1.127      yamt 				if (error == 0 && more) {
    540   1.85      yamt 					nfsrv_wakenfsd(slp);
    541   1.85      yamt 				}
    542    1.1       cgd 			}
    543   1.10   mycroft 		} else {
    544   1.10   mycroft 			error = 0;
    545   1.19      fvdl 			slp = nfsd->nfsd_slp;
    546   1.10   mycroft 		}
    547  1.116      yamt 		KASSERT(slp != NULL);
    548  1.116      yamt 		KASSERT(nfsd->nfsd_slp == slp);
    549  1.127      yamt 		if (error || (slp->ns_flags & SLP_VALID) == 0) {
    550   1.19      fvdl 			if (nd) {
    551   1.93      yamt 				nfsdreq_free(nd);
    552   1.19      fvdl 				nd = NULL;
    553   1.19      fvdl 			}
    554   1.85      yamt 			nfsd->nfsd_slp = NULL;
    555   1.10   mycroft 			nfsrv_slpderef(slp);
    556    1.1       cgd 			continue;
    557    1.1       cgd 		}
    558   1.86      yamt 		sotype = slp->ns_so->so_type;
    559   1.19      fvdl 		if (nd) {
    560   1.95    kardel 			getmicrotime(&nd->nd_starttime);
    561   1.63      yamt 			if (nd->nd_nam2)
    562   1.63      yamt 				nd->nd_nam = nd->nd_nam2;
    563   1.63      yamt 			else
    564   1.63      yamt 				nd->nd_nam = slp->ns_nam;
    565   1.63      yamt 
    566   1.63      yamt 			/*
    567   1.63      yamt 			 * Check to see if authorization is needed.
    568   1.63      yamt 			 */
    569   1.63      yamt 			if (nfsd->nfsd_flag & NFSD_NEEDAUTH) {
    570   1.63      yamt 				nfsd->nfsd_flag &= ~NFSD_NEEDAUTH;
    571   1.63      yamt 				nsd->nsd_haddr = mtod(nd->nd_nam,
    572   1.63      yamt 				    struct sockaddr_in *)->sin_addr.s_addr;
    573   1.63      yamt 				nsd->nsd_authlen = nfsd->nfsd_authlen;
    574   1.63      yamt 				nsd->nsd_verflen = nfsd->nfsd_verflen;
    575   1.63      yamt 				if (!copyout(nfsd->nfsd_authstr,
    576   1.63      yamt 				    nsd->nsd_authstr, nfsd->nfsd_authlen) &&
    577   1.63      yamt 				    !copyout(nfsd->nfsd_verfstr,
    578   1.63      yamt 				    nsd->nsd_verfstr, nfsd->nfsd_verflen) &&
    579   1.63      yamt 				    !copyout(nsd, argp, sizeof (*nsd))) {
    580  1.117        ad 					uvm_lwp_rele(l);
    581   1.63      yamt 					return (ENEEDAUTH);
    582   1.63      yamt 				}
    583   1.63      yamt 				cacherep = RC_DROPIT;
    584   1.10   mycroft 			} else
    585   1.63      yamt 				cacherep = nfsrv_getcache(nd, slp, &mreq);
    586   1.63      yamt 
    587  1.100      yamt 			if (nfsd->nfsd_flag & NFSD_AUTHFAIL) {
    588   1.63      yamt 				nfsd->nfsd_flag &= ~NFSD_AUTHFAIL;
    589   1.10   mycroft 				nd->nd_procnum = NFSPROC_NOOP;
    590   1.63      yamt 				nd->nd_repstat =
    591   1.63      yamt 				    (NFSERR_AUTHERR | AUTH_TOOWEAK);
    592   1.10   mycroft 				cacherep = RC_DOIT;
    593   1.63      yamt 			}
    594   1.10   mycroft 		}
    595   1.10   mycroft 
    596   1.19      fvdl 		/*
    597   1.19      fvdl 		 * Loop to get all the write rpc relies that have been
    598   1.19      fvdl 		 * gathered together.
    599   1.19      fvdl 		 */
    600   1.19      fvdl 		do {
    601   1.63      yamt 			switch (cacherep) {
    602   1.63      yamt 			case RC_DOIT:
    603   1.80      onoe 				mreq = NULL;
    604   1.88      yamt 				netexport_rdlock();
    605   1.90  christos 				if (writes_todo || nd == NULL ||
    606   1.90  christos 				     (!(nd->nd_flag & ND_NFSV3) &&
    607   1.63      yamt 				     nd->nd_procnum == NFSPROC_WRITE &&
    608  1.100      yamt 				     nfsrvw_procrastinate > 0))
    609   1.63      yamt 					error = nfsrv_writegather(&nd, slp,
    610   1.84  christos 					    l, &mreq);
    611   1.63      yamt 				else
    612   1.63      yamt 					error =
    613   1.63      yamt 					    (*(nfsrv3_procs[nd->nd_procnum]))
    614   1.84  christos 					    (nd, slp, l, &mreq);
    615   1.88      yamt 				netexport_rdunlock();
    616   1.80      onoe 				if (mreq == NULL) {
    617   1.80      onoe 					if (nd != NULL) {
    618   1.80      onoe 						if (nd->nd_nam2)
    619   1.80      onoe 							m_free(nd->nd_nam2);
    620   1.80      onoe 					}
    621   1.63      yamt 					break;
    622   1.80      onoe 				}
    623   1.63      yamt 				if (error) {
    624  1.100      yamt 					nfsstats.srv_errs++;
    625  1.106   thorpej 					nfsrv_updatecache(nd, false, mreq);
    626   1.63      yamt 					if (nd->nd_nam2)
    627   1.63      yamt 						m_freem(nd->nd_nam2);
    628   1.63      yamt 					break;
    629   1.63      yamt 				}
    630   1.63      yamt 				nfsstats.srvrpccnt[nd->nd_procnum]++;
    631  1.106   thorpej 				nfsrv_updatecache(nd, true, mreq);
    632   1.63      yamt 				nd->nd_mrep = (struct mbuf *)0;
    633   1.63      yamt 			case RC_REPLY:
    634   1.63      yamt 				m = mreq;
    635   1.63      yamt 				siz = 0;
    636   1.63      yamt 				while (m) {
    637   1.63      yamt 					siz += m->m_len;
    638   1.63      yamt 					m = m->m_next;
    639   1.63      yamt 				}
    640   1.63      yamt 				if (siz <= 0 || siz > NFS_MAXPACKET) {
    641   1.63      yamt 					printf("mbuf siz=%d\n",siz);
    642   1.63      yamt 					panic("Bad nfs svc reply");
    643   1.63      yamt 				}
    644   1.63      yamt 				m = mreq;
    645   1.63      yamt 				m->m_pkthdr.len = siz;
    646   1.63      yamt 				m->m_pkthdr.rcvif = (struct ifnet *)0;
    647   1.63      yamt 				/*
    648   1.63      yamt 				 * For stream protocols, prepend a Sun RPC
    649   1.63      yamt 				 * Record Mark.
    650   1.63      yamt 				 */
    651   1.63      yamt 				if (sotype == SOCK_STREAM) {
    652   1.63      yamt 					M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
    653   1.63      yamt 					*mtod(m, u_int32_t *) =
    654   1.63      yamt 					    htonl(0x80000000 | siz);
    655   1.63      yamt 				}
    656   1.86      yamt 				nd->nd_mreq = m;
    657   1.86      yamt 				if (nfsrtton) {
    658   1.86      yamt 					nfsd_rt(slp->ns_so->so_type, nd,
    659   1.86      yamt 					    cacherep);
    660   1.63      yamt 				}
    661   1.86      yamt 				error = nfsdsock_sendreply(slp, nd);
    662   1.86      yamt 				nd = NULL;
    663   1.63      yamt 				if (error == EPIPE)
    664   1.63      yamt 					nfsrv_zapsock(slp);
    665   1.63      yamt 				if (error == EINTR || error == ERESTART) {
    666  1.114      yamt 					nfsd->nfsd_slp = NULL;
    667   1.63      yamt 					nfsrv_slpderef(slp);
    668   1.63      yamt 					goto done;
    669   1.63      yamt 				}
    670   1.19      fvdl 				break;
    671   1.63      yamt 			case RC_DROPIT:
    672   1.63      yamt 				if (nfsrtton)
    673   1.63      yamt 					nfsd_rt(sotype, nd, cacherep);
    674   1.63      yamt 				m_freem(nd->nd_mrep);
    675   1.63      yamt 				m_freem(nd->nd_nam2);
    676    1.1       cgd 				break;
    677    1.1       cgd 			}
    678   1.63      yamt 			if (nd) {
    679   1.93      yamt 				nfsdreq_free(nd);
    680   1.63      yamt 				nd = NULL;
    681   1.63      yamt 			}
    682   1.63      yamt 
    683    1.1       cgd 			/*
    684   1.63      yamt 			 * Check to see if there are outstanding writes that
    685   1.63      yamt 			 * need to be serviced.
    686    1.1       cgd 			 */
    687   1.95    kardel 			getmicrotime(&tv);
    688   1.95    kardel 			cur_usec = (u_quad_t)tv.tv_sec * 1000000 +
    689   1.95    kardel 			    (u_quad_t)tv.tv_usec;
    690   1.63      yamt 			s = splsoftclock();
    691   1.63      yamt 			if (LIST_FIRST(&slp->ns_tq) &&
    692   1.63      yamt 			    LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec) {
    693   1.63      yamt 				cacherep = RC_DOIT;
    694   1.63      yamt 				writes_todo = 1;
    695   1.63      yamt 			} else
    696   1.63      yamt 				writes_todo = 0;
    697   1.63      yamt 			splx(s);
    698   1.19      fvdl 		} while (writes_todo);
    699  1.127      yamt 		if (nfsrv_dorec(slp, nfsd, &nd, &dummy)) {
    700   1.19      fvdl 			nfsd->nfsd_slp = NULL;
    701   1.10   mycroft 			nfsrv_slpderef(slp);
    702   1.10   mycroft 		}
    703    1.1       cgd 	}
    704   1.10   mycroft done:
    705  1.111      yamt 	mutex_enter(&nfsd_lock);
    706   1.19      fvdl 	TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
    707  1.127      yamt 	doreinit = --nfs_numnfsd == 0;
    708  1.127      yamt 	if (doreinit)
    709  1.127      yamt 		nfssvc_sockhead_flag |= SLP_INIT;
    710  1.111      yamt 	mutex_exit(&nfsd_lock);
    711  1.111      yamt 	cv_destroy(&nfsd->nfsd_cv);
    712  1.130      yamt 	kmem_free(nfsd, sizeof(*nfsd));
    713  1.111      yamt 	nsd->nsd_nfsd = NULL;
    714  1.127      yamt 	if (doreinit)
    715  1.106   thorpej 		nfsrv_init(true);	/* Reinitialize everything */
    716  1.117        ad 	uvm_lwp_rele(l);
    717    1.1       cgd 	return (error);
    718    1.1       cgd }
    719    1.1       cgd 
    720   1.10   mycroft /*
    721   1.10   mycroft  * Shut down a socket associated with an nfssvc_sock structure.
    722   1.10   mycroft  * Should be called with the send lock set, if required.
    723   1.10   mycroft  * The trick here is to increment the sref at the start, so that the nfsds
    724   1.10   mycroft  * will stop using it and clear ns_flag at the end so that it will not be
    725   1.10   mycroft  * reassigned during cleanup.
    726   1.73      yamt  *
    727   1.73      yamt  * called at splsoftnet.
    728   1.10   mycroft  */
    729   1.18  christos void
    730   1.10   mycroft nfsrv_zapsock(slp)
    731   1.38  augustss 	struct nfssvc_sock *slp;
    732   1.10   mycroft {
    733   1.38  augustss 	struct nfsuid *nuidp, *nnuidp;
    734  1.127      yamt 	struct nfsrv_descript *nwp;
    735   1.10   mycroft 	struct socket *so;
    736  1.104      yamt 	struct mbuf *m;
    737   1.10   mycroft 
    738   1.85      yamt 	if (nfsdsock_drain(slp)) {
    739   1.74      yamt 		return;
    740   1.74      yamt 	}
    741  1.111      yamt 	mutex_enter(&nfsd_lock);
    742  1.127      yamt 	if (slp->ns_gflags & SLP_G_DOREC) {
    743   1.73      yamt 		TAILQ_REMOVE(&nfssvc_sockpending, slp, ns_pending);
    744  1.127      yamt 		slp->ns_gflags &= ~SLP_G_DOREC;
    745   1.73      yamt 	}
    746  1.111      yamt 	mutex_exit(&nfsd_lock);
    747   1.77      yamt 
    748   1.77      yamt 	so = slp->ns_so;
    749   1.77      yamt 	KASSERT(so != NULL);
    750  1.134        ad 	solock(so);
    751   1.77      yamt 	so->so_upcall = NULL;
    752   1.77      yamt 	so->so_upcallarg = NULL;
    753   1.77      yamt 	so->so_rcv.sb_flags &= ~SB_UPCALL;
    754   1.77      yamt 	soshutdown(so, SHUT_RDWR);
    755  1.134        ad 	sounlock(so);
    756   1.77      yamt 
    757   1.77      yamt 	if (slp->ns_nam)
    758   1.77      yamt 		m_free(slp->ns_nam);
    759   1.77      yamt 	m_freem(slp->ns_raw);
    760  1.104      yamt 	m = slp->ns_rec;
    761  1.104      yamt 	while (m != NULL) {
    762  1.104      yamt 		struct mbuf *n;
    763  1.104      yamt 
    764  1.104      yamt 		n = m->m_nextpkt;
    765  1.104      yamt 		m_freem(m);
    766  1.104      yamt 		m = n;
    767  1.104      yamt 	}
    768   1.77      yamt 	for (nuidp = TAILQ_FIRST(&slp->ns_uidlruhead); nuidp != 0;
    769   1.77      yamt 	    nuidp = nnuidp) {
    770   1.77      yamt 		nnuidp = TAILQ_NEXT(nuidp, nu_lru);
    771   1.77      yamt 		LIST_REMOVE(nuidp, nu_hash);
    772   1.77      yamt 		TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru);
    773   1.77      yamt 		if (nuidp->nu_flag & NU_NAM)
    774   1.77      yamt 			m_freem(nuidp->nu_nam);
    775  1.130      yamt 		kmem_free(nuidp, sizeof(*nuidp));
    776   1.77      yamt 	}
    777  1.127      yamt 	mutex_enter(&nfsd_lock);
    778  1.127      yamt 	while ((nwp = LIST_FIRST(&slp->ns_tq)) != NULL) {
    779   1.77      yamt 		LIST_REMOVE(nwp, nd_tq);
    780  1.127      yamt 		mutex_exit(&nfsd_lock);
    781   1.93      yamt 		nfsdreq_free(nwp);
    782  1.127      yamt 		mutex_enter(&nfsd_lock);
    783   1.10   mycroft 	}
    784  1.127      yamt 	mutex_exit(&nfsd_lock);
    785   1.10   mycroft }
    786   1.10   mycroft 
    787   1.10   mycroft /*
    788   1.10   mycroft  * Derefence a server socket structure. If it has no more references and
    789   1.10   mycroft  * is no longer valid, you can throw it away.
    790   1.10   mycroft  */
    791   1.10   mycroft void
    792   1.10   mycroft nfsrv_slpderef(slp)
    793   1.38  augustss 	struct nfssvc_sock *slp;
    794   1.10   mycroft {
    795  1.116      yamt 	uint32_t ref;
    796   1.66      yamt 
    797  1.116      yamt 	mutex_enter(&nfsd_lock);
    798  1.116      yamt 	KASSERT(slp->ns_sref > 0);
    799  1.116      yamt 	ref = --slp->ns_sref;
    800  1.116      yamt 	mutex_exit(&nfsd_lock);
    801  1.127      yamt 	if (ref == 0 && (slp->ns_flags & SLP_VALID) == 0) {
    802  1.132        ad 		file_t *fp;
    803  1.111      yamt 
    804  1.111      yamt 		mutex_enter(&nfsd_lock);
    805  1.127      yamt 		KASSERT((slp->ns_gflags & SLP_G_DOREC) == 0);
    806   1.12   mycroft 		TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
    807  1.111      yamt 		mutex_exit(&nfsd_lock);
    808   1.85      yamt 
    809   1.85      yamt 		fp = slp->ns_fp;
    810   1.85      yamt 		if (fp != NULL) {
    811   1.85      yamt 			slp->ns_fp = NULL;
    812   1.85      yamt 			KASSERT(fp != NULL);
    813   1.85      yamt 			KASSERT(fp->f_data == slp->ns_so);
    814  1.132        ad 			KASSERT(fp->f_count > 0);
    815  1.132        ad 			closef(fp);
    816   1.85      yamt 			slp->ns_so = NULL;
    817   1.85      yamt 		}
    818   1.85      yamt 
    819   1.77      yamt 		nfsrv_sockfree(slp);
    820   1.10   mycroft 	}
    821   1.10   mycroft }
    822   1.10   mycroft 
    823   1.10   mycroft /*
    824   1.10   mycroft  * Initialize the data structures for the server.
    825   1.10   mycroft  * Handshake with any new nfsds starting up to avoid any chance of
    826   1.10   mycroft  * corruption.
    827   1.10   mycroft  */
    828   1.10   mycroft void
    829   1.10   mycroft nfsrv_init(terminating)
    830   1.10   mycroft 	int terminating;
    831   1.10   mycroft {
    832   1.66      yamt 	struct nfssvc_sock *slp;
    833   1.10   mycroft 
    834  1.111      yamt 	if (!terminating) {
    835  1.135      yamt 		mutex_init(&nfsd_lock, MUTEX_DRIVER, IPL_SOFTNET);
    836  1.111      yamt 		cv_init(&nfsd_initcv, "nfsdinit");
    837  1.111      yamt 	}
    838  1.111      yamt 
    839  1.111      yamt 	mutex_enter(&nfsd_lock);
    840  1.127      yamt 	if (!terminating && (nfssvc_sockhead_flag & SLP_INIT) != 0)
    841   1.10   mycroft 		panic("nfsd init");
    842   1.12   mycroft 	nfssvc_sockhead_flag |= SLP_INIT;
    843   1.66      yamt 
    844   1.10   mycroft 	if (terminating) {
    845  1.114      yamt 		KASSERT(SLIST_EMPTY(&nfsd_idle_head));
    846  1.114      yamt 		KASSERT(TAILQ_EMPTY(&nfsd_head));
    847   1.66      yamt 		while ((slp = TAILQ_FIRST(&nfssvc_sockhead)) != NULL) {
    848  1.111      yamt 			mutex_exit(&nfsd_lock);
    849   1.85      yamt 			KASSERT(slp->ns_sref == 0);
    850   1.85      yamt 			slp->ns_sref++;
    851   1.85      yamt 			nfsrv_zapsock(slp);
    852   1.85      yamt 			nfsrv_slpderef(slp);
    853  1.111      yamt 			mutex_enter(&nfsd_lock);
    854   1.10   mycroft 		}
    855  1.114      yamt 		KASSERT(TAILQ_EMPTY(&nfssvc_sockpending));
    856  1.111      yamt 		mutex_exit(&nfsd_lock);
    857   1.10   mycroft 		nfsrv_cleancache();	/* And clear out server cache */
    858   1.66      yamt 	} else {
    859  1.111      yamt 		mutex_exit(&nfsd_lock);
    860   1.26      fvdl 		nfs_pub.np_valid = 0;
    861   1.66      yamt 	}
    862   1.12   mycroft 
    863   1.12   mycroft 	TAILQ_INIT(&nfssvc_sockhead);
    864   1.66      yamt 	TAILQ_INIT(&nfssvc_sockpending);
    865   1.12   mycroft 
    866   1.12   mycroft 	TAILQ_INIT(&nfsd_head);
    867   1.66      yamt 	SLIST_INIT(&nfsd_idle_head);
    868   1.12   mycroft 	nfsd_head_flag &= ~NFSD_CHECKSLP;
    869   1.12   mycroft 
    870   1.65      yamt 	nfs_udpsock = nfsrv_sockalloc();
    871   1.65      yamt 	nfs_udp6sock = nfsrv_sockalloc();
    872   1.66      yamt 
    873  1.111      yamt 	mutex_enter(&nfsd_lock);
    874  1.111      yamt 	nfssvc_sockhead_flag &= ~SLP_INIT;
    875  1.111      yamt 	cv_broadcast(&nfsd_initcv);
    876  1.111      yamt 	mutex_exit(&nfsd_lock);
    877   1.10   mycroft }
    878   1.10   mycroft 
    879  1.142        ad void
    880  1.142        ad nfsrv_fini(void)
    881  1.142        ad {
    882  1.142        ad 
    883  1.142        ad 	nfsrv_init(true);
    884  1.142        ad 	cv_destroy(&nfsd_initcv);
    885  1.142        ad 	mutex_destroy(&nfsd_lock);
    886  1.142        ad }
    887  1.142        ad 
    888   1.10   mycroft /*
    889   1.10   mycroft  * Add entries to the server monitor log.
    890   1.10   mycroft  */
    891   1.10   mycroft static void
    892   1.19      fvdl nfsd_rt(sotype, nd, cacherep)
    893   1.10   mycroft 	int sotype;
    894   1.38  augustss 	struct nfsrv_descript *nd;
    895   1.10   mycroft 	int cacherep;
    896   1.10   mycroft {
    897   1.95    kardel 	struct timeval tv;
    898   1.38  augustss 	struct drt *rt;
    899   1.10   mycroft 
    900   1.10   mycroft 	rt = &nfsdrt.drt[nfsdrt.pos];
    901   1.10   mycroft 	if (cacherep == RC_DOIT)
    902   1.10   mycroft 		rt->flag = 0;
    903   1.10   mycroft 	else if (cacherep == RC_REPLY)
    904   1.10   mycroft 		rt->flag = DRT_CACHEREPLY;
    905   1.10   mycroft 	else
    906   1.10   mycroft 		rt->flag = DRT_CACHEDROP;
    907   1.10   mycroft 	if (sotype == SOCK_STREAM)
    908   1.10   mycroft 		rt->flag |= DRT_TCP;
    909  1.102      yamt 	if (nd->nd_flag & ND_NFSV3)
    910   1.19      fvdl 		rt->flag |= DRT_NFSV3;
    911   1.10   mycroft 	rt->proc = nd->nd_procnum;
    912   1.19      fvdl 	if (mtod(nd->nd_nam, struct sockaddr *)->sa_family == AF_INET)
    913   1.19      fvdl 	    rt->ipadr = mtod(nd->nd_nam, struct sockaddr_in *)->sin_addr.s_addr;
    914   1.10   mycroft 	else
    915   1.19      fvdl 	    rt->ipadr = INADDR_ANY;
    916   1.95    kardel 	getmicrotime(&tv);
    917   1.95    kardel 	rt->resptime = ((tv.tv_sec - nd->nd_starttime.tv_sec) * 1000000) +
    918   1.95    kardel 		(tv.tv_usec - nd->nd_starttime.tv_usec);
    919   1.95    kardel 	rt->tstamp = tv;
    920   1.10   mycroft 	nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ;
    921   1.10   mycroft }
    922