xref: /freebsd/sys/fs/nfs/nfs_commonport.c (revision dec8894b452dd67c33902cf30ff431fe9c830bb8)
19ec7b004SRick Macklem /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
49ec7b004SRick Macklem  * Copyright (c) 1989, 1993
59ec7b004SRick Macklem  *	The Regents of the University of California.  All rights reserved.
69ec7b004SRick Macklem  *
79ec7b004SRick Macklem  * This code is derived from software contributed to Berkeley by
89ec7b004SRick Macklem  * Rick Macklem at The University of Guelph.
99ec7b004SRick Macklem  *
109ec7b004SRick Macklem  * Redistribution and use in source and binary forms, with or without
119ec7b004SRick Macklem  * modification, are permitted provided that the following conditions
129ec7b004SRick Macklem  * are met:
139ec7b004SRick Macklem  * 1. Redistributions of source code must retain the above copyright
149ec7b004SRick Macklem  *    notice, this list of conditions and the following disclaimer.
159ec7b004SRick Macklem  * 2. Redistributions in binary form must reproduce the above copyright
169ec7b004SRick Macklem  *    notice, this list of conditions and the following disclaimer in the
179ec7b004SRick Macklem  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
199ec7b004SRick Macklem  *    may be used to endorse or promote products derived from this software
209ec7b004SRick Macklem  *    without specific prior written permission.
219ec7b004SRick Macklem  *
229ec7b004SRick Macklem  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
239ec7b004SRick Macklem  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
249ec7b004SRick Macklem  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
259ec7b004SRick Macklem  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
269ec7b004SRick Macklem  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
279ec7b004SRick Macklem  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
289ec7b004SRick Macklem  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
299ec7b004SRick Macklem  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
309ec7b004SRick Macklem  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
319ec7b004SRick Macklem  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
329ec7b004SRick Macklem  * SUCH DAMAGE.
339ec7b004SRick Macklem  *
349ec7b004SRick Macklem  */
359ec7b004SRick Macklem 
369ec7b004SRick Macklem #include <sys/cdefs.h>
379ec7b004SRick Macklem __FBSDID("$FreeBSD$");
389ec7b004SRick Macklem 
399ec7b004SRick Macklem /*
409ec7b004SRick Macklem  * Functions that need to be different for different versions of BSD
419ec7b004SRick Macklem  * kernel should be kept here, along with any global storage specific
429ec7b004SRick Macklem  * to this BSD variant.
439ec7b004SRick Macklem  */
449ec7b004SRick Macklem #include <fs/nfs/nfsport.h>
45f49c813cSRick Macklem #include <sys/smp.h>
469ec7b004SRick Macklem #include <sys/sysctl.h>
47f49c813cSRick Macklem #include <sys/taskqueue.h>
4825d694a6SRick Macklem #include <rpc/rpc_com.h>
499ec7b004SRick Macklem #include <vm/vm.h>
509ec7b004SRick Macklem #include <vm/vm_object.h>
519ec7b004SRick Macklem #include <vm/vm_page.h>
529ec7b004SRick Macklem #include <vm/vm_param.h>
539ec7b004SRick Macklem #include <vm/vm_map.h>
549ec7b004SRick Macklem #include <vm/vm_kern.h>
559ec7b004SRick Macklem #include <vm/vm_extern.h>
569ec7b004SRick Macklem #include <vm/uma.h>
579ec7b004SRick Macklem 
589ec7b004SRick Macklem extern int nfscl_ticks;
599ec7b004SRick Macklem extern int nfsrv_nfsuserd;
609ec7b004SRick Macklem extern struct nfssockreq nfsrv_nfsuserdsock;
619ec7b004SRick Macklem extern void (*nfsd_call_recall)(struct vnode *, int, struct ucred *,
629ec7b004SRick Macklem     struct thread *);
639ec7b004SRick Macklem extern int nfsrv_useacl;
649ec7b004SRick Macklem struct mount nfsv4root_mnt;
659ec7b004SRick Macklem int newnfs_numnfsd = 0;
661b819cf2SRick Macklem struct nfsstatsv1 nfsstatsv1;
679ec7b004SRick Macklem int nfs_numnfscbd = 0;
68f4e2c07eSRick Macklem int nfscl_debuglevel = 0;
699ec7b004SRick Macklem char nfsv4_callbackaddr[INET6_ADDRSTRLEN];
709ec7b004SRick Macklem struct callout newnfsd_callout;
7184be7e09SRick Macklem int nfsrv_lughashsize = 100;
729ec7b004SRick Macklem void (*nfsd_call_servertimer)(void) = NULL;
739ec7b004SRick Macklem void (*ncl_call_invalcaches)(struct vnode *) = NULL;
749ec7b004SRick Macklem 
75f49c813cSRick Macklem int nfs_pnfsio(task_fn_t *, void *);
76f49c813cSRick Macklem 
779ec7b004SRick Macklem static int nfs_realign_test;
789ec7b004SRick Macklem static int nfs_realign_count;
791b819cf2SRick Macklem static struct ext_nfsstats oldnfsstats;
809ec7b004SRick Macklem 
8150a220c6SEdward Tomasz Napierala SYSCTL_NODE(_vfs, OID_AUTO, nfs, CTLFLAG_RW, 0, "NFS filesystem");
821f376590SRick Macklem SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_test, CTLFLAG_RW, &nfs_realign_test,
830a9f005dSRick Macklem     0, "Number of realign tests done");
841f376590SRick Macklem SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_count, CTLFLAG_RW, &nfs_realign_count,
850a9f005dSRick Macklem     0, "Number of mbuf realignments done");
861f376590SRick Macklem SYSCTL_STRING(_vfs_nfs, OID_AUTO, callback_addr, CTLFLAG_RW,
870a9f005dSRick Macklem     nfsv4_callbackaddr, sizeof(nfsv4_callbackaddr),
880a9f005dSRick Macklem     "NFSv4 callback addr for server to use");
89f4e2c07eSRick Macklem SYSCTL_INT(_vfs_nfs, OID_AUTO, debuglevel, CTLFLAG_RW, &nfscl_debuglevel,
9050a220c6SEdward Tomasz Napierala     0, "Debug level for NFS client");
9184be7e09SRick Macklem SYSCTL_INT(_vfs_nfs, OID_AUTO, userhashsize, CTLFLAG_RDTUN, &nfsrv_lughashsize,
9284be7e09SRick Macklem     0, "Size of hash tables for uid/name mapping");
93*dec8894bSRick Macklem int nfs_pnfsiothreads = -1;
94f49c813cSRick Macklem SYSCTL_INT(_vfs_nfs, OID_AUTO, pnfsiothreads, CTLFLAG_RW, &nfs_pnfsiothreads,
95f49c813cSRick Macklem     0, "Number of pNFS mirror I/O threads");
969ec7b004SRick Macklem 
979ec7b004SRick Macklem /*
989ec7b004SRick Macklem  * Defines for malloc
999ec7b004SRick Macklem  * (Here for FreeBSD, since they allocate storage.)
1009ec7b004SRick Macklem  */
1019ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSRVCACHE, "NFSD srvcache", "NFSD Server Request Cache");
1029ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSDCLIENT, "NFSD V4client", "NFSD V4 Client Id");
103a43fcbe3SRick Macklem MALLOC_DEFINE(M_NEWNFSDSTATE, "NFSD V4state",
104a43fcbe3SRick Macklem     "NFSD V4 State (Openowner, Open, Lockowner, Delegation");
1059ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSDLOCK, "NFSD V4lock", "NFSD V4 byte range lock");
1069ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSDLOCKFILE, "NFSD lckfile", "NFSD Open/Lock file");
1079ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSSTRING, "NFSD string", "NFSD V4 long string");
1089ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSUSERGROUP, "NFSD usrgroup", "NFSD V4 User/group map");
1099ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSDREQ, "NFS req", "NFS request header");
1109ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSFH, "NFS fh", "NFS file handle");
1119ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSCLOWNER, "NFSCL owner", "NFSCL Open Owner");
1129ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSCLOPEN, "NFSCL open", "NFSCL Open");
1139ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSCLDELEG, "NFSCL deleg", "NFSCL Delegation");
1149ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSCLCLIENT, "NFSCL client", "NFSCL Client");
1159ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSCLLOCKOWNER, "NFSCL lckown", "NFSCL Lock Owner");
1169ec7b004SRick Macklem MALLOC_DEFINE(M_NEWNFSCLLOCK, "NFSCL lck", "NFSCL Lock");
11750a220c6SEdward Tomasz Napierala MALLOC_DEFINE(M_NEWNFSV4NODE, "NEWNFSnode", "NFS vnode");
11850a220c6SEdward Tomasz Napierala MALLOC_DEFINE(M_NEWNFSDIRECTIO, "NEWdirectio", "NFS Direct IO buffer");
119a43fcbe3SRick Macklem MALLOC_DEFINE(M_NEWNFSDIROFF, "NFSCL diroffdiroff",
12050a220c6SEdward Tomasz Napierala     "NFS directory offset data");
121a43fcbe3SRick Macklem MALLOC_DEFINE(M_NEWNFSDROLLBACK, "NFSD rollback",
12250a220c6SEdward Tomasz Napierala     "NFS local lock rollback");
1231f60bfd8SRick Macklem MALLOC_DEFINE(M_NEWNFSLAYOUT, "NFSCL layout", "NFSv4.1 Layout");
1241f60bfd8SRick Macklem MALLOC_DEFINE(M_NEWNFSFLAYOUT, "NFSCL flayout", "NFSv4.1 File Layout");
1251f60bfd8SRick Macklem MALLOC_DEFINE(M_NEWNFSDEVINFO, "NFSCL devinfo", "NFSv4.1 Device Info");
1261f60bfd8SRick Macklem MALLOC_DEFINE(M_NEWNFSSOCKREQ, "NFSCL sockreq", "NFS Sock Req");
1271f60bfd8SRick Macklem MALLOC_DEFINE(M_NEWNFSCLDS, "NFSCL session", "NFSv4.1 Session");
1281f60bfd8SRick Macklem MALLOC_DEFINE(M_NEWNFSLAYRECALL, "NFSCL layrecall", "NFSv4.1 Layout Recall");
129c59e4cc3SRick Macklem MALLOC_DEFINE(M_NEWNFSDSESSION, "NFSD session", "NFSD Session for a client");
1309ec7b004SRick Macklem 
1319ec7b004SRick Macklem /*
1329ec7b004SRick Macklem  * Definition of mutex locks.
1339ec7b004SRick Macklem  * newnfsd_mtx is used in nfsrvd_nfsd() to protect the nfs socket list
1349ec7b004SRick Macklem  * and assorted other nfsd structures.
1359ec7b004SRick Macklem  */
1369ec7b004SRick Macklem struct mtx newnfsd_mtx;
1379ec7b004SRick Macklem struct mtx nfs_sockl_mutex;
1389ec7b004SRick Macklem struct mtx nfs_state_mutex;
1399ec7b004SRick Macklem struct mtx nfs_nameid_mutex;
1409ec7b004SRick Macklem struct mtx nfs_req_mutex;
1419ec7b004SRick Macklem struct mtx nfs_slock_mutex;
142b2fc0141SRick Macklem struct mtx nfs_clstate_mutex;
1439ec7b004SRick Macklem 
1449ec7b004SRick Macklem /* local functions */
1459ec7b004SRick Macklem static int nfssvc_call(struct thread *, struct nfssvc_args *, struct ucred *);
1469ec7b004SRick Macklem 
1473dfe81c6SRick Macklem #ifdef __NO_STRICT_ALIGNMENT
1489ec7b004SRick Macklem /*
1499ec7b004SRick Macklem  * These architectures don't need re-alignment, so just return.
1509ec7b004SRick Macklem  */
151d96b98a3SKenneth D. Merry int
152d96b98a3SKenneth D. Merry newnfs_realign(struct mbuf **pm, int how)
1539ec7b004SRick Macklem {
1549ec7b004SRick Macklem 
155d96b98a3SKenneth D. Merry 	return (0);
1569ec7b004SRick Macklem }
1573dfe81c6SRick Macklem #else	/* !__NO_STRICT_ALIGNMENT */
1589ec7b004SRick Macklem /*
15905c965a2SRick Macklem  *	newnfs_realign:
1609ec7b004SRick Macklem  *
1619ec7b004SRick Macklem  *	Check for badly aligned mbuf data and realign by copying the unaligned
1629ec7b004SRick Macklem  *	portion of the data into a new mbuf chain and freeing the portions
1639ec7b004SRick Macklem  *	of the old chain that were replaced.
1649ec7b004SRick Macklem  *
1659ec7b004SRick Macklem  *	We cannot simply realign the data within the existing mbuf chain
1669ec7b004SRick Macklem  *	because the underlying buffers may contain other rpc commands and
1679ec7b004SRick Macklem  *	we cannot afford to overwrite them.
1689ec7b004SRick Macklem  *
1699ec7b004SRick Macklem  *	We would prefer to avoid this situation entirely.  The situation does
170a96c9b30SPedro F. Giffuni  *	not occur with NFS/UDP and is supposed to only occasionally occur
1719ec7b004SRick Macklem  *	with TCP.  Use vfs.nfs.realign_count and realign_test to check this.
17205c965a2SRick Macklem  *
1739ec7b004SRick Macklem  */
174d96b98a3SKenneth D. Merry int
175d96b98a3SKenneth D. Merry newnfs_realign(struct mbuf **pm, int how)
1769ec7b004SRick Macklem {
17705c965a2SRick Macklem 	struct mbuf *m, *n;
17805c965a2SRick Macklem 	int off, space;
1799ec7b004SRick Macklem 
1809ec7b004SRick Macklem 	++nfs_realign_test;
1819ec7b004SRick Macklem 	while ((m = *pm) != NULL) {
1829ec7b004SRick Macklem 		if ((m->m_len & 0x3) || (mtod(m, intptr_t) & 0x3)) {
1839ec7b004SRick Macklem 			/*
18405c965a2SRick Macklem 			 * NB: we can't depend on m_pkthdr.len to help us
18505c965a2SRick Macklem 			 * decide what to do here.  May not be worth doing
18605c965a2SRick Macklem 			 * the m_length calculation as m_copyback will
18705c965a2SRick Macklem 			 * expand the mbuf chain below as needed.
1889ec7b004SRick Macklem 			 */
18905c965a2SRick Macklem 			space = m_length(m, NULL);
19005c965a2SRick Macklem 			if (space >= MINCLSIZE) {
19105c965a2SRick Macklem 				/* NB: m_copyback handles space > MCLBYTES */
192d96b98a3SKenneth D. Merry 				n = m_getcl(how, MT_DATA, 0);
19305c965a2SRick Macklem 			} else
194d96b98a3SKenneth D. Merry 				n = m_get(how, MT_DATA);
19505c965a2SRick Macklem 			if (n == NULL)
196d96b98a3SKenneth D. Merry 				return (ENOMEM);
19705c965a2SRick Macklem 			/*
19805c965a2SRick Macklem 			 * Align the remainder of the mbuf chain.
19905c965a2SRick Macklem 			 */
20005c965a2SRick Macklem 			n->m_len = 0;
20105c965a2SRick Macklem 			off = 0;
20205c965a2SRick Macklem 			while (m != NULL) {
2039ec7b004SRick Macklem 				m_copyback(n, off, m->m_len, mtod(m, caddr_t));
2049ec7b004SRick Macklem 				off += m->m_len;
2059ec7b004SRick Macklem 				m = m->m_next;
2069ec7b004SRick Macklem 			}
2079ec7b004SRick Macklem 			m_freem(*pm);
2089ec7b004SRick Macklem 			*pm = n;
20905c965a2SRick Macklem 			++nfs_realign_count;
21005c965a2SRick Macklem 			break;
21105c965a2SRick Macklem 		}
21205c965a2SRick Macklem 		pm = &m->m_next;
2139ec7b004SRick Macklem 	}
214d96b98a3SKenneth D. Merry 
215d96b98a3SKenneth D. Merry 	return (0);
2169ec7b004SRick Macklem }
2173dfe81c6SRick Macklem #endif	/* __NO_STRICT_ALIGNMENT */
2189ec7b004SRick Macklem 
2199ec7b004SRick Macklem #ifdef notdef
2209ec7b004SRick Macklem static void
2219ec7b004SRick Macklem nfsrv_object_create(struct vnode *vp, struct thread *td)
2229ec7b004SRick Macklem {
2239ec7b004SRick Macklem 
2249ec7b004SRick Macklem 	if (vp == NULL || vp->v_type != VREG)
2259ec7b004SRick Macklem 		return;
2269ec7b004SRick Macklem 	(void) vfs_object_create(vp, td, td->td_ucred);
2279ec7b004SRick Macklem }
2289ec7b004SRick Macklem #endif
2299ec7b004SRick Macklem 
2309ec7b004SRick Macklem /*
2319ec7b004SRick Macklem  * Look up a file name. Basically just initialize stuff and call namei().
2329ec7b004SRick Macklem  */
2339ec7b004SRick Macklem int
2349ec7b004SRick Macklem nfsrv_lookupfilename(struct nameidata *ndp, char *fname, NFSPROC_T *p)
2359ec7b004SRick Macklem {
2369ec7b004SRick Macklem 	int error;
2379ec7b004SRick Macklem 
2385050aa86SKonstantin Belousov 	NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, fname,
23966c0f45aSRick Macklem 	    p);
2409ec7b004SRick Macklem 	error = namei(ndp);
2419ec7b004SRick Macklem 	if (!error) {
2429ec7b004SRick Macklem 		NDFREE(ndp, NDF_ONLY_PNBUF);
2439ec7b004SRick Macklem 	}
2449ec7b004SRick Macklem 	return (error);
2459ec7b004SRick Macklem }
2469ec7b004SRick Macklem 
2479ec7b004SRick Macklem /*
2489ec7b004SRick Macklem  * Copy NFS uid, gids to the cred structure.
2499ec7b004SRick Macklem  */
2509ec7b004SRick Macklem void
2519ec7b004SRick Macklem newnfs_copycred(struct nfscred *nfscr, struct ucred *cr)
2529ec7b004SRick Macklem {
2539ec7b004SRick Macklem 
25455909abfSRick Macklem 	KASSERT(nfscr->nfsc_ngroups >= 0,
25555909abfSRick Macklem 	    ("newnfs_copycred: negative nfsc_ngroups"));
2569ec7b004SRick Macklem 	cr->cr_uid = nfscr->nfsc_uid;
257838d9858SBrooks Davis 	crsetgroups(cr, nfscr->nfsc_ngroups, nfscr->nfsc_groups);
2589ec7b004SRick Macklem }
2599ec7b004SRick Macklem 
2609ec7b004SRick Macklem /*
2619ec7b004SRick Macklem  * Map args from nfsmsleep() to msleep().
2629ec7b004SRick Macklem  */
2639ec7b004SRick Macklem int
2649ec7b004SRick Macklem nfsmsleep(void *chan, void *mutex, int prio, const char *wmesg,
2659ec7b004SRick Macklem     struct timespec *ts)
2669ec7b004SRick Macklem {
2679ec7b004SRick Macklem 	u_int64_t nsecval;
2689ec7b004SRick Macklem 	int error, timeo;
2699ec7b004SRick Macklem 
2709ec7b004SRick Macklem 	if (ts) {
2719ec7b004SRick Macklem 		timeo = hz * ts->tv_sec;
2729ec7b004SRick Macklem 		nsecval = (u_int64_t)ts->tv_nsec;
2739ec7b004SRick Macklem 		nsecval = ((nsecval * ((u_int64_t)hz)) + 500000000) /
2749ec7b004SRick Macklem 		    1000000000;
2759ec7b004SRick Macklem 		timeo += (int)nsecval;
2769ec7b004SRick Macklem 	} else {
2779ec7b004SRick Macklem 		timeo = 0;
2789ec7b004SRick Macklem 	}
2799ec7b004SRick Macklem 	error = msleep(chan, (struct mtx *)mutex, prio, wmesg, timeo);
2809ec7b004SRick Macklem 	return (error);
2819ec7b004SRick Macklem }
2829ec7b004SRick Macklem 
2839ec7b004SRick Macklem /*
2849ec7b004SRick Macklem  * Get the file system info for the server. For now, just assume FFS.
2859ec7b004SRick Macklem  */
2869ec7b004SRick Macklem void
2879ec7b004SRick Macklem nfsvno_getfs(struct nfsfsinfo *sip, int isdgram)
2889ec7b004SRick Macklem {
2899ec7b004SRick Macklem 	int pref;
2909ec7b004SRick Macklem 
2919ec7b004SRick Macklem 	/*
2929ec7b004SRick Macklem 	 * XXX
2939ec7b004SRick Macklem 	 * There should be file system VFS OP(s) to get this information.
2949ec7b004SRick Macklem 	 * For now, assume ufs.
2959ec7b004SRick Macklem 	 */
2969ec7b004SRick Macklem 	if (isdgram)
2979ec7b004SRick Macklem 		pref = NFS_MAXDGRAMDATA;
2989ec7b004SRick Macklem 	else
29966e80f77SRick Macklem 		pref = NFS_SRVMAXIO;
30066e80f77SRick Macklem 	sip->fs_rtmax = NFS_SRVMAXIO;
3019ec7b004SRick Macklem 	sip->fs_rtpref = pref;
3029ec7b004SRick Macklem 	sip->fs_rtmult = NFS_FABLKSIZE;
30366e80f77SRick Macklem 	sip->fs_wtmax = NFS_SRVMAXIO;
3049ec7b004SRick Macklem 	sip->fs_wtpref = pref;
3059ec7b004SRick Macklem 	sip->fs_wtmult = NFS_FABLKSIZE;
3069ec7b004SRick Macklem 	sip->fs_dtpref = pref;
3079ec7b004SRick Macklem 	sip->fs_maxfilesize = 0xffffffffffffffffull;
3089ec7b004SRick Macklem 	sip->fs_timedelta.tv_sec = 0;
3099ec7b004SRick Macklem 	sip->fs_timedelta.tv_nsec = 1;
3109ec7b004SRick Macklem 	sip->fs_properties = (NFSV3FSINFO_LINK |
3119ec7b004SRick Macklem 	    NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
3129ec7b004SRick Macklem 	    NFSV3FSINFO_CANSETTIME);
3139ec7b004SRick Macklem }
3149ec7b004SRick Macklem 
31540435b74SZack Kirsch /*
31640435b74SZack Kirsch  * Do the pathconf vnode op.
31740435b74SZack Kirsch  */
31840435b74SZack Kirsch int
319b1288166SJohn Baldwin nfsvno_pathconf(struct vnode *vp, int flag, long *retf,
32040435b74SZack Kirsch     struct ucred *cred, struct thread *p)
32140435b74SZack Kirsch {
32240435b74SZack Kirsch 	int error;
32340435b74SZack Kirsch 
32440435b74SZack Kirsch 	error = VOP_PATHCONF(vp, flag, retf);
32540435b74SZack Kirsch 	if (error == EOPNOTSUPP || error == EINVAL) {
32640435b74SZack Kirsch 		/*
32740435b74SZack Kirsch 		 * Some file systems return EINVAL for name arguments not
32840435b74SZack Kirsch 		 * supported and some return EOPNOTSUPP for this case.
32940435b74SZack Kirsch 		 * So the NFSv3 Pathconf RPC doesn't fail for these cases,
33040435b74SZack Kirsch 		 * just fake them.
33140435b74SZack Kirsch 		 */
33240435b74SZack Kirsch 		switch (flag) {
33340435b74SZack Kirsch 		case _PC_LINK_MAX:
334a0a073b1SJohn Baldwin 			*retf = NFS_LINK_MAX;
33540435b74SZack Kirsch 			break;
33640435b74SZack Kirsch 		case _PC_NAME_MAX:
33740435b74SZack Kirsch 			*retf = NAME_MAX;
33840435b74SZack Kirsch 			break;
33940435b74SZack Kirsch 		case _PC_CHOWN_RESTRICTED:
34040435b74SZack Kirsch 			*retf = 1;
34140435b74SZack Kirsch 			break;
34240435b74SZack Kirsch 		case _PC_NO_TRUNC:
34340435b74SZack Kirsch 			*retf = 1;
34440435b74SZack Kirsch 			break;
34540435b74SZack Kirsch 		default:
34640435b74SZack Kirsch 			/*
34740435b74SZack Kirsch 			 * Only happens if a _PC_xxx is added to the server,
34840435b74SZack Kirsch 			 * but this isn't updated.
34940435b74SZack Kirsch 			 */
35040435b74SZack Kirsch 			*retf = 0;
35140435b74SZack Kirsch 			printf("nfsrvd pathconf flag=%d not supp\n", flag);
35274b8d63dSPedro F. Giffuni 		}
35340435b74SZack Kirsch 		error = 0;
35440435b74SZack Kirsch 	}
355a9285ae5SZack Kirsch 	NFSEXITCODE(error);
35640435b74SZack Kirsch 	return (error);
35740435b74SZack Kirsch }
35840435b74SZack Kirsch 
3599ec7b004SRick Macklem /* Fake nfsrv_atroot. Just return 0 */
3609ec7b004SRick Macklem int
36195ac7f1aSRick Macklem nfsrv_atroot(struct vnode *vp, uint64_t *retp)
3629ec7b004SRick Macklem {
3639ec7b004SRick Macklem 
3649ec7b004SRick Macklem 	return (0);
3659ec7b004SRick Macklem }
3669ec7b004SRick Macklem 
3679ec7b004SRick Macklem /*
3689ec7b004SRick Macklem  * Set the credentials to refer to root.
3699ec7b004SRick Macklem  * If only the various BSDen could agree on whether cr_gid is a separate
3709ec7b004SRick Macklem  * field or cr_groups[0]...
3719ec7b004SRick Macklem  */
3729ec7b004SRick Macklem void
3739ec7b004SRick Macklem newnfs_setroot(struct ucred *cred)
3749ec7b004SRick Macklem {
3759ec7b004SRick Macklem 
3769ec7b004SRick Macklem 	cred->cr_uid = 0;
3779ec7b004SRick Macklem 	cred->cr_groups[0] = 0;
3789ec7b004SRick Macklem 	cred->cr_ngroups = 1;
3799ec7b004SRick Macklem }
3809ec7b004SRick Macklem 
3819ec7b004SRick Macklem /*
3829ec7b004SRick Macklem  * Get the client credential. Used for Renew and recovery.
3839ec7b004SRick Macklem  */
3849ec7b004SRick Macklem struct ucred *
3859ec7b004SRick Macklem newnfs_getcred(void)
3869ec7b004SRick Macklem {
3879ec7b004SRick Macklem 	struct ucred *cred;
3889ec7b004SRick Macklem 	struct thread *td = curthread;
3899ec7b004SRick Macklem 
3909ec7b004SRick Macklem 	cred = crdup(td->td_ucred);
3919ec7b004SRick Macklem 	newnfs_setroot(cred);
3929ec7b004SRick Macklem 	return (cred);
3939ec7b004SRick Macklem }
3949ec7b004SRick Macklem 
3959ec7b004SRick Macklem /*
3969ec7b004SRick Macklem  * Nfs timer routine
3979ec7b004SRick Macklem  * Call the nfsd's timer function once/sec.
3989ec7b004SRick Macklem  */
3999ec7b004SRick Macklem void
4009ec7b004SRick Macklem newnfs_timer(void *arg)
4019ec7b004SRick Macklem {
4029ec7b004SRick Macklem 	static time_t lasttime = 0;
4039ec7b004SRick Macklem 	/*
4049ec7b004SRick Macklem 	 * Call the server timer, if set up.
4059ec7b004SRick Macklem 	 * The argument indicates if it is the next second and therefore
4069ec7b004SRick Macklem 	 * leases should be checked.
4079ec7b004SRick Macklem 	 */
4089ec7b004SRick Macklem 	if (lasttime != NFSD_MONOSEC) {
4099ec7b004SRick Macklem 		lasttime = NFSD_MONOSEC;
4109ec7b004SRick Macklem 		if (nfsd_call_servertimer != NULL)
4119ec7b004SRick Macklem 			(*nfsd_call_servertimer)();
4129ec7b004SRick Macklem 	}
4139ec7b004SRick Macklem 	callout_reset(&newnfsd_callout, nfscl_ticks, newnfs_timer, NULL);
4149ec7b004SRick Macklem }
4159ec7b004SRick Macklem 
4169ec7b004SRick Macklem 
4179ec7b004SRick Macklem /*
41823f929dfSRick Macklem  * Sleep for a short period of time unless errval == NFSERR_GRACE, where
41923f929dfSRick Macklem  * the sleep should be for 5 seconds.
4209ec7b004SRick Macklem  * Since lbolt doesn't exist in FreeBSD-CURRENT, just use a timeout on
4219ec7b004SRick Macklem  * an event that never gets a wakeup. Only return EINTR or 0.
4229ec7b004SRick Macklem  */
4239ec7b004SRick Macklem int
42423f929dfSRick Macklem nfs_catnap(int prio, int errval, const char *wmesg)
4259ec7b004SRick Macklem {
4269ec7b004SRick Macklem 	static int non_event;
4279ec7b004SRick Macklem 	int ret;
4289ec7b004SRick Macklem 
42923f929dfSRick Macklem 	if (errval == NFSERR_GRACE)
43023f929dfSRick Macklem 		ret = tsleep(&non_event, prio, wmesg, 5 * hz);
43123f929dfSRick Macklem 	else
4329ec7b004SRick Macklem 		ret = tsleep(&non_event, prio, wmesg, 1);
4339ec7b004SRick Macklem 	if (ret != EINTR)
4349ec7b004SRick Macklem 		ret = 0;
4359ec7b004SRick Macklem 	return (ret);
4369ec7b004SRick Macklem }
4379ec7b004SRick Macklem 
4389ec7b004SRick Macklem /*
4399ec7b004SRick Macklem  * Get referral. For now, just fail.
4409ec7b004SRick Macklem  */
4419ec7b004SRick Macklem struct nfsreferral *
4429ec7b004SRick Macklem nfsv4root_getreferral(struct vnode *vp, struct vnode *dvp, u_int32_t fileno)
4439ec7b004SRick Macklem {
4449ec7b004SRick Macklem 
4459ec7b004SRick Macklem 	return (NULL);
4469ec7b004SRick Macklem }
4479ec7b004SRick Macklem 
4489ec7b004SRick Macklem static int
4499ec7b004SRick Macklem nfssvc_nfscommon(struct thread *td, struct nfssvc_args *uap)
4509ec7b004SRick Macklem {
4519ec7b004SRick Macklem 	int error;
4529ec7b004SRick Macklem 
4539ec7b004SRick Macklem 	error = nfssvc_call(td, uap, td->td_ucred);
454a9285ae5SZack Kirsch 	NFSEXITCODE(error);
4559ec7b004SRick Macklem 	return (error);
4569ec7b004SRick Macklem }
4579ec7b004SRick Macklem 
4589ec7b004SRick Macklem static int
4599ec7b004SRick Macklem nfssvc_call(struct thread *p, struct nfssvc_args *uap, struct ucred *cred)
4609ec7b004SRick Macklem {
4611b819cf2SRick Macklem 	int error = EINVAL, i, j;
4629ec7b004SRick Macklem 	struct nfsd_idargs nid;
46384be7e09SRick Macklem 	struct nfsd_oidargs onid;
4641b819cf2SRick Macklem 	struct {
4651b819cf2SRick Macklem 		int vers;	/* Just the first field of nfsstats. */
4661b819cf2SRick Macklem 	} nfsstatver;
4679ec7b004SRick Macklem 
4689ec7b004SRick Macklem 	if (uap->flag & NFSSVC_IDNAME) {
46984be7e09SRick Macklem 		if ((uap->flag & NFSSVC_NEWSTRUCT) != 0)
47084be7e09SRick Macklem 			error = copyin(uap->argp, &nid, sizeof(nid));
47184be7e09SRick Macklem 		else {
47284be7e09SRick Macklem 			error = copyin(uap->argp, &onid, sizeof(onid));
47384be7e09SRick Macklem 			if (error == 0) {
47484be7e09SRick Macklem 				nid.nid_flag = onid.nid_flag;
47584be7e09SRick Macklem 				nid.nid_uid = onid.nid_uid;
47684be7e09SRick Macklem 				nid.nid_gid = onid.nid_gid;
47784be7e09SRick Macklem 				nid.nid_usermax = onid.nid_usermax;
47884be7e09SRick Macklem 				nid.nid_usertimeout = onid.nid_usertimeout;
47984be7e09SRick Macklem 				nid.nid_name = onid.nid_name;
48084be7e09SRick Macklem 				nid.nid_namelen = onid.nid_namelen;
48184be7e09SRick Macklem 				nid.nid_ngroup = 0;
48284be7e09SRick Macklem 				nid.nid_grps = NULL;
48384be7e09SRick Macklem 			}
48484be7e09SRick Macklem 		}
4859ec7b004SRick Macklem 		if (error)
486a9285ae5SZack Kirsch 			goto out;
4879ec7b004SRick Macklem 		error = nfssvc_idname(&nid);
488a9285ae5SZack Kirsch 		goto out;
4899ec7b004SRick Macklem 	} else if (uap->flag & NFSSVC_GETSTATS) {
4901b819cf2SRick Macklem 		if ((uap->flag & NFSSVC_NEWSTRUCT) == 0) {
4911b819cf2SRick Macklem 			/* Copy fields to the old ext_nfsstat structure. */
4921b819cf2SRick Macklem 			oldnfsstats.attrcache_hits =
4931b819cf2SRick Macklem 			    nfsstatsv1.attrcache_hits;
4941b819cf2SRick Macklem 			oldnfsstats.attrcache_misses =
4951b819cf2SRick Macklem 			    nfsstatsv1.attrcache_misses;
4961b819cf2SRick Macklem 			oldnfsstats.lookupcache_hits =
4971b819cf2SRick Macklem 			    nfsstatsv1.lookupcache_hits;
4981b819cf2SRick Macklem 			oldnfsstats.lookupcache_misses =
4991b819cf2SRick Macklem 			    nfsstatsv1.lookupcache_misses;
5001b819cf2SRick Macklem 			oldnfsstats.direofcache_hits =
5011b819cf2SRick Macklem 			    nfsstatsv1.direofcache_hits;
5021b819cf2SRick Macklem 			oldnfsstats.direofcache_misses =
5031b819cf2SRick Macklem 			    nfsstatsv1.direofcache_misses;
5041b819cf2SRick Macklem 			oldnfsstats.accesscache_hits =
5051b819cf2SRick Macklem 			    nfsstatsv1.accesscache_hits;
5061b819cf2SRick Macklem 			oldnfsstats.accesscache_misses =
5071b819cf2SRick Macklem 			    nfsstatsv1.accesscache_misses;
5081b819cf2SRick Macklem 			oldnfsstats.biocache_reads =
5091b819cf2SRick Macklem 			    nfsstatsv1.biocache_reads;
5101b819cf2SRick Macklem 			oldnfsstats.read_bios =
5111b819cf2SRick Macklem 			    nfsstatsv1.read_bios;
5121b819cf2SRick Macklem 			oldnfsstats.read_physios =
5131b819cf2SRick Macklem 			    nfsstatsv1.read_physios;
5141b819cf2SRick Macklem 			oldnfsstats.biocache_writes =
5151b819cf2SRick Macklem 			    nfsstatsv1.biocache_writes;
5161b819cf2SRick Macklem 			oldnfsstats.write_bios =
5171b819cf2SRick Macklem 			    nfsstatsv1.write_bios;
5181b819cf2SRick Macklem 			oldnfsstats.write_physios =
5191b819cf2SRick Macklem 			    nfsstatsv1.write_physios;
5201b819cf2SRick Macklem 			oldnfsstats.biocache_readlinks =
5211b819cf2SRick Macklem 			    nfsstatsv1.biocache_readlinks;
5221b819cf2SRick Macklem 			oldnfsstats.readlink_bios =
5231b819cf2SRick Macklem 			    nfsstatsv1.readlink_bios;
5241b819cf2SRick Macklem 			oldnfsstats.biocache_readdirs =
5251b819cf2SRick Macklem 			    nfsstatsv1.biocache_readdirs;
5261b819cf2SRick Macklem 			oldnfsstats.readdir_bios =
5271b819cf2SRick Macklem 			    nfsstatsv1.readdir_bios;
5281b819cf2SRick Macklem 			for (i = 0; i < NFSV4_NPROCS; i++)
5291b819cf2SRick Macklem 				oldnfsstats.rpccnt[i] = nfsstatsv1.rpccnt[i];
5301b819cf2SRick Macklem 			oldnfsstats.rpcretries = nfsstatsv1.rpcretries;
5311b819cf2SRick Macklem 			for (i = 0; i < NFSV4OP_NOPS; i++)
5321b819cf2SRick Macklem 				oldnfsstats.srvrpccnt[i] =
5331b819cf2SRick Macklem 				    nfsstatsv1.srvrpccnt[i];
5341b819cf2SRick Macklem 			for (i = NFSV42_NOPS, j = NFSV4OP_NOPS;
5351b819cf2SRick Macklem 			    i < NFSV42_NOPS + NFSV4OP_FAKENOPS; i++, j++)
5361b819cf2SRick Macklem 				oldnfsstats.srvrpccnt[j] =
5371b819cf2SRick Macklem 				    nfsstatsv1.srvrpccnt[i];
5381b819cf2SRick Macklem 			oldnfsstats.srvrpc_errs = nfsstatsv1.srvrpc_errs;
5391b819cf2SRick Macklem 			oldnfsstats.srv_errs = nfsstatsv1.srv_errs;
5401b819cf2SRick Macklem 			oldnfsstats.rpcrequests = nfsstatsv1.rpcrequests;
5411b819cf2SRick Macklem 			oldnfsstats.rpctimeouts = nfsstatsv1.rpctimeouts;
5421b819cf2SRick Macklem 			oldnfsstats.rpcunexpected = nfsstatsv1.rpcunexpected;
5431b819cf2SRick Macklem 			oldnfsstats.rpcinvalid = nfsstatsv1.rpcinvalid;
5441b819cf2SRick Macklem 			oldnfsstats.srvcache_inproghits =
5451b819cf2SRick Macklem 			    nfsstatsv1.srvcache_inproghits;
5461b819cf2SRick Macklem 			oldnfsstats.srvcache_idemdonehits =
5471b819cf2SRick Macklem 			    nfsstatsv1.srvcache_idemdonehits;
5481b819cf2SRick Macklem 			oldnfsstats.srvcache_nonidemdonehits =
5491b819cf2SRick Macklem 			    nfsstatsv1.srvcache_nonidemdonehits;
5501b819cf2SRick Macklem 			oldnfsstats.srvcache_misses =
5511b819cf2SRick Macklem 			    nfsstatsv1.srvcache_misses;
5521b819cf2SRick Macklem 			oldnfsstats.srvcache_tcppeak =
5531b819cf2SRick Macklem 			    nfsstatsv1.srvcache_tcppeak;
5541b819cf2SRick Macklem 			oldnfsstats.srvcache_size = nfsstatsv1.srvcache_size;
5551b819cf2SRick Macklem 			oldnfsstats.srvclients = nfsstatsv1.srvclients;
5561b819cf2SRick Macklem 			oldnfsstats.srvopenowners = nfsstatsv1.srvopenowners;
5571b819cf2SRick Macklem 			oldnfsstats.srvopens = nfsstatsv1.srvopens;
5581b819cf2SRick Macklem 			oldnfsstats.srvlockowners = nfsstatsv1.srvlockowners;
5591b819cf2SRick Macklem 			oldnfsstats.srvlocks = nfsstatsv1.srvlocks;
5601b819cf2SRick Macklem 			oldnfsstats.srvdelegates = nfsstatsv1.srvdelegates;
5611b819cf2SRick Macklem 			for (i = 0; i < NFSV4OP_CBNOPS; i++)
5621b819cf2SRick Macklem 				oldnfsstats.cbrpccnt[i] =
5631b819cf2SRick Macklem 				    nfsstatsv1.cbrpccnt[i];
5641b819cf2SRick Macklem 			oldnfsstats.clopenowners = nfsstatsv1.clopenowners;
5651b819cf2SRick Macklem 			oldnfsstats.clopens = nfsstatsv1.clopens;
5661b819cf2SRick Macklem 			oldnfsstats.cllockowners = nfsstatsv1.cllockowners;
5671b819cf2SRick Macklem 			oldnfsstats.cllocks = nfsstatsv1.cllocks;
5681b819cf2SRick Macklem 			oldnfsstats.cldelegates = nfsstatsv1.cldelegates;
5691b819cf2SRick Macklem 			oldnfsstats.cllocalopenowners =
5701b819cf2SRick Macklem 			    nfsstatsv1.cllocalopenowners;
5711b819cf2SRick Macklem 			oldnfsstats.cllocalopens = nfsstatsv1.cllocalopens;
5721b819cf2SRick Macklem 			oldnfsstats.cllocallockowners =
5731b819cf2SRick Macklem 			    nfsstatsv1.cllocallockowners;
5741b819cf2SRick Macklem 			oldnfsstats.cllocallocks = nfsstatsv1.cllocallocks;
5751b819cf2SRick Macklem 			error = copyout(&oldnfsstats, uap->argp,
5761b819cf2SRick Macklem 			    sizeof (oldnfsstats));
5771b819cf2SRick Macklem 		} else {
5781b819cf2SRick Macklem 			error = copyin(uap->argp, &nfsstatver,
5791b819cf2SRick Macklem 			    sizeof(nfsstatver));
5801b819cf2SRick Macklem 			if (error == 0 && nfsstatver.vers != NFSSTATS_V1)
5811b819cf2SRick Macklem 				error = EPERM;
5821b819cf2SRick Macklem 			if (error == 0)
5831b819cf2SRick Macklem 				error = copyout(&nfsstatsv1, uap->argp,
5841b819cf2SRick Macklem 				    sizeof (nfsstatsv1));
5851b819cf2SRick Macklem 		}
5862e3b981aSRick Macklem 		if (error == 0) {
5872e3b981aSRick Macklem 			if ((uap->flag & NFSSVC_ZEROCLTSTATS) != 0) {
5881b819cf2SRick Macklem 				nfsstatsv1.attrcache_hits = 0;
5891b819cf2SRick Macklem 				nfsstatsv1.attrcache_misses = 0;
5901b819cf2SRick Macklem 				nfsstatsv1.lookupcache_hits = 0;
5911b819cf2SRick Macklem 				nfsstatsv1.lookupcache_misses = 0;
5921b819cf2SRick Macklem 				nfsstatsv1.direofcache_hits = 0;
5931b819cf2SRick Macklem 				nfsstatsv1.direofcache_misses = 0;
5941b819cf2SRick Macklem 				nfsstatsv1.accesscache_hits = 0;
5951b819cf2SRick Macklem 				nfsstatsv1.accesscache_misses = 0;
5961b819cf2SRick Macklem 				nfsstatsv1.biocache_reads = 0;
5971b819cf2SRick Macklem 				nfsstatsv1.read_bios = 0;
5981b819cf2SRick Macklem 				nfsstatsv1.read_physios = 0;
5991b819cf2SRick Macklem 				nfsstatsv1.biocache_writes = 0;
6001b819cf2SRick Macklem 				nfsstatsv1.write_bios = 0;
6011b819cf2SRick Macklem 				nfsstatsv1.write_physios = 0;
6021b819cf2SRick Macklem 				nfsstatsv1.biocache_readlinks = 0;
6031b819cf2SRick Macklem 				nfsstatsv1.readlink_bios = 0;
6041b819cf2SRick Macklem 				nfsstatsv1.biocache_readdirs = 0;
6051b819cf2SRick Macklem 				nfsstatsv1.readdir_bios = 0;
6061b819cf2SRick Macklem 				nfsstatsv1.rpcretries = 0;
6071b819cf2SRick Macklem 				nfsstatsv1.rpcrequests = 0;
6081b819cf2SRick Macklem 				nfsstatsv1.rpctimeouts = 0;
6091b819cf2SRick Macklem 				nfsstatsv1.rpcunexpected = 0;
6101b819cf2SRick Macklem 				nfsstatsv1.rpcinvalid = 0;
6111b819cf2SRick Macklem 				bzero(nfsstatsv1.rpccnt,
6121b819cf2SRick Macklem 				    sizeof(nfsstatsv1.rpccnt));
6132e3b981aSRick Macklem 			}
6142e3b981aSRick Macklem 			if ((uap->flag & NFSSVC_ZEROSRVSTATS) != 0) {
6151b819cf2SRick Macklem 				nfsstatsv1.srvrpc_errs = 0;
6161b819cf2SRick Macklem 				nfsstatsv1.srv_errs = 0;
6171b819cf2SRick Macklem 				nfsstatsv1.srvcache_inproghits = 0;
6181b819cf2SRick Macklem 				nfsstatsv1.srvcache_idemdonehits = 0;
6191b819cf2SRick Macklem 				nfsstatsv1.srvcache_nonidemdonehits = 0;
6201b819cf2SRick Macklem 				nfsstatsv1.srvcache_misses = 0;
6211b819cf2SRick Macklem 				nfsstatsv1.srvcache_tcppeak = 0;
6221b819cf2SRick Macklem 				bzero(nfsstatsv1.srvrpccnt,
6231b819cf2SRick Macklem 				    sizeof(nfsstatsv1.srvrpccnt));
6241b819cf2SRick Macklem 				bzero(nfsstatsv1.cbrpccnt,
6251b819cf2SRick Macklem 				    sizeof(nfsstatsv1.cbrpccnt));
6262e3b981aSRick Macklem 			}
6272e3b981aSRick Macklem 		}
628a9285ae5SZack Kirsch 		goto out;
6299ec7b004SRick Macklem 	} else if (uap->flag & NFSSVC_NFSUSERDPORT) {
6309ec7b004SRick Macklem 		u_short sockport;
63125d694a6SRick Macklem 		struct sockaddr *sad;
63225d694a6SRick Macklem 		struct sockaddr_un *sun;
6339ec7b004SRick Macklem 
63425d694a6SRick Macklem 		if ((uap->flag & NFSSVC_NEWSTRUCT) != 0) {
63525d694a6SRick Macklem 			/* New nfsuserd using an AF_LOCAL socket. */
63625d694a6SRick Macklem 			sun = malloc(sizeof(struct sockaddr_un), M_SONAME,
63725d694a6SRick Macklem 			    M_WAITOK | M_ZERO);
63825d694a6SRick Macklem 			error = copyinstr(uap->argp, sun->sun_path,
63925d694a6SRick Macklem 			    sizeof(sun->sun_path), NULL);
64025d694a6SRick Macklem 			if (error != 0) {
64125d694a6SRick Macklem 				free(sun, M_SONAME);
64225d694a6SRick Macklem 				return (error);
64325d694a6SRick Macklem 			}
64425d694a6SRick Macklem 		        sun->sun_family = AF_LOCAL;
64525d694a6SRick Macklem 		        sun->sun_len = SUN_LEN(sun);
64625d694a6SRick Macklem 			sockport = 0;
64725d694a6SRick Macklem 			sad = (struct sockaddr *)sun;
64825d694a6SRick Macklem 		} else {
6499ec7b004SRick Macklem 			error = copyin(uap->argp, (caddr_t)&sockport,
6509ec7b004SRick Macklem 			    sizeof (u_short));
65125d694a6SRick Macklem 			sad = NULL;
65225d694a6SRick Macklem 		}
65325d694a6SRick Macklem 		if (error == 0)
65425d694a6SRick Macklem 			error = nfsrv_nfsuserdport(sad, sockport, p);
6559ec7b004SRick Macklem 	} else if (uap->flag & NFSSVC_NFSUSERDDELPORT) {
6569ec7b004SRick Macklem 		nfsrv_nfsuserddelport();
6579ec7b004SRick Macklem 		error = 0;
6589ec7b004SRick Macklem 	}
659a9285ae5SZack Kirsch 
660a9285ae5SZack Kirsch out:
661a9285ae5SZack Kirsch 	NFSEXITCODE(error);
6629ec7b004SRick Macklem 	return (error);
6639ec7b004SRick Macklem }
6649ec7b004SRick Macklem 
6659ec7b004SRick Macklem /*
6669ec7b004SRick Macklem  * called by all three modevent routines, so that it gets things
6679ec7b004SRick Macklem  * initialized soon enough.
6689ec7b004SRick Macklem  */
6699ec7b004SRick Macklem void
6709ec7b004SRick Macklem newnfs_portinit(void)
6719ec7b004SRick Macklem {
6729ec7b004SRick Macklem 	static int inited = 0;
6739ec7b004SRick Macklem 
6749ec7b004SRick Macklem 	if (inited)
6759ec7b004SRick Macklem 		return;
6769ec7b004SRick Macklem 	inited = 1;
6779ec7b004SRick Macklem 	/* Initialize SMP locks used by both client and server. */
6789ec7b004SRick Macklem 	mtx_init(&newnfsd_mtx, "newnfsd_mtx", NULL, MTX_DEF);
6799ec7b004SRick Macklem 	mtx_init(&nfs_state_mutex, "nfs_state_mutex", NULL, MTX_DEF);
680b2fc0141SRick Macklem 	mtx_init(&nfs_clstate_mutex, "nfs_clstate_mutex", NULL, MTX_DEF);
6819ec7b004SRick Macklem }
6829ec7b004SRick Macklem 
683ee1c1433SRick Macklem /*
684ee1c1433SRick Macklem  * Determine if the file system supports NFSv4 ACLs.
685ee1c1433SRick Macklem  * Return 1 if it does, 0 otherwise.
686ee1c1433SRick Macklem  */
687ee1c1433SRick Macklem int
688a09001a8SRick Macklem nfs_supportsnfsv4acls(struct vnode *vp)
689ee1c1433SRick Macklem {
690a09001a8SRick Macklem 	int error;
691b1288166SJohn Baldwin 	long retval;
692ee1c1433SRick Macklem 
693a09001a8SRick Macklem 	ASSERT_VOP_LOCKED(vp, "nfs supports nfsv4acls");
694a09001a8SRick Macklem 
695a09001a8SRick Macklem 	if (nfsrv_useacl == 0)
696ee1c1433SRick Macklem 		return (0);
697061c683cSZack Kirsch 	error = VOP_PATHCONF(vp, _PC_ACL_NFS4, &retval);
698a09001a8SRick Macklem 	if (error == 0 && retval != 0)
699ee1c1433SRick Macklem 		return (1);
700ee1c1433SRick Macklem 	return (0);
701ee1c1433SRick Macklem }
702ee1c1433SRick Macklem 
703f49c813cSRick Macklem /*
704f49c813cSRick Macklem  * These are the first fields of all the context structures passed into
705f49c813cSRick Macklem  * nfs_pnfsio().
706f49c813cSRick Macklem  */
707f49c813cSRick Macklem struct pnfsio {
708f49c813cSRick Macklem 	int		done;
709f49c813cSRick Macklem 	int		inprog;
710f49c813cSRick Macklem 	struct task	tsk;
711f49c813cSRick Macklem };
712f49c813cSRick Macklem 
713f49c813cSRick Macklem /*
714f49c813cSRick Macklem  * Do a mirror I/O on a pNFS thread.
715f49c813cSRick Macklem  */
716f49c813cSRick Macklem int
717f49c813cSRick Macklem nfs_pnfsio(task_fn_t *func, void *context)
718f49c813cSRick Macklem {
719f49c813cSRick Macklem 	struct pnfsio *pio;
720f49c813cSRick Macklem 	int ret;
721f49c813cSRick Macklem 	static struct taskqueue *pnfsioq = NULL;
722f49c813cSRick Macklem 
723f49c813cSRick Macklem 	pio = (struct pnfsio *)context;
724f49c813cSRick Macklem 	if (pnfsioq == NULL) {
725f49c813cSRick Macklem 		if (nfs_pnfsiothreads == 0)
726*dec8894bSRick Macklem 			return (EPERM);
727*dec8894bSRick Macklem 		if (nfs_pnfsiothreads < 0)
728f49c813cSRick Macklem 			nfs_pnfsiothreads = mp_ncpus * 4;
729f49c813cSRick Macklem 		pnfsioq = taskqueue_create("pnfsioq", M_WAITOK,
730f49c813cSRick Macklem 		    taskqueue_thread_enqueue, &pnfsioq);
731f49c813cSRick Macklem 		if (pnfsioq == NULL)
732f49c813cSRick Macklem 			return (ENOMEM);
733f49c813cSRick Macklem 		ret = taskqueue_start_threads(&pnfsioq, nfs_pnfsiothreads,
734f49c813cSRick Macklem 		    0, "pnfsiot");
735f49c813cSRick Macklem 		if (ret != 0) {
736f49c813cSRick Macklem 			taskqueue_free(pnfsioq);
737f49c813cSRick Macklem 			pnfsioq = NULL;
738f49c813cSRick Macklem 			return (ret);
739f49c813cSRick Macklem 		}
740f49c813cSRick Macklem 	}
741f49c813cSRick Macklem 	pio->inprog = 1;
742f49c813cSRick Macklem 	TASK_INIT(&pio->tsk, 0, func, context);
743f49c813cSRick Macklem 	ret = taskqueue_enqueue(pnfsioq, &pio->tsk);
744f49c813cSRick Macklem 	if (ret != 0)
745f49c813cSRick Macklem 		pio->inprog = 0;
746f49c813cSRick Macklem 	return (ret);
747f49c813cSRick Macklem }
748f49c813cSRick Macklem 
7499ec7b004SRick Macklem extern int (*nfsd_call_nfscommon)(struct thread *, struct nfssvc_args *);
7509ec7b004SRick Macklem 
7519ec7b004SRick Macklem /*
7529ec7b004SRick Macklem  * Called once to initialize data structures...
7539ec7b004SRick Macklem  */
7549ec7b004SRick Macklem static int
7559ec7b004SRick Macklem nfscommon_modevent(module_t mod, int type, void *data)
7569ec7b004SRick Macklem {
7579ec7b004SRick Macklem 	int error = 0;
7589ec7b004SRick Macklem 	static int loaded = 0;
7599ec7b004SRick Macklem 
7609ec7b004SRick Macklem 	switch (type) {
7619ec7b004SRick Macklem 	case MOD_LOAD:
7629ec7b004SRick Macklem 		if (loaded)
763a9285ae5SZack Kirsch 			goto out;
7649ec7b004SRick Macklem 		newnfs_portinit();
7659ec7b004SRick Macklem 		mtx_init(&nfs_nameid_mutex, "nfs_nameid_mutex", NULL, MTX_DEF);
7669ec7b004SRick Macklem 		mtx_init(&nfs_sockl_mutex, "nfs_sockl_mutex", NULL, MTX_DEF);
7679ec7b004SRick Macklem 		mtx_init(&nfs_slock_mutex, "nfs_slock_mutex", NULL, MTX_DEF);
7689ec7b004SRick Macklem 		mtx_init(&nfs_req_mutex, "nfs_req_mutex", NULL, MTX_DEF);
7699ec7b004SRick Macklem 		mtx_init(&nfsrv_nfsuserdsock.nr_mtx, "nfsuserd", NULL,
7709ec7b004SRick Macklem 		    MTX_DEF);
771fd90e2edSJung-uk Kim 		callout_init(&newnfsd_callout, 1);
7729ec7b004SRick Macklem 		newnfs_init();
7739ec7b004SRick Macklem 		nfsd_call_nfscommon = nfssvc_nfscommon;
7749ec7b004SRick Macklem 		loaded = 1;
7759ec7b004SRick Macklem 		break;
7769ec7b004SRick Macklem 
7779ec7b004SRick Macklem 	case MOD_UNLOAD:
7789ec7b004SRick Macklem 		if (newnfs_numnfsd != 0 || nfsrv_nfsuserd != 0 ||
7799ec7b004SRick Macklem 		    nfs_numnfscbd != 0) {
7809ec7b004SRick Macklem 			error = EBUSY;
7819ec7b004SRick Macklem 			break;
7829ec7b004SRick Macklem 		}
7839ec7b004SRick Macklem 
7849ec7b004SRick Macklem 		nfsd_call_nfscommon = NULL;
7859ec7b004SRick Macklem 		callout_drain(&newnfsd_callout);
78665171ebbSRick Macklem 		/* Clean out the name<-->id cache. */
78765171ebbSRick Macklem 		nfsrv_cleanusergroup();
7889ec7b004SRick Macklem 		/* and get rid of the mutexes */
7899ec7b004SRick Macklem 		mtx_destroy(&nfs_nameid_mutex);
7909ec7b004SRick Macklem 		mtx_destroy(&newnfsd_mtx);
7919ec7b004SRick Macklem 		mtx_destroy(&nfs_state_mutex);
792b2fc0141SRick Macklem 		mtx_destroy(&nfs_clstate_mutex);
7939ec7b004SRick Macklem 		mtx_destroy(&nfs_sockl_mutex);
7949ec7b004SRick Macklem 		mtx_destroy(&nfs_slock_mutex);
7959ec7b004SRick Macklem 		mtx_destroy(&nfs_req_mutex);
7969ec7b004SRick Macklem 		mtx_destroy(&nfsrv_nfsuserdsock.nr_mtx);
7979ec7b004SRick Macklem 		loaded = 0;
7989ec7b004SRick Macklem 		break;
7999ec7b004SRick Macklem 	default:
8009ec7b004SRick Macklem 		error = EOPNOTSUPP;
8019ec7b004SRick Macklem 		break;
8029ec7b004SRick Macklem 	}
803a9285ae5SZack Kirsch 
804a9285ae5SZack Kirsch out:
805a9285ae5SZack Kirsch 	NFSEXITCODE(error);
8069ec7b004SRick Macklem 	return error;
8079ec7b004SRick Macklem }
8089ec7b004SRick Macklem static moduledata_t nfscommon_mod = {
8099ec7b004SRick Macklem 	"nfscommon",
8109ec7b004SRick Macklem 	nfscommon_modevent,
8119ec7b004SRick Macklem 	NULL,
8129ec7b004SRick Macklem };
8139ec7b004SRick Macklem DECLARE_MODULE(nfscommon, nfscommon_mod, SI_SUB_VFS, SI_ORDER_ANY);
8149ec7b004SRick Macklem 
8159ec7b004SRick Macklem /* So that loader and kldload(2) can find us, wherever we are.. */
8169ec7b004SRick Macklem MODULE_VERSION(nfscommon, 1);
8179ec7b004SRick Macklem MODULE_DEPEND(nfscommon, nfssvc, 1, 1, 1);
8189ec7b004SRick Macklem MODULE_DEPEND(nfscommon, krpc, 1, 1, 1);
8199ec7b004SRick Macklem 
820