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