]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-ldbm/dbcache.c
Removed unnecessary definition that is already in core.schema.
[openldap] / servers / slapd / back-ldbm / dbcache.c
index bef299be86f319f87dd56793d26c3f2c17051d5b..b624ef55a3e38a36328cbf0139bcd332e27726a3 100644 (file)
@@ -3,26 +3,21 @@
 #include "portable.h"
 
 #include <stdio.h>
+
+#include <ac/errno.h>
+#include <ac/socket.h>
 #include <ac/string.h>
 #include <ac/time.h>
 
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <sys/param.h>
 #include <sys/stat.h>
-#include <errno.h>
 
-#include "slap.h"
-#include "back-ldbm.h"
-#include "ldapconfig.h"
-
-#ifdef DECL_SYS_ERRLIST
-extern int             sys_nerr;
-extern char            *sys_errlist[];
+#ifdef HAVE_SYS_PARAM_H
+#include <sys/param.h>
 #endif
 
-extern time_t          currenttime;
-extern pthread_mutex_t currenttime_mutex;
+#include "ldapconfig.h"
+#include "slap.h"
+#include "back-ldbm.h"
 
 struct dbcache *
 ldbm_cache_open(
@@ -36,21 +31,20 @@ ldbm_cache_open(
        int             i, lru;
        time_t          oldtime, curtime;
        char            buf[MAXPATHLEN];
-       LDBM            db;
+#ifdef HAVE_ST_BLKSIZE
        struct stat     st;
+#endif
 
-       sprintf( buf, "%s/%s%s", li->li_directory, name, suffix );
+       sprintf( buf, "%s%s%s%s", li->li_directory, DIRSEP, name, suffix );
 
        Debug( LDAP_DEBUG_TRACE, "=> ldbm_cache_open( \"%s\", %d, %o )\n", buf,
            flags, li->li_mode );
 
        lru = 0;
-       pthread_mutex_lock( &currenttime_mutex );
-       curtime = currenttime;
-       pthread_mutex_unlock( &currenttime_mutex );
+       curtime = slap_get_time();
        oldtime = curtime;
 
-       pthread_mutex_lock( &li->li_dbcache_mutex );
+       ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
        for ( i = 0; i < MAXDBCACHE && li->li_dbcache[i].dbc_name != NULL;
            i++ ) {
                /* already open - return it */
@@ -58,7 +52,7 @@ ldbm_cache_open(
                        li->li_dbcache[i].dbc_refcnt++;
                        Debug( LDAP_DEBUG_TRACE,
                            "<= ldbm_cache_open (cache %d)\n", i, 0, 0 );
-                       pthread_mutex_unlock( &li->li_dbcache_mutex );
+                       ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
                        return( &li->li_dbcache[i] );
                }
 
@@ -79,7 +73,7 @@ ldbm_cache_open(
                            0, 0, 0 );
                        lru = -1;
                        while ( lru == -1 ) {
-                               pthread_cond_wait( &li->li_dbcache_cv,
+                               ldap_pvt_thread_cond_wait( &li->li_dbcache_cv,
                                    &li->li_dbcache_mutex );
                                for ( i = 0; i < MAXDBCACHE; i++ ) {
                                        if ( li->li_dbcache[i].dbc_refcnt
@@ -98,32 +92,36 @@ ldbm_cache_open(
 
        if ( (li->li_dbcache[i].dbc_db = ldbm_open( buf, flags, li->li_mode,
            li->li_dbcachesize )) == NULL ) {
+               int err = errno;
                Debug( LDAP_DEBUG_TRACE,
                    "<= ldbm_cache_open NULL \"%s\" errno %d reason \"%s\")\n",
-                   buf, errno, errno > -1 && errno < sys_nerr ?
-                   sys_errlist[errno] : "unknown" );
-               pthread_mutex_unlock( &li->li_dbcache_mutex );
+                   buf, err, err > -1 && err < sys_nerr ?
+                   sys_errlist[err] : "unknown" );
+               ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
                return( NULL );
        }
-       li->li_dbcache[i].dbc_name = strdup( buf );
+       li->li_dbcache[i].dbc_name = ch_strdup( buf );
        li->li_dbcache[i].dbc_refcnt = 1;
        li->li_dbcache[i].dbc_lastref = curtime;
+#ifdef HAVE_ST_BLKSIZE
        if ( stat( buf, &st ) == 0 ) {
                li->li_dbcache[i].dbc_blksize = st.st_blksize;
-       } else {
+       } else
+#endif
+       {
                li->li_dbcache[i].dbc_blksize = DEFAULT_BLOCKSIZE;
        }
        li->li_dbcache[i].dbc_maxids = (li->li_dbcache[i].dbc_blksize /
-           sizeof(ID)) - 2;
+           sizeof(ID)) - ID_BLOCK_IDS_OFFSET;
        li->li_dbcache[i].dbc_maxindirect = (SLAPD_LDBM_MIN_MAXIDS /
            li->li_dbcache[i].dbc_maxids) + 1;
 
        Debug( LDAP_DEBUG_ARGS,
-           "ldbm_cache_open (blksize %d) (maxids %d) (maxindirect %d)\n",
+           "ldbm_cache_open (blksize %ld) (maxids %d) (maxindirect %d)\n",
            li->li_dbcache[i].dbc_blksize, li->li_dbcache[i].dbc_maxids,
            li->li_dbcache[i].dbc_maxindirect );
        Debug( LDAP_DEBUG_TRACE, "<= ldbm_cache_open (opened %d)\n", i, 0, 0 );
-       pthread_mutex_unlock( &li->li_dbcache_mutex );
+       ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
        return( &li->li_dbcache[i] );
 }
 
@@ -132,11 +130,11 @@ ldbm_cache_close( Backend *be, struct dbcache *db )
 {
        struct ldbminfo *li = (struct ldbminfo *) be->be_private;
 
-       pthread_mutex_lock( &li->li_dbcache_mutex );
+       ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
        if ( --db->dbc_refcnt == 0 ) {
-               pthread_cond_signal( &li->li_dbcache_cv );
+               ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
        }
-       pthread_mutex_unlock( &li->li_dbcache_mutex );
+       ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
 }
 
 void
@@ -144,14 +142,14 @@ ldbm_cache_really_close( Backend *be, struct dbcache *db )
 {
        struct ldbminfo *li = (struct ldbminfo *) be->be_private;
 
-       pthread_mutex_lock( &li->li_dbcache_mutex );
+       ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
        if ( --db->dbc_refcnt == 0 ) {
-               pthread_cond_signal( &li->li_dbcache_cv );
+               ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
                ldbm_close( db->dbc_db );
                free( db->dbc_name );
                db->dbc_name = NULL;
        }
-       pthread_mutex_unlock( &li->li_dbcache_mutex );
+       ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
 }
 
 void
@@ -160,17 +158,31 @@ ldbm_cache_flush_all( Backend *be )
        struct ldbminfo *li = (struct ldbminfo *) be->be_private;
        int             i;
 
-       pthread_mutex_lock( &li->li_dbcache_mutex );
+       ldap_pvt_thread_mutex_lock( &li->li_dbcache_mutex );
        for ( i = 0; i < MAXDBCACHE; i++ ) {
                if ( li->li_dbcache[i].dbc_name != NULL ) {
                        Debug( LDAP_DEBUG_TRACE, "ldbm flushing db (%s)\n",
                            li->li_dbcache[i].dbc_name, 0, 0 );
-                       pthread_mutex_lock( &li->li_dbcache[i].dbc_mutex );
                        ldbm_sync( li->li_dbcache[i].dbc_db );
-                       pthread_mutex_unlock( &li->li_dbcache[i].dbc_mutex );
+#ifdef SLAP_CLEANUP
+                       if ( li->li_dbcache[i].dbc_refcnt != 0 ) {
+                               Debug( LDAP_DEBUG_TRACE,
+                                      "refcnt = %d, couldn't close db (%s)\n",
+                                      li->li_dbcache[i].dbc_refcnt,
+                                      li->li_dbcache[i].dbc_name, 0 );
+                       } else {
+                               Debug( LDAP_DEBUG_TRACE,
+                                      "ldbm closing db (%s)\n",
+                                      li->li_dbcache[i].dbc_name, 0, 0 );
+                               ldap_pvt_thread_cond_signal( &li->li_dbcache_cv );
+                               ldbm_close( li->li_dbcache[i].dbc_db );
+                               free( li->li_dbcache[i].dbc_name );
+                               li->li_dbcache[i].dbc_name = NULL;
+                       }
+#endif /* SLAP_CLEANUP */
                }
        }
-       pthread_mutex_unlock( &li->li_dbcache_mutex );
+       ldap_pvt_thread_mutex_unlock( &li->li_dbcache_mutex );
 }
 
 Datum
@@ -180,28 +192,11 @@ ldbm_cache_fetch(
 )
 {
        Datum   data;
-#ifdef LDBM_USE_DB2
-       memset( &data, 0, sizeof( data ) );
-#endif
 
-       pthread_mutex_lock( &db->dbc_mutex );
-#ifdef reentrant_database
-       /* increment reader count */
-       db->dbc_readers++
-       pthread_mutex_unlock( &db->dbc_mutex );
-#endif
+       ldbm_datum_init( data );
 
        data = ldbm_fetch( db->dbc_db, key );
 
-#ifdef reentrant_database
-       pthread_mutex_lock( &db->dbc_mutex );
-       /* decrement reader count & signal any waiting writers */
-       if ( --db->dbc_readers == 0 ) {
-               pthread_cond_signal( &db->dbc_cv );
-       }
-#endif
-       pthread_mutex_unlock( &db->dbc_mutex );
-
        return( data );
 }
 
@@ -215,14 +210,6 @@ ldbm_cache_store(
 {
        int     rc;
 
-       pthread_mutex_lock( &db->dbc_mutex );
-#ifdef reentrant_database
-       /* wait for reader count to drop to zero */
-       while ( db->dbc_readers > 0 ) {
-               pthread_cond_wait( &db->dbc_cv, &db->dbc_mutex );
-       }
-#endif
-
 #ifdef LDBM_DEBUG
        Statslog( LDAP_DEBUG_STATS,
                "=> ldbm_cache_store(): key.dptr=%s, key.dsize=%d\n",
@@ -243,8 +230,6 @@ ldbm_cache_store(
 
        rc = ldbm_store( db->dbc_db, key, data, flags );
 
-       pthread_mutex_unlock( &db->dbc_mutex );
-
        return( rc );
 }
 
@@ -256,17 +241,7 @@ ldbm_cache_delete(
 {
        int     rc;
 
-       pthread_mutex_lock( &db->dbc_mutex );
-#ifdef reentrant_database
-       /* wait for reader count to drop to zero - then write */
-       while ( db->dbc_readers > 0 ) {
-               pthread_cond_wait( &db->dbc_cv, &db->dbc_mutex );
-       }
-#endif
-
        rc = ldbm_delete( db->dbc_db, key );
 
-       pthread_mutex_unlock( &db->dbc_mutex );
-
        return( rc );
 }