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