]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-bdb/init.c
use slab memory for proxyauthz
[openldap] / servers / slapd / back-bdb / init.c
index df3221e9fce12c3888188a366dd63239a058612f..eff1b5db620375bf5c7791fa3c0187ca1d9fc391 100644 (file)
@@ -2,7 +2,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 2000-2005 The OpenLDAP Foundation.
+ * Copyright 2000-2006 The OpenLDAP Foundation.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
 #include <ac/stdlib.h>
 #include <ac/errno.h>
 #include <sys/stat.h>
-#ifdef HAVE_UTIME_H
-#  ifdef HAVE_SYS_TYPES_H
-#    include <sys/types.h>
-#  endif
-#  include <utime.h>
-#endif /* HAVE_UTIME_H */
 #include "back-bdb.h"
 #include <lutil.h>
 #include <ldap_rq.h>
@@ -75,7 +69,8 @@ bdb_db_init( BackendDB *be )
 #ifdef BDB_HIER
        ldap_pvt_thread_mutex_init( &bdb->bi_modrdns_mutex );
 #endif
-       ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_mutex );
+       ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_head_mutex );
+       ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_tail_mutex );
        ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_dntree.bei_kids_mutex );
        ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
        ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
@@ -87,6 +82,9 @@ bdb_db_init( BackendDB *be )
        return 0;
 }
 
+static int
+bdb_db_close( BackendDB *be );
+
 static int
 bdb_db_open( BackendDB *be )
 {
@@ -186,7 +184,7 @@ bdb_db_open( BackendDB *be )
                Debug( LDAP_DEBUG_ANY,
                        "bdb_db_open: db_env_create failed: %s (%d)\n",
                        db_strerror(rc), rc, 0 );
-               return rc;
+               goto fail;
        }
 
        bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
@@ -204,7 +202,7 @@ bdb_db_open( BackendDB *be )
                        Debug( LDAP_DEBUG_ANY,
                                "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
                                db_strerror(rc), rc, 0 );
-                       return rc;
+                       goto fail;
                }
        }
 
@@ -254,7 +252,8 @@ bdb_db_open( BackendDB *be )
                                Debug( LDAP_DEBUG_ANY,
                                        "bdb_db_open: Database cannot be recovered. "
                                        "Restore from backup!\n", 0, 0, 0);
-                               return -1;
+                               rc = -1;
+                               goto fail;
                        }
                        /* We need to recover, and we had TXN support before:
                         * Close this env, open a new one with recovery flags.
@@ -267,7 +266,7 @@ bdb_db_open( BackendDB *be )
                                        Debug( LDAP_DEBUG_ANY,
                                                "bdb_db_open: db_env_create failed: %s (%d)\n",
                                                db_strerror(rc), rc, 0 );
-                                       return rc;
+                                       goto fail;
                                }
                                bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv,
                                        be->be_suffix[0].bv_val );
@@ -278,12 +277,21 @@ bdb_db_open( BackendDB *be )
                                        Debug( LDAP_DEBUG_ANY,
                                                "bdb_db_open: recovery failed: %s (%d)\n",
                                                db_strerror(rc), rc, 0 );
-                                       return rc;
+                                       goto fail;
                                }
                                do_recover = 0;
                        }
                        /* Prev environment had TXN support, get rid of it */
                        if ( !flags_ok ) {
+                               bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
+                               bdb->bi_dbenv = NULL;
+                               rc = db_env_create( &bdb->bi_dbenv, 0 );
+                               if( rc != 0 ) {
+                                       Debug( LDAP_DEBUG_ANY,
+                                               "bdb_db_open: db_env_create failed: %s (%d)\n",
+                                               db_strerror(rc), rc, 0 );
+                                       goto fail;
+                               }
                                bdb->bi_dbenv->remove( bdb->bi_dbenv, dbhome, 0 );
                                bdb->bi_dbenv = NULL;
                        }
