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