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