544
544
cache->c_lruhead = cache->c_lrutail = NULL;
545
545
cache_make_hashes(cache, type);
547
if (((cache->c_mutex = PR_NewLock()) == NULL) ||
548
((cache->c_emutexalloc_mutex = PR_NewLock()) == NULL)) {
549
LDAPDebug(LDAP_DEBUG_ANY, "ldbm: cache_init: PR_NewLock failed\n",
547
if (((cache->c_mutex = PR_NewMonitor()) == NULL) ||
548
((cache->c_emutexalloc_mutex = PR_NewLock()) == NULL)) {
549
LDAPDebug0Args(LDAP_DEBUG_ANY, "ldbm: cache_init: PR_NewMonitor failed\n");
553
552
LDAPDebug(LDAP_DEBUG_TRACE, "<= cache_init\n", 0, 0, 0);
787
786
long *nentries, long *maxentries,
788
787
size_t *size, size_t *maxsize)
790
PR_Lock(cache->c_mutex);
791
790
if (hits) *hits = slapi_counter_get_value(cache->c_hits);
792
791
if (tries) *tries = slapi_counter_get_value(cache->c_tries);
793
792
if (nentries) *nentries = cache->c_curentries;
794
793
if (maxentries) *maxentries = cache->c_maxentries;
795
794
if (size) *size = slapi_counter_get_value(cache->c_cursize);
796
795
if (maxsize) *maxsize = cache->c_maxsize;
797
PR_Unlock(cache->c_mutex);
800
799
void cache_debug_hash(struct cache *cache, char **out)
1035
1034
/* If fails, we have to make sure the both entires are removed from the cache,
1036
1035
* otherwise, we have no idea what's left in the cache or not... */
1037
if (!entry_same_dn(newe, (void *)oldndn) && (newe->ep_state & ENTRY_STATE_NOTINCACHE) == 0) {
1036
if (cache_is_in_cache(cache, newe)) {
1038
1037
/* if we're doing a modrdn or turning an entry to a tombstone,
1039
1038
* the new entry can be in the dn table already, so we need to remove that too.
1073
1073
* tested enough that we believe it works.)
1075
1075
if (!add_hash(cache->c_dntable, (void *)newndn, strlen(newndn), newe, (void **)&alte)) {
1076
LOG("entry cache replace (%s): can't add to dn table (returned %s)\n",
1077
newndn, alte?slapi_entry_get_dn(alte->ep_entry):"none", 0);
1078
PR_Unlock(cache->c_mutex);
1076
LOG("entry cache replace (%s): can't add to dn table (returned %s)\n",
1077
newndn, alte?slapi_entry_get_dn(alte->ep_entry):"none", 0);
1081
1081
if (!add_hash(cache->c_idtable, &(newe->ep_id), sizeof(ID), newe, (void **)&alte)) {
1082
LOG("entry cache replace (%s): can't add to id table (returned %s)\n",
1083
newndn, alte?slapi_entry_get_dn(alte->ep_entry):"none", 0);
1084
if(remove_hash(cache->c_dntable, (void *)newndn, strlen(newndn)) == 0){
1085
LOG("entry cache replace: failed to remove dn table\n", 0, 0, 0);
1087
PR_Unlock(cache->c_mutex);
1082
LOG("entry cache replace (%s): can't add to id table (returned %s)\n",
1083
newndn, alte?slapi_entry_get_dn(alte->ep_entry):"none", 0);
1084
if(remove_hash(cache->c_dntable, (void *)newndn, strlen(newndn)) == 0){
1085
LOG("entry cache replace: failed to remove dn table\n", 0, 0, 0);
1087
cache_unlock(cache);
1090
1090
#ifdef UUIDCACHE_ON
1091
1091
if (newuuid && !add_hash(cache->c_uuidtable, (void *)newuuid, strlen(newuuid),
1093
LOG("entry cache replace: can't add uuid\n", 0, 0, 0);
1094
if(remove_hash(cache->c_dntable, (void *)newndn, strlen(newndn)) == 0){
1095
LOG("entry cache replace: failed to remove dn table(uuid cache)\n", 0, 0, 0);
1097
if(remove_hash(cache->c_idtable, &(newe->ep_id), sizeof(ID)) == 0){
1098
LOG("entry cache replace: failed to remove id table(uuid cache)\n", 0, 0, 0);
1100
PR_Unlock(cache->c_mutex);
1093
LOG("entry cache replace: can't add uuid\n", 0, 0, 0);
1094
if(remove_hash(cache->c_dntable, (void *)newndn, strlen(newndn)) == 0){
1095
LOG("entry cache replace: failed to remove dn table(uuid cache)\n", 0, 0, 0);
1097
if(remove_hash(cache->c_idtable, &(newe->ep_id), sizeof(ID)) == 0){
1098
LOG("entry cache replace: failed to remove id table(uuid cache)\n", 0, 0, 0);
1100
cache_unlock(cache);
1104
1104
/* adjust cache meta info */
1185
1189
LOG("=> cache_find_dn (%s)\n", dn, 0, 0);
1187
1191
/*entry normalized by caller (dn2entry.c) */
1188
PR_Lock(cache->c_mutex);
1189
1193
if (find_hash(cache->c_dntable, (void *)dn, ndnlen, (void **)&e)) {
1190
1194
/* need to check entry state */
1191
1195
if (e->ep_state != 0) {
1192
1196
/* entry is deleted or not fully created yet */
1193
PR_Unlock(cache->c_mutex);
1197
cache_unlock(cache);
1194
1198
LOG("<= cache_find_dn (NOT FOUND)\n", 0, 0, 0);
1197
1201
if (e->ep_refcnt == 0)
1198
1202
lru_delete(cache, (void *)e);
1199
1203
e->ep_refcnt++;
1200
PR_Unlock(cache->c_mutex);
1204
cache_unlock(cache);
1201
1205
slapi_counter_increment(cache->c_hits);
1203
PR_Unlock(cache->c_mutex);
1207
cache_unlock(cache);
1205
1209
slapi_counter_increment(cache->c_tries);
1217
1221
LOG("=> cache_find_id (%lu)\n", (u_long)id, 0, 0);
1219
PR_Lock(cache->c_mutex);
1220
1224
if (find_hash(cache->c_idtable, &id, sizeof(ID), (void **)&e)) {
1221
1225
/* need to check entry state */
1222
1226
if (e->ep_state != 0) {
1223
1227
/* entry is deleted or not fully created yet */
1224
PR_Unlock(cache->c_mutex);
1228
cache_unlock(cache);
1225
1229
LOG("<= cache_find_id (NOT FOUND)\n", 0, 0, 0);
1228
1232
if (e->ep_refcnt == 0)
1229
1233
lru_delete(cache, (void *)e);
1230
1234
e->ep_refcnt++;
1231
PR_Unlock(cache->c_mutex);
1235
cache_unlock(cache);
1232
1236
slapi_counter_increment(cache->c_hits);
1234
PR_Unlock(cache->c_mutex);
1238
cache_unlock(cache);
1236
1240
slapi_counter_increment(cache->c_tries);
1248
1252
LOG("=> cache_find_uuid (%s)\n", uuid, 0, 0);
1250
PR_Lock(cache->c_mutex);
1251
1255
if (find_hash(cache->c_uuidtable, uuid, strlen(uuid), (void **)&e)) {
1252
1256
/* need to check entry state */
1253
1257
if (e->ep_state != 0) {
1254
1258
/* entry is deleted or not fully created yet */
1255
PR_Unlock(cache->c_mutex);
1259
cache_unlock(cache);
1256
1260
LOG("<= cache_find_uuid (NOT FOUND)\n", 0, 0, 0);
1259
1263
if (e->ep_refcnt == 0)
1260
1264
lru_delete(cache, (void *)e);
1261
1265
e->ep_refcnt++;
1262
PR_Unlock(cache->c_mutex);
1266
cache_unlock(cache);
1263
1267
slapi_counter_increment(cache->c_hits);
1265
PR_Unlock(cache->c_mutex);
1269
cache_unlock(cache);
1267
1271
slapi_counter_increment(cache->c_tries);
1399
1405
* fine (i think).
1401
1407
LOG("<= entrycache_add_int (ignoring)\n", 0, 0, 0);
1402
PR_Unlock(cache->c_mutex);
1408
cache_unlock(cache);
1405
1411
if(remove_hash(cache->c_dntable, (void *)ndn, strlen(ndn)) == 0){
1406
1412
LOG("entrycache_add_int: failed to remove %s from dn table\n", 0, 0, 0);
1408
1414
e->ep_state |= ENTRY_STATE_NOTINCACHE;
1409
PR_Unlock(cache->c_mutex);
1415
cache_unlock(cache);
1410
1416
LOG("entrycache_add_int: failed to add %s to cache (ep_state: %x, already_in: %d)\n",
1411
1417
ndn, e->ep_state, already_in);
1540
1546
PR_EnterMonitor(e->ep_mutexp);
1542
1548
/* make sure entry hasn't been deleted now */
1543
PR_Lock(cache->c_mutex);
1544
1550
if (e->ep_state & (ENTRY_STATE_DELETED|ENTRY_STATE_NOTINCACHE)) {
1545
PR_Unlock(cache->c_mutex);
1551
cache_unlock(cache);
1546
1552
PR_ExitMonitor(e->ep_mutexp);
1547
1553
LOG("<= cache_lock_entry (DELETED)\n", 0, 0, 0);
1548
1554
return RETRY_CACHE_LOCK;
1550
PR_Unlock(cache->c_mutex);
1556
cache_unlock(cache);
1552
1558
LOG("<= cache_lock_entry (FOUND)\n", 0, 0, 0);
1739
1745
LOG("=> dncache_find_id (%lu)\n", (u_long)id, 0, 0);
1741
PR_Lock(cache->c_mutex);
1742
1748
if (find_hash(cache->c_idtable, &id, sizeof(ID), (void **)&bdn)) {
1743
1749
/* need to check entry state */
1744
1750
if (bdn->ep_state != 0) {
1745
1751
/* entry is deleted or not fully created yet */
1746
PR_Unlock(cache->c_mutex);
1752
cache_unlock(cache);
1747
1753
LOG("<= dncache_find_id (NOT FOUND)\n", 0, 0, 0);
1750
1756
if (bdn->ep_refcnt == 0)
1751
1757
lru_delete(cache, (void *)bdn);
1752
1758
bdn->ep_refcnt++;
1753
PR_Unlock(cache->c_mutex);
1759
cache_unlock(cache);
1754
1760
slapi_counter_increment(cache->c_hits);
1756
PR_Unlock(cache->c_mutex);
1762
cache_unlock(cache);
1758
1764
slapi_counter_increment(cache->c_tries);
1824
1830
LOG("the entry is reserved\n", 0, 0, 0);
1825
1831
bdn->ep_state |= ENTRY_STATE_NOTINCACHE;
1826
PR_Unlock(cache->c_mutex);
1832
cache_unlock(cache);
1829
1835
else if (state != 0)
1831
1837
LOG("the entry already exists. cannot reserve it.\n", 0, 0, 0);
1832
1838
bdn->ep_state |= ENTRY_STATE_NOTINCACHE;
1833
PR_Unlock(cache->c_mutex);
1839
cache_unlock(cache);
2023
2029
#ifdef CACHE_DEBUG
2025
check_entry_cache(struct cache *cache, struct backentry *e, int in_cache)
2031
check_entry_cache(struct cache *cache, struct backentry *e)
2027
2033
Slapi_DN *sdn = slapi_entry_get_sdn(e->ep_entry);
2028
2034
struct backentry *debug_e = cache_find_dn(cache,
2029
2035
slapi_sdn_get_dn(sdn),
2030
2036
slapi_sdn_get_ndn_len(sdn));
2037
in_cache = cache_is_in_cache(cache, (void *)e);
2031
2038
if (in_cache) {
2032
2039
if (debug_e) { /* e is in cache */
2033
2040
CACHE_RETURN(cache, &debug_e);
2034
2041
if ((e != debug_e) && !(e->ep_state & ENTRY_STATE_DELETED)) {
2035
slapi_log_error(SLAPI_LOG_FATAL, "ldbm_back_delete",
2042
slapi_log_error(SLAPI_LOG_FATAL, "check_entry_cache",
2036
2043
"entry 0x%p is not in dn cache but 0x%p having the same dn %s is "
2037
2044
"although in_cache flag is set!!!\n",
2038
2045
e, debug_e, slapi_sdn_get_dn(sdn));
2040
2047
} else if (!(e->ep_state & ENTRY_STATE_DELETED)) {
2041
slapi_log_error(SLAPI_LOG_FATAL, "ldbm_back_delete",
2048
slapi_log_error(SLAPI_LOG_FATAL, "check_entry_cache",
2042
2049
"%s (id %d) is not in dn cache although in_cache flag is set!!!\n",
2043
2050
slapi_sdn_get_dn(sdn), e->ep_id);
2046
2053
if (debug_e) { /* e is in cache */
2047
2054
CACHE_RETURN(cache, &debug_e);
2048
2055
if ((e != debug_e) && !(e->ep_state & ENTRY_STATE_DELETED)) {
2049
slapi_log_error(SLAPI_LOG_FATAL, "ldbm_back_delete",
2056
slapi_log_error(SLAPI_LOG_FATAL, "check_entry_cache",
2050
2057
"entry 0x%p is not in id cache but 0x%p having the same id %d is "
2051
2058
"although in_cache flag is set!!!\n",
2052
2059
e, debug_e, e->ep_id);
2055
slapi_log_error(SLAPI_LOG_CACHE, "ldbm_back_delete",
2062
slapi_log_error(SLAPI_LOG_CACHE, "check_entry_cache",
2056
2063
"%s (id %d) is not in id cache although in_cache flag is set!!!\n",
2057
2064
slapi_sdn_get_dn(sdn), e->ep_id);
2060
2067
if (debug_e) { /* e is in cache */
2061
2068
CACHE_RETURN(cache, &debug_e);
2062
2069
if (e == debug_e) {
2063
slapi_log_error(SLAPI_LOG_FATAL, "ldbm_back_delete",
2070
slapi_log_error(SLAPI_LOG_FATAL, "check_entry_cache",
2064
2071
"%s (id %d) is in dn cache although in_cache flag is not set!!!\n",
2065
2072
slapi_sdn_get_dn(sdn), e->ep_id);
2069
2076
if (debug_e) { /* e is in cache: bad */
2070
2077
CACHE_RETURN(cache, &debug_e);
2071
2078
if (e == debug_e) {
2072
slapi_log_error(SLAPI_LOG_CACHE, "ldbm_back_delete",
2079
slapi_log_error(SLAPI_LOG_CACHE, "check_entry_cache",
2073
2080
"%s (id %d) is in id cache although in_cache flag is not set!!!\n",
2074
2081
slapi_sdn_get_dn(sdn), e->ep_id);
2089
cache_has_otherref(struct cache *cache, void *ptr)
2091
struct backcommon *bep;
2097
bep = (struct backcommon *)ptr;
2098
/* slows down too much? PR_Lock(cache->c_mutex); */
2099
hasref = bep->ep_refcnt;
2100
/* PR_Unlock(cache->c_mutex); */
2101
return (hasref>1)?1:0;
2105
cache_is_in_cache(struct cache *cache, void *ptr)
2107
struct backcommon *bep;
2113
bep = (struct backcommon *)ptr;
2114
/* slows down too much? PR_Lock(cache->c_mutex); */
2115
in_cache = (bep->ep_state & (ENTRY_STATE_DELETED|ENTRY_STATE_NOTINCACHE))?0:1;
2116
/* PR_Unlock(cache->c_mutex); */