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