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 /*
  23  * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
  24  * Copyright (c) 2015 by Delphix. All rights reserved.
  25  * Copyright 2018 Nexenta Systems, Inc. All rights reserved.
  26  */
  27 
  28 #include <sys/param.h>
  29 #include <sys/errno.h>
  30 #include <sys/vfs.h>
  31 #include <sys/vnode.h>
  32 #include <sys/cred.h>
  33 #include <sys/cmn_err.h>
  34 #include <sys/systm.h>
  35 #include <sys/kmem.h>
  36 #include <sys/pathname.h>
  37 #include <sys/utsname.h>
  38 #include <sys/debug.h>
  39 #include <sys/door.h>
  40 #include <sys/sdt.h>
  41 #include <sys/thread.h>
  42 #include <sys/avl.h>
  43 
  44 #include <rpc/types.h>
  45 #include <rpc/auth.h>
  46 #include <rpc/clnt.h>
  47 
  48 #include <nfs/nfs.h>
  49 #include <nfs/export.h>
  50 #include <nfs/nfs_clnt.h>
  51 #include <nfs/auth.h>
  52 
  53 static struct kmem_cache *exi_cache_handle;
  54 static void exi_cache_reclaim(void *);
  55 static void exi_cache_reclaim_zone(nfs_globals_t *);
  56 static void exi_cache_trim(struct exportinfo *exi);
  57 
  58 extern pri_t minclsyspri;
  59 
  60 /* NFS auth cache statistics */
  61 volatile uint_t nfsauth_cache_hit;
  62 volatile uint_t nfsauth_cache_miss;
  63 volatile uint_t nfsauth_cache_refresh;
  64 volatile uint_t nfsauth_cache_reclaim;
  65 volatile uint_t exi_cache_auth_reclaim_failed;
  66 volatile uint_t exi_cache_clnt_reclaim_failed;
  67 
  68 /*
  69  * The lifetime of an auth cache entry:
  70  * ------------------------------------
  71  *
  72  * An auth cache entry is created with both the auth_time
  73  * and auth_freshness times set to the current time.
  74  *
  75  * Upon every client access which results in a hit, the
  76  * auth_time will be updated.
  77  *
  78  * If a client access determines that the auth_freshness
  79  * indicates that the entry is STALE, then it will be
  80  * refreshed. Note that this will explicitly reset
  81  * auth_time.
  82  *
  83  * When the REFRESH successfully occurs, then the
  84  * auth_freshness is updated.
  85  *
  86  * There are two ways for an entry to leave the cache:
  87  *
  88  * 1) Purged by an action on the export (remove or changed)
  89  * 2) Memory backpressure from the kernel (check against NFSAUTH_CACHE_TRIM)
  90  *
  91  * For 2) we check the timeout value against auth_time.
  92  */
  93 
  94 /*
  95  * Number of seconds until we mark for refresh an auth cache entry.
  96  */
  97 #define NFSAUTH_CACHE_REFRESH 600
  98 
  99 /*
 100  * Number of idle seconds until we yield to backpressure
 101  * to trim a cache entry.
 102  */
 103 #define NFSAUTH_CACHE_TRIM 3600
 104 
 105 /*
 106  * While we could encapuslate the exi_list inside the
 107  * exi structure, we can't do that for the auth_list.
 108  * So, to keep things looking clean, we keep them both
 109  * in these external lists.
 110  */
 111 typedef struct refreshq_exi_node {
 112         struct exportinfo       *ren_exi;
 113         list_t                  ren_authlist;
 114         list_node_t             ren_node;
 115 } refreshq_exi_node_t;
 116 
 117 typedef struct refreshq_auth_node {
 118         struct auth_cache       *ran_auth;
 119         char                    *ran_netid;
 120         list_node_t             ran_node;
 121 } refreshq_auth_node_t;
 122 
 123 /*
 124  * Used to manipulate things on the refreshq_queue.  Note that the refresh
 125  * thread will effectively pop a node off of the queue, at which point it
 126  * will no longer need to hold the mutex.
 127  */
 128 static kmutex_t refreshq_lock;
 129 static list_t refreshq_queue;
 130 static kcondvar_t refreshq_cv;
 131 
 132 /*
 133  * If there is ever a problem with loading the module, then nfsauth_fini()
 134  * needs to be called to remove state.  In that event, since the refreshq
 135  * thread has been started, they need to work together to get rid of state.
 136  */
 137 typedef enum nfsauth_refreshq_thread_state {
 138         REFRESHQ_THREAD_RUNNING,
 139         REFRESHQ_THREAD_FINI_REQ,
 140         REFRESHQ_THREAD_HALTED,
 141         REFRESHQ_THREAD_NEED_CREATE
 142 } nfsauth_refreshq_thread_state_t;
 143 
 144 typedef struct nfsauth_globals {
 145         kmutex_t        mountd_lock;
 146         door_handle_t   mountd_dh;
 147 
 148         /*
 149          * Used to manipulate things on the refreshq_queue.  Note that the
 150          * refresh thread will effectively pop a node off of the queue,
 151          * at which point it will no longer need to hold the mutex.
 152          */
 153         kmutex_t        refreshq_lock;
 154         list_t          refreshq_queue;
 155         kcondvar_t      refreshq_cv;
 156 
 157         /*
 158          * A list_t would be overkill.  These are auth_cache entries which are
 159          * no longer linked to an exi.  It should be the case that all of their
 160          * states are NFS_AUTH_INVALID, i.e., the only way to be put on this
 161          * list is iff their state indicated that they had been placed on the
 162          * refreshq_queue.
 163          *
 164          * Note that while there is no link from the exi or back to the exi,
 165          * the exi can not go away until these entries are harvested.
 166          */
 167         struct auth_cache               *refreshq_dead_entries;
 168         nfsauth_refreshq_thread_state_t refreshq_thread_state;
 169 
 170 } nfsauth_globals_t;
 171 
 172 static void nfsauth_free_node(struct auth_cache *);
 173 static void nfsauth_refresh_thread(nfsauth_globals_t *);
 174 
 175 static int nfsauth_cache_compar(const void *, const void *);
 176 
 177 static nfsauth_globals_t *
 178 nfsauth_get_zg(void)
 179 {
 180         nfs_globals_t *ng = zone_getspecific(nfssrv_zone_key, curzone);
 181         nfsauth_globals_t *nag = ng->nfs_auth;
 182         ASSERT(nag != NULL);
 183         return (nag);
 184 }
 185 
 186 void
 187 mountd_args(uint_t did)
 188 {
 189         nfsauth_globals_t *nag;
 190 
 191         nag = nfsauth_get_zg();
 192         mutex_enter(&nag->mountd_lock);
 193         if (nag->mountd_dh != NULL)
 194                 door_ki_rele(nag->mountd_dh);
 195         nag->mountd_dh = door_ki_lookup(did);
 196         mutex_exit(&nag->mountd_lock);
 197 }
 198 
 199 void
 200 nfsauth_init(void)
 201 {
 202         exi_cache_handle = kmem_cache_create("exi_cache_handle",
 203             sizeof (struct auth_cache), 0, NULL, NULL,
 204             exi_cache_reclaim, NULL, NULL, 0);
 205 }
 206 
 207 void
 208 nfsauth_fini(void)
 209 {
 210         kmem_cache_destroy(exi_cache_handle);
 211 }
 212 
 213 void
 214 nfsauth_zone_init(nfs_globals_t *ng)
 215 {
 216         nfsauth_globals_t *nag;
 217 
 218         nag = kmem_zalloc(sizeof (*nag), KM_SLEEP);
 219 
 220         /*
 221          * mountd can be restarted by smf(5).  We need to make sure
 222          * the updated door handle will safely make it to mountd_dh.
 223          */
 224         mutex_init(&nag->mountd_lock, NULL, MUTEX_DEFAULT, NULL);
 225         mutex_init(&nag->refreshq_lock, NULL, MUTEX_DEFAULT, NULL);
 226         list_create(&nag->refreshq_queue, sizeof (refreshq_exi_node_t),
 227             offsetof(refreshq_exi_node_t, ren_node));
 228         cv_init(&nag->refreshq_cv, NULL, CV_DEFAULT, NULL);
 229         nag->refreshq_thread_state = REFRESHQ_THREAD_NEED_CREATE;
 230 
 231         ng->nfs_auth = nag;
 232 }
 233 
 234 void
 235 nfsauth_zone_shutdown(nfs_globals_t *ng)
 236 {
 237         refreshq_exi_node_t     *ren;
 238         nfsauth_globals_t       *nag = ng->nfs_auth;
 239 
 240         /* Prevent the nfsauth_refresh_thread from getting new work */
 241         mutex_enter(&nag->refreshq_lock);
 242         if (nag->refreshq_thread_state == REFRESHQ_THREAD_RUNNING) {
 243                 nag->refreshq_thread_state = REFRESHQ_THREAD_FINI_REQ;
 244                 cv_broadcast(&nag->refreshq_cv);
 245 
 246                 /* Wait for nfsauth_refresh_thread() to exit */
 247                 while (nag->refreshq_thread_state != REFRESHQ_THREAD_HALTED)
 248                         cv_wait(&nag->refreshq_cv, &nag->refreshq_lock);
 249         }
 250         mutex_exit(&nag->refreshq_lock);
 251 
 252         /*
 253          * Walk the exi_list and in turn, walk the auth_lists and free all
 254          * lists.  In addition, free INVALID auth_cache entries.
 255          */
 256         while ((ren = list_remove_head(&nag->refreshq_queue))) {
 257                 refreshq_auth_node_t *ran;
 258 
 259                 while ((ran = list_remove_head(&ren->ren_authlist)) != NULL) {
 260                         struct auth_cache *p = ran->ran_auth;
 261                         if (p->auth_state == NFS_AUTH_INVALID)
 262                                 nfsauth_free_node(p);
 263                         strfree(ran->ran_netid);
 264                         kmem_free(ran, sizeof (*ran));
 265                 }
 266 
 267                 list_destroy(&ren->ren_authlist);
 268                 exi_rele(ren->ren_exi);
 269                 kmem_free(ren, sizeof (*ren));
 270         }
 271 }
 272 
 273 void
 274 nfsauth_zone_fini(nfs_globals_t *ng)
 275 {
 276         nfsauth_globals_t *nag = ng->nfs_auth;
 277 
 278         ng->nfs_auth = NULL;
 279 
 280         list_destroy(&nag->refreshq_queue);
 281         cv_destroy(&nag->refreshq_cv);
 282         mutex_destroy(&nag->refreshq_lock);
 283         mutex_destroy(&nag->mountd_lock);
 284         kmem_free(nag, sizeof (*nag));
 285 }
 286 
 287 /*
 288  * Convert the address in a netbuf to
 289  * a hash index for the auth_cache table.
 290  */
 291 static int
 292 hash(struct netbuf *a)
 293 {
 294         int i, h = 0;
 295 
 296         for (i = 0; i < a->len; i++)
 297                 h ^= a->buf[i];
 298 
 299         return (h & (AUTH_TABLESIZE - 1));
 300 }
 301 
 302 /*
 303  * Mask out the components of an
 304  * address that do not identify
 305  * a host. For socket addresses the
 306  * masking gets rid of the port number.
 307  */
 308 static void
 309 addrmask(struct netbuf *addr, struct netbuf *mask)
 310 {
 311         int i;
 312 
 313         for (i = 0; i < addr->len; i++)
 314                 addr->buf[i] &= mask->buf[i];
 315 }
 316 
 317 /*
 318  * nfsauth4_access is used for NFS V4 auth checking. Besides doing
 319  * the common nfsauth_access(), it will check if the client can
 320  * have a limited access to this vnode even if the security flavor
 321  * used does not meet the policy.
 322  */
 323 int
 324 nfsauth4_access(struct exportinfo *exi, vnode_t *vp, struct svc_req *req,
 325     cred_t *cr, uid_t *uid, gid_t *gid, uint_t *ngids, gid_t **gids)
 326 {
 327         int access;
 328 
 329         access = nfsauth_access(exi, req, cr, uid, gid, ngids, gids);
 330 
 331         /*
 332          * There are cases that the server needs to allow the client
 333          * to have a limited view.
 334          *
 335          * e.g.
 336          * /export is shared as "sec=sys,rw=dfs-test-4,sec=krb5,rw"
 337          * /export/home is shared as "sec=sys,rw"
 338          *
 339          * When the client mounts /export with sec=sys, the client
 340          * would get a limited view with RO access on /export to see
 341          * "home" only because the client is allowed to access
 342          * /export/home with auth_sys.
 343          */
 344         if (access & NFSAUTH_DENIED || access & NFSAUTH_WRONGSEC) {
 345                 /*
 346                  * Allow ro permission with LIMITED view if there is a
 347                  * sub-dir exported under vp.
 348                  */
 349                 if (has_visible(exi, vp))
 350                         return (NFSAUTH_LIMITED);
 351         }
 352 
 353         return (access);
 354 }
 355 
 356 static void
 357 sys_log(const char *msg)
 358 {
 359         static time_t   tstamp = 0;
 360         time_t          now;
 361 
 362         /*
 363          * msg is shown (at most) once per minute
 364          */
 365         now = gethrestime_sec();
 366         if ((tstamp + 60) < now) {
 367                 tstamp = now;
 368                 cmn_err(CE_WARN, msg);
 369         }
 370 }
 371 
 372 /*
 373  * Callup to the mountd to get access information in the kernel.
 374  */
 375 static bool_t
 376 nfsauth_retrieve(nfsauth_globals_t *nag, struct exportinfo *exi,
 377     char *req_netid, int flavor, struct netbuf *addr, int *access,
 378     cred_t *clnt_cred, uid_t *srv_uid, gid_t *srv_gid, uint_t *srv_gids_cnt,
 379     gid_t **srv_gids)
 380 {
 381         varg_t                    varg = {0};
 382         nfsauth_res_t             res = {0};
 383         XDR                       xdrs;
 384         size_t                    absz;
 385         caddr_t                   abuf;
 386         int                       last = 0;
 387         door_arg_t                da;
 388         door_info_t               di;
 389         door_handle_t             dh;
 390         uint_t                    ntries = 0;
 391 
 392         /*
 393          * No entry in the cache for this client/flavor
 394          * so we need to call the nfsauth service in the
 395          * mount daemon.
 396          */
 397 
 398         varg.vers = V_PROTO;
 399         varg.arg_u.arg.cmd = NFSAUTH_ACCESS;
 400         varg.arg_u.arg.areq.req_client.n_len = addr->len;
 401         varg.arg_u.arg.areq.req_client.n_bytes = addr->buf;
 402         varg.arg_u.arg.areq.req_netid = req_netid;
 403         varg.arg_u.arg.areq.req_path = exi->exi_export.ex_path;
 404         varg.arg_u.arg.areq.req_flavor = flavor;
 405         varg.arg_u.arg.areq.req_clnt_uid = crgetuid(clnt_cred);
 406         varg.arg_u.arg.areq.req_clnt_gid = crgetgid(clnt_cred);
 407         varg.arg_u.arg.areq.req_clnt_gids.len = crgetngroups(clnt_cred);
 408         varg.arg_u.arg.areq.req_clnt_gids.val = (gid_t *)crgetgroups(clnt_cred);
 409 
 410         DTRACE_PROBE1(nfsserv__func__nfsauth__varg, varg_t *, &varg);
 411 
 412         /*
 413          * Setup the XDR stream for encoding the arguments. Notice that
 414          * in addition to the args having variable fields (req_netid and
 415          * req_path), the argument data structure is itself versioned,
 416          * so we need to make sure we can size the arguments buffer
 417          * appropriately to encode all the args. If we can't get sizing
 418          * info _or_ properly encode the arguments, there's really no
 419          * point in continuting, so we fail the request.
 420          */
 421         if ((absz = xdr_sizeof(xdr_varg, &varg)) == 0) {
 422                 *access = NFSAUTH_DENIED;
 423                 return (FALSE);
 424         }
 425 
 426         abuf = (caddr_t)kmem_alloc(absz, KM_SLEEP);
 427         xdrmem_create(&xdrs, abuf, absz, XDR_ENCODE);
 428         if (!xdr_varg(&xdrs, &varg)) {
 429                 XDR_DESTROY(&xdrs);
 430                 goto fail;
 431         }
 432         XDR_DESTROY(&xdrs);
 433 
 434         /*
 435          * Prepare the door arguments
 436          *
 437          * We don't know the size of the message the daemon
 438          * will pass back to us.  By setting rbuf to NULL,
 439          * we force the door code to allocate a buf of the
 440          * appropriate size.  We must set rsize > 0, however,
 441          * else the door code acts as if no response was
 442          * expected and doesn't pass the data to us.
 443          */
 444         da.data_ptr = (char *)abuf;
 445         da.data_size = absz;
 446         da.desc_ptr = NULL;
 447         da.desc_num = 0;
 448         da.rbuf = NULL;
 449         da.rsize = 1;
 450 
 451 retry:
 452         mutex_enter(&nag->mountd_lock);
 453         dh = nag->mountd_dh;
 454         if (dh != NULL)
 455                 door_ki_hold(dh);
 456         mutex_exit(&nag->mountd_lock);
 457 
 458         if (dh == NULL) {
 459                 /*
 460                  * The rendezvous point has not been established yet!
 461                  * This could mean that either mountd(1m) has not yet
 462                  * been started or that _this_ routine nuked the door
 463                  * handle after receiving an EINTR for a REVOKED door.
 464                  *
 465                  * Returning NFSAUTH_DROP will cause the NFS client
 466                  * to retransmit the request, so let's try to be more
 467                  * rescillient and attempt for ntries before we bail.
 468                  */
 469                 if (++ntries % NFSAUTH_DR_TRYCNT) {
 470                         delay(hz);
 471                         goto retry;
 472                 }
 473 
 474                 kmem_free(abuf, absz);
 475 
 476                 sys_log("nfsauth: mountd has not established door");
 477                 *access = NFSAUTH_DROP;
 478                 return (FALSE);
 479         }
 480 
 481         ntries = 0;
 482 
 483         /*
 484          * Now that we've got what we need, place the call.
 485          */
 486         switch (door_ki_upcall_limited(dh, &da, NULL, SIZE_MAX, 0)) {
 487         case 0:                         /* Success */
 488                 door_ki_rele(dh);
 489 
 490                 if (da.data_ptr == NULL && da.data_size == 0) {
 491                         /*
 492                          * The door_return that contained the data
 493                          * failed! We're here because of the 2nd
 494                          * door_return (w/o data) such that we can
 495                          * get control of the thread (and exit
 496                          * gracefully).
 497                          */
 498                         DTRACE_PROBE1(nfsserv__func__nfsauth__door__nil,
 499                             door_arg_t *, &da);
 500                         goto fail;
 501                 }
 502 
 503                 break;
 504 
 505         case EAGAIN:
 506                 /*
 507                  * Server out of resources; back off for a bit
 508                  */
 509                 door_ki_rele(dh);
 510                 delay(hz);
 511                 goto retry;
 512                 /* NOTREACHED */
 513 
 514         case EINTR:
 515                 if (!door_ki_info(dh, &di)) {
 516                         door_ki_rele(dh);
 517 
 518                         if (di.di_attributes & DOOR_REVOKED) {
 519                                 /*
 520                                  * The server barfed and revoked
 521                                  * the (existing) door on us; we
 522                                  * want to wait to give smf(5) a
 523                                  * chance to restart mountd(1m)
 524                                  * and establish a new door handle.
 525                                  */
 526                                 mutex_enter(&nag->mountd_lock);
 527                                 if (dh == nag->mountd_dh) {
 528                                         door_ki_rele(nag->mountd_dh);
 529                                         nag->mountd_dh = NULL;
 530                                 }
 531                                 mutex_exit(&nag->mountd_lock);
 532                                 delay(hz);
 533                                 goto retry;
 534                         }
 535                         /*
 536                          * If the door was _not_ revoked on us,
 537                          * then more than likely we took an INTR,
 538                          * so we need to fail the operation.
 539                          */
 540                         goto fail;
 541                 }
 542                 /*
 543                  * The only failure that can occur from getting
 544                  * the door info is EINVAL, so we let the code
 545                  * below handle it.
 546                  */
 547                 /* FALLTHROUGH */
 548 
 549         case EBADF:
 550         case EINVAL:
 551         default:
 552                 /*
 553                  * If we have a stale door handle, give smf a last
 554                  * chance to start it by sleeping for a little bit.
 555                  * If we're still hosed, we'll fail the call.
 556                  *
 557                  * Since we're going to reacquire the door handle
 558                  * upon the retry, we opt to sleep for a bit and
 559                  * _not_ to clear mountd_dh. If mountd restarted
 560                  * and was able to set mountd_dh, we should see
 561                  * the new instance; if not, we won't get caught
 562                  * up in the retry/DELAY loop.
 563                  */
 564                 door_ki_rele(dh);
 565                 if (!last) {
 566                         delay(hz);
 567                         last++;
 568                         goto retry;
 569                 }
 570                 sys_log("nfsauth: stale mountd door handle");
 571                 goto fail;
 572         }
 573 
 574         ASSERT(da.rbuf != NULL);
 575 
 576         /*
 577          * No door errors encountered; setup the XDR stream for decoding
 578          * the results. If we fail to decode the results, we've got no
 579          * other recourse than to fail the request.
 580          */
 581         xdrmem_create(&xdrs, da.rbuf, da.rsize, XDR_DECODE);
 582         if (!xdr_nfsauth_res(&xdrs, &res)) {
 583                 xdr_free(xdr_nfsauth_res, (char *)&res);
 584                 XDR_DESTROY(&xdrs);
 585                 kmem_free(da.rbuf, da.rsize);
 586                 goto fail;
 587         }
 588         XDR_DESTROY(&xdrs);
 589         kmem_free(da.rbuf, da.rsize);
 590 
 591         DTRACE_PROBE1(nfsserv__func__nfsauth__results, nfsauth_res_t *, &res);
 592         switch (res.stat) {
 593                 case NFSAUTH_DR_OKAY:
 594                         *access = res.ares.auth_perm;
 595                         *srv_uid = res.ares.auth_srv_uid;
 596                         *srv_gid = res.ares.auth_srv_gid;
 597                         *srv_gids_cnt = res.ares.auth_srv_gids.len;
 598                         *srv_gids = kmem_alloc(*srv_gids_cnt * sizeof (gid_t),
 599                             KM_SLEEP);
 600                         bcopy(res.ares.auth_srv_gids.val, *srv_gids,
 601                             *srv_gids_cnt * sizeof (gid_t));
 602                         break;
 603 
 604                 case NFSAUTH_DR_EFAIL:
 605                 case NFSAUTH_DR_DECERR:
 606                 case NFSAUTH_DR_BADCMD:
 607                 default:
 608                         xdr_free(xdr_nfsauth_res, (char *)&res);
 609 fail:
 610                         *access = NFSAUTH_DENIED;
 611                         kmem_free(abuf, absz);
 612                         return (FALSE);
 613                         /* NOTREACHED */
 614         }
 615 
 616         xdr_free(xdr_nfsauth_res, (char *)&res);
 617         kmem_free(abuf, absz);
 618 
 619         return (TRUE);
 620 }
 621 
 622 static void
 623 nfsauth_refresh_thread(nfsauth_globals_t *nag)
 624 {
 625         refreshq_exi_node_t     *ren;
 626         refreshq_auth_node_t    *ran;
 627 
 628         struct exportinfo       *exi;
 629 
 630         int                     access;
 631         bool_t                  retrieval;
 632 
 633         callb_cpr_t             cprinfo;
 634 
 635         CALLB_CPR_INIT(&cprinfo, &nag->refreshq_lock, callb_generic_cpr,
 636             "nfsauth_refresh");
 637 
 638         for (;;) {
 639                 mutex_enter(&nag->refreshq_lock);
 640                 if (nag->refreshq_thread_state != REFRESHQ_THREAD_RUNNING) {
 641                         /* Keep the hold on the lock! */
 642                         break;
 643                 }
 644 
 645                 ren = list_remove_head(&nag->refreshq_queue);
 646                 if (ren == NULL) {
 647                         CALLB_CPR_SAFE_BEGIN(&cprinfo);
 648                         cv_wait(&nag->refreshq_cv, &nag->refreshq_lock);
 649                         CALLB_CPR_SAFE_END(&cprinfo, &nag->refreshq_lock);
 650                         mutex_exit(&nag->refreshq_lock);
 651                         continue;
 652                 }
 653                 mutex_exit(&nag->refreshq_lock);
 654 
 655                 exi = ren->ren_exi;
 656                 ASSERT(exi != NULL);
 657 
 658                 /*
 659                  * Since the ren was removed from the refreshq_queue above,
 660                  * this is the only thread aware about the ren existence, so we
 661                  * have the exclusive ownership of it and we do not need to
 662                  * protect it by any lock.
 663                  */
 664                 while ((ran = list_remove_head(&ren->ren_authlist))) {
 665                         uid_t uid;
 666                         gid_t gid;
 667                         uint_t ngids;
 668                         gid_t *gids;
 669                         struct auth_cache *p = ran->ran_auth;
 670                         char *netid = ran->ran_netid;
 671 
 672                         ASSERT(p != NULL);
 673                         ASSERT(netid != NULL);
 674 
 675                         kmem_free(ran, sizeof (refreshq_auth_node_t));
 676 
 677                         mutex_enter(&p->auth_lock);
 678 
 679                         /*
 680                          * Once the entry goes INVALID, it can not change
 681                          * state.
 682                          *
 683                          * No need to refresh entries also in a case we are
 684                          * just shutting down.
 685                          *
 686                          * In general, there is no need to hold the
 687                          * refreshq_lock to test the refreshq_thread_state.  We
 688                          * do hold it at other places because there is some
 689                          * related thread synchronization (or some other tasks)
 690                          * close to the refreshq_thread_state check.
 691                          *
 692                          * The check for the refreshq_thread_state value here
 693                          * is purely advisory to allow the faster
 694                          * nfsauth_refresh_thread() shutdown.  In a case we
 695                          * will miss such advisory, nothing catastrophic
 696                          * happens: we will just spin longer here before the
 697                          * shutdown.
 698                          */
 699                         if (p->auth_state == NFS_AUTH_INVALID ||
 700                             nag->refreshq_thread_state !=
 701                             REFRESHQ_THREAD_RUNNING) {
 702                                 mutex_exit(&p->auth_lock);
 703 
 704                                 if (p->auth_state == NFS_AUTH_INVALID)
 705                                         nfsauth_free_node(p);
 706 
 707                                 strfree(netid);
 708 
 709                                 continue;
 710                         }
 711 
 712                         /*
 713                          * Make sure the state is valid.  Note that once we
 714                          * change the state to NFS_AUTH_REFRESHING, no other
 715                          * thread will be able to work on this entry.
 716                          */
 717                         ASSERT(p->auth_state == NFS_AUTH_STALE);
 718 
 719                         p->auth_state = NFS_AUTH_REFRESHING;
 720                         mutex_exit(&p->auth_lock);
 721 
 722                         DTRACE_PROBE2(nfsauth__debug__cache__refresh,
 723                             struct exportinfo *, exi,
 724                             struct auth_cache *, p);
 725 
 726                         /*
 727                          * The first caching of the access rights
 728                          * is done with the netid pulled out of the
 729                          * request from the client. All subsequent
 730                          * users of the cache may or may not have
 731                          * the same netid. It doesn't matter. So
 732                          * when we refresh, we simply use the netid
 733                          * of the request which triggered the
 734                          * refresh attempt.
 735                          */
 736                         retrieval = nfsauth_retrieve(nag, exi, netid,
 737                             p->auth_flavor, &p->auth_clnt->authc_addr, &access,
 738                             p->auth_clnt_cred, &uid, &gid, &ngids, &gids);
 739 
 740                         /*
 741                          * This can only be set in one other place
 742                          * and the state has to be NFS_AUTH_FRESH.
 743                          */
 744                         strfree(netid);
 745 
 746                         mutex_enter(&p->auth_lock);
 747                         if (p->auth_state == NFS_AUTH_INVALID) {
 748                                 mutex_exit(&p->auth_lock);
 749                                 nfsauth_free_node(p);
 750                                 if (retrieval == TRUE)
 751                                         kmem_free(gids, ngids * sizeof (gid_t));
 752                         } else {
 753                                 /*
 754                                  * If we got an error, do not reset the
 755                                  * time. This will cause the next access
 756                                  * check for the client to reschedule this
 757                                  * node.
 758                                  */
 759                                 if (retrieval == TRUE) {
 760                                         p->auth_access = access;
 761 
 762                                         p->auth_srv_uid = uid;
 763                                         p->auth_srv_gid = gid;
 764                                         kmem_free(p->auth_srv_gids,
 765                                             p->auth_srv_ngids * sizeof (gid_t));
 766                                         p->auth_srv_ngids = ngids;
 767                                         p->auth_srv_gids = gids;
 768 
 769                                         p->auth_freshness = gethrestime_sec();
 770                                 }
 771                                 p->auth_state = NFS_AUTH_FRESH;
 772 
 773                                 cv_broadcast(&p->auth_cv);
 774                                 mutex_exit(&p->auth_lock);
 775                         }
 776                 }
 777 
 778                 list_destroy(&ren->ren_authlist);
 779                 exi_rele(ren->ren_exi);
 780                 kmem_free(ren, sizeof (refreshq_exi_node_t));
 781         }
 782 
 783         nag->refreshq_thread_state = REFRESHQ_THREAD_HALTED;
 784         cv_broadcast(&nag->refreshq_cv);
 785         CALLB_CPR_EXIT(&cprinfo);
 786         DTRACE_PROBE(nfsauth__nfsauth__refresh__thread__exit);
 787         zthread_exit();
 788 }
 789 
 790 int
 791 nfsauth_cache_clnt_compar(const void *v1, const void *v2)
 792 {
 793         int c;
 794 
 795         const struct auth_cache_clnt *a1 = (const struct auth_cache_clnt *)v1;
 796         const struct auth_cache_clnt *a2 = (const struct auth_cache_clnt *)v2;
 797 
 798         if (a1->authc_addr.len < a2->authc_addr.len)
 799                 return (-1);
 800         if (a1->authc_addr.len > a2->authc_addr.len)
 801                 return (1);
 802 
 803         c = memcmp(a1->authc_addr.buf, a2->authc_addr.buf, a1->authc_addr.len);
 804         if (c < 0)
 805                 return (-1);
 806         if (c > 0)
 807                 return (1);
 808 
 809         return (0);
 810 }
 811 
 812 static int
 813 nfsauth_cache_compar(const void *v1, const void *v2)
 814 {
 815         int c;
 816 
 817         const struct auth_cache *a1 = (const struct auth_cache *)v1;
 818         const struct auth_cache *a2 = (const struct auth_cache *)v2;
 819 
 820         if (a1->auth_flavor < a2->auth_flavor)
 821                 return (-1);
 822         if (a1->auth_flavor > a2->auth_flavor)
 823                 return (1);
 824 
 825         if (crgetuid(a1->auth_clnt_cred) < crgetuid(a2->auth_clnt_cred))
 826                 return (-1);
 827         if (crgetuid(a1->auth_clnt_cred) > crgetuid(a2->auth_clnt_cred))
 828                 return (1);
 829 
 830         if (crgetgid(a1->auth_clnt_cred) < crgetgid(a2->auth_clnt_cred))
 831                 return (-1);
 832         if (crgetgid(a1->auth_clnt_cred) > crgetgid(a2->auth_clnt_cred))
 833                 return (1);
 834 
 835         if (crgetngroups(a1->auth_clnt_cred) < crgetngroups(a2->auth_clnt_cred))
 836                 return (-1);
 837         if (crgetngroups(a1->auth_clnt_cred) > crgetngroups(a2->auth_clnt_cred))
 838                 return (1);
 839 
 840         c = memcmp(crgetgroups(a1->auth_clnt_cred),
 841             crgetgroups(a2->auth_clnt_cred), crgetngroups(a1->auth_clnt_cred));
 842         if (c < 0)
 843                 return (-1);
 844         if (c > 0)
 845                 return (1);
 846 
 847         return (0);
 848 }
 849 
 850 /*
 851  * Get the access information from the cache or callup to the mountd
 852  * to get and cache the access information in the kernel.
 853  */
 854 static int
 855 nfsauth_cache_get(struct exportinfo *exi, struct svc_req *req, int flavor,
 856     cred_t *cr, uid_t *uid, gid_t *gid, uint_t *ngids, gid_t **gids)
 857 {
 858         nfsauth_globals_t       *nag;
 859         struct netbuf           *taddrmask;
 860         struct netbuf           addr;   /* temporary copy of client's address */
 861         const struct netbuf     *claddr;
 862         avl_tree_t              *tree;
 863         struct auth_cache       ac;     /* used as a template for avl_find() */
 864         struct auth_cache_clnt  *c;
 865         struct auth_cache_clnt  acc;    /* used as a template for avl_find() */
 866         struct auth_cache       *p = NULL;
 867         int                     access;
 868 
 869         uid_t                   tmpuid;
 870         gid_t                   tmpgid;
 871         uint_t                  tmpngids;
 872         gid_t                   *tmpgids;
 873 
 874         avl_index_t             where;  /* used for avl_find()/avl_insert() */
 875 
 876         ASSERT(cr != NULL);
 877 
 878         nag = nfsauth_get_zg();
 879 
 880         /*
 881          * Now check whether this client already
 882          * has an entry for this flavor in the cache
 883          * for this export.
 884          * Get the caller's address, mask off the
 885          * parts of the address that do not identify
 886          * the host (port number, etc), and then hash
 887          * it to find the chain of cache entries.
 888          */
 889 
 890         claddr = svc_getrpccaller(req->rq_xprt);
 891         addr = *claddr;
 892         addr.buf = kmem_alloc(addr.maxlen, KM_SLEEP);
 893         bcopy(claddr->buf, addr.buf, claddr->len);
 894 
 895         SVC_GETADDRMASK(req->rq_xprt, SVC_TATTR_ADDRMASK, (void **)&taddrmask);
 896         ASSERT(taddrmask != NULL);
 897         addrmask(&addr, taddrmask);
 898 
 899         ac.auth_flavor = flavor;
 900         ac.auth_clnt_cred = crdup(cr);
 901 
 902         acc.authc_addr = addr;
 903 
 904         tree = exi->exi_cache[hash(&addr)];
 905 
 906         rw_enter(&exi->exi_cache_lock, RW_READER);
 907         c = (struct auth_cache_clnt *)avl_find(tree, &acc, NULL);
 908 
 909         if (c == NULL) {
 910                 struct auth_cache_clnt *nc;
 911 
 912                 rw_exit(&exi->exi_cache_lock);
 913 
 914                 nc = kmem_alloc(sizeof (*nc), KM_NOSLEEP | KM_NORMALPRI);
 915                 if (nc == NULL)
 916                         goto retrieve;
 917 
 918                 /*
 919                  * Initialize the new auth_cache_clnt
 920                  */
 921                 nc->authc_addr = addr;
 922                 nc->authc_addr.buf = kmem_alloc(addr.maxlen,
 923                     KM_NOSLEEP | KM_NORMALPRI);
 924                 if (addr.maxlen != 0 && nc->authc_addr.buf == NULL) {
 925                         kmem_free(nc, sizeof (*nc));
 926                         goto retrieve;
 927                 }
 928                 bcopy(addr.buf, nc->authc_addr.buf, addr.len);
 929                 rw_init(&nc->authc_lock, NULL, RW_DEFAULT, NULL);
 930                 avl_create(&nc->authc_tree, nfsauth_cache_compar,
 931                     sizeof (struct auth_cache),
 932                     offsetof(struct auth_cache, auth_link));
 933 
 934                 rw_enter(&exi->exi_cache_lock, RW_WRITER);
 935                 c = (struct auth_cache_clnt *)avl_find(tree, &acc, &where);
 936                 if (c == NULL) {
 937                         avl_insert(tree, nc, where);
 938                         rw_downgrade(&exi->exi_cache_lock);
 939                         c = nc;
 940                 } else {
 941                         rw_downgrade(&exi->exi_cache_lock);
 942 
 943                         avl_destroy(&nc->authc_tree);
 944                         rw_destroy(&nc->authc_lock);
 945                         kmem_free(nc->authc_addr.buf, nc->authc_addr.maxlen);
 946                         kmem_free(nc, sizeof (*nc));
 947                 }
 948         }
 949 
 950         ASSERT(c != NULL);
 951 
 952         rw_enter(&c->authc_lock, RW_READER);
 953         p = (struct auth_cache *)avl_find(&c->authc_tree, &ac, NULL);
 954 
 955         if (p == NULL) {
 956                 struct auth_cache *np;
 957 
 958                 rw_exit(&c->authc_lock);
 959 
 960                 np = kmem_cache_alloc(exi_cache_handle,
 961                     KM_NOSLEEP | KM_NORMALPRI);
 962                 if (np == NULL) {
 963                         rw_exit(&exi->exi_cache_lock);
 964                         goto retrieve;
 965                 }
 966 
 967                 /*
 968                  * Initialize the new auth_cache
 969                  */
 970                 np->auth_clnt = c;
 971                 np->auth_flavor = flavor;
 972                 np->auth_clnt_cred = ac.auth_clnt_cred;
 973                 np->auth_srv_ngids = 0;
 974                 np->auth_srv_gids = NULL;
 975                 np->auth_time = np->auth_freshness = gethrestime_sec();
 976                 np->auth_state = NFS_AUTH_NEW;
 977                 mutex_init(&np->auth_lock, NULL, MUTEX_DEFAULT, NULL);
 978                 cv_init(&np->auth_cv, NULL, CV_DEFAULT, NULL);
 979 
 980                 rw_enter(&c->authc_lock, RW_WRITER);
 981                 rw_exit(&exi->exi_cache_lock);
 982 
 983                 p = (struct auth_cache *)avl_find(&c->authc_tree, &ac, &where);
 984                 if (p == NULL) {
 985                         avl_insert(&c->authc_tree, np, where);
 986                         rw_downgrade(&c->authc_lock);
 987                         p = np;
 988                 } else {
 989                         rw_downgrade(&c->authc_lock);
 990 
 991                         cv_destroy(&np->auth_cv);
 992                         mutex_destroy(&np->auth_lock);
 993                         crfree(ac.auth_clnt_cred);
 994                         kmem_cache_free(exi_cache_handle, np);
 995                 }
 996         } else {
 997                 rw_exit(&exi->exi_cache_lock);
 998                 crfree(ac.auth_clnt_cred);
 999         }
1000 
1001         mutex_enter(&p->auth_lock);
1002         rw_exit(&c->authc_lock);
1003 
1004         /*
1005          * If the entry is in the WAITING state then some other thread is just
1006          * retrieving the required info.  The entry was either NEW, or the list
1007          * of client's supplemental groups is going to be changed (either by
1008          * this thread, or by some other thread).  We need to wait until the
1009          * nfsauth_retrieve() is done.
1010          */
1011         while (p->auth_state == NFS_AUTH_WAITING)
1012                 cv_wait(&p->auth_cv, &p->auth_lock);
1013 
1014         /*
1015          * Here the entry cannot be in WAITING or INVALID state.
1016          */
1017         ASSERT(p->auth_state != NFS_AUTH_WAITING);
1018         ASSERT(p->auth_state != NFS_AUTH_INVALID);
1019 
1020         /*
1021          * If the cache entry is not valid yet, we need to retrieve the
1022          * info ourselves.
1023          */
1024         if (p->auth_state == NFS_AUTH_NEW) {
1025                 bool_t res;
1026                 /*
1027                  * NFS_AUTH_NEW is the default output auth_state value in a
1028                  * case we failed somewhere below.
1029                  */
1030                 auth_state_t state = NFS_AUTH_NEW;
1031 
1032                 p->auth_state = NFS_AUTH_WAITING;
1033                 mutex_exit(&p->auth_lock);
1034                 kmem_free(addr.buf, addr.maxlen);
1035                 addr = p->auth_clnt->authc_addr;
1036 
1037                 atomic_inc_uint(&nfsauth_cache_miss);
1038 
1039                 res = nfsauth_retrieve(nag, exi, svc_getnetid(req->rq_xprt),
1040                     flavor, &addr, &access, cr, &tmpuid, &tmpgid, &tmpngids,
1041                     &tmpgids);
1042 
1043                 p->auth_access = access;
1044                 p->auth_time = p->auth_freshness = gethrestime_sec();
1045 
1046                 if (res == TRUE) {
1047                         if (uid != NULL)
1048                                 *uid = tmpuid;
1049                         if (gid != NULL)
1050                                 *gid = tmpgid;
1051                         if (ngids != NULL && gids != NULL) {
1052                                 *ngids = tmpngids;
1053                                 *gids = tmpgids;
1054 
1055                                 /*
1056                                  * We need a copy of gids for the
1057                                  * auth_cache entry
1058                                  */
1059                                 tmpgids = kmem_alloc(tmpngids * sizeof (gid_t),
1060                                     KM_NOSLEEP | KM_NORMALPRI);
1061                                 if (tmpgids != NULL)
1062                                         bcopy(*gids, tmpgids,
1063                                             tmpngids * sizeof (gid_t));
1064                         }
1065 
1066                         if (tmpgids != NULL || tmpngids == 0) {
1067                                 p->auth_srv_uid = tmpuid;
1068                                 p->auth_srv_gid = tmpgid;
1069                                 p->auth_srv_ngids = tmpngids;
1070                                 p->auth_srv_gids = tmpgids;
1071 
1072                                 state = NFS_AUTH_FRESH;
1073                         }
1074                 }
1075 
1076                 /*
1077                  * Set the auth_state and notify waiters.
1078                  */
1079                 mutex_enter(&p->auth_lock);
1080                 p->auth_state = state;
1081                 cv_broadcast(&p->auth_cv);
1082                 mutex_exit(&p->auth_lock);
1083         } else {
1084                 uint_t nach;
1085                 time_t refresh;
1086 
1087                 refresh = gethrestime_sec() - p->auth_freshness;
1088 
1089                 p->auth_time = gethrestime_sec();
1090 
1091                 if (uid != NULL)
1092                         *uid = p->auth_srv_uid;
1093                 if (gid != NULL)
1094                         *gid = p->auth_srv_gid;
1095                 if (ngids != NULL && gids != NULL) {
1096                         *ngids = p->auth_srv_ngids;
1097                         *gids = kmem_alloc(*ngids * sizeof (gid_t), KM_SLEEP);
1098                         bcopy(p->auth_srv_gids, *gids, *ngids * sizeof (gid_t));
1099                 }
1100 
1101                 access = p->auth_access;
1102 
1103                 if ((refresh > NFSAUTH_CACHE_REFRESH) &&
1104                     p->auth_state == NFS_AUTH_FRESH) {
1105                         refreshq_auth_node_t *ran;
1106                         uint_t nacr;
1107 
1108                         p->auth_state = NFS_AUTH_STALE;
1109                         mutex_exit(&p->auth_lock);
1110 
1111                         nacr = atomic_inc_uint_nv(&nfsauth_cache_refresh);
1112                         DTRACE_PROBE3(nfsauth__debug__cache__stale,
1113                             struct exportinfo *, exi,
1114                             struct auth_cache *, p,
1115                             uint_t, nacr);
1116 
1117                         ran = kmem_alloc(sizeof (refreshq_auth_node_t),
1118                             KM_SLEEP);
1119                         ran->ran_auth = p;
1120                         ran->ran_netid = strdup(svc_getnetid(req->rq_xprt));
1121 
1122                         mutex_enter(&nag->refreshq_lock);
1123 
1124                         if (nag->refreshq_thread_state ==
1125                             REFRESHQ_THREAD_NEED_CREATE) {
1126                                 /* Launch nfsauth refresh thread */
1127                                 nag->refreshq_thread_state =
1128                                     REFRESHQ_THREAD_RUNNING;
1129                                 (void) zthread_create(NULL, 0,
1130                                     nfsauth_refresh_thread, nag, 0,
1131                                     minclsyspri);
1132                         }
1133 
1134                         /*
1135                          * We should not add a work queue item if the thread
1136                          * is not accepting them.
1137                          */
1138                         if (nag->refreshq_thread_state ==
1139                             REFRESHQ_THREAD_RUNNING) {
1140                                 refreshq_exi_node_t *ren;
1141 
1142                                 /*
1143                                  * Is there an existing exi_list?
1144                                  */
1145                                 for (ren = list_head(&nag->refreshq_queue);
1146                                     ren != NULL;
1147                                     ren = list_next(&nag->refreshq_queue,
1148                                     ren)) {
1149                                         if (ren->ren_exi == exi) {
1150                                                 list_insert_tail(
1151                                                     &ren->ren_authlist, ran);
1152                                                 break;
1153                                         }
1154                                 }
1155 
1156                                 if (ren == NULL) {
1157                                         ren = kmem_alloc(
1158                                             sizeof (refreshq_exi_node_t),
1159                                             KM_SLEEP);
1160 
1161                                         exi_hold(exi);
1162                                         ren->ren_exi = exi;
1163 
1164                                         list_create(&ren->ren_authlist,
1165                                             sizeof (refreshq_auth_node_t),
1166                                             offsetof(refreshq_auth_node_t,
1167                                             ran_node));
1168 
1169                                         list_insert_tail(&ren->ren_authlist,
1170                                             ran);
1171                                         list_insert_tail(&nag->refreshq_queue,
1172                                             ren);
1173                                 }
1174 
1175                                 cv_broadcast(&nag->refreshq_cv);
1176                         } else {
1177                                 strfree(ran->ran_netid);
1178                                 kmem_free(ran, sizeof (refreshq_auth_node_t));
1179                         }
1180 
1181                         mutex_exit(&nag->refreshq_lock);
1182                 } else {
1183                         mutex_exit(&p->auth_lock);
1184                 }
1185 
1186                 nach = atomic_inc_uint_nv(&nfsauth_cache_hit);
1187                 DTRACE_PROBE2(nfsauth__debug__cache__hit,
1188                     uint_t, nach,
1189                     time_t, refresh);
1190 
1191                 kmem_free(addr.buf, addr.maxlen);
1192         }
1193 
1194         return (access);
1195 
1196 retrieve:
1197         crfree(ac.auth_clnt_cred);
1198 
1199         /*
1200          * Retrieve the required data without caching.
1201          */
1202 
1203         ASSERT(p == NULL);
1204 
1205         atomic_inc_uint(&nfsauth_cache_miss);
1206 
1207         if (nfsauth_retrieve(nag, exi, svc_getnetid(req->rq_xprt), flavor,
1208             &addr, &access, cr, &tmpuid, &tmpgid, &tmpngids, &tmpgids)) {
1209                 if (uid != NULL)
1210                         *uid = tmpuid;
1211                 if (gid != NULL)
1212                         *gid = tmpgid;
1213                 if (ngids != NULL && gids != NULL) {
1214                         *ngids = tmpngids;
1215                         *gids = tmpgids;
1216                 } else {
1217                         kmem_free(tmpgids, tmpngids * sizeof (gid_t));
1218                 }
1219         }
1220 
1221         kmem_free(addr.buf, addr.maxlen);
1222 
1223         return (access);
1224 }
1225 
1226 /*
1227  * Check if the requesting client has access to the filesystem with
1228  * a given nfs flavor number which is an explicitly shared flavor.
1229  */
1230 int
1231 nfsauth4_secinfo_access(struct exportinfo *exi, struct svc_req *req,
1232     int flavor, int perm, cred_t *cr)
1233 {
1234         int access;
1235 
1236         if (! (perm & M_4SEC_EXPORTED)) {
1237                 return (NFSAUTH_DENIED);
1238         }
1239 
1240         /*
1241          * Optimize if there are no lists
1242          */
1243         if ((perm & (M_ROOT | M_NONE | M_MAP)) == 0) {
1244                 perm &= ~M_4SEC_EXPORTED;
1245                 if (perm == M_RO)
1246                         return (NFSAUTH_RO);
1247                 if (perm == M_RW)
1248                         return (NFSAUTH_RW);
1249         }
1250 
1251         access = nfsauth_cache_get(exi, req, flavor, cr, NULL, NULL, NULL,
1252             NULL);
1253 
1254         return (access);
1255 }
1256 
1257 int
1258 nfsauth_access(struct exportinfo *exi, struct svc_req *req, cred_t *cr,
1259     uid_t *uid, gid_t *gid, uint_t *ngids, gid_t **gids)
1260 {
1261         int access, mapaccess;
1262         struct secinfo *sp;
1263         int i, flavor, perm;
1264         int authnone_entry = -1;
1265 
1266         /*
1267          * By default root is mapped to anonymous user.
1268          * This might get overriden later in nfsauth_cache_get().
1269          */
1270         if (crgetuid(cr) == 0) {
1271                 if (uid != NULL)
1272                         *uid = exi->exi_export.ex_anon;
1273                 if (gid != NULL)
1274                         *gid = exi->exi_export.ex_anon;
1275         } else {
1276                 if (uid != NULL)
1277                         *uid = crgetuid(cr);
1278                 if (gid != NULL)
1279                         *gid = crgetgid(cr);
1280         }
1281 
1282         if (ngids != NULL)
1283                 *ngids = 0;
1284         if (gids != NULL)
1285                 *gids = NULL;
1286 
1287         /*
1288          *  Get the nfs flavor number from xprt.
1289          */
1290         flavor = (int)(uintptr_t)req->rq_xprt->xp_cookie;
1291 
1292         /*
1293          * First check the access restrictions on the filesystem.  If
1294          * there are no lists associated with this flavor then there's no
1295          * need to make an expensive call to the nfsauth service or to
1296          * cache anything.
1297          */
1298 
1299         sp = exi->exi_export.ex_secinfo;
1300         for (i = 0; i < exi->exi_export.ex_seccnt; i++) {
1301                 if (flavor != sp[i].s_secinfo.sc_nfsnum) {
1302                         if (sp[i].s_secinfo.sc_nfsnum == AUTH_NONE)
1303                                 authnone_entry = i;
1304                         continue;
1305                 }
1306                 break;
1307         }
1308 
1309         mapaccess = 0;
1310 
1311         if (i >= exi->exi_export.ex_seccnt) {
1312                 /*
1313                  * Flavor not found, but use AUTH_NONE if it exists
1314                  */
1315                 if (authnone_entry == -1)
1316                         return (NFSAUTH_DENIED);
1317                 flavor = AUTH_NONE;
1318                 mapaccess = NFSAUTH_MAPNONE;
1319                 i = authnone_entry;
1320         }
1321 
1322         /*
1323          * If the flavor is in the ex_secinfo list, but not an explicitly
1324          * shared flavor by the user, it is a result of the nfsv4 server
1325          * namespace setup. We will grant an RO permission similar for
1326          * a pseudo node except that this node is a shared one.
1327          *
1328          * e.g. flavor in (flavor) indicates that it is not explictly
1329          *      shared by the user:
1330          *
1331          *              /       (sys, krb5)
1332          *              |
1333          *              export  #share -o sec=sys (krb5)
1334          *              |
1335          *              secure  #share -o sec=krb5
1336          *
1337          *      In this case, when a krb5 request coming in to access
1338          *      /export, RO permission is granted.
1339          */
1340         if (!(sp[i].s_flags & M_4SEC_EXPORTED))
1341                 return (mapaccess | NFSAUTH_RO);
1342 
1343         /*
1344          * Optimize if there are no lists.
1345          * We cannot optimize for AUTH_SYS with NGRPS (16) supplemental groups.
1346          */
1347         perm = sp[i].s_flags;
1348         if ((perm & (M_ROOT | M_NONE | M_MAP)) == 0 && (ngroups_max <= NGRPS ||
1349             flavor != AUTH_SYS || crgetngroups(cr) < NGRPS)) {
1350                 perm &= ~M_4SEC_EXPORTED;
1351                 if (perm == M_RO)
1352                         return (mapaccess | NFSAUTH_RO);
1353                 if (perm == M_RW)
1354                         return (mapaccess | NFSAUTH_RW);
1355         }
1356 
1357         access = nfsauth_cache_get(exi, req, flavor, cr, uid, gid, ngids, gids);
1358 
1359         /*
1360          * For both NFSAUTH_DENIED and NFSAUTH_WRONGSEC we do not care about
1361          * the supplemental groups.
1362          */
1363         if (access & NFSAUTH_DENIED || access & NFSAUTH_WRONGSEC) {
1364                 if (ngids != NULL && gids != NULL) {
1365                         kmem_free(*gids, *ngids * sizeof (gid_t));
1366                         *ngids = 0;
1367                         *gids = NULL;
1368                 }
1369         }
1370 
1371         /*
1372          * Client's security flavor doesn't match with "ro" or
1373          * "rw" list. Try again using AUTH_NONE if present.
1374          */
1375         if ((access & NFSAUTH_WRONGSEC) && (flavor != AUTH_NONE)) {
1376                 /*
1377                  * Have we already encountered AUTH_NONE ?
1378                  */
1379                 if (authnone_entry != -1) {
1380                         mapaccess = NFSAUTH_MAPNONE;
1381                         access = nfsauth_cache_get(exi, req, AUTH_NONE, cr,
1382                             NULL, NULL, NULL, NULL);
1383                 } else {
1384                         /*
1385                          * Check for AUTH_NONE presence.
1386                          */
1387                         for (; i < exi->exi_export.ex_seccnt; i++) {
1388                                 if (sp[i].s_secinfo.sc_nfsnum == AUTH_NONE) {
1389                                         mapaccess = NFSAUTH_MAPNONE;
1390                                         access = nfsauth_cache_get(exi, req,
1391                                             AUTH_NONE, cr, NULL, NULL, NULL,
1392                                             NULL);
1393                                         break;
1394                                 }
1395                         }
1396                 }
1397         }
1398 
1399         if (access & NFSAUTH_DENIED)
1400                 access = NFSAUTH_DENIED;
1401 
1402         return (access | mapaccess);
1403 }
1404 
1405 static void
1406 nfsauth_free_clnt_node(struct auth_cache_clnt *p)
1407 {
1408         void *cookie = NULL;
1409         struct auth_cache *node;
1410 
1411         while ((node = avl_destroy_nodes(&p->authc_tree, &cookie)) != NULL)
1412                 nfsauth_free_node(node);
1413         avl_destroy(&p->authc_tree);
1414 
1415         kmem_free(p->authc_addr.buf, p->authc_addr.maxlen);
1416         rw_destroy(&p->authc_lock);
1417 
1418         kmem_free(p, sizeof (*p));
1419 }
1420 
1421 static void
1422 nfsauth_free_node(struct auth_cache *p)
1423 {
1424         crfree(p->auth_clnt_cred);
1425         kmem_free(p->auth_srv_gids, p->auth_srv_ngids * sizeof (gid_t));
1426         mutex_destroy(&p->auth_lock);
1427         cv_destroy(&p->auth_cv);
1428         kmem_cache_free(exi_cache_handle, p);
1429 }
1430 
1431 /*
1432  * Free the nfsauth cache for a given export
1433  */
1434 void
1435 nfsauth_cache_free(struct exportinfo *exi)
1436 {
1437         int i;
1438 
1439         /*
1440          * The only way we got here was with an exi_rele, which means that no
1441          * auth cache entry is being refreshed.
1442          */
1443 
1444         for (i = 0; i < AUTH_TABLESIZE; i++) {
1445                 avl_tree_t *tree = exi->exi_cache[i];
1446                 void *cookie = NULL;
1447                 struct auth_cache_clnt *node;
1448 
1449                 while ((node = avl_destroy_nodes(tree, &cookie)) != NULL)
1450                         nfsauth_free_clnt_node(node);
1451         }
1452 }
1453 
1454 /*
1455  * Called by the kernel memory allocator when memory is low.
1456  * Free unused cache entries. If that's not enough, the VM system
1457  * will call again for some more.
1458  *
1459  * This needs to operate on all zones, so we take a reader lock
1460  * on the list of zones and walk the list.  This is OK here
1461  * becuase exi_cache_trim doesn't block or cause new objects
1462  * to be allocated (basically just frees lots of stuff).
1463  * Use care if nfssrv_globals_rwl is taken as reader in any
1464  * other cases because it will block nfs_server_zone_init
1465  * and nfs_server_zone_fini, which enter as writer.
1466  */
1467 /*ARGSUSED*/
1468 void
1469 exi_cache_reclaim(void *cdrarg)
1470 {
1471         nfs_globals_t *ng;
1472 
1473         rw_enter(&nfssrv_globals_rwl, RW_READER);
1474 
1475         ng = list_head(&nfssrv_globals_list);
1476         while (ng != NULL) {
1477                 exi_cache_reclaim_zone(ng);
1478                 ng = list_next(&nfssrv_globals_list, ng);
1479         }
1480 
1481         rw_exit(&nfssrv_globals_rwl);
1482 }
1483 
1484 static void
1485 exi_cache_reclaim_zone(nfs_globals_t *ng)
1486 {
1487         int i;
1488         struct exportinfo *exi;
1489         nfs_export_t *ne = ng->nfs_export;
1490 
1491         rw_enter(&ne->exported_lock, RW_READER);
1492 
1493         for (i = 0; i < EXPTABLESIZE; i++) {
1494                 for (exi = ne->exptable[i]; exi; exi = exi->fid_hash.next)
1495                         exi_cache_trim(exi);
1496         }
1497 
1498         rw_exit(&ne->exported_lock);
1499 
1500         atomic_inc_uint(&nfsauth_cache_reclaim);
1501 }
1502 
1503 static void
1504 exi_cache_trim(struct exportinfo *exi)
1505 {
1506         struct auth_cache_clnt *c;
1507         struct auth_cache_clnt *nextc;
1508         struct auth_cache *p;
1509         struct auth_cache *next;
1510         int i;
1511         time_t stale_time;
1512         avl_tree_t *tree;
1513 
1514         for (i = 0; i < AUTH_TABLESIZE; i++) {
1515                 tree = exi->exi_cache[i];
1516                 stale_time = gethrestime_sec() - NFSAUTH_CACHE_TRIM;
1517                 rw_enter(&exi->exi_cache_lock, RW_READER);
1518 
1519                 /*
1520                  * Free entries that have not been
1521                  * used for NFSAUTH_CACHE_TRIM seconds.
1522                  */
1523                 for (c = avl_first(tree); c != NULL; c = AVL_NEXT(tree, c)) {
1524                         /*
1525                          * We are being called by the kmem subsystem to reclaim
1526                          * memory so don't block if we can't get the lock.
1527                          */
1528                         if (rw_tryenter(&c->authc_lock, RW_WRITER) == 0) {
1529                                 exi_cache_auth_reclaim_failed++;
1530                                 rw_exit(&exi->exi_cache_lock);
1531                                 return;
1532                         }
1533 
1534                         for (p = avl_first(&c->authc_tree); p != NULL;
1535                             p = next) {
1536                                 next = AVL_NEXT(&c->authc_tree, p);
1537 
1538                                 ASSERT(p->auth_state != NFS_AUTH_INVALID);
1539 
1540                                 mutex_enter(&p->auth_lock);
1541 
1542                                 /*
1543                                  * We won't trim recently used and/or WAITING
1544                                  * entries.
1545                                  */
1546                                 if (p->auth_time > stale_time ||
1547                                     p->auth_state == NFS_AUTH_WAITING) {
1548                                         mutex_exit(&p->auth_lock);
1549                                         continue;
1550                                 }
1551 
1552                                 DTRACE_PROBE1(nfsauth__debug__trim__state,
1553                                     auth_state_t, p->auth_state);
1554 
1555                                 /*
1556                                  * STALE and REFRESHING entries needs to be
1557                                  * marked INVALID only because they are
1558                                  * referenced by some other structures or
1559                                  * threads.  They will be freed later.
1560                                  */
1561                                 if (p->auth_state == NFS_AUTH_STALE ||
1562                                     p->auth_state == NFS_AUTH_REFRESHING) {
1563                                         p->auth_state = NFS_AUTH_INVALID;
1564                                         mutex_exit(&p->auth_lock);
1565 
1566                                         avl_remove(&c->authc_tree, p);
1567                                 } else {
1568                                         mutex_exit(&p->auth_lock);
1569 
1570                                         avl_remove(&c->authc_tree, p);
1571                                         nfsauth_free_node(p);
1572                                 }
1573                         }
1574                         rw_exit(&c->authc_lock);
1575                 }
1576 
1577                 if (rw_tryupgrade(&exi->exi_cache_lock) == 0) {
1578                         rw_exit(&exi->exi_cache_lock);
1579                         exi_cache_clnt_reclaim_failed++;
1580                         continue;
1581                 }
1582 
1583                 for (c = avl_first(tree); c != NULL; c = nextc) {
1584                         nextc = AVL_NEXT(tree, c);
1585 
1586                         if (avl_is_empty(&c->authc_tree) == B_FALSE)
1587                                 continue;
1588 
1589                         avl_remove(tree, c);
1590 
1591                         nfsauth_free_clnt_node(c);
1592                 }
1593 
1594                 rw_exit(&exi->exi_cache_lock);
1595         }
1596 }