xref: /freebsd-13-stable/sys/fs/nfsserver/nfs_nfsdkrpc.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Rick Macklem at The University of Guelph.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  */
35 
36 #include <sys/cdefs.h>
37 #include "opt_inet6.h"
38 #include "opt_kgssapi.h"
39 #include "opt_kern_tls.h"
40 
41 #include <fs/nfs/nfsport.h>
42 
43 #include <rpc/rpc.h>
44 #include <rpc/rpcsec_gss.h>
45 #include <rpc/rpcsec_tls.h>
46 
47 #include <fs/nfsserver/nfs_fha_new.h>
48 
49 #include <security/mac/mac_framework.h>
50 
51 NFSDLOCKMUTEX;
52 NFSV4ROOTLOCKMUTEX;
53 char *nfsrv_zeropnfsdat = NULL;
54 
55 /*
56  * Mapping of old NFS Version 2 RPC numbers to generic numbers.
57  */
58 int newnfs_nfsv3_procid[NFS_V3NPROCS] = {
59 	NFSPROC_NULL,
60 	NFSPROC_GETATTR,
61 	NFSPROC_SETATTR,
62 	NFSPROC_NOOP,
63 	NFSPROC_LOOKUP,
64 	NFSPROC_READLINK,
65 	NFSPROC_READ,
66 	NFSPROC_NOOP,
67 	NFSPROC_WRITE,
68 	NFSPROC_CREATE,
69 	NFSPROC_REMOVE,
70 	NFSPROC_RENAME,
71 	NFSPROC_LINK,
72 	NFSPROC_SYMLINK,
73 	NFSPROC_MKDIR,
74 	NFSPROC_RMDIR,
75 	NFSPROC_READDIR,
76 	NFSPROC_FSSTAT,
77 	NFSPROC_NOOP,
78 	NFSPROC_NOOP,
79 	NFSPROC_NOOP,
80 	NFSPROC_NOOP,
81 };
82 
83 SYSCTL_DECL(_vfs_nfsd);
84 
85 NFSD_VNET_DEFINE_STATIC(int, nfs_privport) = 0;
86 SYSCTL_INT(_vfs_nfsd, OID_AUTO, nfs_privport, CTLFLAG_NFSD_VNET | CTLFLAG_RWTUN,
87     &NFSD_VNET_NAME(nfs_privport), 0,
88     "Only allow clients using a privileged port for NFSv2, 3 and 4");
89 
90 NFSD_VNET_DEFINE_STATIC(int, nfs_minvers) = NFS_VER2;
91 SYSCTL_INT(_vfs_nfsd, OID_AUTO, server_min_nfsvers,
92     CTLFLAG_NFSD_VNET | CTLFLAG_RWTUN, &NFSD_VNET_NAME(nfs_minvers), 0,
93     "The lowest version of NFS handled by the server");
94 
95 NFSD_VNET_DEFINE_STATIC(int, nfs_maxvers) = NFS_VER4;
96 SYSCTL_INT(_vfs_nfsd, OID_AUTO, server_max_nfsvers,
97     CTLFLAG_NFSD_VNET | CTLFLAG_RWTUN, &NFSD_VNET_NAME(nfs_maxvers), 0,
98     "The highest version of NFS handled by the server");
99 
100 static int nfs_proc(struct nfsrv_descript *, u_int32_t, SVCXPRT *xprt,
101     struct nfsrvcache **);
102 
103 extern u_long sb_max_adj;
104 extern int newnfs_numnfsd;
105 extern time_t nfsdev_time;
106 extern int nfsrv_writerpc[NFS_NPROCS];
107 extern volatile int nfsrv_devidcnt;
108 extern struct nfsv4_opflag nfsv4_opflag[NFSV42_NOPS];
109 
110 NFSD_VNET_DECLARE(struct proc *, nfsd_master_proc);
111 
112 NFSD_VNET_DEFINE(SVCPOOL *, nfsrvd_pool);
113 NFSD_VNET_DEFINE(int, nfsrv_numnfsd) = 0;
114 NFSD_VNET_DEFINE(struct nfsv4lock, nfsd_suspend_lock);
115 
116 NFSD_VNET_DEFINE_STATIC(bool, nfsrvd_inited) = false;
117 
118 /*
119  * NFS server system calls
120  */
121 
122 static void
nfssvc_program(struct svc_req * rqst,SVCXPRT * xprt)123 nfssvc_program(struct svc_req *rqst, SVCXPRT *xprt)
124 {
125 	struct nfsrv_descript nd;
126 	struct nfsrvcache *rp = NULL;
127 	int cacherep, credflavor;
128 #ifdef KERN_TLS
129 	u_int maxlen;
130 #endif
131 
132 	NFSD_CURVNET_SET_QUIET(NFSD_TD_TO_VNET(curthread));
133 	memset(&nd, 0, sizeof(nd));
134 	if (rqst->rq_vers == NFS_VER2) {
135 		if (rqst->rq_proc > NFSV2PROC_STATFS ||
136 		    newnfs_nfsv3_procid[rqst->rq_proc] == NFSPROC_NOOP) {
137 			svcerr_noproc(rqst);
138 			svc_freereq(rqst);
139 			goto out;
140 		}
141 		nd.nd_procnum = newnfs_nfsv3_procid[rqst->rq_proc];
142 		nd.nd_flag = ND_NFSV2;
143 	} else if (rqst->rq_vers == NFS_VER3) {
144 		if (rqst->rq_proc >= NFS_V3NPROCS) {
145 			svcerr_noproc(rqst);
146 			svc_freereq(rqst);
147 			goto out;
148 		}
149 		nd.nd_procnum = rqst->rq_proc;
150 		nd.nd_flag = ND_NFSV3;
151 	} else {
152 		if (rqst->rq_proc != NFSPROC_NULL &&
153 		    rqst->rq_proc != NFSV4PROC_COMPOUND) {
154 			svcerr_noproc(rqst);
155 			svc_freereq(rqst);
156 			goto out;
157 		}
158 		nd.nd_procnum = rqst->rq_proc;
159 		nd.nd_flag = ND_NFSV4;
160 	}
161 
162 	/*
163 	 * Note: we want rq_addr, not svc_getrpccaller for nd_nam2 -
164 	 * NFS_SRVMAXDATA uses a NULL value for nd_nam2 to detect TCP
165 	 * mounts.
166 	 */
167 	nd.nd_mrep = rqst->rq_args;
168 	rqst->rq_args = NULL;
169 	newnfs_realign(&nd.nd_mrep, M_WAITOK);
170 	nd.nd_md = nd.nd_mrep;
171 	nd.nd_dpos = mtod(nd.nd_md, caddr_t);
172 	nd.nd_nam = svc_getrpccaller(rqst);
173 	nd.nd_nam2 = rqst->rq_addr;
174 	nd.nd_mreq = NULL;
175 	nd.nd_cred = NULL;
176 
177 	if (NFSD_VNET(nfs_privport) != 0) {
178 		/* Check if source port is privileged */
179 		u_short port;
180 		struct sockaddr *nam = nd.nd_nam;
181 		struct sockaddr_in *sin;
182 
183 		sin = (struct sockaddr_in *)nam;
184 		/*
185 		 * INET/INET6 - same code:
186 		 *    sin_port and sin6_port are at same offset
187 		 */
188 		port = ntohs(sin->sin_port);
189 		if (port >= IPPORT_RESERVED &&
190 		    nd.nd_procnum != NFSPROC_NULL) {
191 #ifdef INET6
192 			char buf[INET6_ADDRSTRLEN];
193 #else
194 			char buf[INET_ADDRSTRLEN];
195 #endif
196 #ifdef INET6
197 #if defined(KLD_MODULE)
198 			/* Do not use ip6_sprintf: the nfs module should work without INET6. */
199 #define	ip6_sprintf(buf, a)						\
200 			(sprintf((buf), "%x:%x:%x:%x:%x:%x:%x:%x",	\
201 			    (a)->s6_addr16[0], (a)->s6_addr16[1],	\
202 			    (a)->s6_addr16[2], (a)->s6_addr16[3],	\
203 			    (a)->s6_addr16[4], (a)->s6_addr16[5],	\
204 			    (a)->s6_addr16[6], (a)->s6_addr16[7]),	\
205 			    (buf))
206 #endif
207 #endif
208 			printf("NFS request from unprivileged port (%s:%d)\n",
209 #ifdef INET6
210 			    sin->sin_family == AF_INET6 ?
211 			    ip6_sprintf(buf, &satosin6(sin)->sin6_addr) :
212 #if defined(KLD_MODULE)
213 #undef ip6_sprintf
214 #endif
215 #endif
216 			    inet_ntoa_r(sin->sin_addr, buf), port);
217 			svcerr_weakauth(rqst);
218 			svc_freereq(rqst);
219 			m_freem(nd.nd_mrep);
220 			goto out;
221 		}
222 	}
223 
224 	if (nd.nd_procnum != NFSPROC_NULL) {
225 		if (!svc_getcred(rqst, &nd.nd_cred, &credflavor)) {
226 			svcerr_weakauth(rqst);
227 			svc_freereq(rqst);
228 			m_freem(nd.nd_mrep);
229 			goto out;
230 		}
231 
232 		/* Set the flag based on credflavor */
233 		if (credflavor == RPCSEC_GSS_KRB5) {
234 			nd.nd_flag |= ND_GSS;
235 		} else if (credflavor == RPCSEC_GSS_KRB5I) {
236 			nd.nd_flag |= (ND_GSS | ND_GSSINTEGRITY);
237 		} else if (credflavor == RPCSEC_GSS_KRB5P) {
238 			nd.nd_flag |= (ND_GSS | ND_GSSPRIVACY);
239 		} else if (credflavor != AUTH_SYS) {
240 			svcerr_weakauth(rqst);
241 			svc_freereq(rqst);
242 			m_freem(nd.nd_mrep);
243 			goto out;
244 		}
245 
246 		if ((xprt->xp_tls & RPCTLS_FLAGS_HANDSHAKE) != 0) {
247 			nd.nd_flag |= ND_TLS;
248 			if ((xprt->xp_tls & RPCTLS_FLAGS_VERIFIED) != 0)
249 				nd.nd_flag |= ND_TLSCERT;
250 			if ((xprt->xp_tls & RPCTLS_FLAGS_CERTUSER) != 0)
251 				nd.nd_flag |= ND_TLSCERTUSER;
252 		}
253 		nd.nd_maxextsiz = 16384;
254 #ifdef MAC
255 		mac_cred_associate_nfsd(nd.nd_cred);
256 #endif
257 		/*
258 		 * Get a refcnt (shared lock) on nfsd_suspend_lock.
259 		 * NFSSVC_SUSPENDNFSD will take an exclusive lock on
260 		 * nfsd_suspend_lock to suspend these threads.
261 		 * The call to nfsv4_lock() that precedes nfsv4_getref()
262 		 * ensures that the acquisition of the exclusive lock
263 		 * takes priority over acquisition of the shared lock by
264 		 * waiting for any exclusive lock request to complete.
265 		 * This must be done here, before the check of
266 		 * nfsv4root exports by nfsvno_v4rootexport().
267 		 */
268 		NFSLOCKV4ROOTMUTEX();
269 		nfsv4_lock(&NFSD_VNET(nfsd_suspend_lock), 0, NULL,
270 		    NFSV4ROOTLOCKMUTEXPTR, NULL);
271 		nfsv4_getref(&NFSD_VNET(nfsd_suspend_lock), NULL,
272 		    NFSV4ROOTLOCKMUTEXPTR, NULL);
273 		NFSUNLOCKV4ROOTMUTEX();
274 
275 		if ((nd.nd_flag & ND_NFSV4) != 0) {
276 			nd.nd_repstat = nfsvno_v4rootexport(&nd);
277 			if (nd.nd_repstat != 0) {
278 				NFSLOCKV4ROOTMUTEX();
279 				nfsv4_relref(&NFSD_VNET(nfsd_suspend_lock));
280 				NFSUNLOCKV4ROOTMUTEX();
281 				svcerr_weakauth(rqst);
282 				svc_freereq(rqst);
283 				m_freem(nd.nd_mrep);
284 				goto out;
285 			}
286 		}
287 
288 #ifdef KERN_TLS
289 		if ((xprt->xp_tls & RPCTLS_FLAGS_HANDSHAKE) != 0 &&
290 		    rpctls_getinfo(&maxlen, false, false))
291 			nd.nd_maxextsiz = maxlen;
292 #endif
293 		cacherep = nfs_proc(&nd, rqst->rq_xid, xprt, &rp);
294 		NFSLOCKV4ROOTMUTEX();
295 		nfsv4_relref(&NFSD_VNET(nfsd_suspend_lock));
296 		NFSUNLOCKV4ROOTMUTEX();
297 	} else {
298 		NFSMGET(nd.nd_mreq);
299 		nd.nd_mreq->m_len = 0;
300 		cacherep = RC_REPLY;
301 	}
302 	if (nd.nd_mrep != NULL)
303 		m_freem(nd.nd_mrep);
304 
305 	if (nd.nd_cred != NULL)
306 		crfree(nd.nd_cred);
307 
308 	if (cacherep == RC_DROPIT) {
309 		if (nd.nd_mreq != NULL)
310 			m_freem(nd.nd_mreq);
311 		svc_freereq(rqst);
312 		goto out;
313 	}
314 
315 	if (nd.nd_mreq == NULL) {
316 		svcerr_decode(rqst);
317 		svc_freereq(rqst);
318 		goto out;
319 	}
320 
321 	if (nd.nd_repstat & NFSERR_AUTHERR) {
322 		svcerr_auth(rqst, nd.nd_repstat & ~NFSERR_AUTHERR);
323 		if (nd.nd_mreq != NULL)
324 			m_freem(nd.nd_mreq);
325 	} else if (!svc_sendreply_mbuf(rqst, nd.nd_mreq)) {
326 		svcerr_systemerr(rqst);
327 	}
328 	if (rp != NULL) {
329 		nfsrvd_sentcache(rp, (rqst->rq_reply_seq != 0 ||
330 		    SVC_ACK(xprt, NULL)), rqst->rq_reply_seq);
331 	}
332 	svc_freereq(rqst);
333 
334 out:
335 	NFSD_CURVNET_RESTORE();
336 	td_softdep_cleanup(curthread);
337 	NFSEXITCODE(0);
338 }
339 
340 /*
341  * Check the cache and, optionally, do the RPC.
342  * Return the appropriate cache response.
343  */
344 static int
nfs_proc(struct nfsrv_descript * nd,u_int32_t xid,SVCXPRT * xprt,struct nfsrvcache ** rpp)345 nfs_proc(struct nfsrv_descript *nd, u_int32_t xid, SVCXPRT *xprt,
346     struct nfsrvcache **rpp)
347 {
348 	int cacherep = RC_DOIT, isdgram, taglen = -1;
349 	struct mbuf *m;
350 	u_char tag[NFSV4_SMALLSTR + 1], *tagstr = NULL;
351 	u_int32_t minorvers = 0;
352 	uint32_t ack;
353 
354 	*rpp = NULL;
355 	if (nd->nd_nam2 == NULL) {
356 		nd->nd_flag |= ND_STREAMSOCK;
357 		isdgram = 0;
358 	} else {
359 		isdgram = 1;
360 	}
361 
362 	/*
363 	 * Two cases:
364 	 * 1 - For NFSv2 over UDP, if we are near our malloc/mget
365 	 *     limit, just drop the request. There is no
366 	 *     NFSERR_RESOURCE or NFSERR_DELAY for NFSv2 and the
367 	 *     client will timeout/retry over UDP in a little while.
368 	 * 2 - nd_repstat == 0 && nd_mreq == NULL, which
369 	 *     means a normal nfs rpc, so check the cache
370 	 */
371 	if ((nd->nd_flag & ND_NFSV2) && nd->nd_nam2 != NULL &&
372 	    nfsrv_mallocmget_limit()) {
373 		cacherep = RC_DROPIT;
374 	} else {
375 		/*
376 		 * For NFSv3, play it safe and assume that the client is
377 		 * doing retries on the same TCP connection.
378 		 */
379 		if ((nd->nd_flag & (ND_NFSV4 | ND_STREAMSOCK)) ==
380 		    ND_STREAMSOCK)
381 			nd->nd_flag |= ND_SAMETCPCONN;
382 		nd->nd_retxid = xid;
383 		nd->nd_tcpconntime = NFSD_MONOSEC;
384 		nd->nd_sockref = xprt->xp_sockref;
385 		if ((nd->nd_flag & ND_NFSV4) != 0)
386 			nfsd_getminorvers(nd, tag, &tagstr, &taglen,
387 			    &minorvers);
388 		if ((nd->nd_flag & ND_NFSV41) != 0)
389 			/* NFSv4.1 caches replies in the session slots. */
390 			cacherep = RC_DOIT;
391 		else {
392 			cacherep = nfsrvd_getcache(nd);
393 			ack = 0;
394 			SVC_ACK(xprt, &ack);
395 			nfsrc_trimcache(xprt->xp_sockref, ack, 0);
396 		}
397 	}
398 
399 	/*
400 	 * Handle the request. There are three cases.
401 	 * RC_DOIT - do the RPC
402 	 * RC_REPLY - return the reply already created
403 	 * RC_DROPIT - just throw the request away
404 	 */
405 	if (cacherep == RC_DOIT) {
406 		if ((nd->nd_flag & ND_NFSV41) != 0)
407 			nd->nd_xprt = xprt;
408 		nfsrvd_dorpc(nd, isdgram, tagstr, taglen, minorvers);
409 		if ((nd->nd_flag & ND_NFSV41) != 0) {
410 			if (nd->nd_repstat != NFSERR_REPLYFROMCACHE &&
411 			    (nd->nd_flag & ND_SAVEREPLY) != 0) {
412 				/* Cache a copy of the reply. */
413 				m = m_copym(nd->nd_mreq, 0, M_COPYALL,
414 				    M_WAITOK);
415 			} else
416 				m = NULL;
417 			if ((nd->nd_flag & ND_HASSEQUENCE) != 0)
418 				nfsrv_cache_session(nd, &m);
419 			if (nd->nd_repstat == NFSERR_REPLYFROMCACHE) {
420 				nd->nd_repstat = 0;
421 				if (m != NULL) {
422 					m_freem(nd->nd_mreq);
423 					nd->nd_mreq = m;
424 				}
425 			}
426 			cacherep = RC_REPLY;
427 		} else {
428 			if (nd->nd_repstat == NFSERR_DONTREPLY)
429 				cacherep = RC_DROPIT;
430 			else
431 				cacherep = RC_REPLY;
432 			*rpp = nfsrvd_updatecache(nd);
433 		}
434 	}
435 	if (tagstr != NULL && taglen > NFSV4_SMALLSTR)
436 		free(tagstr, M_TEMP);
437 
438 	NFSEXITCODE2(0, nd);
439 	return (cacherep);
440 }
441 
442 static void
nfssvc_loss(SVCXPRT * xprt)443 nfssvc_loss(SVCXPRT *xprt)
444 {
445 	uint32_t ack;
446 
447 	ack = 0;
448 	SVC_ACK(xprt, &ack);
449 	NFSD_CURVNET_SET(NFSD_TD_TO_VNET(curthread));
450 	nfsrc_trimcache(xprt->xp_sockref, ack, 1);
451 	NFSD_CURVNET_RESTORE();
452 }
453 
454 /*
455  * Adds a socket to the list for servicing by nfsds.
456  */
457 int
nfsrvd_addsock(struct file * fp)458 nfsrvd_addsock(struct file *fp)
459 {
460 	int siz;
461 	struct socket *so;
462 	int error = 0;
463 	SVCXPRT *xprt;
464 	static u_int64_t sockref = 0;
465 
466 	so = fp->f_data;
467 
468 	siz = sb_max_adj;
469 	error = soreserve(so, siz, siz);
470 	if (error)
471 		goto out;
472 
473 	/*
474 	 * Steal the socket from userland so that it doesn't close
475 	 * unexpectedly.
476 	 */
477 	if (so->so_type == SOCK_DGRAM)
478 		xprt = svc_dg_create(NFSD_VNET(nfsrvd_pool), so, 0, 0);
479 	else
480 		xprt = svc_vc_create(NFSD_VNET(nfsrvd_pool), so, 0, 0);
481 	if (xprt) {
482 		fp->f_ops = &badfileops;
483 		fp->f_data = NULL;
484 		xprt->xp_sockref = ++sockref;
485 		if (NFSD_VNET(nfs_minvers) == NFS_VER2)
486 			svc_reg(xprt, NFS_PROG, NFS_VER2, nfssvc_program,
487 			    NULL);
488 		if (NFSD_VNET(nfs_minvers) <= NFS_VER3 &&
489 		    NFSD_VNET(nfs_maxvers) >= NFS_VER3)
490 			svc_reg(xprt, NFS_PROG, NFS_VER3, nfssvc_program,
491 			    NULL);
492 		if (NFSD_VNET(nfs_maxvers) >= NFS_VER4)
493 			svc_reg(xprt, NFS_PROG, NFS_VER4, nfssvc_program,
494 			    NULL);
495 		if (so->so_type == SOCK_STREAM)
496 			svc_loss_reg(xprt, nfssvc_loss);
497 		SVC_RELEASE(xprt);
498 	} else
499 		error = EPERM;
500 
501 out:
502 	NFSEXITCODE(error);
503 	return (error);
504 }
505 
506 /*
507  * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
508  * until it is killed by a signal.
509  */
510 int
nfsrvd_nfsd(struct thread * td,struct nfsd_nfsd_args * args)511 nfsrvd_nfsd(struct thread *td, struct nfsd_nfsd_args *args)
512 {
513 	char principal[MAXHOSTNAMELEN + 5];
514 	struct proc *p;
515 	int error = 0;
516 	bool_t ret2, ret3, ret4;
517 
518 	error = copyinstr(args->principal, principal, sizeof (principal),
519 	    NULL);
520 	if (error)
521 		goto out;
522 
523 	/*
524 	 * Only the first nfsd actually does any work. The RPC code
525 	 * adds threads to it as needed. Any extra processes offered
526 	 * by nfsd just exit. If nfsd is new enough, it will call us
527 	 * once with a structure that specifies how many threads to
528 	 * use.
529 	 */
530 	NFSD_LOCK();
531 	if (NFSD_VNET(nfsrv_numnfsd) == 0) {
532 		nfsdev_time = time_second;
533 		p = td->td_proc;
534 		PROC_LOCK(p);
535 		p->p_flag2 |= P2_AST_SU;
536 		PROC_UNLOCK(p);
537 		newnfs_numnfsd++;	/* Total num for all vnets. */
538 		NFSD_VNET(nfsrv_numnfsd)++;	/* Num for this vnet. */
539 
540 		NFSD_UNLOCK();
541 		error = nfsrv_createdevids(args, td);
542 		if (error == 0) {
543 			/* An empty string implies AUTH_SYS only. */
544 			if (principal[0] != '\0') {
545 				ret2 = rpc_gss_set_svc_name_call(principal,
546 				    "kerberosv5", GSS_C_INDEFINITE, NFS_PROG,
547 				    NFS_VER2);
548 				ret3 = rpc_gss_set_svc_name_call(principal,
549 				    "kerberosv5", GSS_C_INDEFINITE, NFS_PROG,
550 				    NFS_VER3);
551 				ret4 = rpc_gss_set_svc_name_call(principal,
552 				    "kerberosv5", GSS_C_INDEFINITE, NFS_PROG,
553 				    NFS_VER4);
554 
555 				if (!ret2 || !ret3 || !ret4)
556 					printf("nfsd: can't register svc "
557 					    "name %s jid:%d\n", principal,
558 					    td->td_ucred->cr_prison->pr_id);
559 			}
560 
561 			NFSD_VNET(nfsrvd_pool)->sp_minthreads =
562 			    args->minthreads;
563 			NFSD_VNET(nfsrvd_pool)->sp_maxthreads =
564 			    args->maxthreads;
565 
566 			/*
567 			 * If this is a pNFS service, make Getattr do a
568 			 * vn_start_write(), so it can do a vn_set_extattr().
569 			 */
570 			if (nfsrv_devidcnt > 0) {
571 				nfsrv_writerpc[NFSPROC_GETATTR] = 1;
572 				nfsv4_opflag[NFSV4OP_GETATTR].modifyfs = 1;
573 			}
574 
575 			svc_run(NFSD_VNET(nfsrvd_pool));
576 
577 			/* Reset Getattr to not do a vn_start_write(). */
578 			nfsrv_writerpc[NFSPROC_GETATTR] = 0;
579 			nfsv4_opflag[NFSV4OP_GETATTR].modifyfs = 0;
580 
581 			if (principal[0] != '\0') {
582 				rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER2);
583 				rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER3);
584 				rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER4);
585 			}
586 		}
587 		NFSD_LOCK();
588 		newnfs_numnfsd--;
589 		NFSD_VNET(nfsrv_numnfsd)--;
590 		nfsrvd_init(1);
591 		PROC_LOCK(p);
592 		p->p_flag2 &= ~P2_AST_SU;
593 		PROC_UNLOCK(p);
594 	}
595 	NFSD_UNLOCK();
596 
597 out:
598 	NFSEXITCODE(error);
599 	return (error);
600 }
601 
602 /*
603  * Initialize the data structures for the server.
604  * Handshake with any new nfsds starting up to avoid any chance of
605  * corruption.
606  */
607 void
nfsrvd_init(int terminating)608 nfsrvd_init(int terminating)
609 {
610 
611 	NFSD_LOCK_ASSERT();
612 
613 	if (terminating) {
614 		NFSD_VNET(nfsd_master_proc) = NULL;
615 		NFSD_UNLOCK();
616 		nfsrv_freealllayoutsanddevids();
617 		nfsrv_freeallbackchannel_xprts();
618 		svcpool_close(NFSD_VNET(nfsrvd_pool));
619 		free(nfsrv_zeropnfsdat, M_TEMP);
620 		nfsrv_zeropnfsdat = NULL;
621 		NFSD_LOCK();
622 	} else {
623 		/* Initialize per-vnet globals once per vnet. */
624 		if (NFSD_VNET(nfsrvd_inited))
625 			return;
626 		NFSD_VNET(nfsrvd_inited) = true;
627 		NFSD_UNLOCK();
628 		NFSD_VNET(nfsrvd_pool) = svcpool_create("nfsd",
629 		    SYSCTL_STATIC_CHILDREN(_vfs_nfsd));
630 		NFSD_VNET(nfsrvd_pool)->sp_rcache = NULL;
631 		NFSD_VNET(nfsrvd_pool)->sp_assign = fhanew_assign;
632 		NFSD_VNET(nfsrvd_pool)->sp_done = fhanew_nd_complete;
633 		NFSD_LOCK();
634 	}
635 }
636