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