LCOV - code coverage report
Current view: top level - lib/util - memcache.c (source / functions) Hit Total Coverage
Test: coverage report for v4-17-test 1498b464 Lines: 184 197 93.4 %
Date: 2024-06-13 04:01:37 Functions: 17 17 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       47980 : static bool memcache_is_talloc(enum memcache_number n)
      54             : {
      55             :         bool result;
      56             : 
      57       47980 :         switch (n) {
      58       40091 :         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       40091 :                 result = true;
      65       40091 :                 break;
      66        7889 :         default:
      67        7889 :                 result = false;
      68        7889 :                 break;
      69             :         }
      70             : 
      71       47980 :         return result;
      72             : }
      73             : 
      74        5270 : static int memcache_destructor(struct memcache *cache) {
      75             :         struct memcache_element *e, *next;
      76             : 
      77       35483 :         for (e = cache->mru; e != NULL; e = next) {
      78       30213 :                 next = e->next;
      79       30213 :                 TALLOC_FREE(e);
      80             :         }
      81        5270 :         return 0;
      82             : }
      83             : 
      84         120 : struct memcache *memcache_init(TALLOC_CTX *mem_ctx, size_t max_size)
      85             : {
      86             :         struct memcache *result;
      87             : 
      88         120 :         result = talloc_zero(mem_ctx, struct memcache);
      89         120 :         if (result == NULL) {
      90           0 :                 return NULL;
      91             :         }
      92         120 :         result->max_size = max_size;
      93         120 :         talloc_set_destructor(result, memcache_destructor);
      94         120 :         return result;
      95             : }
      96             : 
      97          44 : void memcache_set_global(struct memcache *cache)
      98             : {
      99          44 :         TALLOC_FREE(global_cache);
     100          44 :         global_cache = cache;
     101          44 : }
     102             : 
     103      521768 : static struct memcache_element *memcache_node2elem(struct rb_node *node)
     104             : {
     105      521768 :         return (struct memcache_element *)
     106             :                 ((char *)node - offsetof(struct memcache_element, rb_node));
     107             : }
     108             : 
     109      327990 : static void memcache_element_parse(struct memcache_element *e,
     110             :                                    DATA_BLOB *key, DATA_BLOB *value)
     111             : {
     112      327990 :         key->data = ((uint8_t *)e) + offsetof(struct memcache_element, data);
     113      327990 :         key->length = e->keylength;
     114      327990 :         value->data = key->data + e->keylength;
     115      327990 :         value->length = e->valuelength;
     116      327990 : }
     117             : 
     118       46748 : static size_t memcache_element_size(size_t key_length, size_t value_length)
     119             : {
     120       46748 :         return sizeof(struct memcache_element) - 1 + key_length + value_length;
     121             : }
     122             : 
     123      517546 : static int memcache_compare(struct memcache_element *e, enum memcache_number n,
     124             :                             DATA_BLOB key)
     125             : {
     126             :         DATA_BLOB this_key, this_value;
     127             : 
     128      517546 :         if ((int)e->n < (int)n) return 1;
     129      372121 :         if ((int)e->n > (int)n) return -1;
     130             : 
     131      228206 :         if (e->keylength < key.length) return 1;
     132      208589 :         if (e->keylength > key.length) return -1;
     133             : 
     134      204746 :         memcache_element_parse(e, &this_key, &this_value);
     135      204746 :         return memcmp(this_key.data, key.data, key.length);
     136             : }
     137             : 
     138      158573 : static struct memcache_element *memcache_find(
     139             :         struct memcache *cache, enum memcache_number n, DATA_BLOB key)
     140             : {
     141             :         struct rb_node *node;
     142             : 
     143      158573 :         node = cache->tree.rb_node;
     144             : 
     145      626446 :         while (node != NULL) {
     146      431808 :                 struct memcache_element *elem = memcache_node2elem(node);
     147             :                 int cmp;
     148             : 
     149      431808 :                 cmp = memcache_compare(elem, n, key);
     150      431808 :                 if (cmp == 0) {
     151       86656 :                         return elem;
     152             :                 }
     153      345152 :                 node = (cmp < 0) ? node->rb_left : node->rb_right;
     154             :         }
     155             : 
     156       71917 :         return NULL;
     157             : }
     158             : 
     159      121540 : bool memcache_lookup(struct memcache *cache, enum memcache_number n,
     160             :                      DATA_BLOB key, DATA_BLOB *value)
     161             : {
     162             :         struct memcache_element *e;
     163             : 
     164      121540 :         if (cache == NULL) {
     165       51466 :                 cache = global_cache;
     166             :         }
     167      121540 :         if (cache == NULL) {
     168       12008 :                 return false;
     169             :         }
     170             : 
     171      109532 :         e = memcache_find(cache, n, key);
     172      109532 :         if (e == NULL) {
     173       33652 :                 return false;
     174             :         }
     175             : 
     176       75880 :         if (cache->size != 0) {
     177       75880 :                 DLIST_PROMOTE(cache->mru, e);
     178             :         }
     179             : 
     180       75880 :         memcache_element_parse(e, &key, value);
     181       75880 :         return true;
     182             : }
     183             : 
     184      106151 : void *memcache_lookup_talloc(struct memcache *cache, enum memcache_number n,
     185             :                              DATA_BLOB key)
     186             : {
     187             :         DATA_BLOB value;
     188             :         struct memcache_talloc_value mtv;
     189             : 
     190      106151 :         if (!memcache_lookup(cache, n, key, &value)) {
     191       36197 :                 return NULL;
     192             :         }
     193             : 
     194       69954 :         if (value.length != sizeof(mtv)) {
     195           0 :                 return NULL;
     196             :         }
     197             : 
     198       69954 :         memcpy(&mtv, value.data, sizeof(mtv));
     199             : 
     200       69954 :         return mtv.ptr;
     201             : }
     202             : 
     203       10654 : static void memcache_delete_element(struct memcache *cache,
     204             :                                     struct memcache_element *e)
     205             : {
     206       10654 :         rb_erase(&e->rb_node, &cache->tree);
     207             : 
     208       10654 :         DLIST_REMOVE(cache->mru, e);
     209             : 
     210       10654 :         if (memcache_is_talloc(e->n)) {
     211             :                 DATA_BLOB cache_key, cache_value;
     212             :                 struct memcache_talloc_value mtv;
     213             : 
     214       10654 :                 memcache_element_parse(e, &cache_key, &cache_value);
     215       10654 :                 SMB_ASSERT(cache_value.length == sizeof(mtv));
     216       10654 :                 memcpy(&mtv, cache_value.data, sizeof(mtv));
     217       10654 :                 cache->size -= mtv.len;
     218       10654 :                 TALLOC_FREE(mtv.ptr);
     219             :         }
     220             : 
     221       10654 :         cache->size -= memcache_element_size(e->keylength, e->valuelength);
     222             : 
     223       10654 :         TALLOC_FREE(e);
     224       10654 : }
     225             : 
     226       36094 : static void memcache_trim(struct memcache *cache, struct memcache_element *e)
     227             : {
     228       36094 :         struct memcache_element *tail = NULL;
     229             : 
     230       36094 :         if (cache->max_size == 0) {
     231           0 :                 return;
     232             :         }
     233             : 
     234       63158 :         for (tail = DLIST_TAIL(cache->mru);
     235       36094 :              (cache->size > cache->max_size) && (tail != NULL);
     236           0 :              tail = DLIST_TAIL(cache->mru))
     237             :         {
     238           0 :                 if (tail == e) {
     239           0 :                         tail = DLIST_PREV(tail);
     240           0 :                         if (tail == NULL) {
     241           0 :                                 break;
     242             :                         }
     243             :                 }
     244           0 :                 memcache_delete_element(cache, tail);
     245             :         }
     246             : }
     247             : 
     248       12335 : void memcache_delete(struct memcache *cache, enum memcache_number n,
     249             :                      DATA_BLOB key)
     250             : {
     251             :         struct memcache_element *e;
     252             : 
     253       12335 :         if (cache == NULL) {
     254       10170 :                 cache = global_cache;
     255             :         }
     256       12335 :         if (cache == NULL) {
     257           4 :                 return;
     258             :         }
     259             : 
     260       12331 :         e = memcache_find(cache, n, key);
     261       12331 :         if (e == NULL) {
     262        2171 :                 return;
     263             :         }
     264             : 
     265       10160 :         memcache_delete_element(cache, e);
     266             : }
     267             : 
     268       36990 : void memcache_add(struct memcache *cache, enum memcache_number n,
     269             :                   DATA_BLOB key, DATA_BLOB value)
     270             : {
     271             :         struct memcache_element *e;
     272             :         struct rb_node **p;
     273             :         struct rb_node *parent;
     274             :         DATA_BLOB cache_key, cache_value;
     275             :         size_t element_size;
     276             : 
     277       36990 :         if (cache == NULL) {
     278        5807 :                 cache = global_cache;
     279             :         }
     280       36990 :         if (cache == NULL) {
     281        1103 :                 return;
     282             :         }
     283             : 
     284       36710 :         if (key.length == 0) {
     285           0 :                 return;
     286             :         }
     287             : 
     288       36710 :         e = memcache_find(cache, n, key);
     289             : 
     290       36710 :         if (e != NULL) {
     291         616 :                 memcache_element_parse(e, &cache_key, &cache_value);
     292             : 
     293         616 :                 if (value.length <= cache_value.length) {
     294         616 :                         if (memcache_is_talloc(e->n)) {
     295             :                                 struct memcache_talloc_value mtv;
     296             : 
     297         536 :                                 SMB_ASSERT(cache_value.length == sizeof(mtv));
     298         536 :                                 memcpy(&mtv, cache_value.data, sizeof(mtv));
     299         536 :                                 cache->size -= mtv.len;
     300         536 :                                 TALLOC_FREE(mtv.ptr);
     301             :                         }
     302             :                         /*
     303             :                          * We can reuse the existing record
     304             :                          */
     305         616 :                         memcpy(cache_value.data, value.data, value.length);
     306         616 :                         e->valuelength = value.length;
     307             : 
     308         616 :                         if (memcache_is_talloc(e->n)) {
     309             :                                 struct memcache_talloc_value mtv;
     310             : 
     311         536 :                                 SMB_ASSERT(cache_value.length == sizeof(mtv));
     312         536 :                                 memcpy(&mtv, cache_value.data, sizeof(mtv));
     313         536 :                                 cache->size += mtv.len;
     314             :                         }
     315         616 :                         return;
     316             :                 }
     317             : 
     318           0 :                 memcache_delete_element(cache, e);
     319             :         }
     320             : 
     321       36094 :         element_size = memcache_element_size(key.length, value.length);
     322             : 
     323       36094 :         e = talloc_size(cache, element_size);
     324       36094 :         if (e == NULL) {
     325           0 :                 DEBUG(0, ("talloc failed\n"));
     326           0 :                 return;
     327             :         }
     328       36094 :         talloc_set_type(e, struct memcache_element);
     329             : 
     330       36094 :         e->n = n;
     331       36094 :         e->keylength = key.length;
     332       36094 :         e->valuelength = value.length;
     333             : 
     334       36094 :         memcache_element_parse(e, &cache_key, &cache_value);
     335       36094 :         memcpy(cache_key.data, key.data, key.length);
     336       36094 :         memcpy(cache_value.data, value.data, value.length);
     337             : 
     338       36094 :         parent = NULL;
     339       36094 :         p = &cache->tree.rb_node;
     340             : 
     341      148896 :         while (*p) {
     342       85738 :                 struct memcache_element *elem = memcache_node2elem(*p);
     343             :                 int cmp;
     344             : 
     345       85738 :                 parent = (*p);
     346             : 
     347       85738 :                 cmp = memcache_compare(elem, n, key);
     348             : 
     349       85738 :                 p = (cmp < 0) ? &(*p)->rb_left : &(*p)->rb_right;
     350             :         }
     351             : 
     352       36094 :         rb_link_node(&e->rb_node, parent, p);
     353       36094 :         rb_insert_color(&e->rb_node, &cache->tree);
     354             : 
     355       36094 :         DLIST_ADD(cache->mru, e);
     356             : 
     357       36094 :         cache->size += element_size;
     358       36094 :         if (memcache_is_talloc(e->n)) {
     359             :                 struct memcache_talloc_value mtv;
     360             : 
     361       28365 :                 SMB_ASSERT(cache_value.length == sizeof(mtv));
     362       28365 :                 memcpy(&mtv, cache_value.data, sizeof(mtv));
     363       28365 :                 cache->size += mtv.len;
     364             :         }
     365       36094 :         memcache_trim(cache, e);
     366             : }
     367             : 
     368       31548 : void memcache_add_talloc(struct memcache *cache, enum memcache_number n,
     369             :                          DATA_BLOB key, void *pptr)
     370             : {
     371             :         struct memcache_talloc_value mtv;
     372       31548 :         void **ptr = (void **)pptr;
     373             : 
     374       31548 :         if (cache == NULL) {
     375       18445 :                 cache = global_cache;
     376             :         }
     377       31548 :         if (cache == NULL) {
     378        2647 :                 return;
     379             :         }
     380             : 
     381       28901 :         mtv.len = talloc_total_size(*ptr);
     382       28901 :         mtv.ptr = talloc_move(cache, ptr);
     383       28901 :         memcache_add(cache, n, key, data_blob_const(&mtv, sizeof(mtv)));
     384             : }
     385             : 
     386        1307 : void memcache_flush(struct memcache *cache, enum memcache_number n)
     387             : {
     388             :         struct rb_node *node;
     389             : 
     390        1307 :         if (cache == NULL) {
     391         841 :                 cache = global_cache;
     392             :         }
     393        1307 :         if (cache == NULL) {
     394         313 :                 return;
     395             :         }
     396             : 
     397             :         /*
     398             :          * Find the smallest element of number n
     399             :          */
     400             : 
     401         994 :         node = cache->tree.rb_node;
     402         994 :         if (node == NULL) {
     403          90 :                 return;
     404             :         }
     405             : 
     406             :         /*
     407             :          * First, find *any* element of number n
     408             :          */
     409             : 
     410        1207 :         while (true) {
     411        2111 :                 struct memcache_element *elem = memcache_node2elem(node);
     412             :                 struct rb_node *next;
     413             : 
     414        2111 :                 if ((int)elem->n == (int)n) {
     415         492 :                         break;
     416             :                 }
     417             : 
     418        1619 :                 if ((int)elem->n < (int)n) {
     419         899 :                         next = node->rb_right;
     420             :                 }
     421             :                 else {
     422         720 :                         next = node->rb_left;
     423             :                 }
     424        1619 :                 if (next == NULL) {
     425         412 :                         break;
     426             :                 }
     427        1207 :                 node = next;
     428             :         }
     429             : 
     430             :         /*
     431             :          * Then, find the leftmost element with number n
     432             :          */
     433             : 
     434           2 :         while (true) {
     435         906 :                 struct rb_node *prev = rb_prev(node);
     436             :                 struct memcache_element *elem;
     437             : 
     438         906 :                 if (prev == NULL) {
     439         193 :                         break;
     440             :                 }
     441         713 :                 elem = memcache_node2elem(prev);
     442         713 :                 if ((int)elem->n != (int)n) {
     443         711 :                         break;
     444             :                 }
     445           2 :                 node = prev;
     446             :         }
     447             : 
     448        1885 :         while (node != NULL) {
     449        1398 :                 struct memcache_element *e = memcache_node2elem(node);
     450        1398 :                 struct rb_node *next = rb_next(node);
     451             : 
     452        1398 :                 if (e->n != n) {
     453         904 :                         break;
     454             :                 }
     455             : 
     456         494 :                 memcache_delete_element(cache, e);
     457         494 :                 node = next;
     458             :         }
     459             : }

Generated by: LCOV version 1.13