Home | History | Annotate | Line # | Download | only in nfs
nfs_syscalls.c revision 1.99
      1  1.99      yamt /*	$NetBSD: nfs_syscalls.c,v 1.99 2006/11/09 09:53:57 yamt Exp $	*/
      2  1.11       cgd 
      3   1.1       cgd /*
      4  1.10   mycroft  * Copyright (c) 1989, 1993
      5  1.10   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * This code is derived from software contributed to Berkeley by
      8   1.1       cgd  * Rick Macklem at The University of Guelph.
      9   1.1       cgd  *
     10   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1       cgd  * modification, are permitted provided that the following conditions
     12   1.1       cgd  * are met:
     13   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18  1.70       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1       cgd  *    may be used to endorse or promote products derived from this software
     20   1.1       cgd  *    without specific prior written permission.
     21   1.1       cgd  *
     22   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1       cgd  * SUCH DAMAGE.
     33   1.1       cgd  *
     34  1.19      fvdl  *	@(#)nfs_syscalls.c	8.5 (Berkeley) 3/30/95
     35   1.1       cgd  */
     36  1.49     lukem 
     37  1.49     lukem #include <sys/cdefs.h>
     38  1.99      yamt __KERNEL_RCSID(0, "$NetBSD: nfs_syscalls.c,v 1.99 2006/11/09 09:53:57 yamt Exp $");
     39  1.28   thorpej 
     40  1.28   thorpej #include "fs_nfs.h"
     41  1.43     bjh21 #include "opt_nfs.h"
     42  1.30   thorpej #include "opt_nfsserver.h"
     43  1.31  jonathan #include "opt_iso.h"
     44  1.41      fvdl #include "opt_inet.h"
     45  1.39    tsarna #include "opt_compat_netbsd.h"
     46   1.1       cgd 
     47   1.7   mycroft #include <sys/param.h>
     48   1.7   mycroft #include <sys/systm.h>
     49   1.7   mycroft #include <sys/kernel.h>
     50   1.7   mycroft #include <sys/file.h>
     51   1.7   mycroft #include <sys/stat.h>
     52   1.7   mycroft #include <sys/vnode.h>
     53   1.7   mycroft #include <sys/mount.h>
     54   1.7   mycroft #include <sys/proc.h>
     55  1.10   mycroft #include <sys/uio.h>
     56   1.7   mycroft #include <sys/malloc.h>
     57   1.7   mycroft #include <sys/buf.h>
     58   1.7   mycroft #include <sys/mbuf.h>
     59   1.7   mycroft #include <sys/socket.h>
     60   1.7   mycroft #include <sys/socketvar.h>
     61  1.39    tsarna #include <sys/signalvar.h>
     62   1.7   mycroft #include <sys/domain.h>
     63   1.7   mycroft #include <sys/protosw.h>
     64  1.10   mycroft #include <sys/namei.h>
     65  1.10   mycroft #include <sys/syslog.h>
     66  1.18  christos #include <sys/filedesc.h>
     67  1.39    tsarna #include <sys/kthread.h>
     68  1.92      elad #include <sys/kauth.h>
     69   1.1       cgd 
     70  1.53   thorpej #include <sys/sa.h>
     71  1.13       cgd #include <sys/syscallargs.h>
     72  1.13       cgd 
     73   1.7   mycroft #include <netinet/in.h>
     74   1.7   mycroft #include <netinet/tcp.h>
     75  1.10   mycroft #ifdef ISO
     76  1.10   mycroft #include <netiso/iso.h>
     77  1.10   mycroft #endif
     78  1.19      fvdl #include <nfs/xdr_subs.h>
     79  1.10   mycroft #include <nfs/rpcv2.h>
     80  1.19      fvdl #include <nfs/nfsproto.h>
     81   1.7   mycroft #include <nfs/nfs.h>
     82  1.19      fvdl #include <nfs/nfsm_subs.h>
     83   1.7   mycroft #include <nfs/nfsrvcache.h>
     84  1.10   mycroft #include <nfs/nfsmount.h>
     85  1.10   mycroft #include <nfs/nfsnode.h>
     86  1.10   mycroft #include <nfs/nqnfs.h>
     87  1.10   mycroft #include <nfs/nfsrtt.h>
     88  1.18  christos #include <nfs/nfs_var.h>
     89   1.1       cgd 
     90   1.1       cgd /* Global defs. */
     91  1.19      fvdl extern int32_t (*nfsrv3_procs[NFS_NPROCS]) __P((struct nfsrv_descript *,
     92  1.19      fvdl 						struct nfssvc_sock *,
     93  1.84  christos 						struct lwp *, struct mbuf **));
     94  1.10   mycroft extern time_t nqnfsstarttime;
     95  1.19      fvdl extern int nfsrvw_procrastinate;
     96  1.51      matt 
     97  1.47       chs struct nfssvc_sock *nfs_udpsock;
     98  1.47       chs #ifdef ISO
     99  1.47       chs struct nfssvc_sock *nfs_cltpsock;
    100  1.47       chs #endif
    101  1.41      fvdl #ifdef INET6
    102  1.41      fvdl struct nfssvc_sock *nfs_udp6sock;
    103  1.41      fvdl #endif
    104  1.10   mycroft int nuidhash_max = NFS_MAXUIDHASH;
    105  1.18  christos int nfsd_waiting = 0;
    106  1.18  christos #ifdef NFSSERVER
    107  1.10   mycroft static int nfs_numnfsd = 0;
    108  1.10   mycroft static int notstarted = 1;
    109  1.10   mycroft static int modify_flag = 0;
    110  1.10   mycroft static struct nfsdrt nfsdrt;
    111  1.18  christos #endif
    112  1.51      matt 
    113  1.66      yamt #ifdef NFSSERVER
    114  1.66      yamt struct simplelock nfsd_slock = SIMPLELOCK_INITIALIZER;
    115  1.51      matt struct nfssvc_sockhead nfssvc_sockhead;
    116  1.66      yamt struct nfssvc_sockhead nfssvc_sockpending;
    117  1.51      matt struct nfsdhead nfsd_head;
    118  1.66      yamt struct nfsdidlehead nfsd_idle_head;
    119  1.51      matt 
    120  1.51      matt int nfssvc_sockhead_flag;
    121  1.51      matt int nfsd_head_flag;
    122  1.66      yamt #endif
    123   1.1       cgd 
    124  1.54   thorpej MALLOC_DEFINE(M_NFSUID, "NFS uid", "Nfs uid mapping structure");
    125   1.1       cgd 
    126  1.24   thorpej #ifdef NFS
    127  1.58      yamt struct nfs_iod nfs_asyncdaemon[NFS_MAXASYNCDAEMON];
    128  1.40    tsarna int nfs_niothreads = -1; /* == "0, and has never been set" */
    129  1.23   thorpej #endif
    130  1.19      fvdl 
    131  1.18  christos #ifdef NFSSERVER
    132  1.65      yamt static struct nfssvc_sock *nfsrv_sockalloc __P((void));
    133  1.77      yamt static void nfsrv_sockfree __P((struct nfssvc_sock *));
    134  1.86      yamt static void nfsd_rt __P((int, struct nfsrv_descript *, int));
    135  1.18  christos #endif
    136  1.19      fvdl 
    137   1.1       cgd /*
    138   1.1       cgd  * NFS server system calls
    139   1.1       cgd  */
    140   1.1       cgd 
    141   1.1       cgd 
    142   1.1       cgd /*
    143  1.19      fvdl  * Nfs server pseudo system call for the nfsd's
    144  1.10   mycroft  * Based on the flag value it either:
    145  1.10   mycroft  * - adds a socket to the selection list
    146  1.10   mycroft  * - remains in the kernel as an nfsd
    147  1.10   mycroft  * - remains in the kernel as an nfsiod
    148   1.1       cgd  */
    149  1.18  christos int
    150  1.99      yamt sys_nfssvc(struct lwp *l, void *v, register_t *retval)
    151  1.15   thorpej {
    152  1.38  augustss 	struct sys_nfssvc_args /* {
    153  1.13       cgd 		syscallarg(int) flag;
    154  1.13       cgd 		syscallarg(caddr_t) argp;
    155  1.15   thorpej 	} */ *uap = v;
    156  1.23   thorpej 	int error;
    157  1.24   thorpej #ifdef NFS
    158  1.10   mycroft 	struct nameidata nd;
    159  1.18  christos 	struct nfsmount *nmp;
    160  1.19      fvdl 	struct nfsd_cargs ncd;
    161  1.19      fvdl #endif
    162  1.18  christos #ifdef NFSSERVER
    163  1.67      yamt 	int s;
    164   1.1       cgd 	struct file *fp;
    165  1.10   mycroft 	struct mbuf *nam;
    166  1.10   mycroft 	struct nfsd_args nfsdarg;
    167  1.10   mycroft 	struct nfsd_srvargs nfsd_srvargs, *nsd = &nfsd_srvargs;
    168  1.10   mycroft 	struct nfsd *nfsd;
    169  1.10   mycroft 	struct nfssvc_sock *slp;
    170  1.18  christos 	struct nfsuid *nuidp;
    171  1.18  christos #endif
    172   1.1       cgd 
    173   1.1       cgd 	/*
    174   1.1       cgd 	 * Must be super user
    175   1.1       cgd 	 */
    176  1.96        ad 	error = kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
    177  1.96        ad 	    &l->l_acflag);
    178  1.71   thorpej 	if (error)
    179   1.1       cgd 		return (error);
    180  1.83   thorpej 
    181  1.83   thorpej 	/* Initialize NFS server / client shared data. */
    182  1.83   thorpej 	nfs_init();
    183  1.83   thorpej 
    184  1.67      yamt #ifdef NFSSERVER
    185  1.66      yamt 	s = splsoftnet();
    186  1.66      yamt 	simple_lock(&nfsd_slock);
    187  1.12   mycroft 	while (nfssvc_sockhead_flag & SLP_INIT) {
    188  1.71   thorpej 		nfssvc_sockhead_flag |= SLP_WANTINIT;
    189  1.66      yamt 		(void) ltsleep(&nfssvc_sockhead, PSOCK, "nfsd init", 0,
    190  1.66      yamt 		    &nfsd_slock);
    191  1.10   mycroft 	}
    192  1.66      yamt 	simple_unlock(&nfsd_slock);
    193  1.66      yamt 	splx(s);
    194  1.67      yamt #endif
    195  1.13       cgd 	if (SCARG(uap, flag) & NFSSVC_BIOD) {
    196  1.39    tsarna #if defined(NFS) && defined(COMPAT_14)
    197  1.53   thorpej 		error = nfssvc_iod(l);
    198  1.19      fvdl #else
    199  1.10   mycroft 		error = ENOSYS;
    200  1.19      fvdl #endif
    201  1.13       cgd 	} else if (SCARG(uap, flag) & NFSSVC_MNTD) {
    202  1.24   thorpej #ifndef NFS
    203  1.10   mycroft 		error = ENOSYS;
    204  1.19      fvdl #else
    205  1.18  christos 		error = copyin(SCARG(uap, argp), (caddr_t)&ncd, sizeof (ncd));
    206  1.18  christos 		if (error)
    207  1.10   mycroft 			return (error);
    208  1.10   mycroft 		NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
    209  1.84  christos 			ncd.ncd_dirp, l);
    210  1.19      fvdl 		error = namei(&nd);
    211  1.19      fvdl 		if (error)
    212  1.10   mycroft 			return (error);
    213  1.10   mycroft 		if ((nd.ni_vp->v_flag & VROOT) == 0)
    214  1.10   mycroft 			error = EINVAL;
    215  1.10   mycroft 		nmp = VFSTONFS(nd.ni_vp->v_mount);
    216  1.10   mycroft 		vput(nd.ni_vp);
    217  1.10   mycroft 		if (error)
    218  1.10   mycroft 			return (error);
    219  1.27      fvdl 		if ((nmp->nm_iflag & NFSMNT_MNTD) &&
    220  1.19      fvdl 			(SCARG(uap, flag) & NFSSVC_GOTAUTH) == 0)
    221  1.10   mycroft 			return (0);
    222  1.27      fvdl 		nmp->nm_iflag |= NFSMNT_MNTD;
    223  1.96        ad 		error = nqnfs_clientd(nmp, l->l_cred, &ncd, SCARG(uap, flag),
    224  1.53   thorpej 			SCARG(uap, argp), l);
    225  1.24   thorpej #endif /* NFS */
    226  1.13       cgd 	} else if (SCARG(uap, flag) & NFSSVC_ADDSOCK) {
    227  1.10   mycroft #ifndef NFSSERVER
    228  1.10   mycroft 		error = ENOSYS;
    229  1.19      fvdl #else
    230  1.18  christos 		error = copyin(SCARG(uap, argp), (caddr_t)&nfsdarg,
    231  1.19      fvdl 		    sizeof(nfsdarg));
    232  1.18  christos 		if (error)
    233  1.10   mycroft 			return (error);
    234  1.35   thorpej 		/* getsock() will use the descriptor for us */
    235  1.96        ad 		error = getsock(l->l_proc->p_fd, nfsdarg.sock, &fp);
    236  1.19      fvdl 		if (error)
    237  1.10   mycroft 			return (error);
    238  1.10   mycroft 		/*
    239  1.10   mycroft 		 * Get the client address for connected sockets.
    240  1.10   mycroft 		 */
    241  1.10   mycroft 		if (nfsdarg.name == NULL || nfsdarg.namelen == 0)
    242  1.10   mycroft 			nam = (struct mbuf *)0;
    243  1.18  christos 		else {
    244  1.18  christos 			error = sockargs(&nam, nfsdarg.name, nfsdarg.namelen,
    245  1.19      fvdl 				MT_SONAME);
    246  1.35   thorpej 			if (error) {
    247  1.35   thorpej 				FILE_UNUSE(fp, NULL);
    248  1.18  christos 				return (error);
    249  1.35   thorpej 			}
    250  1.18  christos 		}
    251  1.10   mycroft 		error = nfssvc_addsock(fp, nam);
    252  1.35   thorpej 		FILE_UNUSE(fp, NULL);
    253  1.10   mycroft #endif /* !NFSSERVER */
    254  1.82      jmmv 	} else if (SCARG(uap, flag) & NFSSVC_SETEXPORTSLIST) {
    255  1.82      jmmv #ifndef NFSSERVER
    256  1.82      jmmv 		error = ENOSYS;
    257  1.82      jmmv #else
    258  1.82      jmmv 		struct export_args *args;
    259  1.82      jmmv 		struct mountd_exports_list mel;
    260  1.82      jmmv 
    261  1.82      jmmv 		error = copyin(SCARG(uap, argp), &mel, sizeof(mel));
    262  1.82      jmmv 		if (error != 0)
    263  1.82      jmmv 			return error;
    264  1.82      jmmv 
    265  1.82      jmmv 		args = (struct export_args *)malloc(mel.mel_nexports *
    266  1.82      jmmv 		    sizeof(struct export_args), M_TEMP, M_WAITOK);
    267  1.82      jmmv 		error = copyin(mel.mel_exports, args, mel.mel_nexports *
    268  1.82      jmmv 		    sizeof(struct export_args));
    269  1.82      jmmv 		if (error != 0) {
    270  1.82      jmmv 			free(args, M_TEMP);
    271  1.82      jmmv 			return error;
    272  1.82      jmmv 		}
    273  1.82      jmmv 		mel.mel_exports = args;
    274  1.82      jmmv 
    275  1.84  christos 		error = mountd_set_exports_list(&mel, l);
    276  1.82      jmmv 
    277  1.82      jmmv 		free(args, M_TEMP);
    278  1.82      jmmv #endif /* !NFSSERVER */
    279  1.10   mycroft 	} else {
    280  1.10   mycroft #ifndef NFSSERVER
    281  1.10   mycroft 		error = ENOSYS;
    282  1.19      fvdl #else
    283  1.18  christos 		error = copyin(SCARG(uap, argp), (caddr_t)nsd, sizeof (*nsd));
    284  1.18  christos 		if (error)
    285  1.10   mycroft 			return (error);
    286  1.13       cgd 		if ((SCARG(uap, flag) & NFSSVC_AUTHIN) &&
    287  1.19      fvdl 		    ((nfsd = nsd->nsd_nfsd)) != NULL &&
    288  1.19      fvdl 		    (nfsd->nfsd_slp->ns_flag & SLP_VALID)) {
    289  1.19      fvdl 			slp = nfsd->nfsd_slp;
    290  1.10   mycroft 
    291  1.10   mycroft 			/*
    292  1.10   mycroft 			 * First check to see if another nfsd has already
    293  1.10   mycroft 			 * added this credential.
    294  1.10   mycroft 			 */
    295  1.92      elad 			LIST_FOREACH(nuidp, NUIDHASH(slp, nsd->nsd_cr.cr_uid),
    296  1.57      yamt 			    nu_hash) {
    297  1.93      yamt 				if (kauth_cred_geteuid(nuidp->nu_cr) ==
    298  1.93      yamt 				    nsd->nsd_cr.cr_uid &&
    299  1.19      fvdl 				    (!nfsd->nfsd_nd->nd_nam2 ||
    300  1.19      fvdl 				     netaddr_match(NU_NETFAM(nuidp),
    301  1.19      fvdl 				     &nuidp->nu_haddr, nfsd->nfsd_nd->nd_nam2)))
    302  1.10   mycroft 					break;
    303  1.10   mycroft 			}
    304  1.19      fvdl 			if (nuidp) {
    305  1.93      yamt 			    kauth_cred_hold(nuidp->nu_cr);
    306  1.93      yamt 			    nfsd->nfsd_nd->nd_cr = nuidp->nu_cr;
    307  1.19      fvdl 			    nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
    308  1.19      fvdl 			} else {
    309  1.10   mycroft 			    /*
    310  1.10   mycroft 			     * Nope, so we will.
    311  1.10   mycroft 			     */
    312  1.10   mycroft 			    if (slp->ns_numuids < nuidhash_max) {
    313  1.10   mycroft 				slp->ns_numuids++;
    314  1.10   mycroft 				nuidp = (struct nfsuid *)
    315  1.10   mycroft 				   malloc(sizeof (struct nfsuid), M_NFSUID,
    316  1.10   mycroft 					M_WAITOK);
    317  1.10   mycroft 			    } else
    318  1.10   mycroft 				nuidp = (struct nfsuid *)0;
    319  1.10   mycroft 			    if ((slp->ns_flag & SLP_VALID) == 0) {
    320  1.10   mycroft 				if (nuidp)
    321  1.10   mycroft 				    free((caddr_t)nuidp, M_NFSUID);
    322  1.10   mycroft 			    } else {
    323  1.10   mycroft 				if (nuidp == (struct nfsuid *)0) {
    324  1.57      yamt 				    nuidp = TAILQ_FIRST(&slp->ns_uidlruhead);
    325  1.12   mycroft 				    LIST_REMOVE(nuidp, nu_hash);
    326  1.12   mycroft 				    TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp,
    327  1.12   mycroft 					nu_lru);
    328  1.19      fvdl 				    if (nuidp->nu_flag & NU_NAM)
    329  1.19      fvdl 					m_freem(nuidp->nu_nam);
    330  1.10   mycroft 			        }
    331  1.19      fvdl 				nuidp->nu_flag = 0;
    332  1.98     pooka 				kauth_uucred_to_cred(nuidp->nu_cr,
    333  1.98     pooka 				    &nsd->nsd_cr);
    334  1.19      fvdl 				nuidp->nu_timestamp = nsd->nsd_timestamp;
    335  1.95    kardel 				nuidp->nu_expire = time_second + nsd->nsd_ttl;
    336  1.19      fvdl 				/*
    337  1.19      fvdl 				 * and save the session key in nu_key.
    338  1.19      fvdl 				 */
    339  1.32     perry 				memcpy(nuidp->nu_key, nsd->nsd_key,
    340  1.32     perry 				    sizeof(nsd->nsd_key));
    341  1.19      fvdl 				if (nfsd->nfsd_nd->nd_nam2) {
    342  1.19      fvdl 				    struct sockaddr_in *saddr;
    343  1.19      fvdl 
    344  1.19      fvdl 				    saddr = mtod(nfsd->nfsd_nd->nd_nam2,
    345  1.19      fvdl 					 struct sockaddr_in *);
    346  1.19      fvdl 				    switch (saddr->sin_family) {
    347  1.19      fvdl 				    case AF_INET:
    348  1.19      fvdl 					nuidp->nu_flag |= NU_INETADDR;
    349  1.19      fvdl 					nuidp->nu_inetaddr =
    350  1.19      fvdl 					     saddr->sin_addr.s_addr;
    351  1.19      fvdl 					break;
    352  1.19      fvdl 				    case AF_ISO:
    353  1.19      fvdl 				    default:
    354  1.19      fvdl 					nuidp->nu_flag |= NU_NAM;
    355  1.19      fvdl 					nuidp->nu_nam = m_copym(
    356  1.19      fvdl 					    nfsd->nfsd_nd->nd_nam2, 0,
    357  1.19      fvdl 					     M_COPYALL, M_WAIT);
    358  1.19      fvdl 					break;
    359  1.19      fvdl 				    };
    360  1.19      fvdl 				}
    361  1.12   mycroft 				TAILQ_INSERT_TAIL(&slp->ns_uidlruhead, nuidp,
    362  1.19      fvdl 					nu_lru);
    363  1.12   mycroft 				LIST_INSERT_HEAD(NUIDHASH(slp, nsd->nsd_uid),
    364  1.19      fvdl 					nuidp, nu_hash);
    365  1.93      yamt 				kauth_cred_hold(nuidp->nu_cr);
    366  1.93      yamt 				nfsd->nfsd_nd->nd_cr = nuidp->nu_cr;
    367  1.19      fvdl 				nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
    368  1.10   mycroft 			    }
    369  1.10   mycroft 			}
    370  1.10   mycroft 		}
    371  1.13       cgd 		if ((SCARG(uap, flag) & NFSSVC_AUTHINFAIL) &&
    372  1.13       cgd 		    (nfsd = nsd->nsd_nfsd))
    373  1.19      fvdl 			nfsd->nfsd_flag |= NFSD_AUTHFAIL;
    374  1.53   thorpej 		error = nfssvc_nfsd(nsd, SCARG(uap, argp), l);
    375  1.10   mycroft #endif /* !NFSSERVER */
    376  1.10   mycroft 	}
    377  1.10   mycroft 	if (error == EINTR || error == ERESTART)
    378  1.10   mycroft 		error = 0;
    379  1.10   mycroft 	return (error);
    380  1.10   mycroft }
    381  1.10   mycroft 
    382  1.10   mycroft #ifdef NFSSERVER
    383  1.54   thorpej MALLOC_DEFINE(M_NFSD, "NFS daemon", "Nfs server daemon structure");
    384  1.54   thorpej MALLOC_DEFINE(M_NFSSVC, "NFS srvsock", "Nfs server structure");
    385  1.54   thorpej 
    386  1.65      yamt static struct nfssvc_sock *
    387  1.65      yamt nfsrv_sockalloc()
    388  1.65      yamt {
    389  1.65      yamt 	struct nfssvc_sock *slp;
    390  1.66      yamt 	int s;
    391  1.78     perry 
    392  1.65      yamt 	slp = (struct nfssvc_sock *)
    393  1.65      yamt 	    malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
    394  1.65      yamt 	memset(slp, 0, sizeof (struct nfssvc_sock));
    395  1.85      yamt 	simple_lock_init(&slp->ns_lock);
    396  1.65      yamt 	TAILQ_INIT(&slp->ns_uidlruhead);
    397  1.86      yamt 	LIST_INIT(&slp->ns_tq);
    398  1.86      yamt 	SIMPLEQ_INIT(&slp->ns_sendq);
    399  1.66      yamt 	s = splsoftnet();
    400  1.66      yamt 	simple_lock(&nfsd_slock);
    401  1.65      yamt 	TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
    402  1.66      yamt 	simple_unlock(&nfsd_slock);
    403  1.66      yamt 	splx(s);
    404  1.65      yamt 
    405  1.65      yamt 	return slp;
    406  1.65      yamt }
    407  1.65      yamt 
    408  1.77      yamt static void
    409  1.77      yamt nfsrv_sockfree(struct nfssvc_sock *slp)
    410  1.77      yamt {
    411  1.77      yamt 
    412  1.77      yamt 	KASSERT(slp->ns_so == NULL);
    413  1.77      yamt 	KASSERT(slp->ns_fp == NULL);
    414  1.85      yamt 	KASSERT((slp->ns_flag & SLP_VALID) == 0);
    415  1.77      yamt 	free(slp, M_NFSSVC);
    416  1.77      yamt }
    417  1.77      yamt 
    418  1.10   mycroft /*
    419  1.10   mycroft  * Adds a socket to the list for servicing by nfsds.
    420  1.10   mycroft  */
    421  1.18  christos int
    422  1.10   mycroft nfssvc_addsock(fp, mynam)
    423  1.10   mycroft 	struct file *fp;
    424  1.10   mycroft 	struct mbuf *mynam;
    425  1.10   mycroft {
    426  1.38  augustss 	struct mbuf *m;
    427  1.38  augustss 	int siz;
    428  1.38  augustss 	struct nfssvc_sock *slp;
    429  1.38  augustss 	struct socket *so;
    430  1.10   mycroft 	struct nfssvc_sock *tslp;
    431  1.10   mycroft 	int error, s;
    432  1.10   mycroft 
    433   1.1       cgd 	so = (struct socket *)fp->f_data;
    434  1.10   mycroft 	tslp = (struct nfssvc_sock *)0;
    435  1.10   mycroft 	/*
    436  1.10   mycroft 	 * Add it to the list, as required.
    437  1.10   mycroft 	 */
    438  1.10   mycroft 	if (so->so_proto->pr_protocol == IPPROTO_UDP) {
    439  1.41      fvdl #ifdef INET6
    440  1.41      fvdl 		if (so->so_proto->pr_domain->dom_family == AF_INET6)
    441  1.41      fvdl 			tslp = nfs_udp6sock;
    442  1.41      fvdl 		else
    443  1.41      fvdl #endif
    444  1.10   mycroft 		tslp = nfs_udpsock;
    445  1.10   mycroft 		if (tslp->ns_flag & SLP_VALID) {
    446  1.10   mycroft 			m_freem(mynam);
    447  1.10   mycroft 			return (EPERM);
    448  1.10   mycroft 		}
    449  1.10   mycroft #ifdef ISO
    450  1.10   mycroft 	} else if (so->so_proto->pr_protocol == ISOPROTO_CLTP) {
    451  1.10   mycroft 		tslp = nfs_cltpsock;
    452  1.10   mycroft 		if (tslp->ns_flag & SLP_VALID) {
    453  1.10   mycroft 			m_freem(mynam);
    454  1.10   mycroft 			return (EPERM);
    455  1.10   mycroft 		}
    456  1.10   mycroft #endif /* ISO */
    457  1.10   mycroft 	}
    458  1.10   mycroft 	if (so->so_type == SOCK_STREAM)
    459  1.19      fvdl 		siz = NFS_MAXPACKET + sizeof (u_long);
    460  1.10   mycroft 	else
    461   1.1       cgd 		siz = NFS_MAXPACKET;
    462  1.78     perry 	error = soreserve(so, siz, siz);
    463  1.19      fvdl 	if (error) {
    464  1.10   mycroft 		m_freem(mynam);
    465  1.10   mycroft 		return (error);
    466  1.10   mycroft 	}
    467   1.1       cgd 
    468   1.1       cgd 	/*
    469   1.1       cgd 	 * Set protocol specific options { for now TCP only } and
    470   1.1       cgd 	 * reserve some space. For datagram sockets, this can get called
    471   1.1       cgd 	 * repeatedly for the same socket, but that isn't harmful.
    472   1.1       cgd 	 */
    473  1.10   mycroft 	if (so->so_type == SOCK_STREAM) {
    474  1.55      matt 		m = m_get(M_WAIT, MT_SOOPTS);
    475  1.55      matt 		MCLAIM(m, &nfs_mowner);
    476  1.19      fvdl 		*mtod(m, int32_t *) = 1;
    477  1.19      fvdl 		m->m_len = sizeof(int32_t);
    478   1.1       cgd 		sosetopt(so, SOL_SOCKET, SO_KEEPALIVE, m);
    479   1.1       cgd 	}
    480  1.41      fvdl 	if ((so->so_proto->pr_domain->dom_family == AF_INET
    481  1.41      fvdl #ifdef INET6
    482  1.41      fvdl 	    || so->so_proto->pr_domain->dom_family == AF_INET6
    483  1.41      fvdl #endif
    484  1.41      fvdl 	    ) &&
    485  1.10   mycroft 	    so->so_proto->pr_protocol == IPPROTO_TCP) {
    486  1.55      matt 		m = m_get(M_WAIT, MT_SOOPTS);
    487  1.55      matt 		MCLAIM(m, &nfs_mowner);
    488  1.19      fvdl 		*mtod(m, int32_t *) = 1;
    489  1.19      fvdl 		m->m_len = sizeof(int32_t);
    490   1.1       cgd 		sosetopt(so, IPPROTO_TCP, TCP_NODELAY, m);
    491   1.1       cgd 	}
    492   1.1       cgd 	so->so_rcv.sb_flags &= ~SB_NOINTR;
    493   1.1       cgd 	so->so_rcv.sb_timeo = 0;
    494   1.1       cgd 	so->so_snd.sb_flags &= ~SB_NOINTR;
    495   1.1       cgd 	so->so_snd.sb_timeo = 0;
    496  1.10   mycroft 	if (tslp)
    497  1.10   mycroft 		slp = tslp;
    498  1.10   mycroft 	else {
    499  1.65      yamt 		slp = nfsrv_sockalloc();
    500  1.10   mycroft 	}
    501  1.10   mycroft 	slp->ns_so = so;
    502  1.10   mycroft 	slp->ns_nam = mynam;
    503  1.10   mycroft 	fp->f_count++;
    504  1.10   mycroft 	slp->ns_fp = fp;
    505  1.14   mycroft 	s = splsoftnet();
    506  1.10   mycroft 	so->so_upcallarg = (caddr_t)slp;
    507  1.10   mycroft 	so->so_upcall = nfsrv_rcv;
    508  1.29      matt 	so->so_rcv.sb_flags |= SB_UPCALL;
    509  1.10   mycroft 	slp->ns_flag = (SLP_VALID | SLP_NEEDQ);
    510  1.10   mycroft 	nfsrv_wakenfsd(slp);
    511  1.10   mycroft 	splx(s);
    512  1.10   mycroft 	return (0);
    513  1.10   mycroft }
    514   1.1       cgd 
    515  1.10   mycroft /*
    516  1.10   mycroft  * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
    517  1.10   mycroft  * until it is killed by a signal.
    518  1.10   mycroft  */
    519  1.18  christos int
    520  1.53   thorpej nfssvc_nfsd(nsd, argp, l)
    521  1.10   mycroft 	struct nfsd_srvargs *nsd;
    522  1.10   mycroft 	caddr_t argp;
    523  1.53   thorpej 	struct lwp *l;
    524  1.10   mycroft {
    525  1.95    kardel 	struct timeval tv;
    526  1.38  augustss 	struct mbuf *m;
    527  1.38  augustss 	struct nfssvc_sock *slp;
    528  1.19      fvdl 	struct nfsd *nfsd = nsd->nsd_nfsd;
    529  1.19      fvdl 	struct nfsrv_descript *nd = NULL;
    530  1.19      fvdl 	struct mbuf *mreq;
    531  1.19      fvdl 	u_quad_t cur_usec;
    532  1.95    kardel 	int error = 0, cacherep, s, siz, sotype, writes_todo;
    533  1.53   thorpej 	struct proc *p = l->l_proc;
    534  1.19      fvdl 
    535  1.19      fvdl #ifndef nolint
    536  1.19      fvdl 	cacherep = RC_DOIT;
    537  1.19      fvdl 	writes_todo = 0;
    538  1.19      fvdl #endif
    539  1.14   mycroft 	s = splsoftnet();
    540  1.19      fvdl 	if (nfsd == (struct nfsd *)0) {
    541  1.19      fvdl 		nsd->nsd_nfsd = nfsd = (struct nfsd *)
    542  1.10   mycroft 			malloc(sizeof (struct nfsd), M_NFSD, M_WAITOK);
    543  1.32     perry 		memset((caddr_t)nfsd, 0, sizeof (struct nfsd));
    544  1.19      fvdl 		nfsd->nfsd_procp = p;
    545  1.66      yamt 		simple_lock(&nfsd_slock);
    546  1.19      fvdl 		TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain);
    547  1.10   mycroft 		nfs_numnfsd++;
    548  1.66      yamt 		simple_unlock(&nfsd_slock);
    549  1.10   mycroft 	}
    550  1.53   thorpej 	PHOLD(l);
    551   1.1       cgd 	/*
    552  1.10   mycroft 	 * Loop getting rpc requests until SIGKILL.
    553   1.1       cgd 	 */
    554   1.1       cgd 	for (;;) {
    555  1.85      yamt 		if (nfsd->nfsd_slp == NULL) {
    556  1.66      yamt 			simple_lock(&nfsd_slock);
    557  1.85      yamt 			while (nfsd->nfsd_slp == NULL &&
    558  1.12   mycroft 			    (nfsd_head_flag & NFSD_CHECKSLP) == 0) {
    559  1.66      yamt 				SLIST_INSERT_HEAD(&nfsd_idle_head, nfsd,
    560  1.66      yamt 				    nfsd_idle);
    561  1.10   mycroft 				nfsd_waiting++;
    562  1.66      yamt 				error = ltsleep(nfsd, PSOCK | PCATCH, "nfsd",
    563  1.66      yamt 				    0, &nfsd_slock);
    564  1.10   mycroft 				nfsd_waiting--;
    565  1.66      yamt 				if (error) {
    566  1.66      yamt 					slp = nfsd->nfsd_slp;
    567  1.66      yamt 					nfsd->nfsd_slp = NULL;
    568  1.66      yamt 					if (!slp)
    569  1.66      yamt 						SLIST_REMOVE(&nfsd_idle_head,
    570  1.66      yamt 						    nfsd, nfsd, nfsd_idle);
    571  1.66      yamt 					simple_unlock(&nfsd_slock);
    572  1.66      yamt 					if (slp) {
    573  1.66      yamt 						nfsrv_wakenfsd(slp);
    574  1.66      yamt 						nfsrv_slpderef(slp);
    575  1.66      yamt 					}
    576  1.10   mycroft 					goto done;
    577  1.66      yamt 				}
    578  1.10   mycroft 			}
    579  1.85      yamt 			if (nfsd->nfsd_slp == NULL &&
    580  1.12   mycroft 			    (nfsd_head_flag & NFSD_CHECKSLP) != 0) {
    581  1.66      yamt 				slp = TAILQ_FIRST(&nfssvc_sockpending);
    582  1.66      yamt 				if (slp) {
    583  1.66      yamt 					KASSERT((slp->ns_flag &
    584  1.63      yamt 					    (SLP_VALID | SLP_DOREC))
    585  1.66      yamt 					    == (SLP_VALID | SLP_DOREC));
    586  1.66      yamt 					TAILQ_REMOVE(&nfssvc_sockpending, slp,
    587  1.66      yamt 					    ns_pending);
    588  1.66      yamt 					slp->ns_flag &= ~SLP_DOREC;
    589  1.66      yamt 					slp->ns_sref++;
    590  1.66      yamt 					nfsd->nfsd_slp = slp;
    591  1.66      yamt 				} else
    592  1.12   mycroft 					nfsd_head_flag &= ~NFSD_CHECKSLP;
    593  1.10   mycroft 			}
    594  1.66      yamt 			simple_unlock(&nfsd_slock);
    595  1.85      yamt 			if ((slp = nfsd->nfsd_slp) == NULL)
    596  1.10   mycroft 				continue;
    597  1.10   mycroft 			if (slp->ns_flag & SLP_VALID) {
    598  1.10   mycroft 				if (slp->ns_flag & SLP_DISCONN)
    599  1.10   mycroft 					nfsrv_zapsock(slp);
    600  1.85      yamt 				else if ((slp->ns_flag & SLP_NEEDQ) != 0) {
    601  1.85      yamt 					nfsrv_rcv(slp->ns_so, (void *)slp,
    602  1.85      yamt 					    M_WAIT);
    603  1.10   mycroft 				}
    604  1.19      fvdl 				error = nfsrv_dorec(slp, nfsd, &nd);
    605  1.95    kardel 				getmicrotime(&tv);
    606  1.95    kardel 				cur_usec = (u_quad_t)tv.tv_sec * 1000000 +
    607  1.95    kardel 					(u_quad_t)tv.tv_usec;
    608  1.57      yamt 				if (error && LIST_FIRST(&slp->ns_tq) &&
    609  1.57      yamt 				    LIST_FIRST(&slp->ns_tq)->nd_time <=
    610  1.57      yamt 				    cur_usec) {
    611  1.19      fvdl 					error = 0;
    612  1.19      fvdl 					cacherep = RC_DOIT;
    613  1.19      fvdl 					writes_todo = 1;
    614  1.19      fvdl 				} else
    615  1.19      fvdl 					writes_todo = 0;
    616  1.85      yamt 				if (error == 0 && slp->ns_rec != NULL) {
    617  1.85      yamt 					nfsrv_wakenfsd(slp);
    618  1.85      yamt 				}
    619   1.1       cgd 			}
    620  1.10   mycroft 		} else {
    621  1.10   mycroft 			error = 0;
    622  1.19      fvdl 			slp = nfsd->nfsd_slp;
    623  1.10   mycroft 		}
    624  1.10   mycroft 		if (error || (slp->ns_flag & SLP_VALID) == 0) {
    625  1.19      fvdl 			if (nd) {
    626  1.93      yamt 				nfsdreq_free(nd);
    627  1.19      fvdl 				nd = NULL;
    628  1.19      fvdl 			}
    629  1.85      yamt 			nfsd->nfsd_slp = NULL;
    630  1.10   mycroft 			nfsrv_slpderef(slp);
    631   1.1       cgd 			continue;
    632   1.1       cgd 		}
    633  1.10   mycroft 		splx(s);
    634  1.86      yamt 		sotype = slp->ns_so->so_type;
    635  1.19      fvdl 		if (nd) {
    636  1.95    kardel 			getmicrotime(&nd->nd_starttime);
    637  1.63      yamt 			if (nd->nd_nam2)
    638  1.63      yamt 				nd->nd_nam = nd->nd_nam2;
    639  1.63      yamt 			else
    640  1.63      yamt 				nd->nd_nam = slp->ns_nam;
    641  1.63      yamt 
    642  1.63      yamt 			/*
    643  1.63      yamt 			 * Check to see if authorization is needed.
    644  1.63      yamt 			 */
    645  1.63      yamt 			if (nfsd->nfsd_flag & NFSD_NEEDAUTH) {
    646  1.63      yamt 				nfsd->nfsd_flag &= ~NFSD_NEEDAUTH;
    647  1.63      yamt 				nsd->nsd_haddr = mtod(nd->nd_nam,
    648  1.63      yamt 				    struct sockaddr_in *)->sin_addr.s_addr;
    649  1.63      yamt 				nsd->nsd_authlen = nfsd->nfsd_authlen;
    650  1.63      yamt 				nsd->nsd_verflen = nfsd->nfsd_verflen;
    651  1.63      yamt 				if (!copyout(nfsd->nfsd_authstr,
    652  1.63      yamt 				    nsd->nsd_authstr, nfsd->nfsd_authlen) &&
    653  1.63      yamt 				    !copyout(nfsd->nfsd_verfstr,
    654  1.63      yamt 				    nsd->nsd_verfstr, nfsd->nfsd_verflen) &&
    655  1.63      yamt 				    !copyout(nsd, argp, sizeof (*nsd))) {
    656  1.63      yamt 					PRELE(l);
    657  1.63      yamt 					return (ENEEDAUTH);
    658  1.63      yamt 				}
    659  1.63      yamt 				cacherep = RC_DROPIT;
    660  1.10   mycroft 			} else
    661  1.63      yamt 				cacherep = nfsrv_getcache(nd, slp, &mreq);
    662  1.63      yamt 
    663  1.63      yamt 			/*
    664  1.63      yamt 			 * Check for just starting up for NQNFS and send
    665  1.63      yamt 			 * fake "try again later" replies to the NQNFS clients.
    666  1.63      yamt 			 */
    667  1.95    kardel 			if (notstarted && nqnfsstarttime <= time_second) {
    668  1.63      yamt 				if (modify_flag) {
    669  1.63      yamt 					nqnfsstarttime =
    670  1.95    kardel 					    time_second + nqsrv_writeslack;
    671  1.63      yamt 					modify_flag = 0;
    672  1.63      yamt 				} else
    673  1.63      yamt 					notstarted = 0;
    674  1.63      yamt 			}
    675  1.63      yamt 			if (notstarted) {
    676  1.63      yamt 				if ((nd->nd_flag & ND_NQNFS) == 0)
    677  1.63      yamt 					cacherep = RC_DROPIT;
    678  1.63      yamt 				else if (nd->nd_procnum != NFSPROC_WRITE) {
    679  1.63      yamt 					nd->nd_procnum = NFSPROC_NOOP;
    680  1.63      yamt 					nd->nd_repstat = NQNFS_TRYLATER;
    681  1.63      yamt 					cacherep = RC_DOIT;
    682  1.63      yamt 				} else
    683  1.63      yamt 					modify_flag = 1;
    684  1.63      yamt 			} else if (nfsd->nfsd_flag & NFSD_AUTHFAIL) {
    685  1.63      yamt 				nfsd->nfsd_flag &= ~NFSD_AUTHFAIL;
    686  1.10   mycroft 				nd->nd_procnum = NFSPROC_NOOP;
    687  1.63      yamt 				nd->nd_repstat =
    688  1.63      yamt 				    (NFSERR_AUTHERR | AUTH_TOOWEAK);
    689  1.10   mycroft 				cacherep = RC_DOIT;
    690  1.63      yamt 			}
    691  1.10   mycroft 		}
    692  1.10   mycroft 
    693  1.19      fvdl 		/*
    694  1.19      fvdl 		 * Loop to get all the write rpc relies that have been
    695  1.19      fvdl 		 * gathered together.
    696  1.19      fvdl 		 */
    697  1.19      fvdl 		do {
    698  1.34  sommerfe #ifdef DIAGNOSTIC
    699  1.63      yamt 			int lockcount;
    700  1.34  sommerfe #endif
    701  1.63      yamt 			switch (cacherep) {
    702  1.63      yamt 			case RC_DOIT:
    703  1.34  sommerfe #ifdef DIAGNOSTIC
    704  1.63      yamt 				/*
    705  1.63      yamt 				 * NFS server procs should neither release
    706  1.63      yamt 				 * locks already held, nor leave things
    707  1.63      yamt 				 * locked.  Catch this sooner, rather than
    708  1.63      yamt 				 * later (when we try to relock something we
    709  1.63      yamt 				 * already have locked).  Careful inspection
    710  1.63      yamt 				 * of the failing routine usually turns up the
    711  1.63      yamt 				 * lock leak.. once we know what it is..
    712  1.63      yamt 				 */
    713  1.63      yamt 				lockcount = l->l_locks;
    714  1.34  sommerfe #endif
    715  1.80      onoe 				mreq = NULL;
    716  1.88      yamt 				netexport_rdlock();
    717  1.90  christos 				if (writes_todo || nd == NULL ||
    718  1.90  christos 				     (!(nd->nd_flag & ND_NFSV3) &&
    719  1.63      yamt 				     nd->nd_procnum == NFSPROC_WRITE &&
    720  1.63      yamt 				     nfsrvw_procrastinate > 0 && !notstarted))
    721  1.63      yamt 					error = nfsrv_writegather(&nd, slp,
    722  1.84  christos 					    l, &mreq);
    723  1.63      yamt 				else
    724  1.63      yamt 					error =
    725  1.63      yamt 					    (*(nfsrv3_procs[nd->nd_procnum]))
    726  1.84  christos 					    (nd, slp, l, &mreq);
    727  1.88      yamt 				netexport_rdunlock();
    728  1.34  sommerfe #ifdef DIAGNOSTIC
    729  1.63      yamt 				if (l->l_locks != lockcount) {
    730  1.63      yamt 					/*
    731  1.63      yamt 					 * If you see this panic, audit
    732  1.63      yamt 					 * nfsrv3_procs[nd->nd_procnum] for
    733  1.63      yamt 					 * vnode locking errors (usually, it's
    734  1.63      yamt 					 * due to forgetting to vput()
    735  1.63      yamt 					 * something).
    736  1.63      yamt 					 */
    737  1.46      matt #ifdef DEBUG
    738  1.63      yamt 					extern void printlockedvnodes(void);
    739  1.63      yamt 					printlockedvnodes();
    740  1.46      matt #endif
    741  1.63      yamt 					printf("nfsd: locking botch in op %d"
    742  1.63      yamt 					    " (before %d, after %d)\n",
    743  1.63      yamt 					    nd ? nd->nd_procnum : -1,
    744  1.63      yamt 					    lockcount, l->l_locks);
    745  1.63      yamt 				}
    746  1.34  sommerfe #endif
    747  1.80      onoe 				if (mreq == NULL) {
    748  1.80      onoe 					if (nd != NULL) {
    749  1.80      onoe 						if (nd->nd_nam2)
    750  1.80      onoe 							m_free(nd->nd_nam2);
    751  1.80      onoe 						if (nd->nd_mrep)
    752  1.80      onoe 							m_freem(nd->nd_mrep);
    753  1.80      onoe 					}
    754  1.63      yamt 					break;
    755  1.80      onoe 				}
    756  1.63      yamt 				if (error) {
    757  1.63      yamt 					if (nd->nd_procnum != NQNFSPROC_VACATED)
    758  1.63      yamt 						nfsstats.srv_errs++;
    759  1.63      yamt 					nfsrv_updatecache(nd, FALSE, mreq);
    760  1.63      yamt 					if (nd->nd_nam2)
    761  1.63      yamt 						m_freem(nd->nd_nam2);
    762  1.63      yamt 					break;
    763  1.63      yamt 				}
    764  1.63      yamt 				nfsstats.srvrpccnt[nd->nd_procnum]++;
    765  1.63      yamt 				nfsrv_updatecache(nd, TRUE, mreq);
    766  1.63      yamt 				nd->nd_mrep = (struct mbuf *)0;
    767  1.63      yamt 			case RC_REPLY:
    768  1.63      yamt 				m = mreq;
    769  1.63      yamt 				siz = 0;
    770  1.63      yamt 				while (m) {
    771  1.63      yamt 					siz += m->m_len;
    772  1.63      yamt 					m = m->m_next;
    773  1.63      yamt 				}
    774  1.63      yamt 				if (siz <= 0 || siz > NFS_MAXPACKET) {
    775  1.63      yamt 					printf("mbuf siz=%d\n",siz);
    776  1.63      yamt 					panic("Bad nfs svc reply");
    777  1.63      yamt 				}
    778  1.63      yamt 				m = mreq;
    779  1.63      yamt 				m->m_pkthdr.len = siz;
    780  1.63      yamt 				m->m_pkthdr.rcvif = (struct ifnet *)0;
    781  1.63      yamt 				/*
    782  1.63      yamt 				 * For stream protocols, prepend a Sun RPC
    783  1.63      yamt 				 * Record Mark.
    784  1.63      yamt 				 */
    785  1.63      yamt 				if (sotype == SOCK_STREAM) {
    786  1.63      yamt 					M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
    787  1.63      yamt 					*mtod(m, u_int32_t *) =
    788  1.63      yamt 					    htonl(0x80000000 | siz);
    789  1.63      yamt 				}
    790  1.86      yamt 				nd->nd_mreq = m;
    791  1.86      yamt 				if (nfsrtton) {
    792  1.86      yamt 					nfsd_rt(slp->ns_so->so_type, nd,
    793  1.86      yamt 					    cacherep);
    794  1.63      yamt 				}
    795  1.87      yamt 				s = splsoftnet();
    796  1.86      yamt 				error = nfsdsock_sendreply(slp, nd);
    797  1.86      yamt 				nd = NULL;
    798  1.63      yamt 				if (error == EPIPE)
    799  1.63      yamt 					nfsrv_zapsock(slp);
    800  1.63      yamt 				if (error == EINTR || error == ERESTART) {
    801  1.63      yamt 					nfsrv_slpderef(slp);
    802  1.63      yamt 					goto done;
    803  1.63      yamt 				}
    804  1.87      yamt 				splx(s);
    805  1.19      fvdl 				break;
    806  1.63      yamt 			case RC_DROPIT:
    807  1.63      yamt 				if (nfsrtton)
    808  1.63      yamt 					nfsd_rt(sotype, nd, cacherep);
    809  1.63      yamt 				m_freem(nd->nd_mrep);
    810  1.63      yamt 				m_freem(nd->nd_nam2);
    811   1.1       cgd 				break;
    812   1.1       cgd 			}
    813  1.63      yamt 			if (nd) {
    814  1.93      yamt 				nfsdreq_free(nd);
    815  1.63      yamt 				nd = NULL;
    816  1.63      yamt 			}
    817  1.63      yamt 
    818   1.1       cgd 			/*
    819  1.63      yamt 			 * Check to see if there are outstanding writes that
    820  1.63      yamt 			 * need to be serviced.
    821   1.1       cgd 			 */
    822  1.95    kardel 			getmicrotime(&tv);
    823  1.95    kardel 			cur_usec = (u_quad_t)tv.tv_sec * 1000000 +
    824  1.95    kardel 			    (u_quad_t)tv.tv_usec;
    825  1.63      yamt 			s = splsoftclock();
    826  1.63      yamt 			if (LIST_FIRST(&slp->ns_tq) &&
    827  1.63      yamt 			    LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec) {
    828  1.63      yamt 				cacherep = RC_DOIT;
    829  1.63      yamt 				writes_todo = 1;
    830  1.63      yamt 			} else
    831  1.63      yamt 				writes_todo = 0;
    832  1.63      yamt 			splx(s);
    833  1.19      fvdl 		} while (writes_todo);
    834  1.14   mycroft 		s = splsoftnet();
    835  1.19      fvdl 		if (nfsrv_dorec(slp, nfsd, &nd)) {
    836  1.19      fvdl 			nfsd->nfsd_slp = NULL;
    837  1.10   mycroft 			nfsrv_slpderef(slp);
    838  1.10   mycroft 		}
    839   1.1       cgd 	}
    840  1.10   mycroft done:
    841  1.53   thorpej 	PRELE(l);
    842  1.66      yamt 	simple_lock(&nfsd_slock);
    843  1.19      fvdl 	TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
    844  1.66      yamt 	simple_unlock(&nfsd_slock);
    845  1.10   mycroft 	splx(s);
    846  1.19      fvdl 	free((caddr_t)nfsd, M_NFSD);
    847  1.10   mycroft 	nsd->nsd_nfsd = (struct nfsd *)0;
    848  1.10   mycroft 	if (--nfs_numnfsd == 0)
    849  1.10   mycroft 		nfsrv_init(TRUE);	/* Reinitialize everything */
    850   1.1       cgd 	return (error);
    851   1.1       cgd }
    852   1.1       cgd 
    853  1.10   mycroft /*
    854  1.10   mycroft  * Shut down a socket associated with an nfssvc_sock structure.
    855  1.10   mycroft  * Should be called with the send lock set, if required.
    856  1.10   mycroft  * The trick here is to increment the sref at the start, so that the nfsds
    857  1.10   mycroft  * will stop using it and clear ns_flag at the end so that it will not be
    858  1.10   mycroft  * reassigned during cleanup.
    859  1.73      yamt  *
    860  1.73      yamt  * called at splsoftnet.
    861  1.10   mycroft  */
    862  1.18  christos void
    863  1.10   mycroft nfsrv_zapsock(slp)
    864  1.38  augustss 	struct nfssvc_sock *slp;
    865  1.10   mycroft {
    866  1.38  augustss 	struct nfsuid *nuidp, *nnuidp;
    867  1.38  augustss 	struct nfsrv_descript *nwp, *nnwp;
    868  1.10   mycroft 	struct socket *so;
    869  1.19      fvdl 	int s;
    870  1.10   mycroft 
    871  1.85      yamt 	if (nfsdsock_drain(slp)) {
    872  1.74      yamt 		return;
    873  1.74      yamt 	}
    874  1.85      yamt 	simple_lock(&nfsd_slock);
    875  1.73      yamt 	if (slp->ns_flag & SLP_DOREC) {
    876  1.73      yamt 		TAILQ_REMOVE(&nfssvc_sockpending, slp, ns_pending);
    877  1.73      yamt 	}
    878  1.73      yamt 	simple_unlock(&nfsd_slock);
    879  1.77      yamt 
    880  1.77      yamt 	so = slp->ns_so;
    881  1.77      yamt 	KASSERT(so != NULL);
    882  1.77      yamt 	so->so_upcall = NULL;
    883  1.77      yamt 	so->so_upcallarg = NULL;
    884  1.77      yamt 	so->so_rcv.sb_flags &= ~SB_UPCALL;
    885  1.77      yamt 	soshutdown(so, SHUT_RDWR);
    886  1.77      yamt 
    887  1.77      yamt 	if (slp->ns_nam)
    888  1.77      yamt 		m_free(slp->ns_nam);
    889  1.77      yamt 	m_freem(slp->ns_raw);
    890  1.77      yamt 	m_freem(slp->ns_rec);
    891  1.77      yamt 	for (nuidp = TAILQ_FIRST(&slp->ns_uidlruhead); nuidp != 0;
    892  1.77      yamt 	    nuidp = nnuidp) {
    893  1.77      yamt 		nnuidp = TAILQ_NEXT(nuidp, nu_lru);
    894  1.77      yamt 		LIST_REMOVE(nuidp, nu_hash);
    895  1.77      yamt 		TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru);
    896  1.77      yamt 		if (nuidp->nu_flag & NU_NAM)
    897  1.77      yamt 			m_freem(nuidp->nu_nam);
    898  1.77      yamt 		free((caddr_t)nuidp, M_NFSUID);
    899  1.77      yamt 	}
    900  1.77      yamt 	s = splsoftclock();
    901  1.77      yamt 	for (nwp = LIST_FIRST(&slp->ns_tq); nwp; nwp = nnwp) {
    902  1.77      yamt 		nnwp = LIST_NEXT(nwp, nd_tq);
    903  1.77      yamt 		LIST_REMOVE(nwp, nd_tq);
    904  1.93      yamt 		nfsdreq_free(nwp);
    905  1.10   mycroft 	}
    906  1.77      yamt 	splx(s);
    907  1.10   mycroft }
    908  1.10   mycroft 
    909  1.10   mycroft /*
    910  1.10   mycroft  * Derefence a server socket structure. If it has no more references and
    911  1.10   mycroft  * is no longer valid, you can throw it away.
    912  1.10   mycroft  */
    913  1.10   mycroft void
    914  1.10   mycroft nfsrv_slpderef(slp)
    915  1.38  augustss 	struct nfssvc_sock *slp;
    916  1.10   mycroft {
    917  1.66      yamt 	LOCK_ASSERT(!simple_lock_held(&nfsd_slock));
    918  1.66      yamt 
    919  1.10   mycroft 	if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) {
    920  1.85      yamt 		struct file *fp;
    921  1.66      yamt 		int s = splsoftnet();
    922  1.66      yamt 		simple_lock(&nfsd_slock);
    923  1.12   mycroft 		TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
    924  1.66      yamt 		simple_unlock(&nfsd_slock);
    925  1.66      yamt 		splx(s);
    926  1.85      yamt 
    927  1.85      yamt 		fp = slp->ns_fp;
    928  1.85      yamt 		if (fp != NULL) {
    929  1.85      yamt 			slp->ns_fp = NULL;
    930  1.85      yamt 			KASSERT(fp != NULL);
    931  1.85      yamt 			KASSERT(fp->f_data == slp->ns_so);
    932  1.85      yamt 			simple_lock(&fp->f_slock);
    933  1.85      yamt 			FILE_USE(fp);
    934  1.85      yamt 			closef(fp, (struct lwp *)0);
    935  1.85      yamt 			slp->ns_so = NULL;
    936  1.85      yamt 		}
    937  1.85      yamt 
    938  1.77      yamt 		nfsrv_sockfree(slp);
    939  1.10   mycroft 	}
    940  1.10   mycroft }
    941  1.10   mycroft 
    942  1.10   mycroft /*
    943  1.10   mycroft  * Initialize the data structures for the server.
    944  1.10   mycroft  * Handshake with any new nfsds starting up to avoid any chance of
    945  1.10   mycroft  * corruption.
    946  1.10   mycroft  */
    947  1.10   mycroft void
    948  1.10   mycroft nfsrv_init(terminating)
    949  1.10   mycroft 	int terminating;
    950  1.10   mycroft {
    951  1.66      yamt 	struct nfssvc_sock *slp;
    952  1.66      yamt 	int s;
    953  1.10   mycroft 
    954  1.66      yamt 	s = splsoftnet();
    955  1.66      yamt 	simple_lock(&nfsd_slock);
    956  1.12   mycroft 	if (nfssvc_sockhead_flag & SLP_INIT)
    957  1.10   mycroft 		panic("nfsd init");
    958  1.12   mycroft 	nfssvc_sockhead_flag |= SLP_INIT;
    959  1.66      yamt 
    960  1.10   mycroft 	if (terminating) {
    961  1.66      yamt 		while ((slp = TAILQ_FIRST(&nfssvc_sockhead)) != NULL) {
    962  1.66      yamt 			simple_unlock(&nfsd_slock);
    963  1.85      yamt 			KASSERT(slp->ns_sref == 0);
    964  1.85      yamt 			slp->ns_sref++;
    965  1.85      yamt 			nfsrv_zapsock(slp);
    966  1.85      yamt 			nfsrv_slpderef(slp);
    967  1.66      yamt 			simple_lock(&nfsd_slock);
    968  1.10   mycroft 		}
    969  1.66      yamt 		simple_unlock(&nfsd_slock);
    970  1.66      yamt 		splx(s);
    971  1.10   mycroft 		nfsrv_cleancache();	/* And clear out server cache */
    972  1.66      yamt 	} else {
    973  1.66      yamt 		simple_unlock(&nfsd_slock);
    974  1.66      yamt 		splx(s);
    975  1.26      fvdl 		nfs_pub.np_valid = 0;
    976  1.66      yamt 	}
    977  1.12   mycroft 
    978  1.12   mycroft 	TAILQ_INIT(&nfssvc_sockhead);
    979  1.66      yamt 	TAILQ_INIT(&nfssvc_sockpending);
    980  1.12   mycroft 	nfssvc_sockhead_flag &= ~SLP_INIT;
    981  1.12   mycroft 
    982  1.12   mycroft 	TAILQ_INIT(&nfsd_head);
    983  1.66      yamt 	SLIST_INIT(&nfsd_idle_head);
    984  1.12   mycroft 	nfsd_head_flag &= ~NFSD_CHECKSLP;
    985  1.12   mycroft 
    986  1.65      yamt 	nfs_udpsock = nfsrv_sockalloc();
    987  1.41      fvdl 
    988  1.41      fvdl #ifdef INET6
    989  1.65      yamt 	nfs_udp6sock = nfsrv_sockalloc();
    990  1.41      fvdl #endif
    991  1.12   mycroft 
    992  1.47       chs #ifdef ISO
    993  1.65      yamt 	nfs_cltpsock = nfsrv_sockalloc();
    994  1.47       chs #endif
    995  1.66      yamt 
    996  1.66      yamt 	simple_lock(&nfsd_slock);
    997  1.66      yamt 	if (nfssvc_sockhead_flag & SLP_WANTINIT) {
    998  1.66      yamt 		nfssvc_sockhead_flag &= ~SLP_WANTINIT;
    999  1.66      yamt 		wakeup(&nfssvc_sockhead);
   1000  1.66      yamt 	}
   1001  1.66      yamt 	simple_unlock(&nfsd_slock);
   1002  1.66      yamt 	splx(s);
   1003  1.10   mycroft }
   1004  1.10   mycroft 
   1005  1.10   mycroft /*
   1006  1.10   mycroft  * Add entries to the server monitor log.
   1007  1.10   mycroft  */
   1008  1.10   mycroft static void
   1009  1.19      fvdl nfsd_rt(sotype, nd, cacherep)
   1010  1.10   mycroft 	int sotype;
   1011  1.38  augustss 	struct nfsrv_descript *nd;
   1012  1.10   mycroft 	int cacherep;
   1013  1.10   mycroft {
   1014  1.95    kardel 	struct timeval tv;
   1015  1.38  augustss 	struct drt *rt;
   1016  1.10   mycroft 
   1017  1.10   mycroft 	rt = &nfsdrt.drt[nfsdrt.pos];
   1018  1.10   mycroft 	if (cacherep == RC_DOIT)
   1019  1.10   mycroft 		rt->flag = 0;
   1020  1.10   mycroft 	else if (cacherep == RC_REPLY)
   1021  1.10   mycroft 		rt->flag = DRT_CACHEREPLY;
   1022  1.10   mycroft 	else
   1023  1.10   mycroft 		rt->flag = DRT_CACHEDROP;
   1024  1.10   mycroft 	if (sotype == SOCK_STREAM)
   1025  1.10   mycroft 		rt->flag |= DRT_TCP;
   1026  1.19      fvdl 	if (nd->nd_flag & ND_NQNFS)
   1027  1.10   mycroft 		rt->flag |= DRT_NQNFS;
   1028  1.19      fvdl 	else if (nd->nd_flag & ND_NFSV3)
   1029  1.19      fvdl 		rt->flag |= DRT_NFSV3;
   1030  1.10   mycroft 	rt->proc = nd->nd_procnum;
   1031  1.19      fvdl 	if (mtod(nd->nd_nam, struct sockaddr *)->sa_family == AF_INET)
   1032  1.19      fvdl 	    rt->ipadr = mtod(nd->nd_nam, struct sockaddr_in *)->sin_addr.s_addr;
   1033  1.10   mycroft 	else
   1034  1.19      fvdl 	    rt->ipadr = INADDR_ANY;
   1035  1.95    kardel 	getmicrotime(&tv);
   1036  1.95    kardel 	rt->resptime = ((tv.tv_sec - nd->nd_starttime.tv_sec) * 1000000) +
   1037  1.95    kardel 		(tv.tv_usec - nd->nd_starttime.tv_usec);
   1038  1.95    kardel 	rt->tstamp = tv;
   1039  1.10   mycroft 	nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ;
   1040  1.10   mycroft }
   1041   1.3     glass #endif /* NFSSERVER */
   1042   1.3     glass 
   1043  1.24   thorpej #ifdef NFS
   1044  1.22   thorpej 
   1045  1.22   thorpej int nfs_defect = 0;
   1046   1.1       cgd /*
   1047  1.39    tsarna  * Asynchronous I/O threads for client nfs.
   1048  1.10   mycroft  * They do read-ahead and write-behind operations on the block I/O cache.
   1049  1.10   mycroft  * Never returns unless it fails or gets killed.
   1050   1.1       cgd  */
   1051  1.39    tsarna 
   1052  1.18  christos int
   1053  1.53   thorpej nfssvc_iod(l)
   1054  1.53   thorpej 	struct lwp *l;
   1055   1.1       cgd {
   1056  1.38  augustss 	struct buf *bp;
   1057  1.58      yamt 	int i;
   1058  1.58      yamt 	struct nfs_iod *myiod;
   1059  1.22   thorpej 	struct nfsmount *nmp;
   1060  1.22   thorpej 	int error = 0;
   1061  1.89  christos 	struct proc *p = l->l_proc;
   1062   1.1       cgd 
   1063   1.1       cgd 	/*
   1064   1.1       cgd 	 * Assign my position or return error if too many already running
   1065   1.1       cgd 	 */
   1066  1.58      yamt 	myiod = NULL;
   1067   1.1       cgd 	for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
   1068  1.58      yamt 		if (nfs_asyncdaemon[i].nid_proc == NULL) {
   1069  1.58      yamt 			myiod = &nfs_asyncdaemon[i];
   1070   1.1       cgd 			break;
   1071   1.1       cgd 		}
   1072  1.58      yamt 	if (myiod == NULL)
   1073   1.1       cgd 		return (EBUSY);
   1074  1.58      yamt 	myiod->nid_proc = p;
   1075   1.1       cgd 	nfs_numasync++;
   1076  1.53   thorpej 	PHOLD(l);
   1077   1.1       cgd 	/*
   1078  1.42   nathanw 	 * Just loop around doing our stuff until SIGKILL
   1079   1.1       cgd 	 */
   1080   1.1       cgd 	for (;;) {
   1081  1.60      yamt 		while (/*CONSTCOND*/ TRUE) {
   1082  1.60      yamt 			simple_lock(&myiod->nid_slock);
   1083  1.60      yamt 			nmp = myiod->nid_mount;
   1084  1.60      yamt 			if (nmp) {
   1085  1.60      yamt 				simple_lock(&nmp->nm_slock);
   1086  1.60      yamt 				if (!TAILQ_EMPTY(&nmp->nm_bufq)) {
   1087  1.60      yamt 					simple_unlock(&myiod->nid_slock);
   1088  1.60      yamt 					break;
   1089  1.60      yamt 				}
   1090  1.59      yamt 				nmp->nm_bufqiods--;
   1091  1.60      yamt 				simple_unlock(&nmp->nm_slock);
   1092  1.60      yamt 			}
   1093  1.59      yamt 			myiod->nid_want = p;
   1094  1.59      yamt 			myiod->nid_mount = NULL;
   1095  1.60      yamt 			error = ltsleep(&myiod->nid_want,
   1096  1.60      yamt 			    PWAIT | PCATCH | PNORELOCK, "nfsidl", 0,
   1097  1.60      yamt 			    &myiod->nid_slock);
   1098  1.60      yamt 			if (error)
   1099  1.60      yamt 				goto quit;
   1100  1.59      yamt 		}
   1101  1.60      yamt 
   1102  1.60      yamt 		while ((bp = TAILQ_FIRST(&nmp->nm_bufq)) != NULL) {
   1103  1.59      yamt 			/* Take one off the front of the list */
   1104  1.59      yamt 			TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist);
   1105  1.59      yamt 			nmp->nm_bufqlen--;
   1106  1.59      yamt 			if (nmp->nm_bufqwant &&
   1107  1.59      yamt 			    nmp->nm_bufqlen < 2 * nfs_numasync) {
   1108  1.59      yamt 				nmp->nm_bufqwant = FALSE;
   1109  1.59      yamt 				wakeup(&nmp->nm_bufq);
   1110  1.59      yamt 			}
   1111  1.60      yamt 			simple_unlock(&nmp->nm_slock);
   1112  1.79  christos 			(void)nfs_doio(bp);
   1113  1.60      yamt 			simple_lock(&nmp->nm_slock);
   1114  1.59      yamt 			/*
   1115  1.81    rpaulo 			 * If there are more than one iod on this mount,
   1116  1.81    rpaulo 			 * then defect so that the iods can be shared out
   1117  1.81    rpaulo 			 * fairly between the mounts
   1118  1.59      yamt 			 */
   1119  1.59      yamt 			if (nfs_defect && nmp->nm_bufqiods > 1) {
   1120  1.59      yamt 				myiod->nid_mount = NULL;
   1121  1.59      yamt 				nmp->nm_bufqiods--;
   1122  1.59      yamt 				break;
   1123  1.59      yamt 			}
   1124  1.59      yamt 		}
   1125  1.60      yamt 		simple_unlock(&nmp->nm_slock);
   1126   1.1       cgd 	}
   1127  1.60      yamt quit:
   1128  1.53   thorpej 	PRELE(l);
   1129  1.45       chs 	if (nmp)
   1130  1.45       chs 		nmp->nm_bufqiods--;
   1131  1.58      yamt 	myiod->nid_want = NULL;
   1132  1.58      yamt 	myiod->nid_mount = NULL;
   1133  1.58      yamt 	myiod->nid_proc = NULL;
   1134  1.33   mycroft 	nfs_numasync--;
   1135  1.39    tsarna 
   1136  1.60      yamt 	return error;
   1137  1.60      yamt }
   1138  1.60      yamt 
   1139  1.60      yamt void
   1140  1.60      yamt nfs_iodinit()
   1141  1.60      yamt {
   1142  1.60      yamt 	int i;
   1143  1.60      yamt 
   1144  1.60      yamt 	for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
   1145  1.60      yamt 		simple_lock_init(&nfs_asyncdaemon[i].nid_slock);
   1146   1.1       cgd }
   1147   1.3     glass 
   1148  1.39    tsarna void
   1149  1.99      yamt start_nfsio(void *arg)
   1150  1.39    tsarna {
   1151  1.53   thorpej 	nfssvc_iod(curlwp);
   1152  1.78     perry 
   1153  1.39    tsarna 	kthread_exit(0);
   1154  1.39    tsarna }
   1155  1.39    tsarna 
   1156  1.39    tsarna void
   1157  1.39    tsarna nfs_getset_niothreads(set)
   1158  1.39    tsarna 	int set;
   1159  1.39    tsarna {
   1160  1.39    tsarna 	int i, have, start;
   1161  1.78     perry 
   1162  1.39    tsarna 	for (have = 0, i = 0; i < NFS_MAXASYNCDAEMON; i++)
   1163  1.58      yamt 		if (nfs_asyncdaemon[i].nid_proc != NULL)
   1164  1.39    tsarna 			have++;
   1165  1.39    tsarna 
   1166  1.39    tsarna 	if (set) {
   1167  1.39    tsarna 		/* clamp to sane range */
   1168  1.39    tsarna 		nfs_niothreads = max(0, min(nfs_niothreads, NFS_MAXASYNCDAEMON));
   1169  1.39    tsarna 
   1170  1.39    tsarna 		start = nfs_niothreads - have;
   1171  1.39    tsarna 
   1172  1.39    tsarna 		while (start > 0) {
   1173  1.39    tsarna 			kthread_create1(start_nfsio, NULL, NULL, "nfsio");
   1174  1.39    tsarna 			start--;
   1175  1.39    tsarna 		}
   1176  1.39    tsarna 
   1177  1.39    tsarna 		for (i = 0; (start < 0) && (i < NFS_MAXASYNCDAEMON); i++)
   1178  1.58      yamt 			if (nfs_asyncdaemon[i].nid_proc != NULL) {
   1179  1.58      yamt 				psignal(nfs_asyncdaemon[i].nid_proc, SIGKILL);
   1180  1.39    tsarna 				start++;
   1181  1.39    tsarna 			}
   1182  1.39    tsarna 	} else {
   1183  1.40    tsarna 		if (nfs_niothreads >= 0)
   1184  1.40    tsarna 			nfs_niothreads = have;
   1185  1.39    tsarna 	}
   1186  1.39    tsarna }
   1187  1.19      fvdl 
   1188  1.10   mycroft /*
   1189  1.10   mycroft  * Get an authorization string for the uid by having the mount_nfs sitting
   1190  1.10   mycroft  * on this mount point porpous out of the kernel and do it.
   1191  1.10   mycroft  */
   1192  1.18  christos int
   1193  1.19      fvdl nfs_getauth(nmp, rep, cred, auth_str, auth_len, verf_str, verf_len, key)
   1194  1.38  augustss 	struct nfsmount *nmp;
   1195  1.10   mycroft 	struct nfsreq *rep;
   1196  1.92      elad 	kauth_cred_t cred;
   1197  1.10   mycroft 	char **auth_str;
   1198  1.10   mycroft 	int *auth_len;
   1199  1.19      fvdl 	char *verf_str;
   1200  1.19      fvdl 	int *verf_len;
   1201  1.19      fvdl 	NFSKERBKEY_T key;		/* return session key */
   1202  1.10   mycroft {
   1203  1.10   mycroft 	int error = 0;
   1204  1.10   mycroft 
   1205  1.27      fvdl 	while ((nmp->nm_iflag & NFSMNT_WAITAUTH) == 0) {
   1206  1.27      fvdl 		nmp->nm_iflag |= NFSMNT_WANTAUTH;
   1207  1.10   mycroft 		(void) tsleep((caddr_t)&nmp->nm_authtype, PSOCK,
   1208  1.10   mycroft 			"nfsauth1", 2 * hz);
   1209  1.84  christos 		error = nfs_sigintr(nmp, rep, rep->r_lwp);
   1210  1.18  christos 		if (error) {
   1211  1.27      fvdl 			nmp->nm_iflag &= ~NFSMNT_WANTAUTH;
   1212  1.10   mycroft 			return (error);
   1213  1.10   mycroft 		}
   1214  1.10   mycroft 	}
   1215  1.27      fvdl 	nmp->nm_iflag &= ~(NFSMNT_WAITAUTH | NFSMNT_WANTAUTH);
   1216  1.10   mycroft 	nmp->nm_authstr = *auth_str = (char *)malloc(RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK);
   1217  1.19      fvdl 	nmp->nm_authlen = RPCAUTH_MAXSIZ;
   1218  1.19      fvdl 	nmp->nm_verfstr = verf_str;
   1219  1.19      fvdl 	nmp->nm_verflen = *verf_len;
   1220  1.92      elad 	nmp->nm_authuid = kauth_cred_geteuid(cred);
   1221  1.10   mycroft 	wakeup((caddr_t)&nmp->nm_authstr);
   1222   1.3     glass 
   1223  1.10   mycroft 	/*
   1224  1.10   mycroft 	 * And wait for mount_nfs to do its stuff.
   1225  1.10   mycroft 	 */
   1226  1.27      fvdl 	while ((nmp->nm_iflag & NFSMNT_HASAUTH) == 0 && error == 0) {
   1227  1.10   mycroft 		(void) tsleep((caddr_t)&nmp->nm_authlen, PSOCK,
   1228  1.10   mycroft 			"nfsauth2", 2 * hz);
   1229  1.84  christos 		error = nfs_sigintr(nmp, rep, rep->r_lwp);
   1230  1.10   mycroft 	}
   1231  1.27      fvdl 	if (nmp->nm_iflag & NFSMNT_AUTHERR) {
   1232  1.27      fvdl 		nmp->nm_iflag &= ~NFSMNT_AUTHERR;
   1233  1.10   mycroft 		error = EAUTH;
   1234  1.10   mycroft 	}
   1235  1.10   mycroft 	if (error)
   1236  1.10   mycroft 		free((caddr_t)*auth_str, M_TEMP);
   1237  1.10   mycroft 	else {
   1238  1.10   mycroft 		*auth_len = nmp->nm_authlen;
   1239  1.19      fvdl 		*verf_len = nmp->nm_verflen;
   1240  1.52       chs 		memcpy(key, nmp->nm_key, sizeof (NFSKERBKEY_T));
   1241  1.10   mycroft 	}
   1242  1.27      fvdl 	nmp->nm_iflag &= ~NFSMNT_HASAUTH;
   1243  1.27      fvdl 	nmp->nm_iflag |= NFSMNT_WAITAUTH;
   1244  1.27      fvdl 	if (nmp->nm_iflag & NFSMNT_WANTAUTH) {
   1245  1.27      fvdl 		nmp->nm_iflag &= ~NFSMNT_WANTAUTH;
   1246  1.10   mycroft 		wakeup((caddr_t)&nmp->nm_authtype);
   1247  1.10   mycroft 	}
   1248  1.19      fvdl 	return (error);
   1249  1.19      fvdl }
   1250  1.19      fvdl 
   1251  1.19      fvdl /*
   1252  1.19      fvdl  * Get a nickname authenticator and verifier.
   1253  1.19      fvdl  */
   1254  1.19      fvdl int
   1255  1.99      yamt nfs_getnickauth(struct nfsmount *nmp, kauth_cred_t cred, char **auth_str,
   1256  1.99      yamt     int *auth_len, char *verf_str, int verf_len)
   1257  1.19      fvdl {
   1258  1.95    kardel 	struct timeval ktvin, ktvout, tv;
   1259  1.38  augustss 	struct nfsuid *nuidp;
   1260  1.38  augustss 	u_int32_t *nickp, *verfp;
   1261  1.19      fvdl 
   1262  1.91       mrg 	memset(&ktvout, 0, sizeof ktvout);	/* XXX gcc */
   1263  1.91       mrg 
   1264  1.19      fvdl #ifdef DIAGNOSTIC
   1265  1.19      fvdl 	if (verf_len < (4 * NFSX_UNSIGNED))
   1266  1.19      fvdl 		panic("nfs_getnickauth verf too small");
   1267  1.19      fvdl #endif
   1268  1.92      elad 	LIST_FOREACH(nuidp, NMUIDHASH(nmp, kauth_cred_geteuid(cred)), nu_hash) {
   1269  1.92      elad 		if (kauth_cred_geteuid(nuidp->nu_cr) == kauth_cred_geteuid(cred))
   1270  1.19      fvdl 			break;
   1271  1.19      fvdl 	}
   1272  1.95    kardel 	if (!nuidp || nuidp->nu_expire < time_second)
   1273  1.19      fvdl 		return (EACCES);
   1274  1.19      fvdl 
   1275  1.19      fvdl 	/*
   1276  1.19      fvdl 	 * Move to the end of the lru list (end of lru == most recently used).
   1277  1.19      fvdl 	 */
   1278  1.19      fvdl 	TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
   1279  1.19      fvdl 	TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, nu_lru);
   1280  1.19      fvdl 
   1281  1.19      fvdl 	nickp = (u_int32_t *)malloc(2 * NFSX_UNSIGNED, M_TEMP, M_WAITOK);
   1282  1.19      fvdl 	*nickp++ = txdr_unsigned(RPCAKN_NICKNAME);
   1283  1.19      fvdl 	*nickp = txdr_unsigned(nuidp->nu_nickname);
   1284  1.19      fvdl 	*auth_str = (char *)nickp;
   1285  1.19      fvdl 	*auth_len = 2 * NFSX_UNSIGNED;
   1286  1.19      fvdl 
   1287  1.19      fvdl 	/*
   1288  1.19      fvdl 	 * Now we must encrypt the verifier and package it up.
   1289  1.19      fvdl 	 */
   1290  1.19      fvdl 	verfp = (u_int32_t *)verf_str;
   1291  1.19      fvdl 	*verfp++ = txdr_unsigned(RPCAKN_NICKNAME);
   1292  1.95    kardel 	getmicrotime(&tv);
   1293  1.95    kardel 	if (tv.tv_sec > nuidp->nu_timestamp.tv_sec ||
   1294  1.95    kardel 	    (tv.tv_sec == nuidp->nu_timestamp.tv_sec &&
   1295  1.95    kardel 	     tv.tv_usec > nuidp->nu_timestamp.tv_usec))
   1296  1.95    kardel 		nuidp->nu_timestamp = tv;
   1297  1.19      fvdl 	else
   1298  1.19      fvdl 		nuidp->nu_timestamp.tv_usec++;
   1299  1.19      fvdl 	ktvin.tv_sec = txdr_unsigned(nuidp->nu_timestamp.tv_sec);
   1300  1.19      fvdl 	ktvin.tv_usec = txdr_unsigned(nuidp->nu_timestamp.tv_usec);
   1301  1.19      fvdl 
   1302  1.19      fvdl 	/*
   1303  1.19      fvdl 	 * Now encrypt the timestamp verifier in ecb mode using the session
   1304  1.19      fvdl 	 * key.
   1305  1.19      fvdl 	 */
   1306  1.19      fvdl #ifdef NFSKERB
   1307  1.19      fvdl 	XXX
   1308  1.19      fvdl #endif
   1309  1.19      fvdl 
   1310  1.19      fvdl 	*verfp++ = ktvout.tv_sec;
   1311  1.19      fvdl 	*verfp++ = ktvout.tv_usec;
   1312  1.19      fvdl 	*verfp = 0;
   1313  1.19      fvdl 	return (0);
   1314  1.19      fvdl }
   1315  1.19      fvdl 
   1316  1.19      fvdl /*
   1317  1.19      fvdl  * Save the current nickname in a hash list entry on the mount point.
   1318  1.19      fvdl  */
   1319  1.19      fvdl int
   1320  1.19      fvdl nfs_savenickauth(nmp, cred, len, key, mdp, dposp, mrep)
   1321  1.38  augustss 	struct nfsmount *nmp;
   1322  1.92      elad 	kauth_cred_t cred;
   1323  1.19      fvdl 	int len;
   1324  1.19      fvdl 	NFSKERBKEY_T key;
   1325  1.19      fvdl 	struct mbuf **mdp;
   1326  1.19      fvdl 	char **dposp;
   1327  1.19      fvdl 	struct mbuf *mrep;
   1328  1.19      fvdl {
   1329  1.38  augustss 	struct nfsuid *nuidp;
   1330  1.38  augustss 	u_int32_t *tl;
   1331  1.38  augustss 	int32_t t1;
   1332  1.19      fvdl 	struct mbuf *md = *mdp;
   1333  1.19      fvdl 	struct timeval ktvin, ktvout;
   1334  1.19      fvdl 	u_int32_t nick;
   1335  1.19      fvdl 	char *dpos = *dposp, *cp2;
   1336  1.19      fvdl 	int deltasec, error = 0;
   1337  1.19      fvdl 
   1338  1.91       mrg 	memset(&ktvout, 0, sizeof ktvout);	 /* XXX gcc */
   1339  1.91       mrg 
   1340  1.19      fvdl 	if (len == (3 * NFSX_UNSIGNED)) {
   1341  1.19      fvdl 		nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   1342  1.19      fvdl 		ktvin.tv_sec = *tl++;
   1343  1.19      fvdl 		ktvin.tv_usec = *tl++;
   1344  1.19      fvdl 		nick = fxdr_unsigned(u_int32_t, *tl);
   1345  1.19      fvdl 
   1346  1.19      fvdl 		/*
   1347  1.19      fvdl 		 * Decrypt the timestamp in ecb mode.
   1348  1.19      fvdl 		 */
   1349  1.19      fvdl #ifdef NFSKERB
   1350  1.19      fvdl 		XXX
   1351  1.19      fvdl #endif
   1352  1.19      fvdl 		ktvout.tv_sec = fxdr_unsigned(long, ktvout.tv_sec);
   1353  1.19      fvdl 		ktvout.tv_usec = fxdr_unsigned(long, ktvout.tv_usec);
   1354  1.95    kardel 		deltasec = time_second - ktvout.tv_sec;
   1355  1.19      fvdl 		if (deltasec < 0)
   1356  1.19      fvdl 			deltasec = -deltasec;
   1357  1.19      fvdl 		/*
   1358  1.19      fvdl 		 * If ok, add it to the hash list for the mount point.
   1359  1.19      fvdl 		 */
   1360  1.19      fvdl 		if (deltasec <= NFS_KERBCLOCKSKEW) {
   1361  1.19      fvdl 			if (nmp->nm_numuids < nuidhash_max) {
   1362  1.19      fvdl 				nmp->nm_numuids++;
   1363  1.19      fvdl 				nuidp = (struct nfsuid *)
   1364  1.19      fvdl 				   malloc(sizeof (struct nfsuid), M_NFSUID,
   1365  1.19      fvdl 					M_WAITOK);
   1366  1.19      fvdl 			} else {
   1367  1.57      yamt 				nuidp = TAILQ_FIRST(&nmp->nm_uidlruhead);
   1368  1.19      fvdl 				LIST_REMOVE(nuidp, nu_hash);
   1369  1.19      fvdl 				TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp,
   1370  1.19      fvdl 					nu_lru);
   1371  1.19      fvdl 			}
   1372  1.19      fvdl 			nuidp->nu_flag = 0;
   1373  1.92      elad 			kauth_cred_seteuid(nuidp->nu_cr, kauth_cred_geteuid(cred));
   1374  1.95    kardel 			nuidp->nu_expire = time_second + NFS_KERBTTL;
   1375  1.19      fvdl 			nuidp->nu_timestamp = ktvout;
   1376  1.19      fvdl 			nuidp->nu_nickname = nick;
   1377  1.52       chs 			memcpy(nuidp->nu_key, key, sizeof (NFSKERBKEY_T));
   1378  1.19      fvdl 			TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp,
   1379  1.19      fvdl 				nu_lru);
   1380  1.92      elad 			LIST_INSERT_HEAD(NMUIDHASH(nmp, kauth_cred_geteuid(cred)),
   1381  1.19      fvdl 				nuidp, nu_hash);
   1382  1.19      fvdl 		}
   1383  1.19      fvdl 	} else
   1384  1.19      fvdl 		nfsm_adv(nfsm_rndup(len));
   1385  1.19      fvdl nfsmout:
   1386  1.19      fvdl 	*mdp = md;
   1387  1.19      fvdl 	*dposp = dpos;
   1388  1.10   mycroft 	return (error);
   1389  1.10   mycroft }
   1390  1.24   thorpej #endif /* NFS */
   1391