LCOV - code coverage report
Current view: top level - lib/util - memcache.c (source / functions) Hit Total Coverage
Test: coverage report for vadcx-master-patch-75612 fe003de8 Lines: 218 226 96.5 %
Date: 2024-02-29 22:57:05 Functions: 18 18 100.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             :    In-memory cache
       4             :    Copyright (C) Volker Lendecke 2007
       5             : 
       6             :    This program is free software; you can redistribute it and/or modify
       7             :    it under the terms of the GNU General Public License as published by
       8             :    the Free Software Foundation; either version 3 of the License, or
       9             :    (at your option) any later version.
      10             : 
      11             :    This program is distributed in the hope that it will be useful,
      12             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :    GNU General Public License for more details.
      15             : 
      16             :    You should have received a copy of the GNU General Public License
      17             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      18             : */
      19             : 
      20             : #include "replace.h"
      21             : #include <talloc.h>
      22             : #include "../lib/util/debug.h"
      23             : #include "../lib/util/samba_util.h"
      24             : #include "../lib/util/dlinklist.h"
      25             : #include "../lib/util/rbtree.h"
      26             : #include "memcache.h"
      27             : 
      28             : static struct memcache *global_cache;
      29             : 
      30             : struct memcache_talloc_value {
      31             :         void *ptr;
      32             :         size_t len;
      33             : };
      34             : 
      35             : struct memcache_element {
      36             :         struct rb_node rb_node;
      37             :         struct memcache_element *prev, *next;
      38             :         size_t keylength, valuelength;
      39             :         uint8_t n;              /* This is really an enum, but save memory */
      40             :         char data[1];           /* placeholder for offsetof */
      41             : };
      42             : 
      43             : struct memcache {
      44             :         struct memcache_element *mru;
      45             :         struct rb_root tree;
      46             :         size_t size;
      47             :         size_t max_size;
      48             : };
      49             : 
      50             : static void memcache_element_parse(struct memcache_element *e,
      51             :                                    DATA_BLOB *key, DATA_BLOB *value);
      52             : 
      53     1374110 : static bool memcache_is_talloc(enum memcache_number n)
      54             : {
      55        6236 :         bool result;
      56             : 
      57     1374110 :         switch (n) {
      58     1249833 :         case GETPWNAM_CACHE:
      59             :         case PDB_GETPWSID_CACHE:
      60             :         case SINGLETON_CACHE_TALLOC:
      61             :         case SHARE_MODE_LOCK_CACHE:
      62             :         case GETWD_CACHE:
      63             :         case VIRUSFILTER_SCAN_RESULTS_CACHE_TALLOC:
      64     1249833 :                 result = true;
      65     1249833 :                 break;
      66      118041 :         default:
      67      118041 :                 result = false;
      68      118041 :                 break;
      69             :         }
      70             : 
      71     1372878 :         return result;
      72             : }
      73             : 
      74       31885 : static int memcache_destructor(struct memcache *cache) {
      75         850 :         struct memcache_element *e, *next;
      76             : 
      77      261474 :         for (e = cache->mru; e != NULL; e = next) {
      78      229589 :                 next = e->next;
      79      229589 :                 TALLOC_FREE(e);
      80             :         }
      81       31885 :         return 0;
      82             : }
      83             : 
      84         754 : struct memcache *memcache_init(TALLOC_CTX *mem_ctx, size_t max_size)
      85             : {
      86          14 :         struct memcache *result;
      87             : 
      88         754 :         result = talloc_zero(mem_ctx, struct memcache);
      89         754 :         if (result == NULL) {
      90           0 :                 return NULL;
      91             :         }
      92         754 :         result->max_size = max_size;
      93         754 :         talloc_set_destructor(result, memcache_destructor);
      94         754 :         return result;
      95             : }
      96             : 
      97         512 : void memcache_set_global(struct memcache *cache)
      98             : {
      99         512 :         TALLOC_FREE(global_cache);
     100         512 :         global_cache = cache;
     101         512 : }
     102             : 
     103    22989385 : static struct memcache_element *memcache_node2elem(struct rb_node *node)
     104             : {
     105    22989385 :         return (struct memcache_element *)
     106             :                 ((char *)node - offsetof(struct memcache_element, rb_node));
     107             : }
     108             : 
     109    14376714 : static void memcache_element_parse(struct memcache_element *e,
     110             :                                    DATA_BLOB *key, DATA_BLOB *value)
     111             : {
     112    14376714 :         key->data = ((uint8_t *)e) + offsetof(struct memcache_element, data);
     113    14376714 :         key->length = e->keylength;
     114    14376714 :         value->data = key->data + e->keylength;
     115    14376714 :         value->length = e->valuelength;
     116    14354790 : }
     117             : 
     118     1294738 : static size_t memcache_element_size(size_t key_length, size_t value_length)
     119             : {
     120     1294738 :         return sizeof(struct memcache_element) - 1 + key_length + value_length;
     121             : }
     122             : 
     123    22908797 : static int memcache_compare(struct memcache_element *e, enum memcache_number n,
     124             :                             DATA_BLOB key)
     125             : {
     126       47800 :         DATA_BLOB this_key, this_value;
     127             : 
     128    22908797 :         if ((int)e->n < (int)n) return 1;
     129    18364836 :         if ((int)e->n > (int)n) return -1;
     130             : 
     131    10350840 :         if (e->keylength < key.length) return 1;
     132    10134980 :         if (e->keylength > key.length) return -1;
     133             : 
     134     9999754 :         memcache_element_parse(e, &this_key, &this_value);
     135     9999754 :         return memcmp(this_key.data, key.data, key.length);
     136             : }
     137             : 
     138     4825587 : static struct memcache_element *memcache_find(
     139             :         struct memcache *cache, enum memcache_number n, DATA_BLOB key)
     140             : {
     141       15454 :         struct rb_node *node;
     142             : 
     143     4825587 :         node = cache->tree.rb_node;
     144             : 
     145    21511736 :         while (node != NULL) {
     146    20237187 :                 struct memcache_element *elem = memcache_node2elem(node);
     147       38496 :                 int cmp;
     148             : 
     149    20237187 :                 cmp = memcache_compare(elem, n, key);
     150    20237187 :                 if (cmp == 0) {
     151     3551038 :                         return elem;
     152             :                 }
     153    16686149 :                 node = (cmp < 0) ? node->rb_left : node->rb_right;
     154             :         }
     155             : 
     156     1265948 :         return NULL;
     157             : }
     158             : 
     159     3617585 : bool memcache_lookup(struct memcache *cache, enum memcache_number n,
     160             :                      DATA_BLOB key, DATA_BLOB *value)
     161             : {
     162       10521 :         struct memcache_element *e;
     163             : 
     164     3617585 :         if (cache == NULL) {
     165     1203907 :                 cache = global_cache;
     166             :         }
     167     3617585 :         if (cache == NULL) {
     168       47287 :                 return false;
     169             :         }
     170             : 
     171     3569157 :         e = memcache_find(cache, n, key);
     172     3569157 :         if (e == NULL) {
     173      522516 :                 return false;
     174             :         }
     175             : 
     176     3042606 :         if (cache->size != 0) {
     177     3042606 :                 DLIST_PROMOTE(cache->mru, e);
     178             :         }
     179             : 
     180     3042606 :         memcache_element_parse(e, &key, value);
     181     3042606 :         return true;
     182             : }
     183             : 
     184     3167146 : void *memcache_lookup_talloc(struct memcache *cache, enum memcache_number n,
     185             :                              DATA_BLOB key)
     186             : {
     187        8740 :         DATA_BLOB value;
     188        8740 :         struct memcache_talloc_value mtv;
     189             : 
     190     3167146 :         if (!memcache_lookup(cache, n, key, &value)) {
     191      274737 :                 return NULL;
     192             :         }
     193             : 
     194     2888113 :         if (value.length != sizeof(mtv)) {
     195           0 :                 return NULL;
     196             :         }
     197             : 
     198     2888113 :         memcpy(&mtv, value.data, sizeof(mtv));
     199             : 
     200     2888113 :         return mtv.ptr;
     201             : }
     202             : 
     203      547734 : static void memcache_delete_element(struct memcache *cache,
     204             :                                     struct memcache_element *e)
     205             : {
     206      547734 :         rb_erase(&e->rb_node, &cache->tree);
     207             : 
     208      547734 :         DLIST_REMOVE(cache->mru, e);
     209             : 
     210      547734 :         if (memcache_is_talloc(e->n)) {
     211        1352 :                 DATA_BLOB cache_key, cache_value;
     212        1352 :                 struct memcache_talloc_value mtv;
     213             : 
     214      547664 :                 memcache_element_parse(e, &cache_key, &cache_value);
     215      547664 :                 SMB_ASSERT(cache_value.length == sizeof(mtv));
     216      547664 :                 memcpy(&mtv, cache_value.data, sizeof(mtv));
     217      547664 :                 cache->size -= mtv.len;
     218      547664 :                 TALLOC_FREE(mtv.ptr);
     219             :         }
     220             : 
     221      547734 :         cache->size -= memcache_element_size(e->keylength, e->valuelength);
     222             : 
     223      547734 :         TALLOC_FREE(e);
     224      547734 : }
     225             : 
     226      747004 : static void memcache_trim(struct memcache *cache, struct memcache_element *e)
     227             : {
     228      747004 :         struct memcache_element *tail = NULL;
     229             : 
     230      747004 :         if (cache->max_size == 0) {
     231        1007 :                 return;
     232             :         }
     233             : 
     234      745994 :         for (tail = DLIST_TAIL(cache->mru);
     235      804360 :              (cache->size > cache->max_size) && (tail != NULL);
     236       58366 :              tail = DLIST_TAIL(cache->mru))
     237             :         {
     238       58369 :                 if (tail == e) {
     239           3 :                         tail = DLIST_PREV(tail);
     240           0 :                         if (tail == NULL) {
     241           0 :                                 break;
     242             :                         }
     243             :                 }
     244       58366 :                 memcache_delete_element(cache, tail);
     245             :         }
     246             : }
     247             : 
     248      469795 : void memcache_delete(struct memcache *cache, enum memcache_number n,
     249             :                      DATA_BLOB key)
     250             : {
     251        1351 :         struct memcache_element *e;
     252             : 
     253      469795 :         if (cache == NULL) {
     254      469794 :                 cache = global_cache;
     255             :         }
     256      469795 :         if (cache == NULL) {
     257          55 :                 return;
     258             :         }
     259             : 
     260      469740 :         e = memcache_find(cache, n, key);
     261      469740 :         if (e == NULL) {
     262         994 :                 return;
     263             :         }
     264             : 
     265      468746 :         memcache_delete_element(cache, e);
     266             : }
     267             : 
     268      790256 : bool memcache_add(struct memcache *cache, enum memcache_number n,
     269             :                   DATA_BLOB key, DATA_BLOB value)
     270             : {
     271        4723 :         struct memcache_element *e;
     272        4723 :         struct rb_node **p;
     273        4723 :         struct rb_node *parent;
     274        4723 :         DATA_BLOB cache_key, cache_value;
     275        4723 :         size_t element_size;
     276             : 
     277      790256 :         if (cache == NULL) {
     278       58896 :                 cache = global_cache;
     279             :         }
     280      790256 :         if (cache == NULL) {
     281        3566 :                 return false;
     282             :         }
     283             : 
     284      786690 :         if (key.length == 0) {
     285           0 :                 return false;
     286             :         }
     287             : 
     288      786690 :         e = memcache_find(cache, n, key);
     289             : 
     290      786690 :         if (e != NULL) {
     291       39686 :                 memcache_element_parse(e, &cache_key, &cache_value);
     292             : 
     293       39686 :                 if (value.length <= cache_value.length) {
     294       39686 :                         if (memcache_is_talloc(e->n)) {
     295           8 :                                 struct memcache_talloc_value mtv;
     296             : 
     297       26435 :                                 SMB_ASSERT(cache_value.length == sizeof(mtv));
     298       26435 :                                 memcpy(&mtv, cache_value.data, sizeof(mtv));
     299       26435 :                                 cache->size -= mtv.len;
     300       26435 :                                 TALLOC_FREE(mtv.ptr);
     301             :                         }
     302             :                         /*
     303             :                          * We can reuse the existing record
     304             :                          */
     305       39686 :                         memcpy(cache_value.data, value.data, value.length);
     306       39686 :                         e->valuelength = value.length;
     307             : 
     308       39686 :                         if (memcache_is_talloc(e->n)) {
     309           8 :                                 struct memcache_talloc_value mtv;
     310             : 
     311       26435 :                                 SMB_ASSERT(cache_value.length == sizeof(mtv));
     312       26435 :                                 memcpy(&mtv, cache_value.data, sizeof(mtv));
     313       26435 :                                 cache->size += mtv.len;
     314             :                         }
     315       39686 :                         return true;
     316             :                 }
     317             : 
     318           0 :                 memcache_delete_element(cache, e);
     319             :         }
     320             : 
     321      747004 :         element_size = memcache_element_size(key.length, value.length);
     322             : 
     323      747004 :         e = talloc_size(cache, element_size);
     324      747004 :         if (e == NULL) {
     325           0 :                 DEBUG(0, ("talloc failed\n"));
     326           0 :                 return false;
     327             :         }
     328      747004 :         talloc_set_type(e, struct memcache_element);
     329             : 
     330      747004 :         e->n = n;
     331      747004 :         e->keylength = key.length;
     332      747004 :         e->valuelength = value.length;
     333             : 
     334      747004 :         memcache_element_parse(e, &cache_key, &cache_value);
     335      747004 :         memcpy(cache_key.data, key.data, key.length);
     336      747004 :         memcpy(cache_value.data, value.data, value.length);
     337             : 
     338      747004 :         parent = NULL;
     339      747004 :         p = &cache->tree.rb_node;
     340             : 
     341     3418614 :         while (*p) {
     342     2671610 :                 struct memcache_element *elem = memcache_node2elem(*p);
     343        9304 :                 int cmp;
     344             : 
     345     2671610 :                 parent = (*p);
     346             : 
     347     2671610 :                 cmp = memcache_compare(elem, n, key);
     348             : 
     349     2671610 :                 p = (cmp < 0) ? &(*p)->rb_left : &(*p)->rb_right;
     350             :         }
     351             : 
     352      747004 :         rb_link_node(&e->rb_node, parent, p);
     353      747004 :         rb_insert_color(&e->rb_node, &cache->tree);
     354             : 
     355      747004 :         DLIST_ADD(cache->mru, e);
     356             : 
     357      747004 :         cache->size += element_size;
     358      747004 :         if (memcache_is_talloc(e->n)) {
     359        3636 :                 struct memcache_talloc_value mtv;
     360             : 
     361      654303 :                 SMB_ASSERT(cache_value.length == sizeof(mtv));
     362      654303 :                 memcpy(&mtv, cache_value.data, sizeof(mtv));
     363      654303 :                 cache->size += mtv.len;
     364             :         }
     365      747004 :         memcache_trim(cache, e);
     366             : 
     367      747004 :         return true;
     368             : }
     369             : 
     370      698606 : bool memcache_add_talloc(struct memcache *cache, enum memcache_number n,
     371             :                          DATA_BLOB key, void *pptr)
     372             : {
     373        3651 :         struct memcache_talloc_value mtv;
     374      698606 :         void **ptr = (void **)pptr;
     375             : 
     376      698606 :         if (cache == NULL) {
     377      621396 :                 cache = global_cache;
     378             :         }
     379      698606 :         if (cache == NULL) {
     380       17861 :                 return false;
     381             :         }
     382             : 
     383      680738 :         mtv.len = talloc_total_size(*ptr);
     384      680738 :         mtv.ptr = talloc_move(cache, ptr);
     385             : 
     386      680738 :         return memcache_add(cache, n, key, data_blob_const(&mtv, sizeof(mtv)));
     387             : }
     388             : 
     389       22849 : void memcache_flush(struct memcache *cache, enum memcache_number n)
     390             : {
     391           1 :         struct rb_node *node;
     392             : 
     393       22849 :         if (cache == NULL) {
     394       21644 :                 cache = global_cache;
     395             :         }
     396       22849 :         if (cache == NULL) {
     397         867 :                 return;
     398             :         }
     399             : 
     400             :         /*
     401             :          * Find the smallest element of number n
     402             :          */
     403             : 
     404       21982 :         node = cache->tree.rb_node;
     405       21982 :         if (node == NULL) {
     406          86 :                 return;
     407             :         }
     408             : 
     409             :         /*
     410             :          * First, find *any* element of number n
     411             :          */
     412             : 
     413       42629 :         while (true) {
     414       64524 :                 struct memcache_element *elem = memcache_node2elem(node);
     415           1 :                 struct rb_node *next;
     416             : 
     417       64524 :                 if ((int)elem->n == (int)n) {
     418       20619 :                         break;
     419             :                 }
     420             : 
     421       43904 :                 if ((int)elem->n < (int)n) {
     422       23638 :                         next = node->rb_right;
     423             :                 }
     424             :                 else {
     425       20266 :                         next = node->rb_left;
     426             :                 }
     427       43904 :                 if (next == NULL) {
     428        1276 :                         break;
     429             :                 }
     430       42628 :                 node = next;
     431             :         }
     432             : 
     433             :         /*
     434             :          * Then, find the leftmost element with number n
     435             :          */
     436             : 
     437           3 :         while (true) {
     438       21898 :                 struct rb_node *prev = rb_prev(node);
     439           1 :                 struct memcache_element *elem;
     440             : 
     441       21898 :                 if (prev == NULL) {
     442          43 :                         break;
     443             :                 }
     444       21854 :                 elem = memcache_node2elem(prev);
     445       21854 :                 if ((int)elem->n != (int)n) {
     446       21852 :                         break;
     447             :                 }
     448           2 :                 node = prev;
     449             :         }
     450             : 
     451       42518 :         while (node != NULL) {
     452       42012 :                 struct memcache_element *e = memcache_node2elem(node);
     453       42012 :                 struct rb_node *next = rb_next(node);
     454             : 
     455       42012 :                 if (e->n != n) {
     456       21390 :                         break;
     457             :                 }
     458             : 
     459       20622 :                 memcache_delete_element(cache, e);
     460       20622 :                 node = next;
     461             :         }
     462             : }
     463             : 
     464       14437 : void gfree_memcache(void)
     465             : {
     466       14437 :         TALLOC_FREE(global_cache);
     467       14437 : }

Generated by: LCOV version 1.14