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