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