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