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