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