xref: /freebsd/sys/fs/nfs/nfs_commonport.c (revision 9ec7b004d0edbf800e5b875353109a36b997b0a3)
19ec7b004SRick Macklem /*-
29ec7b004SRick Macklem  * Copyright (c) 1989, 1993
39ec7b004SRick Macklem  *	The Regents of the University of California.  All rights reserved.
49ec7b004SRick Macklem  *
59ec7b004SRick Macklem  * This code is derived from software contributed to Berkeley by
69ec7b004SRick Macklem  * Rick Macklem at The University of Guelph.
79ec7b004SRick Macklem  *
89ec7b004SRick Macklem  * Redistribution and use in source and binary forms, with or without
99ec7b004SRick Macklem  * modification, are permitted provided that the following conditions
109ec7b004SRick Macklem  * are met:
119ec7b004SRick Macklem  * 1. Redistributions of source code must retain the above copyright
129ec7b004SRick Macklem  *    notice, this list of conditions and the following disclaimer.
139ec7b004SRick Macklem  * 2. Redistributions in binary form must reproduce the above copyright
149ec7b004SRick Macklem  *    notice, this list of conditions and the following disclaimer in the
159ec7b004SRick Macklem  *    documentation and/or other materials provided with the distribution.
169ec7b004SRick Macklem  * 4. Neither the name of the University nor the names of its contributors
179ec7b004SRick Macklem  *    may be used to endorse or promote products derived from this software
189ec7b004SRick Macklem  *    without specific prior written permission.
199ec7b004SRick Macklem  *
209ec7b004SRick Macklem  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
219ec7b004SRick Macklem  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
229ec7b004SRick Macklem  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
239ec7b004SRick Macklem  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
249ec7b004SRick Macklem  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
259ec7b004SRick Macklem  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
269ec7b004SRick Macklem  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
279ec7b004SRick Macklem  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
289ec7b004SRick Macklem  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
299ec7b004SRick Macklem  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
309ec7b004SRick Macklem  * SUCH DAMAGE.
319ec7b004SRick Macklem  *
329ec7b004SRick Macklem  */
339ec7b004SRick Macklem 
349ec7b004SRick Macklem #include <sys/cdefs.h>
359ec7b004SRick Macklem __FBSDID("$FreeBSD$");
369ec7b004SRick Macklem 
379ec7b004SRick Macklem /*
389ec7b004SRick Macklem  * Functions that need to be different for different versions of BSD
399ec7b004SRick Macklem  * kernel should be kept here, along with any global storage specific
409ec7b004SRick Macklem  * to this BSD variant.
419ec7b004SRick Macklem  */
429ec7b004SRick Macklem #include <fs/nfs/nfsport.h>
439ec7b004SRick Macklem #include <sys/sysctl.h>
449ec7b004SRick Macklem #include <vm/vm.h>
459ec7b004SRick Macklem #include <vm/vm_object.h>
469ec7b004SRick Macklem #include <vm/vm_page.h>
479ec7b004SRick Macklem #include <vm/vm_param.h>
489ec7b004SRick Macklem #include <vm/vm_map.h>
499ec7b004SRick Macklem #include <vm/vm_kern.h>
509ec7b004SRick Macklem #include <vm/vm_extern.h>
519ec7b004SRick Macklem #include <vm/uma.h>
529ec7b004SRick Macklem #include <vm/uma_int.h>
539ec7b004SRick Macklem 
549ec7b004SRick Macklem extern int nfscl_ticks;
559ec7b004SRick Macklem extern int nfsrv_nfsuserd;
569ec7b004SRick Macklem extern struct nfssockreq nfsrv_nfsuserdsock;
579ec7b004SRick Macklem extern void (*nfsd_call_recall)(struct vnode *, int, struct ucred *,
589ec7b004SRick Macklem     struct thread *);
599ec7b004SRick Macklem extern int nfsrv_useacl;
609ec7b004SRick Macklem struct mount nfsv4root_mnt;
619ec7b004SRick Macklem int newnfs_numnfsd = 0;
629ec7b004SRick Macklem struct nfsstats newnfsstats;
639ec7b004SRick Macklem int nfs_numnfscbd = 0;
649ec7b004SRick Macklem char nfsv4_callbackaddr[INET6_ADDRSTRLEN];
659ec7b004SRick Macklem struct callout newnfsd_callout;
669ec7b004SRick Macklem void (*nfsd_call_servertimer)(void) = NULL;
679ec7b004SRick Macklem void (*ncl_call_invalcaches)(struct vnode *) = NULL;
689ec7b004SRick Macklem 
699ec7b004SRick Macklem static int nfs_realign_test;
709ec7b004SRick Macklem static int nfs_realign_count;
719ec7b004SRick Macklem 
729ec7b004SRick Macklem SYSCTL_NODE(_vfs, OID_AUTO, newnfs, CTLFLAG_RW, 0, "New NFS filesystem");
739ec7b004SRick Macklem SYSCTL_INT(_vfs_newnfs, OID_AUTO, realign_test, CTLFLAG_RW, &nfs_realign_test, 0, "");
749ec7b004SRick Macklem SYSCTL_INT(_vfs_newnfs, OID_AUTO, realign_count, CTLFLAG_RW, &nfs_realign_count, 0, "");
759ec7b004SRick Macklem SYSCTL_INT(_vfs_newnfs, OID_AUTO, nfs4acl_enable, CTLFLAG_RW, &nfsrv_useacl, 0, "");
769ec7b004SRick Macklem SYSCTL_STRING(_vfs_newnfs, OID_AUTO, callback_addr, CTLFLAG_RW,
779ec7b004SRick Macklem     nfsv4_callbackaddr, sizeof(nfsv4_callbackaddr), "");
789ec7b004SRick Macklem 
799ec7b004SRick Macklem /*
809ec7b004SRick Macklem  * Defines for malloc
819ec7b004SRick Macklem  * (Here for FreeBSD, since they allocate storage.)
829ec7b004SRick Macklem  */
839ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSRVCACHE, "NFSD srvcache", "NFSD Server Request Cache");
849ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSDCLIENT, "NFSD V4client", "NFSD V4 Client Id");
859ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSDSTATE, "NFSD V4state", "NFSD V4 State (Openowner, Open, Lockowner, Delegation");
869ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSDLOCK, "NFSD V4lock", "NFSD V4 byte range lock");
879ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSDLOCKFILE, "NFSD lckfile", "NFSD Open/Lock file");
889ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSSTRING, "NFSD string", "NFSD V4 long string");
899ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSUSERGROUP, "NFSD usrgroup", "NFSD V4 User/group map");
909ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSDREQ, "NFS req", "NFS request header");
919ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSFH, "NFS fh", "NFS file handle");
929ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSCLOWNER, "NFSCL owner", "NFSCL Open Owner");
939ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSCLOPEN, "NFSCL open", "NFSCL Open");
949ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSCLDELEG, "NFSCL deleg", "NFSCL Delegation");
959ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSCLCLIENT, "NFSCL client", "NFSCL Client");
969ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSCLLOCKOWNER, "NFSCL lckown", "NFSCL Lock Owner");
979ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSCLLOCK, "NFSCL lck", "NFSCL Lock");
989ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSV4NODE, "NEWNFSnode", "New nfs vnode");
999ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSDIRECTIO, "NEWdirectio", "New nfs Direct IO buffer");
1009ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSDIROFF, "Newnfscl_diroff", "New NFS directory offset data");
1019ec7b004SRick Macklem 
1029ec7b004SRick Macklem /*
1039ec7b004SRick Macklem  * Definition of mutex locks.
1049ec7b004SRick Macklem  * newnfsd_mtx is used in nfsrvd_nfsd() to protect the nfs socket list
1059ec7b004SRick Macklem  * and assorted other nfsd structures.
1069ec7b004SRick Macklem  * Giant is used to protect the nfsd list and count, which is just
1079ec7b004SRick Macklem  *  updated when nfsd's start/stop and is grabbed for nfsrvd_dorpc()
1089ec7b004SRick Macklem  *  for the VFS ops.
1099ec7b004SRick Macklem  */
1109ec7b004SRick Macklem struct mtx newnfsd_mtx;
1119ec7b004SRick Macklem struct mtx nfs_sockl_mutex;
1129ec7b004SRick Macklem struct mtx nfs_state_mutex;
1139ec7b004SRick Macklem struct mtx nfs_nameid_mutex;
1149ec7b004SRick Macklem struct mtx nfs_req_mutex;
1159ec7b004SRick Macklem struct mtx nfs_slock_mutex;
1169ec7b004SRick Macklem 
1179ec7b004SRick Macklem /* local functions */
1189ec7b004SRick Macklem static int nfssvc_call(struct thread *, struct nfssvc_args *, struct ucred *);
1199ec7b004SRick Macklem 
1209ec7b004SRick Macklem #if defined(__i386__)
1219ec7b004SRick Macklem /*
1229ec7b004SRick Macklem  * These architectures don't need re-alignment, so just return.
1239ec7b004SRick Macklem  */
1249ec7b004SRick Macklem void
1259ec7b004SRick Macklem newnfs_realign(struct mbuf **pm)
1269ec7b004SRick Macklem {
1279ec7b004SRick Macklem 
1289ec7b004SRick Macklem 	return;
1299ec7b004SRick Macklem }
1309ec7b004SRick Macklem #else
1319ec7b004SRick Macklem /*
1329ec7b004SRick Macklem  *	nfs_realign:
1339ec7b004SRick Macklem  *
1349ec7b004SRick Macklem  *	Check for badly aligned mbuf data and realign by copying the unaligned
1359ec7b004SRick Macklem  *	portion of the data into a new mbuf chain and freeing the portions
1369ec7b004SRick Macklem  *	of the old chain that were replaced.
1379ec7b004SRick Macklem  *
1389ec7b004SRick Macklem  *	We cannot simply realign the data within the existing mbuf chain
1399ec7b004SRick Macklem  *	because the underlying buffers may contain other rpc commands and
1409ec7b004SRick Macklem  *	we cannot afford to overwrite them.
1419ec7b004SRick Macklem  *
1429ec7b004SRick Macklem  *	We would prefer to avoid this situation entirely.  The situation does
1439ec7b004SRick Macklem  *	not occur with NFS/UDP and is supposed to only occassionally occur
1449ec7b004SRick Macklem  *	with TCP.  Use vfs.nfs.realign_count and realign_test to check this.
1459ec7b004SRick Macklem  */
1469ec7b004SRick Macklem void
1479ec7b004SRick Macklem newnfs_realign(struct mbuf **pm)
1489ec7b004SRick Macklem {
1499ec7b004SRick Macklem 	struct mbuf *m;
1509ec7b004SRick Macklem 	struct mbuf *n = NULL;
1519ec7b004SRick Macklem 	int off = 0;
1529ec7b004SRick Macklem 
1539ec7b004SRick Macklem 	++nfs_realign_test;
1549ec7b004SRick Macklem 	while ((m = *pm) != NULL) {
1559ec7b004SRick Macklem 		if ((m->m_len & 0x3) || (mtod(m, intptr_t) & 0x3)) {
1569ec7b004SRick Macklem 			MGET(n, M_WAIT, MT_DATA);
1579ec7b004SRick Macklem 			if (m->m_len >= MINCLSIZE) {
1589ec7b004SRick Macklem 				MCLGET(n, M_WAIT);
1599ec7b004SRick Macklem 			}
1609ec7b004SRick Macklem 			n->m_len = 0;
1619ec7b004SRick Macklem 			break;
1629ec7b004SRick Macklem 		}
1639ec7b004SRick Macklem 		pm = &m->m_next;
1649ec7b004SRick Macklem 	}
1659ec7b004SRick Macklem 
1669ec7b004SRick Macklem 	/*
1679ec7b004SRick Macklem 	 * If n is non-NULL, loop on m copying data, then replace the
1689ec7b004SRick Macklem 	 * portion of the chain that had to be realigned.
1699ec7b004SRick Macklem 	 */
1709ec7b004SRick Macklem 	if (n != NULL) {
1719ec7b004SRick Macklem 		++nfs_realign_count;
1729ec7b004SRick Macklem 		while (m) {
1739ec7b004SRick Macklem 			m_copyback(n, off, m->m_len, mtod(m, caddr_t));
1749ec7b004SRick Macklem 			off += m->m_len;
1759ec7b004SRick Macklem 			m = m->m_next;
1769ec7b004SRick Macklem 		}
1779ec7b004SRick Macklem 		m_freem(*pm);
1789ec7b004SRick Macklem 		*pm = n;
1799ec7b004SRick Macklem 	}
1809ec7b004SRick Macklem }
1819ec7b004SRick Macklem #endif	/* newnfs_realign */
1829ec7b004SRick Macklem 
1839ec7b004SRick Macklem #ifdef notdef
1849ec7b004SRick Macklem static void
1859ec7b004SRick Macklem nfsrv_object_create(struct vnode *vp, struct thread *td)
1869ec7b004SRick Macklem {
1879ec7b004SRick Macklem 
1889ec7b004SRick Macklem 	if (vp == NULL || vp->v_type != VREG)
1899ec7b004SRick Macklem 		return;
1909ec7b004SRick Macklem 	(void) vfs_object_create(vp, td, td->td_ucred);
1919ec7b004SRick Macklem }
1929ec7b004SRick Macklem #endif
1939ec7b004SRick Macklem 
1949ec7b004SRick Macklem /*
1959ec7b004SRick Macklem  * Look up a file name. Basically just initialize stuff and call namei().
1969ec7b004SRick Macklem  */
1979ec7b004SRick Macklem int
1989ec7b004SRick Macklem nfsrv_lookupfilename(struct nameidata *ndp, char *fname, NFSPROC_T *p)
1999ec7b004SRick Macklem {
2009ec7b004SRick Macklem 	int error;
2019ec7b004SRick Macklem 
2029ec7b004SRick Macklem 	NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, fname, p);
2039ec7b004SRick Macklem 	error = namei(ndp);
2049ec7b004SRick Macklem 	if (!error) {
2059ec7b004SRick Macklem 		NDFREE(ndp, NDF_ONLY_PNBUF);
2069ec7b004SRick Macklem 	}
2079ec7b004SRick Macklem 	return (error);
2089ec7b004SRick Macklem }
2099ec7b004SRick Macklem 
2109ec7b004SRick Macklem /*
2119ec7b004SRick Macklem  * Copy NFS uid, gids to the cred structure.
2129ec7b004SRick Macklem  */
2139ec7b004SRick Macklem void
2149ec7b004SRick Macklem newnfs_copycred(struct nfscred *nfscr, struct ucred *cr)
2159ec7b004SRick Macklem {
2169ec7b004SRick Macklem 	int ngroups, i;
2179ec7b004SRick Macklem 
2189ec7b004SRick Macklem 	cr->cr_uid = nfscr->nfsc_uid;
2199ec7b004SRick Macklem 	ngroups = (nfscr->nfsc_ngroups < NGROUPS) ?
2209ec7b004SRick Macklem 	    nfscr->nfsc_ngroups : NGROUPS;
2219ec7b004SRick Macklem 	for (i = 0; i < ngroups; i++)
2229ec7b004SRick Macklem 		cr->cr_groups[i] = nfscr->nfsc_groups[i];
2239ec7b004SRick Macklem 	cr->cr_ngroups = ngroups;
2249ec7b004SRick Macklem }
2259ec7b004SRick Macklem 
2269ec7b004SRick Macklem /*
2279ec7b004SRick Macklem  * Map args from nfsmsleep() to msleep().
2289ec7b004SRick Macklem  */
2299ec7b004SRick Macklem int
2309ec7b004SRick Macklem nfsmsleep(void *chan, void *mutex, int prio, const char *wmesg,
2319ec7b004SRick Macklem     struct timespec *ts)
2329ec7b004SRick Macklem {
2339ec7b004SRick Macklem 	u_int64_t nsecval;
2349ec7b004SRick Macklem 	int error, timeo;
2359ec7b004SRick Macklem 
2369ec7b004SRick Macklem 	if (ts) {
2379ec7b004SRick Macklem 		timeo = hz * ts->tv_sec;
2389ec7b004SRick Macklem 		nsecval = (u_int64_t)ts->tv_nsec;
2399ec7b004SRick Macklem 		nsecval = ((nsecval * ((u_int64_t)hz)) + 500000000) /
2409ec7b004SRick Macklem 		    1000000000;
2419ec7b004SRick Macklem 		timeo += (int)nsecval;
2429ec7b004SRick Macklem 	} else {
2439ec7b004SRick Macklem 		timeo = 0;
2449ec7b004SRick Macklem 	}
2459ec7b004SRick Macklem 	error = msleep(chan, (struct mtx *)mutex, prio, wmesg, timeo);
2469ec7b004SRick Macklem 	return (error);
2479ec7b004SRick Macklem }
2489ec7b004SRick Macklem 
2499ec7b004SRick Macklem /*
2509ec7b004SRick Macklem  * Get the file system info for the server. For now, just assume FFS.
2519ec7b004SRick Macklem  */
2529ec7b004SRick Macklem void
2539ec7b004SRick Macklem nfsvno_getfs(struct nfsfsinfo *sip, int isdgram)
2549ec7b004SRick Macklem {
2559ec7b004SRick Macklem 	int pref;
2569ec7b004SRick Macklem 
2579ec7b004SRick Macklem 	/*
2589ec7b004SRick Macklem 	 * XXX
2599ec7b004SRick Macklem 	 * There should be file system VFS OP(s) to get this information.
2609ec7b004SRick Macklem 	 * For now, assume ufs.
2619ec7b004SRick Macklem 	 */
2629ec7b004SRick Macklem 	if (isdgram)
2639ec7b004SRick Macklem 		pref = NFS_MAXDGRAMDATA;
2649ec7b004SRick Macklem 	else
2659ec7b004SRick Macklem 		pref = NFS_MAXDATA;
2669ec7b004SRick Macklem 	sip->fs_rtmax = NFS_MAXDATA;
2679ec7b004SRick Macklem 	sip->fs_rtpref = pref;
2689ec7b004SRick Macklem 	sip->fs_rtmult = NFS_FABLKSIZE;
2699ec7b004SRick Macklem 	sip->fs_wtmax = NFS_MAXDATA;
2709ec7b004SRick Macklem 	sip->fs_wtpref = pref;
2719ec7b004SRick Macklem 	sip->fs_wtmult = NFS_FABLKSIZE;
2729ec7b004SRick Macklem 	sip->fs_dtpref = pref;
2739ec7b004SRick Macklem 	sip->fs_maxfilesize = 0xffffffffffffffffull;
2749ec7b004SRick Macklem 	sip->fs_timedelta.tv_sec = 0;
2759ec7b004SRick Macklem 	sip->fs_timedelta.tv_nsec = 1;
2769ec7b004SRick Macklem 	sip->fs_properties = (NFSV3FSINFO_LINK |
2779ec7b004SRick Macklem 	    NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
2789ec7b004SRick Macklem 	    NFSV3FSINFO_CANSETTIME);
2799ec7b004SRick Macklem }
2809ec7b004SRick Macklem 
2819ec7b004SRick Macklem /* Fake nfsrv_atroot. Just return 0 */
2829ec7b004SRick Macklem int
2839ec7b004SRick Macklem nfsrv_atroot(struct vnode *vp, long *retp)
2849ec7b004SRick Macklem {
2859ec7b004SRick Macklem 
2869ec7b004SRick Macklem 	return (0);
2879ec7b004SRick Macklem }
2889ec7b004SRick Macklem 
2899ec7b004SRick Macklem /*
2909ec7b004SRick Macklem  * Set the credentials to refer to root.
2919ec7b004SRick Macklem  * If only the various BSDen could agree on whether cr_gid is a separate
2929ec7b004SRick Macklem  * field or cr_groups[0]...
2939ec7b004SRick Macklem  */
2949ec7b004SRick Macklem void
2959ec7b004SRick Macklem newnfs_setroot(struct ucred *cred)
2969ec7b004SRick Macklem {
2979ec7b004SRick Macklem 
2989ec7b004SRick Macklem 	cred->cr_uid = 0;
2999ec7b004SRick Macklem 	cred->cr_groups[0] = 0;
3009ec7b004SRick Macklem 	cred->cr_ngroups = 1;
3019ec7b004SRick Macklem }
3029ec7b004SRick Macklem 
3039ec7b004SRick Macklem /*
3049ec7b004SRick Macklem  * Get the client credential. Used for Renew and recovery.
3059ec7b004SRick Macklem  */
3069ec7b004SRick Macklem struct ucred *
3079ec7b004SRick Macklem newnfs_getcred(void)
3089ec7b004SRick Macklem {
3099ec7b004SRick Macklem 	struct ucred *cred;
3109ec7b004SRick Macklem 	struct thread *td = curthread;
3119ec7b004SRick Macklem 
3129ec7b004SRick Macklem 	cred = crdup(td->td_ucred);
3139ec7b004SRick Macklem 	newnfs_setroot(cred);
3149ec7b004SRick Macklem 	return (cred);
3159ec7b004SRick Macklem }
3169ec7b004SRick Macklem 
3179ec7b004SRick Macklem /*
3189ec7b004SRick Macklem  * Nfs timer routine
3199ec7b004SRick Macklem  * Call the nfsd's timer function once/sec.
3209ec7b004SRick Macklem  */
3219ec7b004SRick Macklem void
3229ec7b004SRick Macklem newnfs_timer(void *arg)
3239ec7b004SRick Macklem {
3249ec7b004SRick Macklem 	static time_t lasttime = 0;
3259ec7b004SRick Macklem 	/*
3269ec7b004SRick Macklem 	 * Call the server timer, if set up.
3279ec7b004SRick Macklem 	 * The argument indicates if it is the next second and therefore
3289ec7b004SRick Macklem 	 * leases should be checked.
3299ec7b004SRick Macklem 	 */
3309ec7b004SRick Macklem 	if (lasttime != NFSD_MONOSEC) {
3319ec7b004SRick Macklem 		lasttime = NFSD_MONOSEC;
3329ec7b004SRick Macklem 		if (nfsd_call_servertimer != NULL)
3339ec7b004SRick Macklem 			(*nfsd_call_servertimer)();
3349ec7b004SRick Macklem 	}
3359ec7b004SRick Macklem 	callout_reset(&newnfsd_callout, nfscl_ticks, newnfs_timer, NULL);
3369ec7b004SRick Macklem }
3379ec7b004SRick Macklem 
3389ec7b004SRick Macklem 
3399ec7b004SRick Macklem /*
3409ec7b004SRick Macklem  * sleep for a short period of time.
3419ec7b004SRick Macklem  * Since lbolt doesn't exist in FreeBSD-CURRENT, just use a timeout on
3429ec7b004SRick Macklem  * an event that never gets a wakeup. Only return EINTR or 0.
3439ec7b004SRick Macklem  */
3449ec7b004SRick Macklem int
3459ec7b004SRick Macklem nfs_catnap(int prio, const char *wmesg)
3469ec7b004SRick Macklem {
3479ec7b004SRick Macklem 	static int non_event;
3489ec7b004SRick Macklem 	int ret;
3499ec7b004SRick Macklem 
3509ec7b004SRick Macklem 	ret = tsleep(&non_event, prio, wmesg, 1);
3519ec7b004SRick Macklem 	if (ret != EINTR)
3529ec7b004SRick Macklem 		ret = 0;
3539ec7b004SRick Macklem 	return (ret);
3549ec7b004SRick Macklem }
3559ec7b004SRick Macklem 
3569ec7b004SRick Macklem /*
3579ec7b004SRick Macklem  * Get referral. For now, just fail.
3589ec7b004SRick Macklem  */
3599ec7b004SRick Macklem struct nfsreferral *
3609ec7b004SRick Macklem nfsv4root_getreferral(struct vnode *vp, struct vnode *dvp, u_int32_t fileno)
3619ec7b004SRick Macklem {
3629ec7b004SRick Macklem 
3639ec7b004SRick Macklem 	return (NULL);
3649ec7b004SRick Macklem }
3659ec7b004SRick Macklem 
3669ec7b004SRick Macklem static int
3679ec7b004SRick Macklem nfssvc_nfscommon(struct thread *td, struct nfssvc_args *uap)
3689ec7b004SRick Macklem {
3699ec7b004SRick Macklem 	int error;
3709ec7b004SRick Macklem 
3719ec7b004SRick Macklem 	error = nfssvc_call(td, uap, td->td_ucred);
3729ec7b004SRick Macklem 	return (error);
3739ec7b004SRick Macklem }
3749ec7b004SRick Macklem 
3759ec7b004SRick Macklem static int
3769ec7b004SRick Macklem nfssvc_call(struct thread *p, struct nfssvc_args *uap, struct ucred *cred)
3779ec7b004SRick Macklem {
3789ec7b004SRick Macklem 	int error = EINVAL;
3799ec7b004SRick Macklem 	struct nfsd_idargs nid;
3809ec7b004SRick Macklem 
3819ec7b004SRick Macklem 	if (uap->flag & NFSSVC_IDNAME) {
3829ec7b004SRick Macklem 		error = copyin(uap->argp, (caddr_t)&nid, sizeof (nid));
3839ec7b004SRick Macklem 		if (error)
3849ec7b004SRick Macklem 			return (error);
3859ec7b004SRick Macklem 		error = nfssvc_idname(&nid);
3869ec7b004SRick Macklem 		return (error);
3879ec7b004SRick Macklem 	} else if (uap->flag & NFSSVC_GETSTATS) {
3889ec7b004SRick Macklem 		error = copyout(&newnfsstats,
3899ec7b004SRick Macklem 		    CAST_USER_ADDR_T(uap->argp), sizeof (newnfsstats));
3909ec7b004SRick Macklem 		return (error);
3919ec7b004SRick Macklem 	} else if (uap->flag & NFSSVC_NFSUSERDPORT) {
3929ec7b004SRick Macklem 		u_short sockport;
3939ec7b004SRick Macklem 
3949ec7b004SRick Macklem 		error = copyin(uap->argp, (caddr_t)&sockport,
3959ec7b004SRick Macklem 		    sizeof (u_short));
3969ec7b004SRick Macklem 		if (!error)
3979ec7b004SRick Macklem 			error = nfsrv_nfsuserdport(sockport, p);
3989ec7b004SRick Macklem 	} else if (uap->flag & NFSSVC_NFSUSERDDELPORT) {
3999ec7b004SRick Macklem 		nfsrv_nfsuserddelport();
4009ec7b004SRick Macklem 		error = 0;
4019ec7b004SRick Macklem 	}
4029ec7b004SRick Macklem 	return (error);
4039ec7b004SRick Macklem }
4049ec7b004SRick Macklem 
4059ec7b004SRick Macklem /*
4069ec7b004SRick Macklem  * called by all three modevent routines, so that it gets things
4079ec7b004SRick Macklem  * initialized soon enough.
4089ec7b004SRick Macklem  */
4099ec7b004SRick Macklem void
4109ec7b004SRick Macklem newnfs_portinit(void)
4119ec7b004SRick Macklem {
4129ec7b004SRick Macklem 	static int inited = 0;
4139ec7b004SRick Macklem 
4149ec7b004SRick Macklem 	if (inited)
4159ec7b004SRick Macklem 		return;
4169ec7b004SRick Macklem 	inited = 1;
4179ec7b004SRick Macklem 	/* Initialize SMP locks used by both client and server. */
4189ec7b004SRick Macklem 	mtx_init(&newnfsd_mtx, "newnfsd_mtx", NULL, MTX_DEF);
4199ec7b004SRick Macklem 	mtx_init(&nfs_state_mutex, "nfs_state_mutex", NULL, MTX_DEF);
4209ec7b004SRick Macklem }
4219ec7b004SRick Macklem 
4229ec7b004SRick Macklem extern int (*nfsd_call_nfscommon)(struct thread *, struct nfssvc_args *);
4239ec7b004SRick Macklem 
4249ec7b004SRick Macklem /*
4259ec7b004SRick Macklem  * Called once to initialize data structures...
4269ec7b004SRick Macklem  */
4279ec7b004SRick Macklem static int
4289ec7b004SRick Macklem nfscommon_modevent(module_t mod, int type, void *data)
4299ec7b004SRick Macklem {
4309ec7b004SRick Macklem 	int error = 0;
4319ec7b004SRick Macklem 	static int loaded = 0;
4329ec7b004SRick Macklem 
4339ec7b004SRick Macklem 	switch (type) {
4349ec7b004SRick Macklem 	case MOD_LOAD:
4359ec7b004SRick Macklem 		if (loaded)
4369ec7b004SRick Macklem 			return (0);
4379ec7b004SRick Macklem 		newnfs_portinit();
4389ec7b004SRick Macklem 		mtx_init(&nfs_nameid_mutex, "nfs_nameid_mutex", NULL, MTX_DEF);
4399ec7b004SRick Macklem 		mtx_init(&nfs_sockl_mutex, "nfs_sockl_mutex", NULL, MTX_DEF);
4409ec7b004SRick Macklem 		mtx_init(&nfs_slock_mutex, "nfs_slock_mutex", NULL, MTX_DEF);
4419ec7b004SRick Macklem 		mtx_init(&nfs_req_mutex, "nfs_req_mutex", NULL, MTX_DEF);
4429ec7b004SRick Macklem 		mtx_init(&nfsrv_nfsuserdsock.nr_mtx, "nfsuserd", NULL,
4439ec7b004SRick Macklem 		    MTX_DEF);
4449ec7b004SRick Macklem 		callout_init(&newnfsd_callout, CALLOUT_MPSAFE);
4459ec7b004SRick Macklem 		newnfs_init();
4469ec7b004SRick Macklem 		nfsd_call_nfscommon = nfssvc_nfscommon;
4479ec7b004SRick Macklem 		loaded = 1;
4489ec7b004SRick Macklem 		break;
4499ec7b004SRick Macklem 
4509ec7b004SRick Macklem 	case MOD_UNLOAD:
4519ec7b004SRick Macklem 		if (newnfs_numnfsd != 0 || nfsrv_nfsuserd != 0 ||
4529ec7b004SRick Macklem 		    nfs_numnfscbd != 0) {
4539ec7b004SRick Macklem 			error = EBUSY;
4549ec7b004SRick Macklem 			break;
4559ec7b004SRick Macklem 		}
4569ec7b004SRick Macklem 
4579ec7b004SRick Macklem 		nfsd_call_nfscommon = NULL;
4589ec7b004SRick Macklem 		callout_drain(&newnfsd_callout);
4599ec7b004SRick Macklem 		/* and get rid of the mutexes */
4609ec7b004SRick Macklem 		mtx_destroy(&nfs_nameid_mutex);
4619ec7b004SRick Macklem 		mtx_destroy(&newnfsd_mtx);
4629ec7b004SRick Macklem 		mtx_destroy(&nfs_state_mutex);
4639ec7b004SRick Macklem 		mtx_destroy(&nfs_sockl_mutex);
4649ec7b004SRick Macklem 		mtx_destroy(&nfs_slock_mutex);
4659ec7b004SRick Macklem 		mtx_destroy(&nfs_req_mutex);
4669ec7b004SRick Macklem 		mtx_destroy(&nfsrv_nfsuserdsock.nr_mtx);
4679ec7b004SRick Macklem 		loaded = 0;
4689ec7b004SRick Macklem 		break;
4699ec7b004SRick Macklem 	default:
4709ec7b004SRick Macklem 		error = EOPNOTSUPP;
4719ec7b004SRick Macklem 		break;
4729ec7b004SRick Macklem 	}
4739ec7b004SRick Macklem 	return error;
4749ec7b004SRick Macklem }
4759ec7b004SRick Macklem static moduledata_t nfscommon_mod = {
4769ec7b004SRick Macklem 	"nfscommon",
4779ec7b004SRick Macklem 	nfscommon_modevent,
4789ec7b004SRick Macklem 	NULL,
4799ec7b004SRick Macklem };
4809ec7b004SRick Macklem DECLARE_MODULE(nfscommon, nfscommon_mod, SI_SUB_VFS, SI_ORDER_ANY);
4819ec7b004SRick Macklem 
4829ec7b004SRick Macklem /* So that loader and kldload(2) can find us, wherever we are.. */
4839ec7b004SRick Macklem MODULE_VERSION(nfscommon, 1);
4849ec7b004SRick Macklem MODULE_DEPEND(nfscommon, nfssvc, 1, 1, 1);
4859ec7b004SRick Macklem MODULE_DEPEND(nfscommon, krpc, 1, 1, 1);
4869ec7b004SRick Macklem 
487