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