@@ -294,7 +302,8 @@ bdb_db_open( BackendDB *be )
                                Debug( LDAP_DEBUG_ANY,
                                        "bdb_db_open: Database cannot be recovered. "
                                        "Restore from backup!\n", 0, 0, 0);
-                               return -1;
+                               rc = -1;
+                               goto fail;
                        }
                        /* Prev environment had no TXN support, close it */
                        if ( !flags_ok ) {
@@ -309,12 +318,16 @@ bdb_db_open( BackendDB *be )
                        open_env = 0;
                } else {
                        /* Create a new env that can take the desired settings */
+                       if ( bdb->bi_dbenv != NULL ) {
+                               bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
+                               bdb->bi_dbenv = NULL;
+                       }
                        rc = db_env_create( &bdb->bi_dbenv, 0 );
                        if( rc != 0 ) {
                                Debug( LDAP_DEBUG_ANY,
                                        "bdb_db_open: db_env_create failed: %s (%d)\n",
                                        db_strerror(rc), rc, 0 );
-                               return rc;
+                               goto fail;
                        }
 
                        bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
@@ -331,7 +344,7 @@ bdb_db_open( BackendDB *be )
                                        Debug( LDAP_DEBUG_ANY,
                                                "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
                                                db_strerror(rc), rc, 0 );
-                                       return rc;
+                                       goto fail;
                                }
                        }
                }
@@ -368,7 +381,7 @@ bdb_db_open( BackendDB *be )
                        Debug( LDAP_DEBUG_ANY,
                                "bdb_db_open: dbenv_open failed: %s (%d)\n",
                                db_strerror(rc), rc, 0 );
-                       return rc;
+                       goto fail;
                }
        }
 
@@ -376,7 +389,8 @@ bdb_db_open( BackendDB *be )
                Debug( LDAP_DEBUG_ANY,
                        "bdb_db_open: alock_recover failed\n",
                        0, 0, 0 );
-               return -1;
+               rc = -1;
+               goto fail;
        }
 
 #ifdef SLAP_ZONE_ALLOC
@@ -415,10 +429,14 @@ bdb_db_open( BackendDB *be )
                        Debug( LDAP_DEBUG_ANY,
                                "bdb_db_open: db_create(%s) failed: %s (%d)\n",
                                bdb->bi_dbenv_home, db_strerror(rc), rc );
-                       return rc;
+                       goto fail;
                }
 
                if( i == BDB_ID2ENTRY ) {
+                       if ( slapMode & SLAP_TOOL_MODE )
+                               db->bdi_db->mpf->set_priority( db->bdi_db->mpf,
+                                       DB_PRIORITY_VERY_LOW );
+
                        rc = db->bdi_db->set_pagesize( db->bdi_db,
                                BDB_ID2ENTRY_PAGESIZE );
                        if ( slapMode & SLAP_TOOL_READMAIN ) {
@@ -472,15 +490,9 @@ bdb_db_open( BackendDB *be )
                        Debug( LDAP_DEBUG_ANY,
                                "bdb_db_open: db_open(%s) failed: %s (%d)\n",
                                buf, db_strerror(rc), rc );
-                       return rc;
-               }
-
-#if 0
-               if( i == BDB_ID2ENTRY && ( slapMode & SLAP_TOOL_MODE )) {
-                       db->bdi_db->mpf->set_priority( db->bdi_db->mpf,
-                               DB_PRIORITY_VERY_LOW );
+                       db->bdi_db->close( db->bdi_db, 0 );
+                       goto fail;
                }
-#endif
 
                flags &= ~(DB_CREATE | DB_RDONLY);
                db->bdi_name = bdbi_databases[i].name;
@@ -496,7 +508,7 @@ bdb_db_open( BackendDB *be )
                Debug( LDAP_DEBUG_ANY,
                        "bdb_db_open: last_id(%s) failed: %s (%d)\n",
                        bdb->bi_dbenv_home, db_strerror(rc), rc );
-               return rc;
+               goto fail;
        }
 
        if ( !( slapMode & SLAP_TOOL_QUICK )) {
@@ -506,6 +518,10 @@ bdb_db_open( BackendDB *be )
        bdb->bi_flags |= BDB_IS_OPEN;
 
        return 0;
+
+fail:
+       bdb_db_close( be );
+       return rc;
 }
 
 static int
