]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-bdb/init.c
ITS#3857 fix bottom-up modrdn cascades, use a global modrdn counter.
[openldap] / servers / slapd / back-bdb / init.c
index fb2af4d1db02c33f0bea7790811ced21babee473..bfd5296266561b3a660ccec9f62136b83797727f 100644 (file)
@@ -66,30 +66,21 @@ bdb_db_init( BackendDB *be )
 
        ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
        ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
+#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.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 );
+       ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
 
        be->be_private = bdb;
-       be->be_cf_table = be->bd_info->bi_cf_table;
+       be->be_cf_ocs = be->bd_info->bi_cf_ocs;
 
        return 0;
 }
 
-static void *
-bdb_checkpoint( void *ctx, void *arg )
-{
-       struct re_s *rtask = arg;
-       struct bdb_info *bdb = rtask->arg;
-       
-       TXN_CHECKPOINT( bdb->bi_dbenv, bdb->bi_txn_cp_kbyte,
-               bdb->bi_txn_cp_min, 0 );
-       ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
-       ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
-       ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
-       return NULL;
-}
-
 /*
  * Unconditionally perform a database recovery. Only works on
  * databases that were previously opened with transactions and
@@ -124,6 +115,12 @@ bdb_do_recovery( BackendDB *be )
        flags = DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL |
                DB_INIT_TXN | DB_USE_ENVIRON | DB_RECOVER;
 
+       /* If a key was set, use shared memory for the BDB environment */
+       if ( bdb->bi_shm_key ) {
+               re_dbenv->set_shm_key( re_dbenv, bdb->bi_shm_key );
+               flags |= DB_SYSTEM_MEM;
+       }
+
        /* Open the environment, which will also perform the recovery */
 #ifdef HAVE_EBCDIC
        strcpy( path, bdb->bi_dbenv_home );
@@ -215,7 +212,10 @@ bdb_db_recover( BackendDB *be )
 #endif
 
        if( rc == ENOENT ) {
-               goto re_exit;
+               Debug( LDAP_DEBUG_TRACE,
+                       "bdb_db_recover: DB environment files are missing, assuming it was "
+                       "manually recovered\n", 0, 0, 0 );
+               return 0;
        }
        else if( rc != 0 ) {
                Debug( LDAP_DEBUG_ANY,
@@ -393,8 +393,6 @@ bdb_db_open( BackendDB *be )
 
        if ( bdb->bi_idl_cache_max_size ) {
                bdb->bi_idl_tree = NULL;
-               ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
-               ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
                bdb->bi_idl_cache_size = 0;
        }
 
@@ -535,44 +533,7 @@ bdb_db_open( BackendDB *be )
                XLOCK_ID(bdb->bi_dbenv, &bdb->bi_cache.c_locker);
        }
 
-       /* If we're in server mode and time-based checkpointing is enabled,
-        * submit a task to perform periodic checkpoints.
-        */
-       if (( slapMode & SLAP_SERVER_MODE ) && bdb->bi_txn_cp &&
-               bdb->bi_txn_cp_min )  {
-               ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
-               ldap_pvt_runqueue_insert( &slapd_rq, bdb->bi_txn_cp_min*60,
-                       bdb_checkpoint, bdb );
-               ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
-       }
-
-       if ( slapMode & SLAP_SERVER_MODE && bdb->bi_db_has_config ) {
-               char    buf[SLAP_TEXT_BUFLEN];
-               FILE *f = fopen( bdb->bi_db_config_path, "r" );
-               struct berval bv;
-
-               if ( f ) {
-                       while ( fgets( buf, sizeof(buf), f )) {
-                               ber_str2bv( buf, 0, 1, &bv );
-                               if ( bv.bv_val[bv.bv_len-1] == '\n' ) {
-                                       bv.bv_len--;
-                                       bv.bv_val[bv.bv_len] = '\0';
-                               }
-                               /* shouldn't need this, but ... */
-                               if ( bv.bv_val[bv.bv_len-1] == '\r' ) {
-                                       bv.bv_len--;
-                                       bv.bv_val[bv.bv_len] = '\0';
-                               }
-                               ber_bvarray_add( &bdb->bi_db_config, &bv );
-                       }
-                       fclose( f );
-               } else {
-                       /* Eh? It disappeared between config and open?? */
-                       bdb->bi_db_has_config = 0;
-               }
-
-       }
-       bdb->bi_db_is_open = 1;
+       bdb->bi_flags |= BDB_IS_OPEN;
 
        return 0;
 }
