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