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