xref: /titanic_44/usr/src/uts/common/fs/nfs/nfs_server.c (revision 5fd03bc0f2e00e7ba02316c2e08f45d52aab15db)
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 	uint_t ngids;
2023 	gid_t *gids;
2024 
2025 	/*
2026 	 * Check for privileged port number
2027 	 * N.B.:  this assumes that we know the format of a netbuf.
2028 	 */
2029 	if (nfs_portmon) {
2030 		struct sockaddr *ca;
2031 		ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
2032 
2033 		if (ca == NULL)
2034 			return (0);
2035 
2036 		if ((ca->sa_family == AF_INET &&
2037 		    ntohs(((struct sockaddr_in *)ca)->sin_port) >=
2038 		    IPPORT_RESERVED) ||
2039 		    (ca->sa_family == AF_INET6 &&
2040 		    ntohs(((struct sockaddr_in6 *)ca)->sin6_port) >=
2041 		    IPPORT_RESERVED)) {
2042 			cmn_err(CE_NOTE,
2043 			    "nfs_server: client %s%ssent NFS request from "
2044 			    "unprivileged port",
2045 			    client_name(req), client_addr(req, buf));
2046 			return (0);
2047 		}
2048 	}
2049 
2050 	/*
2051 	 *  return 1 on success or 0 on failure
2052 	 */
2053 	stat = sec_svc_getcred(req, cr, &principal, &nfsflavor);
2054 
2055 	/*
2056 	 * A failed AUTH_UNIX sec_svc_getcred() implies we couldn't set
2057 	 * the credentials; below we map that to anonymous.
2058 	 */
2059 	if (!stat && nfsflavor != AUTH_UNIX) {
2060 		cmn_err(CE_NOTE,
2061 		    "nfs_server: couldn't get unix cred for %s",
2062 		    client_name(req));
2063 		return (0);
2064 	}
2065 
2066 	/*
2067 	 * Short circuit checkauth() on operations that support the
2068 	 * public filehandle, and if the request for that operation
2069 	 * is using the public filehandle. Note that we must call
2070 	 * sec_svc_getcred() first so that xp_cookie is set to the
2071 	 * right value. Normally xp_cookie is just the RPC flavor
2072 	 * of the the request, but in the case of RPCSEC_GSS it
2073 	 * could be a pseudo flavor.
2074 	 */
2075 	if (publicfh_ok)
2076 		return (1);
2077 
2078 	rpcflavor = req->rq_cred.oa_flavor;
2079 	/*
2080 	 * Check if the auth flavor is valid for this export
2081 	 */
2082 	access = nfsauth_access(exi, req, cr, &uid, &gid, &ngids, &gids);
2083 	if (access & NFSAUTH_DROP)
2084 		return (-1);	/* drop the request */
2085 
2086 	if (access & NFSAUTH_RO)
2087 		*ro = TRUE;
2088 
2089 	if (access & NFSAUTH_DENIED) {
2090 		/*
2091 		 * If anon_ok == 1 and we got NFSAUTH_DENIED, it was
2092 		 * probably due to the flavor not matching during
2093 		 * the mount attempt. So map the flavor to AUTH_NONE
2094 		 * so that the credentials get mapped to the anonymous
2095 		 * user.
2096 		 */
2097 		if (anon_ok == 1)
2098 			rpcflavor = AUTH_NONE;
2099 		else
2100 			return (0);	/* deny access */
2101 
2102 	} else if (access & NFSAUTH_MAPNONE) {
2103 		/*
2104 		 * Access was granted even though the flavor mismatched
2105 		 * because AUTH_NONE was one of the exported flavors.
2106 		 */
2107 		rpcflavor = AUTH_NONE;
2108 
2109 	} else if (access & NFSAUTH_WRONGSEC) {
2110 		/*
2111 		 * NFSAUTH_WRONGSEC is used for NFSv4. If we get here,
2112 		 * it means a client ignored the list of allowed flavors
2113 		 * returned via the MOUNT protocol. So we just disallow it!
2114 		 */
2115 		return (0);
2116 	}
2117 
2118 	if (rpcflavor != AUTH_SYS)
2119 		kmem_free(gids, ngids * sizeof (gid_t));
2120 
2121 	switch (rpcflavor) {
2122 	case AUTH_NONE:
2123 		anon_res = crsetugid(cr, exi->exi_export.ex_anon,
2124 		    exi->exi_export.ex_anon);
2125 		(void) crsetgroups(cr, 0, NULL);
2126 		break;
2127 
2128 	case AUTH_UNIX:
2129 		if (!stat || crgetuid(cr) == 0 && !(access & NFSAUTH_UIDMAP)) {
2130 			anon_res = crsetugid(cr, exi->exi_export.ex_anon,
2131 			    exi->exi_export.ex_anon);
2132 			(void) crsetgroups(cr, 0, NULL);
2133 		} else if (crgetuid(cr) == 0 && access & NFSAUTH_ROOT) {
2134 			/*
2135 			 * It is root, so apply rootid to get real UID
2136 			 * Find the secinfo structure.  We should be able
2137 			 * to find it by the time we reach here.
2138 			 * nfsauth_access() has done the checking.
2139 			 */
2140 			secp = NULL;
2141 			for (i = 0; i < exi->exi_export.ex_seccnt; i++) {
2142 				struct secinfo *sptr;
2143 				sptr = &exi->exi_export.ex_secinfo[i];
2144 				if (sptr->s_secinfo.sc_nfsnum == nfsflavor) {
2145 					secp = sptr;
2146 					break;
2147 				}
2148 			}
2149 			if (secp != NULL) {
2150 				(void) crsetugid(cr, secp->s_rootid,
2151 				    secp->s_rootid);
2152 				(void) crsetgroups(cr, 0, NULL);
2153 			}
2154 		} else if (crgetuid(cr) != uid || crgetgid(cr) != gid) {
2155 			if (crsetugid(cr, uid, gid) != 0)
2156 				anon_res = crsetugid(cr,
2157 				    exi->exi_export.ex_anon,
2158 				    exi->exi_export.ex_anon);
2159 			(void) crsetgroups(cr, 0, NULL);
2160 		} else if (access & NFSAUTH_GROUPS) {
2161 			(void) crsetgroups(cr, ngids, gids);
2162 		}
2163 
2164 		kmem_free(gids, ngids * sizeof (gid_t));
2165 
2166 		break;
2167 
2168 	case AUTH_DES:
2169 	case RPCSEC_GSS:
2170 		/*
2171 		 *  Find the secinfo structure.  We should be able
2172 		 *  to find it by the time we reach here.
2173 		 *  nfsauth_access() has done the checking.
2174 		 */
2175 		secp = NULL;
2176 		for (i = 0; i < exi->exi_export.ex_seccnt; i++) {
2177 			if (exi->exi_export.ex_secinfo[i].s_secinfo.sc_nfsnum ==
2178 			    nfsflavor) {
2179 				secp = &exi->exi_export.ex_secinfo[i];
2180 				break;
2181 			}
2182 		}
2183 
2184 		if (!secp) {
2185 			cmn_err(CE_NOTE, "nfs_server: client %s%shad "
2186 			    "no secinfo data for flavor %d",
2187 			    client_name(req), client_addr(req, buf),
2188 			    nfsflavor);
2189 			return (0);
2190 		}
2191 
2192 		if (!checkwin(rpcflavor, secp->s_window, req)) {
2193 			cmn_err(CE_NOTE,
2194 			    "nfs_server: client %s%sused invalid "
2195 			    "auth window value",
2196 			    client_name(req), client_addr(req, buf));
2197 			return (0);
2198 		}
2199 
2200 		/*
2201 		 * Map root principals listed in the share's root= list to root,
2202 		 * and map any others principals that were mapped to root by RPC
2203 		 * to anon.
2204 		 */
2205 		if (principal && sec_svc_inrootlist(rpcflavor, principal,
2206 		    secp->s_rootcnt, secp->s_rootnames)) {
2207 			if (crgetuid(cr) == 0 && secp->s_rootid == 0)
2208 				return (1);
2209 
2210 
2211 			(void) crsetugid(cr, secp->s_rootid, secp->s_rootid);
2212 
2213 			/*
2214 			 * NOTE: If and when kernel-land privilege tracing is
2215 			 * added this may have to be replaced with code that
2216 			 * retrieves root's supplementary groups (e.g., using
2217 			 * kgss_get_group_info().  In the meantime principals
2218 			 * mapped to uid 0 get all privileges, so setting cr's
2219 			 * supplementary groups for them does nothing.
2220 			 */
2221 			(void) crsetgroups(cr, 0, NULL);
2222 
2223 			return (1);
2224 		}
2225 
2226 		/*
2227 		 * Not a root princ, or not in root list, map UID 0/nobody to
2228 		 * the anon ID for the share.  (RPC sets cr's UIDs and GIDs to
2229 		 * UID_NOBODY and GID_NOBODY, respectively.)
2230 		 */
2231 		if (crgetuid(cr) != 0 &&
2232 		    (crgetuid(cr) != UID_NOBODY || crgetgid(cr) != GID_NOBODY))
2233 			return (1);
2234 
2235 		anon_res = crsetugid(cr, exi->exi_export.ex_anon,
2236 		    exi->exi_export.ex_anon);
2237 		(void) crsetgroups(cr, 0, NULL);
2238 		break;
2239 	default:
2240 		return (0);
2241 	} /* switch on rpcflavor */
2242 
2243 	/*
2244 	 * Even if anon access is disallowed via ex_anon == -1, we allow
2245 	 * this access if anon_ok is set.  So set creds to the default
2246 	 * "nobody" id.
2247 	 */
2248 	if (anon_res != 0) {
2249 		if (anon_ok == 0) {
2250 			cmn_err(CE_NOTE,
2251 			    "nfs_server: client %s%ssent wrong "
2252 			    "authentication for %s",
2253 			    client_name(req), client_addr(req, buf),
2254 			    exi->exi_export.ex_path ?
2255 			    exi->exi_export.ex_path : "?");
2256 			return (0);
2257 		}
2258 
2259 		if (crsetugid(cr, UID_NOBODY, GID_NOBODY) != 0)
2260 			return (0);
2261 	}
2262 
2263 	return (1);
2264 }
2265 
2266 /*
2267  * returns 0 on failure, -1 on a drop, -2 on wrong security flavor,
2268  * and 1 on success
2269  */
2270 int
2271 checkauth4(struct compound_state *cs, struct svc_req *req)
2272 {
2273 	int i, rpcflavor, access;
2274 	struct secinfo *secp;
2275 	char buf[MAXHOST + 1];
2276 	int anon_res = 0, nfsflavor;
2277 	struct exportinfo *exi;
2278 	cred_t	*cr;
2279 	caddr_t	principal;
2280 
2281 	uid_t uid;
2282 	gid_t gid;
2283 	uint_t ngids;
2284 	gid_t *gids;
2285 
2286 	exi = cs->exi;
2287 	cr = cs->cr;
2288 	principal = cs->principal;
2289 	nfsflavor = cs->nfsflavor;
2290 
2291 	ASSERT(cr != NULL);
2292 
2293 	rpcflavor = req->rq_cred.oa_flavor;
2294 	cs->access &= ~CS_ACCESS_LIMITED;
2295 
2296 	/*
2297 	 * Check for privileged port number
2298 	 * N.B.:  this assumes that we know the format of a netbuf.
2299 	 */
2300 	if (nfs_portmon) {
2301 		struct sockaddr *ca;
2302 		ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
2303 
2304 		if (ca == NULL)
2305 			return (0);
2306 
2307 		if ((ca->sa_family == AF_INET &&
2308 		    ntohs(((struct sockaddr_in *)ca)->sin_port) >=
2309 		    IPPORT_RESERVED) ||
2310 		    (ca->sa_family == AF_INET6 &&
2311 		    ntohs(((struct sockaddr_in6 *)ca)->sin6_port) >=
2312 		    IPPORT_RESERVED)) {
2313 			cmn_err(CE_NOTE,
2314 			    "nfs_server: client %s%ssent NFSv4 request from "
2315 			    "unprivileged port",
2316 			    client_name(req), client_addr(req, buf));
2317 			return (0);
2318 		}
2319 	}
2320 
2321 	/*
2322 	 * Check the access right per auth flavor on the vnode of
2323 	 * this export for the given request.
2324 	 */
2325 	access = nfsauth4_access(cs->exi, cs->vp, req, cr, &uid, &gid, &ngids,
2326 	    &gids);
2327 
2328 	if (access & NFSAUTH_WRONGSEC)
2329 		return (-2);	/* no access for this security flavor */
2330 
2331 	if (access & NFSAUTH_DROP)
2332 		return (-1);	/* drop the request */
2333 
2334 	if (access & NFSAUTH_DENIED) {
2335 
2336 		if (exi->exi_export.ex_seccnt > 0)
2337 			return (0);	/* deny access */
2338 
2339 	} else if (access & NFSAUTH_LIMITED) {
2340 
2341 		cs->access |= CS_ACCESS_LIMITED;
2342 
2343 	} else if (access & NFSAUTH_MAPNONE) {
2344 		/*
2345 		 * Access was granted even though the flavor mismatched
2346 		 * because AUTH_NONE was one of the exported flavors.
2347 		 */
2348 		rpcflavor = AUTH_NONE;
2349 	}
2350 
2351 	/*
2352 	 * XXX probably need to redo some of it for nfsv4?
2353 	 * return 1 on success or 0 on failure
2354 	 */
2355 
2356 	if (rpcflavor != AUTH_SYS)
2357 		kmem_free(gids, ngids * sizeof (gid_t));
2358 
2359 	switch (rpcflavor) {
2360 	case AUTH_NONE:
2361 		anon_res = crsetugid(cr, exi->exi_export.ex_anon,
2362 		    exi->exi_export.ex_anon);
2363 		(void) crsetgroups(cr, 0, NULL);
2364 		break;
2365 
2366 	case AUTH_UNIX:
2367 		if (crgetuid(cr) == 0 && !(access & NFSAUTH_UIDMAP)) {
2368 			anon_res = crsetugid(cr, exi->exi_export.ex_anon,
2369 			    exi->exi_export.ex_anon);
2370 			(void) crsetgroups(cr, 0, NULL);
2371 		} else if (crgetuid(cr) == 0 && access & NFSAUTH_ROOT) {
2372 			/*
2373 			 * It is root, so apply rootid to get real UID
2374 			 * Find the secinfo structure.  We should be able
2375 			 * to find it by the time we reach here.
2376 			 * nfsauth_access() has done the checking.
2377 			 */
2378 			secp = NULL;
2379 			for (i = 0; i < exi->exi_export.ex_seccnt; i++) {
2380 				struct secinfo *sptr;
2381 				sptr = &exi->exi_export.ex_secinfo[i];
2382 				if (sptr->s_secinfo.sc_nfsnum == nfsflavor) {
2383 					secp = &exi->exi_export.ex_secinfo[i];
2384 					break;
2385 				}
2386 			}
2387 			if (secp != NULL) {
2388 				(void) crsetugid(cr, secp->s_rootid,
2389 				    secp->s_rootid);
2390 				(void) crsetgroups(cr, 0, NULL);
2391 			}
2392 		} else if (crgetuid(cr) != uid || crgetgid(cr) != gid) {
2393 			if (crsetugid(cr, uid, gid) != 0)
2394 				anon_res = crsetugid(cr,
2395 				    exi->exi_export.ex_anon,
2396 				    exi->exi_export.ex_anon);
2397 			(void) crsetgroups(cr, 0, NULL);
2398 		} if (access & NFSAUTH_GROUPS) {
2399 			(void) crsetgroups(cr, ngids, gids);
2400 		}
2401 
2402 		kmem_free(gids, ngids * sizeof (gid_t));
2403 
2404 		break;
2405 
2406 	default:
2407 		/*
2408 		 *  Find the secinfo structure.  We should be able
2409 		 *  to find it by the time we reach here.
2410 		 *  nfsauth_access() has done the checking.
2411 		 */
2412 		secp = NULL;
2413 		for (i = 0; i < exi->exi_export.ex_seccnt; i++) {
2414 			if (exi->exi_export.ex_secinfo[i].s_secinfo.sc_nfsnum ==
2415 			    nfsflavor) {
2416 				secp = &exi->exi_export.ex_secinfo[i];
2417 				break;
2418 			}
2419 		}
2420 
2421 		if (!secp) {
2422 			cmn_err(CE_NOTE, "nfs_server: client %s%shad "
2423 			    "no secinfo data for flavor %d",
2424 			    client_name(req), client_addr(req, buf),
2425 			    nfsflavor);
2426 			return (0);
2427 		}
2428 
2429 		if (!checkwin(rpcflavor, secp->s_window, req)) {
2430 			cmn_err(CE_NOTE,
2431 			    "nfs_server: client %s%sused invalid "
2432 			    "auth window value",
2433 			    client_name(req), client_addr(req, buf));
2434 			return (0);
2435 		}
2436 
2437 		/*
2438 		 * Map root principals listed in the share's root= list to root,
2439 		 * and map any others principals that were mapped to root by RPC
2440 		 * to anon. If not going to anon, set to rootid (root_mapping).
2441 		 */
2442 		if (principal && sec_svc_inrootlist(rpcflavor, principal,
2443 		    secp->s_rootcnt, secp->s_rootnames)) {
2444 			if (crgetuid(cr) == 0 && secp->s_rootid == 0)
2445 				return (1);
2446 
2447 			(void) crsetugid(cr, secp->s_rootid, secp->s_rootid);
2448 
2449 			/*
2450 			 * NOTE: If and when kernel-land privilege tracing is
2451 			 * added this may have to be replaced with code that
2452 			 * retrieves root's supplementary groups (e.g., using
2453 			 * kgss_get_group_info().  In the meantime principals
2454 			 * mapped to uid 0 get all privileges, so setting cr's
2455 			 * supplementary groups for them does nothing.
2456 			 */
2457 			(void) crsetgroups(cr, 0, NULL);
2458 
2459 			return (1);
2460 		}
2461 
2462 		/*
2463 		 * Not a root princ, or not in root list, map UID 0/nobody to
2464 		 * the anon ID for the share.  (RPC sets cr's UIDs and GIDs to
2465 		 * UID_NOBODY and GID_NOBODY, respectively.)
2466 		 */
2467 		if (crgetuid(cr) != 0 &&
2468 		    (crgetuid(cr) != UID_NOBODY || crgetgid(cr) != GID_NOBODY))
2469 			return (1);
2470 
2471 		anon_res = crsetugid(cr, exi->exi_export.ex_anon,
2472 		    exi->exi_export.ex_anon);
2473 		(void) crsetgroups(cr, 0, NULL);
2474 		break;
2475 	} /* switch on rpcflavor */
2476 
2477 	/*
2478 	 * Even if anon access is disallowed via ex_anon == -1, we allow
2479 	 * this access if anon_ok is set.  So set creds to the default
2480 	 * "nobody" id.
2481 	 */
2482 
2483 	if (anon_res != 0) {
2484 		cmn_err(CE_NOTE,
2485 		    "nfs_server: client %s%ssent wrong "
2486 		    "authentication for %s",
2487 		    client_name(req), client_addr(req, buf),
2488 		    exi->exi_export.ex_path ?
2489 		    exi->exi_export.ex_path : "?");
2490 		return (0);
2491 	}
2492 
2493 	return (1);
2494 }
2495 
2496 
2497 static char *
2498 client_name(struct svc_req *req)
2499 {
2500 	char *hostname = NULL;
2501 
2502 	/*
2503 	 * If it's a Unix cred then use the
2504 	 * hostname from the credential.
2505 	 */
2506 	if (req->rq_cred.oa_flavor == AUTH_UNIX) {
2507 		hostname = ((struct authunix_parms *)
2508 		    req->rq_clntcred)->aup_machname;
2509 	}
2510 	if (hostname == NULL)
2511 		hostname = "";
2512 
2513 	return (hostname);
2514 }
2515 
2516 static char *
2517 client_addr(struct svc_req *req, char *buf)
2518 {
2519 	struct sockaddr *ca;
2520 	uchar_t *b;
2521 	char *frontspace = "";
2522 
2523 	/*
2524 	 * We assume we are called in tandem with client_name and the
2525 	 * format string looks like "...client %s%sblah blah..."
2526 	 *
2527 	 * If it's a Unix cred then client_name returned
2528 	 * a host name, so we need insert a space between host name
2529 	 * and IP address.
2530 	 */
2531 	if (req->rq_cred.oa_flavor == AUTH_UNIX)
2532 		frontspace = " ";
2533 
2534 	/*
2535 	 * Convert the caller's IP address to a dotted string
2536 	 */
2537 	ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
2538 
2539 	if (ca->sa_family == AF_INET) {
2540 		b = (uchar_t *)&((struct sockaddr_in *)ca)->sin_addr;
2541 		(void) sprintf(buf, "%s(%d.%d.%d.%d) ", frontspace,
2542 		    b[0] & 0xFF, b[1] & 0xFF, b[2] & 0xFF, b[3] & 0xFF);
2543 	} else if (ca->sa_family == AF_INET6) {
2544 		struct sockaddr_in6 *sin6;
2545 		sin6 = (struct sockaddr_in6 *)ca;
2546 		(void) kinet_ntop6((uchar_t *)&sin6->sin6_addr,
2547 		    buf, INET6_ADDRSTRLEN);
2548 
2549 	} else {
2550 
2551 		/*
2552 		 * No IP address to print. If there was a host name
2553 		 * printed, then we print a space.
2554 		 */
2555 		(void) sprintf(buf, frontspace);
2556 	}
2557 
2558 	return (buf);
2559 }
2560 
2561 /*
2562  * NFS Server initialization routine.  This routine should only be called
2563  * once.  It performs the following tasks:
2564  *	- Call sub-initialization routines (localize access to variables)
2565  *	- Initialize all locks
2566  *	- initialize the version 3 write verifier
2567  */
2568 int
2569 nfs_srvinit(void)
2570 {
2571 	int error;
2572 
2573 	error = nfs_exportinit();
2574 	if (error != 0)
2575 		return (error);
2576 	error = rfs4_srvrinit();
2577 	if (error != 0) {
2578 		nfs_exportfini();
2579 		return (error);
2580 	}
2581 	rfs_srvrinit();
2582 	rfs3_srvrinit();
2583 	nfsauth_init();
2584 
2585 	/* Init the stuff to control start/stop */
2586 	nfs_server_upordown = NFS_SERVER_STOPPED;
2587 	mutex_init(&nfs_server_upordown_lock, NULL, MUTEX_DEFAULT, NULL);
2588 	cv_init(&nfs_server_upordown_cv, NULL, CV_DEFAULT, NULL);
2589 	mutex_init(&rdma_wait_mutex, NULL, MUTEX_DEFAULT, NULL);
2590 	cv_init(&rdma_wait_cv, NULL, CV_DEFAULT, NULL);
2591 
2592 	return (0);
2593 }
2594 
2595 /*
2596  * NFS Server finalization routine. This routine is called to cleanup the
2597  * initialization work previously performed if the NFS server module could
2598  * not be loaded correctly.
2599  */
2600 void
2601 nfs_srvfini(void)
2602 {
2603 	nfsauth_fini();
2604 	rfs3_srvrfini();
2605 	rfs_srvrfini();
2606 	nfs_exportfini();
2607 
2608 	mutex_destroy(&nfs_server_upordown_lock);
2609 	cv_destroy(&nfs_server_upordown_cv);
2610 	mutex_destroy(&rdma_wait_mutex);
2611 	cv_destroy(&rdma_wait_cv);
2612 }
2613 
2614 /*
2615  * Set up an iovec array of up to cnt pointers.
2616  */
2617 
2618 void
2619 mblk_to_iov(mblk_t *m, int cnt, struct iovec *iovp)
2620 {
2621 	while (m != NULL && cnt-- > 0) {
2622 		iovp->iov_base = (caddr_t)m->b_rptr;
2623 		iovp->iov_len = (m->b_wptr - m->b_rptr);
2624 		iovp++;
2625 		m = m->b_cont;
2626 	}
2627 }
2628 
2629 /*
2630  * Common code between NFS Version 2 and NFS Version 3 for the public
2631  * filehandle multicomponent lookups.
2632  */
2633 
2634 /*
2635  * Public filehandle evaluation of a multi-component lookup, following
2636  * symbolic links, if necessary. This may result in a vnode in another
2637  * filesystem, which is OK as long as the other filesystem is exported.
2638  *
2639  * Note that the exi will be set either to NULL or a new reference to the
2640  * exportinfo struct that corresponds to the vnode of the multi-component path.
2641  * It is the callers responsibility to release this reference.
2642  */
2643 int
2644 rfs_publicfh_mclookup(char *p, vnode_t *dvp, cred_t *cr, vnode_t **vpp,
2645     struct exportinfo **exi, struct sec_ol *sec)
2646 {
2647 	int pathflag;
2648 	vnode_t *mc_dvp = NULL;
2649 	vnode_t *realvp;
2650 	int error;
2651 
2652 	*exi = NULL;
2653 
2654 	/*
2655 	 * check if the given path is a url or native path. Since p is
2656 	 * modified by MCLpath(), it may be empty after returning from
2657 	 * there, and should be checked.
2658 	 */
2659 	if ((pathflag = MCLpath(&p)) == -1)
2660 		return (EIO);
2661 
2662 	/*
2663 	 * If pathflag is SECURITY_QUERY, turn the SEC_QUERY bit
2664 	 * on in sec->sec_flags. This bit will later serve as an
2665 	 * indication in makefh_ol() or makefh3_ol() to overload the
2666 	 * filehandle to contain the sec modes used by the server for
2667 	 * the path.
2668 	 */
2669 	if (pathflag == SECURITY_QUERY) {
2670 		if ((sec->sec_index = (uint_t)(*p)) > 0) {
2671 			sec->sec_flags |= SEC_QUERY;
2672 			p++;
2673 			if ((pathflag = MCLpath(&p)) == -1)
2674 				return (EIO);
2675 		} else {
2676 			cmn_err(CE_NOTE,
2677 			    "nfs_server: invalid security index %d, "
2678 			    "violating WebNFS SNEGO protocol.", sec->sec_index);
2679 			return (EIO);
2680 		}
2681 	}
2682 
2683 	if (p[0] == '\0') {
2684 		error = ENOENT;
2685 		goto publicfh_done;
2686 	}
2687 
2688 	error = rfs_pathname(p, &mc_dvp, vpp, dvp, cr, pathflag);
2689 
2690 	/*
2691 	 * If name resolves to "/" we get EINVAL since we asked for
2692 	 * the vnode of the directory that the file is in. Try again
2693 	 * with NULL directory vnode.
2694 	 */
2695 	if (error == EINVAL) {
2696 		error = rfs_pathname(p, NULL, vpp, dvp, cr, pathflag);
2697 		if (!error) {
2698 			ASSERT(*vpp != NULL);
2699 			if ((*vpp)->v_type == VDIR) {
2700 				VN_HOLD(*vpp);
2701 				mc_dvp = *vpp;
2702 			} else {
2703 				/*
2704 				 * This should not happen, the filesystem is
2705 				 * in an inconsistent state. Fail the lookup
2706 				 * at this point.
2707 				 */
2708 				VN_RELE(*vpp);
2709 				error = EINVAL;
2710 			}
2711 		}
2712 	}
2713 
2714 	if (error)
2715 		goto publicfh_done;
2716 
2717 	if (*vpp == NULL) {
2718 		error = ENOENT;
2719 		goto publicfh_done;
2720 	}
2721 
2722 	ASSERT(mc_dvp != NULL);
2723 	ASSERT(*vpp != NULL);
2724 
2725 	if ((*vpp)->v_type == VDIR) {
2726 		do {
2727 			/*
2728 			 * *vpp may be an AutoFS node, so we perform
2729 			 * a VOP_ACCESS() to trigger the mount of the intended
2730 			 * filesystem, so we can perform the lookup in the
2731 			 * intended filesystem.
2732 			 */
2733 			(void) VOP_ACCESS(*vpp, 0, 0, cr, NULL);
2734 
2735 			/*
2736 			 * If vnode is covered, get the
2737 			 * the topmost vnode.
2738 			 */
2739 			if (vn_mountedvfs(*vpp) != NULL) {
2740 				error = traverse(vpp);
2741 				if (error) {
2742 					VN_RELE(*vpp);
2743 					goto publicfh_done;
2744 				}
2745 			}
2746 
2747 			if (VOP_REALVP(*vpp, &realvp, NULL) == 0 &&
2748 			    realvp != *vpp) {
2749 				/*
2750 				 * If realvp is different from *vpp
2751 				 * then release our reference on *vpp, so that
2752 				 * the export access check be performed on the
2753 				 * real filesystem instead.
2754 				 */
2755 				VN_HOLD(realvp);
2756 				VN_RELE(*vpp);
2757 				*vpp = realvp;
2758 			} else {
2759 				break;
2760 			}
2761 		/* LINTED */
2762 		} while (TRUE);
2763 
2764 		/*
2765 		 * Let nfs_vptexi() figure what the real parent is.
2766 		 */
2767 		VN_RELE(mc_dvp);
2768 		mc_dvp = NULL;
2769 
2770 	} else {
2771 		/*
2772 		 * If vnode is covered, get the
2773 		 * the topmost vnode.
2774 		 */
2775 		if (vn_mountedvfs(mc_dvp) != NULL) {
2776 			error = traverse(&mc_dvp);
2777 			if (error) {
2778 				VN_RELE(*vpp);
2779 				goto publicfh_done;
2780 			}
2781 		}
2782 
2783 		if (VOP_REALVP(mc_dvp, &realvp, NULL) == 0 &&
2784 		    realvp != mc_dvp) {
2785 			/*
2786 			 * *vpp is a file, obtain realvp of the parent
2787 			 * directory vnode.
2788 			 */
2789 			VN_HOLD(realvp);
2790 			VN_RELE(mc_dvp);
2791 			mc_dvp = realvp;
2792 		}
2793 	}
2794 
2795 	/*
2796 	 * The pathname may take us from the public filesystem to another.
2797 	 * If that's the case then just set the exportinfo to the new export
2798 	 * and build filehandle for it. Thanks to per-access checking there's
2799 	 * no security issues with doing this. If the client is not allowed
2800 	 * access to this new export then it will get an access error when it
2801 	 * tries to use the filehandle
2802 	 */
2803 	if (error = nfs_check_vpexi(mc_dvp, *vpp, kcred, exi)) {
2804 		VN_RELE(*vpp);
2805 		goto publicfh_done;
2806 	}
2807 
2808 	/*
2809 	 * Not allowed access to pseudo exports.
2810 	 */
2811 	if (PSEUDO(*exi)) {
2812 		error = ENOENT;
2813 		VN_RELE(*vpp);
2814 		goto publicfh_done;
2815 	}
2816 
2817 	/*
2818 	 * Do a lookup for the index file. We know the index option doesn't
2819 	 * allow paths through handling in the share command, so mc_dvp will
2820 	 * be the parent for the index file vnode, if its present. Use
2821 	 * temporary pointers to preserve and reuse the vnode pointers of the
2822 	 * original directory in case there's no index file. Note that the
2823 	 * index file is a native path, and should not be interpreted by
2824 	 * the URL parser in rfs_pathname()
2825 	 */
2826 	if (((*exi)->exi_export.ex_flags & EX_INDEX) &&
2827 	    ((*vpp)->v_type == VDIR) && (pathflag == URLPATH)) {
2828 		vnode_t *tvp, *tmc_dvp;	/* temporary vnode pointers */
2829 
2830 		tmc_dvp = mc_dvp;
2831 		mc_dvp = tvp = *vpp;
2832 
2833 		error = rfs_pathname((*exi)->exi_export.ex_index, NULL, vpp,
2834 		    mc_dvp, cr, NATIVEPATH);
2835 
2836 		if (error == ENOENT) {
2837 			*vpp = tvp;
2838 			mc_dvp = tmc_dvp;
2839 			error = 0;
2840 		} else {	/* ok or error other than ENOENT */
2841 			if (tmc_dvp)
2842 				VN_RELE(tmc_dvp);
2843 			if (error)
2844 				goto publicfh_done;
2845 
2846 			/*
2847 			 * Found a valid vp for index "filename". Sanity check
2848 			 * for odd case where a directory is provided as index
2849 			 * option argument and leads us to another filesystem
2850 			 */
2851 
2852 			/* Release the reference on the old exi value */
2853 			ASSERT(*exi != NULL);
2854 			exi_rele(*exi);
2855 
2856 			if (error = nfs_check_vpexi(mc_dvp, *vpp, kcred, exi)) {
2857 				VN_RELE(*vpp);
2858 				goto publicfh_done;
2859 			}
2860 		}
2861 	}
2862 
2863 publicfh_done:
2864 	if (mc_dvp)
2865 		VN_RELE(mc_dvp);
2866 
2867 	return (error);
2868 }
2869 
2870 /*
2871  * Evaluate a multi-component path
2872  */
2873 int
2874 rfs_pathname(
2875 	char *path,			/* pathname to evaluate */
2876 	vnode_t **dirvpp,		/* ret for ptr to parent dir vnode */
2877 	vnode_t **compvpp,		/* ret for ptr to component vnode */
2878 	vnode_t *startdvp,		/* starting vnode */
2879 	cred_t *cr,			/* user's credential */
2880 	int pathflag)			/* flag to identify path, e.g. URL */
2881 {
2882 	char namebuf[TYPICALMAXPATHLEN];
2883 	struct pathname pn;
2884 	int error;
2885 
2886 	/*
2887 	 * If pathname starts with '/', then set startdvp to root.
2888 	 */
2889 	if (*path == '/') {
2890 		while (*path == '/')
2891 			path++;
2892 
2893 		startdvp = rootdir;
2894 	}
2895 
2896 	error = pn_get_buf(path, UIO_SYSSPACE, &pn, namebuf, sizeof (namebuf));
2897 	if (error == 0) {
2898 		/*
2899 		 * Call the URL parser for URL paths to modify the original
2900 		 * string to handle any '%' encoded characters that exist.
2901 		 * Done here to avoid an extra bcopy in the lookup.
2902 		 * We need to be careful about pathlen's. We know that
2903 		 * rfs_pathname() is called with a non-empty path. However,
2904 		 * it could be emptied due to the path simply being all /'s,
2905 		 * which is valid to proceed with the lookup, or due to the
2906 		 * URL parser finding an encoded null character at the
2907 		 * beginning of path which should not proceed with the lookup.
2908 		 */
2909 		if (pn.pn_pathlen != 0 && pathflag == URLPATH) {
2910 			URLparse(pn.pn_path);
2911 			if ((pn.pn_pathlen = strlen(pn.pn_path)) == 0)
2912 				return (ENOENT);
2913 		}
2914 		VN_HOLD(startdvp);
2915 		error = lookuppnvp(&pn, NULL, NO_FOLLOW, dirvpp, compvpp,
2916 		    rootdir, startdvp, cr);
2917 	}
2918 	if (error == ENAMETOOLONG) {
2919 		/*
2920 		 * This thread used a pathname > TYPICALMAXPATHLEN bytes long.
2921 		 */
2922 		if (error = pn_get(path, UIO_SYSSPACE, &pn))
2923 			return (error);
2924 		if (pn.pn_pathlen != 0 && pathflag == URLPATH) {
2925 			URLparse(pn.pn_path);
2926 			if ((pn.pn_pathlen = strlen(pn.pn_path)) == 0) {
2927 				pn_free(&pn);
2928 				return (ENOENT);
2929 			}
2930 		}
2931 		VN_HOLD(startdvp);
2932 		error = lookuppnvp(&pn, NULL, NO_FOLLOW, dirvpp, compvpp,
2933 		    rootdir, startdvp, cr);
2934 		pn_free(&pn);
2935 	}
2936 
2937 	return (error);
2938 }
2939 
2940 /*
2941  * Adapt the multicomponent lookup path depending on the pathtype
2942  */
2943 static int
2944 MCLpath(char **path)
2945 {
2946 	unsigned char c = (unsigned char)**path;
2947 
2948 	/*
2949 	 * If the MCL path is between 0x20 and 0x7E (graphic printable
2950 	 * character of the US-ASCII coded character set), its a URL path,
2951 	 * per RFC 1738.
2952 	 */
2953 	if (c >= 0x20 && c <= 0x7E)
2954 		return (URLPATH);
2955 
2956 	/*
2957 	 * If the first octet of the MCL path is not an ASCII character
2958 	 * then it must be interpreted as a tag value that describes the
2959 	 * format of the remaining octets of the MCL path.
2960 	 *
2961 	 * If the first octet of the MCL path is 0x81 it is a query
2962 	 * for the security info.
2963 	 */
2964 	switch (c) {
2965 	case 0x80:	/* native path, i.e. MCL via mount protocol */
2966 		(*path)++;
2967 		return (NATIVEPATH);
2968 	case 0x81:	/* security query */
2969 		(*path)++;
2970 		return (SECURITY_QUERY);
2971 	default:
2972 		return (-1);
2973 	}
2974 }
2975 
2976 #define	fromhex(c)  ((c >= '0' && c <= '9') ? (c - '0') : \
2977 			((c >= 'A' && c <= 'F') ? (c - 'A' + 10) :\
2978 			((c >= 'a' && c <= 'f') ? (c - 'a' + 10) : 0)))
2979 
2980 /*
2981  * The implementation of URLparse guarantees that the final string will
2982  * fit in the original one. Replaces '%' occurrences followed by 2 characters
2983  * with its corresponding hexadecimal character.
2984  */
2985 static void
2986 URLparse(char *str)
2987 {
2988 	char *p, *q;
2989 
2990 	p = q = str;
2991 	while (*p) {
2992 		*q = *p;
2993 		if (*p++ == '%') {
2994 			if (*p) {
2995 				*q = fromhex(*p) * 16;
2996 				p++;
2997 				if (*p) {
2998 					*q += fromhex(*p);
2999 					p++;
3000 				}
3001 			}
3002 		}
3003 		q++;
3004 	}
3005 	*q = '\0';
3006 }
3007 
3008 
3009 /*
3010  * Get the export information for the lookup vnode, and verify its
3011  * useable.
3012  */
3013 int
3014 nfs_check_vpexi(vnode_t *mc_dvp, vnode_t *vp, cred_t *cr,
3015     struct exportinfo **exi)
3016 {
3017 	int walk;
3018 	int error = 0;
3019 
3020 	*exi = nfs_vptoexi(mc_dvp, vp, cr, &walk, NULL, FALSE);
3021 	if (*exi == NULL)
3022 		error = EACCES;
3023 	else {
3024 		/*
3025 		 * If nosub is set for this export then
3026 		 * a lookup relative to the public fh
3027 		 * must not terminate below the
3028 		 * exported directory.
3029 		 */
3030 		if ((*exi)->exi_export.ex_flags & EX_NOSUB && walk > 0)
3031 			error = EACCES;
3032 	}
3033 
3034 	return (error);
3035 }
3036 
3037 /*
3038  * Do the main work of handling HA-NFSv4 Resource Group failover on
3039  * Sun Cluster.
3040  * We need to detect whether any RG admin paths have been added or removed,
3041  * and adjust resources accordingly.
3042  * Currently we're using a very inefficient algorithm, ~ 2 * O(n**2). In
3043  * order to scale, the list and array of paths need to be held in more
3044  * suitable data structures.
3045  */
3046 static void
3047 hanfsv4_failover(void)
3048 {
3049 	int i, start_grace, numadded_paths = 0;
3050 	char **added_paths = NULL;
3051 	rfs4_dss_path_t *dss_path;
3052 
3053 	/*
3054 	 * Note: currently, rfs4_dss_pathlist cannot be NULL, since
3055 	 * it will always include an entry for NFS4_DSS_VAR_DIR. If we
3056 	 * make the latter dynamically specified too, the following will
3057 	 * need to be adjusted.
3058 	 */
3059 
3060 	/*
3061 	 * First, look for removed paths: RGs that have been failed-over
3062 	 * away from this node.
3063 	 * Walk the "currently-serving" rfs4_dss_pathlist and, for each
3064 	 * path, check if it is on the "passed-in" rfs4_dss_newpaths array
3065 	 * from nfsd. If not, that RG path has been removed.
3066 	 *
3067 	 * Note that nfsd has sorted rfs4_dss_newpaths for us, and removed
3068 	 * any duplicates.
3069 	 */
3070 	dss_path = rfs4_dss_pathlist;
3071 	do {
3072 		int found = 0;
3073 		char *path = dss_path->path;
3074 
3075 		/* used only for non-HA so may not be removed */
3076 		if (strcmp(path, NFS4_DSS_VAR_DIR) == 0) {
3077 			dss_path = dss_path->next;
3078 			continue;
3079 		}
3080 
3081 		for (i = 0; i < rfs4_dss_numnewpaths; i++) {
3082 			int cmpret;
3083 			char *newpath = rfs4_dss_newpaths[i];
3084 
3085 			/*
3086 			 * Since nfsd has sorted rfs4_dss_newpaths for us,
3087 			 * once the return from strcmp is negative we know
3088 			 * we've passed the point where "path" should be,
3089 			 * and can stop searching: "path" has been removed.
3090 			 */
3091 			cmpret = strcmp(path, newpath);
3092 			if (cmpret < 0)
3093 				break;
3094 			if (cmpret == 0) {
3095 				found = 1;
3096 				break;
3097 			}
3098 		}
3099 
3100 		if (found == 0) {
3101 			unsigned index = dss_path->index;
3102 			rfs4_servinst_t *sip = dss_path->sip;
3103 			rfs4_dss_path_t *path_next = dss_path->next;
3104 
3105 			/*
3106 			 * This path has been removed.
3107 			 * We must clear out the servinst reference to
3108 			 * it, since it's now owned by another
3109 			 * node: we should not attempt to touch it.
3110 			 */
3111 			ASSERT(dss_path == sip->dss_paths[index]);
3112 			sip->dss_paths[index] = NULL;
3113 
3114 			/* remove from "currently-serving" list, and destroy */
3115 			remque(dss_path);
3116 			/* allow for NUL */
3117 			kmem_free(dss_path->path, strlen(dss_path->path) + 1);
3118 			kmem_free(dss_path, sizeof (rfs4_dss_path_t));
3119 
3120 			dss_path = path_next;
3121 		} else {
3122 			/* path was found; not removed */
3123 			dss_path = dss_path->next;
3124 		}
3125 	} while (dss_path != rfs4_dss_pathlist);
3126 
3127 	/*
3128 	 * Now, look for added paths: RGs that have been failed-over
3129 	 * to this node.
3130 	 * Walk the "passed-in" rfs4_dss_newpaths array from nfsd and,
3131 	 * for each path, check if it is on the "currently-serving"
3132 	 * rfs4_dss_pathlist. If not, that RG path has been added.
3133 	 *
3134 	 * Note: we don't do duplicate detection here; nfsd does that for us.
3135 	 *
3136 	 * Note: numadded_paths <= rfs4_dss_numnewpaths, which gives us
3137 	 * an upper bound for the size needed for added_paths[numadded_paths].
3138 	 */
3139 
3140 	/* probably more space than we need, but guaranteed to be enough */
3141 	if (rfs4_dss_numnewpaths > 0) {
3142 		size_t sz = rfs4_dss_numnewpaths * sizeof (char *);
3143 		added_paths = kmem_zalloc(sz, KM_SLEEP);
3144 	}
3145 
3146 	/* walk the "passed-in" rfs4_dss_newpaths array from nfsd */
3147 	for (i = 0; i < rfs4_dss_numnewpaths; i++) {
3148 		int found = 0;
3149 		char *newpath = rfs4_dss_newpaths[i];
3150 
3151 		dss_path = rfs4_dss_pathlist;
3152 		do {
3153 			char *path = dss_path->path;
3154 
3155 			/* used only for non-HA */
3156 			if (strcmp(path, NFS4_DSS_VAR_DIR) == 0) {
3157 				dss_path = dss_path->next;
3158 				continue;
3159 			}
3160 
3161 			if (strncmp(path, newpath, strlen(path)) == 0) {
3162 				found = 1;
3163 				break;
3164 			}
3165 
3166 			dss_path = dss_path->next;
3167 		} while (dss_path != rfs4_dss_pathlist);
3168 
3169 		if (found == 0) {
3170 			added_paths[numadded_paths] = newpath;
3171 			numadded_paths++;
3172 		}
3173 	}
3174 
3175 	/* did we find any added paths? */
3176 	if (numadded_paths > 0) {
3177 		/* create a new server instance, and start its grace period */
3178 		start_grace = 1;
3179 		rfs4_servinst_create(start_grace, numadded_paths, added_paths);
3180 
3181 		/* read in the stable storage state from these paths */
3182 		rfs4_dss_readstate(numadded_paths, added_paths);
3183 
3184 		/*
3185 		 * Multiple failovers during a grace period will cause
3186 		 * clients of the same resource group to be partitioned
3187 		 * into different server instances, with different
3188 		 * grace periods.  Since clients of the same resource
3189 		 * group must be subject to the same grace period,
3190 		 * we need to reset all currently active grace periods.
3191 		 */
3192 		rfs4_grace_reset_all();
3193 	}
3194 
3195 	if (rfs4_dss_numnewpaths > 0)
3196 		kmem_free(added_paths, rfs4_dss_numnewpaths * sizeof (char *));
3197 }
3198 
3199 /*
3200  * Used by NFSv3 and NFSv4 server to query label of
3201  * a pathname component during lookup/access ops.
3202  */
3203 ts_label_t *
3204 nfs_getflabel(vnode_t *vp, struct exportinfo *exi)
3205 {
3206 	zone_t *zone;
3207 	ts_label_t *zone_label;
3208 	char *path;
3209 
3210 	mutex_enter(&vp->v_lock);
3211 	if (vp->v_path != NULL) {
3212 		zone = zone_find_by_any_path(vp->v_path, B_FALSE);
3213 		mutex_exit(&vp->v_lock);
3214 	} else {
3215 		/*
3216 		 * v_path not cached. Fall back on pathname of exported
3217 		 * file system as we rely on pathname from which we can
3218 		 * derive a label. The exported file system portion of
3219 		 * path is sufficient to obtain a label.
3220 		 */
3221 		path = exi->exi_export.ex_path;
3222 		if (path == NULL) {
3223 			mutex_exit(&vp->v_lock);
3224 			return (NULL);
3225 		}
3226 		zone = zone_find_by_any_path(path, B_FALSE);
3227 		mutex_exit(&vp->v_lock);
3228 	}
3229 	/*
3230 	 * Caller has verified that the file is either
3231 	 * exported or visible. So if the path falls in
3232 	 * global zone, admin_low is returned; otherwise
3233 	 * the zone's label is returned.
3234 	 */
3235 	zone_label = zone->zone_slabel;
3236 	label_hold(zone_label);
3237 	zone_rele(zone);
3238 	return (zone_label);
3239 }
3240 
3241 /*
3242  * TX NFS routine used by NFSv3 and NFSv4 to do label check
3243  * on client label and server's file object lable.
3244  */
3245 boolean_t
3246 do_rfs_label_check(bslabel_t *clabel, vnode_t *vp, int flag,
3247     struct exportinfo *exi)
3248 {
3249 	bslabel_t *slabel;
3250 	ts_label_t *tslabel;
3251 	boolean_t result;
3252 
3253 	if ((tslabel = nfs_getflabel(vp, exi)) == NULL) {
3254 		return (B_FALSE);
3255 	}
3256 	slabel = label2bslabel(tslabel);
3257 	DTRACE_PROBE4(tx__rfs__log__info__labelcheck, char *,
3258 	    "comparing server's file label(1) with client label(2) (vp(3))",
3259 	    bslabel_t *, slabel, bslabel_t *, clabel, vnode_t *, vp);
3260 
3261 	if (flag == EQUALITY_CHECK)
3262 		result = blequal(clabel, slabel);
3263 	else
3264 		result = bldominates(clabel, slabel);
3265 	label_rele(tslabel);
3266 	return (result);
3267 }
3268 
3269 /*
3270  * Callback function to return the loaned buffers.
3271  * Calls VOP_RETZCBUF() only after all uio_iov[]
3272  * buffers are returned. nu_ref maintains the count.
3273  */
3274 void
3275 rfs_free_xuio(void *free_arg)
3276 {
3277 	uint_t ref;
3278 	nfs_xuio_t *nfsuiop = (nfs_xuio_t *)free_arg;
3279 
3280 	ref = atomic_dec_uint_nv(&nfsuiop->nu_ref);
3281 
3282 	/*
3283 	 * Call VOP_RETZCBUF() only when all the iov buffers
3284 	 * are sent OTW.
3285 	 */
3286 	if (ref != 0)
3287 		return;
3288 
3289 	if (((uio_t *)nfsuiop)->uio_extflg & UIO_XUIO) {
3290 		(void) VOP_RETZCBUF(nfsuiop->nu_vp, (xuio_t *)free_arg, NULL,
3291 		    NULL);
3292 		VN_RELE(nfsuiop->nu_vp);
3293 	}
3294 
3295 	kmem_cache_free(nfs_xuio_cache, free_arg);
3296 }
3297 
3298 xuio_t *
3299 rfs_setup_xuio(vnode_t *vp)
3300 {
3301 	nfs_xuio_t *nfsuiop;
3302 
3303 	nfsuiop = kmem_cache_alloc(nfs_xuio_cache, KM_SLEEP);
3304 
3305 	bzero(nfsuiop, sizeof (nfs_xuio_t));
3306 	nfsuiop->nu_vp = vp;
3307 
3308 	/*
3309 	 * ref count set to 1. more may be added
3310 	 * if multiple mblks refer to multiple iov's.
3311 	 * This is done in uio_to_mblk().
3312 	 */
3313 
3314 	nfsuiop->nu_ref = 1;
3315 
3316 	nfsuiop->nu_frtn.free_func = rfs_free_xuio;
3317 	nfsuiop->nu_frtn.free_arg = (char *)nfsuiop;
3318 
3319 	nfsuiop->nu_uio.xu_type = UIOTYPE_ZEROCOPY;
3320 
3321 	return (&nfsuiop->nu_uio);
3322 }
3323 
3324 mblk_t *
3325 uio_to_mblk(uio_t *uiop)
3326 {
3327 	struct iovec *iovp;
3328 	int i;
3329 	mblk_t *mp, *mp1;
3330 	nfs_xuio_t *nfsuiop = (nfs_xuio_t *)uiop;
3331 
3332 	if (uiop->uio_iovcnt == 0)
3333 		return (NULL);
3334 
3335 	iovp = uiop->uio_iov;
3336 	mp = mp1 = esballoca((uchar_t *)iovp->iov_base, iovp->iov_len,
3337 	    BPRI_MED, &nfsuiop->nu_frtn);
3338 	ASSERT(mp != NULL);
3339 
3340 	mp->b_wptr += iovp->iov_len;
3341 	mp->b_datap->db_type = M_DATA;
3342 
3343 	for (i = 1; i < uiop->uio_iovcnt; i++) {
3344 		iovp = (uiop->uio_iov + i);
3345 
3346 		mp1->b_cont = esballoca(
3347 		    (uchar_t *)iovp->iov_base, iovp->iov_len, BPRI_MED,
3348 		    &nfsuiop->nu_frtn);
3349 
3350 		mp1 = mp1->b_cont;
3351 		ASSERT(mp1 != NULL);
3352 		mp1->b_wptr += iovp->iov_len;
3353 		mp1->b_datap->db_type = M_DATA;
3354 	}
3355 
3356 	nfsuiop->nu_ref = uiop->uio_iovcnt;
3357 
3358 	return (mp);
3359 }
3360 
3361 /*
3362  * Allocate memory to hold data for a read request of len bytes.
3363  *
3364  * We don't allocate buffers greater than kmem_max_cached in size to avoid
3365  * allocating memory from the kmem_oversized arena.  If we allocate oversized
3366  * buffers, we incur heavy cross-call activity when freeing these large buffers
3367  * in the TCP receive path. Note that we can't set b_wptr here since the
3368  * length of the data returned may differ from the length requested when
3369  * reading the end of a file; we set b_wptr in rfs_rndup_mblks() once the
3370  * length of the read is known.
3371  */
3372 mblk_t *
3373 rfs_read_alloc(uint_t len, struct iovec **iov, int *iovcnt)
3374 {
3375 	struct iovec *iovarr;
3376 	mblk_t *mp, **mpp = &mp;
3377 	size_t mpsize;
3378 	uint_t remain = len;
3379 	int i, err = 0;
3380 
3381 	*iovcnt = howmany(len, kmem_max_cached);
3382 
3383 	iovarr = kmem_alloc(*iovcnt * sizeof (struct iovec), KM_SLEEP);
3384 	*iov = iovarr;
3385 
3386 	for (i = 0; i < *iovcnt; remain -= mpsize, i++) {
3387 		ASSERT(remain <= len);
3388 		/*
3389 		 * We roundup the size we allocate to a multiple of
3390 		 * BYTES_PER_XDR_UNIT (4 bytes) so that the call to
3391 		 * xdrmblk_putmblk() never fails.
3392 		 */
3393 		ASSERT(kmem_max_cached % BYTES_PER_XDR_UNIT == 0);
3394 		mpsize = MIN(kmem_max_cached, remain);
3395 		*mpp = allocb_wait(RNDUP(mpsize), BPRI_MED, STR_NOSIG, &err);
3396 		ASSERT(*mpp != NULL);
3397 		ASSERT(err == 0);
3398 
3399 		iovarr[i].iov_base = (caddr_t)(*mpp)->b_rptr;
3400 		iovarr[i].iov_len = mpsize;
3401 		mpp = &(*mpp)->b_cont;
3402 	}
3403 	return (mp);
3404 }
3405 
3406 void
3407 rfs_rndup_mblks(mblk_t *mp, uint_t len, int buf_loaned)
3408 {
3409 	int i;
3410 	int alloc_err = 0;
3411 	mblk_t *rmp;
3412 	uint_t mpsize, remainder;
3413 
3414 	remainder = P2NPHASE(len, BYTES_PER_XDR_UNIT);
3415 
3416 	/*
3417 	 * Non copy-reduction case.  This function assumes that blocks were
3418 	 * allocated in multiples of BYTES_PER_XDR_UNIT bytes, which makes this
3419 	 * padding safe without bounds checking.
3420 	 */
3421 	if (!buf_loaned) {
3422 		/*
3423 		 * Set the size of each mblk in the chain until we've consumed
3424 		 * the specified length for all but the last one.
3425 		 */
3426 		while ((mpsize = MBLKSIZE(mp)) < len) {
3427 			ASSERT(mpsize % BYTES_PER_XDR_UNIT == 0);
3428 			mp->b_wptr += mpsize;
3429 			len -= mpsize;
3430 			mp = mp->b_cont;
3431 			ASSERT(mp != NULL);
3432 		}
3433 
3434 		ASSERT(len + remainder <= mpsize);
3435 		mp->b_wptr += len;
3436 		for (i = 0; i < remainder; i++)
3437 			*mp->b_wptr++ = '\0';
3438 		return;
3439 	}
3440 
3441 	/*
3442 	 * No remainder mblk required.
3443 	 */
3444 	if (remainder == 0)
3445 		return;
3446 
3447 	/*
3448 	 * Get to the last mblk in the chain.
3449 	 */
3450 	while (mp->b_cont != NULL)
3451 		mp = mp->b_cont;
3452 
3453 	/*
3454 	 * In case of copy-reduction mblks, the size of the mblks are fixed
3455 	 * and are of the size of the loaned buffers.  Allocate a remainder
3456 	 * mblk and chain it to the data buffers. This is sub-optimal, but not
3457 	 * expected to happen commonly.
3458 	 */
3459 	rmp = allocb_wait(remainder, BPRI_MED, STR_NOSIG, &alloc_err);
3460 	ASSERT(rmp != NULL);
3461 	ASSERT(alloc_err == 0);
3462 
3463 	for (i = 0; i < remainder; i++)
3464 		*rmp->b_wptr++ = '\0';
3465 
3466 	rmp->b_datap->db_type = M_DATA;
3467 	mp->b_cont = rmp;
3468 }
3469