LCOV - code coverage report
Current view: top level - lib/util - memcache.c (source / functions) Hit Total Coverage
Test: coverage report for fix-15632 9995c5c2 Lines: 218 226 96.5 %
Date: 2024-04-13 12:30:31 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     1367457 : static bool memcache_is_talloc(enum memcache_number n)
      54             : {
      55        6236 :         bool result;
      56             : 
      57     1367457 :         switch (n) {
      58     1245163 :         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     1245163 :                 result = true;
      65     1245163 :                 break;
      66      116058 :         default:
      67      116058 :                 result = false;
      68      116058 :                 break;
      69             :         }
      70             : 
      71     1366225 :         return result;
      72             : }
      73             : 
      74       31532 : static int memcache_destructor(struct memcache *cache) {
      75         850 :         struct memcache_element *e, *next;
      76             : 
      77      258007 :         for (e = cache->mru; e != NULL; e = next) {
      78      226475 :                 next = e->next;
      79      226475 :                 TALLOC_FREE(e);
      80             :         }
      81       31532 :         return 0;
      82             : }
      83             : 
      84         718 : struct memcache *memcache_init(TALLOC_CTX *mem_ctx, size_t max_size)
      85             : {
      86          14 :         struct memcache *result;
      87             : 
      88         718 :         result = talloc_zero(mem_ctx, struct memcache);
      89         718 :         if (result == NULL) {
      90           0 :                 return NULL;
      91             :         }
      92         718 :         result->max_size = max_size;
      93         718 :         talloc_set_destructor(result, memcache_destructor);
      94         718 :         return result;
      95             : }
      96             : 
      97         472 : void memcache_set_global(struct memcache *cache)
      98             : {
      99         472 :         TALLOC_FREE(global_cache);
     100         472 :         global_cache = cache;
     101         472 : }
     102             : 
     103    22875961 : static struct memcache_element *memcache_node2elem(struct rb_node *node)
     104             : {
     105    22875961 :         return (struct memcache_element *)
     106             :                 ((char *)node - offsetof(struct memcache_element, rb_node));
     107             : }
     108             : 
     109    14340747 : static void memcache_element_parse(struct memcache_element *e,
     110             :                                    DATA_BLOB *key, DATA_BLOB *value)
     111             : {
     112    14340747 :         key->data = ((uint8_t *)e) + offsetof(struct memcache_element, data);
     113    14340747 :         key->length = e->keylength;
     114    14340747 :         value->data = key->data + e->keylength;
     115    14340747 :         value->length = e->valuelength;
     116    14319107 : }
     117             : 
     118     1287917 : static size_t memcache_element_size(size_t key_length, size_t value_length)
     119             : {
     120     1287917 :         return sizeof(struct memcache_element) - 1 + key_length + value_length;
     121             : }
     122             : 
     123    22797070 : static int memcache_compare(struct memcache_element *e, enum memcache_number n,
     124             :                             DATA_BLOB key)
     125             : {
     126       47519 :         DATA_BLOB this_key, this_value;
     127             : 
     128    22797070 :         if ((int)e->n < (int)n) return 1;
     129    18262318 :         if ((int)e->n > (int)n) return -1;
     130             : 
     131    10326810 :         if (e->keylength < key.length) return 1;
     132    10114284 :         if (e->keylength > key.length) return -1;
     133             : 
     134     9981173 :         memcache_element_parse(e, &this_key, &this_value);
     135     9981173 :         return memcmp(this_key.data, key.data, key.length);
     136             : }
     137             : 
     138     4806281 : 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     4806281 :         node = cache->tree.rb_node;
     144             : 
     145    21407829 :         while (node != NULL) {
     146    20140360 :                 struct memcache_element *elem = memcache_node2elem(node);
     147       38216 :                 int cmp;
     148             : 
     149    20140360 :                 cmp = memcache_compare(elem, n, key);
     150    20140360 :                 if (cmp == 0) {
     151     3538812 :                         return elem;
     152             :                 }
     153    16601548 :                 node = (cmp < 0) ? node->rb_left : node->rb_right;
     154             :         }
     155             : 
     156     1258868 :         return NULL;
     157             : }
     158             : 
     159     3604466 : 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     3604466 :         if (cache == NULL) {
     165     1195174 :                 cache = global_cache;
     166             :         }
     167     3604466 :         if (cache == NULL) {
     168       47083 :                 return false;
     169             :         }
     170             : 
     171     3556242 :         e = memcache_find(cache, n, key);
     172     3556242 :         if (e == NULL) {
     173      520246 :                 return false;
     174             :         }
     175             : 
     176     3031961 :         if (cache->size != 0) {
     177     3031961 :                 DLIST_PROMOTE(cache->mru, e);
     178             :         }
     179             : 
     180     3031961 :         memcache_element_parse(e, &key, value);
     181     3031961 :         return true;
     182             : }
     183             : 
     184     3159169 : 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     3159169 :         if (!memcache_lookup(cache, n, key, &value)) {
     191      273422 :                 return NULL;
     192             :         }
     193             : 
     194     2881451 :         if (value.length != sizeof(mtv)) {
     195           0 :                 return NULL;
     196             :         }
     197             : 
     198     2881451 :         memcpy(&mtv, value.data, sizeof(mtv));
     199             : 
     200     2881451 :         return mtv.ptr;
     201             : }
     202             : 
     203      545723 : static void memcache_delete_element(struct memcache *cache,
     204             :                                     struct memcache_element *e)
     205             : {
     206      545723 :         rb_erase(&e->rb_node, &cache->tree);
     207             : 
     208      545723 :         DLIST_REMOVE(cache->mru, e);
     209             : 
     210      545723 :         if (memcache_is_talloc(e->n)) {
     211        1352 :                 DATA_BLOB cache_key, cache_value;
     212        1352 :                 struct memcache_talloc_value mtv;
     213             : 
     214      545649 :                 memcache_element_parse(e, &cache_key, &cache_value);
     215      545649 :                 SMB_ASSERT(cache_value.length == sizeof(mtv));
     216      545649 :                 memcpy(&mtv, cache_value.data, sizeof(mtv));
     217      545649 :                 cache->size -= mtv.len;
     218      545649 :                 TALLOC_FREE(mtv.ptr);
     219             :         }
     220             : 
     221      545723 :         cache->size -= memcache_element_size(e->keylength, e->valuelength);
     222             : 
     223      545723 :         TALLOC_FREE(e);
     224      545723 : }
     225             : 
     226      742194 : static void memcache_trim(struct memcache *cache, struct memcache_element *e)
     227             : {
     228      742194 :         struct memcache_element *tail = NULL;
     229             : 
     230      742194 :         if (cache->max_size == 0) {
     231         927 :                 return;
     232             :         }
     233             : 
     234      741264 :         for (tail = DLIST_TAIL(cache->mru);
     235      799630 :              (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      468130 : void memcache_delete(struct memcache *cache, enum memcache_number n,
     249             :                      DATA_BLOB key)
     250             : {
     251        1351 :         struct memcache_element *e;
     252             : 
     253      468130 :         if (cache == NULL) {
     254      468129 :                 cache = global_cache;
     255             :         }
     256      468130 :         if (cache == NULL) {
     257          55 :                 return;
     258             :         }
     259             : 
     260      468075 :         e = memcache_find(cache, n, key);
     261      468075 :         if (e == NULL) {
     262         994 :                 return;
     263             :         }
     264             : 
     265      467081 :         memcache_delete_element(cache, e);
     266             : }
     267             : 
     268      785524 : 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      785524 :         if (cache == NULL) {
     278       57435 :                 cache = global_cache;
     279             :         }
     280      785524 :         if (cache == NULL) {
     281        3560 :                 return false;
     282             :         }
     283             : 
     284      781964 :         if (key.length == 0) {
     285           0 :                 return false;
     286             :         }
     287             : 
     288      781964 :         e = memcache_find(cache, n, key);
     289             : 
     290      781964 :         if (e != NULL) {
     291       39770 :                 memcache_element_parse(e, &cache_key, &cache_value);
     292             : 
     293       39770 :                 if (value.length <= cache_value.length) {
     294       39770 :                         if (memcache_is_talloc(e->n)) {
     295           8 :                                 struct memcache_talloc_value mtv;
     296             : 
     297       26559 :                                 SMB_ASSERT(cache_value.length == sizeof(mtv));
     298       26559 :                                 memcpy(&mtv, cache_value.data, sizeof(mtv));
     299       26559 :                                 cache->size -= mtv.len;
     300       26559 :                                 TALLOC_FREE(mtv.ptr);
     301             :                         }
     302             :                         /*
     303             :                          * We can reuse the existing record
     304             :                          */
     305       39770 :                         memcpy(cache_value.data, value.data, value.length);
     306       39770 :                         e->valuelength = value.length;
     307             : 
     308       39770 :                         if (memcache_is_talloc(e->n)) {
     309           8 :                                 struct memcache_talloc_value mtv;
     310             : 
     311       26559 :                                 SMB_ASSERT(cache_value.length == sizeof(mtv));
     312       26559 :                                 memcpy(&mtv, cache_value.data, sizeof(mtv));
     313       26559 :                                 cache->size += mtv.len;
     314             :                         }
     315       39770 :                         return true;
     316             :                 }
     317             : 
     318           0 :                 memcache_delete_element(cache, e);
     319             :         }
     320             : 
     321      742194 :         element_size = memcache_element_size(key.length, value.length);
     322             : 
     323      742194 :         e = talloc_size(cache, element_size);
     324      742194 :         if (e == NULL) {
     325           0 :                 DEBUG(0, ("talloc failed\n"));
     326           0 :                 return false;
     327             :         }
     328      742194 :         talloc_set_type(e, struct memcache_element);
     329             : 
     330      742194 :         e->n = n;
     331      742194 :         e->keylength = key.length;
     332      742194 :         e->valuelength = value.length;
     333             : 
     334      742194 :         memcache_element_parse(e, &cache_key, &cache_value);
     335      742194 :         memcpy(cache_key.data, key.data, key.length);
     336      742194 :         memcpy(cache_value.data, value.data, value.length);
     337             : 
     338      742194 :         parent = NULL;
     339      742194 :         p = &cache->tree.rb_node;
     340             : 
     341     3398904 :         while (*p) {
     342     2656710 :                 struct memcache_element *elem = memcache_node2elem(*p);
     343        9303 :                 int cmp;
     344             : 
     345     2656710 :                 parent = (*p);
     346             : 
     347     2656710 :                 cmp = memcache_compare(elem, n, key);
     348             : 
     349     2656710 :                 p = (cmp < 0) ? &(*p)->rb_left : &(*p)->rb_right;
     350             :         }
     351             : 
     352      742194 :         rb_link_node(&e->rb_node, parent, p);
     353      742194 :         rb_insert_color(&e->rb_node, &cache->tree);
     354             : 
     355      742194 :         DLIST_ADD(cache->mru, e);
     356             : 
     357      742194 :         cache->size += element_size;
     358      742194 :         if (memcache_is_talloc(e->n)) {
     359        3636 :                 struct memcache_talloc_value mtv;
     360             : 
     361      651400 :                 SMB_ASSERT(cache_value.length == sizeof(mtv));
     362      651400 :                 memcpy(&mtv, cache_value.data, sizeof(mtv));
     363      651400 :                 cache->size += mtv.len;
     364             :         }
     365      742194 :         memcache_trim(cache, e);
     366             : 
     367      742194 :         return true;
     368             : }
     369             : 
     370      695655 : 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      695655 :         void **ptr = (void **)pptr;
     375             : 
     376      695655 :         if (cache == NULL) {
     377      619122 :                 cache = global_cache;
     378             :         }
     379      695655 :         if (cache == NULL) {
     380       17689 :                 return false;
     381             :         }
     382             : 
     383      677959 :         mtv.len = talloc_total_size(*ptr);
     384      677959 :         mtv.ptr = talloc_move(cache, ptr);
     385             : 
     386      677959 :         return memcache_add(cache, n, key, data_blob_const(&mtv, sizeof(mtv)));
     387             : }
     388             : 
     389       22511 : void memcache_flush(struct memcache *cache, enum memcache_number n)
     390             : {
     391           1 :         struct rb_node *node;
     392             : 
     393       22511 :         if (cache == NULL) {
     394       21302 :                 cache = global_cache;
     395             :         }
     396       22511 :         if (cache == NULL) {
     397         871 :                 return;
     398             :         }
     399             : 
     400             :         /*
     401             :          * Find the smallest element of number n
     402             :          */
     403             : 
     404       21640 :         node = cache->tree.rb_node;
     405       21640 :         if (node == NULL) {
     406          86 :                 return;
     407             :         }
     408             : 
     409             :         /*
     410             :          * First, find *any* element of number n
     411             :          */
     412             : 
     413       41983 :         while (true) {
     414       63536 :                 struct memcache_element *elem = memcache_node2elem(node);
     415           1 :                 struct rb_node *next;
     416             : 
     417       63536 :                 if ((int)elem->n == (int)n) {
     418       20273 :                         break;
     419             :                 }
     420             : 
     421       43262 :                 if ((int)elem->n < (int)n) {
     422       23302 :                         next = node->rb_right;
     423             :                 }
     424             :                 else {
     425       19960 :                         next = node->rb_left;
     426             :                 }
     427       43262 :                 if (next == NULL) {
     428        1280 :                         break;
     429             :                 }
     430       41982 :                 node = next;
     431             :         }
     432             : 
     433             :         /*
     434             :          * Then, find the leftmost element with number n
     435             :          */
     436             : 
     437           3 :         while (true) {
     438       21556 :                 struct rb_node *prev = rb_prev(node);
     439           1 :                 struct memcache_element *elem;
     440             : 
     441       21556 :                 if (prev == NULL) {
     442          43 :                         break;
     443             :                 }
     444       21512 :                 elem = memcache_node2elem(prev);
     445       21512 :                 if ((int)elem->n != (int)n) {
     446       21510 :                         break;
     447             :                 }
     448           2 :                 node = prev;
     449             :         }
     450             : 
     451       41830 :         while (node != NULL) {
     452       41364 :                 struct memcache_element *e = memcache_node2elem(node);
     453       41364 :                 struct rb_node *next = rb_next(node);
     454             : 
     455       41364 :                 if (e->n != n) {
     456       21088 :                         break;
     457             :                 }
     458             : 
     459       20276 :                 memcache_delete_element(cache, e);
     460       20276 :                 node = next;
     461             :         }
     462             : }
     463             : 
     464       14049 : void gfree_memcache(void)
     465             : {
     466       14049 :         TALLOC_FREE(global_cache);
     467       14049 : }

Generated by: LCOV version 1.14