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