@@ -516,10 +532,6 @@ bdb_db_close( BackendDB *be )
        struct bdb_db_info *db;
        bdb_idl_cache_entry_t *entry, *next_entry;
 
-       /* backend_shutdown closes everything, even if not all were opened */
-       if ( !( bdb->bi_flags & BDB_IS_OPEN ))
-               return 0;
-
        bdb->bi_flags &= ~BDB_IS_OPEN;
 
        ber_bvarray_free( bdb->bi_db_config );
@@ -539,7 +551,6 @@ bdb_db_close( BackendDB *be )
        bdb_cache_release_all (&bdb->bi_cache);
 
        if ( bdb->bi_idl_cache_max_size ) {
-               ldap_pvt_thread_rdwr_wlock ( &bdb->bi_idl_tree_rwlock );
                avl_free( bdb->bi_idl_tree, NULL );
                bdb->bi_idl_tree = NULL;
                entry = bdb->bi_idl_lru_head;
@@ -552,16 +563,16 @@ bdb_db_close( BackendDB *be )
                        entry = next_entry;
                }
                bdb->bi_idl_lru_head = bdb->bi_idl_lru_tail = NULL;
-               ldap_pvt_thread_rdwr_wunlock ( &bdb->bi_idl_tree_rwlock );
-       }
-
-       if ( !( slapMode & SLAP_TOOL_QUICK ) && bdb->bi_dbenv ) {
-               XLOCK_ID_FREE(bdb->bi_dbenv, bdb->bi_cache.c_locker);
-               bdb->bi_cache.c_locker = 0;
        }
 
        /* close db environment */
        if( bdb->bi_dbenv ) {
+               /* Free cache locker if we enabled locking */
+               if ( !( slapMode & SLAP_TOOL_QUICK )) {
+                       XLOCK_ID_FREE(bdb->bi_dbenv, bdb->bi_cache.c_locker);
+                       bdb->bi_cache.c_locker = 0;
+               }
+
                /* force a checkpoint, but not if we were ReadOnly,
                 * and not in Quick mode since there are no transactions there.
                 */
@@ -602,10 +613,11 @@ bdb_db_destroy( BackendDB *be )
        if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
        if( bdb->bi_db_config_path ) ch_free( bdb->bi_db_config_path );
 
-       bdb_attr_index_destroy( bdb->bi_attrs );
+       bdb_attr_index_destroy( bdb );
 
        ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
-       ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
+       ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_head_mutex );
+       ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_tail_mutex );
        ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
 #ifdef BDB_HIER
        ldap_pvt_thread_mutex_destroy( &bdb->bi_modrdns_mutex );
@@ -632,12 +644,8 @@ bdb_back_initialize(
                LDAP_CONTROL_MANAGEDSAIT,
                LDAP_CONTROL_NOOP,
                LDAP_CONTROL_PAGEDRESULTS,
-#ifdef LDAP_CONTROL_SUBENTRIES
                LDAP_CONTROL_SUBENTRIES,
-#endif
-#ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
                LDAP_CONTROL_X_PERMISSIVE_MODIFY,
-#endif
                NULL
        };
 
@@ -648,9 +656,7 @@ bdb_back_initialize(
 
        bi->bi_flags |=
                SLAP_BFLAG_INCREMENT |
-#ifdef BDB_SUBENTRIES
                SLAP_BFLAG_SUBENTRIES |
-#endif
                SLAP_BFLAG_ALIASES |
                SLAP_BFLAG_REFERRALS;