Print this page
11083 support NFS server in zone
Portions contributed by: Dan Kruchinin <dan.kruchinin@nexenta.com>
Portions contributed by: Stepan Zastupov <stepan.zastupov@gmail.com>
Portions contributed by: Joyce McIntosh <joyce.mcintosh@nexenta.com>
Portions contributed by: Mike Zeller <mike@mikezeller.net>
Portions contributed by: Dan McDonald <danmcd@joyent.com>
Portions contributed by: Gordon Ross <gordon.w.ross@gmail.com>
Portions contributed by: Vitaliy Gusev <gusev.vitaliy@gmail.com>
Reviewed by: Rick McNeal <rick.mcneal@nexenta.com>
Reviewed by: Rob Gittins <rob.gittins@nexenta.com>
Reviewed by: Sanjay Nadkarni <sanjay.nadkarni@nexenta.com>
Reviewed by: Jason King <jbk@joyent.com>
Reviewed by: C Fraire <cfraire@me.com>
Change-Id: I22f289d357503f9b48a0bc2482cc4328a6d43d16


   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 2016 Nexenta Systems, Inc.  All rights reserved.
  24  * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
  25  * Copyright (c) 2015 by Delphix. All rights reserved.
  26  * Copyright (c) 2015 Joyent, Inc.  All rights reserved.

  27  */
  28 
  29 #include <sys/param.h>
  30 #include <sys/errno.h>
  31 #include <sys/vfs.h>
  32 #include <sys/vnode.h>
  33 #include <sys/cred.h>
  34 #include <sys/cmn_err.h>
  35 #include <sys/systm.h>
  36 #include <sys/kmem.h>
  37 #include <sys/pathname.h>
  38 #include <sys/utsname.h>
  39 #include <sys/debug.h>
  40 #include <sys/door.h>
  41 #include <sys/sdt.h>
  42 #include <sys/thread.h>
  43 #include <sys/avl.h>
  44 
  45 #include <rpc/types.h>
  46 #include <rpc/auth.h>
  47 #include <rpc/clnt.h>
  48 
  49 #include <nfs/nfs.h>
  50 #include <nfs/export.h>
  51 #include <nfs/nfs_clnt.h>
  52 #include <nfs/auth.h>
  53 
  54 static struct kmem_cache *exi_cache_handle;
  55 static void exi_cache_reclaim(void *);

  56 static void exi_cache_trim(struct exportinfo *exi);
  57 
  58 extern pri_t minclsyspri;
  59 

  60 volatile uint_t nfsauth_cache_hit;
  61 volatile uint_t nfsauth_cache_miss;
  62 volatile uint_t nfsauth_cache_refresh;
  63 volatile uint_t nfsauth_cache_reclaim;
  64 volatile uint_t exi_cache_auth_reclaim_failed;
  65 volatile uint_t exi_cache_clnt_reclaim_failed;
  66 
  67 /*
  68  * The lifetime of an auth cache entry:
  69  * ------------------------------------
  70  *
  71  * An auth cache entry is created with both the auth_time
  72  * and auth_freshness times set to the current time.
  73  *
  74  * Upon every client access which results in a hit, the
  75  * auth_time will be updated.
  76  *
  77  * If a client access determines that the auth_freshness
  78  * indicates that the entry is STALE, then it will be
  79  * refreshed. Note that this will explicitly reset


 103 
 104 /*
 105  * While we could encapuslate the exi_list inside the
 106  * exi structure, we can't do that for the auth_list.
 107  * So, to keep things looking clean, we keep them both
 108  * in these external lists.
 109  */
 110 typedef struct refreshq_exi_node {
 111         struct exportinfo       *ren_exi;
 112         list_t                  ren_authlist;
 113         list_node_t             ren_node;
 114 } refreshq_exi_node_t;
 115 
 116 typedef struct refreshq_auth_node {
 117         struct auth_cache       *ran_auth;
 118         char                    *ran_netid;
 119         list_node_t             ran_node;
 120 } refreshq_auth_node_t;
 121 
 122 /*
 123  * Used to manipulate things on the refreshq_queue.
 124  * Note that the refresh thread will effectively
 125  * 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
 134  * module, then nfsauth_fini() needs to be called
 135  * to remove state. In that event, since the
 136  * refreshq thread has been started, they need to
 137  * work together to get rid of state.
 138  */
 139 typedef enum nfsauth_refreshq_thread_state {
 140         REFRESHQ_THREAD_RUNNING,
 141         REFRESHQ_THREAD_FINI_REQ,
 142         REFRESHQ_THREAD_HALTED

 143 } nfsauth_refreshq_thread_state_t;
 144 
 145 nfsauth_refreshq_thread_state_t
 146 refreshq_thread_state = REFRESHQ_THREAD_HALTED;

 147 
























 148 static void nfsauth_free_node(struct auth_cache *);
 149 static void nfsauth_refresh_thread(void);
 150 
 151 static int nfsauth_cache_compar(const void *, const void *);
 152 
 153 /*
 154  * mountd is a server-side only daemon. This will need to be
 155  * revisited if the NFS server is ever made zones-aware.
 156  */
 157 kmutex_t        mountd_lock;
 158 door_handle_t   mountd_dh;


 159 
 160 void
 161 mountd_args(uint_t did)
 162 {
 163         mutex_enter(&mountd_lock);
 164         if (mountd_dh != NULL)
 165                 door_ki_rele(mountd_dh);
 166         mountd_dh = door_ki_lookup(did);
 167         mutex_exit(&mountd_lock);



 168 }
 169 
 170 void
 171 nfsauth_init(void)
 172 {
 173         /*
 174          * mountd can be restarted by smf(5). We need to make sure
 175          * the updated door handle will safely make it to mountd_dh
 176          */
 177         mutex_init(&mountd_lock, NULL, MUTEX_DEFAULT, NULL);
 178 
 179         mutex_init(&refreshq_lock, NULL, MUTEX_DEFAULT, NULL);
 180         list_create(&refreshq_queue, sizeof (refreshq_exi_node_t),
 181             offsetof(refreshq_exi_node_t, ren_node));


 182 
 183         cv_init(&refreshq_cv, NULL, CV_DEFAULT, NULL);



 184 


 185         /*
 186          * Allocate nfsauth cache handle

 187          */
 188         exi_cache_handle = kmem_cache_create("exi_cache_handle",
 189             sizeof (struct auth_cache), 0, NULL, NULL,
 190             exi_cache_reclaim, NULL, NULL, 0);



 191 
 192         refreshq_thread_state = REFRESHQ_THREAD_RUNNING;
 193         (void) zthread_create(NULL, 0, nfsauth_refresh_thread,
 194             NULL, 0, minclsyspri);
 195 }
 196 
 197 /*
 198  * Finalization routine for nfsauth. It is important to call this routine
 199  * before destroying the exported_lock.
 200  */
 201 void
 202 nfsauth_fini(void)
 203 {
 204         refreshq_exi_node_t     *ren;

 205 
 206         /*
 207          * Prevent the nfsauth_refresh_thread from getting new
 208          * work.
 209          */
 210         mutex_enter(&refreshq_lock);
 211         if (refreshq_thread_state != REFRESHQ_THREAD_HALTED) {
 212                 refreshq_thread_state = REFRESHQ_THREAD_FINI_REQ;
 213                 cv_broadcast(&refreshq_cv);
 214 
 215                 /*
 216                  * Also, wait for nfsauth_refresh_thread() to exit.
 217                  */
 218                 while (refreshq_thread_state != REFRESHQ_THREAD_HALTED) {
 219                         cv_wait(&refreshq_cv, &refreshq_lock);
 220                 }
 221         }
 222         mutex_exit(&refreshq_lock);
 223 
 224         /*
 225          * Walk the exi_list and in turn, walk the auth_lists and free all
 226          * lists.  In addition, free INVALID auth_cache entries.
 227          */
 228         while ((ren = list_remove_head(&refreshq_queue))) {
 229                 refreshq_auth_node_t *ran;
 230 
 231                 while ((ran = list_remove_head(&ren->ren_authlist)) != NULL) {
 232                         struct auth_cache *p = ran->ran_auth;
 233                         if (p->auth_state == NFS_AUTH_INVALID)
 234                                 nfsauth_free_node(p);
 235                         strfree(ran->ran_netid);
 236                         kmem_free(ran, sizeof (refreshq_auth_node_t));
 237                 }
 238 
 239                 list_destroy(&ren->ren_authlist);
 240                 exi_rele(ren->ren_exi);
 241                 kmem_free(ren, sizeof (refreshq_exi_node_t));
 242         }
 243         list_destroy(&refreshq_queue);
 244 
 245         cv_destroy(&refreshq_cv);
 246         mutex_destroy(&refreshq_lock);


 247 
 248         mutex_destroy(&mountd_lock);
 249 
 250         /*
 251          * Deallocate nfsauth cache handle
 252          */
 253         kmem_cache_destroy(exi_cache_handle);




 254 }
 255 
 256 /*
 257  * Convert the address in a netbuf to
 258  * a hash index for the auth_cache table.
 259  */
 260 static int
 261 hash(struct netbuf *a)
 262 {
 263         int i, h = 0;
 264 
 265         for (i = 0; i < a->len; i++)
 266                 h ^= a->buf[i];
 267 
 268         return (h & (AUTH_TABLESIZE - 1));
 269 }
 270 
 271 /*
 272  * Mask out the components of an
 273  * address that do not identify


 325 static void
 326 sys_log(const char *msg)
 327 {
 328         static time_t   tstamp = 0;
 329         time_t          now;
 330 
 331         /*
 332          * msg is shown (at most) once per minute
 333          */
 334         now = gethrestime_sec();
 335         if ((tstamp + 60) < now) {
 336                 tstamp = now;
 337                 cmn_err(CE_WARN, msg);
 338         }
 339 }
 340 
 341 /*
 342  * Callup to the mountd to get access information in the kernel.
 343  */
 344 static bool_t
 345 nfsauth_retrieve(struct exportinfo *exi, char *req_netid, int flavor,
 346     struct netbuf *addr, int *access, cred_t *clnt_cred, uid_t *srv_uid,
 347     gid_t *srv_gid, uint_t *srv_gids_cnt, gid_t **srv_gids)

 348 {
 349         varg_t                    varg = {0};
 350         nfsauth_res_t             res = {0};
 351         XDR                       xdrs;
 352         size_t                    absz;
 353         caddr_t                   abuf;
 354         int                       last = 0;
 355         door_arg_t                da;
 356         door_info_t               di;
 357         door_handle_t             dh;
 358         uint_t                    ntries = 0;
 359 
 360         /*
 361          * No entry in the cache for this client/flavor
 362          * so we need to call the nfsauth service in the
 363          * mount daemon.
 364          */
 365 
 366         varg.vers = V_PROTO;
 367         varg.arg_u.arg.cmd = NFSAUTH_ACCESS;


 400         XDR_DESTROY(&xdrs);
 401 
 402         /*
 403          * Prepare the door arguments
 404          *
 405          * We don't know the size of the message the daemon
 406          * will pass back to us.  By setting rbuf to NULL,
 407          * we force the door code to allocate a buf of the
 408          * appropriate size.  We must set rsize > 0, however,
 409          * else the door code acts as if no response was
 410          * expected and doesn't pass the data to us.
 411          */
 412         da.data_ptr = (char *)abuf;
 413         da.data_size = absz;
 414         da.desc_ptr = NULL;
 415         da.desc_num = 0;
 416         da.rbuf = NULL;
 417         da.rsize = 1;
 418 
 419 retry:
 420         mutex_enter(&mountd_lock);
 421         dh = mountd_dh;
 422         if (dh != NULL)
 423                 door_ki_hold(dh);
 424         mutex_exit(&mountd_lock);
 425 
 426         if (dh == NULL) {
 427                 /*
 428                  * The rendezvous point has not been established yet!
 429                  * This could mean that either mountd(1m) has not yet
 430                  * been started or that _this_ routine nuked the door
 431                  * handle after receiving an EINTR for a REVOKED door.
 432                  *
 433                  * Returning NFSAUTH_DROP will cause the NFS client
 434                  * to retransmit the request, so let's try to be more
 435                  * rescillient and attempt for ntries before we bail.
 436                  */
 437                 if (++ntries % NFSAUTH_DR_TRYCNT) {
 438                         delay(hz);
 439                         goto retry;
 440                 }
 441 
 442                 kmem_free(abuf, absz);
 443 
 444                 sys_log("nfsauth: mountd has not established door");


 474                 /*
 475                  * Server out of resources; back off for a bit
 476                  */
 477                 door_ki_rele(dh);
 478                 delay(hz);
 479                 goto retry;
 480                 /* NOTREACHED */
 481 
 482         case EINTR:
 483                 if (!door_ki_info(dh, &di)) {
 484                         door_ki_rele(dh);
 485 
 486                         if (di.di_attributes & DOOR_REVOKED) {
 487                                 /*
 488                                  * The server barfed and revoked
 489                                  * the (existing) door on us; we
 490                                  * want to wait to give smf(5) a
 491                                  * chance to restart mountd(1m)
 492                                  * and establish a new door handle.
 493                                  */
 494                                 mutex_enter(&mountd_lock);
 495                                 if (dh == mountd_dh) {
 496                                         door_ki_rele(mountd_dh);
 497                                         mountd_dh = NULL;
 498                                 }
 499                                 mutex_exit(&mountd_lock);
 500                                 delay(hz);
 501                                 goto retry;
 502                         }
 503                         /*
 504                          * If the door was _not_ revoked on us,
 505                          * then more than likely we took an INTR,
 506                          * so we need to fail the operation.
 507                          */
 508                         goto fail;
 509                 }
 510                 /*
 511                  * The only failure that can occur from getting
 512                  * the door info is EINVAL, so we let the code
 513                  * below handle it.
 514                  */
 515                 /* FALLTHROUGH */
 516 
 517         case EBADF:
 518         case EINVAL:
 519         default:


 576 
 577                 case NFSAUTH_DR_EFAIL:
 578                 case NFSAUTH_DR_DECERR:
 579                 case NFSAUTH_DR_BADCMD:
 580                 default:
 581                         xdr_free(xdr_nfsauth_res, (char *)&res);
 582 fail:
 583                         *access = NFSAUTH_DENIED;
 584                         kmem_free(abuf, absz);
 585                         return (FALSE);
 586                         /* NOTREACHED */
 587         }
 588 
 589         xdr_free(xdr_nfsauth_res, (char *)&res);
 590         kmem_free(abuf, absz);
 591 
 592         return (TRUE);
 593 }
 594 
 595 static void
 596 nfsauth_refresh_thread(void)
 597 {
 598         refreshq_exi_node_t     *ren;
 599         refreshq_auth_node_t    *ran;
 600 
 601         struct exportinfo       *exi;
 602 
 603         int                     access;
 604         bool_t                  retrieval;
 605 
 606         callb_cpr_t             cprinfo;
 607 
 608         CALLB_CPR_INIT(&cprinfo, &refreshq_lock, callb_generic_cpr,
 609             "nfsauth_refresh");
 610 
 611         for (;;) {
 612                 mutex_enter(&refreshq_lock);
 613                 if (refreshq_thread_state != REFRESHQ_THREAD_RUNNING) {
 614                         /* Keep the hold on the lock! */
 615                         break;
 616                 }
 617 
 618                 ren = list_remove_head(&refreshq_queue);
 619                 if (ren == NULL) {
 620                         CALLB_CPR_SAFE_BEGIN(&cprinfo);
 621                         cv_wait(&refreshq_cv, &refreshq_lock);
 622                         CALLB_CPR_SAFE_END(&cprinfo, &refreshq_lock);
 623                         mutex_exit(&refreshq_lock);
 624                         continue;
 625                 }
 626                 mutex_exit(&refreshq_lock);
 627 
 628                 exi = ren->ren_exi;
 629                 ASSERT(exi != NULL);
 630 
 631                 /*
 632                  * Since the ren was removed from the refreshq_queue above,
 633                  * this is the only thread aware about the ren existence, so we
 634                  * have the exclusive ownership of it and we do not need to
 635                  * protect it by any lock.
 636                  */
 637                 while ((ran = list_remove_head(&ren->ren_authlist))) {
 638                         uid_t uid;
 639                         gid_t gid;
 640                         uint_t ngids;
 641                         gid_t *gids;
 642                         struct auth_cache *p = ran->ran_auth;
 643                         char *netid = ran->ran_netid;
 644 
 645                         ASSERT(p != NULL);
 646                         ASSERT(netid != NULL);


 653                          * Once the entry goes INVALID, it can not change
 654                          * state.
 655                          *
 656                          * No need to refresh entries also in a case we are
 657                          * just shutting down.
 658                          *
 659                          * In general, there is no need to hold the
 660                          * refreshq_lock to test the refreshq_thread_state.  We
 661                          * do hold it at other places because there is some
 662                          * related thread synchronization (or some other tasks)
 663                          * close to the refreshq_thread_state check.
 664                          *
 665                          * The check for the refreshq_thread_state value here
 666                          * is purely advisory to allow the faster
 667                          * nfsauth_refresh_thread() shutdown.  In a case we
 668                          * will miss such advisory, nothing catastrophic
 669                          * happens: we will just spin longer here before the
 670                          * shutdown.
 671                          */
 672                         if (p->auth_state == NFS_AUTH_INVALID ||
 673                             refreshq_thread_state != REFRESHQ_THREAD_RUNNING) {

 674                                 mutex_exit(&p->auth_lock);
 675 
 676                                 if (p->auth_state == NFS_AUTH_INVALID)
 677                                         nfsauth_free_node(p);
 678 
 679                                 strfree(netid);
 680 
 681                                 continue;
 682                         }
 683 
 684                         /*
 685                          * Make sure the state is valid.  Note that once we
 686                          * change the state to NFS_AUTH_REFRESHING, no other
 687                          * thread will be able to work on this entry.
 688                          */
 689                         ASSERT(p->auth_state == NFS_AUTH_STALE);
 690 
 691                         p->auth_state = NFS_AUTH_REFRESHING;
 692                         mutex_exit(&p->auth_lock);
 693 
 694                         DTRACE_PROBE2(nfsauth__debug__cache__refresh,
 695                             struct exportinfo *, exi,
 696                             struct auth_cache *, p);
 697 
 698                         /*
 699                          * The first caching of the access rights
 700                          * is done with the netid pulled out of the
 701                          * request from the client. All subsequent
 702                          * users of the cache may or may not have
 703                          * the same netid. It doesn't matter. So
 704                          * when we refresh, we simply use the netid
 705                          * of the request which triggered the
 706                          * refresh attempt.
 707                          */
 708                         retrieval = nfsauth_retrieve(exi, netid,
 709                             p->auth_flavor, &p->auth_clnt->authc_addr, &access,
 710                             p->auth_clnt_cred, &uid, &gid, &ngids, &gids);
 711 
 712                         /*
 713                          * This can only be set in one other place
 714                          * and the state has to be NFS_AUTH_FRESH.
 715                          */
 716                         strfree(netid);
 717 
 718                         mutex_enter(&p->auth_lock);
 719                         if (p->auth_state == NFS_AUTH_INVALID) {
 720                                 mutex_exit(&p->auth_lock);
 721                                 nfsauth_free_node(p);
 722                                 if (retrieval == TRUE)
 723                                         kmem_free(gids, ngids * sizeof (gid_t));
 724                         } else {
 725                                 /*
 726                                  * If we got an error, do not reset the
 727                                  * time. This will cause the next access
 728                                  * check for the client to reschedule this


 735                                         p->auth_srv_gid = gid;
 736                                         kmem_free(p->auth_srv_gids,
 737                                             p->auth_srv_ngids * sizeof (gid_t));
 738                                         p->auth_srv_ngids = ngids;
 739                                         p->auth_srv_gids = gids;
 740 
 741                                         p->auth_freshness = gethrestime_sec();
 742                                 }
 743                                 p->auth_state = NFS_AUTH_FRESH;
 744 
 745                                 cv_broadcast(&p->auth_cv);
 746                                 mutex_exit(&p->auth_lock);
 747                         }
 748                 }
 749 
 750                 list_destroy(&ren->ren_authlist);
 751                 exi_rele(ren->ren_exi);
 752                 kmem_free(ren, sizeof (refreshq_exi_node_t));
 753         }
 754 
 755         refreshq_thread_state = REFRESHQ_THREAD_HALTED;
 756         cv_broadcast(&refreshq_cv);
 757         CALLB_CPR_EXIT(&cprinfo);

 758         zthread_exit();
 759 }
 760 
 761 int
 762 nfsauth_cache_clnt_compar(const void *v1, const void *v2)
 763 {
 764         int c;
 765 
 766         const struct auth_cache_clnt *a1 = (const struct auth_cache_clnt *)v1;
 767         const struct auth_cache_clnt *a2 = (const struct auth_cache_clnt *)v2;
 768 
 769         if (a1->authc_addr.len < a2->authc_addr.len)
 770                 return (-1);
 771         if (a1->authc_addr.len > a2->authc_addr.len)
 772                 return (1);
 773 
 774         c = memcmp(a1->authc_addr.buf, a2->authc_addr.buf, a1->authc_addr.len);
 775         if (c < 0)
 776                 return (-1);
 777         if (c > 0)


 809                 return (1);
 810 
 811         c = memcmp(crgetgroups(a1->auth_clnt_cred),
 812             crgetgroups(a2->auth_clnt_cred), crgetngroups(a1->auth_clnt_cred));
 813         if (c < 0)
 814                 return (-1);
 815         if (c > 0)
 816                 return (1);
 817 
 818         return (0);
 819 }
 820 
 821 /*
 822  * Get the access information from the cache or callup to the mountd
 823  * to get and cache the access information in the kernel.
 824  */
 825 static int
 826 nfsauth_cache_get(struct exportinfo *exi, struct svc_req *req, int flavor,
 827     cred_t *cr, uid_t *uid, gid_t *gid, uint_t *ngids, gid_t **gids)
 828 {

 829         struct netbuf           *taddrmask;
 830         struct netbuf           addr;   /* temporary copy of client's address */
 831         const struct netbuf     *claddr;
 832         avl_tree_t              *tree;
 833         struct auth_cache       ac;     /* used as a template for avl_find() */
 834         struct auth_cache_clnt  *c;
 835         struct auth_cache_clnt  acc;    /* used as a template for avl_find() */
 836         struct auth_cache       *p = NULL;
 837         int                     access;
 838 
 839         uid_t                   tmpuid;
 840         gid_t                   tmpgid;
 841         uint_t                  tmpngids;
 842         gid_t                   *tmpgids;
 843 
 844         avl_index_t             where;  /* used for avl_find()/avl_insert() */
 845 
 846         ASSERT(cr != NULL);
 847 



 848         /*
 849          * Now check whether this client already
 850          * has an entry for this flavor in the cache
 851          * for this export.
 852          * Get the caller's address, mask off the
 853          * parts of the address that do not identify
 854          * the host (port number, etc), and then hash
 855          * it to find the chain of cache entries.
 856          */
 857 
 858         claddr = svc_getrpccaller(req->rq_xprt);
 859         addr = *claddr;

 860         addr.buf = kmem_alloc(addr.maxlen, KM_SLEEP);
 861         bcopy(claddr->buf, addr.buf, claddr->len);



 862 
 863         SVC_GETADDRMASK(req->rq_xprt, SVC_TATTR_ADDRMASK, (void **)&taddrmask);
 864         ASSERT(taddrmask != NULL);
 865         addrmask(&addr, taddrmask);
 866 
 867         ac.auth_flavor = flavor;
 868         ac.auth_clnt_cred = crdup(cr);
 869 
 870         acc.authc_addr = addr;
 871 
 872         tree = exi->exi_cache[hash(&addr)];
 873 
 874         rw_enter(&exi->exi_cache_lock, RW_READER);
 875         c = (struct auth_cache_clnt *)avl_find(tree, &acc, NULL);
 876 
 877         if (c == NULL) {
 878                 struct auth_cache_clnt *nc;
 879 
 880                 rw_exit(&exi->exi_cache_lock);
 881 


 987 
 988         /*
 989          * If the cache entry is not valid yet, we need to retrieve the
 990          * info ourselves.
 991          */
 992         if (p->auth_state == NFS_AUTH_NEW) {
 993                 bool_t res;
 994                 /*
 995                  * NFS_AUTH_NEW is the default output auth_state value in a
 996                  * case we failed somewhere below.
 997                  */
 998                 auth_state_t state = NFS_AUTH_NEW;
 999 
1000                 p->auth_state = NFS_AUTH_WAITING;
1001                 mutex_exit(&p->auth_lock);
1002                 kmem_free(addr.buf, addr.maxlen);
1003                 addr = p->auth_clnt->authc_addr;
1004 
1005                 atomic_inc_uint(&nfsauth_cache_miss);
1006 
1007                 res = nfsauth_retrieve(exi, svc_getnetid(req->rq_xprt), flavor,
1008                     &addr, &access, cr, &tmpuid, &tmpgid, &tmpngids, &tmpgids);

1009 
1010                 p->auth_access = access;
1011                 p->auth_time = p->auth_freshness = gethrestime_sec();
1012 
1013                 if (res == TRUE) {
1014                         if (uid != NULL)
1015                                 *uid = tmpuid;
1016                         if (gid != NULL)
1017                                 *gid = tmpgid;
1018                         if (ngids != NULL && gids != NULL) {
1019                                 *ngids = tmpngids;
1020                                 *gids = tmpgids;
1021 
1022                                 /*
1023                                  * We need a copy of gids for the
1024                                  * auth_cache entry
1025                                  */
1026                                 tmpgids = kmem_alloc(tmpngids * sizeof (gid_t),
1027                                     KM_NOSLEEP | KM_NORMALPRI);
1028                                 if (tmpgids != NULL)


1073 
1074                 if ((refresh > NFSAUTH_CACHE_REFRESH) &&
1075                     p->auth_state == NFS_AUTH_FRESH) {
1076                         refreshq_auth_node_t *ran;
1077                         uint_t nacr;
1078 
1079                         p->auth_state = NFS_AUTH_STALE;
1080                         mutex_exit(&p->auth_lock);
1081 
1082                         nacr = atomic_inc_uint_nv(&nfsauth_cache_refresh);
1083                         DTRACE_PROBE3(nfsauth__debug__cache__stale,
1084                             struct exportinfo *, exi,
1085                             struct auth_cache *, p,
1086                             uint_t, nacr);
1087 
1088                         ran = kmem_alloc(sizeof (refreshq_auth_node_t),
1089                             KM_SLEEP);
1090                         ran->ran_auth = p;
1091                         ran->ran_netid = strdup(svc_getnetid(req->rq_xprt));
1092 
1093                         mutex_enter(&refreshq_lock);











1094                         /*
1095                          * We should not add a work queue
1096                          * item if the thread is not
1097                          * accepting them.
1098                          */
1099                         if (refreshq_thread_state == REFRESHQ_THREAD_RUNNING) {

1100                                 refreshq_exi_node_t *ren;
1101 
1102                                 /*
1103                                  * Is there an existing exi_list?
1104                                  */
1105                                 for (ren = list_head(&refreshq_queue);
1106                                     ren != NULL;
1107                                     ren = list_next(&refreshq_queue, ren)) {

1108                                         if (ren->ren_exi == exi) {
1109                                                 list_insert_tail(
1110                                                     &ren->ren_authlist, ran);
1111                                                 break;
1112                                         }
1113                                 }
1114 
1115                                 if (ren == NULL) {
1116                                         ren = kmem_alloc(
1117                                             sizeof (refreshq_exi_node_t),
1118                                             KM_SLEEP);
1119 
1120                                         exi_hold(exi);
1121                                         ren->ren_exi = exi;
1122 
1123                                         list_create(&ren->ren_authlist,
1124                                             sizeof (refreshq_auth_node_t),
1125                                             offsetof(refreshq_auth_node_t,
1126                                             ran_node));
1127 
1128                                         list_insert_tail(&ren->ren_authlist,
1129                                             ran);
1130                                         list_insert_tail(&refreshq_queue, ren);

1131                                 }
1132 
1133                                 cv_broadcast(&refreshq_cv);
1134                         } else {
1135                                 strfree(ran->ran_netid);
1136                                 kmem_free(ran, sizeof (refreshq_auth_node_t));
1137                         }
1138 
1139                         mutex_exit(&refreshq_lock);
1140                 } else {
1141                         mutex_exit(&p->auth_lock);
1142                 }
1143 
1144                 nach = atomic_inc_uint_nv(&nfsauth_cache_hit);
1145                 DTRACE_PROBE2(nfsauth__debug__cache__hit,
1146                     uint_t, nach,
1147                     time_t, refresh);
1148 
1149                 kmem_free(addr.buf, addr.maxlen);
1150         }
1151 
1152         return (access);
1153 
1154 retrieve:
1155         crfree(ac.auth_clnt_cred);
1156 
1157         /*
1158          * Retrieve the required data without caching.
1159          */
1160 
1161         ASSERT(p == NULL);
1162 
1163         atomic_inc_uint(&nfsauth_cache_miss);
1164 
1165         if (nfsauth_retrieve(exi, svc_getnetid(req->rq_xprt), flavor, &addr,
1166             &access, cr, &tmpuid, &tmpgid, &tmpngids, &tmpgids)) {
1167                 if (uid != NULL)
1168                         *uid = tmpuid;
1169                 if (gid != NULL)
1170                         *gid = tmpgid;
1171                 if (ngids != NULL && gids != NULL) {
1172                         *ngids = tmpngids;
1173                         *gids = tmpgids;
1174                 } else {
1175                         kmem_free(tmpgids, tmpngids * sizeof (gid_t));
1176                 }
1177         }
1178 
1179         kmem_free(addr.buf, addr.maxlen);
1180 
1181         return (access);
1182 }
1183 
1184 /*
1185  * Check if the requesting client has access to the filesystem with
1186  * a given nfs flavor number which is an explicitly shared flavor.


1393 nfsauth_cache_free(struct exportinfo *exi)
1394 {
1395         int i;
1396 
1397         /*
1398          * The only way we got here was with an exi_rele, which means that no
1399          * auth cache entry is being refreshed.
1400          */
1401 
1402         for (i = 0; i < AUTH_TABLESIZE; i++) {
1403                 avl_tree_t *tree = exi->exi_cache[i];
1404                 void *cookie = NULL;
1405                 struct auth_cache_clnt *node;
1406 
1407                 while ((node = avl_destroy_nodes(tree, &cookie)) != NULL)
1408                         nfsauth_free_clnt_node(node);
1409         }
1410 }
1411 
1412 /*
1413  * Called by the kernel memory allocator when
1414  * memory is low. Free unused cache entries.
1415  * If that's not enough, the VM system will
1416  * call again for some more.







1417  */
1418 /*ARGSUSED*/
1419 void
1420 exi_cache_reclaim(void *cdrarg)
1421 {
















1422         int i;
1423         struct exportinfo *exi;

1424 
1425         rw_enter(&exported_lock, RW_READER);
1426 
1427         for (i = 0; i < EXPTABLESIZE; i++) {
1428                 for (exi = exptable[i]; exi; exi = exi->fid_hash.next) {
1429                         exi_cache_trim(exi);
1430                 }
1431         }
1432 
1433         rw_exit(&exported_lock);
1434 
1435         atomic_inc_uint(&nfsauth_cache_reclaim);
1436 }
1437 
1438 void
1439 exi_cache_trim(struct exportinfo *exi)
1440 {
1441         struct auth_cache_clnt *c;
1442         struct auth_cache_clnt *nextc;
1443         struct auth_cache *p;
1444         struct auth_cache *next;
1445         int i;
1446         time_t stale_time;
1447         avl_tree_t *tree;
1448 
1449         for (i = 0; i < AUTH_TABLESIZE; i++) {
1450                 tree = exi->exi_cache[i];
1451                 stale_time = gethrestime_sec() - NFSAUTH_CACHE_TRIM;
1452                 rw_enter(&exi->exi_cache_lock, RW_READER);
1453 
1454                 /*
1455                  * Free entries that have not been
1456                  * used for NFSAUTH_CACHE_TRIM seconds.
1457                  */
1458                 for (c = avl_first(tree); c != NULL; c = AVL_NEXT(tree, c)) {




   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 (c) 2015 Joyent, Inc.  All rights reserved.
  26  * Copyright 2018 Nexenta Systems, Inc. All rights reserved.
  27  */
  28 
  29 #include <sys/param.h>
  30 #include <sys/errno.h>
  31 #include <sys/vfs.h>
  32 #include <sys/vnode.h>
  33 #include <sys/cred.h>
  34 #include <sys/cmn_err.h>
  35 #include <sys/systm.h>
  36 #include <sys/kmem.h>
  37 #include <sys/pathname.h>
  38 #include <sys/utsname.h>
  39 #include <sys/debug.h>
  40 #include <sys/door.h>
  41 #include <sys/sdt.h>
  42 #include <sys/thread.h>
  43 #include <sys/avl.h>
  44 
  45 #include <rpc/types.h>
  46 #include <rpc/auth.h>
  47 #include <rpc/clnt.h>
  48 
  49 #include <nfs/nfs.h>
  50 #include <nfs/export.h>
  51 #include <nfs/nfs_clnt.h>
  52 #include <nfs/auth.h>
  53 
  54 static struct kmem_cache *exi_cache_handle;
  55 static void exi_cache_reclaim(void *);
  56 static void exi_cache_reclaim_zone(nfs_globals_t *);
  57 static void exi_cache_trim(struct exportinfo *exi);
  58 
  59 extern pri_t minclsyspri;
  60 
  61 /* NFS auth cache statistics */
  62 volatile uint_t nfsauth_cache_hit;
  63 volatile uint_t nfsauth_cache_miss;
  64 volatile uint_t nfsauth_cache_refresh;
  65 volatile uint_t nfsauth_cache_reclaim;
  66 volatile uint_t exi_cache_auth_reclaim_failed;
  67 volatile uint_t exi_cache_clnt_reclaim_failed;
  68 
  69 /*
  70  * The lifetime of an auth cache entry:
  71  * ------------------------------------
  72  *
  73  * An auth cache entry is created with both the auth_time
  74  * and auth_freshness times set to the current time.
  75  *
  76  * Upon every client access which results in a hit, the
  77  * auth_time will be updated.
  78  *
  79  * If a client access determines that the auth_freshness
  80  * indicates that the entry is STALE, then it will be
  81  * refreshed. Note that this will explicitly reset


 105 
 106 /*
 107  * While we could encapuslate the exi_list inside the
 108  * exi structure, we can't do that for the auth_list.
 109  * So, to keep things looking clean, we keep them both
 110  * in these external lists.
 111  */
 112 typedef struct refreshq_exi_node {
 113         struct exportinfo       *ren_exi;
 114         list_t                  ren_authlist;
 115         list_node_t             ren_node;
 116 } refreshq_exi_node_t;
 117 
 118 typedef struct refreshq_auth_node {
 119         struct auth_cache       *ran_auth;
 120         char                    *ran_netid;
 121         list_node_t             ran_node;
 122 } refreshq_auth_node_t;
 123 
 124 /*
 125  * Used to manipulate things on the refreshq_queue.  Note that the refresh
 126  * thread will effectively pop a node off of the queue, at which point it

 127  * will no longer need to hold the mutex.
 128  */
 129 static kmutex_t refreshq_lock;
 130 static list_t refreshq_queue;
 131 static kcondvar_t refreshq_cv;
 132 
 133 /*
 134  * If there is ever a problem with loading the module, then nfsauth_fini()
 135  * needs to be called to remove state.  In that event, since the refreshq
 136  * thread has been started, they need to work together to get rid of state.


 137  */
 138 typedef enum nfsauth_refreshq_thread_state {
 139         REFRESHQ_THREAD_RUNNING,
 140         REFRESHQ_THREAD_FINI_REQ,
 141         REFRESHQ_THREAD_HALTED,
 142         REFRESHQ_THREAD_NEED_CREATE
 143 } nfsauth_refreshq_thread_state_t;
 144 
 145 typedef struct nfsauth_globals {
 146         kmutex_t        mountd_lock;
 147         door_handle_t   mountd_dh;
 148 
 149         /*
 150          * Used to manipulate things on the refreshq_queue.  Note that the
 151          * refresh thread will effectively pop a node off of the queue,
 152          * at which point it will no longer need to hold the mutex.
 153          */
 154         kmutex_t        refreshq_lock;
 155         list_t          refreshq_queue;
 156         kcondvar_t      refreshq_cv;
 157 
 158         /*
 159          * A list_t would be overkill.  These are auth_cache entries which are
 160          * no longer linked to an exi.  It should be the case that all of their
 161          * states are NFS_AUTH_INVALID, i.e., the only way to be put on this
 162          * list is iff their state indicated that they had been placed on the
 163          * refreshq_queue.
 164          *
 165          * Note that while there is no link from the exi or back to the exi,
 166          * the exi can not go away until these entries are harvested.
 167          */
 168         struct auth_cache               *refreshq_dead_entries;
 169         nfsauth_refreshq_thread_state_t refreshq_thread_state;
 170 
 171 } nfsauth_globals_t;
 172 
 173 static void nfsauth_free_node(struct auth_cache *);
 174 static void nfsauth_refresh_thread(nfsauth_globals_t *);
 175 
 176 static int nfsauth_cache_compar(const void *, const void *);
 177 
 178 static nfsauth_globals_t *
 179 nfsauth_get_zg(void)
 180 {
 181         nfs_globals_t *ng = nfs_srv_getzg();
 182         nfsauth_globals_t *nag = ng->nfs_auth;
 183         ASSERT(nag != NULL);
 184         return (nag);
 185 }
 186 
 187 void
 188 mountd_args(uint_t did)
 189 {
 190         nfsauth_globals_t *nag;
 191 
 192         nag = nfsauth_get_zg();
 193         mutex_enter(&nag->mountd_lock);
 194         if (nag->mountd_dh != NULL)
 195                 door_ki_rele(nag->mountd_dh);
 196         nag->mountd_dh = door_ki_lookup(did);
 197         mutex_exit(&nag->mountd_lock);
 198 }
 199 
 200 void
 201 nfsauth_init(void)
 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 void
 215 nfsauth_zone_init(nfs_globals_t *ng)
 216 {
 217         nfsauth_globals_t *nag;
 218 
 219         nag = kmem_zalloc(sizeof (*nag), KM_SLEEP);
 220 
 221         /*
 222          * mountd can be restarted by smf(5).  We need to make sure
 223          * the updated door handle will safely make it to mountd_dh.
 224          */
 225         mutex_init(&nag->mountd_lock, NULL, MUTEX_DEFAULT, NULL);
 226         mutex_init(&nag->refreshq_lock, NULL, MUTEX_DEFAULT, NULL);
 227         list_create(&nag->refreshq_queue, sizeof (refreshq_exi_node_t),
 228             offsetof(refreshq_exi_node_t, ren_node));
 229         cv_init(&nag->refreshq_cv, NULL, CV_DEFAULT, NULL);
 230         nag->refreshq_thread_state = REFRESHQ_THREAD_NEED_CREATE;
 231 
 232         ng->nfs_auth = nag;


 233 }
 234 




 235 void
 236 nfsauth_zone_shutdown(nfs_globals_t *ng)
 237 {
 238         refreshq_exi_node_t     *ren;
 239         nfsauth_globals_t       *nag = ng->nfs_auth;
 240 
 241         /* Prevent the nfsauth_refresh_thread from getting new work */
 242         mutex_enter(&nag->refreshq_lock);
 243         if (nag->refreshq_thread_state == REFRESHQ_THREAD_RUNNING) {
 244                 nag->refreshq_thread_state = REFRESHQ_THREAD_FINI_REQ;
 245                 cv_broadcast(&nag->refreshq_cv);



 246 
 247                 /* Wait for nfsauth_refresh_thread() to exit */
 248                 while (nag->refreshq_thread_state != REFRESHQ_THREAD_HALTED)
 249                         cv_wait(&nag->refreshq_cv, &nag->refreshq_lock);


 250         }
 251         mutex_exit(&nag->refreshq_lock);

 252 
 253         /*
 254          * Walk the exi_list and in turn, walk the auth_lists and free all
 255          * lists.  In addition, free INVALID auth_cache entries.
 256          */
 257         while ((ren = list_remove_head(&nag->refreshq_queue))) {
 258                 refreshq_auth_node_t *ran;
 259 
 260                 while ((ran = list_remove_head(&ren->ren_authlist)) != NULL) {
 261                         struct auth_cache *p = ran->ran_auth;
 262                         if (p->auth_state == NFS_AUTH_INVALID)
 263                                 nfsauth_free_node(p);
 264                         strfree(ran->ran_netid);
 265                         kmem_free(ran, sizeof (*ran));
 266                 }
 267 
 268                 list_destroy(&ren->ren_authlist);
 269                 exi_rele(ren->ren_exi);
 270                 kmem_free(ren, sizeof (*ren));
 271         }
 272 }
 273 
 274 void
 275 nfsauth_zone_fini(nfs_globals_t *ng)
 276 {
 277         nfsauth_globals_t *nag = ng->nfs_auth;
 278 
 279         ng->nfs_auth = NULL;
 280 
 281         list_destroy(&nag->refreshq_queue);
 282         cv_destroy(&nag->refreshq_cv);
 283         mutex_destroy(&nag->refreshq_lock);
 284         mutex_destroy(&nag->mountd_lock);
 285         /* Extra cleanup. */
 286         if (nag->mountd_dh != NULL)
 287                 door_ki_rele(nag->mountd_dh);
 288         kmem_free(nag, sizeof (*nag));
 289 }
 290 
 291 /*
 292  * Convert the address in a netbuf to
 293  * a hash index for the auth_cache table.
 294  */
 295 static int
 296 hash(struct netbuf *a)
 297 {
 298         int i, h = 0;
 299 
 300         for (i = 0; i < a->len; i++)
 301                 h ^= a->buf[i];
 302 
 303         return (h & (AUTH_TABLESIZE - 1));
 304 }
 305 
 306 /*
 307  * Mask out the components of an
 308  * address that do not identify


 360 static void
 361 sys_log(const char *msg)
 362 {
 363         static time_t   tstamp = 0;
 364         time_t          now;
 365 
 366         /*
 367          * msg is shown (at most) once per minute
 368          */
 369         now = gethrestime_sec();
 370         if ((tstamp + 60) < now) {
 371                 tstamp = now;
 372                 cmn_err(CE_WARN, msg);
 373         }
 374 }
 375 
 376 /*
 377  * Callup to the mountd to get access information in the kernel.
 378  */
 379 static bool_t
 380 nfsauth_retrieve(nfsauth_globals_t *nag, struct exportinfo *exi,
 381     char *req_netid, int flavor, struct netbuf *addr, int *access,
 382     cred_t *clnt_cred, uid_t *srv_uid, gid_t *srv_gid, uint_t *srv_gids_cnt,
 383     gid_t **srv_gids)
 384 {
 385         varg_t                    varg = {0};
 386         nfsauth_res_t             res = {0};
 387         XDR                       xdrs;
 388         size_t                    absz;
 389         caddr_t                   abuf;
 390         int                       last = 0;
 391         door_arg_t                da;
 392         door_info_t               di;
 393         door_handle_t             dh;
 394         uint_t                    ntries = 0;
 395 
 396         /*
 397          * No entry in the cache for this client/flavor
 398          * so we need to call the nfsauth service in the
 399          * mount daemon.
 400          */
 401 
 402         varg.vers = V_PROTO;
 403         varg.arg_u.arg.cmd = NFSAUTH_ACCESS;


 436         XDR_DESTROY(&xdrs);
 437 
 438         /*
 439          * Prepare the door arguments
 440          *
 441          * We don't know the size of the message the daemon
 442          * will pass back to us.  By setting rbuf to NULL,
 443          * we force the door code to allocate a buf of the
 444          * appropriate size.  We must set rsize > 0, however,
 445          * else the door code acts as if no response was
 446          * expected and doesn't pass the data to us.
 447          */
 448         da.data_ptr = (char *)abuf;
 449         da.data_size = absz;
 450         da.desc_ptr = NULL;
 451         da.desc_num = 0;
 452         da.rbuf = NULL;
 453         da.rsize = 1;
 454 
 455 retry:
 456         mutex_enter(&nag->mountd_lock);
 457         dh = nag->mountd_dh;
 458         if (dh != NULL)
 459                 door_ki_hold(dh);
 460         mutex_exit(&nag->mountd_lock);
 461 
 462         if (dh == NULL) {
 463                 /*
 464                  * The rendezvous point has not been established yet!
 465                  * This could mean that either mountd(1m) has not yet
 466                  * been started or that _this_ routine nuked the door
 467                  * handle after receiving an EINTR for a REVOKED door.
 468                  *
 469                  * Returning NFSAUTH_DROP will cause the NFS client
 470                  * to retransmit the request, so let's try to be more
 471                  * rescillient and attempt for ntries before we bail.
 472                  */
 473                 if (++ntries % NFSAUTH_DR_TRYCNT) {
 474                         delay(hz);
 475                         goto retry;
 476                 }
 477 
 478                 kmem_free(abuf, absz);
 479 
 480                 sys_log("nfsauth: mountd has not established door");


 510                 /*
 511                  * Server out of resources; back off for a bit
 512                  */
 513                 door_ki_rele(dh);
 514                 delay(hz);
 515                 goto retry;
 516                 /* NOTREACHED */
 517 
 518         case EINTR:
 519                 if (!door_ki_info(dh, &di)) {
 520                         door_ki_rele(dh);
 521 
 522                         if (di.di_attributes & DOOR_REVOKED) {
 523                                 /*
 524                                  * The server barfed and revoked
 525                                  * the (existing) door on us; we
 526                                  * want to wait to give smf(5) a
 527                                  * chance to restart mountd(1m)
 528                                  * and establish a new door handle.
 529                                  */
 530                                 mutex_enter(&nag->mountd_lock);
 531                                 if (dh == nag->mountd_dh) {
 532                                         door_ki_rele(nag->mountd_dh);
 533                                         nag->mountd_dh = NULL;
 534                                 }
 535                                 mutex_exit(&nag->mountd_lock);
 536                                 delay(hz);
 537                                 goto retry;
 538                         }
 539                         /*
 540                          * If the door was _not_ revoked on us,
 541                          * then more than likely we took an INTR,
 542                          * so we need to fail the operation.
 543                          */
 544                         goto fail;
 545                 }
 546                 /*
 547                  * The only failure that can occur from getting
 548                  * the door info is EINVAL, so we let the code
 549                  * below handle it.
 550                  */
 551                 /* FALLTHROUGH */
 552 
 553         case EBADF:
 554         case EINVAL:
 555         default:


 612 
 613                 case NFSAUTH_DR_EFAIL:
 614                 case NFSAUTH_DR_DECERR:
 615                 case NFSAUTH_DR_BADCMD:
 616                 default:
 617                         xdr_free(xdr_nfsauth_res, (char *)&res);
 618 fail:
 619                         *access = NFSAUTH_DENIED;
 620                         kmem_free(abuf, absz);
 621                         return (FALSE);
 622                         /* NOTREACHED */
 623         }
 624 
 625         xdr_free(xdr_nfsauth_res, (char *)&res);
 626         kmem_free(abuf, absz);
 627 
 628         return (TRUE);
 629 }
 630 
 631 static void
 632 nfsauth_refresh_thread(nfsauth_globals_t *nag)
 633 {
 634         refreshq_exi_node_t     *ren;
 635         refreshq_auth_node_t    *ran;
 636 
 637         struct exportinfo       *exi;
 638 
 639         int                     access;
 640         bool_t                  retrieval;
 641 
 642         callb_cpr_t             cprinfo;
 643 
 644         CALLB_CPR_INIT(&cprinfo, &nag->refreshq_lock, callb_generic_cpr,
 645             "nfsauth_refresh");
 646 
 647         for (;;) {
 648                 mutex_enter(&nag->refreshq_lock);
 649                 if (nag->refreshq_thread_state != REFRESHQ_THREAD_RUNNING) {
 650                         /* Keep the hold on the lock! */
 651                         break;
 652                 }
 653 
 654                 ren = list_remove_head(&nag->refreshq_queue);
 655                 if (ren == NULL) {
 656                         CALLB_CPR_SAFE_BEGIN(&cprinfo);
 657                         cv_wait(&nag->refreshq_cv, &nag->refreshq_lock);
 658                         CALLB_CPR_SAFE_END(&cprinfo, &nag->refreshq_lock);
 659                         mutex_exit(&nag->refreshq_lock);
 660                         continue;
 661                 }
 662                 mutex_exit(&nag->refreshq_lock);
 663 
 664                 exi = ren->ren_exi;
 665                 ASSERT(exi != NULL);
 666 
 667                 /*
 668                  * Since the ren was removed from the refreshq_queue above,
 669                  * this is the only thread aware about the ren existence, so we
 670                  * have the exclusive ownership of it and we do not need to
 671                  * protect it by any lock.
 672                  */
 673                 while ((ran = list_remove_head(&ren->ren_authlist))) {
 674                         uid_t uid;
 675                         gid_t gid;
 676                         uint_t ngids;
 677                         gid_t *gids;
 678                         struct auth_cache *p = ran->ran_auth;
 679                         char *netid = ran->ran_netid;
 680 
 681                         ASSERT(p != NULL);
 682                         ASSERT(netid != NULL);


 689                          * Once the entry goes INVALID, it can not change
 690                          * state.
 691                          *
 692                          * No need to refresh entries also in a case we are
 693                          * just shutting down.
 694                          *
 695                          * In general, there is no need to hold the
 696                          * refreshq_lock to test the refreshq_thread_state.  We
 697                          * do hold it at other places because there is some
 698                          * related thread synchronization (or some other tasks)
 699                          * close to the refreshq_thread_state check.
 700                          *
 701                          * The check for the refreshq_thread_state value here
 702                          * is purely advisory to allow the faster
 703                          * nfsauth_refresh_thread() shutdown.  In a case we
 704                          * will miss such advisory, nothing catastrophic
 705                          * happens: we will just spin longer here before the
 706                          * shutdown.
 707                          */
 708                         if (p->auth_state == NFS_AUTH_INVALID ||
 709                             nag->refreshq_thread_state !=
 710                             REFRESHQ_THREAD_RUNNING) {
 711                                 mutex_exit(&p->auth_lock);
 712 
 713                                 if (p->auth_state == NFS_AUTH_INVALID)
 714                                         nfsauth_free_node(p);
 715 
 716                                 strfree(netid);
 717 
 718                                 continue;
 719                         }
 720 
 721                         /*
 722                          * Make sure the state is valid.  Note that once we
 723                          * change the state to NFS_AUTH_REFRESHING, no other
 724                          * thread will be able to work on this entry.
 725                          */
 726                         ASSERT(p->auth_state == NFS_AUTH_STALE);
 727 
 728                         p->auth_state = NFS_AUTH_REFRESHING;
 729                         mutex_exit(&p->auth_lock);
 730 
 731                         DTRACE_PROBE2(nfsauth__debug__cache__refresh,
 732                             struct exportinfo *, exi,
 733                             struct auth_cache *, p);
 734 
 735                         /*
 736                          * The first caching of the access rights
 737                          * is done with the netid pulled out of the
 738                          * request from the client. All subsequent
 739                          * users of the cache may or may not have
 740                          * the same netid. It doesn't matter. So
 741                          * when we refresh, we simply use the netid
 742                          * of the request which triggered the
 743                          * refresh attempt.
 744                          */
 745                         retrieval = nfsauth_retrieve(nag, exi, netid,
 746                             p->auth_flavor, &p->auth_clnt->authc_addr, &access,
 747                             p->auth_clnt_cred, &uid, &gid, &ngids, &gids);
 748 
 749                         /*
 750                          * This can only be set in one other place
 751                          * and the state has to be NFS_AUTH_FRESH.
 752                          */
 753                         strfree(netid);
 754 
 755                         mutex_enter(&p->auth_lock);
 756                         if (p->auth_state == NFS_AUTH_INVALID) {
 757                                 mutex_exit(&p->auth_lock);
 758                                 nfsauth_free_node(p);
 759                                 if (retrieval == TRUE)
 760                                         kmem_free(gids, ngids * sizeof (gid_t));
 761                         } else {
 762                                 /*
 763                                  * If we got an error, do not reset the
 764                                  * time. This will cause the next access
 765                                  * check for the client to reschedule this


 772                                         p->auth_srv_gid = gid;
 773                                         kmem_free(p->auth_srv_gids,
 774                                             p->auth_srv_ngids * sizeof (gid_t));
 775                                         p->auth_srv_ngids = ngids;
 776                                         p->auth_srv_gids = gids;
 777 
 778                                         p->auth_freshness = gethrestime_sec();
 779                                 }
 780                                 p->auth_state = NFS_AUTH_FRESH;
 781 
 782                                 cv_broadcast(&p->auth_cv);
 783                                 mutex_exit(&p->auth_lock);
 784                         }
 785                 }
 786 
 787                 list_destroy(&ren->ren_authlist);
 788                 exi_rele(ren->ren_exi);
 789                 kmem_free(ren, sizeof (refreshq_exi_node_t));
 790         }
 791 
 792         nag->refreshq_thread_state = REFRESHQ_THREAD_HALTED;
 793         cv_broadcast(&nag->refreshq_cv);
 794         CALLB_CPR_EXIT(&cprinfo);
 795         DTRACE_PROBE(nfsauth__nfsauth__refresh__thread__exit);
 796         zthread_exit();
 797 }
 798 
 799 int
 800 nfsauth_cache_clnt_compar(const void *v1, const void *v2)
 801 {
 802         int c;
 803 
 804         const struct auth_cache_clnt *a1 = (const struct auth_cache_clnt *)v1;
 805         const struct auth_cache_clnt *a2 = (const struct auth_cache_clnt *)v2;
 806 
 807         if (a1->authc_addr.len < a2->authc_addr.len)
 808                 return (-1);
 809         if (a1->authc_addr.len > a2->authc_addr.len)
 810                 return (1);
 811 
 812         c = memcmp(a1->authc_addr.buf, a2->authc_addr.buf, a1->authc_addr.len);
 813         if (c < 0)
 814                 return (-1);
 815         if (c > 0)


 847                 return (1);
 848 
 849         c = memcmp(crgetgroups(a1->auth_clnt_cred),
 850             crgetgroups(a2->auth_clnt_cred), crgetngroups(a1->auth_clnt_cred));
 851         if (c < 0)
 852                 return (-1);
 853         if (c > 0)
 854                 return (1);
 855 
 856         return (0);
 857 }
 858 
 859 /*
 860  * Get the access information from the cache or callup to the mountd
 861  * to get and cache the access information in the kernel.
 862  */
 863 static int
 864 nfsauth_cache_get(struct exportinfo *exi, struct svc_req *req, int flavor,
 865     cred_t *cr, uid_t *uid, gid_t *gid, uint_t *ngids, gid_t **gids)
 866 {
 867         nfsauth_globals_t       *nag;
 868         struct netbuf           *taddrmask;
 869         struct netbuf           addr;   /* temporary copy of client's address */
 870         const struct netbuf     *claddr;
 871         avl_tree_t              *tree;
 872         struct auth_cache       ac;     /* used as a template for avl_find() */
 873         struct auth_cache_clnt  *c;
 874         struct auth_cache_clnt  acc;    /* used as a template for avl_find() */
 875         struct auth_cache       *p = NULL;
 876         int                     access;
 877 
 878         uid_t                   tmpuid;
 879         gid_t                   tmpgid;
 880         uint_t                  tmpngids;
 881         gid_t                   *tmpgids;
 882 
 883         avl_index_t             where;  /* used for avl_find()/avl_insert() */
 884 
 885         ASSERT(cr != NULL);
 886 
 887         ASSERT3P(curzone->zone_id, ==, exi->exi_zoneid);
 888         nag = nfsauth_get_zg();
 889 
 890         /*
 891          * Now check whether this client already
 892          * has an entry for this flavor in the cache
 893          * for this export.
 894          * Get the caller's address, mask off the
 895          * parts of the address that do not identify
 896          * the host (port number, etc), and then hash
 897          * it to find the chain of cache entries.
 898          */
 899 
 900         claddr = svc_getrpccaller(req->rq_xprt);
 901         addr = *claddr;
 902         if (claddr->len != 0) {
 903                 addr.buf = kmem_alloc(addr.maxlen, KM_SLEEP);
 904                 bcopy(claddr->buf, addr.buf, claddr->len);
 905         } else {
 906                 addr.buf = NULL;
 907         }
 908 
 909         SVC_GETADDRMASK(req->rq_xprt, SVC_TATTR_ADDRMASK, (void **)&taddrmask);
 910         ASSERT(taddrmask != NULL);
 911         addrmask(&addr, taddrmask);
 912 
 913         ac.auth_flavor = flavor;
 914         ac.auth_clnt_cred = crdup(cr);
 915 
 916         acc.authc_addr = addr;
 917 
 918         tree = exi->exi_cache[hash(&addr)];
 919 
 920         rw_enter(&exi->exi_cache_lock, RW_READER);
 921         c = (struct auth_cache_clnt *)avl_find(tree, &acc, NULL);
 922 
 923         if (c == NULL) {
 924                 struct auth_cache_clnt *nc;
 925 
 926                 rw_exit(&exi->exi_cache_lock);
 927 


1033 
1034         /*
1035          * If the cache entry is not valid yet, we need to retrieve the
1036          * info ourselves.
1037          */
1038         if (p->auth_state == NFS_AUTH_NEW) {
1039                 bool_t res;
1040                 /*
1041                  * NFS_AUTH_NEW is the default output auth_state value in a
1042                  * case we failed somewhere below.
1043                  */
1044                 auth_state_t state = NFS_AUTH_NEW;
1045 
1046                 p->auth_state = NFS_AUTH_WAITING;
1047                 mutex_exit(&p->auth_lock);
1048                 kmem_free(addr.buf, addr.maxlen);
1049                 addr = p->auth_clnt->authc_addr;
1050 
1051                 atomic_inc_uint(&nfsauth_cache_miss);
1052 
1053                 res = nfsauth_retrieve(nag, exi, svc_getnetid(req->rq_xprt),
1054                     flavor, &addr, &access, cr, &tmpuid, &tmpgid, &tmpngids,
1055                     &tmpgids);
1056 
1057                 p->auth_access = access;
1058                 p->auth_time = p->auth_freshness = gethrestime_sec();
1059 
1060                 if (res == TRUE) {
1061                         if (uid != NULL)
1062                                 *uid = tmpuid;
1063                         if (gid != NULL)
1064                                 *gid = tmpgid;
1065                         if (ngids != NULL && gids != NULL) {
1066                                 *ngids = tmpngids;
1067                                 *gids = tmpgids;
1068 
1069                                 /*
1070                                  * We need a copy of gids for the
1071                                  * auth_cache entry
1072                                  */
1073                                 tmpgids = kmem_alloc(tmpngids * sizeof (gid_t),
1074                                     KM_NOSLEEP | KM_NORMALPRI);
1075                                 if (tmpgids != NULL)


1120 
1121                 if ((refresh > NFSAUTH_CACHE_REFRESH) &&
1122                     p->auth_state == NFS_AUTH_FRESH) {
1123                         refreshq_auth_node_t *ran;
1124                         uint_t nacr;
1125 
1126                         p->auth_state = NFS_AUTH_STALE;
1127                         mutex_exit(&p->auth_lock);
1128 
1129                         nacr = atomic_inc_uint_nv(&nfsauth_cache_refresh);
1130                         DTRACE_PROBE3(nfsauth__debug__cache__stale,
1131                             struct exportinfo *, exi,
1132                             struct auth_cache *, p,
1133                             uint_t, nacr);
1134 
1135                         ran = kmem_alloc(sizeof (refreshq_auth_node_t),
1136                             KM_SLEEP);
1137                         ran->ran_auth = p;
1138                         ran->ran_netid = strdup(svc_getnetid(req->rq_xprt));
1139 
1140                         mutex_enter(&nag->refreshq_lock);
1141 
1142                         if (nag->refreshq_thread_state ==
1143                             REFRESHQ_THREAD_NEED_CREATE) {
1144                                 /* Launch nfsauth refresh thread */
1145                                 nag->refreshq_thread_state =
1146                                     REFRESHQ_THREAD_RUNNING;
1147                                 (void) zthread_create(NULL, 0,
1148                                     nfsauth_refresh_thread, nag, 0,
1149                                     minclsyspri);
1150                         }
1151 
1152                         /*
1153                          * We should not add a work queue item if the thread
1154                          * is not accepting them.

1155                          */
1156                         if (nag->refreshq_thread_state ==
1157                             REFRESHQ_THREAD_RUNNING) {
1158                                 refreshq_exi_node_t *ren;
1159 
1160                                 /*
1161                                  * Is there an existing exi_list?
1162                                  */
1163                                 for (ren = list_head(&nag->refreshq_queue);
1164                                     ren != NULL;
1165                                     ren = list_next(&nag->refreshq_queue,
1166                                     ren)) {
1167                                         if (ren->ren_exi == exi) {
1168                                                 list_insert_tail(
1169                                                     &ren->ren_authlist, ran);
1170                                                 break;
1171                                         }
1172                                 }
1173 
1174                                 if (ren == NULL) {
1175                                         ren = kmem_alloc(
1176                                             sizeof (refreshq_exi_node_t),
1177                                             KM_SLEEP);
1178 
1179                                         exi_hold(exi);
1180                                         ren->ren_exi = exi;
1181 
1182                                         list_create(&ren->ren_authlist,
1183                                             sizeof (refreshq_auth_node_t),
1184                                             offsetof(refreshq_auth_node_t,
1185                                             ran_node));
1186 
1187                                         list_insert_tail(&ren->ren_authlist,
1188                                             ran);
1189                                         list_insert_tail(&nag->refreshq_queue,
1190                                             ren);
1191                                 }
1192 
1193                                 cv_broadcast(&nag->refreshq_cv);
1194                         } else {
1195                                 strfree(ran->ran_netid);
1196                                 kmem_free(ran, sizeof (refreshq_auth_node_t));
1197                         }
1198 
1199                         mutex_exit(&nag->refreshq_lock);
1200                 } else {
1201                         mutex_exit(&p->auth_lock);
1202                 }
1203 
1204                 nach = atomic_inc_uint_nv(&nfsauth_cache_hit);
1205                 DTRACE_PROBE2(nfsauth__debug__cache__hit,
1206                     uint_t, nach,
1207                     time_t, refresh);
1208 
1209                 kmem_free(addr.buf, addr.maxlen);
1210         }
1211 
1212         return (access);
1213 
1214 retrieve:
1215         crfree(ac.auth_clnt_cred);
1216 
1217         /*
1218          * Retrieve the required data without caching.
1219          */
1220 
1221         ASSERT(p == NULL);
1222 
1223         atomic_inc_uint(&nfsauth_cache_miss);
1224 
1225         if (nfsauth_retrieve(nag, exi, svc_getnetid(req->rq_xprt), flavor,
1226             &addr, &access, cr, &tmpuid, &tmpgid, &tmpngids, &tmpgids)) {
1227                 if (uid != NULL)
1228                         *uid = tmpuid;
1229                 if (gid != NULL)
1230                         *gid = tmpgid;
1231                 if (ngids != NULL && gids != NULL) {
1232                         *ngids = tmpngids;
1233                         *gids = tmpgids;
1234                 } else {
1235                         kmem_free(tmpgids, tmpngids * sizeof (gid_t));
1236                 }
1237         }
1238 
1239         kmem_free(addr.buf, addr.maxlen);
1240 
1241         return (access);
1242 }
1243 
1244 /*
1245  * Check if the requesting client has access to the filesystem with
1246  * a given nfs flavor number which is an explicitly shared flavor.


1453 nfsauth_cache_free(struct exportinfo *exi)
1454 {
1455         int i;
1456 
1457         /*
1458          * The only way we got here was with an exi_rele, which means that no
1459          * auth cache entry is being refreshed.
1460          */
1461 
1462         for (i = 0; i < AUTH_TABLESIZE; i++) {
1463                 avl_tree_t *tree = exi->exi_cache[i];
1464                 void *cookie = NULL;
1465                 struct auth_cache_clnt *node;
1466 
1467                 while ((node = avl_destroy_nodes(tree, &cookie)) != NULL)
1468                         nfsauth_free_clnt_node(node);
1469         }
1470 }
1471 
1472 /*
1473  * Called by the kernel memory allocator when memory is low.
1474  * Free unused cache entries. If that's not enough, the VM system
1475  * will call again for some more.
1476  *
1477  * This needs to operate on all zones, so we take a reader lock
1478  * on the list of zones and walk the list.  This is OK here
1479  * becuase exi_cache_trim doesn't block or cause new objects
1480  * to be allocated (basically just frees lots of stuff).
1481  * Use care if nfssrv_globals_rwl is taken as reader in any
1482  * other cases because it will block nfs_server_zone_init
1483  * and nfs_server_zone_fini, which enter as writer.
1484  */
1485 /*ARGSUSED*/
1486 void
1487 exi_cache_reclaim(void *cdrarg)
1488 {
1489         nfs_globals_t *ng;
1490 
1491         rw_enter(&nfssrv_globals_rwl, RW_READER);
1492 
1493         ng = list_head(&nfssrv_globals_list);
1494         while (ng != NULL) {
1495                 exi_cache_reclaim_zone(ng);
1496                 ng = list_next(&nfssrv_globals_list, ng);
1497         }
1498 
1499         rw_exit(&nfssrv_globals_rwl);
1500 }
1501 
1502 static void
1503 exi_cache_reclaim_zone(nfs_globals_t *ng)
1504 {
1505         int i;
1506         struct exportinfo *exi;
1507         nfs_export_t *ne = ng->nfs_export;
1508 
1509         rw_enter(&ne->exported_lock, RW_READER);
1510 
1511         for (i = 0; i < EXPTABLESIZE; i++) {
1512                 for (exi = ne->exptable[i]; exi; exi = exi->fid_hash.next)
1513                         exi_cache_trim(exi);
1514         }

1515 
1516         rw_exit(&ne->exported_lock);
1517 
1518         atomic_inc_uint(&nfsauth_cache_reclaim);
1519 }
1520 
1521 static void
1522 exi_cache_trim(struct exportinfo *exi)
1523 {
1524         struct auth_cache_clnt *c;
1525         struct auth_cache_clnt *nextc;
1526         struct auth_cache *p;
1527         struct auth_cache *next;
1528         int i;
1529         time_t stale_time;
1530         avl_tree_t *tree;
1531 
1532         for (i = 0; i < AUTH_TABLESIZE; i++) {
1533                 tree = exi->exi_cache[i];
1534                 stale_time = gethrestime_sec() - NFSAUTH_CACHE_TRIM;
1535                 rw_enter(&exi->exi_cache_lock, RW_READER);
1536 
1537                 /*
1538                  * Free entries that have not been
1539                  * used for NFSAUTH_CACHE_TRIM seconds.
1540                  */
1541                 for (c = avl_first(tree); c != NULL; c = AVL_NEXT(tree, c)) {