LCOV - code coverage report
Current view: top level - lib/util - memcache.c (source / functions) Hit Total Coverage
Test: coverage report for recycleplus df22b230 Lines: 178 197 90.4 %
Date: 2024-02-14 10:14:15 Functions: 16 17 94.1 %

          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       66082 : static bool memcache_is_talloc(enum memcache_number n)
      54             : {
      55             :         bool result;
      56             : 
      57       66082 :         switch (n) {
      58       56636 :         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       56636 :                 result = true;
      65       56636 :                 break;
      66        9446 :         default:
      67        9446 :                 result = false;
      68        9446 :                 break;
      69             :         }
      70             : 
      71       66082 :         return result;
      72             : }
      73             : 
      74        5251 : static int memcache_destructor(struct memcache *cache) {
      75             :         struct memcache_element *e, *next;
      76             : 
      77       36538 :         for (e = cache->mru; e != NULL; e = next) {
      78       31287 :                 next = e->next;
      79       31287 :                 TALLOC_FREE(e);
      80             :         }
      81        5251 :         return 0;
      82             : }
      83             : 
      84          75 : struct memcache *memcache_init(TALLOC_CTX *mem_ctx, size_t max_size)
      85             : {
      86             :         struct memcache *result;
      87             : 
      88          75 :         result = talloc_zero(mem_ctx, struct memcache);
      89          75 :         if (result == NULL) {
      90           0 :                 return NULL;
      91             :         }
      92          75 :         result->max_size = max_size;
      93          75 :         talloc_set_destructor(result, memcache_destructor);
      94          75 :         return result;
      95             : }
      96             : 
      97           0 : void memcache_set_global(struct memcache *cache)
      98             : {
      99           0 :         TALLOC_FREE(global_cache);
     100           0 :         global_cache = cache;
     101           0 : }
     102             : 
     103      586097 : static struct memcache_element *memcache_node2elem(struct rb_node *node)
     104             : {
     105      586097 :         return (struct memcache_element *)
     106             :                 ((char *)node - offsetof(struct memcache_element, rb_node));
     107             : }
     108             : 
     109      359175 : static void memcache_element_parse(struct memcache_element *e,
     110             :                                    DATA_BLOB *key, DATA_BLOB *value)
     111             : {
     112      359175 :         key->data = ((uint8_t *)e) + offsetof(struct memcache_element, data);
     113      359175 :         key->length = e->keylength;
     114      359175 :         value->data = key->data + e->keylength;
     115      359175 :         value->length = e->valuelength;
     116      359175 : }
     117             : 
     118       63092 : static size_t memcache_element_size(size_t key_length, size_t value_length)
     119             : {
     120       63092 :         return sizeof(struct memcache_element) - 1 + key_length + value_length;
     121             : }
     122             : 
     123      582271 : 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      582271 :         if ((int)e->n < (int)n) return 1;
     129      387688 :         if ((int)e->n > (int)n) return -1;
     130             : 
     131      236963 :         if (e->keylength < key.length) return 1;
     132      212318 :         if (e->keylength > key.length) return -1;
     133             : 
     134      208568 :         memcache_element_parse(e, &this_key, &this_value);
     135      208568 :         return memcmp(this_key.data, key.data, key.length);
     136             : }
     137             : 
     138      172637 : 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      172637 :         node = cache->tree.rb_node;
     144             : 
     145      545418 :         while (node != NULL) {
     146      478140 :                 struct memcache_element *elem = memcache_node2elem(node);
     147             :                 int cmp;
     148             : 
     149      478140 :                 cmp = memcache_compare(elem, n, key);
     150      478140 :                 if (cmp == 0) {
     151      105359 :                         return elem;
     152             :                 }
     153      372781 :                 node = (cmp < 0) ? node->rb_left : node->rb_right;
     154             :         }
     155             : 
     156       67278 :         return NULL;
     157             : }
     158             : 
     159      120969 : 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      120969 :         if (cache == NULL) {
     165       55753 :                 cache = global_cache;
     166             :         }
     167      120969 :         if (cache == NULL) {
     168       12430 :                 return false;
     169             :         }
     170             : 
     171      108539 :         e = memcache_find(cache, n, key);
     172      108539 :         if (e == NULL) {
     173       22519 :                 return false;
     174             :         }
     175             : 
     176       86020 :         if (cache->size != 0) {
     177       86020 :                 DLIST_PROMOTE(cache->mru, e);
     178             :         }
     179             : 
     180       86020 :         memcache_element_parse(e, &key, value);
     181       86020 :         return true;
     182             : }
     183             : 
     184      103232 : 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      103232 :         if (!memcache_lookup(cache, n, key, &value)) {
     191       30074 :                 return NULL;
     192             :         }
     193             : 
     194       73158 :         if (value.length != sizeof(mtv)) {
     195           0 :                 return NULL;
     196             :         }
     197             : 
     198       73158 :         memcpy(&mtv, value.data, sizeof(mtv));
     199             : 
     200       73158 :         return mtv.ptr;
     201             : }
     202             : 
     203       18337 : static void memcache_delete_element(struct memcache *cache,
     204             :                                     struct memcache_element *e)
     205             : {
     206       18337 :         rb_erase(&e->rb_node, &cache->tree);
     207             : 
     208       18337 :         DLIST_REMOVE(cache->mru, e);
     209             : 
     210       18337 :         if (memcache_is_talloc(e->n)) {
     211             :                 DATA_BLOB cache_key, cache_value;
     212             :                 struct memcache_talloc_value mtv;
     213             : 
     214       18337 :                 memcache_element_parse(e, &cache_key, &cache_value);
     215       18337 :                 SMB_ASSERT(cache_value.length == sizeof(mtv));
     216       18337 :                 memcpy(&mtv, cache_value.data, sizeof(mtv));
     217       18337 :                 cache->size -= mtv.len;
     218       18337 :                 TALLOC_FREE(mtv.ptr);
     219             :         }
     220             : 
     221       18337 :         cache->size -= memcache_element_size(e->keylength, e->valuelength);
     222             : 
     223       18337 :         TALLOC_FREE(e);
     224       18337 : }
     225             : 
     226       44755 : static void memcache_trim(struct memcache *cache, struct memcache_element *e)
     227             : {
     228       44755 :         struct memcache_element *tail = NULL;
     229             : 
     230       44755 :         if (cache->max_size == 0) {
     231           0 :                 return;
     232             :         }
     233             : 
     234       44755 :         for (tail = DLIST_TAIL(cache->mru);
     235       44755 :              (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       17851 : void memcache_delete(struct memcache *cache, enum memcache_number n,
     249             :                      DATA_BLOB key)
     250             : {
     251             :         struct memcache_element *e;
     252             : 
     253       17851 :         if (cache == NULL) {
     254       17851 :                 cache = global_cache;
     255             :         }
     256       17851 :         if (cache == NULL) {
     257           3 :                 return;
     258             :         }
     259             : 
     260       17848 :         e = memcache_find(cache, n, key);
     261       17848 :         if (e == NULL) {
     262           4 :                 return;
     263             :         }
     264             : 
     265       17844 :         memcache_delete_element(cache, e);
     266             : }
     267             : 
     268       47020 : 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       47020 :         if (cache == NULL) {
     278        7580 :                 cache = global_cache;
     279             :         }
     280       47020 :         if (cache == NULL) {
     281        2265 :                 return;
     282             :         }
     283             : 
     284       46250 :         if (key.length == 0) {
     285           0 :                 return;
     286             :         }
     287             : 
     288       46250 :         e = memcache_find(cache, n, key);
     289             : 
     290       46250 :         if (e != NULL) {
     291        1495 :                 memcache_element_parse(e, &cache_key, &cache_value);
     292             : 
     293        1495 :                 if (value.length <= cache_value.length) {
     294        1495 :                         if (memcache_is_talloc(e->n)) {
     295             :                                 struct memcache_talloc_value mtv;
     296             : 
     297        1409 :                                 SMB_ASSERT(cache_value.length == sizeof(mtv));
     298        1409 :                                 memcpy(&mtv, cache_value.data, sizeof(mtv));
     299        1409 :                                 cache->size -= mtv.len;
     300        1409 :                                 TALLOC_FREE(mtv.ptr);
     301             :                         }
     302             :                         /*
     303             :                          * We can reuse the existing record
     304             :                          */
     305        1495 :                         memcpy(cache_value.data, value.data, value.length);
     306        1495 :                         e->valuelength = value.length;
     307             : 
     308        1495 :                         if (memcache_is_talloc(e->n)) {
     309             :                                 struct memcache_talloc_value mtv;
     310             : 
     311        1409 :                                 SMB_ASSERT(cache_value.length == sizeof(mtv));
     312        1409 :                                 memcpy(&mtv, cache_value.data, sizeof(mtv));
     313        1409 :                                 cache->size += mtv.len;
     314             :                         }
     315        1495 :                         return;
     316             :                 }
     317             : 
     318           0 :                 memcache_delete_element(cache, e);
     319             :         }
     320             : 
     321       44755 :         element_size = memcache_element_size(key.length, value.length);
     322             : 
     323       44755 :         e = talloc_size(cache, element_size);
     324       44755 :         if (e == NULL) {
     325           0 :                 DEBUG(0, ("talloc failed\n"));
     326           0 :                 return;
     327             :         }
     328       44755 :         talloc_set_type(e, struct memcache_element);
     329             : 
     330       44755 :         e->n = n;
     331       44755 :         e->keylength = key.length;
     332       44755 :         e->valuelength = value.length;
     333             : 
     334       44755 :         memcache_element_parse(e, &cache_key, &cache_value);
     335       44755 :         memcpy(cache_key.data, key.data, key.length);
     336       44755 :         memcpy(cache_value.data, value.data, value.length);
     337             : 
     338       44755 :         parent = NULL;
     339       44755 :         p = &cache->tree.rb_node;
     340             : 
     341      148886 :         while (*p) {
     342      104131 :                 struct memcache_element *elem = memcache_node2elem(*p);
     343             :                 int cmp;
     344             : 
     345      104131 :                 parent = (*p);
     346             : 
     347      104131 :                 cmp = memcache_compare(elem, n, key);
     348             : 
     349      104131 :                 p = (cmp < 0) ? &(*p)->rb_left : &(*p)->rb_right;
     350             :         }
     351             : 
     352       44755 :         rb_link_node(&e->rb_node, parent, p);
     353       44755 :         rb_insert_color(&e->rb_node, &cache->tree);
     354             : 
     355       44755 :         DLIST_ADD(cache->mru, e);
     356             : 
     357       44755 :         cache->size += element_size;
     358       44755 :         if (memcache_is_talloc(e->n)) {
     359             :                 struct memcache_talloc_value mtv;
     360             : 
     361       35481 :                 SMB_ASSERT(cache_value.length == sizeof(mtv));
     362       35481 :                 memcpy(&mtv, cache_value.data, sizeof(mtv));
     363       35481 :                 cache->size += mtv.len;
     364             :         }
     365       44755 :         memcache_trim(cache, e);
     366             : }
     367             : 
     368       39484 : 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       39484 :         void **ptr = (void **)pptr;
     373             : 
     374       39484 :         if (cache == NULL) {
     375       26912 :                 cache = global_cache;
     376             :         }
     377       39484 :         if (cache == NULL) {
     378        2594 :                 return;
     379             :         }
     380             : 
     381       36890 :         mtv.len = talloc_total_size(*ptr);
     382       36890 :         mtv.ptr = talloc_move(cache, ptr);
     383       36890 :         memcache_add(cache, n, key, data_blob_const(&mtv, sizeof(mtv)));
     384             : }
     385             : 
     386        1021 : void memcache_flush(struct memcache *cache, enum memcache_number n)
     387             : {
     388             :         struct rb_node *node;
     389             : 
     390        1021 :         if (cache == NULL) {
     391         838 :                 cache = global_cache;
     392             :         }
     393        1021 :         if (cache == NULL) {
     394         311 :                 return;
     395             :         }
     396             : 
     397             :         /*
     398             :          * Find the smallest element of number n
     399             :          */
     400             : 
     401         710 :         node = cache->tree.rb_node;
     402         710 :         if (node == NULL) {
     403           0 :                 return;
     404             :         }
     405             : 
     406             :         /*
     407             :          * First, find *any* element of number n
     408             :          */
     409             : 
     410        1201 :         while (true) {
     411        1911 :                 struct memcache_element *elem = memcache_node2elem(node);
     412             :                 struct rb_node *next;
     413             : 
     414        1911 :                 if ((int)elem->n == (int)n) {
     415         491 :                         break;
     416             :                 }
     417             : 
     418        1420 :                 if ((int)elem->n < (int)n) {
     419         893 :                         next = node->rb_right;
     420             :                 }
     421             :                 else {
     422         527 :                         next = node->rb_left;
     423             :                 }
     424        1420 :                 if (next == NULL) {
     425         219 :                         break;
     426             :                 }
     427        1201 :                 node = next;
     428             :         }
     429             : 
     430             :         /*
     431             :          * Then, find the leftmost element with number n
     432             :          */
     433             : 
     434           2 :         while (true) {
     435         712 :                 struct rb_node *prev = rb_prev(node);
     436             :                 struct memcache_element *elem;
     437             : 
     438         712 :                 if (prev == NULL) {
     439           0 :                         break;
     440             :                 }
     441         712 :                 elem = memcache_node2elem(prev);
     442         712 :                 if ((int)elem->n != (int)n) {
     443         710 :                         break;
     444             :                 }
     445           2 :                 node = prev;
     446             :         }
     447             : 
     448        1203 :         while (node != NULL) {
     449        1203 :                 struct memcache_element *e = memcache_node2elem(node);
     450        1203 :                 struct rb_node *next = rb_next(node);
     451             : 
     452        1203 :                 if (e->n != n) {
     453         710 :                         break;
     454             :                 }
     455             : 
     456         493 :                 memcache_delete_element(cache, e);
     457         493 :                 node = next;
     458             :         }
     459             : }

Generated by: LCOV version 1.14