Home | History | Annotate | Line # | Download | only in nfs
nfs_syscalls.c revision 1.134.2.5
      1  1.134.2.5      yamt /*	$NetBSD: nfs_syscalls.c,v 1.134.2.5 2009/07/18 14:53:26 yamt 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.134.2.5      yamt __KERNEL_RCSID(0, "$NetBSD: nfs_syscalls.c,v 1.134.2.5 2009/07/18 14:53:26 yamt 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.134.2.3      yamt extern int32_t (*nfsrv3_procs[NFS_NPROCS])(struct nfsrv_descript *,
     79       1.19      fvdl 						struct nfssvc_sock *,
     80  1.134.2.3      yamt 						struct lwp *, struct mbuf **);
     81       1.19      fvdl extern int nfsrvw_procrastinate;
     82  1.134.2.3      yamt 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.134.2.3      yamt struct nfssvc_sock *nfs_udpsock;
     97  1.134.2.3      yamt struct nfssvc_sock *nfs_udp6sock;
     98       1.19      fvdl 
     99  1.134.2.3      yamt static struct nfssvc_sock *nfsrv_sockalloc(void);
    100  1.134.2.3      yamt static void nfsrv_sockfree(struct nfssvc_sock *);
    101  1.134.2.3      yamt static void nfsd_rt(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.134.2.3      yamt 
    145       1.13       cgd 	if (SCARG(uap, flag) & NFSSVC_BIOD) {
    146  1.134.2.3      yamt 		/* Dummy implementation of nfsios for 1.4 and earlier. */
    147  1.134.2.3      yamt 		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.134.2.3      yamt 		if ((fp = fd_getfile(nfsdarg.sock)) == NULL)
    157  1.134.2.3      yamt 			return (EBADF);
    158  1.134.2.3      yamt 		if (fp->f_type != DTYPE_SOCKET) {
    159  1.134.2.3      yamt 			fd_putfile(nfsdarg.sock);
    160  1.134.2.3      yamt 			return (ENOTSOCK);
    161  1.134.2.3      yamt 		}
    162       1.10   mycroft 		/*
    163       1.10   mycroft 		 * Get the client address for connected sockets.
    164       1.10   mycroft 		 */
    165       1.10   mycroft 		if (nfsdarg.name == NULL || nfsdarg.namelen == 0)
    166       1.10   mycroft 			nam = (struct mbuf *)0;
    167       1.18  christos 		else {
    168       1.18  christos 			error = sockargs(&nam, nfsdarg.name, nfsdarg.namelen,
    169       1.19      fvdl 				MT_SONAME);
    170       1.35   thorpej 			if (error) {
    171      1.132        ad 				fd_putfile(nfsdarg.sock);
    172       1.18  christos 				return (error);
    173       1.35   thorpej 			}
    174       1.18  christos 		}
    175       1.10   mycroft 		error = nfssvc_addsock(fp, nam);
    176      1.132        ad 		fd_putfile(nfsdarg.sock);
    177       1.82      jmmv 	} else if (SCARG(uap, flag) & NFSSVC_SETEXPORTSLIST) {
    178       1.82      jmmv 		struct export_args *args;
    179       1.82      jmmv 		struct mountd_exports_list mel;
    180       1.82      jmmv 
    181       1.82      jmmv 		error = copyin(SCARG(uap, argp), &mel, sizeof(mel));
    182       1.82      jmmv 		if (error != 0)
    183       1.82      jmmv 			return error;
    184       1.82      jmmv 
    185       1.82      jmmv 		args = (struct export_args *)malloc(mel.mel_nexports *
    186       1.82      jmmv 		    sizeof(struct export_args), M_TEMP, M_WAITOK);
    187       1.82      jmmv 		error = copyin(mel.mel_exports, args, mel.mel_nexports *
    188       1.82      jmmv 		    sizeof(struct export_args));
    189       1.82      jmmv 		if (error != 0) {
    190       1.82      jmmv 			free(args, M_TEMP);
    191       1.82      jmmv 			return error;
    192       1.82      jmmv 		}
    193       1.82      jmmv 		mel.mel_exports = args;
    194       1.82      jmmv 
    195  1.134.2.5      yamt 		error = mountd_set_exports_list(&mel, l, NULL);
    196       1.82      jmmv 
    197       1.82      jmmv 		free(args, M_TEMP);
    198       1.10   mycroft 	} else {
    199      1.107  christos 		error = copyin(SCARG(uap, argp), (void *)nsd, sizeof (*nsd));
    200       1.18  christos 		if (error)
    201       1.10   mycroft 			return (error);
    202       1.13       cgd 		if ((SCARG(uap, flag) & NFSSVC_AUTHIN) &&
    203       1.19      fvdl 		    ((nfsd = nsd->nsd_nfsd)) != NULL &&
    204      1.127      yamt 		    (nfsd->nfsd_slp->ns_flags & SLP_VALID)) {
    205       1.19      fvdl 			slp = nfsd->nfsd_slp;
    206       1.10   mycroft 
    207       1.10   mycroft 			/*
    208       1.10   mycroft 			 * First check to see if another nfsd has already
    209       1.10   mycroft 			 * added this credential.
    210       1.10   mycroft 			 */
    211       1.92      elad 			LIST_FOREACH(nuidp, NUIDHASH(slp, nsd->nsd_cr.cr_uid),
    212       1.57      yamt 			    nu_hash) {
    213       1.93      yamt 				if (kauth_cred_geteuid(nuidp->nu_cr) ==
    214       1.93      yamt 				    nsd->nsd_cr.cr_uid &&
    215       1.19      fvdl 				    (!nfsd->nfsd_nd->nd_nam2 ||
    216       1.19      fvdl 				     netaddr_match(NU_NETFAM(nuidp),
    217       1.19      fvdl 				     &nuidp->nu_haddr, nfsd->nfsd_nd->nd_nam2)))
    218       1.10   mycroft 					break;
    219       1.10   mycroft 			}
    220       1.19      fvdl 			if (nuidp) {
    221       1.93      yamt 			    kauth_cred_hold(nuidp->nu_cr);
    222       1.93      yamt 			    nfsd->nfsd_nd->nd_cr = nuidp->nu_cr;
    223       1.19      fvdl 			    nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
    224       1.19      fvdl 			} else {
    225       1.10   mycroft 			    /*
    226       1.10   mycroft 			     * Nope, so we will.
    227       1.10   mycroft 			     */
    228       1.10   mycroft 			    if (slp->ns_numuids < nuidhash_max) {
    229       1.10   mycroft 				slp->ns_numuids++;
    230      1.130      yamt 				nuidp = kmem_alloc(sizeof(*nuidp), KM_SLEEP);
    231       1.10   mycroft 			    } else
    232       1.10   mycroft 				nuidp = (struct nfsuid *)0;
    233      1.127      yamt 			    if ((slp->ns_flags & SLP_VALID) == 0) {
    234       1.10   mycroft 				if (nuidp)
    235      1.130      yamt 				    kmem_free(nuidp, sizeof(*nuidp));
    236       1.10   mycroft 			    } else {
    237       1.10   mycroft 				if (nuidp == (struct nfsuid *)0) {
    238       1.57      yamt 				    nuidp = TAILQ_FIRST(&slp->ns_uidlruhead);
    239       1.12   mycroft 				    LIST_REMOVE(nuidp, nu_hash);
    240       1.12   mycroft 				    TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp,
    241       1.12   mycroft 					nu_lru);
    242       1.19      fvdl 				    if (nuidp->nu_flag & NU_NAM)
    243       1.19      fvdl 					m_freem(nuidp->nu_nam);
    244       1.10   mycroft 			        }
    245       1.19      fvdl 				nuidp->nu_flag = 0;
    246       1.98     pooka 				kauth_uucred_to_cred(nuidp->nu_cr,
    247       1.98     pooka 				    &nsd->nsd_cr);
    248       1.19      fvdl 				nuidp->nu_timestamp = nsd->nsd_timestamp;
    249       1.95    kardel 				nuidp->nu_expire = time_second + nsd->nsd_ttl;
    250       1.19      fvdl 				/*
    251       1.19      fvdl 				 * and save the session key in nu_key.
    252       1.19      fvdl 				 */
    253       1.32     perry 				memcpy(nuidp->nu_key, nsd->nsd_key,
    254       1.32     perry 				    sizeof(nsd->nsd_key));
    255       1.19      fvdl 				if (nfsd->nfsd_nd->nd_nam2) {
    256       1.19      fvdl 				    struct sockaddr_in *saddr;
    257       1.19      fvdl 
    258       1.19      fvdl 				    saddr = mtod(nfsd->nfsd_nd->nd_nam2,
    259       1.19      fvdl 					 struct sockaddr_in *);
    260       1.19      fvdl 				    switch (saddr->sin_family) {
    261       1.19      fvdl 				    case AF_INET:
    262       1.19      fvdl 					nuidp->nu_flag |= NU_INETADDR;
    263       1.19      fvdl 					nuidp->nu_inetaddr =
    264       1.19      fvdl 					     saddr->sin_addr.s_addr;
    265       1.19      fvdl 					break;
    266  1.134.2.3      yamt 				    case AF_INET6:
    267       1.19      fvdl 					nuidp->nu_flag |= NU_NAM;
    268       1.19      fvdl 					nuidp->nu_nam = m_copym(
    269       1.19      fvdl 					    nfsd->nfsd_nd->nd_nam2, 0,
    270       1.19      fvdl 					     M_COPYALL, M_WAIT);
    271       1.19      fvdl 					break;
    272  1.134.2.3      yamt 				    default:
    273  1.134.2.3      yamt 					return EAFNOSUPPORT;
    274       1.19      fvdl 				    };
    275       1.19      fvdl 				}
    276       1.12   mycroft 				TAILQ_INSERT_TAIL(&slp->ns_uidlruhead, nuidp,
    277       1.19      fvdl 					nu_lru);
    278       1.12   mycroft 				LIST_INSERT_HEAD(NUIDHASH(slp, nsd->nsd_uid),
    279       1.19      fvdl 					nuidp, nu_hash);
    280       1.93      yamt 				kauth_cred_hold(nuidp->nu_cr);
    281       1.93      yamt 				nfsd->nfsd_nd->nd_cr = nuidp->nu_cr;
    282       1.19      fvdl 				nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
    283       1.10   mycroft 			    }
    284       1.10   mycroft 			}
    285       1.10   mycroft 		}
    286       1.13       cgd 		if ((SCARG(uap, flag) & NFSSVC_AUTHINFAIL) &&
    287       1.13       cgd 		    (nfsd = nsd->nsd_nfsd))
    288       1.19      fvdl 			nfsd->nfsd_flag |= NFSD_AUTHFAIL;
    289       1.53   thorpej 		error = nfssvc_nfsd(nsd, SCARG(uap, argp), l);
    290       1.10   mycroft 	}
    291       1.10   mycroft 	if (error == EINTR || error == ERESTART)
    292       1.10   mycroft 		error = 0;
    293       1.10   mycroft 	return (error);
    294       1.10   mycroft }
    295       1.10   mycroft 
    296       1.65      yamt static struct nfssvc_sock *
    297  1.134.2.3      yamt nfsrv_sockalloc(void)
    298       1.65      yamt {
    299       1.65      yamt 	struct nfssvc_sock *slp;
    300       1.78     perry 
    301      1.130      yamt 	slp = kmem_alloc(sizeof(*slp), KM_SLEEP);
    302       1.65      yamt 	memset(slp, 0, sizeof (struct nfssvc_sock));
    303  1.134.2.2      yamt 	mutex_init(&slp->ns_lock, MUTEX_DRIVER, IPL_SOFTNET);
    304  1.134.2.2      yamt 	mutex_init(&slp->ns_alock, MUTEX_DRIVER, IPL_SOFTNET);
    305      1.111      yamt 	cv_init(&slp->ns_cv, "nfsdsock");
    306       1.65      yamt 	TAILQ_INIT(&slp->ns_uidlruhead);
    307       1.86      yamt 	LIST_INIT(&slp->ns_tq);
    308       1.86      yamt 	SIMPLEQ_INIT(&slp->ns_sendq);
    309      1.111      yamt 	mutex_enter(&nfsd_lock);
    310       1.65      yamt 	TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
    311      1.111      yamt 	mutex_exit(&nfsd_lock);
    312       1.65      yamt 
    313       1.65      yamt 	return slp;
    314       1.65      yamt }
    315       1.65      yamt 
    316       1.77      yamt static void
    317       1.77      yamt nfsrv_sockfree(struct nfssvc_sock *slp)
    318       1.77      yamt {
    319       1.77      yamt 
    320       1.77      yamt 	KASSERT(slp->ns_so == NULL);
    321       1.77      yamt 	KASSERT(slp->ns_fp == NULL);
    322      1.127      yamt 	KASSERT((slp->ns_flags & SLP_VALID) == 0);
    323      1.111      yamt 	mutex_destroy(&slp->ns_lock);
    324      1.127      yamt 	mutex_destroy(&slp->ns_alock);
    325      1.111      yamt 	cv_destroy(&slp->ns_cv);
    326      1.130      yamt 	kmem_free(slp, sizeof(*slp));
    327       1.77      yamt }
    328       1.77      yamt 
    329       1.10   mycroft /*
    330       1.10   mycroft  * Adds a socket to the list for servicing by nfsds.
    331       1.10   mycroft  */
    332       1.18  christos int
    333  1.134.2.3      yamt nfssvc_addsock(file_t *fp, struct mbuf *mynam)
    334       1.10   mycroft {
    335       1.38  augustss 	int siz;
    336       1.38  augustss 	struct nfssvc_sock *slp;
    337       1.38  augustss 	struct socket *so;
    338       1.10   mycroft 	struct nfssvc_sock *tslp;
    339      1.134        ad 	int error;
    340  1.134.2.3      yamt 	int val;
    341       1.10   mycroft 
    342        1.1       cgd 	so = (struct socket *)fp->f_data;
    343       1.10   mycroft 	tslp = (struct nfssvc_sock *)0;
    344       1.10   mycroft 	/*
    345       1.10   mycroft 	 * Add it to the list, as required.
    346       1.10   mycroft 	 */
    347       1.10   mycroft 	if (so->so_proto->pr_protocol == IPPROTO_UDP) {
    348       1.41      fvdl 		if (so->so_proto->pr_domain->dom_family == AF_INET6)
    349       1.41      fvdl 			tslp = nfs_udp6sock;
    350  1.134.2.3      yamt 		else {
    351  1.134.2.3      yamt 			tslp = nfs_udpsock;
    352  1.134.2.3      yamt 			if (tslp->ns_flags & SLP_VALID) {
    353  1.134.2.3      yamt 				m_freem(mynam);
    354  1.134.2.3      yamt 				return (EPERM);
    355  1.134.2.3      yamt 			}
    356       1.10   mycroft 		}
    357       1.10   mycroft 	}
    358       1.10   mycroft 	if (so->so_type == SOCK_STREAM)
    359       1.19      fvdl 		siz = NFS_MAXPACKET + sizeof (u_long);
    360       1.10   mycroft 	else
    361        1.1       cgd 		siz = NFS_MAXPACKET;
    362      1.134        ad 	solock(so);
    363       1.78     perry 	error = soreserve(so, siz, siz);
    364      1.134        ad 	sounlock(so);
    365       1.19      fvdl 	if (error) {
    366       1.10   mycroft 		m_freem(mynam);
    367       1.10   mycroft 		return (error);
    368       1.10   mycroft 	}
    369        1.1       cgd 
    370        1.1       cgd 	/*
    371        1.1       cgd 	 * Set protocol specific options { for now TCP only } and
    372        1.1       cgd 	 * reserve some space. For datagram sockets, this can get called
    373        1.1       cgd 	 * repeatedly for the same socket, but that isn't harmful.
    374        1.1       cgd 	 */
    375       1.10   mycroft 	if (so->so_type == SOCK_STREAM) {
    376  1.134.2.3      yamt 		val = 1;
    377  1.134.2.3      yamt 		so_setsockopt(NULL, so, SOL_SOCKET, SO_KEEPALIVE, &val,
    378  1.134.2.3      yamt 		    sizeof(val));
    379        1.1       cgd 	}
    380  1.134.2.3      yamt 	if ((so->so_proto->pr_domain->dom_family == AF_INET ||
    381  1.134.2.3      yamt 	    so->so_proto->pr_domain->dom_family == AF_INET6) &&
    382       1.10   mycroft 	    so->so_proto->pr_protocol == IPPROTO_TCP) {
    383  1.134.2.3      yamt 		val = 1;
    384  1.134.2.3      yamt 		so_setsockopt(NULL, so, IPPROTO_TCP, TCP_NODELAY, &val,
    385  1.134.2.3      yamt 		    sizeof(val));
    386        1.1       cgd 	}
    387      1.134        ad 	solock(so);
    388        1.1       cgd 	so->so_rcv.sb_flags &= ~SB_NOINTR;
    389        1.1       cgd 	so->so_rcv.sb_timeo = 0;
    390        1.1       cgd 	so->so_snd.sb_flags &= ~SB_NOINTR;
    391        1.1       cgd 	so->so_snd.sb_timeo = 0;
    392      1.134        ad 	sounlock(so);
    393      1.111      yamt 	if (tslp) {
    394       1.10   mycroft 		slp = tslp;
    395      1.111      yamt 	} else {
    396       1.65      yamt 		slp = nfsrv_sockalloc();
    397       1.10   mycroft 	}
    398       1.10   mycroft 	slp->ns_so = so;
    399       1.10   mycroft 	slp->ns_nam = mynam;
    400      1.127      yamt 	mutex_enter(&fp->f_lock);
    401       1.10   mycroft 	fp->f_count++;
    402      1.127      yamt 	mutex_exit(&fp->f_lock);
    403       1.10   mycroft 	slp->ns_fp = fp;
    404      1.127      yamt 	slp->ns_flags = SLP_VALID;
    405      1.127      yamt 	slp->ns_aflags = SLP_A_NEEDQ;
    406      1.127      yamt 	slp->ns_gflags = 0;
    407      1.127      yamt 	slp->ns_sflags = 0;
    408      1.134        ad 	solock(so);
    409      1.107  christos 	so->so_upcallarg = (void *)slp;
    410      1.127      yamt 	so->so_upcall = nfsrv_soupcall;
    411       1.29      matt 	so->so_rcv.sb_flags |= SB_UPCALL;
    412      1.134        ad 	sounlock(so);
    413       1.10   mycroft 	nfsrv_wakenfsd(slp);
    414       1.10   mycroft 	return (0);
    415       1.10   mycroft }
    416        1.1       cgd 
    417       1.10   mycroft /*
    418       1.10   mycroft  * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
    419       1.10   mycroft  * until it is killed by a signal.
    420       1.10   mycroft  */
    421       1.18  christos int
    422  1.134.2.3      yamt nfssvc_nfsd(struct nfsd_srvargs *nsd, void *argp, struct lwp *l)
    423       1.10   mycroft {
    424       1.95    kardel 	struct timeval tv;
    425       1.38  augustss 	struct mbuf *m;
    426       1.38  augustss 	struct nfssvc_sock *slp;
    427       1.19      fvdl 	struct nfsd *nfsd = nsd->nsd_nfsd;
    428       1.19      fvdl 	struct nfsrv_descript *nd = NULL;
    429       1.19      fvdl 	struct mbuf *mreq;
    430       1.19      fvdl 	u_quad_t cur_usec;
    431      1.111      yamt 	int error = 0, cacherep, siz, sotype, writes_todo;
    432       1.53   thorpej 	struct proc *p = l->l_proc;
    433      1.127      yamt 	bool doreinit;
    434       1.19      fvdl 
    435       1.19      fvdl #ifndef nolint
    436       1.19      fvdl 	cacherep = RC_DOIT;
    437       1.19      fvdl 	writes_todo = 0;
    438       1.19      fvdl #endif
    439      1.117        ad 	uvm_lwp_hold(l);
    440      1.111      yamt 	if (nfsd == NULL) {
    441      1.130      yamt 		nsd->nsd_nfsd = nfsd = kmem_alloc(sizeof(*nfsd), KM_SLEEP);
    442      1.111      yamt 		memset(nfsd, 0, sizeof (struct nfsd));
    443      1.111      yamt 		cv_init(&nfsd->nfsd_cv, "nfsd");
    444       1.19      fvdl 		nfsd->nfsd_procp = p;
    445      1.111      yamt 		mutex_enter(&nfsd_lock);
    446      1.127      yamt 		while ((nfssvc_sockhead_flag & SLP_INIT) != 0) {
    447      1.127      yamt 			KASSERT(nfs_numnfsd == 0);
    448      1.127      yamt 			cv_wait(&nfsd_initcv, &nfsd_lock);
    449      1.127      yamt 		}
    450       1.19      fvdl 		TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain);
    451       1.10   mycroft 		nfs_numnfsd++;
    452      1.111      yamt 		mutex_exit(&nfsd_lock);
    453       1.10   mycroft 	}
    454        1.1       cgd 	/*
    455       1.10   mycroft 	 * Loop getting rpc requests until SIGKILL.
    456        1.1       cgd 	 */
    457        1.1       cgd 	for (;;) {
    458      1.127      yamt 		bool dummy;
    459      1.127      yamt 
    460      1.113      yamt 		if ((curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
    461      1.113      yamt 		    != 0) {
    462      1.113      yamt 			preempt();
    463      1.113      yamt 		}
    464       1.85      yamt 		if (nfsd->nfsd_slp == NULL) {
    465      1.111      yamt 			mutex_enter(&nfsd_lock);
    466       1.85      yamt 			while (nfsd->nfsd_slp == NULL &&
    467       1.12   mycroft 			    (nfsd_head_flag & NFSD_CHECKSLP) == 0) {
    468       1.66      yamt 				SLIST_INSERT_HEAD(&nfsd_idle_head, nfsd,
    469       1.66      yamt 				    nfsd_idle);
    470      1.111      yamt 				error = cv_wait_sig(&nfsd->nfsd_cv, &nfsd_lock);
    471       1.66      yamt 				if (error) {
    472       1.66      yamt 					slp = nfsd->nfsd_slp;
    473       1.66      yamt 					nfsd->nfsd_slp = NULL;
    474       1.66      yamt 					if (!slp)
    475       1.66      yamt 						SLIST_REMOVE(&nfsd_idle_head,
    476       1.66      yamt 						    nfsd, nfsd, nfsd_idle);
    477      1.111      yamt 					mutex_exit(&nfsd_lock);
    478       1.66      yamt 					if (slp) {
    479       1.66      yamt 						nfsrv_wakenfsd(slp);
    480       1.66      yamt 						nfsrv_slpderef(slp);
    481       1.66      yamt 					}
    482       1.10   mycroft 					goto done;
    483       1.66      yamt 				}
    484       1.10   mycroft 			}
    485       1.85      yamt 			if (nfsd->nfsd_slp == NULL &&
    486       1.12   mycroft 			    (nfsd_head_flag & NFSD_CHECKSLP) != 0) {
    487       1.66      yamt 				slp = TAILQ_FIRST(&nfssvc_sockpending);
    488       1.66      yamt 				if (slp) {
    489      1.127      yamt 					KASSERT((slp->ns_gflags & SLP_G_DOREC)
    490      1.121      yamt 					    != 0);
    491       1.66      yamt 					TAILQ_REMOVE(&nfssvc_sockpending, slp,
    492       1.66      yamt 					    ns_pending);
    493      1.127      yamt 					slp->ns_gflags &= ~SLP_G_DOREC;
    494       1.66      yamt 					slp->ns_sref++;
    495       1.66      yamt 					nfsd->nfsd_slp = slp;
    496       1.66      yamt 				} else
    497       1.12   mycroft 					nfsd_head_flag &= ~NFSD_CHECKSLP;
    498       1.10   mycroft 			}
    499      1.116      yamt 			KASSERT(nfsd->nfsd_slp == NULL ||
    500      1.116      yamt 			    nfsd->nfsd_slp->ns_sref > 0);
    501      1.111      yamt 			mutex_exit(&nfsd_lock);
    502       1.85      yamt 			if ((slp = nfsd->nfsd_slp) == NULL)
    503       1.10   mycroft 				continue;
    504      1.127      yamt 			if (slp->ns_flags & SLP_VALID) {
    505      1.127      yamt 				bool more;
    506      1.127      yamt 
    507      1.127      yamt 				if (nfsdsock_testbits(slp, SLP_A_NEEDQ)) {
    508      1.127      yamt 					nfsrv_rcv(slp);
    509       1.10   mycroft 				}
    510      1.127      yamt 				if (nfsdsock_testbits(slp, SLP_A_DISCONN)) {
    511      1.119      yamt 					nfsrv_zapsock(slp);
    512      1.119      yamt 				}
    513      1.127      yamt 				error = nfsrv_dorec(slp, nfsd, &nd, &more);
    514       1.95    kardel 				getmicrotime(&tv);
    515       1.95    kardel 				cur_usec = (u_quad_t)tv.tv_sec * 1000000 +
    516       1.95    kardel 					(u_quad_t)tv.tv_usec;
    517      1.127      yamt 				writes_todo = 0;
    518      1.127      yamt 				if (error) {
    519      1.127      yamt 					struct nfsrv_descript *nd2;
    520      1.127      yamt 
    521      1.127      yamt 					mutex_enter(&nfsd_lock);
    522      1.127      yamt 					nd2 = LIST_FIRST(&slp->ns_tq);
    523      1.127      yamt 					if (nd2 != NULL &&
    524      1.127      yamt 					    nd2->nd_time <= cur_usec) {
    525      1.127      yamt 						error = 0;
    526      1.127      yamt 						cacherep = RC_DOIT;
    527      1.127      yamt 						writes_todo = 1;
    528      1.127      yamt 					}
    529      1.127      yamt 					mutex_exit(&nfsd_lock);
    530      1.127      yamt 				}
    531      1.127      yamt 				if (error == 0 && more) {
    532       1.85      yamt 					nfsrv_wakenfsd(slp);
    533       1.85      yamt 				}
    534        1.1       cgd 			}
    535       1.10   mycroft 		} else {
    536       1.10   mycroft 			error = 0;
    537       1.19      fvdl 			slp = nfsd->nfsd_slp;
    538       1.10   mycroft 		}
    539      1.116      yamt 		KASSERT(slp != NULL);
    540      1.116      yamt 		KASSERT(nfsd->nfsd_slp == slp);
    541      1.127      yamt 		if (error || (slp->ns_flags & SLP_VALID) == 0) {
    542       1.19      fvdl 			if (nd) {
    543       1.93      yamt 				nfsdreq_free(nd);
    544       1.19      fvdl 				nd = NULL;
    545       1.19      fvdl 			}
    546       1.85      yamt 			nfsd->nfsd_slp = NULL;
    547       1.10   mycroft 			nfsrv_slpderef(slp);
    548        1.1       cgd 			continue;
    549        1.1       cgd 		}
    550       1.86      yamt 		sotype = slp->ns_so->so_type;
    551       1.19      fvdl 		if (nd) {
    552       1.95    kardel 			getmicrotime(&nd->nd_starttime);
    553       1.63      yamt 			if (nd->nd_nam2)
    554       1.63      yamt 				nd->nd_nam = nd->nd_nam2;
    555       1.63      yamt 			else
    556       1.63      yamt 				nd->nd_nam = slp->ns_nam;
    557       1.63      yamt 
    558       1.63      yamt 			/*
    559       1.63      yamt 			 * Check to see if authorization is needed.
    560       1.63      yamt 			 */
    561       1.63      yamt 			if (nfsd->nfsd_flag & NFSD_NEEDAUTH) {
    562       1.63      yamt 				nfsd->nfsd_flag &= ~NFSD_NEEDAUTH;
    563       1.63      yamt 				nsd->nsd_haddr = mtod(nd->nd_nam,
    564       1.63      yamt 				    struct sockaddr_in *)->sin_addr.s_addr;
    565       1.63      yamt 				nsd->nsd_authlen = nfsd->nfsd_authlen;
    566       1.63      yamt 				nsd->nsd_verflen = nfsd->nfsd_verflen;
    567       1.63      yamt 				if (!copyout(nfsd->nfsd_authstr,
    568       1.63      yamt 				    nsd->nsd_authstr, nfsd->nfsd_authlen) &&
    569       1.63      yamt 				    !copyout(nfsd->nfsd_verfstr,
    570       1.63      yamt 				    nsd->nsd_verfstr, nfsd->nfsd_verflen) &&
    571       1.63      yamt 				    !copyout(nsd, argp, sizeof (*nsd))) {
    572      1.117        ad 					uvm_lwp_rele(l);
    573       1.63      yamt 					return (ENEEDAUTH);
    574       1.63      yamt 				}
    575       1.63      yamt 				cacherep = RC_DROPIT;
    576       1.10   mycroft 			} else
    577       1.63      yamt 				cacherep = nfsrv_getcache(nd, slp, &mreq);
    578       1.63      yamt 
    579      1.100      yamt 			if (nfsd->nfsd_flag & NFSD_AUTHFAIL) {
    580       1.63      yamt 				nfsd->nfsd_flag &= ~NFSD_AUTHFAIL;
    581       1.10   mycroft 				nd->nd_procnum = NFSPROC_NOOP;
    582       1.63      yamt 				nd->nd_repstat =
    583       1.63      yamt 				    (NFSERR_AUTHERR | AUTH_TOOWEAK);
    584       1.10   mycroft 				cacherep = RC_DOIT;
    585       1.63      yamt 			}
    586       1.10   mycroft 		}
    587       1.10   mycroft 
    588       1.19      fvdl 		/*
    589       1.19      fvdl 		 * Loop to get all the write rpc relies that have been
    590       1.19      fvdl 		 * gathered together.
    591       1.19      fvdl 		 */
    592       1.19      fvdl 		do {
    593       1.63      yamt 			switch (cacherep) {
    594       1.63      yamt 			case RC_DOIT:
    595       1.80      onoe 				mreq = NULL;
    596       1.88      yamt 				netexport_rdlock();
    597       1.90  christos 				if (writes_todo || nd == NULL ||
    598       1.90  christos 				     (!(nd->nd_flag & ND_NFSV3) &&
    599       1.63      yamt 				     nd->nd_procnum == NFSPROC_WRITE &&
    600      1.100      yamt 				     nfsrvw_procrastinate > 0))
    601       1.63      yamt 					error = nfsrv_writegather(&nd, slp,
    602       1.84  christos 					    l, &mreq);
    603       1.63      yamt 				else
    604       1.63      yamt 					error =
    605       1.63      yamt 					    (*(nfsrv3_procs[nd->nd_procnum]))
    606       1.84  christos 					    (nd, slp, l, &mreq);
    607       1.88      yamt 				netexport_rdunlock();
    608       1.80      onoe 				if (mreq == NULL) {
    609       1.80      onoe 					if (nd != NULL) {
    610       1.80      onoe 						if (nd->nd_nam2)
    611       1.80      onoe 							m_free(nd->nd_nam2);
    612       1.80      onoe 					}
    613       1.63      yamt 					break;
    614       1.80      onoe 				}
    615       1.63      yamt 				if (error) {
    616      1.100      yamt 					nfsstats.srv_errs++;
    617      1.106   thorpej 					nfsrv_updatecache(nd, false, mreq);
    618       1.63      yamt 					if (nd->nd_nam2)
    619       1.63      yamt 						m_freem(nd->nd_nam2);
    620       1.63      yamt 					break;
    621       1.63      yamt 				}
    622       1.63      yamt 				nfsstats.srvrpccnt[nd->nd_procnum]++;
    623      1.106   thorpej 				nfsrv_updatecache(nd, true, mreq);
    624       1.63      yamt 				nd->nd_mrep = (struct mbuf *)0;
    625       1.63      yamt 			case RC_REPLY:
    626       1.63      yamt 				m = mreq;
    627       1.63      yamt 				siz = 0;
    628       1.63      yamt 				while (m) {
    629       1.63      yamt 					siz += m->m_len;
    630       1.63      yamt 					m = m->m_next;
    631       1.63      yamt 				}
    632       1.63      yamt 				if (siz <= 0 || siz > NFS_MAXPACKET) {
    633       1.63      yamt 					printf("mbuf siz=%d\n",siz);
    634       1.63      yamt 					panic("Bad nfs svc reply");
    635       1.63      yamt 				}
    636       1.63      yamt 				m = mreq;
    637       1.63      yamt 				m->m_pkthdr.len = siz;
    638       1.63      yamt 				m->m_pkthdr.rcvif = (struct ifnet *)0;
    639       1.63      yamt 				/*
    640       1.63      yamt 				 * For stream protocols, prepend a Sun RPC
    641       1.63      yamt 				 * Record Mark.
    642       1.63      yamt 				 */
    643       1.63      yamt 				if (sotype == SOCK_STREAM) {
    644       1.63      yamt 					M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
    645       1.63      yamt 					*mtod(m, u_int32_t *) =
    646       1.63      yamt 					    htonl(0x80000000 | siz);
    647       1.63      yamt 				}
    648       1.86      yamt 				nd->nd_mreq = m;
    649       1.86      yamt 				if (nfsrtton) {
    650       1.86      yamt 					nfsd_rt(slp->ns_so->so_type, nd,
    651       1.86      yamt 					    cacherep);
    652       1.63      yamt 				}
    653       1.86      yamt 				error = nfsdsock_sendreply(slp, nd);
    654       1.86      yamt 				nd = NULL;
    655       1.63      yamt 				if (error == EPIPE)
    656       1.63      yamt 					nfsrv_zapsock(slp);
    657       1.63      yamt 				if (error == EINTR || error == ERESTART) {
    658      1.114      yamt 					nfsd->nfsd_slp = NULL;
    659       1.63      yamt 					nfsrv_slpderef(slp);
    660       1.63      yamt 					goto done;
    661       1.63      yamt 				}
    662       1.19      fvdl 				break;
    663       1.63      yamt 			case RC_DROPIT:
    664       1.63      yamt 				if (nfsrtton)
    665       1.63      yamt 					nfsd_rt(sotype, nd, cacherep);
    666       1.63      yamt 				m_freem(nd->nd_mrep);
    667       1.63      yamt 				m_freem(nd->nd_nam2);
    668        1.1       cgd 				break;
    669        1.1       cgd 			}
    670       1.63      yamt 			if (nd) {
    671       1.93      yamt 				nfsdreq_free(nd);
    672       1.63      yamt 				nd = NULL;
    673       1.63      yamt 			}
    674       1.63      yamt 
    675        1.1       cgd 			/*
    676       1.63      yamt 			 * Check to see if there are outstanding writes that
    677       1.63      yamt 			 * need to be serviced.
    678        1.1       cgd 			 */
    679       1.95    kardel 			getmicrotime(&tv);
    680       1.95    kardel 			cur_usec = (u_quad_t)tv.tv_sec * 1000000 +
    681       1.95    kardel 			    (u_quad_t)tv.tv_usec;
    682  1.134.2.3      yamt 			mutex_enter(&nfsd_lock);
    683       1.63      yamt 			if (LIST_FIRST(&slp->ns_tq) &&
    684       1.63      yamt 			    LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec) {
    685       1.63      yamt 				cacherep = RC_DOIT;
    686       1.63      yamt 				writes_todo = 1;
    687       1.63      yamt 			} else
    688       1.63      yamt 				writes_todo = 0;
    689  1.134.2.3      yamt 			mutex_exit(&nfsd_lock);
    690       1.19      fvdl 		} while (writes_todo);
    691      1.127      yamt 		if (nfsrv_dorec(slp, nfsd, &nd, &dummy)) {
    692       1.19      fvdl 			nfsd->nfsd_slp = NULL;
    693       1.10   mycroft 			nfsrv_slpderef(slp);
    694       1.10   mycroft 		}
    695        1.1       cgd 	}
    696       1.10   mycroft done:
    697      1.111      yamt 	mutex_enter(&nfsd_lock);
    698       1.19      fvdl 	TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
    699      1.127      yamt 	doreinit = --nfs_numnfsd == 0;
    700      1.127      yamt 	if (doreinit)
    701      1.127      yamt 		nfssvc_sockhead_flag |= SLP_INIT;
    702      1.111      yamt 	mutex_exit(&nfsd_lock);
    703      1.111      yamt 	cv_destroy(&nfsd->nfsd_cv);
    704      1.130      yamt 	kmem_free(nfsd, sizeof(*nfsd));
    705      1.111      yamt 	nsd->nsd_nfsd = NULL;
    706      1.127      yamt 	if (doreinit)
    707      1.106   thorpej 		nfsrv_init(true);	/* Reinitialize everything */
    708      1.117        ad 	uvm_lwp_rele(l);
    709        1.1       cgd 	return (error);
    710        1.1       cgd }
    711        1.1       cgd 
    712       1.10   mycroft /*
    713       1.10   mycroft  * Shut down a socket associated with an nfssvc_sock structure.
    714       1.10   mycroft  * Should be called with the send lock set, if required.
    715       1.10   mycroft  * The trick here is to increment the sref at the start, so that the nfsds
    716       1.10   mycroft  * will stop using it and clear ns_flag at the end so that it will not be
    717       1.10   mycroft  * reassigned during cleanup.
    718       1.73      yamt  *
    719       1.73      yamt  * called at splsoftnet.
    720       1.10   mycroft  */
    721       1.18  christos void
    722  1.134.2.3      yamt nfsrv_zapsock(struct nfssvc_sock *slp)
    723       1.10   mycroft {
    724       1.38  augustss 	struct nfsuid *nuidp, *nnuidp;
    725      1.127      yamt 	struct nfsrv_descript *nwp;
    726       1.10   mycroft 	struct socket *so;
    727      1.104      yamt 	struct mbuf *m;
    728       1.10   mycroft 
    729       1.85      yamt 	if (nfsdsock_drain(slp)) {
    730       1.74      yamt 		return;
    731       1.74      yamt 	}
    732      1.111      yamt 	mutex_enter(&nfsd_lock);
    733      1.127      yamt 	if (slp->ns_gflags & SLP_G_DOREC) {
    734       1.73      yamt 		TAILQ_REMOVE(&nfssvc_sockpending, slp, ns_pending);
    735      1.127      yamt 		slp->ns_gflags &= ~SLP_G_DOREC;
    736       1.73      yamt 	}
    737      1.111      yamt 	mutex_exit(&nfsd_lock);
    738       1.77      yamt 
    739       1.77      yamt 	so = slp->ns_so;
    740       1.77      yamt 	KASSERT(so != NULL);
    741      1.134        ad 	solock(so);
    742       1.77      yamt 	so->so_upcall = NULL;
    743       1.77      yamt 	so->so_upcallarg = NULL;
    744       1.77      yamt 	so->so_rcv.sb_flags &= ~SB_UPCALL;
    745       1.77      yamt 	soshutdown(so, SHUT_RDWR);
    746      1.134        ad 	sounlock(so);
    747       1.77      yamt 
    748       1.77      yamt 	m_freem(slp->ns_raw);
    749      1.104      yamt 	m = slp->ns_rec;
    750      1.104      yamt 	while (m != NULL) {
    751      1.104      yamt 		struct mbuf *n;
    752      1.104      yamt 
    753      1.104      yamt 		n = m->m_nextpkt;
    754      1.104      yamt 		m_freem(m);
    755      1.104      yamt 		m = n;
    756      1.104      yamt 	}
    757  1.134.2.3      yamt 	/* XXX what about freeing ns_frag ? */
    758       1.77      yamt 	for (nuidp = TAILQ_FIRST(&slp->ns_uidlruhead); nuidp != 0;
    759       1.77      yamt 	    nuidp = nnuidp) {
    760       1.77      yamt 		nnuidp = TAILQ_NEXT(nuidp, nu_lru);
    761       1.77      yamt 		LIST_REMOVE(nuidp, nu_hash);
    762       1.77      yamt 		TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru);
    763       1.77      yamt 		if (nuidp->nu_flag & NU_NAM)
    764       1.77      yamt 			m_freem(nuidp->nu_nam);
    765      1.130      yamt 		kmem_free(nuidp, sizeof(*nuidp));
    766       1.77      yamt 	}
    767      1.127      yamt 	mutex_enter(&nfsd_lock);
    768      1.127      yamt 	while ((nwp = LIST_FIRST(&slp->ns_tq)) != NULL) {
    769       1.77      yamt 		LIST_REMOVE(nwp, nd_tq);
    770      1.127      yamt 		mutex_exit(&nfsd_lock);
    771       1.93      yamt 		nfsdreq_free(nwp);
    772      1.127      yamt 		mutex_enter(&nfsd_lock);
    773       1.10   mycroft 	}
    774      1.127      yamt 	mutex_exit(&nfsd_lock);
    775       1.10   mycroft }
    776       1.10   mycroft 
    777       1.10   mycroft /*
    778       1.10   mycroft  * Derefence a server socket structure. If it has no more references and
    779       1.10   mycroft  * is no longer valid, you can throw it away.
    780       1.10   mycroft  */
    781       1.10   mycroft void
    782  1.134.2.3      yamt nfsrv_slpderef(struct nfssvc_sock *slp)
    783       1.10   mycroft {
    784      1.116      yamt 	uint32_t ref;
    785       1.66      yamt 
    786      1.116      yamt 	mutex_enter(&nfsd_lock);
    787      1.116      yamt 	KASSERT(slp->ns_sref > 0);
    788      1.116      yamt 	ref = --slp->ns_sref;
    789      1.127      yamt 	if (ref == 0 && (slp->ns_flags & SLP_VALID) == 0) {
    790      1.132        ad 		file_t *fp;
    791      1.111      yamt 
    792      1.127      yamt 		KASSERT((slp->ns_gflags & SLP_G_DOREC) == 0);
    793       1.12   mycroft 		TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
    794      1.111      yamt 		mutex_exit(&nfsd_lock);
    795       1.85      yamt 
    796       1.85      yamt 		fp = slp->ns_fp;
    797       1.85      yamt 		if (fp != NULL) {
    798       1.85      yamt 			slp->ns_fp = NULL;
    799       1.85      yamt 			KASSERT(fp != NULL);
    800       1.85      yamt 			KASSERT(fp->f_data == slp->ns_so);
    801      1.132        ad 			KASSERT(fp->f_count > 0);
    802      1.132        ad 			closef(fp);
    803       1.85      yamt 			slp->ns_so = NULL;
    804       1.85      yamt 		}
    805  1.134.2.3      yamt 
    806  1.134.2.3      yamt 		if (slp->ns_nam)
    807  1.134.2.3      yamt 			m_free(slp->ns_nam);
    808       1.77      yamt 		nfsrv_sockfree(slp);
    809  1.134.2.3      yamt 	} else
    810  1.134.2.3      yamt 		mutex_exit(&nfsd_lock);
    811       1.10   mycroft }
    812       1.10   mycroft 
    813       1.10   mycroft /*
    814       1.10   mycroft  * Initialize the data structures for the server.
    815       1.10   mycroft  * Handshake with any new nfsds starting up to avoid any chance of
    816       1.10   mycroft  * corruption.
    817       1.10   mycroft  */
    818       1.10   mycroft void
    819  1.134.2.3      yamt nfsrv_init(int terminating)
    820       1.10   mycroft {
    821       1.66      yamt 	struct nfssvc_sock *slp;
    822       1.10   mycroft 
    823      1.111      yamt 	if (!terminating) {
    824  1.134.2.2      yamt 		mutex_init(&nfsd_lock, MUTEX_DRIVER, IPL_SOFTNET);
    825      1.111      yamt 		cv_init(&nfsd_initcv, "nfsdinit");
    826      1.111      yamt 	}
    827      1.111      yamt 
    828      1.111      yamt 	mutex_enter(&nfsd_lock);
    829      1.127      yamt 	if (!terminating && (nfssvc_sockhead_flag & SLP_INIT) != 0)
    830       1.10   mycroft 		panic("nfsd init");
    831       1.12   mycroft 	nfssvc_sockhead_flag |= SLP_INIT;
    832       1.66      yamt 
    833       1.10   mycroft 	if (terminating) {
    834      1.114      yamt 		KASSERT(SLIST_EMPTY(&nfsd_idle_head));
    835      1.114      yamt 		KASSERT(TAILQ_EMPTY(&nfsd_head));
    836       1.66      yamt 		while ((slp = TAILQ_FIRST(&nfssvc_sockhead)) != NULL) {
    837      1.111      yamt 			mutex_exit(&nfsd_lock);
    838       1.85      yamt 			KASSERT(slp->ns_sref == 0);
    839       1.85      yamt 			slp->ns_sref++;
    840       1.85      yamt 			nfsrv_zapsock(slp);
    841       1.85      yamt 			nfsrv_slpderef(slp);
    842      1.111      yamt 			mutex_enter(&nfsd_lock);
    843       1.10   mycroft 		}
    844      1.114      yamt 		KASSERT(TAILQ_EMPTY(&nfssvc_sockpending));
    845      1.111      yamt 		mutex_exit(&nfsd_lock);
    846       1.10   mycroft 		nfsrv_cleancache();	/* And clear out server cache */
    847       1.66      yamt 	} else {
    848      1.111      yamt 		mutex_exit(&nfsd_lock);
    849       1.26      fvdl 		nfs_pub.np_valid = 0;
    850       1.66      yamt 	}
    851       1.12   mycroft 
    852       1.12   mycroft 	TAILQ_INIT(&nfssvc_sockhead);
    853       1.66      yamt 	TAILQ_INIT(&nfssvc_sockpending);
    854       1.12   mycroft 
    855       1.12   mycroft 	TAILQ_INIT(&nfsd_head);
    856       1.66      yamt 	SLIST_INIT(&nfsd_idle_head);
    857       1.12   mycroft 	nfsd_head_flag &= ~NFSD_CHECKSLP;
    858       1.12   mycroft 
    859       1.65      yamt 	nfs_udpsock = nfsrv_sockalloc();
    860       1.65      yamt 	nfs_udp6sock = nfsrv_sockalloc();
    861       1.66      yamt 
    862      1.111      yamt 	mutex_enter(&nfsd_lock);
    863      1.111      yamt 	nfssvc_sockhead_flag &= ~SLP_INIT;
    864      1.111      yamt 	cv_broadcast(&nfsd_initcv);
    865      1.111      yamt 	mutex_exit(&nfsd_lock);
    866       1.10   mycroft }
    867       1.10   mycroft 
    868  1.134.2.3      yamt void
    869  1.134.2.3      yamt nfsrv_fini(void)
    870  1.134.2.3      yamt {
    871  1.134.2.3      yamt 
    872  1.134.2.3      yamt 	nfsrv_init(true);
    873  1.134.2.3      yamt 	cv_destroy(&nfsd_initcv);
    874  1.134.2.3      yamt 	mutex_destroy(&nfsd_lock);
    875  1.134.2.3      yamt }
    876  1.134.2.3      yamt 
    877       1.10   mycroft /*
    878       1.10   mycroft  * Add entries to the server monitor log.
    879       1.10   mycroft  */
    880       1.10   mycroft static void
    881  1.134.2.3      yamt nfsd_rt(int sotype, struct nfsrv_descript *nd, int cacherep)
    882       1.10   mycroft {
    883       1.95    kardel 	struct timeval tv;
    884       1.38  augustss 	struct drt *rt;
    885       1.10   mycroft 
    886       1.10   mycroft 	rt = &nfsdrt.drt[nfsdrt.pos];
    887       1.10   mycroft 	if (cacherep == RC_DOIT)
    888       1.10   mycroft 		rt->flag = 0;
    889       1.10   mycroft 	else if (cacherep == RC_REPLY)
    890       1.10   mycroft 		rt->flag = DRT_CACHEREPLY;
    891       1.10   mycroft 	else
    892       1.10   mycroft 		rt->flag = DRT_CACHEDROP;
    893       1.10   mycroft 	if (sotype == SOCK_STREAM)
    894       1.10   mycroft 		rt->flag |= DRT_TCP;
    895      1.102      yamt 	if (nd->nd_flag & ND_NFSV3)
    896       1.19      fvdl 		rt->flag |= DRT_NFSV3;
    897       1.10   mycroft 	rt->proc = nd->nd_procnum;
    898       1.19      fvdl 	if (mtod(nd->nd_nam, struct sockaddr *)->sa_family == AF_INET)
    899       1.19      fvdl 	    rt->ipadr = mtod(nd->nd_nam, struct sockaddr_in *)->sin_addr.s_addr;
    900       1.10   mycroft 	else
    901       1.19      fvdl 	    rt->ipadr = INADDR_ANY;
    902       1.95    kardel 	getmicrotime(&tv);
    903       1.95    kardel 	rt->resptime = ((tv.tv_sec - nd->nd_starttime.tv_sec) * 1000000) +
    904       1.95    kardel 		(tv.tv_usec - nd->nd_starttime.tv_usec);
    905       1.95    kardel 	rt->tstamp = tv;
    906       1.10   mycroft 	nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ;
    907       1.10   mycroft }
    908