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