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