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