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