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