@@ -585,11 +546,16 @@ bdb_db_close( BackendDB *be )
        struct bdb_db_info *db;
        bdb_idl_cache_entry_t *entry, *next_entry;
 
-       bdb->bi_db_is_open = 0;
+       /* 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 );
+       bdb->bi_db_config = NULL;
 
-       while( bdb->bi_ndatabases-- ) {
+       while( bdb->bi_databases && bdb->bi_ndatabases-- ) {
                db = bdb->bi_databases[bdb->bi_ndatabases];
                rc = db->bdi_db->close( db->bdi_db, 0 );
                /* Lower numbered names are not strdup'd */
@@ -598,13 +564,14 @@ bdb_db_close( BackendDB *be )
                free( db );
        }
        free( bdb->bi_databases );
-       bdb_attr_index_destroy( bdb->bi_attrs );
+       bdb->bi_databases = NULL;
 
        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;
                while ( entry != NULL ) {
                        next_entry = entry->idl_lru_next;
@@ -614,30 +581,25 @@ bdb_db_close( BackendDB *be )
                        free( entry );
                        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;
        }
 
-       return 0;
-}
-
-static int
-bdb_db_destroy( BackendDB *be )
-{
-       int rc;
-       struct bdb_info *bdb = (struct bdb_info *) be->be_private;
-
        /* close db environment */
        if( bdb->bi_dbenv ) {
-               /* force a checkpoint */
-               if ( !( slapMode & SLAP_TOOL_QUICK )) {
+               /* force a checkpoint, but not if we were ReadOnly,
+                * and not in Quick mode since there are no transactions there.
+                */
+               if ( !( slapMode & ( SLAP_TOOL_QUICK|SLAP_TOOL_READONLY ))) {
                        rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
                        if( rc != 0 ) {
                                Debug( LDAP_DEBUG_ANY,
-                                       "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
+                                       "bdb_db_close: txn_checkpoint failed: %s (%d)\n",
                                        db_strerror(rc), rc, 0 );
                        }
                }
@@ -646,31 +608,68 @@ bdb_db_destroy( BackendDB *be )
                bdb->bi_dbenv = NULL;
                if( rc != 0 ) {
                        Debug( LDAP_DEBUG_ANY,
-                               "bdb_db_destroy: close failed: %s (%d)\n",
+                               "bdb_db_close: close failed: %s (%d)\n",
                                db_strerror(rc), rc, 0 );
                        return rc;
                }
+
+#if DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR == 2
+               /* Delete the environment if we were in quick mode. This
+                * works around a bug in bdb4.2 that interferes with the
+                * operation of db_stat and other tools after a slapadd -q
+                * or slapindex -q has taken place.
+                */
+               if( slapMode & SLAP_TOOL_QUICK ) {
+                       rc = db_env_create( &bdb->bi_dbenv, 0 );
+                       if( rc != 0 ) {
+                               Debug( LDAP_DEBUG_ANY,
+                                       "bdb_db_close: db_env_create failed: %s (%d)\n",
+                                       db_strerror(rc), rc, 0 );
+                               return rc;
+                       }
+                       rc = bdb->bi_dbenv->remove(bdb->bi_dbenv, bdb->bi_dbenv_home,
+                                       DB_FORCE);
+                       bdb->bi_dbenv = NULL;
+                       if( rc != 0 ) {
+                               Debug( LDAP_DEBUG_ANY,
+                                       "bdb_db_close: dbenv_remove failed: %s (%d)\n",
+                                       db_strerror(rc), rc, 0 );
+                               return rc;
+                       }
+               }
+#endif
        }
 
        rc = alock_close( &bdb->bi_alock_info );
        if( rc != 0 ) {
                Debug( LDAP_DEBUG_ANY,
-                       "bdb_db_destroy: alock_close failed\n", 0, 0, 0 );
+                       "bdb_db_close: alock_close failed\n", 0, 0, 0 );
                return -1;
        }
 
+       return 0;
+}
+
+static int
+bdb_db_destroy( BackendDB *be )
+{
+       struct bdb_info *bdb = (struct bdb_info *) be->be_private;
+
        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 );
+
        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.c_dntree.bei_kids_mutex );
+#ifdef BDB_HIER
+       ldap_pvt_thread_mutex_destroy( &bdb->bi_modrdns_mutex );
+#endif
        ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
        ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
-       if ( bdb->bi_idl_cache_max_size ) {
-               ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
-               ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
-       }
+       ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
+       ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
 
        ch_free( bdb );
        be->be_private = NULL;