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