xref: /titanic_41/usr/src/uts/common/fs/nfs/nfs_server.c (revision 60405de4d8688d96dd05157c28db3ade5c9bc234)
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 2006 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 
263 /*
264  * RDMA wait variables.
265  */
266 static kcondvar_t rdma_wait_cv;
267 static kmutex_t rdma_wait_mutex;
268 
269 /*
270  * Will be called at the point the server pool is being unregistered
271  * from the pool list. From that point onwards, the pool is waiting
272  * to be drained and as such the server state is stale and pertains
273  * to the old instantiation of the NFS server pool.
274  */
275 void
276 nfs_srv_offline(void)
277 {
278 	mutex_enter(&nfs_server_upordown_lock);
279 	if (nfs_server_upordown == NFS_SERVER_RUNNING) {
280 		nfs_server_upordown = NFS_SERVER_OFFLINE;
281 	}
282 	mutex_exit(&nfs_server_upordown_lock);
283 }
284 
285 /*
286  * Will be called at the point the server pool is being destroyed so
287  * all transports have been closed and no service threads are in
288  * existence.
289  *
290  * If we quiesce the server, we're shutting it down without destroying the
291  * server state. This allows it to warm start subsequently.
292  */
293 void
294 nfs_srv_stop_all(void)
295 {
296 	int quiesce = 0;
297 	nfs_srv_shutdown_all(quiesce);
298 }
299 
300 /*
301  * This alternative shutdown routine can be requested via nfssys()
302  */
303 void
304 nfs_srv_quiesce_all(void)
305 {
306 	int quiesce = 1;
307 	nfs_srv_shutdown_all(quiesce);
308 }
309 
310 static void
311 nfs_srv_shutdown_all(int quiesce) {
312 	mutex_enter(&nfs_server_upordown_lock);
313 	if (quiesce) {
314 		if (nfs_server_upordown == NFS_SERVER_RUNNING ||
315 			nfs_server_upordown == NFS_SERVER_OFFLINE) {
316 			nfs_server_upordown = NFS_SERVER_QUIESCED;
317 			cv_signal(&nfs_server_upordown_cv);
318 
319 			/* reset DSS state, for subsequent warm restart */
320 			rfs4_dss_numnewpaths = 0;
321 			rfs4_dss_newpaths = NULL;
322 
323 			cmn_err(CE_NOTE, "nfs_server: server is now quiesced; "
324 			    "NFSv4 state has been preserved");
325 		}
326 	} else {
327 		if (nfs_server_upordown == NFS_SERVER_OFFLINE) {
328 			nfs_server_upordown = NFS_SERVER_STOPPING;
329 			mutex_exit(&nfs_server_upordown_lock);
330 			rfs4_state_fini();
331 			rfs4_fini_drc(nfs4_drc);
332 			mutex_enter(&nfs_server_upordown_lock);
333 			nfs_server_upordown = NFS_SERVER_STOPPED;
334 			cv_signal(&nfs_server_upordown_cv);
335 		}
336 	}
337 	mutex_exit(&nfs_server_upordown_lock);
338 }
339 
340 static int
341 nfs_srv_set_sc_versions(struct file *fp, SVC_CALLOUT_TABLE **sctpp,
342 			rpcvers_t versmin, rpcvers_t versmax)
343 {
344 	struct strioctl strioc;
345 	struct T_info_ack tinfo;
346 	int		error, retval;
347 
348 	/*
349 	 * Find out what type of transport this is.
350 	 */
351 	strioc.ic_cmd = TI_GETINFO;
352 	strioc.ic_timout = -1;
353 	strioc.ic_len = sizeof (tinfo);
354 	strioc.ic_dp = (char *)&tinfo;
355 	tinfo.PRIM_type = T_INFO_REQ;
356 
357 	error = strioctl(fp->f_vnode, I_STR, (intptr_t)&strioc, 0, K_TO_K,
358 	    CRED(), &retval);
359 	if (error || retval)
360 		return (error);
361 
362 	/*
363 	 * Based on our query of the transport type...
364 	 *
365 	 * Reset the min/max versions based on the caller's request
366 	 * NOTE: This assumes that NFS_PROGRAM is first in the array!!
367 	 * And the second entry is the NFS_ACL_PROGRAM.
368 	 */
369 	switch (tinfo.SERV_type) {
370 	case T_CLTS:
371 		if (versmax == NFS_V4)
372 			return (EINVAL);
373 		__nfs_sc_clts[0].sc_versmin = versmin;
374 		__nfs_sc_clts[0].sc_versmax = versmax;
375 		__nfs_sc_clts[1].sc_versmin = versmin;
376 		__nfs_sc_clts[1].sc_versmax = versmax;
377 		*sctpp = &nfs_sct_clts;
378 		break;
379 	case T_COTS:
380 	case T_COTS_ORD:
381 		__nfs_sc_cots[0].sc_versmin = versmin;
382 		__nfs_sc_cots[0].sc_versmax = versmax;
383 		/* For the NFS_ACL program, check the max version */
384 		if (versmax > NFS_ACL_VERSMAX)
385 			versmax = NFS_ACL_VERSMAX;
386 		__nfs_sc_cots[1].sc_versmin = versmin;
387 		__nfs_sc_cots[1].sc_versmax = versmax;
388 		*sctpp = &nfs_sct_cots;
389 		break;
390 	default:
391 		error = EINVAL;
392 	}
393 
394 	return (error);
395 }
396 
397 /*
398  * NFS Server system call.
399  * Does all of the work of running a NFS server.
400  * uap->fd is the fd of an open transport provider
401  */
402 int
403 nfs_svc(struct nfs_svc_args *arg, model_t model)
404 {
405 	file_t *fp;
406 	SVCMASTERXPRT *xprt;
407 	int error;
408 	int readsize;
409 	char buf[KNC_STRSIZE];
410 	size_t len;
411 	STRUCT_HANDLE(nfs_svc_args, uap);
412 	struct netbuf addrmask;
413 	SVC_CALLOUT_TABLE *sctp = NULL;
414 
415 #ifdef lint
416 	model = model;		/* STRUCT macros don't always refer to it */
417 #endif
418 
419 	STRUCT_SET_HANDLE(uap, model, arg);
420 
421 	/* Check privileges in nfssys() */
422 
423 	if ((fp = getf(STRUCT_FGET(uap, fd))) == NULL)
424 		return (EBADF);
425 
426 	/*
427 	 * Set read buffer size to rsize
428 	 * and add room for RPC headers.
429 	 */
430 	readsize = nfs3tsize() + (RPC_MAXDATASIZE - NFS_MAXDATA);
431 	if (readsize < RPC_MAXDATASIZE)
432 		readsize = RPC_MAXDATASIZE;
433 
434 	error = copyinstr((const char *)STRUCT_FGETP(uap, netid), buf,
435 	    KNC_STRSIZE, &len);
436 	if (error) {
437 		releasef(STRUCT_FGET(uap, fd));
438 		return (error);
439 	}
440 
441 	addrmask.len = STRUCT_FGET(uap, addrmask.len);
442 	addrmask.maxlen = STRUCT_FGET(uap, addrmask.maxlen);
443 	addrmask.buf = kmem_alloc(addrmask.maxlen, KM_SLEEP);
444 	error = copyin(STRUCT_FGETP(uap, addrmask.buf), addrmask.buf,
445 	    addrmask.len);
446 	if (error) {
447 		releasef(STRUCT_FGET(uap, fd));
448 		kmem_free(addrmask.buf, addrmask.maxlen);
449 		return (error);
450 	}
451 
452 	nfs_versmin = STRUCT_FGET(uap, versmin);
453 	nfs_versmax = STRUCT_FGET(uap, versmax);
454 
455 	/* Double check the vers min/max ranges */
456 	if ((nfs_versmin > nfs_versmax) ||
457 		(nfs_versmin < NFS_VERSMIN) ||
458 		(nfs_versmax > NFS_VERSMAX)) {
459 		nfs_versmin = NFS_VERSMIN_DEFAULT;
460 		nfs_versmax = NFS_VERSMAX_DEFAULT;
461 	}
462 
463 	if (error =
464 	    nfs_srv_set_sc_versions(fp, &sctp, nfs_versmin, nfs_versmax)) {
465 		releasef(STRUCT_FGET(uap, fd));
466 		kmem_free(addrmask.buf, addrmask.maxlen);
467 		return (error);
468 	}
469 
470 	/* Initialize nfsv4 server */
471 	if (nfs_versmax == (rpcvers_t)NFS_V4)
472 		rfs4_server_start(STRUCT_FGET(uap, delegation));
473 
474 	/* Create a transport handle. */
475 	error = svc_tli_kcreate(fp, readsize, buf, &addrmask, &xprt,
476 				sctp, NULL, NFS_SVCPOOL_ID, TRUE);
477 
478 	if (error)
479 		kmem_free(addrmask.buf, addrmask.maxlen);
480 
481 	releasef(STRUCT_FGET(uap, fd));
482 
483 	/* HA-NFSv4: save the cluster nodeid */
484 	if (cluster_bootflags & CLUSTER_BOOTED)
485 		lm_global_nlmid = clconf_get_nodeid();
486 
487 	return (error);
488 }
489 
490 static void
491 rfs4_server_start(int nfs4_srv_delegation)
492 {
493 	/*
494 	 * Determine if the server has previously been "started" and
495 	 * if not, do the per instance initialization
496 	 */
497 	mutex_enter(&nfs_server_upordown_lock);
498 
499 	if (nfs_server_upordown != NFS_SERVER_RUNNING) {
500 		/* Do we need to stop and wait on the previous server? */
501 		while (nfs_server_upordown == NFS_SERVER_STOPPING ||
502 			nfs_server_upordown == NFS_SERVER_OFFLINE)
503 			cv_wait(&nfs_server_upordown_cv,
504 			    &nfs_server_upordown_lock);
505 
506 		if (nfs_server_upordown != NFS_SERVER_RUNNING) {
507 			(void) svc_pool_control(NFS_SVCPOOL_ID,
508 			    SVCPSET_UNREGISTER_PROC, (void *)&nfs_srv_offline);
509 			(void) svc_pool_control(NFS_SVCPOOL_ID,
510 			    SVCPSET_SHUTDOWN_PROC, (void *)&nfs_srv_stop_all);
511 
512 			/* is this an nfsd warm start? */
513 			if (nfs_server_upordown == NFS_SERVER_QUIESCED) {
514 				cmn_err(CE_NOTE, "nfs_server: "
515 				    "server was previously quiesced; "
516 				    "existing NFSv4 state will be re-used");
517 
518 				/*
519 				 * HA-NFSv4: this is also the signal
520 				 * that a Resource Group failover has
521 				 * occurred.
522 				 */
523 				if (cluster_bootflags & CLUSTER_BOOTED)
524 					hanfsv4_failover();
525 			} else {
526 				/* cold start */
527 				rfs4_state_init();
528 				nfs4_drc = rfs4_init_drc(nfs4_drc_max,
529 							nfs4_drc_hash,
530 							nfs4_drc_lifetime);
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 			svcerr_decode(xprt);
1486 			error++;
1487 			cmn_err(CE_NOTE,
1488 			    "Failed to decode arguments for %s version %u "
1489 			    "procedure %s client %s%s",
1490 			    pgmname, vers + min_vers, procnames[which],
1491 			    client_name(req), client_addr(req, cbuf));
1492 			goto done;
1493 		}
1494 	}
1495 
1496 	/*
1497 	 * If Version 4 use that specific dispatch function.
1498 	 */
1499 	if (req->rq_vers == 4) {
1500 		error += rfs4_dispatch(disp, req, xprt, args);
1501 		goto done;
1502 	}
1503 
1504 	dis_flags = disp->dis_flags;
1505 
1506 	/*
1507 	 * Find export information and check authentication,
1508 	 * setting the credential if everything is ok.
1509 	 */
1510 	if (disp->dis_getfh != NULL) {
1511 		void *fh;
1512 		fsid_t *fsid;
1513 		fid_t *fid, *xfid;
1514 		fhandle_t *fh2;
1515 		nfs_fh3 *fh3;
1516 
1517 		fh = (*disp->dis_getfh)(args);
1518 		switch (req->rq_vers) {
1519 		case NFS_VERSION:
1520 			fh2 = (fhandle_t *)fh;
1521 			fsid = &fh2->fh_fsid;
1522 			fid = (fid_t *)&fh2->fh_len;
1523 			xfid = (fid_t *)&fh2->fh_xlen;
1524 			break;
1525 		case NFS_V3:
1526 			fh3 = (nfs_fh3 *)fh;
1527 			fsid = &fh3->fh3_fsid;
1528 			fid = FH3TOFIDP(fh3);
1529 			xfid = FH3TOXFIDP(fh3);
1530 			break;
1531 		}
1532 
1533 		/*
1534 		 * Fix for bug 1038302 - corbin
1535 		 * There is a problem here if anonymous access is
1536 		 * disallowed.  If the current request is part of the
1537 		 * client's mount process for the requested filesystem,
1538 		 * then it will carry root (uid 0) credentials on it, and
1539 		 * will be denied by checkauth if that client does not
1540 		 * have explicit root=0 permission.  This will cause the
1541 		 * client's mount operation to fail.  As a work-around,
1542 		 * we check here to see if the request is a getattr or
1543 		 * statfs operation on the exported vnode itself, and
1544 		 * pass a flag to checkauth with the result of this test.
1545 		 *
1546 		 * The filehandle refers to the mountpoint itself if
1547 		 * the fh_data and fh_xdata portions of the filehandle
1548 		 * are equal.
1549 		 *
1550 		 * Added anon_ok argument to checkauth().
1551 		 */
1552 
1553 		if ((dis_flags & RPC_ALLOWANON) && EQFID(fid, xfid))
1554 			anon_ok = 1;
1555 		else
1556 			anon_ok = 0;
1557 
1558 		cr = xprt->xp_cred;
1559 		ASSERT(cr != NULL);
1560 #ifdef DEBUG
1561 		if (crgetref(cr) != 1) {
1562 			crfree(cr);
1563 			cr = crget();
1564 			xprt->xp_cred = cr;
1565 			cred_misses++;
1566 		} else
1567 			cred_hits++;
1568 #else
1569 		if (crgetref(cr) != 1) {
1570 			crfree(cr);
1571 			cr = crget();
1572 			xprt->xp_cred = cr;
1573 		}
1574 #endif
1575 
1576 		exi = checkexport(fsid, xfid);
1577 
1578 		if (exi != NULL) {
1579 			publicfh_ok = PUBLICFH_CHECK(disp, exi, fsid, xfid);
1580 
1581 			/*
1582 			 * Don't allow non-V4 clients access
1583 			 * to pseudo exports
1584 			 */
1585 			if (PSEUDO(exi)) {
1586 				svcerr_weakauth(xprt);
1587 				error++;
1588 				goto done;
1589 			}
1590 
1591 			authres = checkauth(exi, req, cr, anon_ok, publicfh_ok);
1592 			/*
1593 			 * authres >  0: authentication OK - proceed
1594 			 * authres == 0: authentication weak - return error
1595 			 * authres <  0: authentication timeout - drop
1596 			 */
1597 			if (authres <= 0) {
1598 				if (authres == 0) {
1599 					svcerr_weakauth(xprt);
1600 					error++;
1601 				}
1602 				goto done;
1603 			}
1604 		}
1605 	} else
1606 		cr = NULL;
1607 
1608 	if ((dis_flags & RPC_MAPRESP) && (auth_flavor != RPCSEC_GSS)) {
1609 		res = (char *)SVC_GETRES(xprt, disp->dis_ressz);
1610 		if (res == NULL)
1611 			res = (char *)&res_buf;
1612 	} else
1613 		res = (char *)&res_buf;
1614 
1615 	if (!(dis_flags & RPC_IDEMPOTENT)) {
1616 		dupstat = SVC_DUP_EXT(xprt, req, res, disp->dis_ressz, &dr,
1617 				&dupcached);
1618 
1619 		switch (dupstat) {
1620 		case DUP_ERROR:
1621 			svcerr_systemerr(xprt);
1622 			error++;
1623 			goto done;
1624 			/* NOTREACHED */
1625 		case DUP_INPROGRESS:
1626 			if (res != (char *)&res_buf)
1627 				SVC_FREERES(xprt);
1628 			error++;
1629 			goto done;
1630 			/* NOTREACHED */
1631 		case DUP_NEW:
1632 		case DUP_DROP:
1633 			curthread->t_flag |= T_DONTPEND;
1634 
1635 			(*disp->dis_proc)(args, res, exi, req, cr);
1636 
1637 			curthread->t_flag &= ~T_DONTPEND;
1638 			if (curthread->t_flag & T_WOULDBLOCK) {
1639 				curthread->t_flag &= ~T_WOULDBLOCK;
1640 				SVC_DUPDONE_EXT(xprt, dr, res, NULL,
1641 					disp->dis_ressz, DUP_DROP);
1642 				if (res != (char *)&res_buf)
1643 					SVC_FREERES(xprt);
1644 				error++;
1645 				goto done;
1646 			}
1647 			if (dis_flags & RPC_AVOIDWORK) {
1648 				SVC_DUPDONE_EXT(xprt, dr, res, NULL,
1649 					disp->dis_ressz, DUP_DROP);
1650 			} else {
1651 				SVC_DUPDONE_EXT(xprt, dr, res,
1652 					disp->dis_resfree == nullfree ? NULL :
1653 					disp->dis_resfree,
1654 					disp->dis_ressz, DUP_DONE);
1655 				dupcached = TRUE;
1656 			}
1657 			break;
1658 		case DUP_DONE:
1659 			break;
1660 		}
1661 
1662 	} else {
1663 		curthread->t_flag |= T_DONTPEND;
1664 
1665 		(*disp->dis_proc)(args, res, exi, req, cr);
1666 
1667 		curthread->t_flag &= ~T_DONTPEND;
1668 		if (curthread->t_flag & T_WOULDBLOCK) {
1669 			curthread->t_flag &= ~T_WOULDBLOCK;
1670 			if (res != (char *)&res_buf)
1671 				SVC_FREERES(xprt);
1672 			error++;
1673 			goto done;
1674 		}
1675 	}
1676 
1677 	if (auth_tooweak(req, res)) {
1678 		svcerr_weakauth(xprt);
1679 		error++;
1680 		goto done;
1681 	}
1682 
1683 	/*
1684 	 * Check to see if logging has been enabled on the server.
1685 	 * If so, then obtain the export info struct to be used for
1686 	 * the later writing of the log record.  This is done for
1687 	 * the case that a lookup is done across a non-logged public
1688 	 * file system.
1689 	 */
1690 	if (nfslog_buffer_list != NULL) {
1691 		nfslog_exi = nfslog_get_exi(exi, req, res, &nfslog_rec_id);
1692 		/*
1693 		 * Is logging enabled?
1694 		 */
1695 		logging_enabled = (nfslog_exi != NULL);
1696 
1697 		/*
1698 		 * Copy the netbuf for logging purposes, before it is
1699 		 * freed by svc_sendreply().
1700 		 */
1701 		if (logging_enabled) {
1702 			NFSLOG_COPY_NETBUF(nfslog_exi, xprt, &nb);
1703 			/*
1704 			 * If RPC_MAPRESP flag set (i.e. in V2 ops) the
1705 			 * res gets copied directly into the mbuf and
1706 			 * may be freed soon after the sendreply. So we
1707 			 * must copy it here to a safe place...
1708 			 */
1709 			if (res != (char *)&res_buf) {
1710 				bcopy(res, (char *)&res_buf, disp->dis_ressz);
1711 			}
1712 		}
1713 	}
1714 
1715 	/*
1716 	 * Serialize and send results struct
1717 	 */
1718 #ifdef DEBUG
1719 	if (rfs_no_fast_xdrres == 0 && res != (char *)&res_buf)
1720 #else
1721 	if (res != (char *)&res_buf)
1722 #endif
1723 	{
1724 		if (!svc_sendreply(xprt, disp->dis_fastxdrres, res)) {
1725 			cmn_err(CE_NOTE, "%s: bad sendreply", pgmname);
1726 			error++;
1727 		}
1728 	} else {
1729 		if (!svc_sendreply(xprt, disp->dis_xdrres, res)) {
1730 			cmn_err(CE_NOTE, "%s: bad sendreply", pgmname);
1731 			error++;
1732 		}
1733 	}
1734 
1735 	/*
1736 	 * Log if needed
1737 	 */
1738 	if (logging_enabled) {
1739 		nfslog_write_record(nfslog_exi, req, args, (char *)&res_buf,
1740 			cr, &nb, nfslog_rec_id, NFSLOG_ONE_BUFFER);
1741 		exi_rele(nfslog_exi);
1742 		kmem_free((&nb)->buf, (&nb)->len);
1743 	}
1744 
1745 	/*
1746 	 * Free results struct. With the addition of NFS V4 we can
1747 	 * have non-idempotent procedures with functions.
1748 	 */
1749 	if (disp->dis_resfree != nullfree && dupcached == FALSE) {
1750 		(*disp->dis_resfree)(res);
1751 	}
1752 
1753 done:
1754 	/*
1755 	 * Free arguments struct
1756 	 */
1757 	if (disp) {
1758 		if (!SVC_FREEARGS(xprt, disp->dis_xdrargs, args)) {
1759 			cmn_err(CE_NOTE, "%s: bad freeargs", pgmname);
1760 			error++;
1761 		}
1762 	} else {
1763 		if (!SVC_FREEARGS(xprt, (xdrproc_t)0, (caddr_t)0)) {
1764 			cmn_err(CE_NOTE, "%s: bad freeargs", pgmname);
1765 			error++;
1766 		}
1767 	}
1768 
1769 	if (exi != NULL)
1770 		exi_rele(exi);
1771 
1772 	global_svstat_ptr[req->rq_vers][NFS_BADCALLS].value.ui64 += error;
1773 
1774 	global_svstat_ptr[req->rq_vers][NFS_CALLS].value.ui64++;
1775 }
1776 
1777 static void
1778 rfs_dispatch(struct svc_req *req, SVCXPRT *xprt)
1779 {
1780 	common_dispatch(req, xprt, NFS_VERSMIN, NFS_VERSMAX,
1781 		"NFS", rfs_disptable);
1782 }
1783 
1784 static char *aclcallnames_v2[] = {
1785 	"ACL2_NULL",
1786 	"ACL2_GETACL",
1787 	"ACL2_SETACL",
1788 	"ACL2_GETATTR",
1789 	"ACL2_ACCESS",
1790 	"ACL2_GETXATTRDIR"
1791 };
1792 
1793 static struct rpcdisp acldisptab_v2[] = {
1794 	/*
1795 	 * ACL VERSION 2
1796 	 */
1797 
1798 	/* ACL2_NULL = 0 */
1799 	{rpc_null,
1800 	    xdr_void, NULL_xdrproc_t, 0,
1801 	    xdr_void, NULL_xdrproc_t, 0,
1802 	    nullfree, RPC_IDEMPOTENT,
1803 	    0},
1804 
1805 	/* ACL2_GETACL = 1 */
1806 	{acl2_getacl,
1807 	    xdr_GETACL2args, xdr_fastGETACL2args, sizeof (GETACL2args),
1808 	    xdr_GETACL2res, NULL_xdrproc_t, sizeof (GETACL2res),
1809 	    acl2_getacl_free, RPC_IDEMPOTENT,
1810 	    acl2_getacl_getfh},
1811 
1812 	/* ACL2_SETACL = 2 */
1813 	{acl2_setacl,
1814 	    xdr_SETACL2args, NULL_xdrproc_t, sizeof (SETACL2args),
1815 #ifdef _LITTLE_ENDIAN
1816 	    xdr_SETACL2res, xdr_fastSETACL2res, sizeof (SETACL2res),
1817 #else
1818 	    xdr_SETACL2res, NULL_xdrproc_t, sizeof (SETACL2res),
1819 #endif
1820 	    nullfree, RPC_MAPRESP,
1821 	    acl2_setacl_getfh},
1822 
1823 	/* ACL2_GETATTR = 3 */
1824 	{acl2_getattr,
1825 	    xdr_GETATTR2args, xdr_fastGETATTR2args, sizeof (GETATTR2args),
1826 #ifdef _LITTLE_ENDIAN
1827 	    xdr_GETATTR2res, xdr_fastGETATTR2res, sizeof (GETATTR2res),
1828 #else
1829 	    xdr_GETATTR2res, NULL_xdrproc_t, sizeof (GETATTR2res),
1830 #endif
1831 	    nullfree, RPC_IDEMPOTENT|RPC_ALLOWANON|RPC_MAPRESP,
1832 	    acl2_getattr_getfh},
1833 
1834 	/* ACL2_ACCESS = 4 */
1835 	{acl2_access,
1836 	    xdr_ACCESS2args, xdr_fastACCESS2args, sizeof (ACCESS2args),
1837 #ifdef _LITTLE_ENDIAN
1838 	    xdr_ACCESS2res, xdr_fastACCESS2res, sizeof (ACCESS2res),
1839 #else
1840 	    xdr_ACCESS2res, NULL_xdrproc_t, sizeof (ACCESS2res),
1841 #endif
1842 	    nullfree, RPC_IDEMPOTENT|RPC_MAPRESP,
1843 	    acl2_access_getfh},
1844 
1845 	/* ACL2_GETXATTRDIR = 5 */
1846 	{acl2_getxattrdir,
1847 	    xdr_GETXATTRDIR2args, NULL_xdrproc_t, sizeof (GETXATTRDIR2args),
1848 	    xdr_GETXATTRDIR2res, NULL_xdrproc_t, sizeof (GETXATTRDIR2res),
1849 	    nullfree, RPC_IDEMPOTENT,
1850 	    acl2_getxattrdir_getfh},
1851 };
1852 
1853 static char *aclcallnames_v3[] = {
1854 	"ACL3_NULL",
1855 	"ACL3_GETACL",
1856 	"ACL3_SETACL",
1857 	"ACL3_GETXATTRDIR"
1858 };
1859 
1860 static struct rpcdisp acldisptab_v3[] = {
1861 	/*
1862 	 * ACL VERSION 3
1863 	 */
1864 
1865 	/* ACL3_NULL = 0 */
1866 	{rpc_null,
1867 	    xdr_void, NULL_xdrproc_t, 0,
1868 	    xdr_void, NULL_xdrproc_t, 0,
1869 	    nullfree, RPC_IDEMPOTENT,
1870 	    0},
1871 
1872 	/* ACL3_GETACL = 1 */
1873 	{acl3_getacl,
1874 	    xdr_GETACL3args, NULL_xdrproc_t, sizeof (GETACL3args),
1875 	    xdr_GETACL3res, NULL_xdrproc_t, sizeof (GETACL3res),
1876 	    acl3_getacl_free, RPC_IDEMPOTENT,
1877 	    acl3_getacl_getfh},
1878 
1879 	/* ACL3_SETACL = 2 */
1880 	{acl3_setacl,
1881 	    xdr_SETACL3args, NULL_xdrproc_t, sizeof (SETACL3args),
1882 	    xdr_SETACL3res, NULL_xdrproc_t, sizeof (SETACL3res),
1883 	    nullfree, 0,
1884 	    acl3_setacl_getfh},
1885 
1886 	/* ACL3_GETXATTRDIR = 3 */
1887 	{acl3_getxattrdir,
1888 	    xdr_GETXATTRDIR3args, NULL_xdrproc_t, sizeof (GETXATTRDIR3args),
1889 	    xdr_GETXATTRDIR3res, NULL_xdrproc_t, sizeof (GETXATTRDIR3res),
1890 	    nullfree, RPC_IDEMPOTENT,
1891 	    acl3_getxattrdir_getfh},
1892 };
1893 
1894 static struct rpc_disptable acl_disptable[] = {
1895 	{sizeof (acldisptab_v2) / sizeof (acldisptab_v2[0]),
1896 		aclcallnames_v2,
1897 		&aclproccnt_v2_ptr, acldisptab_v2},
1898 	{sizeof (acldisptab_v3) / sizeof (acldisptab_v3[0]),
1899 		aclcallnames_v3,
1900 		&aclproccnt_v3_ptr, acldisptab_v3},
1901 };
1902 
1903 static void
1904 acl_dispatch(struct svc_req *req, SVCXPRT *xprt)
1905 {
1906 	common_dispatch(req, xprt, NFS_ACL_VERSMIN, NFS_ACL_VERSMAX,
1907 		"ACL", acl_disptable);
1908 }
1909 
1910 int
1911 checkwin(int flavor, int window, struct svc_req *req)
1912 {
1913 	struct authdes_cred *adc;
1914 
1915 	switch (flavor) {
1916 	case AUTH_DES:
1917 		adc = (struct authdes_cred *)req->rq_clntcred;
1918 		if (adc->adc_fullname.window > window)
1919 			return (0);
1920 		break;
1921 
1922 	default:
1923 		break;
1924 	}
1925 	return (1);
1926 }
1927 
1928 
1929 /*
1930  * checkauth() will check the access permission against the export
1931  * information.  Then map root uid/gid to appropriate uid/gid.
1932  *
1933  * This routine is used by NFS V3 and V2 code.
1934  */
1935 static int
1936 checkauth(struct exportinfo *exi, struct svc_req *req, cred_t *cr, int anon_ok,
1937     bool_t publicfh_ok)
1938 {
1939 	int i, nfsflavor, rpcflavor, stat, access;
1940 	struct secinfo *secp;
1941 	caddr_t principal;
1942 	char buf[INET6_ADDRSTRLEN]; /* to hold both IPv4 and IPv6 addr */
1943 	int anon_res = 0;
1944 
1945 	/*
1946 	 *	Check for privileged port number
1947 	 *	N.B.:  this assumes that we know the format of a netbuf.
1948 	 */
1949 	if (nfs_portmon) {
1950 		struct sockaddr *ca;
1951 		ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
1952 
1953 		if (ca == NULL)
1954 			return (0);
1955 
1956 		if ((ca->sa_family == AF_INET &&
1957 		    ntohs(((struct sockaddr_in *)ca)->sin_port) >=
1958 		    IPPORT_RESERVED) ||
1959 		    (ca->sa_family == AF_INET6 &&
1960 		    ntohs(((struct sockaddr_in6 *)ca)->sin6_port) >=
1961 		    IPPORT_RESERVED)) {
1962 			cmn_err(CE_NOTE,
1963 			    "nfs_server: client %s%ssent NFS request from "
1964 			    "unprivileged port",
1965 			    client_name(req), client_addr(req, buf));
1966 			return (0);
1967 		}
1968 	}
1969 
1970 	/*
1971 	 *  return 1 on success or 0 on failure
1972 	 */
1973 	stat = sec_svc_getcred(req, cr, &principal, &nfsflavor);
1974 
1975 	/*
1976 	 * A failed AUTH_UNIX svc_get_cred() implies we couldn't set
1977 	 * the credentials; below we map that to anonymous.
1978 	 */
1979 	if (!stat && nfsflavor != AUTH_UNIX) {
1980 		cmn_err(CE_NOTE,
1981 		    "nfs_server: couldn't get unix cred for %s",
1982 		    client_name(req));
1983 		return (0);
1984 	}
1985 
1986 	/*
1987 	 * Short circuit checkauth() on operations that support the
1988 	 * public filehandle, and if the request for that operation
1989 	 * is using the public filehandle. Note that we must call
1990 	 * sec_svc_getcred() first so that xp_cookie is set to the
1991 	 * right value. Normally xp_cookie is just the RPC flavor
1992 	 * of the the request, but in the case of RPCSEC_GSS it
1993 	 * could be a pseudo flavor.
1994 	 */
1995 	if (publicfh_ok)
1996 		return (1);
1997 
1998 	rpcflavor = req->rq_cred.oa_flavor;
1999 	/*
2000 	 * Check if the auth flavor is valid for this export
2001 	 */
2002 	access = nfsauth_access(exi, req);
2003 	if (access & NFSAUTH_DROP)
2004 		return (-1);	/* drop the request */
2005 
2006 	if (access & NFSAUTH_DENIED) {
2007 		/*
2008 		 * If anon_ok == 1 and we got NFSAUTH_DENIED, it was
2009 		 * probably due to the flavor not matching during the
2010 		 * the mount attempt. So map the flavor to AUTH_NONE
2011 		 * so that the credentials get mapped to the anonymous
2012 		 * user.
2013 		 */
2014 		if (anon_ok == 1)
2015 			rpcflavor = AUTH_NONE;
2016 		else
2017 			return (0);	/* deny access */
2018 
2019 	} else if (access & NFSAUTH_MAPNONE) {
2020 		/*
2021 		 * Access was granted even though the flavor mismatched
2022 		 * because AUTH_NONE was one of the exported flavors.
2023 		 */
2024 		rpcflavor = AUTH_NONE;
2025 
2026 	} else if (access & NFSAUTH_WRONGSEC) {
2027 		/*
2028 		 * NFSAUTH_WRONGSEC is used for NFSv4. Since V2/V3 already
2029 		 * negotiates the security flavor thru MOUNT protocol, the
2030 		 * only way it can get NFSAUTH_WRONGSEC here is from
2031 		 * NFS_ACL for V4. This could be for a limited view, so
2032 		 * map it to RO access. V4 lookup/readdir will take care
2033 		 * of the limited view portion.
2034 		 */
2035 		access |= NFSAUTH_RO;
2036 		access &= ~NFSAUTH_WRONGSEC;
2037 	}
2038 
2039 	switch (rpcflavor) {
2040 	case AUTH_NONE:
2041 		anon_res = crsetugid(cr, exi->exi_export.ex_anon,
2042 				exi->exi_export.ex_anon);
2043 		(void) crsetgroups(cr, 0, NULL);
2044 		break;
2045 
2046 	case AUTH_UNIX:
2047 		if (!stat || crgetuid(cr) == 0 && !(access & NFSAUTH_ROOT)) {
2048 			anon_res = crsetugid(cr, exi->exi_export.ex_anon,
2049 					exi->exi_export.ex_anon);
2050 			(void) crsetgroups(cr, 0, NULL);
2051 		}
2052 		break;
2053 
2054 	case AUTH_DES:
2055 	case RPCSEC_GSS:
2056 		/*
2057 		 *  Find the secinfo structure.  We should be able
2058 		 *  to find it by the time we reach here.
2059 		 *  nfsauth_access() has done the checking.
2060 		 */
2061 		secp = NULL;
2062 		for (i = 0; i < exi->exi_export.ex_seccnt; i++) {
2063 			if (exi->exi_export.ex_secinfo[i].s_secinfo.sc_nfsnum ==
2064 			    nfsflavor) {
2065 				secp = &exi->exi_export.ex_secinfo[i];
2066 				break;
2067 			}
2068 		}
2069 
2070 		if (!secp) {
2071 			cmn_err(CE_NOTE, "nfs_server: client %s%shad "
2072 			    "no secinfo data for flavor %d",
2073 			    client_name(req), client_addr(req, buf),
2074 			    nfsflavor);
2075 			return (0);
2076 		}
2077 
2078 		if (!checkwin(rpcflavor, secp->s_window, req)) {
2079 			cmn_err(CE_NOTE,
2080 			    "nfs_server: client %s%sused invalid "
2081 			    "auth window value",
2082 			    client_name(req), client_addr(req, buf));
2083 			return (0);
2084 		}
2085 
2086 		/*
2087 		 * Map root principals listed in the share's root= list to root,
2088 		 * and map any others principals that were mapped to root by RPC
2089 		 * to anon.
2090 		 */
2091 		if (principal && sec_svc_inrootlist(rpcflavor, principal,
2092 			secp->s_rootcnt, secp->s_rootnames)) {
2093 			if (crgetuid(cr) == 0)
2094 				return (1);
2095 
2096 			(void) crsetugid(cr, 0, 0);
2097 
2098 			/*
2099 			 * NOTE: If and when kernel-land privilege tracing is
2100 			 * added this may have to be replaced with code that
2101 			 * retrieves root's supplementary groups (e.g., using
2102 			 * kgss_get_group_info().  In the meantime principals
2103 			 * mapped to uid 0 get all privileges, so setting cr's
2104 			 * supplementary groups for them does nothing.
2105 			 */
2106 			(void) crsetgroups(cr, 0, NULL);
2107 
2108 			return (1);
2109 		}
2110 
2111 		/*
2112 		 * Not a root princ, or not in root list, map UID 0/nobody to
2113 		 * the anon ID for the share.  (RPC sets cr's UIDs and GIDs to
2114 		 * UID_NOBODY and GID_NOBODY, respectively.)
2115 		 */
2116 		if (crgetuid(cr) != 0 &&
2117 		    (crgetuid(cr) != UID_NOBODY || crgetgid(cr) != GID_NOBODY))
2118 			return (1);
2119 
2120 		anon_res = crsetugid(cr, exi->exi_export.ex_anon,
2121 			exi->exi_export.ex_anon);
2122 		(void) crsetgroups(cr, 0, NULL);
2123 		break;
2124 	default:
2125 		return (0);
2126 	} /* switch on rpcflavor */
2127 
2128 	/*
2129 	 * Even if anon access is disallowed via ex_anon == -1, we allow
2130 	 * this access if anon_ok is set.  So set creds to the default
2131 	 * "nobody" id.
2132 	 */
2133 	if (anon_res != 0) {
2134 		if (anon_ok == 0) {
2135 			cmn_err(CE_NOTE,
2136 			    "nfs_server: client %s%ssent wrong "
2137 			    "authentication for %s",
2138 			    client_name(req), client_addr(req, buf),
2139 			    exi->exi_export.ex_path ?
2140 			    exi->exi_export.ex_path : "?");
2141 			return (0);
2142 		}
2143 
2144 		if (crsetugid(cr, UID_NOBODY, GID_NOBODY) != 0)
2145 			return (0);
2146 	}
2147 
2148 	return (1);
2149 }
2150 
2151 /*
2152  * returns 0 on failure, -1 on a drop, -2 on wrong security flavor,
2153  * and 1 on success
2154  */
2155 int
2156 checkauth4(struct compound_state *cs, struct svc_req *req)
2157 {
2158 	int i, rpcflavor, access;
2159 	struct secinfo *secp;
2160 	char buf[MAXHOST + 1];
2161 	int anon_res = 0, nfsflavor;
2162 	struct exportinfo *exi;
2163 	cred_t	*cr;
2164 	caddr_t	principal;
2165 
2166 	exi = cs->exi;
2167 	cr = cs->cr;
2168 	principal = cs->principal;
2169 	nfsflavor = cs->nfsflavor;
2170 
2171 	ASSERT(cr != NULL);
2172 
2173 	rpcflavor = req->rq_cred.oa_flavor;
2174 	cs->access &= ~CS_ACCESS_LIMITED;
2175 
2176 	/*
2177 	 * Check the access right per auth flavor on the vnode of
2178 	 * this export for the given request.
2179 	 */
2180 	access = nfsauth4_access(cs->exi, cs->vp, req);
2181 
2182 	if (access & NFSAUTH_WRONGSEC)
2183 		return (-2);	/* no access for this security flavor */
2184 
2185 	if (access & NFSAUTH_DROP)
2186 		return (-1);	/* drop the request */
2187 
2188 	if (access & NFSAUTH_DENIED) {
2189 
2190 		if (exi->exi_export.ex_seccnt > 0)
2191 			return (0);	/* deny access */
2192 
2193 	} else if (access & NFSAUTH_LIMITED) {
2194 
2195 		cs->access |= CS_ACCESS_LIMITED;
2196 
2197 	} else if (access & NFSAUTH_MAPNONE) {
2198 		/*
2199 		 * Access was granted even though the flavor mismatched
2200 		 * because AUTH_NONE was one of the exported flavors.
2201 		 */
2202 		rpcflavor = AUTH_NONE;
2203 	}
2204 
2205 	/*
2206 	 * XXX probably need to redo some of it for nfsv4?
2207 	 * return 1 on success or 0 on failure
2208 	 */
2209 
2210 	switch (rpcflavor) {
2211 	case AUTH_NONE:
2212 		anon_res = crsetugid(cr, exi->exi_export.ex_anon,
2213 				exi->exi_export.ex_anon);
2214 		(void) crsetgroups(cr, 0, NULL);
2215 		break;
2216 
2217 	case AUTH_UNIX:
2218 		if (crgetuid(cr) == 0 && !(access & NFSAUTH_ROOT)) {
2219 			anon_res = crsetugid(cr, exi->exi_export.ex_anon,
2220 					exi->exi_export.ex_anon);
2221 			(void) crsetgroups(cr, 0, NULL);
2222 		}
2223 		break;
2224 
2225 	default:
2226 		/*
2227 		 *  Find the secinfo structure.  We should be able
2228 		 *  to find it by the time we reach here.
2229 		 *  nfsauth_access() has done the checking.
2230 		 */
2231 		secp = NULL;
2232 		for (i = 0; i < exi->exi_export.ex_seccnt; i++) {
2233 			if (exi->exi_export.ex_secinfo[i].s_secinfo.sc_nfsnum ==
2234 			    nfsflavor) {
2235 				secp = &exi->exi_export.ex_secinfo[i];
2236 				break;
2237 			}
2238 		}
2239 
2240 		if (!secp) {
2241 			cmn_err(CE_NOTE, "nfs_server: client %s%shad "
2242 			    "no secinfo data for flavor %d",
2243 			    client_name(req), client_addr(req, buf),
2244 			    nfsflavor);
2245 			return (0);
2246 		}
2247 
2248 		if (!checkwin(rpcflavor, secp->s_window, req)) {
2249 			cmn_err(CE_NOTE,
2250 			    "nfs_server: client %s%sused invalid "
2251 			    "auth window value",
2252 			    client_name(req), client_addr(req, buf));
2253 			return (0);
2254 		}
2255 
2256 		/*
2257 		 * Map root principals listed in the share's root= list to root,
2258 		 * and map any others principals that were mapped to root by RPC
2259 		 * to anon.
2260 		 */
2261 		if (principal && sec_svc_inrootlist(rpcflavor, principal,
2262 			secp->s_rootcnt, secp->s_rootnames)) {
2263 			if (crgetuid(cr) == 0)
2264 				return (1);
2265 
2266 			(void) crsetugid(cr, 0, 0);
2267 
2268 			/*
2269 			 * NOTE: If and when kernel-land privilege tracing is
2270 			 * added this may have to be replaced with code that
2271 			 * retrieves root's supplementary groups (e.g., using
2272 			 * kgss_get_group_info().  In the meantime principals
2273 			 * mapped to uid 0 get all privileges, so setting cr's
2274 			 * supplementary groups for them does nothing.
2275 			 */
2276 			(void) crsetgroups(cr, 0, NULL);
2277 
2278 			return (1);
2279 		}
2280 
2281 		/*
2282 		 * Not a root princ, or not in root list, map UID 0/nobody to
2283 		 * the anon ID for the share.  (RPC sets cr's UIDs and GIDs to
2284 		 * UID_NOBODY and GID_NOBODY, respectively.)
2285 		 */
2286 		if (crgetuid(cr) != 0 &&
2287 		    (crgetuid(cr) != UID_NOBODY || crgetgid(cr) != GID_NOBODY))
2288 			return (1);
2289 
2290 		anon_res = crsetugid(cr, exi->exi_export.ex_anon,
2291 			exi->exi_export.ex_anon);
2292 		(void) crsetgroups(cr, 0, NULL);
2293 		break;
2294 	} /* switch on rpcflavor */
2295 
2296 	/*
2297 	 * Even if anon access is disallowed via ex_anon == -1, we allow
2298 	 * this access if anon_ok is set.  So set creds to the default
2299 	 * "nobody" id.
2300 	 */
2301 
2302 	if (anon_res != 0) {
2303 		cmn_err(CE_NOTE,
2304 			"nfs_server: client %s%ssent wrong "
2305 			"authentication for %s",
2306 			client_name(req), client_addr(req, buf),
2307 			exi->exi_export.ex_path ?
2308 			exi->exi_export.ex_path : "?");
2309 		return (0);
2310 	}
2311 
2312 	return (1);
2313 }
2314 
2315 
2316 static char *
2317 client_name(struct svc_req *req)
2318 {
2319 	char *hostname = NULL;
2320 
2321 	/*
2322 	 * If it's a Unix cred then use the
2323 	 * hostname from the credential.
2324 	 */
2325 	if (req->rq_cred.oa_flavor == AUTH_UNIX) {
2326 		hostname = ((struct authunix_parms *)
2327 		    req->rq_clntcred)->aup_machname;
2328 	}
2329 	if (hostname == NULL)
2330 		hostname = "";
2331 
2332 	return (hostname);
2333 }
2334 
2335 static char *
2336 client_addr(struct svc_req *req, char *buf)
2337 {
2338 	struct sockaddr *ca;
2339 	uchar_t *b;
2340 	char *frontspace = "";
2341 
2342 	/*
2343 	 * We assume we are called in tandem with client_name and the
2344 	 * format string looks like "...client %s%sblah blah..."
2345 	 *
2346 	 * If it's a Unix cred then client_name returned
2347 	 * a host name, so we need insert a space between host name
2348 	 * and IP address.
2349 	 */
2350 	if (req->rq_cred.oa_flavor == AUTH_UNIX)
2351 		frontspace = " ";
2352 
2353 	/*
2354 	 * Convert the caller's IP address to a dotted string
2355 	 */
2356 	ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
2357 
2358 	if (ca->sa_family == AF_INET) {
2359 	    b = (uchar_t *)&((struct sockaddr_in *)ca)->sin_addr;
2360 	    (void) sprintf(buf, "%s(%d.%d.%d.%d) ", frontspace,
2361 		b[0] & 0xFF, b[1] & 0xFF, b[2] & 0xFF, b[3] & 0xFF);
2362 	} else if (ca->sa_family == AF_INET6) {
2363 		struct sockaddr_in6 *sin6;
2364 		sin6 = (struct sockaddr_in6 *)ca;
2365 		(void) kinet_ntop6((uchar_t *)&sin6->sin6_addr,
2366 				buf, INET6_ADDRSTRLEN);
2367 
2368 	} else {
2369 
2370 		/*
2371 		 * No IP address to print. If there was a host name
2372 		 * printed, then we print a space.
2373 		 */
2374 		(void) sprintf(buf, frontspace);
2375 	}
2376 
2377 	return (buf);
2378 }
2379 
2380 /*
2381  * NFS Server initialization routine.  This routine should only be called
2382  * once.  It performs the following tasks:
2383  *	- Call sub-initialization routines (localize access to variables)
2384  *	- Initialize all locks
2385  *	- initialize the version 3 write verifier
2386  */
2387 int
2388 nfs_srvinit(void)
2389 {
2390 	int error;
2391 
2392 	error = nfs_exportinit();
2393 	if (error != 0)
2394 		return (error);
2395 	error = rfs4_srvrinit();
2396 	if (error != 0) {
2397 		nfs_exportfini();
2398 		return (error);
2399 	}
2400 	rfs_srvrinit();
2401 	rfs3_srvrinit();
2402 	nfsauth_init();
2403 
2404 	/* Init the stuff to control start/stop */
2405 	nfs_server_upordown = NFS_SERVER_STOPPED;
2406 	mutex_init(&nfs_server_upordown_lock, NULL, MUTEX_DEFAULT, NULL);
2407 	cv_init(&nfs_server_upordown_cv, NULL, CV_DEFAULT, NULL);
2408 	mutex_init(&rdma_wait_mutex, NULL, MUTEX_DEFAULT, NULL);
2409 	cv_init(&rdma_wait_cv, NULL, CV_DEFAULT, NULL);
2410 
2411 	return (0);
2412 }
2413 
2414 /*
2415  * NFS Server finalization routine. This routine is called to cleanup the
2416  * initialization work previously performed if the NFS server module could
2417  * not be loaded correctly.
2418  */
2419 void
2420 nfs_srvfini(void)
2421 {
2422 	nfsauth_fini();
2423 	rfs3_srvrfini();
2424 	rfs_srvrfini();
2425 	nfs_exportfini();
2426 
2427 	mutex_destroy(&nfs_server_upordown_lock);
2428 	cv_destroy(&nfs_server_upordown_cv);
2429 	mutex_destroy(&rdma_wait_mutex);
2430 	cv_destroy(&rdma_wait_cv);
2431 }
2432 
2433 /*
2434  * Set up an iovec array of up to cnt pointers.
2435  */
2436 
2437 void
2438 mblk_to_iov(mblk_t *m, int cnt, struct iovec *iovp)
2439 {
2440 	while (m != NULL && cnt-- > 0) {
2441 		iovp->iov_base = (caddr_t)m->b_rptr;
2442 		iovp->iov_len = (m->b_wptr - m->b_rptr);
2443 		iovp++;
2444 		m = m->b_cont;
2445 	}
2446 }
2447 
2448 /*
2449  * Common code between NFS Version 2 and NFS Version 3 for the public
2450  * filehandle multicomponent lookups.
2451  */
2452 
2453 /*
2454  * Public filehandle evaluation of a multi-component lookup, following
2455  * symbolic links, if necessary. This may result in a vnode in another
2456  * filesystem, which is OK as long as the other filesystem is exported.
2457  *
2458  * Note that the exi will be set either to NULL or a new reference to the
2459  * exportinfo struct that corresponds to the vnode of the multi-component path.
2460  * It is the callers responsibility to release this reference.
2461  */
2462 int
2463 rfs_publicfh_mclookup(char *p, vnode_t *dvp, cred_t *cr, vnode_t **vpp,
2464     struct exportinfo **exi, struct sec_ol *sec)
2465 {
2466 	int pathflag;
2467 	vnode_t *mc_dvp = NULL;
2468 	vnode_t *realvp;
2469 	int error;
2470 
2471 	*exi = NULL;
2472 
2473 	/*
2474 	 * check if the given path is a url or native path. Since p is
2475 	 * modified by MCLpath(), it may be empty after returning from
2476 	 * there, and should be checked.
2477 	 */
2478 	if ((pathflag = MCLpath(&p)) == -1)
2479 		return (EIO);
2480 
2481 	/*
2482 	 * If pathflag is SECURITY_QUERY, turn the SEC_QUERY bit
2483 	 * on in sec->sec_flags. This bit will later serve as an
2484 	 * indication in makefh_ol() or makefh3_ol() to overload the
2485 	 * filehandle to contain the sec modes used by the server for
2486 	 * the path.
2487 	 */
2488 	if (pathflag == SECURITY_QUERY) {
2489 		if ((sec->sec_index = (uint_t)(*p)) > 0) {
2490 			sec->sec_flags |= SEC_QUERY;
2491 			p++;
2492 			if ((pathflag = MCLpath(&p)) == -1)
2493 				return (EIO);
2494 		} else {
2495 			cmn_err(CE_NOTE,
2496 			    "nfs_server: invalid security index %d, "
2497 			    "violating WebNFS SNEGO protocol.", sec->sec_index);
2498 			return (EIO);
2499 		}
2500 	}
2501 
2502 	if (p[0] == '\0') {
2503 		error = ENOENT;
2504 		goto publicfh_done;
2505 	}
2506 
2507 	error = rfs_pathname(p, &mc_dvp, vpp, dvp, cr, pathflag);
2508 
2509 	/*
2510 	 * If name resolves to "/" we get EINVAL since we asked for
2511 	 * the vnode of the directory that the file is in. Try again
2512 	 * with NULL directory vnode.
2513 	 */
2514 	if (error == EINVAL) {
2515 		error = rfs_pathname(p, NULL, vpp, dvp, cr, pathflag);
2516 		if (!error) {
2517 			ASSERT(*vpp != NULL);
2518 			if ((*vpp)->v_type == VDIR) {
2519 				VN_HOLD(*vpp);
2520 				mc_dvp = *vpp;
2521 			} else {
2522 				/*
2523 				 * This should not happen, the filesystem is
2524 				 * in an inconsistent state. Fail the lookup
2525 				 * at this point.
2526 				 */
2527 				VN_RELE(*vpp);
2528 				error = EINVAL;
2529 			}
2530 		}
2531 	}
2532 
2533 	if (error)
2534 		goto publicfh_done;
2535 
2536 	if (*vpp == NULL) {
2537 		error = ENOENT;
2538 		goto publicfh_done;
2539 	}
2540 
2541 	ASSERT(mc_dvp != NULL);
2542 	ASSERT(*vpp != NULL);
2543 
2544 	if ((*vpp)->v_type == VDIR) {
2545 		do {
2546 			/*
2547 			 * *vpp may be an AutoFS node, so we perform
2548 			 * a VOP_ACCESS() to trigger the mount of the intended
2549 			 * filesystem, so we can perform the lookup in the
2550 			 * intended filesystem.
2551 			 */
2552 			(void) VOP_ACCESS(*vpp, 0, 0, cr);
2553 
2554 			/*
2555 			 * If vnode is covered, get the
2556 			 * the topmost vnode.
2557 			 */
2558 			if (vn_mountedvfs(*vpp) != NULL) {
2559 				error = traverse(vpp);
2560 				if (error) {
2561 					VN_RELE(*vpp);
2562 					goto publicfh_done;
2563 				}
2564 			}
2565 
2566 			if (VOP_REALVP(*vpp, &realvp) == 0 && realvp != *vpp) {
2567 				/*
2568 				 * If realvp is different from *vpp
2569 				 * then release our reference on *vpp, so that
2570 				 * the export access check be performed on the
2571 				 * real filesystem instead.
2572 				 */
2573 				VN_HOLD(realvp);
2574 				VN_RELE(*vpp);
2575 				*vpp = realvp;
2576 			} else
2577 			    break;
2578 		/* LINTED */
2579 		} while (TRUE);
2580 
2581 		/*
2582 		 * Let nfs_vptexi() figure what the real parent is.
2583 		 */
2584 		VN_RELE(mc_dvp);
2585 		mc_dvp = NULL;
2586 
2587 	} else {
2588 		/*
2589 		 * If vnode is covered, get the
2590 		 * the topmost vnode.
2591 		 */
2592 		if (vn_mountedvfs(mc_dvp) != NULL) {
2593 			error = traverse(&mc_dvp);
2594 			if (error) {
2595 			    VN_RELE(*vpp);
2596 			    goto publicfh_done;
2597 			}
2598 		}
2599 
2600 		if (VOP_REALVP(mc_dvp, &realvp) == 0 && realvp != mc_dvp) {
2601 			/*
2602 			 * *vpp is a file, obtain realvp of the parent
2603 			 * directory vnode.
2604 			 */
2605 			VN_HOLD(realvp);
2606 			VN_RELE(mc_dvp);
2607 			mc_dvp = realvp;
2608 		}
2609 	}
2610 
2611 	/*
2612 	 * The pathname may take us from the public filesystem to another.
2613 	 * If that's the case then just set the exportinfo to the new export
2614 	 * and build filehandle for it. Thanks to per-access checking there's
2615 	 * no security issues with doing this. If the client is not allowed
2616 	 * access to this new export then it will get an access error when it
2617 	 * tries to use the filehandle
2618 	 */
2619 	if (error = nfs_check_vpexi(mc_dvp, *vpp, kcred, exi)) {
2620 		VN_RELE(*vpp);
2621 		goto publicfh_done;
2622 	}
2623 
2624 	/*
2625 	 * Not allowed access to pseudo exports.
2626 	 */
2627 	if (PSEUDO(*exi)) {
2628 		error = ENOENT;
2629 		VN_RELE(*vpp);
2630 		goto publicfh_done;
2631 	}
2632 
2633 	/*
2634 	 * Do a lookup for the index file. We know the index option doesn't
2635 	 * allow paths through handling in the share command, so mc_dvp will
2636 	 * be the parent for the index file vnode, if its present. Use
2637 	 * temporary pointers to preserve and reuse the vnode pointers of the
2638 	 * original directory in case there's no index file. Note that the
2639 	 * index file is a native path, and should not be interpreted by
2640 	 * the URL parser in rfs_pathname()
2641 	 */
2642 	if (((*exi)->exi_export.ex_flags & EX_INDEX) &&
2643 	    ((*vpp)->v_type == VDIR) && (pathflag == URLPATH)) {
2644 		vnode_t *tvp, *tmc_dvp;	/* temporary vnode pointers */
2645 
2646 		tmc_dvp = mc_dvp;
2647 		mc_dvp = tvp = *vpp;
2648 
2649 		error = rfs_pathname((*exi)->exi_export.ex_index, NULL, vpp,
2650 		    mc_dvp, cr, NATIVEPATH);
2651 
2652 		if (error == ENOENT) {
2653 			*vpp = tvp;
2654 			mc_dvp = tmc_dvp;
2655 			error = 0;
2656 		} else {	/* ok or error other than ENOENT */
2657 			if (tmc_dvp)
2658 				VN_RELE(tmc_dvp);
2659 			if (error)
2660 				goto publicfh_done;
2661 
2662 			/*
2663 			 * Found a valid vp for index "filename". Sanity check
2664 			 * for odd case where a directory is provided as index
2665 			 * option argument and leads us to another filesystem
2666 			 */
2667 
2668 			/* Release the reference on the old exi value */
2669 			ASSERT(*exi != NULL);
2670 			exi_rele(*exi);
2671 
2672 			if (error = nfs_check_vpexi(mc_dvp, *vpp, kcred, exi)) {
2673 				VN_RELE(*vpp);
2674 				goto publicfh_done;
2675 			}
2676 		}
2677 	}
2678 
2679 publicfh_done:
2680 	if (mc_dvp)
2681 		VN_RELE(mc_dvp);
2682 
2683 	return (error);
2684 }
2685 
2686 /*
2687  * Evaluate a multi-component path
2688  */
2689 int
2690 rfs_pathname(
2691 	char *path,			/* pathname to evaluate */
2692 	vnode_t **dirvpp,		/* ret for ptr to parent dir vnode */
2693 	vnode_t **compvpp,		/* ret for ptr to component vnode */
2694 	vnode_t *startdvp,		/* starting vnode */
2695 	cred_t *cr,			/* user's credential */
2696 	int pathflag)			/* flag to identify path, e.g. URL */
2697 {
2698 	char namebuf[TYPICALMAXPATHLEN];
2699 	struct pathname pn;
2700 	int error;
2701 
2702 	/*
2703 	 * If pathname starts with '/', then set startdvp to root.
2704 	 */
2705 	if (*path == '/') {
2706 		while (*path == '/')
2707 			path++;
2708 
2709 		startdvp = rootdir;
2710 	}
2711 
2712 	error = pn_get_buf(path, UIO_SYSSPACE, &pn, namebuf, sizeof (namebuf));
2713 	if (error == 0) {
2714 		/*
2715 		 * Call the URL parser for URL paths to modify the original
2716 		 * string to handle any '%' encoded characters that exist.
2717 		 * Done here to avoid an extra bcopy in the lookup.
2718 		 * We need to be careful about pathlen's. We know that
2719 		 * rfs_pathname() is called with a non-empty path. However,
2720 		 * it could be emptied due to the path simply being all /'s,
2721 		 * which is valid to proceed with the lookup, or due to the
2722 		 * URL parser finding an encoded null character at the
2723 		 * beginning of path which should not proceed with the lookup.
2724 		 */
2725 		if (pn.pn_pathlen != 0 && pathflag == URLPATH) {
2726 			URLparse(pn.pn_path);
2727 			if ((pn.pn_pathlen = strlen(pn.pn_path)) == 0)
2728 				return (ENOENT);
2729 		}
2730 		VN_HOLD(startdvp);
2731 		error = lookuppnvp(&pn, NULL, NO_FOLLOW, dirvpp, compvpp,
2732 		    rootdir, startdvp, cr);
2733 	}
2734 	if (error == ENAMETOOLONG) {
2735 		/*
2736 		 * This thread used a pathname > TYPICALMAXPATHLEN bytes long.
2737 		 */
2738 		if (error = pn_get(path, UIO_SYSSPACE, &pn))
2739 			return (error);
2740 		if (pn.pn_pathlen != 0 && pathflag == URLPATH) {
2741 			URLparse(pn.pn_path);
2742 			if ((pn.pn_pathlen = strlen(pn.pn_path)) == 0) {
2743 				pn_free(&pn);
2744 				return (ENOENT);
2745 			}
2746 		}
2747 		VN_HOLD(startdvp);
2748 		error = lookuppnvp(&pn, NULL, NO_FOLLOW, dirvpp, compvpp,
2749 		    rootdir, startdvp, cr);
2750 		pn_free(&pn);
2751 	}
2752 
2753 	return (error);
2754 }
2755 
2756 /*
2757  * Adapt the multicomponent lookup path depending on the pathtype
2758  */
2759 static int
2760 MCLpath(char **path)
2761 {
2762 	unsigned char c = (unsigned char)**path;
2763 
2764 	/*
2765 	 * If the MCL path is between 0x20 and 0x7E (graphic printable
2766 	 * character of the US-ASCII coded character set), its a URL path,
2767 	 * per RFC 1738.
2768 	 */
2769 	if (c >= 0x20 && c <= 0x7E)
2770 		return (URLPATH);
2771 
2772 	/*
2773 	 * If the first octet of the MCL path is not an ASCII character
2774 	 * then it must be interpreted as a tag value that describes the
2775 	 * format of the remaining octets of the MCL path.
2776 	 *
2777 	 * If the first octet of the MCL path is 0x81 it is a query
2778 	 * for the security info.
2779 	 */
2780 	switch (c) {
2781 	case 0x80:	/* native path, i.e. MCL via mount protocol */
2782 		(*path)++;
2783 		return (NATIVEPATH);
2784 	case 0x81:	/* security query */
2785 		(*path)++;
2786 		return (SECURITY_QUERY);
2787 	default:
2788 		return (-1);
2789 	}
2790 }
2791 
2792 #define	fromhex(c)  ((c >= '0' && c <= '9') ? (c - '0') : \
2793 			((c >= 'A' && c <= 'F') ? (c - 'A' + 10) :\
2794 			((c >= 'a' && c <= 'f') ? (c - 'a' + 10) : 0)))
2795 
2796 /*
2797  * The implementation of URLparse gaurantees that the final string will
2798  * fit in the original one. Replaces '%' occurrences followed by 2 characters
2799  * with its corresponding hexadecimal character.
2800  */
2801 static void
2802 URLparse(char *str)
2803 {
2804 	char *p, *q;
2805 
2806 	p = q = str;
2807 	while (*p) {
2808 		*q = *p;
2809 		if (*p++ == '%') {
2810 			if (*p) {
2811 				*q = fromhex(*p) * 16;
2812 				p++;
2813 				if (*p) {
2814 					*q += fromhex(*p);
2815 					p++;
2816 				}
2817 			}
2818 		}
2819 		q++;
2820 	}
2821 	*q = '\0';
2822 }
2823 
2824 
2825 /*
2826  * Get the export information for the lookup vnode, and verify its
2827  * useable.
2828  */
2829 int
2830 nfs_check_vpexi(vnode_t *mc_dvp, vnode_t *vp, cred_t *cr,
2831     struct exportinfo **exi)
2832 {
2833 	int walk;
2834 	int error = 0;
2835 
2836 	*exi = nfs_vptoexi(mc_dvp, vp, cr, &walk, NULL, FALSE);
2837 	if (*exi == NULL)
2838 		error = EACCES;
2839 	else {
2840 		/*
2841 		 * If nosub is set for this export then
2842 		 * a lookup relative to the public fh
2843 		 * must not terminate below the
2844 		 * exported directory.
2845 		 */
2846 		if ((*exi)->exi_export.ex_flags & EX_NOSUB && walk > 0)
2847 			error = EACCES;
2848 	}
2849 
2850 	return (error);
2851 }
2852 
2853 /*
2854  * Do the main work of handling HA-NFSv4 Resource Group failover on
2855  * Sun Cluster.
2856  * We need to detect whether any RG admin paths have been added or removed,
2857  * and adjust resources accordingly.
2858  * Currently we're using a very inefficient algorithm, ~ 2 * O(n**2). In
2859  * order to scale, the list and array of paths need to be held in more
2860  * suitable data structures.
2861  */
2862 static void
2863 hanfsv4_failover(void)
2864 {
2865 	int i, start_grace, numadded_paths = 0;
2866 	char **added_paths = NULL;
2867 	rfs4_dss_path_t *dss_path;
2868 
2869 	/*
2870 	 * Note: currently, rfs4_dss_pathlist cannot be NULL, since
2871 	 * it will always include an entry for NFS4_DSS_VAR_DIR. If we
2872 	 * make the latter dynamically specified too, the following will
2873 	 * need to be adjusted.
2874 	 */
2875 
2876 	/*
2877 	 * First, look for removed paths: RGs that have been failed-over
2878 	 * away from this node.
2879 	 * Walk the "currently-serving" rfs4_dss_pathlist and, for each
2880 	 * path, check if it is on the "passed-in" rfs4_dss_newpaths array
2881 	 * from nfsd. If not, that RG path has been removed.
2882 	 *
2883 	 * Note that nfsd has sorted rfs4_dss_newpaths for us, and removed
2884 	 * any duplicates.
2885 	 */
2886 	dss_path = rfs4_dss_pathlist;
2887 	do {
2888 		int found = 0;
2889 		char *path = dss_path->path;
2890 
2891 		/* used only for non-HA so may not be removed */
2892 		if (strcmp(path, NFS4_DSS_VAR_DIR) == 0) {
2893 			dss_path = dss_path->next;
2894 			continue;
2895 		}
2896 
2897 		for (i = 0; i < rfs4_dss_numnewpaths; i++) {
2898 			int cmpret;
2899 			char *newpath = rfs4_dss_newpaths[i];
2900 
2901 			/*
2902 			 * Since nfsd has sorted rfs4_dss_newpaths for us,
2903 			 * once the return from strcmp is negative we know
2904 			 * we've passed the point where "path" should be,
2905 			 * and can stop searching: "path" has been removed.
2906 			 */
2907 			cmpret = strcmp(path, newpath);
2908 			if (cmpret < 0)
2909 				break;
2910 			if (cmpret == 0) {
2911 				found = 1;
2912 				break;
2913 			}
2914 		}
2915 
2916 		if (found == 0) {
2917 			unsigned index = dss_path->index;
2918 			rfs4_servinst_t *sip = dss_path->sip;
2919 			rfs4_dss_path_t *path_next = dss_path->next;
2920 
2921 			/*
2922 			 * This path has been removed.
2923 			 * We must clear out the servinst reference to
2924 			 * it, since it's now owned by another
2925 			 * node: we should not attempt to touch it.
2926 			 */
2927 			ASSERT(dss_path == sip->dss_paths[index]);
2928 			sip->dss_paths[index] = NULL;
2929 
2930 			/* remove from "currently-serving" list, and destroy */
2931 			remque(dss_path);
2932 			/* allow for NUL */
2933 			kmem_free(dss_path->path, strlen(dss_path->path) + 1);
2934 			kmem_free(dss_path, sizeof (rfs4_dss_path_t));
2935 
2936 			dss_path = path_next;
2937 		} else {
2938 			/* path was found; not removed */
2939 			dss_path = dss_path->next;
2940 		}
2941 	} while (dss_path != rfs4_dss_pathlist);
2942 
2943 	/*
2944 	 * Now, look for added paths: RGs that have been failed-over
2945 	 * to this node.
2946 	 * Walk the "passed-in" rfs4_dss_newpaths array from nfsd and,
2947 	 * for each path, check if it is on the "currently-serving"
2948 	 * rfs4_dss_pathlist. If not, that RG path has been added.
2949 	 *
2950 	 * Note: we don't do duplicate detection here; nfsd does that for us.
2951 	 *
2952 	 * Note: numadded_paths <= rfs4_dss_numnewpaths, which gives us
2953 	 * an upper bound for the size needed for added_paths[numadded_paths].
2954 	 */
2955 
2956 	/* probably more space than we need, but guaranteed to be enough */
2957 	if (rfs4_dss_numnewpaths > 0) {
2958 		size_t sz = rfs4_dss_numnewpaths * sizeof (char *);
2959 		added_paths = kmem_zalloc(sz, KM_SLEEP);
2960 	}
2961 
2962 	/* walk the "passed-in" rfs4_dss_newpaths array from nfsd */
2963 	for (i = 0; i < rfs4_dss_numnewpaths; i++) {
2964 		int found = 0;
2965 		char *newpath = rfs4_dss_newpaths[i];
2966 
2967 		dss_path = rfs4_dss_pathlist;
2968 		do {
2969 			char *path = dss_path->path;
2970 
2971 			/* used only for non-HA */
2972 			if (strcmp(path, NFS4_DSS_VAR_DIR) == 0) {
2973 				dss_path = dss_path->next;
2974 				continue;
2975 			}
2976 
2977 			if (strncmp(path, newpath, strlen(path)) == 0) {
2978 				found = 1;
2979 				break;
2980 			}
2981 
2982 			dss_path = dss_path->next;
2983 		} while (dss_path != rfs4_dss_pathlist);
2984 
2985 		if (found == 0) {
2986 			added_paths[numadded_paths] = newpath;
2987 			numadded_paths++;
2988 		}
2989 	}
2990 
2991 	/* did we find any added paths? */
2992 	if (numadded_paths > 0) {
2993 		/* create a new server instance, and start its grace period */
2994 		start_grace = 1;
2995 		rfs4_servinst_create(start_grace, numadded_paths, added_paths);
2996 
2997 		/* read in the stable storage state from these paths */
2998 		rfs4_dss_readstate(numadded_paths, added_paths);
2999 
3000 		/*
3001 		 * Multiple failovers during a grace period will cause
3002 		 * clients of the same resource group to be partitioned
3003 		 * into different server instances, with different
3004 		 * grace periods.  Since clients of the same resource
3005 		 * group must be subject to the same grace period,
3006 		 * we need to reset all currently active grace periods.
3007 		 */
3008 		rfs4_grace_reset_all();
3009 	}
3010 
3011 	if (rfs4_dss_numnewpaths > 0)
3012 		kmem_free(added_paths, rfs4_dss_numnewpaths * sizeof (char *));
3013 }
3014