xref: /freebsd/sys/fs/nfsserver/nfs_nfsdkrpc.c (revision 6d5ce2bb63445e9c09c3b5c29fb18983e1e2628c)
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 #include "opt_inet6.h"
389ec7b004SRick Macklem #include "opt_kgssapi.h"
396e4b6ff8SRick Macklem #include "opt_kern_tls.h"
409ec7b004SRick Macklem 
419ec7b004SRick Macklem #include <fs/nfs/nfsport.h>
429ec7b004SRick Macklem 
439ec7b004SRick Macklem #include <rpc/rpc.h>
449ec7b004SRick Macklem #include <rpc/rpcsec_gss.h>
456e4b6ff8SRick Macklem #include <rpc/rpcsec_tls.h>
469ec7b004SRick Macklem 
47d96b98a3SKenneth D. Merry #include <fs/nfsserver/nfs_fha_new.h>
48d96b98a3SKenneth D. Merry 
49bcf11e8dSRobert Watson #include <security/mac/mac_framework.h>
50bcf11e8dSRobert Watson 
519ec7b004SRick Macklem NFSDLOCKMUTEX;
526001db29SRick Macklem NFSV4ROOTLOCKMUTEX;
53acc6e58dSRick Macklem char *nfsrv_zeropnfsdat = NULL;
549ec7b004SRick Macklem 
559ec7b004SRick Macklem /*
569ec7b004SRick Macklem  * Mapping of old NFS Version 2 RPC numbers to generic numbers.
579ec7b004SRick Macklem  */
58d96b98a3SKenneth D. Merry int newnfs_nfsv3_procid[NFS_V3NPROCS] = {
599ec7b004SRick Macklem 	NFSPROC_NULL,
609ec7b004SRick Macklem 	NFSPROC_GETATTR,
619ec7b004SRick Macklem 	NFSPROC_SETATTR,
629ec7b004SRick Macklem 	NFSPROC_NOOP,
639ec7b004SRick Macklem 	NFSPROC_LOOKUP,
649ec7b004SRick Macklem 	NFSPROC_READLINK,
659ec7b004SRick Macklem 	NFSPROC_READ,
669ec7b004SRick Macklem 	NFSPROC_NOOP,
679ec7b004SRick Macklem 	NFSPROC_WRITE,
689ec7b004SRick Macklem 	NFSPROC_CREATE,
699ec7b004SRick Macklem 	NFSPROC_REMOVE,
709ec7b004SRick Macklem 	NFSPROC_RENAME,
719ec7b004SRick Macklem 	NFSPROC_LINK,
729ec7b004SRick Macklem 	NFSPROC_SYMLINK,
739ec7b004SRick Macklem 	NFSPROC_MKDIR,
749ec7b004SRick Macklem 	NFSPROC_RMDIR,
759ec7b004SRick Macklem 	NFSPROC_READDIR,
769ec7b004SRick Macklem 	NFSPROC_FSSTAT,
779ec7b004SRick Macklem 	NFSPROC_NOOP,
789ec7b004SRick Macklem 	NFSPROC_NOOP,
799ec7b004SRick Macklem 	NFSPROC_NOOP,
809ec7b004SRick Macklem 	NFSPROC_NOOP,
819ec7b004SRick Macklem };
829ec7b004SRick Macklem 
83a0c2c369SRick Macklem SYSCTL_DECL(_vfs_nfsd);
849ec7b004SRick Macklem 
85*6d5ce2bbSMark Johnston NFSD_VNET_DEFINE_STATIC(int, nfs_privport) = 1;
867e44856eSRick Macklem SYSCTL_INT(_vfs_nfsd, OID_AUTO, nfs_privport, CTLFLAG_NFSD_VNET | CTLFLAG_RWTUN,
877e44856eSRick Macklem     &NFSD_VNET_NAME(nfs_privport), 0,
8823114c6cSRick Macklem     "Only allow clients using a privileged port for NFSv2, 3 and 4");
899ec7b004SRick Macklem 
907e44856eSRick Macklem NFSD_VNET_DEFINE_STATIC(int, nfs_minvers) = NFS_VER2;
917e44856eSRick Macklem SYSCTL_INT(_vfs_nfsd, OID_AUTO, server_min_nfsvers,
927e44856eSRick Macklem     CTLFLAG_NFSD_VNET | CTLFLAG_RWTUN, &NFSD_VNET_NAME(nfs_minvers), 0,
937e44856eSRick Macklem     "The lowest version of NFS handled by the server");
9410a5a16eSRick Macklem 
957e44856eSRick Macklem NFSD_VNET_DEFINE_STATIC(int, nfs_maxvers) = NFS_VER4;
967e44856eSRick Macklem SYSCTL_INT(_vfs_nfsd, OID_AUTO, server_max_nfsvers,
977e44856eSRick Macklem     CTLFLAG_NFSD_VNET | CTLFLAG_RWTUN, &NFSD_VNET_NAME(nfs_maxvers), 0,
987e44856eSRick Macklem     "The highest version of NFS handled by the server");
9910a5a16eSRick Macklem 
100d473bac7SAlexander Motin static int nfs_proc(struct nfsrv_descript *, u_int32_t, SVCXPRT *xprt,
101d473bac7SAlexander Motin     struct nfsrvcache **);
1029ec7b004SRick Macklem 
1039ec7b004SRick Macklem extern u_long sb_max_adj;
1049ec7b004SRick Macklem extern int newnfs_numnfsd;
10590d2dfabSRick Macklem extern time_t nfsdev_time;
1063e5ba2e1SRick Macklem extern int nfsrv_writerpc[NFS_NPROCS];
1073e5ba2e1SRick Macklem extern volatile int nfsrv_devidcnt;
108c057a378SRick Macklem extern struct nfsv4_opflag nfsv4_opflag[NFSV42_NOPS];
109ff2f1f69SRick Macklem extern int nfsd_debuglevel;
1109ec7b004SRick Macklem 
1117e44856eSRick Macklem NFSD_VNET_DECLARE(struct proc *, nfsd_master_proc);
1127e44856eSRick Macklem 
1137e44856eSRick Macklem NFSD_VNET_DEFINE(SVCPOOL *, nfsrvd_pool);
1147e44856eSRick Macklem NFSD_VNET_DEFINE(int, nfsrv_numnfsd) = 0;
1157e44856eSRick Macklem NFSD_VNET_DEFINE(struct nfsv4lock, nfsd_suspend_lock);
1167e44856eSRick Macklem 
1177e44856eSRick Macklem NFSD_VNET_DEFINE_STATIC(bool, nfsrvd_inited) = false;
1187e44856eSRick Macklem 
1199ec7b004SRick Macklem /*
1209ec7b004SRick Macklem  * NFS server system calls
1219ec7b004SRick Macklem  */
1229ec7b004SRick Macklem 
1239ec7b004SRick Macklem static void
1249ec7b004SRick Macklem nfssvc_program(struct svc_req *rqst, SVCXPRT *xprt)
1259ec7b004SRick Macklem {
1269ec7b004SRick Macklem 	struct nfsrv_descript nd;
1279ec7b004SRick Macklem 	struct nfsrvcache *rp = NULL;
128ff2f1f69SRick Macklem 	rpc_gss_rawcred_t *rcredp;
129ff2f1f69SRick Macklem 	int cacherep, credflavor, i, j;
130ff2f1f69SRick Macklem 	u_char *p;
1316e4b6ff8SRick Macklem #ifdef KERN_TLS
1326e4b6ff8SRick Macklem 	u_int maxlen;
1336e4b6ff8SRick Macklem #endif
1349ec7b004SRick Macklem 
1357e44856eSRick Macklem 	NFSD_CURVNET_SET_QUIET(NFSD_TD_TO_VNET(curthread));
1369ec7b004SRick Macklem 	memset(&nd, 0, sizeof(nd));
1379ec7b004SRick Macklem 	if (rqst->rq_vers == NFS_VER2) {
1382fbb9d56SRick Macklem 		if (rqst->rq_proc > NFSV2PROC_STATFS ||
1392fbb9d56SRick Macklem 		    newnfs_nfsv3_procid[rqst->rq_proc] == NFSPROC_NOOP) {
1409ec7b004SRick Macklem 			svcerr_noproc(rqst);
1419ec7b004SRick Macklem 			svc_freereq(rqst);
142a9285ae5SZack Kirsch 			goto out;
1439ec7b004SRick Macklem 		}
1449ec7b004SRick Macklem 		nd.nd_procnum = newnfs_nfsv3_procid[rqst->rq_proc];
1459ec7b004SRick Macklem 		nd.nd_flag = ND_NFSV2;
1469ec7b004SRick Macklem 	} else if (rqst->rq_vers == NFS_VER3) {
1479ec7b004SRick Macklem 		if (rqst->rq_proc >= NFS_V3NPROCS) {
1489ec7b004SRick Macklem 			svcerr_noproc(rqst);
1499ec7b004SRick Macklem 			svc_freereq(rqst);
150a9285ae5SZack Kirsch 			goto out;
1519ec7b004SRick Macklem 		}
1529ec7b004SRick Macklem 		nd.nd_procnum = rqst->rq_proc;
1539ec7b004SRick Macklem 		nd.nd_flag = ND_NFSV3;
1549ec7b004SRick Macklem 	} else {
1559ec7b004SRick Macklem 		if (rqst->rq_proc != NFSPROC_NULL &&
1569ec7b004SRick Macklem 		    rqst->rq_proc != NFSV4PROC_COMPOUND) {
1579ec7b004SRick Macklem 			svcerr_noproc(rqst);
1589ec7b004SRick Macklem 			svc_freereq(rqst);
159a9285ae5SZack Kirsch 			goto out;
1609ec7b004SRick Macklem 		}
1619ec7b004SRick Macklem 		nd.nd_procnum = rqst->rq_proc;
1629ec7b004SRick Macklem 		nd.nd_flag = ND_NFSV4;
1639ec7b004SRick Macklem 	}
1649ec7b004SRick Macklem 
1659ec7b004SRick Macklem 	/*
1669ec7b004SRick Macklem 	 * Note: we want rq_addr, not svc_getrpccaller for nd_nam2 -
1679ec7b004SRick Macklem 	 * NFS_SRVMAXDATA uses a NULL value for nd_nam2 to detect TCP
1689ec7b004SRick Macklem 	 * mounts.
1699ec7b004SRick Macklem 	 */
1709ec7b004SRick Macklem 	nd.nd_mrep = rqst->rq_args;
1719ec7b004SRick Macklem 	rqst->rq_args = NULL;
172d96b98a3SKenneth D. Merry 	newnfs_realign(&nd.nd_mrep, M_WAITOK);
1739ec7b004SRick Macklem 	nd.nd_md = nd.nd_mrep;
1749ec7b004SRick Macklem 	nd.nd_dpos = mtod(nd.nd_md, caddr_t);
1759ec7b004SRick Macklem 	nd.nd_nam = svc_getrpccaller(rqst);
1769ec7b004SRick Macklem 	nd.nd_nam2 = rqst->rq_addr;
1779ec7b004SRick Macklem 	nd.nd_mreq = NULL;
1789ec7b004SRick Macklem 	nd.nd_cred = NULL;
1799ec7b004SRick Macklem 
1807e44856eSRick Macklem 	if (NFSD_VNET(nfs_privport) != 0) {
1819ec7b004SRick Macklem 		/* Check if source port is privileged */
1829ec7b004SRick Macklem 		u_short port;
1839ec7b004SRick Macklem 		struct sockaddr *nam = nd.nd_nam;
1849ec7b004SRick Macklem 		struct sockaddr_in *sin;
1859ec7b004SRick Macklem 
1869ec7b004SRick Macklem 		sin = (struct sockaddr_in *)nam;
1879ec7b004SRick Macklem 		/*
1889ec7b004SRick Macklem 		 * INET/INET6 - same code:
1899ec7b004SRick Macklem 		 *    sin_port and sin6_port are at same offset
1909ec7b004SRick Macklem 		 */
1919ec7b004SRick Macklem 		port = ntohs(sin->sin_port);
1929ec7b004SRick Macklem 		if (port >= IPPORT_RESERVED &&
1939ec7b004SRick Macklem 		    nd.nd_procnum != NFSPROC_NULL) {
194b7e4666dSMark Johnston 			static struct timeval privport_ratecheck = {
195b7e4666dSMark Johnston 				.tv_sec = 0, .tv_usec = 0
196b7e4666dSMark Johnston 			};
197b7e4666dSMark Johnston 			static const struct timeval privport_ratecheck_int = {
198b7e4666dSMark Johnston 				.tv_sec = 1, .tv_usec = 0
199b7e4666dSMark Johnston 			};
2009ec7b004SRick Macklem #ifdef INET6
2018144690aSEric van Gyzen 			char buf[INET6_ADDRSTRLEN];
2028144690aSEric van Gyzen #else
2038144690aSEric van Gyzen 			char buf[INET_ADDRSTRLEN];
2048144690aSEric van Gyzen #endif
2058144690aSEric van Gyzen #ifdef INET6
2069ec7b004SRick Macklem #if defined(KLD_MODULE)
2079ec7b004SRick Macklem 			/* Do not use ip6_sprintf: the nfs module should work without INET6. */
2089ec7b004SRick Macklem #define	ip6_sprintf(buf, a)						\
2099ec7b004SRick Macklem 			(sprintf((buf), "%x:%x:%x:%x:%x:%x:%x:%x",	\
2109ec7b004SRick Macklem 			    (a)->s6_addr16[0], (a)->s6_addr16[1],	\
2119ec7b004SRick Macklem 			    (a)->s6_addr16[2], (a)->s6_addr16[3],	\
2129ec7b004SRick Macklem 			    (a)->s6_addr16[4], (a)->s6_addr16[5],	\
2139ec7b004SRick Macklem 			    (a)->s6_addr16[6], (a)->s6_addr16[7]),	\
2149ec7b004SRick Macklem 			    (buf))
2159ec7b004SRick Macklem #endif
2169ec7b004SRick Macklem #endif
217b7e4666dSMark Johnston 			if (ratecheck(&privport_ratecheck,
218b7e4666dSMark Johnston 			    &privport_ratecheck_int)) {
219b7e4666dSMark Johnston 				printf(
220b7e4666dSMark Johnston 			    "NFS request from unprivileged port (%s:%d)\n",
2219ec7b004SRick Macklem #ifdef INET6
2229ec7b004SRick Macklem 				    sin->sin_family == AF_INET6 ?
2238144690aSEric van Gyzen 				    ip6_sprintf(buf, &satosin6(sin)->sin6_addr) :
2249ec7b004SRick Macklem #if defined(KLD_MODULE)
2259ec7b004SRick Macklem #undef ip6_sprintf
2269ec7b004SRick Macklem #endif
2279ec7b004SRick Macklem #endif
2288144690aSEric van Gyzen 				    inet_ntoa_r(sin->sin_addr, buf), port);
229b7e4666dSMark Johnston 			}
2309ec7b004SRick Macklem 			svcerr_weakauth(rqst);
2319ec7b004SRick Macklem 			svc_freereq(rqst);
2329ec7b004SRick Macklem 			m_freem(nd.nd_mrep);
233a9285ae5SZack Kirsch 			goto out;
2349ec7b004SRick Macklem 		}
2359ec7b004SRick Macklem 	}
2369ec7b004SRick Macklem 
2379ec7b004SRick Macklem 	if (nd.nd_procnum != NFSPROC_NULL) {
23898ad4453SRick Macklem 		if (!svc_getcred(rqst, &nd.nd_cred, &credflavor)) {
2399ec7b004SRick Macklem 			svcerr_weakauth(rqst);
2409ec7b004SRick Macklem 			svc_freereq(rqst);
2419ec7b004SRick Macklem 			m_freem(nd.nd_mrep);
242a9285ae5SZack Kirsch 			goto out;
2439ec7b004SRick Macklem 		}
24498ad4453SRick Macklem 
24598ad4453SRick Macklem 		/* Set the flag based on credflavor */
24698ad4453SRick Macklem 		if (credflavor == RPCSEC_GSS_KRB5) {
24798ad4453SRick Macklem 			nd.nd_flag |= ND_GSS;
24898ad4453SRick Macklem 		} else if (credflavor == RPCSEC_GSS_KRB5I) {
24998ad4453SRick Macklem 			nd.nd_flag |= (ND_GSS | ND_GSSINTEGRITY);
25098ad4453SRick Macklem 		} else if (credflavor == RPCSEC_GSS_KRB5P) {
25198ad4453SRick Macklem 			nd.nd_flag |= (ND_GSS | ND_GSSPRIVACY);
25298ad4453SRick Macklem 		} else if (credflavor != AUTH_SYS) {
25398ad4453SRick Macklem 			svcerr_weakauth(rqst);
25498ad4453SRick Macklem 			svc_freereq(rqst);
25598ad4453SRick Macklem 			m_freem(nd.nd_mrep);
256a9285ae5SZack Kirsch 			goto out;
25798ad4453SRick Macklem 		}
25898ad4453SRick Macklem 
259ff2f1f69SRick Macklem 		/* Acquire the principal name for the RPCSEC_GSS cases. */
260ff2f1f69SRick Macklem 		if ((nd.nd_flag & (ND_NFSV4 | ND_GSS)) == (ND_NFSV4 | ND_GSS)) {
261ff2f1f69SRick Macklem 			rcredp = NULL;
262ff2f1f69SRick Macklem 			rpc_gss_getcred_call(rqst, &rcredp, NULL, NULL);
263ff2f1f69SRick Macklem 			/*
264ff2f1f69SRick Macklem 			 * The exported principal name consists of:
265ff2f1f69SRick Macklem 			 * - TOK_ID bytes with value of 4 and 1.
266ff2f1f69SRick Macklem 			 * - 2 byte mech length
267ff2f1f69SRick Macklem 			 * - mech
268ff2f1f69SRick Macklem 			 * - 4 byte principal name length
269ff2f1f69SRick Macklem 			 * - principal name
270ff2f1f69SRick Macklem 			 * A call to gss_import_name() would be an
271ff2f1f69SRick Macklem 			 * upcall to the gssd, so parse it here.
272ff2f1f69SRick Macklem 			 * See lib/libgssapi/gss_import_name.c for the
273ff2f1f69SRick Macklem 			 * above format.
274ff2f1f69SRick Macklem 			 */
275ff2f1f69SRick Macklem 			if (rcredp != NULL &&
276ff2f1f69SRick Macklem 			    rcredp->client_principal->len > 4 &&
277ff2f1f69SRick Macklem 			    rcredp->client_principal->name[0] == 4 &&
278ff2f1f69SRick Macklem 			    rcredp->client_principal->name[1] == 1) {
279ff2f1f69SRick Macklem 				/* Skip over the mech. */
280ff2f1f69SRick Macklem 				p = &rcredp->client_principal->name[2];
281ff2f1f69SRick Macklem 				i = (p[0] << 8) | p[1];
282ff2f1f69SRick Macklem 				p += i + 2;
283ff2f1f69SRick Macklem 				i += 4;
284ff2f1f69SRick Macklem 				/*
285ff2f1f69SRick Macklem 				 * Set "j" to a bogus length so that the
286ff2f1f69SRick Macklem 				 * "i + j" check will fail unless the below
287ff2f1f69SRick Macklem 				 * code sets "j" correctly.
288ff2f1f69SRick Macklem 				 */
289ff2f1f69SRick Macklem 				j = rcredp->client_principal->len;
290ff2f1f69SRick Macklem 				if (rcredp->client_principal->len > i + 4) {
291ff2f1f69SRick Macklem 					j = (p[0] << 24) | (p[1] << 16) |
292ff2f1f69SRick Macklem 					    (p[2] << 8) | p[3];
293ff2f1f69SRick Macklem 					i += 4;
294ff2f1f69SRick Macklem 					p += 4;
295ff2f1f69SRick Macklem 				}
296ff2f1f69SRick Macklem 				if (i + j == rcredp->client_principal->len) {
297ff2f1f69SRick Macklem 					nd.nd_principal = malloc(j + 1, M_TEMP,
298ff2f1f69SRick Macklem 					    M_WAITOK);
299ff2f1f69SRick Macklem 					nd.nd_princlen = j;
300ff2f1f69SRick Macklem 					memcpy(nd.nd_principal, p, j);
301ff2f1f69SRick Macklem 					nd.nd_principal[j] = '\0';
302ff2f1f69SRick Macklem 					NFSD_DEBUG(1, "nfssvc_program: "
303ff2f1f69SRick Macklem 					    "principal=%s\n", nd.nd_principal);
304ff2f1f69SRick Macklem 				}
305ff2f1f69SRick Macklem 			}
306ff2f1f69SRick Macklem 			if (nd.nd_princlen == 0)
307ff2f1f69SRick Macklem 				printf("nfssvc_program: cannot get RPCSEC_GSS "
308ff2f1f69SRick Macklem 				    "principal name\n");
309ff2f1f69SRick Macklem 		}
310ff2f1f69SRick Macklem 
3116e4b6ff8SRick Macklem 		if ((xprt->xp_tls & RPCTLS_FLAGS_HANDSHAKE) != 0) {
3126e4b6ff8SRick Macklem 			nd.nd_flag |= ND_TLS;
3136e4b6ff8SRick Macklem 			if ((xprt->xp_tls & RPCTLS_FLAGS_VERIFIED) != 0)
3146e4b6ff8SRick Macklem 				nd.nd_flag |= ND_TLSCERT;
3156e4b6ff8SRick Macklem 			if ((xprt->xp_tls & RPCTLS_FLAGS_CERTUSER) != 0)
3166e4b6ff8SRick Macklem 				nd.nd_flag |= ND_TLSCERTUSER;
3176e4b6ff8SRick Macklem 		}
3186e4b6ff8SRick Macklem 		nd.nd_maxextsiz = 16384;
3199ec7b004SRick Macklem #ifdef MAC
3209ec7b004SRick Macklem 		mac_cred_associate_nfsd(nd.nd_cred);
3219ec7b004SRick Macklem #endif
3226001db29SRick Macklem 		/*
3236001db29SRick Macklem 		 * Get a refcnt (shared lock) on nfsd_suspend_lock.
3246001db29SRick Macklem 		 * NFSSVC_SUSPENDNFSD will take an exclusive lock on
3256001db29SRick Macklem 		 * nfsd_suspend_lock to suspend these threads.
326ee58b564SPedro F. Giffuni 		 * The call to nfsv4_lock() that precedes nfsv4_getref()
3278eabbbe2SRick Macklem 		 * ensures that the acquisition of the exclusive lock
3288eabbbe2SRick Macklem 		 * takes priority over acquisition of the shared lock by
3298eabbbe2SRick Macklem 		 * waiting for any exclusive lock request to complete.
3306001db29SRick Macklem 		 * This must be done here, before the check of
3316001db29SRick Macklem 		 * nfsv4root exports by nfsvno_v4rootexport().
3326001db29SRick Macklem 		 */
3336001db29SRick Macklem 		NFSLOCKV4ROOTMUTEX();
3347e44856eSRick Macklem 		nfsv4_lock(&NFSD_VNET(nfsd_suspend_lock), 0, NULL,
3357e44856eSRick Macklem 		    NFSV4ROOTLOCKMUTEXPTR, NULL);
3367e44856eSRick Macklem 		nfsv4_getref(&NFSD_VNET(nfsd_suspend_lock), NULL,
3377e44856eSRick Macklem 		    NFSV4ROOTLOCKMUTEXPTR, NULL);
3386001db29SRick Macklem 		NFSUNLOCKV4ROOTMUTEX();
3396001db29SRick Macklem 
340415670e4SRick Macklem 		if ((nd.nd_flag & ND_NFSV4) != 0) {
3419ec7b004SRick Macklem 			nd.nd_repstat = nfsvno_v4rootexport(&nd);
342415670e4SRick Macklem 			if (nd.nd_repstat != 0) {
3436001db29SRick Macklem 				NFSLOCKV4ROOTMUTEX();
3447e44856eSRick Macklem 				nfsv4_relref(&NFSD_VNET(nfsd_suspend_lock));
3456001db29SRick Macklem 				NFSUNLOCKV4ROOTMUTEX();
346415670e4SRick Macklem 				svcerr_weakauth(rqst);
347415670e4SRick Macklem 				svc_freereq(rqst);
348415670e4SRick Macklem 				m_freem(nd.nd_mrep);
349a9285ae5SZack Kirsch 				goto out;
350415670e4SRick Macklem 			}
351415670e4SRick Macklem 		}
3529ec7b004SRick Macklem 
3536e4b6ff8SRick Macklem #ifdef KERN_TLS
3546e4b6ff8SRick Macklem 		if ((xprt->xp_tls & RPCTLS_FLAGS_HANDSHAKE) != 0 &&
3556e4b6ff8SRick Macklem 		    rpctls_getinfo(&maxlen, false, false))
3566e4b6ff8SRick Macklem 			nd.nd_maxextsiz = maxlen;
3576e4b6ff8SRick Macklem #endif
358d473bac7SAlexander Motin 		cacherep = nfs_proc(&nd, rqst->rq_xid, xprt, &rp);
3596001db29SRick Macklem 		NFSLOCKV4ROOTMUTEX();
3607e44856eSRick Macklem 		nfsv4_relref(&NFSD_VNET(nfsd_suspend_lock));
3616001db29SRick Macklem 		NFSUNLOCKV4ROOTMUTEX();
3629ec7b004SRick Macklem 	} else {
3639ec7b004SRick Macklem 		NFSMGET(nd.nd_mreq);
3649ec7b004SRick Macklem 		nd.nd_mreq->m_len = 0;
3659ec7b004SRick Macklem 		cacherep = RC_REPLY;
3669ec7b004SRick Macklem 	}
3679ec7b004SRick Macklem 	if (nd.nd_mrep != NULL)
3689ec7b004SRick Macklem 		m_freem(nd.nd_mrep);
3699ec7b004SRick Macklem 
3709ec7b004SRick Macklem 	if (nd.nd_cred != NULL)
3719ec7b004SRick Macklem 		crfree(nd.nd_cred);
3729ec7b004SRick Macklem 
3739ec7b004SRick Macklem 	if (cacherep == RC_DROPIT) {
3749ec7b004SRick Macklem 		if (nd.nd_mreq != NULL)
3759ec7b004SRick Macklem 			m_freem(nd.nd_mreq);
3769ec7b004SRick Macklem 		svc_freereq(rqst);
377a9285ae5SZack Kirsch 		goto out;
3789ec7b004SRick Macklem 	}
3799ec7b004SRick Macklem 
3809ec7b004SRick Macklem 	if (nd.nd_mreq == NULL) {
3819ec7b004SRick Macklem 		svcerr_decode(rqst);
3829ec7b004SRick Macklem 		svc_freereq(rqst);
383a9285ae5SZack Kirsch 		goto out;
3849ec7b004SRick Macklem 	}
3859ec7b004SRick Macklem 
3869ec7b004SRick Macklem 	if (nd.nd_repstat & NFSERR_AUTHERR) {
3879ec7b004SRick Macklem 		svcerr_auth(rqst, nd.nd_repstat & ~NFSERR_AUTHERR);
3889ec7b004SRick Macklem 		if (nd.nd_mreq != NULL)
3899ec7b004SRick Macklem 			m_freem(nd.nd_mreq);
3909ec7b004SRick Macklem 	} else if (!svc_sendreply_mbuf(rqst, nd.nd_mreq)) {
3919ec7b004SRick Macklem 		svcerr_systemerr(rqst);
3929ec7b004SRick Macklem 	}
393d473bac7SAlexander Motin 	if (rp != NULL) {
3946103bae6SAlexander Motin 		nfsrvd_sentcache(rp, (rqst->rq_reply_seq != 0 ||
3956103bae6SAlexander Motin 		    SVC_ACK(xprt, NULL)), rqst->rq_reply_seq);
396d473bac7SAlexander Motin 	}
3979ec7b004SRick Macklem 	svc_freereq(rqst);
398a9285ae5SZack Kirsch 
399a9285ae5SZack Kirsch out:
400ff2f1f69SRick Macklem 	free(nd.nd_principal, M_TEMP);
4017e44856eSRick Macklem 	NFSD_CURVNET_RESTORE();
402c6d31b83SKonstantin Belousov 	ast_kclear(curthread);
403a9285ae5SZack Kirsch 	NFSEXITCODE(0);
4049ec7b004SRick Macklem }
4059ec7b004SRick Macklem 
4069ec7b004SRick Macklem /*
4079ec7b004SRick Macklem  * Check the cache and, optionally, do the RPC.
4089ec7b004SRick Macklem  * Return the appropriate cache response.
4099ec7b004SRick Macklem  */
4109ec7b004SRick Macklem static int
411d473bac7SAlexander Motin nfs_proc(struct nfsrv_descript *nd, u_int32_t xid, SVCXPRT *xprt,
412d473bac7SAlexander Motin     struct nfsrvcache **rpp)
4139ec7b004SRick Macklem {
414c59e4cc3SRick Macklem 	int cacherep = RC_DOIT, isdgram, taglen = -1;
415c59e4cc3SRick Macklem 	struct mbuf *m;
416c59e4cc3SRick Macklem 	u_char tag[NFSV4_SMALLSTR + 1], *tagstr = NULL;
417c59e4cc3SRick Macklem 	u_int32_t minorvers = 0;
418d473bac7SAlexander Motin 	uint32_t ack;
4199ec7b004SRick Macklem 
4209ec7b004SRick Macklem 	*rpp = NULL;
4219ec7b004SRick Macklem 	if (nd->nd_nam2 == NULL) {
4229ec7b004SRick Macklem 		nd->nd_flag |= ND_STREAMSOCK;
4239ec7b004SRick Macklem 		isdgram = 0;
4249ec7b004SRick Macklem 	} else {
4259ec7b004SRick Macklem 		isdgram = 1;
4269ec7b004SRick Macklem 	}
4279ec7b004SRick Macklem 
4289ec7b004SRick Macklem 	/*
429415670e4SRick Macklem 	 * Two cases:
4309ec7b004SRick Macklem 	 * 1 - For NFSv2 over UDP, if we are near our malloc/mget
4319ec7b004SRick Macklem 	 *     limit, just drop the request. There is no
4329ec7b004SRick Macklem 	 *     NFSERR_RESOURCE or NFSERR_DELAY for NFSv2 and the
4339ec7b004SRick Macklem 	 *     client will timeout/retry over UDP in a little while.
434415670e4SRick Macklem 	 * 2 - nd_repstat == 0 && nd_mreq == NULL, which
4359ec7b004SRick Macklem 	 *     means a normal nfs rpc, so check the cache
4369ec7b004SRick Macklem 	 */
4379ec7b004SRick Macklem 	if ((nd->nd_flag & ND_NFSV2) && nd->nd_nam2 != NULL &&
4389ec7b004SRick Macklem 	    nfsrv_mallocmget_limit()) {
4399ec7b004SRick Macklem 		cacherep = RC_DROPIT;
4409ec7b004SRick Macklem 	} else {
4419ec7b004SRick Macklem 		/*
4429ec7b004SRick Macklem 		 * For NFSv3, play it safe and assume that the client is
4439ec7b004SRick Macklem 		 * doing retries on the same TCP connection.
4449ec7b004SRick Macklem 		 */
4459ec7b004SRick Macklem 		if ((nd->nd_flag & (ND_NFSV4 | ND_STREAMSOCK)) ==
4469ec7b004SRick Macklem 		    ND_STREAMSOCK)
4479ec7b004SRick Macklem 			nd->nd_flag |= ND_SAMETCPCONN;
4489ec7b004SRick Macklem 		nd->nd_retxid = xid;
4499ec7b004SRick Macklem 		nd->nd_tcpconntime = NFSD_MONOSEC;
450d473bac7SAlexander Motin 		nd->nd_sockref = xprt->xp_sockref;
451c59e4cc3SRick Macklem 		if ((nd->nd_flag & ND_NFSV4) != 0)
452c59e4cc3SRick Macklem 			nfsd_getminorvers(nd, tag, &tagstr, &taglen,
453c59e4cc3SRick Macklem 			    &minorvers);
454c59e4cc3SRick Macklem 		if ((nd->nd_flag & ND_NFSV41) != 0)
455c59e4cc3SRick Macklem 			/* NFSv4.1 caches replies in the session slots. */
456c59e4cc3SRick Macklem 			cacherep = RC_DOIT;
457c59e4cc3SRick Macklem 		else {
458d473bac7SAlexander Motin 			cacherep = nfsrvd_getcache(nd);
459d473bac7SAlexander Motin 			ack = 0;
460d473bac7SAlexander Motin 			SVC_ACK(xprt, &ack);
461d473bac7SAlexander Motin 			nfsrc_trimcache(xprt->xp_sockref, ack, 0);
4629ec7b004SRick Macklem 		}
463c59e4cc3SRick Macklem 	}
4649ec7b004SRick Macklem 
4659ec7b004SRick Macklem 	/*
4669ec7b004SRick Macklem 	 * Handle the request. There are three cases.
4679ec7b004SRick Macklem 	 * RC_DOIT - do the RPC
4689ec7b004SRick Macklem 	 * RC_REPLY - return the reply already created
4699ec7b004SRick Macklem 	 * RC_DROPIT - just throw the request away
4709ec7b004SRick Macklem 	 */
4719ec7b004SRick Macklem 	if (cacherep == RC_DOIT) {
472c59e4cc3SRick Macklem 		if ((nd->nd_flag & ND_NFSV41) != 0)
473c59e4cc3SRick Macklem 			nd->nd_xprt = xprt;
474113aa933SEdward Tomasz Napierala 		nfsrvd_dorpc(nd, isdgram, tagstr, taglen, minorvers);
475c59e4cc3SRick Macklem 		if ((nd->nd_flag & ND_NFSV41) != 0) {
476c59e4cc3SRick Macklem 			if (nd->nd_repstat != NFSERR_REPLYFROMCACHE &&
477c59e4cc3SRick Macklem 			    (nd->nd_flag & ND_SAVEREPLY) != 0) {
478c59e4cc3SRick Macklem 				/* Cache a copy of the reply. */
479c59e4cc3SRick Macklem 				m = m_copym(nd->nd_mreq, 0, M_COPYALL,
480c59e4cc3SRick Macklem 				    M_WAITOK);
481c59e4cc3SRick Macklem 			} else
482c59e4cc3SRick Macklem 				m = NULL;
483c59e4cc3SRick Macklem 			if ((nd->nd_flag & ND_HASSEQUENCE) != 0)
48476fd19b0SRick Macklem 				nfsrv_cache_session(nd, &m);
48505a39c2cSRick Macklem 			if (nd->nd_repstat == NFSERR_REPLYFROMCACHE) {
486c59e4cc3SRick Macklem 				nd->nd_repstat = 0;
48705a39c2cSRick Macklem 				if (m != NULL) {
48805a39c2cSRick Macklem 					m_freem(nd->nd_mreq);
48905a39c2cSRick Macklem 					nd->nd_mreq = m;
49005a39c2cSRick Macklem 				}
49105a39c2cSRick Macklem 			}
492c59e4cc3SRick Macklem 			cacherep = RC_REPLY;
493c59e4cc3SRick Macklem 		} else {
4949ec7b004SRick Macklem 			if (nd->nd_repstat == NFSERR_DONTREPLY)
4959ec7b004SRick Macklem 				cacherep = RC_DROPIT;
4969ec7b004SRick Macklem 			else
4979ec7b004SRick Macklem 				cacherep = RC_REPLY;
498d473bac7SAlexander Motin 			*rpp = nfsrvd_updatecache(nd);
4999ec7b004SRick Macklem 		}
500c59e4cc3SRick Macklem 	}
501c59e4cc3SRick Macklem 	if (tagstr != NULL && taglen > NFSV4_SMALLSTR)
502c59e4cc3SRick Macklem 		free(tagstr, M_TEMP);
503a9285ae5SZack Kirsch 
504a9285ae5SZack Kirsch 	NFSEXITCODE2(0, nd);
5059ec7b004SRick Macklem 	return (cacherep);
5069ec7b004SRick Macklem }
5079ec7b004SRick Macklem 
508d473bac7SAlexander Motin static void
509d473bac7SAlexander Motin nfssvc_loss(SVCXPRT *xprt)
510d473bac7SAlexander Motin {
511d473bac7SAlexander Motin 	uint32_t ack;
512d473bac7SAlexander Motin 
513d473bac7SAlexander Motin 	ack = 0;
514d473bac7SAlexander Motin 	SVC_ACK(xprt, &ack);
5157e44856eSRick Macklem 	NFSD_CURVNET_SET(NFSD_TD_TO_VNET(curthread));
516d473bac7SAlexander Motin 	nfsrc_trimcache(xprt->xp_sockref, ack, 1);
5177e44856eSRick Macklem 	NFSD_CURVNET_RESTORE();
518d473bac7SAlexander Motin }
519d473bac7SAlexander Motin 
5209ec7b004SRick Macklem /*
5219ec7b004SRick Macklem  * Adds a socket to the list for servicing by nfsds.
5229ec7b004SRick Macklem  */
5239ec7b004SRick Macklem int
5249ec7b004SRick Macklem nfsrvd_addsock(struct file *fp)
5259ec7b004SRick Macklem {
5269ec7b004SRick Macklem 	int siz;
5279ec7b004SRick Macklem 	struct socket *so;
528a9285ae5SZack Kirsch 	int error = 0;
5299ec7b004SRick Macklem 	SVCXPRT *xprt;
5309ec7b004SRick Macklem 	static u_int64_t sockref = 0;
5319ec7b004SRick Macklem 
5329ec7b004SRick Macklem 	so = fp->f_data;
5339ec7b004SRick Macklem 
5349ec7b004SRick Macklem 	siz = sb_max_adj;
5359ec7b004SRick Macklem 	error = soreserve(so, siz, siz);
536a9285ae5SZack Kirsch 	if (error)
537a9285ae5SZack Kirsch 		goto out;
5389ec7b004SRick Macklem 
5399ec7b004SRick Macklem 	/*
5409ec7b004SRick Macklem 	 * Steal the socket from userland so that it doesn't close
5419ec7b004SRick Macklem 	 * unexpectedly.
5429ec7b004SRick Macklem 	 */
5439ec7b004SRick Macklem 	if (so->so_type == SOCK_DGRAM)
5447e44856eSRick Macklem 		xprt = svc_dg_create(NFSD_VNET(nfsrvd_pool), so, 0, 0);
5459ec7b004SRick Macklem 	else
5467e44856eSRick Macklem 		xprt = svc_vc_create(NFSD_VNET(nfsrvd_pool), so, 0, 0);
5479ec7b004SRick Macklem 	if (xprt) {
5489ec7b004SRick Macklem 		fp->f_ops = &badfileops;
5499ec7b004SRick Macklem 		fp->f_data = NULL;
5509ec7b004SRick Macklem 		xprt->xp_sockref = ++sockref;
5517e44856eSRick Macklem 		if (NFSD_VNET(nfs_minvers) == NFS_VER2)
55210a5a16eSRick Macklem 			svc_reg(xprt, NFS_PROG, NFS_VER2, nfssvc_program,
55310a5a16eSRick Macklem 			    NULL);
5547e44856eSRick Macklem 		if (NFSD_VNET(nfs_minvers) <= NFS_VER3 &&
5557e44856eSRick Macklem 		    NFSD_VNET(nfs_maxvers) >= NFS_VER3)
55610a5a16eSRick Macklem 			svc_reg(xprt, NFS_PROG, NFS_VER3, nfssvc_program,
55710a5a16eSRick Macklem 			    NULL);
5587e44856eSRick Macklem 		if (NFSD_VNET(nfs_maxvers) >= NFS_VER4)
55910a5a16eSRick Macklem 			svc_reg(xprt, NFS_PROG, NFS_VER4, nfssvc_program,
56010a5a16eSRick Macklem 			    NULL);
561d473bac7SAlexander Motin 		if (so->so_type == SOCK_STREAM)
562d473bac7SAlexander Motin 			svc_loss_reg(xprt, nfssvc_loss);
56376b30a0cSRick Macklem 		SVC_RELEASE(xprt);
5647e44856eSRick Macklem 	} else
5657e44856eSRick Macklem 		error = EPERM;
5669ec7b004SRick Macklem 
567a9285ae5SZack Kirsch out:
568a9285ae5SZack Kirsch 	NFSEXITCODE(error);
569a9285ae5SZack Kirsch 	return (error);
5709ec7b004SRick Macklem }
5719ec7b004SRick Macklem 
5729ec7b004SRick Macklem /*
5739ec7b004SRick Macklem  * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
5749ec7b004SRick Macklem  * until it is killed by a signal.
5759ec7b004SRick Macklem  */
5769ec7b004SRick Macklem int
5779ec7b004SRick Macklem nfsrvd_nfsd(struct thread *td, struct nfsd_nfsd_args *args)
5789ec7b004SRick Macklem {
5797e745519SRick Macklem 	char principal[MAXHOSTNAMELEN + 5];
5801bc93bb7SKonstantin Belousov 	struct proc *p;
581a9285ae5SZack Kirsch 	int error = 0;
5829ec7b004SRick Macklem 	bool_t ret2, ret3, ret4;
5839ec7b004SRick Macklem 
5847e745519SRick Macklem 	error = copyinstr(args->principal, principal, sizeof (principal),
5857e745519SRick Macklem 	    NULL);
5869ec7b004SRick Macklem 	if (error)
587a9285ae5SZack Kirsch 		goto out;
5889ec7b004SRick Macklem 
5899ec7b004SRick Macklem 	/*
5909ec7b004SRick Macklem 	 * Only the first nfsd actually does any work. The RPC code
5919ec7b004SRick Macklem 	 * adds threads to it as needed. Any extra processes offered
5929ec7b004SRick Macklem 	 * by nfsd just exit. If nfsd is new enough, it will call us
5939ec7b004SRick Macklem 	 * once with a structure that specifies how many threads to
5949ec7b004SRick Macklem 	 * use.
5959ec7b004SRick Macklem 	 */
5969ec7b004SRick Macklem 	NFSD_LOCK();
5977e44856eSRick Macklem 	if (NFSD_VNET(nfsrv_numnfsd) == 0) {
59890d2dfabSRick Macklem 		nfsdev_time = time_second;
5991bc93bb7SKonstantin Belousov 		p = td->td_proc;
6001bc93bb7SKonstantin Belousov 		PROC_LOCK(p);
6011bc93bb7SKonstantin Belousov 		p->p_flag2 |= P2_AST_SU;
6021bc93bb7SKonstantin Belousov 		PROC_UNLOCK(p);
6037e44856eSRick Macklem 		newnfs_numnfsd++;	/* Total num for all vnets. */
6047e44856eSRick Macklem 		NFSD_VNET(nfsrv_numnfsd)++;	/* Num for this vnet. */
6059ec7b004SRick Macklem 
6069ec7b004SRick Macklem 		NFSD_UNLOCK();
60790d2dfabSRick Macklem 		error = nfsrv_createdevids(args, td);
60890d2dfabSRick Macklem 		if (error == 0) {
6097e745519SRick Macklem 			/* An empty string implies AUTH_SYS only. */
6107e745519SRick Macklem 			if (principal[0] != '\0') {
6117e7fd7d1SRick Macklem 				ret2 = rpc_gss_set_svc_name_call(principal,
61290d2dfabSRick Macklem 				    "kerberosv5", GSS_C_INDEFINITE, NFS_PROG,
61390d2dfabSRick Macklem 				    NFS_VER2);
6147e7fd7d1SRick Macklem 				ret3 = rpc_gss_set_svc_name_call(principal,
61590d2dfabSRick Macklem 				    "kerberosv5", GSS_C_INDEFINITE, NFS_PROG,
61690d2dfabSRick Macklem 				    NFS_VER3);
6177e7fd7d1SRick Macklem 				ret4 = rpc_gss_set_svc_name_call(principal,
61890d2dfabSRick Macklem 				    "kerberosv5", GSS_C_INDEFINITE, NFS_PROG,
61990d2dfabSRick Macklem 				    NFS_VER4);
6209ec7b004SRick Macklem 
6217e7fd7d1SRick Macklem 				if (!ret2 || !ret3 || !ret4)
6227e44856eSRick Macklem 					printf("nfsd: can't register svc "
6237e44856eSRick Macklem 					    "name %s jid:%d\n", principal,
6247e44856eSRick Macklem 					    td->td_ucred->cr_prison->pr_id);
6259ec7b004SRick Macklem 			}
6269ec7b004SRick Macklem 
6277e44856eSRick Macklem 			NFSD_VNET(nfsrvd_pool)->sp_minthreads =
6287e44856eSRick Macklem 			    args->minthreads;
6297e44856eSRick Macklem 			NFSD_VNET(nfsrvd_pool)->sp_maxthreads =
6307e44856eSRick Macklem 			    args->maxthreads;
6319ec7b004SRick Macklem 
6323e5ba2e1SRick Macklem 			/*
6333e5ba2e1SRick Macklem 			 * If this is a pNFS service, make Getattr do a
6343e5ba2e1SRick Macklem 			 * vn_start_write(), so it can do a vn_set_extattr().
6353e5ba2e1SRick Macklem 			 */
6363e5ba2e1SRick Macklem 			if (nfsrv_devidcnt > 0) {
6373e5ba2e1SRick Macklem 				nfsrv_writerpc[NFSPROC_GETATTR] = 1;
6383e5ba2e1SRick Macklem 				nfsv4_opflag[NFSV4OP_GETATTR].modifyfs = 1;
6393e5ba2e1SRick Macklem 			}
6403e5ba2e1SRick Macklem 
6417e44856eSRick Macklem 			svc_run(NFSD_VNET(nfsrvd_pool));
6429ec7b004SRick Macklem 
6433e5ba2e1SRick Macklem 			/* Reset Getattr to not do a vn_start_write(). */
6443e5ba2e1SRick Macklem 			nfsrv_writerpc[NFSPROC_GETATTR] = 0;
6453e5ba2e1SRick Macklem 			nfsv4_opflag[NFSV4OP_GETATTR].modifyfs = 0;
6463e5ba2e1SRick Macklem 
6477e745519SRick Macklem 			if (principal[0] != '\0') {
6487e7fd7d1SRick Macklem 				rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER2);
6497e7fd7d1SRick Macklem 				rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER3);
6507e7fd7d1SRick Macklem 				rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER4);
6517e745519SRick Macklem 			}
65290d2dfabSRick Macklem 		}
6539ec7b004SRick Macklem 		NFSD_LOCK();
6549ec7b004SRick Macklem 		newnfs_numnfsd--;
6557e44856eSRick Macklem 		NFSD_VNET(nfsrv_numnfsd)--;
6569ec7b004SRick Macklem 		nfsrvd_init(1);
6571bc93bb7SKonstantin Belousov 		PROC_LOCK(p);
6581bc93bb7SKonstantin Belousov 		p->p_flag2 &= ~P2_AST_SU;
6591bc93bb7SKonstantin Belousov 		PROC_UNLOCK(p);
6609ec7b004SRick Macklem 	}
6619ec7b004SRick Macklem 	NFSD_UNLOCK();
6629ec7b004SRick Macklem 
663a9285ae5SZack Kirsch out:
664a9285ae5SZack Kirsch 	NFSEXITCODE(error);
665a9285ae5SZack Kirsch 	return (error);
6669ec7b004SRick Macklem }
6679ec7b004SRick Macklem 
6689ec7b004SRick Macklem /*
6699ec7b004SRick Macklem  * Initialize the data structures for the server.
6709ec7b004SRick Macklem  * Handshake with any new nfsds starting up to avoid any chance of
6719ec7b004SRick Macklem  * corruption.
6729ec7b004SRick Macklem  */
6739ec7b004SRick Macklem void
6749ec7b004SRick Macklem nfsrvd_init(int terminating)
6759ec7b004SRick Macklem {
6769ec7b004SRick Macklem 
6779ec7b004SRick Macklem 	NFSD_LOCK_ASSERT();
6789ec7b004SRick Macklem 
6799ec7b004SRick Macklem 	if (terminating) {
6807e44856eSRick Macklem 		NFSD_VNET(nfsd_master_proc) = NULL;
6819ec7b004SRick Macklem 		NFSD_UNLOCK();
68290d2dfabSRick Macklem 		nfsrv_freealllayoutsanddevids();
683a0962bf8SRick Macklem 		nfsrv_freeallbackchannel_xprts();
6847e44856eSRick Macklem 		svcpool_close(NFSD_VNET(nfsrvd_pool));
685acc6e58dSRick Macklem 		free(nfsrv_zeropnfsdat, M_TEMP);
686acc6e58dSRick Macklem 		nfsrv_zeropnfsdat = NULL;
6879ec7b004SRick Macklem 		NFSD_LOCK();
68890f90687SAndriy Gapon 	} else {
6897e44856eSRick Macklem 		/* Initialize per-vnet globals once per vnet. */
6907e44856eSRick Macklem 		if (NFSD_VNET(nfsrvd_inited))
6917e44856eSRick Macklem 			return;
6927e44856eSRick Macklem 		NFSD_VNET(nfsrvd_inited) = true;
6939ec7b004SRick Macklem 		NFSD_UNLOCK();
6947e44856eSRick Macklem 		NFSD_VNET(nfsrvd_pool) = svcpool_create("nfsd",
69590f90687SAndriy Gapon 		    SYSCTL_STATIC_CHILDREN(_vfs_nfsd));
6967e44856eSRick Macklem 		NFSD_VNET(nfsrvd_pool)->sp_rcache = NULL;
6977e44856eSRick Macklem 		NFSD_VNET(nfsrvd_pool)->sp_assign = fhanew_assign;
6987e44856eSRick Macklem 		NFSD_VNET(nfsrvd_pool)->sp_done = fhanew_nd_complete;
6999ec7b004SRick Macklem 		NFSD_LOCK();
7009ec7b004SRick Macklem 	}
70190f90687SAndriy Gapon }
702