xref: /titanic_41/usr/src/uts/common/fs/nfs/nfs_server.c (revision c037192b037119c9fd354732fc29b38ce097d356)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2011 Bayard G. Bell. All rights reserved.
24  * Copyright (c) 2013 by Delphix. All rights reserved.
25  * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
26  */
27 
28 /*
29  *	Copyright (c) 1983,1984,1985,1986,1987,1988,1989  AT&T.
30  *	All rights reserved.
31  *	Use is subject to license terms.
32  */
33 
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/systm.h>
37 #include <sys/cred.h>
38 #include <sys/proc.h>
39 #include <sys/user.h>
40 #include <sys/buf.h>
41 #include <sys/vfs.h>
42 #include <sys/vnode.h>
43 #include <sys/pathname.h>
44 #include <sys/uio.h>
45 #include <sys/file.h>
46 #include <sys/stat.h>
47 #include <sys/errno.h>
48 #include <sys/socket.h>
49 #include <sys/sysmacros.h>
50 #include <sys/siginfo.h>
51 #include <sys/tiuser.h>
52 #include <sys/statvfs.h>
53 #include <sys/stream.h>
54 #include <sys/strsun.h>
55 #include <sys/strsubr.h>
56 #include <sys/stropts.h>
57 #include <sys/timod.h>
58 #include <sys/t_kuser.h>
59 #include <sys/kmem.h>
60 #include <sys/kstat.h>
61 #include <sys/dirent.h>
62 #include <sys/cmn_err.h>
63 #include <sys/debug.h>
64 #include <sys/unistd.h>
65 #include <sys/vtrace.h>
66 #include <sys/mode.h>
67 #include <sys/acl.h>
68 #include <sys/sdt.h>
69 
70 #include <rpc/types.h>
71 #include <rpc/auth.h>
72 #include <rpc/auth_unix.h>
73 #include <rpc/auth_des.h>
74 #include <rpc/svc.h>
75 #include <rpc/xdr.h>
76 #include <rpc/rpc_rdma.h>
77 
78 #include <nfs/nfs.h>
79 #include <nfs/export.h>
80 #include <nfs/nfssys.h>
81 #include <nfs/nfs_clnt.h>
82 #include <nfs/nfs_acl.h>
83 #include <nfs/nfs_log.h>
84 #include <nfs/nfs_cmd.h>
85 #include <nfs/lm.h>
86 #include <nfs/nfs_dispatch.h>
87 #include <nfs/nfs4_drc.h>
88 
89 #include <sys/modctl.h>
90 #include <sys/cladm.h>
91 #include <sys/clconf.h>
92 
93 #include <sys/tsol/label.h>
94 
95 #define	MAXHOST 32
96 const char *kinet_ntop6(uchar_t *, char *, size_t);
97 
98 /*
99  * Module linkage information.
100  */
101 
102 static struct modlmisc modlmisc = {
103 	&mod_miscops, "NFS server module"
104 };
105 
106 static struct modlinkage modlinkage = {
107 	MODREV_1, (void *)&modlmisc, NULL
108 };
109 
110 kmem_cache_t *nfs_xuio_cache;
111 int nfs_loaned_buffers = 0;
112 
113 int
114 _init(void)
115 {
116 	int status;
117 
118 	if ((status = nfs_srvinit()) != 0) {
119 		cmn_err(CE_WARN, "_init: nfs_srvinit failed");
120 		return (status);
121 	}
122 
123 	status = mod_install((struct modlinkage *)&modlinkage);
124 	if (status != 0) {
125 		/*
126 		 * Could not load module, cleanup previous
127 		 * initialization work.
128 		 */
129 		nfs_srvfini();
130 
131 		return (status);
132 	}
133 
134 	/*
135 	 * Initialise some placeholders for nfssys() calls. These have
136 	 * to be declared by the nfs module, since that handles nfssys()
137 	 * calls - also used by NFS clients - but are provided by this
138 	 * nfssrv module. These also then serve as confirmation to the
139 	 * relevant code in nfs that nfssrv has been loaded, as they're
140 	 * initially NULL.
141 	 */
142 	nfs_srv_quiesce_func = nfs_srv_quiesce_all;
143 	nfs_srv_dss_func = rfs4_dss_setpaths;
144 
145 	/* setup DSS paths here; must be done before initial server startup */
146 	rfs4_dss_paths = rfs4_dss_oldpaths = NULL;
147 
148 	/* initialize the copy reduction caches */
149 
150 	nfs_xuio_cache = kmem_cache_create("nfs_xuio_cache",
151 	    sizeof (nfs_xuio_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
152 
153 	return (status);
154 }
155 
156 int
157 _fini()
158 {
159 	return (EBUSY);
160 }
161 
162 int
163 _info(struct modinfo *modinfop)
164 {
165 	return (mod_info(&modlinkage, modinfop));
166 }
167 
168 /*
169  * PUBLICFH_CHECK() checks if the dispatch routine supports
170  * RPC_PUBLICFH_OK, if the filesystem is exported public, and if the
171  * incoming request is using the public filehandle. The check duplicates
172  * the exportmatch() call done in checkexport(), and we should consider
173  * modifying those routines to avoid the duplication. For now, we optimize
174  * by calling exportmatch() only after checking that the dispatch routine
175  * supports RPC_PUBLICFH_OK, and if the filesystem is explicitly exported
176  * public (i.e., not the placeholder).
177  */
178 #define	PUBLICFH_CHECK(disp, exi, fsid, xfid) \
179 		((disp->dis_flags & RPC_PUBLICFH_OK) && \
180 		((exi->exi_export.ex_flags & EX_PUBLIC) || \
181 		(exi == exi_public && exportmatch(exi_root, \
182 		fsid, xfid))))
183 
184 static void	nfs_srv_shutdown_all(int);
185 static void	rfs4_server_start(int);
186 static void	nullfree(void);
187 static void	rfs_dispatch(struct svc_req *, SVCXPRT *);
188 static void	acl_dispatch(struct svc_req *, SVCXPRT *);
189 static void	common_dispatch(struct svc_req *, SVCXPRT *,
190 		rpcvers_t, rpcvers_t, char *,
191 		struct rpc_disptable *);
192 static void	hanfsv4_failover(void);
193 static	int	checkauth(struct exportinfo *, struct svc_req *, cred_t *, int,
194 			bool_t);
195 static char	*client_name(struct svc_req *req);
196 static char	*client_addr(struct svc_req *req, char *buf);
197 extern	int	sec_svc_getcred(struct svc_req *, cred_t *cr, char **, int *);
198 extern	bool_t	sec_svc_inrootlist(int, caddr_t, int, caddr_t *);
199 
200 #define	NFSLOG_COPY_NETBUF(exi, xprt, nb)	{		\
201 	(nb)->maxlen = (xprt)->xp_rtaddr.maxlen;		\
202 	(nb)->len = (xprt)->xp_rtaddr.len;			\
203 	(nb)->buf = kmem_alloc((nb)->len, KM_SLEEP);		\
204 	bcopy((xprt)->xp_rtaddr.buf, (nb)->buf, (nb)->len);	\
205 	}
206 
207 /*
208  * Public Filehandle common nfs routines
209  */
210 static int	MCLpath(char **);
211 static void	URLparse(char *);
212 
213 /*
214  * NFS callout table.
215  * This table is used by svc_getreq() to dispatch a request with
216  * a given prog/vers pair to an appropriate service provider
217  * dispatch routine.
218  *
219  * NOTE: ordering is relied upon below when resetting the version min/max
220  * for NFS_PROGRAM.  Careful, if this is ever changed.
221  */
222 static SVC_CALLOUT __nfs_sc_clts[] = {
223 	{ NFS_PROGRAM,	   NFS_VERSMIN,	    NFS_VERSMAX,	rfs_dispatch },
224 	{ NFS_ACL_PROGRAM, NFS_ACL_VERSMIN, NFS_ACL_VERSMAX,	acl_dispatch }
225 };
226 
227 static SVC_CALLOUT_TABLE nfs_sct_clts = {
228 	sizeof (__nfs_sc_clts) / sizeof (__nfs_sc_clts[0]), FALSE,
229 	__nfs_sc_clts
230 };
231 
232 static SVC_CALLOUT __nfs_sc_cots[] = {
233 	{ NFS_PROGRAM,	   NFS_VERSMIN,	    NFS_VERSMAX,	rfs_dispatch },
234 	{ NFS_ACL_PROGRAM, NFS_ACL_VERSMIN, NFS_ACL_VERSMAX,	acl_dispatch }
235 };
236 
237 static SVC_CALLOUT_TABLE nfs_sct_cots = {
238 	sizeof (__nfs_sc_cots) / sizeof (__nfs_sc_cots[0]), FALSE, __nfs_sc_cots
239 };
240 
241 static SVC_CALLOUT __nfs_sc_rdma[] = {
242 	{ NFS_PROGRAM,	   NFS_VERSMIN,	    NFS_VERSMAX,	rfs_dispatch },
243 	{ NFS_ACL_PROGRAM, NFS_ACL_VERSMIN, NFS_ACL_VERSMAX,	acl_dispatch }
244 };
245 
246 static SVC_CALLOUT_TABLE nfs_sct_rdma = {
247 	sizeof (__nfs_sc_rdma) / sizeof (__nfs_sc_rdma[0]), FALSE, __nfs_sc_rdma
248 };
249 rpcvers_t nfs_versmin = NFS_VERSMIN_DEFAULT;
250 rpcvers_t nfs_versmax = NFS_VERSMAX_DEFAULT;
251 
252 /*
253  * Used to track the state of the server so that initialization
254  * can be done properly.
255  */
256 typedef enum {
257 	NFS_SERVER_STOPPED,	/* server state destroyed */
258 	NFS_SERVER_STOPPING,	/* server state being destroyed */
259 	NFS_SERVER_RUNNING,
260 	NFS_SERVER_QUIESCED,	/* server state preserved */
261 	NFS_SERVER_OFFLINE	/* server pool offline */
262 } nfs_server_running_t;
263 
264 static nfs_server_running_t nfs_server_upordown;
265 static kmutex_t nfs_server_upordown_lock;
266 static	kcondvar_t nfs_server_upordown_cv;
267 
268 /*
269  * DSS: distributed stable storage
270  * lists of all DSS paths: current, and before last warmstart
271  */
272 nvlist_t *rfs4_dss_paths, *rfs4_dss_oldpaths;
273 
274 int rfs4_dispatch(struct rpcdisp *, struct svc_req *, SVCXPRT *, char *);
275 bool_t rfs4_minorvers_mismatch(struct svc_req *, SVCXPRT *, void *);
276 
277 /*
278  * RDMA wait variables.
279  */
280 static kcondvar_t rdma_wait_cv;
281 static kmutex_t rdma_wait_mutex;
282 
283 /*
284  * Will be called at the point the server pool is being unregistered
285  * from the pool list. From that point onwards, the pool is waiting
286  * to be drained and as such the server state is stale and pertains
287  * to the old instantiation of the NFS server pool.
288  */
289 void
290 nfs_srv_offline(void)
291 {
292 	mutex_enter(&nfs_server_upordown_lock);
293 	if (nfs_server_upordown == NFS_SERVER_RUNNING) {
294 		nfs_server_upordown = NFS_SERVER_OFFLINE;
295 	}
296 	mutex_exit(&nfs_server_upordown_lock);
297 }
298 
299 /*
300  * Will be called at the point the server pool is being destroyed so
301  * all transports have been closed and no service threads are in
302  * existence.
303  *
304  * If we quiesce the server, we're shutting it down without destroying the
305  * server state. This allows it to warm start subsequently.
306  */
307 void
308 nfs_srv_stop_all(void)
309 {
310 	int quiesce = 0;
311 	nfs_srv_shutdown_all(quiesce);
312 }
313 
314 /*
315  * This alternative shutdown routine can be requested via nfssys()
316  */
317 void
318 nfs_srv_quiesce_all(void)
319 {
320 	int quiesce = 1;
321 	nfs_srv_shutdown_all(quiesce);
322 }
323 
324 static void
325 nfs_srv_shutdown_all(int quiesce) {
326 	mutex_enter(&nfs_server_upordown_lock);
327 	if (quiesce) {
328 		if (nfs_server_upordown == NFS_SERVER_RUNNING ||
329 			nfs_server_upordown == NFS_SERVER_OFFLINE) {
330 			nfs_server_upordown = NFS_SERVER_QUIESCED;
331 			cv_signal(&nfs_server_upordown_cv);
332 
333 			/* reset DSS state, for subsequent warm restart */
334 			rfs4_dss_numnewpaths = 0;
335 			rfs4_dss_newpaths = NULL;
336 
337 			cmn_err(CE_NOTE, "nfs_server: server is now quiesced; "
338 			    "NFSv4 state has been preserved");
339 		}
340 	} else {
341 		if (nfs_server_upordown == NFS_SERVER_OFFLINE) {
342 			nfs_server_upordown = NFS_SERVER_STOPPING;
343 			mutex_exit(&nfs_server_upordown_lock);
344 			rfs4_state_fini();
345 			rfs4_fini_drc(nfs4_drc);
346 			mutex_enter(&nfs_server_upordown_lock);
347 			nfs_server_upordown = NFS_SERVER_STOPPED;
348 			cv_signal(&nfs_server_upordown_cv);
349 		}
350 	}
351 	mutex_exit(&nfs_server_upordown_lock);
352 }
353 
354 static int
355 nfs_srv_set_sc_versions(struct file *fp, SVC_CALLOUT_TABLE **sctpp,
356 			rpcvers_t versmin, rpcvers_t versmax)
357 {
358 	struct strioctl strioc;
359 	struct T_info_ack tinfo;
360 	int		error, retval;
361 
362 	/*
363 	 * Find out what type of transport this is.
364 	 */
365 	strioc.ic_cmd = TI_GETINFO;
366 	strioc.ic_timout = -1;
367 	strioc.ic_len = sizeof (tinfo);
368 	strioc.ic_dp = (char *)&tinfo;
369 	tinfo.PRIM_type = T_INFO_REQ;
370 
371 	error = strioctl(fp->f_vnode, I_STR, (intptr_t)&strioc, 0, K_TO_K,
372 	    CRED(), &retval);
373 	if (error || retval)
374 		return (error);
375 
376 	/*
377 	 * Based on our query of the transport type...
378 	 *
379 	 * Reset the min/max versions based on the caller's request
380 	 * NOTE: This assumes that NFS_PROGRAM is first in the array!!
381 	 * And the second entry is the NFS_ACL_PROGRAM.
382 	 */
383 	switch (tinfo.SERV_type) {
384 	case T_CLTS:
385 		if (versmax == NFS_V4)
386 			return (EINVAL);
387 		__nfs_sc_clts[0].sc_versmin = versmin;
388 		__nfs_sc_clts[0].sc_versmax = versmax;
389 		__nfs_sc_clts[1].sc_versmin = versmin;
390 		__nfs_sc_clts[1].sc_versmax = versmax;
391 		*sctpp = &nfs_sct_clts;
392 		break;
393 	case T_COTS:
394 	case T_COTS_ORD:
395 		__nfs_sc_cots[0].sc_versmin = versmin;
396 		__nfs_sc_cots[0].sc_versmax = versmax;
397 		/* For the NFS_ACL program, check the max version */
398 		if (versmax > NFS_ACL_VERSMAX)
399 			versmax = NFS_ACL_VERSMAX;
400 		__nfs_sc_cots[1].sc_versmin = versmin;
401 		__nfs_sc_cots[1].sc_versmax = versmax;
402 		*sctpp = &nfs_sct_cots;
403 		break;
404 	default:
405 		error = EINVAL;
406 	}
407 
408 	return (error);
409 }
410 
411 /*
412  * NFS Server system call.
413  * Does all of the work of running a NFS server.
414  * uap->fd is the fd of an open transport provider
415  */
416 int
417 nfs_svc(struct nfs_svc_args *arg, model_t model)
418 {
419 	file_t *fp;
420 	SVCMASTERXPRT *xprt;
421 	int error;
422 	int readsize;
423 	char buf[KNC_STRSIZE];
424 	size_t len;
425 	STRUCT_HANDLE(nfs_svc_args, uap);
426 	struct netbuf addrmask;
427 	SVC_CALLOUT_TABLE *sctp = NULL;
428 
429 #ifdef lint
430 	model = model;		/* STRUCT macros don't always refer to it */
431 #endif
432 
433 	STRUCT_SET_HANDLE(uap, model, arg);
434 
435 	/* Check privileges in nfssys() */
436 
437 	if ((fp = getf(STRUCT_FGET(uap, fd))) == NULL)
438 		return (EBADF);
439 
440 	/*
441 	 * Set read buffer size to rsize
442 	 * and add room for RPC headers.
443 	 */
444 	readsize = nfs3tsize() + (RPC_MAXDATASIZE - NFS_MAXDATA);
445 	if (readsize < RPC_MAXDATASIZE)
446 		readsize = RPC_MAXDATASIZE;
447 
448 	error = copyinstr((const char *)STRUCT_FGETP(uap, netid), buf,
449 	    KNC_STRSIZE, &len);
450 	if (error) {
451 		releasef(STRUCT_FGET(uap, fd));
452 		return (error);
453 	}
454 
455 	addrmask.len = STRUCT_FGET(uap, addrmask.len);
456 	addrmask.maxlen = STRUCT_FGET(uap, addrmask.maxlen);
457 	addrmask.buf = kmem_alloc(addrmask.maxlen, KM_SLEEP);
458 	error = copyin(STRUCT_FGETP(uap, addrmask.buf), addrmask.buf,
459 	    addrmask.len);
460 	if (error) {
461 		releasef(STRUCT_FGET(uap, fd));
462 		kmem_free(addrmask.buf, addrmask.maxlen);
463 		return (error);
464 	}
465 
466 	nfs_versmin = STRUCT_FGET(uap, versmin);
467 	nfs_versmax = STRUCT_FGET(uap, versmax);
468 
469 	/* Double check the vers min/max ranges */
470 	if ((nfs_versmin > nfs_versmax) ||
471 	    (nfs_versmin < NFS_VERSMIN) ||
472 	    (nfs_versmax > NFS_VERSMAX)) {
473 		nfs_versmin = NFS_VERSMIN_DEFAULT;
474 		nfs_versmax = NFS_VERSMAX_DEFAULT;
475 	}
476 
477 	if (error =
478 	    nfs_srv_set_sc_versions(fp, &sctp, nfs_versmin, nfs_versmax)) {
479 		releasef(STRUCT_FGET(uap, fd));
480 		kmem_free(addrmask.buf, addrmask.maxlen);
481 		return (error);
482 	}
483 
484 	/* Initialize nfsv4 server */
485 	if (nfs_versmax == (rpcvers_t)NFS_V4)
486 		rfs4_server_start(STRUCT_FGET(uap, delegation));
487 
488 	/* Create a transport handle. */
489 	error = svc_tli_kcreate(fp, readsize, buf, &addrmask, &xprt,
490 	    sctp, NULL, NFS_SVCPOOL_ID, TRUE);
491 
492 	if (error)
493 		kmem_free(addrmask.buf, addrmask.maxlen);
494 
495 	releasef(STRUCT_FGET(uap, fd));
496 
497 	/* HA-NFSv4: save the cluster nodeid */
498 	if (cluster_bootflags & CLUSTER_BOOTED)
499 		lm_global_nlmid = clconf_get_nodeid();
500 
501 	return (error);
502 }
503 
504 static void
505 rfs4_server_start(int nfs4_srv_delegation)
506 {
507 	/*
508 	 * Determine if the server has previously been "started" and
509 	 * if not, do the per instance initialization
510 	 */
511 	mutex_enter(&nfs_server_upordown_lock);
512 
513 	if (nfs_server_upordown != NFS_SERVER_RUNNING) {
514 		/* Do we need to stop and wait on the previous server? */
515 		while (nfs_server_upordown == NFS_SERVER_STOPPING ||
516 		    nfs_server_upordown == NFS_SERVER_OFFLINE)
517 			cv_wait(&nfs_server_upordown_cv,
518 			    &nfs_server_upordown_lock);
519 
520 		if (nfs_server_upordown != NFS_SERVER_RUNNING) {
521 			(void) svc_pool_control(NFS_SVCPOOL_ID,
522 			    SVCPSET_UNREGISTER_PROC, (void *)&nfs_srv_offline);
523 			(void) svc_pool_control(NFS_SVCPOOL_ID,
524 			    SVCPSET_SHUTDOWN_PROC, (void *)&nfs_srv_stop_all);
525 
526 			/* is this an nfsd warm start? */
527 			if (nfs_server_upordown == NFS_SERVER_QUIESCED) {
528 				cmn_err(CE_NOTE, "nfs_server: "
529 				    "server was previously quiesced; "
530 				    "existing NFSv4 state will be re-used");
531 
532 				/*
533 				 * HA-NFSv4: this is also the signal
534 				 * that a Resource Group failover has
535 				 * occurred.
536 				 */
537 				if (cluster_bootflags & CLUSTER_BOOTED)
538 					hanfsv4_failover();
539 			} else {
540 				/* cold start */
541 				rfs4_state_init();
542 				nfs4_drc = rfs4_init_drc(nfs4_drc_max,
543 				    nfs4_drc_hash);
544 			}
545 
546 			/*
547 			 * Check to see if delegation is to be
548 			 * enabled at the server
549 			 */
550 			if (nfs4_srv_delegation != FALSE)
551 				rfs4_set_deleg_policy(SRV_NORMAL_DELEGATE);
552 
553 			nfs_server_upordown = NFS_SERVER_RUNNING;
554 		}
555 		cv_signal(&nfs_server_upordown_cv);
556 	}
557 	mutex_exit(&nfs_server_upordown_lock);
558 }
559 
560 /*
561  * If RDMA device available,
562  * start RDMA listener.
563  */
564 int
565 rdma_start(struct rdma_svc_args *rsa)
566 {
567 	int error;
568 	rdma_xprt_group_t started_rdma_xprts;
569 	rdma_stat stat;
570 	int svc_state = 0;
571 
572 	/* Double check the vers min/max ranges */
573 	if ((rsa->nfs_versmin > rsa->nfs_versmax) ||
574 	    (rsa->nfs_versmin < NFS_VERSMIN) ||
575 	    (rsa->nfs_versmax > NFS_VERSMAX)) {
576 		rsa->nfs_versmin = NFS_VERSMIN_DEFAULT;
577 		rsa->nfs_versmax = NFS_VERSMAX_DEFAULT;
578 	}
579 	nfs_versmin = rsa->nfs_versmin;
580 	nfs_versmax = rsa->nfs_versmax;
581 
582 	/* Set the versions in the callout table */
583 	__nfs_sc_rdma[0].sc_versmin = rsa->nfs_versmin;
584 	__nfs_sc_rdma[0].sc_versmax = rsa->nfs_versmax;
585 	/* For the NFS_ACL program, check the max version */
586 	__nfs_sc_rdma[1].sc_versmin = rsa->nfs_versmin;
587 	if (rsa->nfs_versmax > NFS_ACL_VERSMAX)
588 		__nfs_sc_rdma[1].sc_versmax = NFS_ACL_VERSMAX;
589 	else
590 		__nfs_sc_rdma[1].sc_versmax = rsa->nfs_versmax;
591 
592 	/* Initialize nfsv4 server */
593 	if (rsa->nfs_versmax == (rpcvers_t)NFS_V4)
594 		rfs4_server_start(rsa->delegation);
595 
596 	started_rdma_xprts.rtg_count = 0;
597 	started_rdma_xprts.rtg_listhead = NULL;
598 	started_rdma_xprts.rtg_poolid = rsa->poolid;
599 
600 restart:
601 	error = svc_rdma_kcreate(rsa->netid, &nfs_sct_rdma, rsa->poolid,
602 	    &started_rdma_xprts);
603 
604 	svc_state = !error;
605 
606 	while (!error) {
607 
608 		/*
609 		 * wait till either interrupted by a signal on
610 		 * nfs service stop/restart or signalled by a
611 		 * rdma plugin attach/detatch.
612 		 */
613 
614 		stat = rdma_kwait();
615 
616 		/*
617 		 * stop services if running -- either on a HCA detach event
618 		 * or if the nfs service is stopped/restarted.
619 		 */
620 
621 		if ((stat == RDMA_HCA_DETACH || stat == RDMA_INTR) &&
622 		    svc_state) {
623 			rdma_stop(&started_rdma_xprts);
624 			svc_state = 0;
625 		}
626 
627 		/*
628 		 * nfs service stop/restart, break out of the
629 		 * wait loop and return;
630 		 */
631 		if (stat == RDMA_INTR)
632 			return (0);
633 
634 		/*
635 		 * restart stopped services on a HCA attach event
636 		 * (if not already running)
637 		 */
638 
639 		if ((stat == RDMA_HCA_ATTACH) && (svc_state == 0))
640 			goto restart;
641 
642 		/*
643 		 * loop until a nfs service stop/restart
644 		 */
645 	}
646 
647 	return (error);
648 }
649 
650 /* ARGSUSED */
651 void
652 rpc_null(caddr_t *argp, caddr_t *resp)
653 {
654 }
655 
656 /* ARGSUSED */
657 void
658 rpc_null_v3(caddr_t *argp, caddr_t *resp, struct exportinfo *exi,
659     struct svc_req *req, cred_t *cr)
660 {
661 	DTRACE_NFSV3_3(op__null__start, struct svc_req *, req,
662 	    cred_t *, cr, vnode_t *, NULL);
663 	DTRACE_NFSV3_3(op__null__done, struct svc_req *, req,
664 	    cred_t *, cr, vnode_t *, NULL);
665 }
666 
667 /* ARGSUSED */
668 static void
669 rfs_error(caddr_t *argp, caddr_t *resp)
670 {
671 	/* return (EOPNOTSUPP); */
672 }
673 
674 static void
675 nullfree(void)
676 {
677 }
678 
679 static char *rfscallnames_v2[] = {
680 	"RFS2_NULL",
681 	"RFS2_GETATTR",
682 	"RFS2_SETATTR",
683 	"RFS2_ROOT",
684 	"RFS2_LOOKUP",
685 	"RFS2_READLINK",
686 	"RFS2_READ",
687 	"RFS2_WRITECACHE",
688 	"RFS2_WRITE",
689 	"RFS2_CREATE",
690 	"RFS2_REMOVE",
691 	"RFS2_RENAME",
692 	"RFS2_LINK",
693 	"RFS2_SYMLINK",
694 	"RFS2_MKDIR",
695 	"RFS2_RMDIR",
696 	"RFS2_READDIR",
697 	"RFS2_STATFS"
698 };
699 
700 static struct rpcdisp rfsdisptab_v2[] = {
701 	/*
702 	 * NFS VERSION 2
703 	 */
704 
705 	/* RFS_NULL = 0 */
706 	{rpc_null,
707 	    xdr_void, NULL_xdrproc_t, 0,
708 	    xdr_void, NULL_xdrproc_t, 0,
709 	    nullfree, RPC_IDEMPOTENT,
710 	    0},
711 
712 	/* RFS_GETATTR = 1 */
713 	{rfs_getattr,
714 	    xdr_fhandle, xdr_fastfhandle, sizeof (fhandle_t),
715 	    xdr_attrstat, xdr_fastattrstat, sizeof (struct nfsattrstat),
716 	    nullfree, RPC_IDEMPOTENT|RPC_ALLOWANON|RPC_MAPRESP,
717 	    rfs_getattr_getfh},
718 
719 	/* RFS_SETATTR = 2 */
720 	{rfs_setattr,
721 	    xdr_saargs, NULL_xdrproc_t, sizeof (struct nfssaargs),
722 	    xdr_attrstat, xdr_fastattrstat, sizeof (struct nfsattrstat),
723 	    nullfree, RPC_MAPRESP,
724 	    rfs_setattr_getfh},
725 
726 	/* RFS_ROOT = 3 *** NO LONGER SUPPORTED *** */
727 	{rfs_error,
728 	    xdr_void, NULL_xdrproc_t, 0,
729 	    xdr_void, NULL_xdrproc_t, 0,
730 	    nullfree, RPC_IDEMPOTENT,
731 	    0},
732 
733 	/* RFS_LOOKUP = 4 */
734 	{rfs_lookup,
735 	    xdr_diropargs, NULL_xdrproc_t, sizeof (struct nfsdiropargs),
736 	    xdr_diropres, xdr_fastdiropres, sizeof (struct nfsdiropres),
737 	    nullfree, RPC_IDEMPOTENT|RPC_MAPRESP|RPC_PUBLICFH_OK,
738 	    rfs_lookup_getfh},
739 
740 	/* RFS_READLINK = 5 */
741 	{rfs_readlink,
742 	    xdr_fhandle, xdr_fastfhandle, sizeof (fhandle_t),
743 	    xdr_rdlnres, NULL_xdrproc_t, sizeof (struct nfsrdlnres),
744 	    rfs_rlfree, RPC_IDEMPOTENT,
745 	    rfs_readlink_getfh},
746 
747 	/* RFS_READ = 6 */
748 	{rfs_read,
749 	    xdr_readargs, NULL_xdrproc_t, sizeof (struct nfsreadargs),
750 	    xdr_rdresult, NULL_xdrproc_t, sizeof (struct nfsrdresult),
751 	    rfs_rdfree, RPC_IDEMPOTENT,
752 	    rfs_read_getfh},
753 
754 	/* RFS_WRITECACHE = 7 *** NO LONGER SUPPORTED *** */
755 	{rfs_error,
756 	    xdr_void, NULL_xdrproc_t, 0,
757 	    xdr_void, NULL_xdrproc_t, 0,
758 	    nullfree, RPC_IDEMPOTENT,
759 	    0},
760 
761 	/* RFS_WRITE = 8 */
762 	{rfs_write,
763 	    xdr_writeargs, NULL_xdrproc_t, sizeof (struct nfswriteargs),
764 	    xdr_attrstat, xdr_fastattrstat, sizeof (struct nfsattrstat),
765 	    nullfree, RPC_MAPRESP,
766 	    rfs_write_getfh},
767 
768 	/* RFS_CREATE = 9 */
769 	{rfs_create,
770 	    xdr_creatargs, NULL_xdrproc_t, sizeof (struct nfscreatargs),
771 	    xdr_diropres, xdr_fastdiropres, sizeof (struct nfsdiropres),
772 	    nullfree, RPC_MAPRESP,
773 	    rfs_create_getfh},
774 
775 	/* RFS_REMOVE = 10 */
776 	{rfs_remove,
777 	    xdr_diropargs, NULL_xdrproc_t, sizeof (struct nfsdiropargs),
778 #ifdef _LITTLE_ENDIAN
779 	    xdr_enum, xdr_fastenum, sizeof (enum nfsstat),
780 #else
781 	    xdr_enum, NULL_xdrproc_t, sizeof (enum nfsstat),
782 #endif
783 	    nullfree, RPC_MAPRESP,
784 	    rfs_remove_getfh},
785 
786 	/* RFS_RENAME = 11 */
787 	{rfs_rename,
788 	    xdr_rnmargs, NULL_xdrproc_t, sizeof (struct nfsrnmargs),
789 #ifdef _LITTLE_ENDIAN
790 	    xdr_enum, xdr_fastenum, sizeof (enum nfsstat),
791 #else
792 	    xdr_enum, NULL_xdrproc_t, sizeof (enum nfsstat),
793 #endif
794 	    nullfree, RPC_MAPRESP,
795 	    rfs_rename_getfh},
796 
797 	/* RFS_LINK = 12 */
798 	{rfs_link,
799 	    xdr_linkargs, NULL_xdrproc_t, sizeof (struct nfslinkargs),
800 #ifdef _LITTLE_ENDIAN
801 	    xdr_enum, xdr_fastenum, sizeof (enum nfsstat),
802 #else
803 	    xdr_enum, NULL_xdrproc_t, sizeof (enum nfsstat),
804 #endif
805 	    nullfree, RPC_MAPRESP,
806 	    rfs_link_getfh},
807 
808 	/* RFS_SYMLINK = 13 */
809 	{rfs_symlink,
810 	    xdr_slargs, NULL_xdrproc_t, sizeof (struct nfsslargs),
811 #ifdef _LITTLE_ENDIAN
812 	    xdr_enum, xdr_fastenum, sizeof (enum nfsstat),
813 #else
814 	    xdr_enum, NULL_xdrproc_t, sizeof (enum nfsstat),
815 #endif
816 	    nullfree, RPC_MAPRESP,
817 	    rfs_symlink_getfh},
818 
819 	/* RFS_MKDIR = 14 */
820 	{rfs_mkdir,
821 	    xdr_creatargs, NULL_xdrproc_t, sizeof (struct nfscreatargs),
822 	    xdr_diropres, xdr_fastdiropres, sizeof (struct nfsdiropres),
823 	    nullfree, RPC_MAPRESP,
824 	    rfs_mkdir_getfh},
825 
826 	/* RFS_RMDIR = 15 */
827 	{rfs_rmdir,
828 	    xdr_diropargs, NULL_xdrproc_t, sizeof (struct nfsdiropargs),
829 #ifdef _LITTLE_ENDIAN
830 	    xdr_enum, xdr_fastenum, sizeof (enum nfsstat),
831 #else
832 	    xdr_enum, NULL_xdrproc_t, sizeof (enum nfsstat),
833 #endif
834 	    nullfree, RPC_MAPRESP,
835 	    rfs_rmdir_getfh},
836 
837 	/* RFS_READDIR = 16 */
838 	{rfs_readdir,
839 	    xdr_rddirargs, NULL_xdrproc_t, sizeof (struct nfsrddirargs),
840 	    xdr_putrddirres, NULL_xdrproc_t, sizeof (struct nfsrddirres),
841 	    rfs_rddirfree, RPC_IDEMPOTENT,
842 	    rfs_readdir_getfh},
843 
844 	/* RFS_STATFS = 17 */
845 	{rfs_statfs,
846 	    xdr_fhandle, xdr_fastfhandle, sizeof (fhandle_t),
847 	    xdr_statfs, xdr_faststatfs, sizeof (struct nfsstatfs),
848 	    nullfree, RPC_IDEMPOTENT|RPC_ALLOWANON|RPC_MAPRESP,
849 	    rfs_statfs_getfh},
850 };
851 
852 static char *rfscallnames_v3[] = {
853 	"RFS3_NULL",
854 	"RFS3_GETATTR",
855 	"RFS3_SETATTR",
856 	"RFS3_LOOKUP",
857 	"RFS3_ACCESS",
858 	"RFS3_READLINK",
859 	"RFS3_READ",
860 	"RFS3_WRITE",
861 	"RFS3_CREATE",
862 	"RFS3_MKDIR",
863 	"RFS3_SYMLINK",
864 	"RFS3_MKNOD",
865 	"RFS3_REMOVE",
866 	"RFS3_RMDIR",
867 	"RFS3_RENAME",
868 	"RFS3_LINK",
869 	"RFS3_READDIR",
870 	"RFS3_READDIRPLUS",
871 	"RFS3_FSSTAT",
872 	"RFS3_FSINFO",
873 	"RFS3_PATHCONF",
874 	"RFS3_COMMIT"
875 };
876 
877 static struct rpcdisp rfsdisptab_v3[] = {
878 	/*
879 	 * NFS VERSION 3
880 	 */
881 
882 	/* RFS_NULL = 0 */
883 	{rpc_null_v3,
884 	    xdr_void, NULL_xdrproc_t, 0,
885 	    xdr_void, NULL_xdrproc_t, 0,
886 	    nullfree, RPC_IDEMPOTENT,
887 	    0},
888 
889 	/* RFS3_GETATTR = 1 */
890 	{rfs3_getattr,
891 	    xdr_nfs_fh3_server, NULL_xdrproc_t, sizeof (GETATTR3args),
892 	    xdr_GETATTR3res, NULL_xdrproc_t, sizeof (GETATTR3res),
893 	    nullfree, (RPC_IDEMPOTENT | RPC_ALLOWANON),
894 	    rfs3_getattr_getfh},
895 
896 	/* RFS3_SETATTR = 2 */
897 	{rfs3_setattr,
898 	    xdr_SETATTR3args, NULL_xdrproc_t, sizeof (SETATTR3args),
899 	    xdr_SETATTR3res, NULL_xdrproc_t, sizeof (SETATTR3res),
900 	    nullfree, 0,
901 	    rfs3_setattr_getfh},
902 
903 	/* RFS3_LOOKUP = 3 */
904 	{rfs3_lookup,
905 	    xdr_diropargs3, NULL_xdrproc_t, sizeof (LOOKUP3args),
906 	    xdr_LOOKUP3res, NULL_xdrproc_t, sizeof (LOOKUP3res),
907 	    nullfree, (RPC_IDEMPOTENT | RPC_PUBLICFH_OK),
908 	    rfs3_lookup_getfh},
909 
910 	/* RFS3_ACCESS = 4 */
911 	{rfs3_access,
912 	    xdr_ACCESS3args, NULL_xdrproc_t, sizeof (ACCESS3args),
913 	    xdr_ACCESS3res, NULL_xdrproc_t, sizeof (ACCESS3res),
914 	    nullfree, RPC_IDEMPOTENT,
915 	    rfs3_access_getfh},
916 
917 	/* RFS3_READLINK = 5 */
918 	{rfs3_readlink,
919 	    xdr_nfs_fh3_server, NULL_xdrproc_t, sizeof (READLINK3args),
920 	    xdr_READLINK3res, NULL_xdrproc_t, sizeof (READLINK3res),
921 	    rfs3_readlink_free, RPC_IDEMPOTENT,
922 	    rfs3_readlink_getfh},
923 
924 	/* RFS3_READ = 6 */
925 	{rfs3_read,
926 	    xdr_READ3args, NULL_xdrproc_t, sizeof (READ3args),
927 	    xdr_READ3res, NULL_xdrproc_t, sizeof (READ3res),
928 	    rfs3_read_free, RPC_IDEMPOTENT,
929 	    rfs3_read_getfh},
930 
931 	/* RFS3_WRITE = 7 */
932 	{rfs3_write,
933 	    xdr_WRITE3args, NULL_xdrproc_t, sizeof (WRITE3args),
934 	    xdr_WRITE3res, NULL_xdrproc_t, sizeof (WRITE3res),
935 	    nullfree, 0,
936 	    rfs3_write_getfh},
937 
938 	/* RFS3_CREATE = 8 */
939 	{rfs3_create,
940 	    xdr_CREATE3args, NULL_xdrproc_t, sizeof (CREATE3args),
941 	    xdr_CREATE3res, NULL_xdrproc_t, sizeof (CREATE3res),
942 	    nullfree, 0,
943 	    rfs3_create_getfh},
944 
945 	/* RFS3_MKDIR = 9 */
946 	{rfs3_mkdir,
947 	    xdr_MKDIR3args, NULL_xdrproc_t, sizeof (MKDIR3args),
948 	    xdr_MKDIR3res, NULL_xdrproc_t, sizeof (MKDIR3res),
949 	    nullfree, 0,
950 	    rfs3_mkdir_getfh},
951 
952 	/* RFS3_SYMLINK = 10 */
953 	{rfs3_symlink,
954 	    xdr_SYMLINK3args, NULL_xdrproc_t, sizeof (SYMLINK3args),
955 	    xdr_SYMLINK3res, NULL_xdrproc_t, sizeof (SYMLINK3res),
956 	    nullfree, 0,
957 	    rfs3_symlink_getfh},
958 
959 	/* RFS3_MKNOD = 11 */
960 	{rfs3_mknod,
961 	    xdr_MKNOD3args, NULL_xdrproc_t, sizeof (MKNOD3args),
962 	    xdr_MKNOD3res, NULL_xdrproc_t, sizeof (MKNOD3res),
963 	    nullfree, 0,
964 	    rfs3_mknod_getfh},
965 
966 	/* RFS3_REMOVE = 12 */
967 	{rfs3_remove,
968 	    xdr_diropargs3, NULL_xdrproc_t, sizeof (REMOVE3args),
969 	    xdr_REMOVE3res, NULL_xdrproc_t, sizeof (REMOVE3res),
970 	    nullfree, 0,
971 	    rfs3_remove_getfh},
972 
973 	/* RFS3_RMDIR = 13 */
974 	{rfs3_rmdir,
975 	    xdr_diropargs3, NULL_xdrproc_t, sizeof (RMDIR3args),
976 	    xdr_RMDIR3res, NULL_xdrproc_t, sizeof (RMDIR3res),
977 	    nullfree, 0,
978 	    rfs3_rmdir_getfh},
979 
980 	/* RFS3_RENAME = 14 */
981 	{rfs3_rename,
982 	    xdr_RENAME3args, NULL_xdrproc_t, sizeof (RENAME3args),
983 	    xdr_RENAME3res, NULL_xdrproc_t, sizeof (RENAME3res),
984 	    nullfree, 0,
985 	    rfs3_rename_getfh},
986 
987 	/* RFS3_LINK = 15 */
988 	{rfs3_link,
989 	    xdr_LINK3args, NULL_xdrproc_t, sizeof (LINK3args),
990 	    xdr_LINK3res, NULL_xdrproc_t, sizeof (LINK3res),
991 	    nullfree, 0,
992 	    rfs3_link_getfh},
993 
994 	/* RFS3_READDIR = 16 */
995 	{rfs3_readdir,
996 	    xdr_READDIR3args, NULL_xdrproc_t, sizeof (READDIR3args),
997 	    xdr_READDIR3res, NULL_xdrproc_t, sizeof (READDIR3res),
998 	    rfs3_readdir_free, RPC_IDEMPOTENT,
999 	    rfs3_readdir_getfh},
1000 
1001 	/* RFS3_READDIRPLUS = 17 */
1002 	{rfs3_readdirplus,
1003 	    xdr_READDIRPLUS3args, NULL_xdrproc_t, sizeof (READDIRPLUS3args),
1004 	    xdr_READDIRPLUS3res, NULL_xdrproc_t, sizeof (READDIRPLUS3res),
1005 	    rfs3_readdirplus_free, RPC_AVOIDWORK,
1006 	    rfs3_readdirplus_getfh},
1007 
1008 	/* RFS3_FSSTAT = 18 */
1009 	{rfs3_fsstat,
1010 	    xdr_nfs_fh3_server, NULL_xdrproc_t, sizeof (FSSTAT3args),
1011 	    xdr_FSSTAT3res, NULL_xdrproc_t, sizeof (FSSTAT3res),
1012 	    nullfree, RPC_IDEMPOTENT,
1013 	    rfs3_fsstat_getfh},
1014 
1015 	/* RFS3_FSINFO = 19 */
1016 	{rfs3_fsinfo,
1017 	    xdr_nfs_fh3_server, NULL_xdrproc_t, sizeof (FSINFO3args),
1018 	    xdr_FSINFO3res, NULL_xdrproc_t, sizeof (FSINFO3res),
1019 	    nullfree, RPC_IDEMPOTENT|RPC_ALLOWANON,
1020 	    rfs3_fsinfo_getfh},
1021 
1022 	/* RFS3_PATHCONF = 20 */
1023 	{rfs3_pathconf,
1024 	    xdr_nfs_fh3_server, NULL_xdrproc_t, sizeof (PATHCONF3args),
1025 	    xdr_PATHCONF3res, NULL_xdrproc_t, sizeof (PATHCONF3res),
1026 	    nullfree, RPC_IDEMPOTENT,
1027 	    rfs3_pathconf_getfh},
1028 
1029 	/* RFS3_COMMIT = 21 */
1030 	{rfs3_commit,
1031 	    xdr_COMMIT3args, NULL_xdrproc_t, sizeof (COMMIT3args),
1032 	    xdr_COMMIT3res, NULL_xdrproc_t, sizeof (COMMIT3res),
1033 	    nullfree, RPC_IDEMPOTENT,
1034 	    rfs3_commit_getfh},
1035 };
1036 
1037 static char *rfscallnames_v4[] = {
1038 	"RFS4_NULL",
1039 	"RFS4_COMPOUND",
1040 	"RFS4_NULL",
1041 	"RFS4_NULL",
1042 	"RFS4_NULL",
1043 	"RFS4_NULL",
1044 	"RFS4_NULL",
1045 	"RFS4_NULL",
1046 	"RFS4_CREATE"
1047 };
1048 
1049 static struct rpcdisp rfsdisptab_v4[] = {
1050 	/*
1051 	 * NFS VERSION 4
1052 	 */
1053 
1054 	/* RFS_NULL = 0 */
1055 	{rpc_null,
1056 	    xdr_void, NULL_xdrproc_t, 0,
1057 	    xdr_void, NULL_xdrproc_t, 0,
1058 	    nullfree, RPC_IDEMPOTENT, 0},
1059 
1060 	/* RFS4_compound = 1 */
1061 	{rfs4_compound,
1062 	    xdr_COMPOUND4args_srv, NULL_xdrproc_t, sizeof (COMPOUND4args),
1063 	    xdr_COMPOUND4res_srv, NULL_xdrproc_t, sizeof (COMPOUND4res),
1064 	    rfs4_compound_free, 0, 0},
1065 };
1066 
1067 union rfs_args {
1068 	/*
1069 	 * NFS VERSION 2
1070 	 */
1071 
1072 	/* RFS_NULL = 0 */
1073 
1074 	/* RFS_GETATTR = 1 */
1075 	fhandle_t nfs2_getattr_args;
1076 
1077 	/* RFS_SETATTR = 2 */
1078 	struct nfssaargs nfs2_setattr_args;
1079 
1080 	/* RFS_ROOT = 3 *** NO LONGER SUPPORTED *** */
1081 
1082 	/* RFS_LOOKUP = 4 */
1083 	struct nfsdiropargs nfs2_lookup_args;
1084 
1085 	/* RFS_READLINK = 5 */
1086 	fhandle_t nfs2_readlink_args;
1087 
1088 	/* RFS_READ = 6 */
1089 	struct nfsreadargs nfs2_read_args;
1090 
1091 	/* RFS_WRITECACHE = 7 *** NO LONGER SUPPORTED *** */
1092 
1093 	/* RFS_WRITE = 8 */
1094 	struct nfswriteargs nfs2_write_args;
1095 
1096 	/* RFS_CREATE = 9 */
1097 	struct nfscreatargs nfs2_create_args;
1098 
1099 	/* RFS_REMOVE = 10 */
1100 	struct nfsdiropargs nfs2_remove_args;
1101 
1102 	/* RFS_RENAME = 11 */
1103 	struct nfsrnmargs nfs2_rename_args;
1104 
1105 	/* RFS_LINK = 12 */
1106 	struct nfslinkargs nfs2_link_args;
1107 
1108 	/* RFS_SYMLINK = 13 */
1109 	struct nfsslargs nfs2_symlink_args;
1110 
1111 	/* RFS_MKDIR = 14 */
1112 	struct nfscreatargs nfs2_mkdir_args;
1113 
1114 	/* RFS_RMDIR = 15 */
1115 	struct nfsdiropargs nfs2_rmdir_args;
1116 
1117 	/* RFS_READDIR = 16 */
1118 	struct nfsrddirargs nfs2_readdir_args;
1119 
1120 	/* RFS_STATFS = 17 */
1121 	fhandle_t nfs2_statfs_args;
1122 
1123 	/*
1124 	 * NFS VERSION 3
1125 	 */
1126 
1127 	/* RFS_NULL = 0 */
1128 
1129 	/* RFS3_GETATTR = 1 */
1130 	GETATTR3args nfs3_getattr_args;
1131 
1132 	/* RFS3_SETATTR = 2 */
1133 	SETATTR3args nfs3_setattr_args;
1134 
1135 	/* RFS3_LOOKUP = 3 */
1136 	LOOKUP3args nfs3_lookup_args;
1137 
1138 	/* RFS3_ACCESS = 4 */
1139 	ACCESS3args nfs3_access_args;
1140 
1141 	/* RFS3_READLINK = 5 */
1142 	READLINK3args nfs3_readlink_args;
1143 
1144 	/* RFS3_READ = 6 */
1145 	READ3args nfs3_read_args;
1146 
1147 	/* RFS3_WRITE = 7 */
1148 	WRITE3args nfs3_write_args;
1149 
1150 	/* RFS3_CREATE = 8 */
1151 	CREATE3args nfs3_create_args;
1152 
1153 	/* RFS3_MKDIR = 9 */
1154 	MKDIR3args nfs3_mkdir_args;
1155 
1156 	/* RFS3_SYMLINK = 10 */
1157 	SYMLINK3args nfs3_symlink_args;
1158 
1159 	/* RFS3_MKNOD = 11 */
1160 	MKNOD3args nfs3_mknod_args;
1161 
1162 	/* RFS3_REMOVE = 12 */
1163 	REMOVE3args nfs3_remove_args;
1164 
1165 	/* RFS3_RMDIR = 13 */
1166 	RMDIR3args nfs3_rmdir_args;
1167 
1168 	/* RFS3_RENAME = 14 */
1169 	RENAME3args nfs3_rename_args;
1170 
1171 	/* RFS3_LINK = 15 */
1172 	LINK3args nfs3_link_args;
1173 
1174 	/* RFS3_READDIR = 16 */
1175 	READDIR3args nfs3_readdir_args;
1176 
1177 	/* RFS3_READDIRPLUS = 17 */
1178 	READDIRPLUS3args nfs3_readdirplus_args;
1179 
1180 	/* RFS3_FSSTAT = 18 */
1181 	FSSTAT3args nfs3_fsstat_args;
1182 
1183 	/* RFS3_FSINFO = 19 */
1184 	FSINFO3args nfs3_fsinfo_args;
1185 
1186 	/* RFS3_PATHCONF = 20 */
1187 	PATHCONF3args nfs3_pathconf_args;
1188 
1189 	/* RFS3_COMMIT = 21 */
1190 	COMMIT3args nfs3_commit_args;
1191 
1192 	/*
1193 	 * NFS VERSION 4
1194 	 */
1195 
1196 	/* RFS_NULL = 0 */
1197 
1198 	/* COMPUND = 1 */
1199 	COMPOUND4args nfs4_compound_args;
1200 };
1201 
1202 union rfs_res {
1203 	/*
1204 	 * NFS VERSION 2
1205 	 */
1206 
1207 	/* RFS_NULL = 0 */
1208 
1209 	/* RFS_GETATTR = 1 */
1210 	struct nfsattrstat nfs2_getattr_res;
1211 
1212 	/* RFS_SETATTR = 2 */
1213 	struct nfsattrstat nfs2_setattr_res;
1214 
1215 	/* RFS_ROOT = 3 *** NO LONGER SUPPORTED *** */
1216 
1217 	/* RFS_LOOKUP = 4 */
1218 	struct nfsdiropres nfs2_lookup_res;
1219 
1220 	/* RFS_READLINK = 5 */
1221 	struct nfsrdlnres nfs2_readlink_res;
1222 
1223 	/* RFS_READ = 6 */
1224 	struct nfsrdresult nfs2_read_res;
1225 
1226 	/* RFS_WRITECACHE = 7 *** NO LONGER SUPPORTED *** */
1227 
1228 	/* RFS_WRITE = 8 */
1229 	struct nfsattrstat nfs2_write_res;
1230 
1231 	/* RFS_CREATE = 9 */
1232 	struct nfsdiropres nfs2_create_res;
1233 
1234 	/* RFS_REMOVE = 10 */
1235 	enum nfsstat nfs2_remove_res;
1236 
1237 	/* RFS_RENAME = 11 */
1238 	enum nfsstat nfs2_rename_res;
1239 
1240 	/* RFS_LINK = 12 */
1241 	enum nfsstat nfs2_link_res;
1242 
1243 	/* RFS_SYMLINK = 13 */
1244 	enum nfsstat nfs2_symlink_res;
1245 
1246 	/* RFS_MKDIR = 14 */
1247 	struct nfsdiropres nfs2_mkdir_res;
1248 
1249 	/* RFS_RMDIR = 15 */
1250 	enum nfsstat nfs2_rmdir_res;
1251 
1252 	/* RFS_READDIR = 16 */
1253 	struct nfsrddirres nfs2_readdir_res;
1254 
1255 	/* RFS_STATFS = 17 */
1256 	struct nfsstatfs nfs2_statfs_res;
1257 
1258 	/*
1259 	 * NFS VERSION 3
1260 	 */
1261 
1262 	/* RFS_NULL = 0 */
1263 
1264 	/* RFS3_GETATTR = 1 */
1265 	GETATTR3res nfs3_getattr_res;
1266 
1267 	/* RFS3_SETATTR = 2 */
1268 	SETATTR3res nfs3_setattr_res;
1269 
1270 	/* RFS3_LOOKUP = 3 */
1271 	LOOKUP3res nfs3_lookup_res;
1272 
1273 	/* RFS3_ACCESS = 4 */
1274 	ACCESS3res nfs3_access_res;
1275 
1276 	/* RFS3_READLINK = 5 */
1277 	READLINK3res nfs3_readlink_res;
1278 
1279 	/* RFS3_READ = 6 */
1280 	READ3res nfs3_read_res;
1281 
1282 	/* RFS3_WRITE = 7 */
1283 	WRITE3res nfs3_write_res;
1284 
1285 	/* RFS3_CREATE = 8 */
1286 	CREATE3res nfs3_create_res;
1287 
1288 	/* RFS3_MKDIR = 9 */
1289 	MKDIR3res nfs3_mkdir_res;
1290 
1291 	/* RFS3_SYMLINK = 10 */
1292 	SYMLINK3res nfs3_symlink_res;
1293 
1294 	/* RFS3_MKNOD = 11 */
1295 	MKNOD3res nfs3_mknod_res;
1296 
1297 	/* RFS3_REMOVE = 12 */
1298 	REMOVE3res nfs3_remove_res;
1299 
1300 	/* RFS3_RMDIR = 13 */
1301 	RMDIR3res nfs3_rmdir_res;
1302 
1303 	/* RFS3_RENAME = 14 */
1304 	RENAME3res nfs3_rename_res;
1305 
1306 	/* RFS3_LINK = 15 */
1307 	LINK3res nfs3_link_res;
1308 
1309 	/* RFS3_READDIR = 16 */
1310 	READDIR3res nfs3_readdir_res;
1311 
1312 	/* RFS3_READDIRPLUS = 17 */
1313 	READDIRPLUS3res nfs3_readdirplus_res;
1314 
1315 	/* RFS3_FSSTAT = 18 */
1316 	FSSTAT3res nfs3_fsstat_res;
1317 
1318 	/* RFS3_FSINFO = 19 */
1319 	FSINFO3res nfs3_fsinfo_res;
1320 
1321 	/* RFS3_PATHCONF = 20 */
1322 	PATHCONF3res nfs3_pathconf_res;
1323 
1324 	/* RFS3_COMMIT = 21 */
1325 	COMMIT3res nfs3_commit_res;
1326 
1327 	/*
1328 	 * NFS VERSION 4
1329 	 */
1330 
1331 	/* RFS_NULL = 0 */
1332 
1333 	/* RFS4_COMPOUND = 1 */
1334 	COMPOUND4res nfs4_compound_res;
1335 
1336 };
1337 
1338 static struct rpc_disptable rfs_disptable[] = {
1339 	{sizeof (rfsdisptab_v2) / sizeof (rfsdisptab_v2[0]),
1340 	    rfscallnames_v2,
1341 	    &rfsproccnt_v2_ptr, rfsdisptab_v2},
1342 	{sizeof (rfsdisptab_v3) / sizeof (rfsdisptab_v3[0]),
1343 	    rfscallnames_v3,
1344 	    &rfsproccnt_v3_ptr, rfsdisptab_v3},
1345 	{sizeof (rfsdisptab_v4) / sizeof (rfsdisptab_v4[0]),
1346 	    rfscallnames_v4,
1347 	    &rfsproccnt_v4_ptr, rfsdisptab_v4},
1348 };
1349 
1350 /*
1351  * If nfs_portmon is set, then clients are required to use privileged
1352  * ports (ports < IPPORT_RESERVED) in order to get NFS services.
1353  *
1354  * N.B.: this attempt to carry forward the already ill-conceived notion
1355  * of privileged ports for TCP/UDP is really quite ineffectual.  Not only
1356  * is it transport-dependent, it's laughably easy to spoof.  If you're
1357  * really interested in security, you must start with secure RPC instead.
1358  */
1359 static int nfs_portmon = 0;
1360 
1361 #ifdef DEBUG
1362 static int cred_hits = 0;
1363 static int cred_misses = 0;
1364 #endif
1365 
1366 
1367 #ifdef DEBUG
1368 /*
1369  * Debug code to allow disabling of rfs_dispatch() use of
1370  * fastxdrargs() and fastxdrres() calls for testing purposes.
1371  */
1372 static int rfs_no_fast_xdrargs = 0;
1373 static int rfs_no_fast_xdrres = 0;
1374 #endif
1375 
1376 union acl_args {
1377 	/*
1378 	 * ACL VERSION 2
1379 	 */
1380 
1381 	/* ACL2_NULL = 0 */
1382 
1383 	/* ACL2_GETACL = 1 */
1384 	GETACL2args acl2_getacl_args;
1385 
1386 	/* ACL2_SETACL = 2 */
1387 	SETACL2args acl2_setacl_args;
1388 
1389 	/* ACL2_GETATTR = 3 */
1390 	GETATTR2args acl2_getattr_args;
1391 
1392 	/* ACL2_ACCESS = 4 */
1393 	ACCESS2args acl2_access_args;
1394 
1395 	/* ACL2_GETXATTRDIR = 5 */
1396 	GETXATTRDIR2args acl2_getxattrdir_args;
1397 
1398 	/*
1399 	 * ACL VERSION 3
1400 	 */
1401 
1402 	/* ACL3_NULL = 0 */
1403 
1404 	/* ACL3_GETACL = 1 */
1405 	GETACL3args acl3_getacl_args;
1406 
1407 	/* ACL3_SETACL = 2 */
1408 	SETACL3args acl3_setacl;
1409 
1410 	/* ACL3_GETXATTRDIR = 3 */
1411 	GETXATTRDIR3args acl3_getxattrdir_args;
1412 
1413 };
1414 
1415 union acl_res {
1416 	/*
1417 	 * ACL VERSION 2
1418 	 */
1419 
1420 	/* ACL2_NULL = 0 */
1421 
1422 	/* ACL2_GETACL = 1 */
1423 	GETACL2res acl2_getacl_res;
1424 
1425 	/* ACL2_SETACL = 2 */
1426 	SETACL2res acl2_setacl_res;
1427 
1428 	/* ACL2_GETATTR = 3 */
1429 	GETATTR2res acl2_getattr_res;
1430 
1431 	/* ACL2_ACCESS = 4 */
1432 	ACCESS2res acl2_access_res;
1433 
1434 	/* ACL2_GETXATTRDIR = 5 */
1435 	GETXATTRDIR2args acl2_getxattrdir_res;
1436 
1437 	/*
1438 	 * ACL VERSION 3
1439 	 */
1440 
1441 	/* ACL3_NULL = 0 */
1442 
1443 	/* ACL3_GETACL = 1 */
1444 	GETACL3res acl3_getacl_res;
1445 
1446 	/* ACL3_SETACL = 2 */
1447 	SETACL3res acl3_setacl_res;
1448 
1449 	/* ACL3_GETXATTRDIR = 3 */
1450 	GETXATTRDIR3res acl3_getxattrdir_res;
1451 
1452 };
1453 
1454 static bool_t
1455 auth_tooweak(struct svc_req *req, char *res)
1456 {
1457 
1458 	if (req->rq_vers == NFS_VERSION && req->rq_proc == RFS_LOOKUP) {
1459 		struct nfsdiropres *dr = (struct nfsdiropres *)res;
1460 		if ((enum wnfsstat)dr->dr_status == WNFSERR_CLNT_FLAVOR)
1461 			return (TRUE);
1462 	} else if (req->rq_vers == NFS_V3 && req->rq_proc == NFSPROC3_LOOKUP) {
1463 		LOOKUP3res *resp = (LOOKUP3res *)res;
1464 		if ((enum wnfsstat)resp->status == WNFSERR_CLNT_FLAVOR)
1465 			return (TRUE);
1466 	}
1467 	return (FALSE);
1468 }
1469 
1470 
1471 static void
1472 common_dispatch(struct svc_req *req, SVCXPRT *xprt, rpcvers_t min_vers,
1473 		rpcvers_t max_vers, char *pgmname,
1474 		struct rpc_disptable *disptable)
1475 {
1476 	int which;
1477 	rpcvers_t vers;
1478 	char *args;
1479 	union {
1480 			union rfs_args ra;
1481 			union acl_args aa;
1482 		} args_buf;
1483 	char *res;
1484 	union {
1485 			union rfs_res rr;
1486 			union acl_res ar;
1487 		} res_buf;
1488 	struct rpcdisp *disp = NULL;
1489 	int dis_flags = 0;
1490 	cred_t *cr;
1491 	int error = 0;
1492 	int anon_ok;
1493 	struct exportinfo *exi = NULL;
1494 	unsigned int nfslog_rec_id;
1495 	int dupstat;
1496 	struct dupreq *dr;
1497 	int authres;
1498 	bool_t publicfh_ok = FALSE;
1499 	enum_t auth_flavor;
1500 	bool_t dupcached = FALSE;
1501 	struct netbuf	nb;
1502 	bool_t logging_enabled = FALSE;
1503 	struct exportinfo *nfslog_exi = NULL;
1504 	char **procnames;
1505 	char cbuf[INET6_ADDRSTRLEN];	/* to hold both IPv4 and IPv6 addr */
1506 
1507 	vers = req->rq_vers;
1508 
1509 	if (vers < min_vers || vers > max_vers) {
1510 		svcerr_progvers(req->rq_xprt, min_vers, max_vers);
1511 		error++;
1512 		cmn_err(CE_NOTE, "%s: bad version number %u", pgmname, vers);
1513 		goto done;
1514 	}
1515 	vers -= min_vers;
1516 
1517 	which = req->rq_proc;
1518 	if (which < 0 || which >= disptable[(int)vers].dis_nprocs) {
1519 		svcerr_noproc(req->rq_xprt);
1520 		error++;
1521 		goto done;
1522 	}
1523 
1524 	(*(disptable[(int)vers].dis_proccntp))[which].value.ui64++;
1525 
1526 	disp = &disptable[(int)vers].dis_table[which];
1527 	procnames = disptable[(int)vers].dis_procnames;
1528 
1529 	auth_flavor = req->rq_cred.oa_flavor;
1530 
1531 	/*
1532 	 * Deserialize into the args struct.
1533 	 */
1534 	args = (char *)&args_buf;
1535 
1536 #ifdef DEBUG
1537 	if (rfs_no_fast_xdrargs || (auth_flavor == RPCSEC_GSS) ||
1538 	    disp->dis_fastxdrargs == NULL_xdrproc_t ||
1539 	    !SVC_GETARGS(xprt, disp->dis_fastxdrargs, (char *)&args))
1540 #else
1541 	if ((auth_flavor == RPCSEC_GSS) ||
1542 	    disp->dis_fastxdrargs == NULL_xdrproc_t ||
1543 	    !SVC_GETARGS(xprt, disp->dis_fastxdrargs, (char *)&args))
1544 #endif
1545 	{
1546 		bzero(args, disp->dis_argsz);
1547 		if (!SVC_GETARGS(xprt, disp->dis_xdrargs, args)) {
1548 			error++;
1549 			/*
1550 			 * Check if we are outside our capabilities.
1551 			 */
1552 			if (rfs4_minorvers_mismatch(req, xprt, (void *)args))
1553 				goto done;
1554 
1555 			svcerr_decode(xprt);
1556 			cmn_err(CE_NOTE,
1557 			    "Failed to decode arguments for %s version %u "
1558 			    "procedure %s client %s%s",
1559 			    pgmname, vers + min_vers, procnames[which],
1560 			    client_name(req), client_addr(req, cbuf));
1561 			goto done;
1562 		}
1563 	}
1564 
1565 	/*
1566 	 * If Version 4 use that specific dispatch function.
1567 	 */
1568 	if (req->rq_vers == 4) {
1569 		error += rfs4_dispatch(disp, req, xprt, args);
1570 		goto done;
1571 	}
1572 
1573 	dis_flags = disp->dis_flags;
1574 
1575 	/*
1576 	 * Find export information and check authentication,
1577 	 * setting the credential if everything is ok.
1578 	 */
1579 	if (disp->dis_getfh != NULL) {
1580 		void *fh;
1581 		fsid_t *fsid;
1582 		fid_t *fid, *xfid;
1583 		fhandle_t *fh2;
1584 		nfs_fh3 *fh3;
1585 
1586 		fh = (*disp->dis_getfh)(args);
1587 		switch (req->rq_vers) {
1588 		case NFS_VERSION:
1589 			fh2 = (fhandle_t *)fh;
1590 			fsid = &fh2->fh_fsid;
1591 			fid = (fid_t *)&fh2->fh_len;
1592 			xfid = (fid_t *)&fh2->fh_xlen;
1593 			break;
1594 		case NFS_V3:
1595 			fh3 = (nfs_fh3 *)fh;
1596 			fsid = &fh3->fh3_fsid;
1597 			fid = FH3TOFIDP(fh3);
1598 			xfid = FH3TOXFIDP(fh3);
1599 			break;
1600 		}
1601 
1602 		/*
1603 		 * Fix for bug 1038302 - corbin
1604 		 * There is a problem here if anonymous access is
1605 		 * disallowed.  If the current request is part of the
1606 		 * client's mount process for the requested filesystem,
1607 		 * then it will carry root (uid 0) credentials on it, and
1608 		 * will be denied by checkauth if that client does not
1609 		 * have explicit root=0 permission.  This will cause the
1610 		 * client's mount operation to fail.  As a work-around,
1611 		 * we check here to see if the request is a getattr or
1612 		 * statfs operation on the exported vnode itself, and
1613 		 * pass a flag to checkauth with the result of this test.
1614 		 *
1615 		 * The filehandle refers to the mountpoint itself if
1616 		 * the fh_data and fh_xdata portions of the filehandle
1617 		 * are equal.
1618 		 *
1619 		 * Added anon_ok argument to checkauth().
1620 		 */
1621 
1622 		if ((dis_flags & RPC_ALLOWANON) && EQFID(fid, xfid))
1623 			anon_ok = 1;
1624 		else
1625 			anon_ok = 0;
1626 
1627 		cr = xprt->xp_cred;
1628 		ASSERT(cr != NULL);
1629 #ifdef DEBUG
1630 		if (crgetref(cr) != 1) {
1631 			crfree(cr);
1632 			cr = crget();
1633 			xprt->xp_cred = cr;
1634 			cred_misses++;
1635 		} else
1636 			cred_hits++;
1637 #else
1638 		if (crgetref(cr) != 1) {
1639 			crfree(cr);
1640 			cr = crget();
1641 			xprt->xp_cred = cr;
1642 		}
1643 #endif
1644 
1645 		exi = checkexport(fsid, xfid);
1646 
1647 		if (exi != NULL) {
1648 			publicfh_ok = PUBLICFH_CHECK(disp, exi, fsid, xfid);
1649 
1650 			/*
1651 			 * Don't allow non-V4 clients access
1652 			 * to pseudo exports
1653 			 */
1654 			if (PSEUDO(exi)) {
1655 				svcerr_weakauth(xprt);
1656 				error++;
1657 				goto done;
1658 			}
1659 
1660 			authres = checkauth(exi, req, cr, anon_ok, publicfh_ok);
1661 			/*
1662 			 * authres >  0: authentication OK - proceed
1663 			 * authres == 0: authentication weak - return error
1664 			 * authres <  0: authentication timeout - drop
1665 			 */
1666 			if (authres <= 0) {
1667 				if (authres == 0) {
1668 					svcerr_weakauth(xprt);
1669 					error++;
1670 				}
1671 				goto done;
1672 			}
1673 		}
1674 	} else
1675 		cr = NULL;
1676 
1677 	if ((dis_flags & RPC_MAPRESP) && (auth_flavor != RPCSEC_GSS)) {
1678 		res = (char *)SVC_GETRES(xprt, disp->dis_ressz);
1679 		if (res == NULL)
1680 			res = (char *)&res_buf;
1681 	} else
1682 		res = (char *)&res_buf;
1683 
1684 	if (!(dis_flags & RPC_IDEMPOTENT)) {
1685 		dupstat = SVC_DUP_EXT(xprt, req, res, disp->dis_ressz, &dr,
1686 		    &dupcached);
1687 
1688 		switch (dupstat) {
1689 		case DUP_ERROR:
1690 			svcerr_systemerr(xprt);
1691 			error++;
1692 			goto done;
1693 			/* NOTREACHED */
1694 		case DUP_INPROGRESS:
1695 			if (res != (char *)&res_buf)
1696 				SVC_FREERES(xprt);
1697 			error++;
1698 			goto done;
1699 			/* NOTREACHED */
1700 		case DUP_NEW:
1701 		case DUP_DROP:
1702 			curthread->t_flag |= T_DONTPEND;
1703 
1704 			(*disp->dis_proc)(args, res, exi, req, cr);
1705 
1706 			curthread->t_flag &= ~T_DONTPEND;
1707 			if (curthread->t_flag & T_WOULDBLOCK) {
1708 				curthread->t_flag &= ~T_WOULDBLOCK;
1709 				SVC_DUPDONE_EXT(xprt, dr, res, NULL,
1710 				    disp->dis_ressz, DUP_DROP);
1711 				if (res != (char *)&res_buf)
1712 					SVC_FREERES(xprt);
1713 				error++;
1714 				goto done;
1715 			}
1716 			if (dis_flags & RPC_AVOIDWORK) {
1717 				SVC_DUPDONE_EXT(xprt, dr, res, NULL,
1718 				    disp->dis_ressz, DUP_DROP);
1719 			} else {
1720 				SVC_DUPDONE_EXT(xprt, dr, res,
1721 				    disp->dis_resfree == nullfree ? NULL :
1722 				    disp->dis_resfree,
1723 				    disp->dis_ressz, DUP_DONE);
1724 				dupcached = TRUE;
1725 			}
1726 			break;
1727 		case DUP_DONE:
1728 			break;
1729 		}
1730 
1731 	} else {
1732 		curthread->t_flag |= T_DONTPEND;
1733 
1734 		(*disp->dis_proc)(args, res, exi, req, cr);
1735 
1736 		curthread->t_flag &= ~T_DONTPEND;
1737 		if (curthread->t_flag & T_WOULDBLOCK) {
1738 			curthread->t_flag &= ~T_WOULDBLOCK;
1739 			if (res != (char *)&res_buf)
1740 				SVC_FREERES(xprt);
1741 			error++;
1742 			goto done;
1743 		}
1744 	}
1745 
1746 	if (auth_tooweak(req, res)) {
1747 		svcerr_weakauth(xprt);
1748 		error++;
1749 		goto done;
1750 	}
1751 
1752 	/*
1753 	 * Check to see if logging has been enabled on the server.
1754 	 * If so, then obtain the export info struct to be used for
1755 	 * the later writing of the log record.  This is done for
1756 	 * the case that a lookup is done across a non-logged public
1757 	 * file system.
1758 	 */
1759 	if (nfslog_buffer_list != NULL) {
1760 		nfslog_exi = nfslog_get_exi(exi, req, res, &nfslog_rec_id);
1761 		/*
1762 		 * Is logging enabled?
1763 		 */
1764 		logging_enabled = (nfslog_exi != NULL);
1765 
1766 		/*
1767 		 * Copy the netbuf for logging purposes, before it is
1768 		 * freed by svc_sendreply().
1769 		 */
1770 		if (logging_enabled) {
1771 			NFSLOG_COPY_NETBUF(nfslog_exi, xprt, &nb);
1772 			/*
1773 			 * If RPC_MAPRESP flag set (i.e. in V2 ops) the
1774 			 * res gets copied directly into the mbuf and
1775 			 * may be freed soon after the sendreply. So we
1776 			 * must copy it here to a safe place...
1777 			 */
1778 			if (res != (char *)&res_buf) {
1779 				bcopy(res, (char *)&res_buf, disp->dis_ressz);
1780 			}
1781 		}
1782 	}
1783 
1784 	/*
1785 	 * Serialize and send results struct
1786 	 */
1787 #ifdef DEBUG
1788 	if (rfs_no_fast_xdrres == 0 && res != (char *)&res_buf)
1789 #else
1790 	if (res != (char *)&res_buf)
1791 #endif
1792 	{
1793 		if (!svc_sendreply(xprt, disp->dis_fastxdrres, res)) {
1794 			cmn_err(CE_NOTE, "%s: bad sendreply", pgmname);
1795 			svcerr_systemerr(xprt);
1796 			error++;
1797 		}
1798 	} else {
1799 		if (!svc_sendreply(xprt, disp->dis_xdrres, res)) {
1800 			cmn_err(CE_NOTE, "%s: bad sendreply", pgmname);
1801 			svcerr_systemerr(xprt);
1802 			error++;
1803 		}
1804 	}
1805 
1806 	/*
1807 	 * Log if needed
1808 	 */
1809 	if (logging_enabled) {
1810 		nfslog_write_record(nfslog_exi, req, args, (char *)&res_buf,
1811 		    cr, &nb, nfslog_rec_id, NFSLOG_ONE_BUFFER);
1812 		exi_rele(nfslog_exi);
1813 		kmem_free((&nb)->buf, (&nb)->len);
1814 	}
1815 
1816 	/*
1817 	 * Free results struct. With the addition of NFS V4 we can
1818 	 * have non-idempotent procedures with functions.
1819 	 */
1820 	if (disp->dis_resfree != nullfree && dupcached == FALSE) {
1821 		(*disp->dis_resfree)(res);
1822 	}
1823 
1824 done:
1825 	/*
1826 	 * Free arguments struct
1827 	 */
1828 	if (disp) {
1829 		if (!SVC_FREEARGS(xprt, disp->dis_xdrargs, args)) {
1830 			cmn_err(CE_NOTE, "%s: bad freeargs", pgmname);
1831 			error++;
1832 		}
1833 	} else {
1834 		if (!SVC_FREEARGS(xprt, (xdrproc_t)0, (caddr_t)0)) {
1835 			cmn_err(CE_NOTE, "%s: bad freeargs", pgmname);
1836 			error++;
1837 		}
1838 	}
1839 
1840 	if (exi != NULL)
1841 		exi_rele(exi);
1842 
1843 	global_svstat_ptr[req->rq_vers][NFS_BADCALLS].value.ui64 += error;
1844 
1845 	global_svstat_ptr[req->rq_vers][NFS_CALLS].value.ui64++;
1846 }
1847 
1848 static void
1849 rfs_dispatch(struct svc_req *req, SVCXPRT *xprt)
1850 {
1851 	common_dispatch(req, xprt, NFS_VERSMIN, NFS_VERSMAX,
1852 	    "NFS", rfs_disptable);
1853 }
1854 
1855 static char *aclcallnames_v2[] = {
1856 	"ACL2_NULL",
1857 	"ACL2_GETACL",
1858 	"ACL2_SETACL",
1859 	"ACL2_GETATTR",
1860 	"ACL2_ACCESS",
1861 	"ACL2_GETXATTRDIR"
1862 };
1863 
1864 static struct rpcdisp acldisptab_v2[] = {
1865 	/*
1866 	 * ACL VERSION 2
1867 	 */
1868 
1869 	/* ACL2_NULL = 0 */
1870 	{rpc_null,
1871 	    xdr_void, NULL_xdrproc_t, 0,
1872 	    xdr_void, NULL_xdrproc_t, 0,
1873 	    nullfree, RPC_IDEMPOTENT,
1874 	    0},
1875 
1876 	/* ACL2_GETACL = 1 */
1877 	{acl2_getacl,
1878 	    xdr_GETACL2args, xdr_fastGETACL2args, sizeof (GETACL2args),
1879 	    xdr_GETACL2res, NULL_xdrproc_t, sizeof (GETACL2res),
1880 	    acl2_getacl_free, RPC_IDEMPOTENT,
1881 	    acl2_getacl_getfh},
1882 
1883 	/* ACL2_SETACL = 2 */
1884 	{acl2_setacl,
1885 	    xdr_SETACL2args, NULL_xdrproc_t, sizeof (SETACL2args),
1886 #ifdef _LITTLE_ENDIAN
1887 	    xdr_SETACL2res, xdr_fastSETACL2res, sizeof (SETACL2res),
1888 #else
1889 	    xdr_SETACL2res, NULL_xdrproc_t, sizeof (SETACL2res),
1890 #endif
1891 	    nullfree, RPC_MAPRESP,
1892 	    acl2_setacl_getfh},
1893 
1894 	/* ACL2_GETATTR = 3 */
1895 	{acl2_getattr,
1896 	    xdr_GETATTR2args, xdr_fastGETATTR2args, sizeof (GETATTR2args),
1897 #ifdef _LITTLE_ENDIAN
1898 	    xdr_GETATTR2res, xdr_fastGETATTR2res, sizeof (GETATTR2res),
1899 #else
1900 	    xdr_GETATTR2res, NULL_xdrproc_t, sizeof (GETATTR2res),
1901 #endif
1902 	    nullfree, RPC_IDEMPOTENT|RPC_ALLOWANON|RPC_MAPRESP,
1903 	    acl2_getattr_getfh},
1904 
1905 	/* ACL2_ACCESS = 4 */
1906 	{acl2_access,
1907 	    xdr_ACCESS2args, xdr_fastACCESS2args, sizeof (ACCESS2args),
1908 #ifdef _LITTLE_ENDIAN
1909 	    xdr_ACCESS2res, xdr_fastACCESS2res, sizeof (ACCESS2res),
1910 #else
1911 	    xdr_ACCESS2res, NULL_xdrproc_t, sizeof (ACCESS2res),
1912 #endif
1913 	    nullfree, RPC_IDEMPOTENT|RPC_MAPRESP,
1914 	    acl2_access_getfh},
1915 
1916 	/* ACL2_GETXATTRDIR = 5 */
1917 	{acl2_getxattrdir,
1918 	    xdr_GETXATTRDIR2args, NULL_xdrproc_t, sizeof (GETXATTRDIR2args),
1919 	    xdr_GETXATTRDIR2res, NULL_xdrproc_t, sizeof (GETXATTRDIR2res),
1920 	    nullfree, RPC_IDEMPOTENT,
1921 	    acl2_getxattrdir_getfh},
1922 };
1923 
1924 static char *aclcallnames_v3[] = {
1925 	"ACL3_NULL",
1926 	"ACL3_GETACL",
1927 	"ACL3_SETACL",
1928 	"ACL3_GETXATTRDIR"
1929 };
1930 
1931 static struct rpcdisp acldisptab_v3[] = {
1932 	/*
1933 	 * ACL VERSION 3
1934 	 */
1935 
1936 	/* ACL3_NULL = 0 */
1937 	{rpc_null,
1938 	    xdr_void, NULL_xdrproc_t, 0,
1939 	    xdr_void, NULL_xdrproc_t, 0,
1940 	    nullfree, RPC_IDEMPOTENT,
1941 	    0},
1942 
1943 	/* ACL3_GETACL = 1 */
1944 	{acl3_getacl,
1945 	    xdr_GETACL3args, NULL_xdrproc_t, sizeof (GETACL3args),
1946 	    xdr_GETACL3res, NULL_xdrproc_t, sizeof (GETACL3res),
1947 	    acl3_getacl_free, RPC_IDEMPOTENT,
1948 	    acl3_getacl_getfh},
1949 
1950 	/* ACL3_SETACL = 2 */
1951 	{acl3_setacl,
1952 	    xdr_SETACL3args, NULL_xdrproc_t, sizeof (SETACL3args),
1953 	    xdr_SETACL3res, NULL_xdrproc_t, sizeof (SETACL3res),
1954 	    nullfree, 0,
1955 	    acl3_setacl_getfh},
1956 
1957 	/* ACL3_GETXATTRDIR = 3 */
1958 	{acl3_getxattrdir,
1959 	    xdr_GETXATTRDIR3args, NULL_xdrproc_t, sizeof (GETXATTRDIR3args),
1960 	    xdr_GETXATTRDIR3res, NULL_xdrproc_t, sizeof (GETXATTRDIR3res),
1961 	    nullfree, RPC_IDEMPOTENT,
1962 	    acl3_getxattrdir_getfh},
1963 };
1964 
1965 static struct rpc_disptable acl_disptable[] = {
1966 	{sizeof (acldisptab_v2) / sizeof (acldisptab_v2[0]),
1967 		aclcallnames_v2,
1968 		&aclproccnt_v2_ptr, acldisptab_v2},
1969 	{sizeof (acldisptab_v3) / sizeof (acldisptab_v3[0]),
1970 		aclcallnames_v3,
1971 		&aclproccnt_v3_ptr, acldisptab_v3},
1972 };
1973 
1974 static void
1975 acl_dispatch(struct svc_req *req, SVCXPRT *xprt)
1976 {
1977 	common_dispatch(req, xprt, NFS_ACL_VERSMIN, NFS_ACL_VERSMAX,
1978 	    "ACL", acl_disptable);
1979 }
1980 
1981 int
1982 checkwin(int flavor, int window, struct svc_req *req)
1983 {
1984 	struct authdes_cred *adc;
1985 
1986 	switch (flavor) {
1987 	case AUTH_DES:
1988 		adc = (struct authdes_cred *)req->rq_clntcred;
1989 		if (adc->adc_fullname.window > window)
1990 			return (0);
1991 		break;
1992 
1993 	default:
1994 		break;
1995 	}
1996 	return (1);
1997 }
1998 
1999 
2000 /*
2001  * checkauth() will check the access permission against the export
2002  * information.  Then map root uid/gid to appropriate uid/gid.
2003  *
2004  * This routine is used by NFS V3 and V2 code.
2005  */
2006 static int
2007 checkauth(struct exportinfo *exi, struct svc_req *req, cred_t *cr, int anon_ok,
2008     bool_t publicfh_ok)
2009 {
2010 	int i, nfsflavor, rpcflavor, stat, access;
2011 	struct secinfo *secp;
2012 	caddr_t principal;
2013 	char buf[INET6_ADDRSTRLEN]; /* to hold both IPv4 and IPv6 addr */
2014 	int anon_res = 0;
2015 
2016 	/*
2017 	 * Check for privileged port number
2018 	 * N.B.:  this assumes that we know the format of a netbuf.
2019 	 */
2020 	if (nfs_portmon) {
2021 		struct sockaddr *ca;
2022 		ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
2023 
2024 		if (ca == NULL)
2025 			return (0);
2026 
2027 		if ((ca->sa_family == AF_INET &&
2028 		    ntohs(((struct sockaddr_in *)ca)->sin_port) >=
2029 		    IPPORT_RESERVED) ||
2030 		    (ca->sa_family == AF_INET6 &&
2031 		    ntohs(((struct sockaddr_in6 *)ca)->sin6_port) >=
2032 		    IPPORT_RESERVED)) {
2033 			cmn_err(CE_NOTE,
2034 			    "nfs_server: client %s%ssent NFS request from "
2035 			    "unprivileged port",
2036 			    client_name(req), client_addr(req, buf));
2037 			return (0);
2038 		}
2039 	}
2040 
2041 	/*
2042 	 *  return 1 on success or 0 on failure
2043 	 */
2044 	stat = sec_svc_getcred(req, cr, &principal, &nfsflavor);
2045 
2046 	/*
2047 	 * A failed AUTH_UNIX svc_get_cred() implies we couldn't set
2048 	 * the credentials; below we map that to anonymous.
2049 	 */
2050 	if (!stat && nfsflavor != AUTH_UNIX) {
2051 		cmn_err(CE_NOTE,
2052 		    "nfs_server: couldn't get unix cred for %s",
2053 		    client_name(req));
2054 		return (0);
2055 	}
2056 
2057 	/*
2058 	 * Short circuit checkauth() on operations that support the
2059 	 * public filehandle, and if the request for that operation
2060 	 * is using the public filehandle. Note that we must call
2061 	 * sec_svc_getcred() first so that xp_cookie is set to the
2062 	 * right value. Normally xp_cookie is just the RPC flavor
2063 	 * of the the request, but in the case of RPCSEC_GSS it
2064 	 * could be a pseudo flavor.
2065 	 */
2066 	if (publicfh_ok)
2067 		return (1);
2068 
2069 	rpcflavor = req->rq_cred.oa_flavor;
2070 	/*
2071 	 * Check if the auth flavor is valid for this export
2072 	 */
2073 	access = nfsauth_access(exi, req);
2074 	if (access & NFSAUTH_DROP)
2075 		return (-1);	/* drop the request */
2076 
2077 	if (access & NFSAUTH_DENIED) {
2078 		/*
2079 		 * If anon_ok == 1 and we got NFSAUTH_DENIED, it was
2080 		 * probably due to the flavor not matching during the
2081 		 * the mount attempt. So map the flavor to AUTH_NONE
2082 		 * so that the credentials get mapped to the anonymous
2083 		 * user.
2084 		 */
2085 		if (anon_ok == 1)
2086 			rpcflavor = AUTH_NONE;
2087 		else
2088 			return (0);	/* deny access */
2089 
2090 	} else if (access & NFSAUTH_MAPNONE) {
2091 		/*
2092 		 * Access was granted even though the flavor mismatched
2093 		 * because AUTH_NONE was one of the exported flavors.
2094 		 */
2095 		rpcflavor = AUTH_NONE;
2096 
2097 	} else if (access & NFSAUTH_WRONGSEC) {
2098 		/*
2099 		 * NFSAUTH_WRONGSEC is used for NFSv4. If we get here,
2100 		 * it means a client ignored the list of allowed flavors
2101 		 * returned via the MOUNT protocol. So we just disallow it!
2102 		 */
2103 		return (0);
2104 	}
2105 
2106 	switch (rpcflavor) {
2107 	case AUTH_NONE:
2108 		anon_res = crsetugid(cr, exi->exi_export.ex_anon,
2109 		    exi->exi_export.ex_anon);
2110 		(void) crsetgroups(cr, 0, NULL);
2111 		break;
2112 
2113 	case AUTH_UNIX:
2114 		if (!stat || crgetuid(cr) == 0 && !(access & NFSAUTH_ROOT)) {
2115 			anon_res = crsetugid(cr, exi->exi_export.ex_anon,
2116 			    exi->exi_export.ex_anon);
2117 			(void) crsetgroups(cr, 0, NULL);
2118 		} else if (!stat || crgetuid(cr) == 0 &&
2119 		    access & NFSAUTH_ROOT) {
2120 			/*
2121 			 * It is root, so apply rootid to get real UID
2122 			 * Find the secinfo structure.  We should be able
2123 			 * to find it by the time we reach here.
2124 			 * nfsauth_access() has done the checking.
2125 			 */
2126 			secp = NULL;
2127 			for (i = 0; i < exi->exi_export.ex_seccnt; i++) {
2128 				struct secinfo *sptr;
2129 				sptr = &exi->exi_export.ex_secinfo[i];
2130 				if (sptr->s_secinfo.sc_nfsnum == nfsflavor) {
2131 					secp = sptr;
2132 					break;
2133 				}
2134 			}
2135 			if (secp != NULL) {
2136 				(void) crsetugid(cr, secp->s_rootid,
2137 				    secp->s_rootid);
2138 				(void) crsetgroups(cr, 0, NULL);
2139 			}
2140 		}
2141 		break;
2142 
2143 	case AUTH_DES:
2144 	case RPCSEC_GSS:
2145 		/*
2146 		 *  Find the secinfo structure.  We should be able
2147 		 *  to find it by the time we reach here.
2148 		 *  nfsauth_access() has done the checking.
2149 		 */
2150 		secp = NULL;
2151 		for (i = 0; i < exi->exi_export.ex_seccnt; i++) {
2152 			if (exi->exi_export.ex_secinfo[i].s_secinfo.sc_nfsnum ==
2153 			    nfsflavor) {
2154 				secp = &exi->exi_export.ex_secinfo[i];
2155 				break;
2156 			}
2157 		}
2158 
2159 		if (!secp) {
2160 			cmn_err(CE_NOTE, "nfs_server: client %s%shad "
2161 			    "no secinfo data for flavor %d",
2162 			    client_name(req), client_addr(req, buf),
2163 			    nfsflavor);
2164 			return (0);
2165 		}
2166 
2167 		if (!checkwin(rpcflavor, secp->s_window, req)) {
2168 			cmn_err(CE_NOTE,
2169 			    "nfs_server: client %s%sused invalid "
2170 			    "auth window value",
2171 			    client_name(req), client_addr(req, buf));
2172 			return (0);
2173 		}
2174 
2175 		/*
2176 		 * Map root principals listed in the share's root= list to root,
2177 		 * and map any others principals that were mapped to root by RPC
2178 		 * to anon.
2179 		 */
2180 		if (principal && sec_svc_inrootlist(rpcflavor, principal,
2181 		    secp->s_rootcnt, secp->s_rootnames)) {
2182 			if (crgetuid(cr) == 0 && secp->s_rootid == 0)
2183 				return (1);
2184 
2185 
2186 			(void) crsetugid(cr, secp->s_rootid, secp->s_rootid);
2187 
2188 			/*
2189 			 * NOTE: If and when kernel-land privilege tracing is
2190 			 * added this may have to be replaced with code that
2191 			 * retrieves root's supplementary groups (e.g., using
2192 			 * kgss_get_group_info().  In the meantime principals
2193 			 * mapped to uid 0 get all privileges, so setting cr's
2194 			 * supplementary groups for them does nothing.
2195 			 */
2196 			(void) crsetgroups(cr, 0, NULL);
2197 
2198 			return (1);
2199 		}
2200 
2201 		/*
2202 		 * Not a root princ, or not in root list, map UID 0/nobody to
2203 		 * the anon ID for the share.  (RPC sets cr's UIDs and GIDs to
2204 		 * UID_NOBODY and GID_NOBODY, respectively.)
2205 		 */
2206 		if (crgetuid(cr) != 0 &&
2207 		    (crgetuid(cr) != UID_NOBODY || crgetgid(cr) != GID_NOBODY))
2208 			return (1);
2209 
2210 		anon_res = crsetugid(cr, exi->exi_export.ex_anon,
2211 		    exi->exi_export.ex_anon);
2212 		(void) crsetgroups(cr, 0, NULL);
2213 		break;
2214 	default:
2215 		return (0);
2216 	} /* switch on rpcflavor */
2217 
2218 	/*
2219 	 * Even if anon access is disallowed via ex_anon == -1, we allow
2220 	 * this access if anon_ok is set.  So set creds to the default
2221 	 * "nobody" id.
2222 	 */
2223 	if (anon_res != 0) {
2224 		if (anon_ok == 0) {
2225 			cmn_err(CE_NOTE,
2226 			    "nfs_server: client %s%ssent wrong "
2227 			    "authentication for %s",
2228 			    client_name(req), client_addr(req, buf),
2229 			    exi->exi_export.ex_path ?
2230 			    exi->exi_export.ex_path : "?");
2231 			return (0);
2232 		}
2233 
2234 		if (crsetugid(cr, UID_NOBODY, GID_NOBODY) != 0)
2235 			return (0);
2236 	}
2237 
2238 	return (1);
2239 }
2240 
2241 /*
2242  * returns 0 on failure, -1 on a drop, -2 on wrong security flavor,
2243  * and 1 on success
2244  */
2245 int
2246 checkauth4(struct compound_state *cs, struct svc_req *req)
2247 {
2248 	int i, rpcflavor, access;
2249 	struct secinfo *secp;
2250 	char buf[MAXHOST + 1];
2251 	int anon_res = 0, nfsflavor;
2252 	struct exportinfo *exi;
2253 	cred_t	*cr;
2254 	caddr_t	principal;
2255 
2256 	exi = cs->exi;
2257 	cr = cs->cr;
2258 	principal = cs->principal;
2259 	nfsflavor = cs->nfsflavor;
2260 
2261 	ASSERT(cr != NULL);
2262 
2263 	rpcflavor = req->rq_cred.oa_flavor;
2264 	cs->access &= ~CS_ACCESS_LIMITED;
2265 
2266 	/*
2267 	 * Check for privileged port number
2268 	 * N.B.:  this assumes that we know the format of a netbuf.
2269 	 */
2270 	if (nfs_portmon) {
2271 		struct sockaddr *ca;
2272 		ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
2273 
2274 		if (ca == NULL)
2275 			return (0);
2276 
2277 		if ((ca->sa_family == AF_INET &&
2278 		    ntohs(((struct sockaddr_in *)ca)->sin_port) >=
2279 		    IPPORT_RESERVED) ||
2280 		    (ca->sa_family == AF_INET6 &&
2281 		    ntohs(((struct sockaddr_in6 *)ca)->sin6_port) >=
2282 		    IPPORT_RESERVED)) {
2283 			cmn_err(CE_NOTE,
2284 			    "nfs_server: client %s%ssent NFSv4 request from "
2285 			    "unprivileged port",
2286 			    client_name(req), client_addr(req, buf));
2287 			return (0);
2288 		}
2289 	}
2290 
2291 	/*
2292 	 * Check the access right per auth flavor on the vnode of
2293 	 * this export for the given request.
2294 	 */
2295 	access = nfsauth4_access(cs->exi, cs->vp, req);
2296 
2297 	if (access & NFSAUTH_WRONGSEC)
2298 		return (-2);	/* no access for this security flavor */
2299 
2300 	if (access & NFSAUTH_DROP)
2301 		return (-1);	/* drop the request */
2302 
2303 	if (access & NFSAUTH_DENIED) {
2304 
2305 		if (exi->exi_export.ex_seccnt > 0)
2306 			return (0);	/* deny access */
2307 
2308 	} else if (access & NFSAUTH_LIMITED) {
2309 
2310 		cs->access |= CS_ACCESS_LIMITED;
2311 
2312 	} else if (access & NFSAUTH_MAPNONE) {
2313 		/*
2314 		 * Access was granted even though the flavor mismatched
2315 		 * because AUTH_NONE was one of the exported flavors.
2316 		 */
2317 		rpcflavor = AUTH_NONE;
2318 	}
2319 
2320 	/*
2321 	 * XXX probably need to redo some of it for nfsv4?
2322 	 * return 1 on success or 0 on failure
2323 	 */
2324 
2325 	switch (rpcflavor) {
2326 	case AUTH_NONE:
2327 		anon_res = crsetugid(cr, exi->exi_export.ex_anon,
2328 		    exi->exi_export.ex_anon);
2329 		(void) crsetgroups(cr, 0, NULL);
2330 		break;
2331 
2332 	case AUTH_UNIX:
2333 		if (crgetuid(cr) == 0 && !(access & NFSAUTH_ROOT)) {
2334 			anon_res = crsetugid(cr, exi->exi_export.ex_anon,
2335 			    exi->exi_export.ex_anon);
2336 			(void) crsetgroups(cr, 0, NULL);
2337 		} else if (crgetuid(cr) == 0 && access & NFSAUTH_ROOT) {
2338 			/*
2339 			 * It is root, so apply rootid to get real UID
2340 			 * Find the secinfo structure.  We should be able
2341 			 * to find it by the time we reach here.
2342 			 * nfsauth_access() has done the checking.
2343 			 */
2344 			secp = NULL;
2345 			for (i = 0; i < exi->exi_export.ex_seccnt; i++) {
2346 				struct secinfo *sptr;
2347 				sptr = &exi->exi_export.ex_secinfo[i];
2348 				if (sptr->s_secinfo.sc_nfsnum == nfsflavor) {
2349 					secp = &exi->exi_export.ex_secinfo[i];
2350 					break;
2351 				}
2352 			}
2353 			if (secp != NULL) {
2354 				(void) crsetugid(cr, secp->s_rootid,
2355 				    secp->s_rootid);
2356 				(void) crsetgroups(cr, 0, NULL);
2357 			}
2358 		}
2359 		break;
2360 
2361 	default:
2362 		/*
2363 		 *  Find the secinfo structure.  We should be able
2364 		 *  to find it by the time we reach here.
2365 		 *  nfsauth_access() has done the checking.
2366 		 */
2367 		secp = NULL;
2368 		for (i = 0; i < exi->exi_export.ex_seccnt; i++) {
2369 			if (exi->exi_export.ex_secinfo[i].s_secinfo.sc_nfsnum ==
2370 			    nfsflavor) {
2371 				secp = &exi->exi_export.ex_secinfo[i];
2372 				break;
2373 			}
2374 		}
2375 
2376 		if (!secp) {
2377 			cmn_err(CE_NOTE, "nfs_server: client %s%shad "
2378 			    "no secinfo data for flavor %d",
2379 			    client_name(req), client_addr(req, buf),
2380 			    nfsflavor);
2381 			return (0);
2382 		}
2383 
2384 		if (!checkwin(rpcflavor, secp->s_window, req)) {
2385 			cmn_err(CE_NOTE,
2386 			    "nfs_server: client %s%sused invalid "
2387 			    "auth window value",
2388 			    client_name(req), client_addr(req, buf));
2389 			return (0);
2390 		}
2391 
2392 		/*
2393 		 * Map root principals listed in the share's root= list to root,
2394 		 * and map any others principals that were mapped to root by RPC
2395 		 * to anon. If not going to anon, set to rootid (root_mapping).
2396 		 */
2397 		if (principal && sec_svc_inrootlist(rpcflavor, principal,
2398 		    secp->s_rootcnt, secp->s_rootnames)) {
2399 			if (crgetuid(cr) == 0 && secp->s_rootid == 0)
2400 				return (1);
2401 
2402 			(void) crsetugid(cr, secp->s_rootid, secp->s_rootid);
2403 
2404 			/*
2405 			 * NOTE: If and when kernel-land privilege tracing is
2406 			 * added this may have to be replaced with code that
2407 			 * retrieves root's supplementary groups (e.g., using
2408 			 * kgss_get_group_info().  In the meantime principals
2409 			 * mapped to uid 0 get all privileges, so setting cr's
2410 			 * supplementary groups for them does nothing.
2411 			 */
2412 			(void) crsetgroups(cr, 0, NULL);
2413 
2414 			return (1);
2415 		}
2416 
2417 		/*
2418 		 * Not a root princ, or not in root list, map UID 0/nobody to
2419 		 * the anon ID for the share.  (RPC sets cr's UIDs and GIDs to
2420 		 * UID_NOBODY and GID_NOBODY, respectively.)
2421 		 */
2422 		if (crgetuid(cr) != 0 &&
2423 		    (crgetuid(cr) != UID_NOBODY || crgetgid(cr) != GID_NOBODY))
2424 			return (1);
2425 
2426 		anon_res = crsetugid(cr, exi->exi_export.ex_anon,
2427 		    exi->exi_export.ex_anon);
2428 		(void) crsetgroups(cr, 0, NULL);
2429 		break;
2430 	} /* switch on rpcflavor */
2431 
2432 	/*
2433 	 * Even if anon access is disallowed via ex_anon == -1, we allow
2434 	 * this access if anon_ok is set.  So set creds to the default
2435 	 * "nobody" id.
2436 	 */
2437 
2438 	if (anon_res != 0) {
2439 		cmn_err(CE_NOTE,
2440 		    "nfs_server: client %s%ssent wrong "
2441 		    "authentication for %s",
2442 		    client_name(req), client_addr(req, buf),
2443 		    exi->exi_export.ex_path ?
2444 		    exi->exi_export.ex_path : "?");
2445 		return (0);
2446 	}
2447 
2448 	return (1);
2449 }
2450 
2451 
2452 static char *
2453 client_name(struct svc_req *req)
2454 {
2455 	char *hostname = NULL;
2456 
2457 	/*
2458 	 * If it's a Unix cred then use the
2459 	 * hostname from the credential.
2460 	 */
2461 	if (req->rq_cred.oa_flavor == AUTH_UNIX) {
2462 		hostname = ((struct authunix_parms *)
2463 		    req->rq_clntcred)->aup_machname;
2464 	}
2465 	if (hostname == NULL)
2466 		hostname = "";
2467 
2468 	return (hostname);
2469 }
2470 
2471 static char *
2472 client_addr(struct svc_req *req, char *buf)
2473 {
2474 	struct sockaddr *ca;
2475 	uchar_t *b;
2476 	char *frontspace = "";
2477 
2478 	/*
2479 	 * We assume we are called in tandem with client_name and the
2480 	 * format string looks like "...client %s%sblah blah..."
2481 	 *
2482 	 * If it's a Unix cred then client_name returned
2483 	 * a host name, so we need insert a space between host name
2484 	 * and IP address.
2485 	 */
2486 	if (req->rq_cred.oa_flavor == AUTH_UNIX)
2487 		frontspace = " ";
2488 
2489 	/*
2490 	 * Convert the caller's IP address to a dotted string
2491 	 */
2492 	ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
2493 
2494 	if (ca->sa_family == AF_INET) {
2495 		b = (uchar_t *)&((struct sockaddr_in *)ca)->sin_addr;
2496 		(void) sprintf(buf, "%s(%d.%d.%d.%d) ", frontspace,
2497 		    b[0] & 0xFF, b[1] & 0xFF, b[2] & 0xFF, b[3] & 0xFF);
2498 	} else if (ca->sa_family == AF_INET6) {
2499 		struct sockaddr_in6 *sin6;
2500 		sin6 = (struct sockaddr_in6 *)ca;
2501 		(void) kinet_ntop6((uchar_t *)&sin6->sin6_addr,
2502 		    buf, INET6_ADDRSTRLEN);
2503 
2504 	} else {
2505 
2506 		/*
2507 		 * No IP address to print. If there was a host name
2508 		 * printed, then we print a space.
2509 		 */
2510 		(void) sprintf(buf, frontspace);
2511 	}
2512 
2513 	return (buf);
2514 }
2515 
2516 /*
2517  * NFS Server initialization routine.  This routine should only be called
2518  * once.  It performs the following tasks:
2519  *	- Call sub-initialization routines (localize access to variables)
2520  *	- Initialize all locks
2521  *	- initialize the version 3 write verifier
2522  */
2523 int
2524 nfs_srvinit(void)
2525 {
2526 	int error;
2527 
2528 	error = nfs_exportinit();
2529 	if (error != 0)
2530 		return (error);
2531 	error = rfs4_srvrinit();
2532 	if (error != 0) {
2533 		nfs_exportfini();
2534 		return (error);
2535 	}
2536 	rfs_srvrinit();
2537 	rfs3_srvrinit();
2538 	nfsauth_init();
2539 
2540 	/* Init the stuff to control start/stop */
2541 	nfs_server_upordown = NFS_SERVER_STOPPED;
2542 	mutex_init(&nfs_server_upordown_lock, NULL, MUTEX_DEFAULT, NULL);
2543 	cv_init(&nfs_server_upordown_cv, NULL, CV_DEFAULT, NULL);
2544 	mutex_init(&rdma_wait_mutex, NULL, MUTEX_DEFAULT, NULL);
2545 	cv_init(&rdma_wait_cv, NULL, CV_DEFAULT, NULL);
2546 
2547 	return (0);
2548 }
2549 
2550 /*
2551  * NFS Server finalization routine. This routine is called to cleanup the
2552  * initialization work previously performed if the NFS server module could
2553  * not be loaded correctly.
2554  */
2555 void
2556 nfs_srvfini(void)
2557 {
2558 	nfsauth_fini();
2559 	rfs3_srvrfini();
2560 	rfs_srvrfini();
2561 	nfs_exportfini();
2562 
2563 	mutex_destroy(&nfs_server_upordown_lock);
2564 	cv_destroy(&nfs_server_upordown_cv);
2565 	mutex_destroy(&rdma_wait_mutex);
2566 	cv_destroy(&rdma_wait_cv);
2567 }
2568 
2569 /*
2570  * Set up an iovec array of up to cnt pointers.
2571  */
2572 
2573 void
2574 mblk_to_iov(mblk_t *m, int cnt, struct iovec *iovp)
2575 {
2576 	while (m != NULL && cnt-- > 0) {
2577 		iovp->iov_base = (caddr_t)m->b_rptr;
2578 		iovp->iov_len = (m->b_wptr - m->b_rptr);
2579 		iovp++;
2580 		m = m->b_cont;
2581 	}
2582 }
2583 
2584 /*
2585  * Common code between NFS Version 2 and NFS Version 3 for the public
2586  * filehandle multicomponent lookups.
2587  */
2588 
2589 /*
2590  * Public filehandle evaluation of a multi-component lookup, following
2591  * symbolic links, if necessary. This may result in a vnode in another
2592  * filesystem, which is OK as long as the other filesystem is exported.
2593  *
2594  * Note that the exi will be set either to NULL or a new reference to the
2595  * exportinfo struct that corresponds to the vnode of the multi-component path.
2596  * It is the callers responsibility to release this reference.
2597  */
2598 int
2599 rfs_publicfh_mclookup(char *p, vnode_t *dvp, cred_t *cr, vnode_t **vpp,
2600     struct exportinfo **exi, struct sec_ol *sec)
2601 {
2602 	int pathflag;
2603 	vnode_t *mc_dvp = NULL;
2604 	vnode_t *realvp;
2605 	int error;
2606 
2607 	*exi = NULL;
2608 
2609 	/*
2610 	 * check if the given path is a url or native path. Since p is
2611 	 * modified by MCLpath(), it may be empty after returning from
2612 	 * there, and should be checked.
2613 	 */
2614 	if ((pathflag = MCLpath(&p)) == -1)
2615 		return (EIO);
2616 
2617 	/*
2618 	 * If pathflag is SECURITY_QUERY, turn the SEC_QUERY bit
2619 	 * on in sec->sec_flags. This bit will later serve as an
2620 	 * indication in makefh_ol() or makefh3_ol() to overload the
2621 	 * filehandle to contain the sec modes used by the server for
2622 	 * the path.
2623 	 */
2624 	if (pathflag == SECURITY_QUERY) {
2625 		if ((sec->sec_index = (uint_t)(*p)) > 0) {
2626 			sec->sec_flags |= SEC_QUERY;
2627 			p++;
2628 			if ((pathflag = MCLpath(&p)) == -1)
2629 				return (EIO);
2630 		} else {
2631 			cmn_err(CE_NOTE,
2632 			    "nfs_server: invalid security index %d, "
2633 			    "violating WebNFS SNEGO protocol.", sec->sec_index);
2634 			return (EIO);
2635 		}
2636 	}
2637 
2638 	if (p[0] == '\0') {
2639 		error = ENOENT;
2640 		goto publicfh_done;
2641 	}
2642 
2643 	error = rfs_pathname(p, &mc_dvp, vpp, dvp, cr, pathflag);
2644 
2645 	/*
2646 	 * If name resolves to "/" we get EINVAL since we asked for
2647 	 * the vnode of the directory that the file is in. Try again
2648 	 * with NULL directory vnode.
2649 	 */
2650 	if (error == EINVAL) {
2651 		error = rfs_pathname(p, NULL, vpp, dvp, cr, pathflag);
2652 		if (!error) {
2653 			ASSERT(*vpp != NULL);
2654 			if ((*vpp)->v_type == VDIR) {
2655 				VN_HOLD(*vpp);
2656 				mc_dvp = *vpp;
2657 			} else {
2658 				/*
2659 				 * This should not happen, the filesystem is
2660 				 * in an inconsistent state. Fail the lookup
2661 				 * at this point.
2662 				 */
2663 				VN_RELE(*vpp);
2664 				error = EINVAL;
2665 			}
2666 		}
2667 	}
2668 
2669 	if (error)
2670 		goto publicfh_done;
2671 
2672 	if (*vpp == NULL) {
2673 		error = ENOENT;
2674 		goto publicfh_done;
2675 	}
2676 
2677 	ASSERT(mc_dvp != NULL);
2678 	ASSERT(*vpp != NULL);
2679 
2680 	if ((*vpp)->v_type == VDIR) {
2681 		do {
2682 			/*
2683 			 * *vpp may be an AutoFS node, so we perform
2684 			 * a VOP_ACCESS() to trigger the mount of the intended
2685 			 * filesystem, so we can perform the lookup in the
2686 			 * intended filesystem.
2687 			 */
2688 			(void) VOP_ACCESS(*vpp, 0, 0, cr, NULL);
2689 
2690 			/*
2691 			 * If vnode is covered, get the
2692 			 * the topmost vnode.
2693 			 */
2694 			if (vn_mountedvfs(*vpp) != NULL) {
2695 				error = traverse(vpp);
2696 				if (error) {
2697 					VN_RELE(*vpp);
2698 					goto publicfh_done;
2699 				}
2700 			}
2701 
2702 			if (VOP_REALVP(*vpp, &realvp, NULL) == 0 &&
2703 			    realvp != *vpp) {
2704 				/*
2705 				 * If realvp is different from *vpp
2706 				 * then release our reference on *vpp, so that
2707 				 * the export access check be performed on the
2708 				 * real filesystem instead.
2709 				 */
2710 				VN_HOLD(realvp);
2711 				VN_RELE(*vpp);
2712 				*vpp = realvp;
2713 			} else {
2714 				break;
2715 			}
2716 		/* LINTED */
2717 		} while (TRUE);
2718 
2719 		/*
2720 		 * Let nfs_vptexi() figure what the real parent is.
2721 		 */
2722 		VN_RELE(mc_dvp);
2723 		mc_dvp = NULL;
2724 
2725 	} else {
2726 		/*
2727 		 * If vnode is covered, get the
2728 		 * the topmost vnode.
2729 		 */
2730 		if (vn_mountedvfs(mc_dvp) != NULL) {
2731 			error = traverse(&mc_dvp);
2732 			if (error) {
2733 				VN_RELE(*vpp);
2734 				goto publicfh_done;
2735 			}
2736 		}
2737 
2738 		if (VOP_REALVP(mc_dvp, &realvp, NULL) == 0 &&
2739 		    realvp != mc_dvp) {
2740 			/*
2741 			 * *vpp is a file, obtain realvp of the parent
2742 			 * directory vnode.
2743 			 */
2744 			VN_HOLD(realvp);
2745 			VN_RELE(mc_dvp);
2746 			mc_dvp = realvp;
2747 		}
2748 	}
2749 
2750 	/*
2751 	 * The pathname may take us from the public filesystem to another.
2752 	 * If that's the case then just set the exportinfo to the new export
2753 	 * and build filehandle for it. Thanks to per-access checking there's
2754 	 * no security issues with doing this. If the client is not allowed
2755 	 * access to this new export then it will get an access error when it
2756 	 * tries to use the filehandle
2757 	 */
2758 	if (error = nfs_check_vpexi(mc_dvp, *vpp, kcred, exi)) {
2759 		VN_RELE(*vpp);
2760 		goto publicfh_done;
2761 	}
2762 
2763 	/*
2764 	 * Not allowed access to pseudo exports.
2765 	 */
2766 	if (PSEUDO(*exi)) {
2767 		error = ENOENT;
2768 		VN_RELE(*vpp);
2769 		goto publicfh_done;
2770 	}
2771 
2772 	/*
2773 	 * Do a lookup for the index file. We know the index option doesn't
2774 	 * allow paths through handling in the share command, so mc_dvp will
2775 	 * be the parent for the index file vnode, if its present. Use
2776 	 * temporary pointers to preserve and reuse the vnode pointers of the
2777 	 * original directory in case there's no index file. Note that the
2778 	 * index file is a native path, and should not be interpreted by
2779 	 * the URL parser in rfs_pathname()
2780 	 */
2781 	if (((*exi)->exi_export.ex_flags & EX_INDEX) &&
2782 	    ((*vpp)->v_type == VDIR) && (pathflag == URLPATH)) {
2783 		vnode_t *tvp, *tmc_dvp;	/* temporary vnode pointers */
2784 
2785 		tmc_dvp = mc_dvp;
2786 		mc_dvp = tvp = *vpp;
2787 
2788 		error = rfs_pathname((*exi)->exi_export.ex_index, NULL, vpp,
2789 		    mc_dvp, cr, NATIVEPATH);
2790 
2791 		if (error == ENOENT) {
2792 			*vpp = tvp;
2793 			mc_dvp = tmc_dvp;
2794 			error = 0;
2795 		} else {	/* ok or error other than ENOENT */
2796 			if (tmc_dvp)
2797 				VN_RELE(tmc_dvp);
2798 			if (error)
2799 				goto publicfh_done;
2800 
2801 			/*
2802 			 * Found a valid vp for index "filename". Sanity check
2803 			 * for odd case where a directory is provided as index
2804 			 * option argument and leads us to another filesystem
2805 			 */
2806 
2807 			/* Release the reference on the old exi value */
2808 			ASSERT(*exi != NULL);
2809 			exi_rele(*exi);
2810 
2811 			if (error = nfs_check_vpexi(mc_dvp, *vpp, kcred, exi)) {
2812 				VN_RELE(*vpp);
2813 				goto publicfh_done;
2814 			}
2815 		}
2816 	}
2817 
2818 publicfh_done:
2819 	if (mc_dvp)
2820 		VN_RELE(mc_dvp);
2821 
2822 	return (error);
2823 }
2824 
2825 /*
2826  * Evaluate a multi-component path
2827  */
2828 int
2829 rfs_pathname(
2830 	char *path,			/* pathname to evaluate */
2831 	vnode_t **dirvpp,		/* ret for ptr to parent dir vnode */
2832 	vnode_t **compvpp,		/* ret for ptr to component vnode */
2833 	vnode_t *startdvp,		/* starting vnode */
2834 	cred_t *cr,			/* user's credential */
2835 	int pathflag)			/* flag to identify path, e.g. URL */
2836 {
2837 	char namebuf[TYPICALMAXPATHLEN];
2838 	struct pathname pn;
2839 	int error;
2840 
2841 	/*
2842 	 * If pathname starts with '/', then set startdvp to root.
2843 	 */
2844 	if (*path == '/') {
2845 		while (*path == '/')
2846 			path++;
2847 
2848 		startdvp = rootdir;
2849 	}
2850 
2851 	error = pn_get_buf(path, UIO_SYSSPACE, &pn, namebuf, sizeof (namebuf));
2852 	if (error == 0) {
2853 		/*
2854 		 * Call the URL parser for URL paths to modify the original
2855 		 * string to handle any '%' encoded characters that exist.
2856 		 * Done here to avoid an extra bcopy in the lookup.
2857 		 * We need to be careful about pathlen's. We know that
2858 		 * rfs_pathname() is called with a non-empty path. However,
2859 		 * it could be emptied due to the path simply being all /'s,
2860 		 * which is valid to proceed with the lookup, or due to the
2861 		 * URL parser finding an encoded null character at the
2862 		 * beginning of path which should not proceed with the lookup.
2863 		 */
2864 		if (pn.pn_pathlen != 0 && pathflag == URLPATH) {
2865 			URLparse(pn.pn_path);
2866 			if ((pn.pn_pathlen = strlen(pn.pn_path)) == 0)
2867 				return (ENOENT);
2868 		}
2869 		VN_HOLD(startdvp);
2870 		error = lookuppnvp(&pn, NULL, NO_FOLLOW, dirvpp, compvpp,
2871 		    rootdir, startdvp, cr);
2872 	}
2873 	if (error == ENAMETOOLONG) {
2874 		/*
2875 		 * This thread used a pathname > TYPICALMAXPATHLEN bytes long.
2876 		 */
2877 		if (error = pn_get(path, UIO_SYSSPACE, &pn))
2878 			return (error);
2879 		if (pn.pn_pathlen != 0 && pathflag == URLPATH) {
2880 			URLparse(pn.pn_path);
2881 			if ((pn.pn_pathlen = strlen(pn.pn_path)) == 0) {
2882 				pn_free(&pn);
2883 				return (ENOENT);
2884 			}
2885 		}
2886 		VN_HOLD(startdvp);
2887 		error = lookuppnvp(&pn, NULL, NO_FOLLOW, dirvpp, compvpp,
2888 		    rootdir, startdvp, cr);
2889 		pn_free(&pn);
2890 	}
2891 
2892 	return (error);
2893 }
2894 
2895 /*
2896  * Adapt the multicomponent lookup path depending on the pathtype
2897  */
2898 static int
2899 MCLpath(char **path)
2900 {
2901 	unsigned char c = (unsigned char)**path;
2902 
2903 	/*
2904 	 * If the MCL path is between 0x20 and 0x7E (graphic printable
2905 	 * character of the US-ASCII coded character set), its a URL path,
2906 	 * per RFC 1738.
2907 	 */
2908 	if (c >= 0x20 && c <= 0x7E)
2909 		return (URLPATH);
2910 
2911 	/*
2912 	 * If the first octet of the MCL path is not an ASCII character
2913 	 * then it must be interpreted as a tag value that describes the
2914 	 * format of the remaining octets of the MCL path.
2915 	 *
2916 	 * If the first octet of the MCL path is 0x81 it is a query
2917 	 * for the security info.
2918 	 */
2919 	switch (c) {
2920 	case 0x80:	/* native path, i.e. MCL via mount protocol */
2921 		(*path)++;
2922 		return (NATIVEPATH);
2923 	case 0x81:	/* security query */
2924 		(*path)++;
2925 		return (SECURITY_QUERY);
2926 	default:
2927 		return (-1);
2928 	}
2929 }
2930 
2931 #define	fromhex(c)  ((c >= '0' && c <= '9') ? (c - '0') : \
2932 			((c >= 'A' && c <= 'F') ? (c - 'A' + 10) :\
2933 			((c >= 'a' && c <= 'f') ? (c - 'a' + 10) : 0)))
2934 
2935 /*
2936  * The implementation of URLparse guarantees that the final string will
2937  * fit in the original one. Replaces '%' occurrences followed by 2 characters
2938  * with its corresponding hexadecimal character.
2939  */
2940 static void
2941 URLparse(char *str)
2942 {
2943 	char *p, *q;
2944 
2945 	p = q = str;
2946 	while (*p) {
2947 		*q = *p;
2948 		if (*p++ == '%') {
2949 			if (*p) {
2950 				*q = fromhex(*p) * 16;
2951 				p++;
2952 				if (*p) {
2953 					*q += fromhex(*p);
2954 					p++;
2955 				}
2956 			}
2957 		}
2958 		q++;
2959 	}
2960 	*q = '\0';
2961 }
2962 
2963 
2964 /*
2965  * Get the export information for the lookup vnode, and verify its
2966  * useable.
2967  */
2968 int
2969 nfs_check_vpexi(vnode_t *mc_dvp, vnode_t *vp, cred_t *cr,
2970     struct exportinfo **exi)
2971 {
2972 	int walk;
2973 	int error = 0;
2974 
2975 	*exi = nfs_vptoexi(mc_dvp, vp, cr, &walk, NULL, FALSE);
2976 	if (*exi == NULL)
2977 		error = EACCES;
2978 	else {
2979 		/*
2980 		 * If nosub is set for this export then
2981 		 * a lookup relative to the public fh
2982 		 * must not terminate below the
2983 		 * exported directory.
2984 		 */
2985 		if ((*exi)->exi_export.ex_flags & EX_NOSUB && walk > 0)
2986 			error = EACCES;
2987 	}
2988 
2989 	return (error);
2990 }
2991 
2992 /*
2993  * Do the main work of handling HA-NFSv4 Resource Group failover on
2994  * Sun Cluster.
2995  * We need to detect whether any RG admin paths have been added or removed,
2996  * and adjust resources accordingly.
2997  * Currently we're using a very inefficient algorithm, ~ 2 * O(n**2). In
2998  * order to scale, the list and array of paths need to be held in more
2999  * suitable data structures.
3000  */
3001 static void
3002 hanfsv4_failover(void)
3003 {
3004 	int i, start_grace, numadded_paths = 0;
3005 	char **added_paths = NULL;
3006 	rfs4_dss_path_t *dss_path;
3007 
3008 	/*
3009 	 * Note: currently, rfs4_dss_pathlist cannot be NULL, since
3010 	 * it will always include an entry for NFS4_DSS_VAR_DIR. If we
3011 	 * make the latter dynamically specified too, the following will
3012 	 * need to be adjusted.
3013 	 */
3014 
3015 	/*
3016 	 * First, look for removed paths: RGs that have been failed-over
3017 	 * away from this node.
3018 	 * Walk the "currently-serving" rfs4_dss_pathlist and, for each
3019 	 * path, check if it is on the "passed-in" rfs4_dss_newpaths array
3020 	 * from nfsd. If not, that RG path has been removed.
3021 	 *
3022 	 * Note that nfsd has sorted rfs4_dss_newpaths for us, and removed
3023 	 * any duplicates.
3024 	 */
3025 	dss_path = rfs4_dss_pathlist;
3026 	do {
3027 		int found = 0;
3028 		char *path = dss_path->path;
3029 
3030 		/* used only for non-HA so may not be removed */
3031 		if (strcmp(path, NFS4_DSS_VAR_DIR) == 0) {
3032 			dss_path = dss_path->next;
3033 			continue;
3034 		}
3035 
3036 		for (i = 0; i < rfs4_dss_numnewpaths; i++) {
3037 			int cmpret;
3038 			char *newpath = rfs4_dss_newpaths[i];
3039 
3040 			/*
3041 			 * Since nfsd has sorted rfs4_dss_newpaths for us,
3042 			 * once the return from strcmp is negative we know
3043 			 * we've passed the point where "path" should be,
3044 			 * and can stop searching: "path" has been removed.
3045 			 */
3046 			cmpret = strcmp(path, newpath);
3047 			if (cmpret < 0)
3048 				break;
3049 			if (cmpret == 0) {
3050 				found = 1;
3051 				break;
3052 			}
3053 		}
3054 
3055 		if (found == 0) {
3056 			unsigned index = dss_path->index;
3057 			rfs4_servinst_t *sip = dss_path->sip;
3058 			rfs4_dss_path_t *path_next = dss_path->next;
3059 
3060 			/*
3061 			 * This path has been removed.
3062 			 * We must clear out the servinst reference to
3063 			 * it, since it's now owned by another
3064 			 * node: we should not attempt to touch it.
3065 			 */
3066 			ASSERT(dss_path == sip->dss_paths[index]);
3067 			sip->dss_paths[index] = NULL;
3068 
3069 			/* remove from "currently-serving" list, and destroy */
3070 			remque(dss_path);
3071 			/* allow for NUL */
3072 			kmem_free(dss_path->path, strlen(dss_path->path) + 1);
3073 			kmem_free(dss_path, sizeof (rfs4_dss_path_t));
3074 
3075 			dss_path = path_next;
3076 		} else {
3077 			/* path was found; not removed */
3078 			dss_path = dss_path->next;
3079 		}
3080 	} while (dss_path != rfs4_dss_pathlist);
3081 
3082 	/*
3083 	 * Now, look for added paths: RGs that have been failed-over
3084 	 * to this node.
3085 	 * Walk the "passed-in" rfs4_dss_newpaths array from nfsd and,
3086 	 * for each path, check if it is on the "currently-serving"
3087 	 * rfs4_dss_pathlist. If not, that RG path has been added.
3088 	 *
3089 	 * Note: we don't do duplicate detection here; nfsd does that for us.
3090 	 *
3091 	 * Note: numadded_paths <= rfs4_dss_numnewpaths, which gives us
3092 	 * an upper bound for the size needed for added_paths[numadded_paths].
3093 	 */
3094 
3095 	/* probably more space than we need, but guaranteed to be enough */
3096 	if (rfs4_dss_numnewpaths > 0) {
3097 		size_t sz = rfs4_dss_numnewpaths * sizeof (char *);
3098 		added_paths = kmem_zalloc(sz, KM_SLEEP);
3099 	}
3100 
3101 	/* walk the "passed-in" rfs4_dss_newpaths array from nfsd */
3102 	for (i = 0; i < rfs4_dss_numnewpaths; i++) {
3103 		int found = 0;
3104 		char *newpath = rfs4_dss_newpaths[i];
3105 
3106 		dss_path = rfs4_dss_pathlist;
3107 		do {
3108 			char *path = dss_path->path;
3109 
3110 			/* used only for non-HA */
3111 			if (strcmp(path, NFS4_DSS_VAR_DIR) == 0) {
3112 				dss_path = dss_path->next;
3113 				continue;
3114 			}
3115 
3116 			if (strncmp(path, newpath, strlen(path)) == 0) {
3117 				found = 1;
3118 				break;
3119 			}
3120 
3121 			dss_path = dss_path->next;
3122 		} while (dss_path != rfs4_dss_pathlist);
3123 
3124 		if (found == 0) {
3125 			added_paths[numadded_paths] = newpath;
3126 			numadded_paths++;
3127 		}
3128 	}
3129 
3130 	/* did we find any added paths? */
3131 	if (numadded_paths > 0) {
3132 		/* create a new server instance, and start its grace period */
3133 		start_grace = 1;
3134 		rfs4_servinst_create(start_grace, numadded_paths, added_paths);
3135 
3136 		/* read in the stable storage state from these paths */
3137 		rfs4_dss_readstate(numadded_paths, added_paths);
3138 
3139 		/*
3140 		 * Multiple failovers during a grace period will cause
3141 		 * clients of the same resource group to be partitioned
3142 		 * into different server instances, with different
3143 		 * grace periods.  Since clients of the same resource
3144 		 * group must be subject to the same grace period,
3145 		 * we need to reset all currently active grace periods.
3146 		 */
3147 		rfs4_grace_reset_all();
3148 	}
3149 
3150 	if (rfs4_dss_numnewpaths > 0)
3151 		kmem_free(added_paths, rfs4_dss_numnewpaths * sizeof (char *));
3152 }
3153 
3154 /*
3155  * Used by NFSv3 and NFSv4 server to query label of
3156  * a pathname component during lookup/access ops.
3157  */
3158 ts_label_t *
3159 nfs_getflabel(vnode_t *vp, struct exportinfo *exi)
3160 {
3161 	zone_t *zone;
3162 	ts_label_t *zone_label;
3163 	char *path;
3164 
3165 	mutex_enter(&vp->v_lock);
3166 	if (vp->v_path != NULL) {
3167 		zone = zone_find_by_any_path(vp->v_path, B_FALSE);
3168 		mutex_exit(&vp->v_lock);
3169 	} else {
3170 		/*
3171 		 * v_path not cached. Fall back on pathname of exported
3172 		 * file system as we rely on pathname from which we can
3173 		 * derive a label. The exported file system portion of
3174 		 * path is sufficient to obtain a label.
3175 		 */
3176 		path = exi->exi_export.ex_path;
3177 		if (path == NULL) {
3178 			mutex_exit(&vp->v_lock);
3179 			return (NULL);
3180 		}
3181 		zone = zone_find_by_any_path(path, B_FALSE);
3182 		mutex_exit(&vp->v_lock);
3183 	}
3184 	/*
3185 	 * Caller has verified that the file is either
3186 	 * exported or visible. So if the path falls in
3187 	 * global zone, admin_low is returned; otherwise
3188 	 * the zone's label is returned.
3189 	 */
3190 	zone_label = zone->zone_slabel;
3191 	label_hold(zone_label);
3192 	zone_rele(zone);
3193 	return (zone_label);
3194 }
3195 
3196 /*
3197  * TX NFS routine used by NFSv3 and NFSv4 to do label check
3198  * on client label and server's file object lable.
3199  */
3200 boolean_t
3201 do_rfs_label_check(bslabel_t *clabel, vnode_t *vp, int flag,
3202     struct exportinfo *exi)
3203 {
3204 	bslabel_t *slabel;
3205 	ts_label_t *tslabel;
3206 	boolean_t result;
3207 
3208 	if ((tslabel = nfs_getflabel(vp, exi)) == NULL) {
3209 		return (B_FALSE);
3210 	}
3211 	slabel = label2bslabel(tslabel);
3212 	DTRACE_PROBE4(tx__rfs__log__info__labelcheck, char *,
3213 	    "comparing server's file label(1) with client label(2) (vp(3))",
3214 	    bslabel_t *, slabel, bslabel_t *, clabel, vnode_t *, vp);
3215 
3216 	if (flag == EQUALITY_CHECK)
3217 		result = blequal(clabel, slabel);
3218 	else
3219 		result = bldominates(clabel, slabel);
3220 	label_rele(tslabel);
3221 	return (result);
3222 }
3223 
3224 /*
3225  * Callback function to return the loaned buffers.
3226  * Calls VOP_RETZCBUF() only after all uio_iov[]
3227  * buffers are returned. nu_ref maintains the count.
3228  */
3229 void
3230 rfs_free_xuio(void *free_arg)
3231 {
3232 	uint_t ref;
3233 	nfs_xuio_t *nfsuiop = (nfs_xuio_t *)free_arg;
3234 
3235 	ref = atomic_dec_uint_nv(&nfsuiop->nu_ref);
3236 
3237 	/*
3238 	 * Call VOP_RETZCBUF() only when all the iov buffers
3239 	 * are sent OTW.
3240 	 */
3241 	if (ref != 0)
3242 		return;
3243 
3244 	if (((uio_t *)nfsuiop)->uio_extflg & UIO_XUIO) {
3245 		(void) VOP_RETZCBUF(nfsuiop->nu_vp, (xuio_t *)free_arg, NULL,
3246 		    NULL);
3247 		VN_RELE(nfsuiop->nu_vp);
3248 	}
3249 
3250 	kmem_cache_free(nfs_xuio_cache, free_arg);
3251 }
3252 
3253 xuio_t *
3254 rfs_setup_xuio(vnode_t *vp)
3255 {
3256 	nfs_xuio_t *nfsuiop;
3257 
3258 	nfsuiop = kmem_cache_alloc(nfs_xuio_cache, KM_SLEEP);
3259 
3260 	bzero(nfsuiop, sizeof (nfs_xuio_t));
3261 	nfsuiop->nu_vp = vp;
3262 
3263 	/*
3264 	 * ref count set to 1. more may be added
3265 	 * if multiple mblks refer to multiple iov's.
3266 	 * This is done in uio_to_mblk().
3267 	 */
3268 
3269 	nfsuiop->nu_ref = 1;
3270 
3271 	nfsuiop->nu_frtn.free_func = rfs_free_xuio;
3272 	nfsuiop->nu_frtn.free_arg = (char *)nfsuiop;
3273 
3274 	nfsuiop->nu_uio.xu_type = UIOTYPE_ZEROCOPY;
3275 
3276 	return (&nfsuiop->nu_uio);
3277 }
3278 
3279 mblk_t *
3280 uio_to_mblk(uio_t *uiop)
3281 {
3282 	struct iovec *iovp;
3283 	int i;
3284 	mblk_t *mp, *mp1;
3285 	nfs_xuio_t *nfsuiop = (nfs_xuio_t *)uiop;
3286 
3287 	if (uiop->uio_iovcnt == 0)
3288 		return (NULL);
3289 
3290 	iovp = uiop->uio_iov;
3291 	mp = mp1 = esballoca((uchar_t *)iovp->iov_base, iovp->iov_len,
3292 	    BPRI_MED, &nfsuiop->nu_frtn);
3293 	ASSERT(mp != NULL);
3294 
3295 	mp->b_wptr += iovp->iov_len;
3296 	mp->b_datap->db_type = M_DATA;
3297 
3298 	for (i = 1; i < uiop->uio_iovcnt; i++) {
3299 		iovp = (uiop->uio_iov + i);
3300 
3301 		mp1->b_cont = esballoca(
3302 		    (uchar_t *)iovp->iov_base, iovp->iov_len, BPRI_MED,
3303 		    &nfsuiop->nu_frtn);
3304 
3305 		mp1 = mp1->b_cont;
3306 		ASSERT(mp1 != NULL);
3307 		mp1->b_wptr += iovp->iov_len;
3308 		mp1->b_datap->db_type = M_DATA;
3309 	}
3310 
3311 	nfsuiop->nu_ref = uiop->uio_iovcnt;
3312 
3313 	return (mp);
3314 }
3315 
3316 /*
3317  * Allocate memory to hold data for a read request of len bytes.
3318  *
3319  * We don't allocate buffers greater than kmem_max_cached in size to avoid
3320  * allocating memory from the kmem_oversized arena.  If we allocate oversized
3321  * buffers, we incur heavy cross-call activity when freeing these large buffers
3322  * in the TCP receive path. Note that we can't set b_wptr here since the
3323  * length of the data returned may differ from the length requested when
3324  * reading the end of a file; we set b_wptr in rfs_rndup_mblks() once the
3325  * length of the read is known.
3326  */
3327 mblk_t *
3328 rfs_read_alloc(uint_t len, struct iovec **iov, int *iovcnt)
3329 {
3330 	struct iovec *iovarr;
3331 	mblk_t *mp, **mpp = &mp;
3332 	size_t mpsize;
3333 	uint_t remain = len;
3334 	int i, err = 0;
3335 
3336 	*iovcnt = howmany(len, kmem_max_cached);
3337 
3338 	iovarr = kmem_alloc(*iovcnt * sizeof (struct iovec), KM_SLEEP);
3339 	*iov = iovarr;
3340 
3341 	for (i = 0; i < *iovcnt; remain -= mpsize, i++) {
3342 		ASSERT(remain <= len);
3343 		/*
3344 		 * We roundup the size we allocate to a multiple of
3345 		 * BYTES_PER_XDR_UNIT (4 bytes) so that the call to
3346 		 * xdrmblk_putmblk() never fails.
3347 		 */
3348 		ASSERT(kmem_max_cached % BYTES_PER_XDR_UNIT == 0);
3349 		mpsize = MIN(kmem_max_cached, remain);
3350 		*mpp = allocb_wait(RNDUP(mpsize), BPRI_MED, STR_NOSIG, &err);
3351 		ASSERT(*mpp != NULL);
3352 		ASSERT(err == 0);
3353 
3354 		iovarr[i].iov_base = (caddr_t)(*mpp)->b_rptr;
3355 		iovarr[i].iov_len = mpsize;
3356 		mpp = &(*mpp)->b_cont;
3357 	}
3358 	return (mp);
3359 }
3360 
3361 void
3362 rfs_rndup_mblks(mblk_t *mp, uint_t len, int buf_loaned)
3363 {
3364 	int i;
3365 	int alloc_err = 0;
3366 	mblk_t *rmp;
3367 	uint_t mpsize, remainder;
3368 
3369 	remainder = P2NPHASE(len, BYTES_PER_XDR_UNIT);
3370 
3371 	/*
3372 	 * Non copy-reduction case.  This function assumes that blocks were
3373 	 * allocated in multiples of BYTES_PER_XDR_UNIT bytes, which makes this
3374 	 * padding safe without bounds checking.
3375 	 */
3376 	if (!buf_loaned) {
3377 		/*
3378 		 * Set the size of each mblk in the chain until we've consumed
3379 		 * the specified length for all but the last one.
3380 		 */
3381 		while ((mpsize = MBLKSIZE(mp)) < len) {
3382 			ASSERT(mpsize % BYTES_PER_XDR_UNIT == 0);
3383 			mp->b_wptr += mpsize;
3384 			len -= mpsize;
3385 			mp = mp->b_cont;
3386 			ASSERT(mp != NULL);
3387 		}
3388 
3389 		ASSERT(len + remainder <= mpsize);
3390 		mp->b_wptr += len;
3391 		for (i = 0; i < remainder; i++)
3392 			*mp->b_wptr++ = '\0';
3393 		return;
3394 	}
3395 
3396 	/*
3397 	 * No remainder mblk required.
3398 	 */
3399 	if (remainder == 0)
3400 		return;
3401 
3402 	/*
3403 	 * Get to the last mblk in the chain.
3404 	 */
3405 	while (mp->b_cont != NULL)
3406 		mp = mp->b_cont;
3407 
3408 	/*
3409 	 * In case of copy-reduction mblks, the size of the mblks are fixed
3410 	 * and are of the size of the loaned buffers.  Allocate a remainder
3411 	 * mblk and chain it to the data buffers. This is sub-optimal, but not
3412 	 * expected to happen commonly.
3413 	 */
3414 	rmp = allocb_wait(remainder, BPRI_MED, STR_NOSIG, &alloc_err);
3415 	ASSERT(rmp != NULL);
3416 	ASSERT(alloc_err == 0);
3417 
3418 	for (i = 0; i < remainder; i++)
3419 		*rmp->b_wptr++ = '\0';
3420 
3421 	rmp->b_datap->db_type = M_DATA;
3422 	mp->b_cont = rmp;
3423 }
3424