/** BEGIN COPYRIGHT BLOCK
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 *
 * License: GPL (version 3 or any later version).
 * See LICENSE for details. 
 * END COPYRIGHT BLOCK **/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

/* ldbm_config.c - Handles configuration information that is global to all ldbm instances. */

#include "back-ldbm.h"
#include "dblayer.h"

/* Forward declarations */
static int parse_ldbm_config_entry(struct ldbminfo *li, Slapi_Entry *e, config_info *config_array);

/* Forward callback declarations */
int ldbm_config_search_entry_callback(Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);
int ldbm_config_modify_entry_callback(Slapi_PBlock *pb, Slapi_Entry* e, Slapi_Entry* entryAfter, int *returncode, char *returntext, void *arg);

static char *ldbm_skeleton_entries[] =
{
    "dn:cn=config, cn=%s, cn=plugins, cn=config\n"
    "objectclass:top\n"
    "objectclass:extensibleObject\n"
    "cn:config\n",

    "dn:cn=monitor, cn=%s, cn=plugins, cn=config\n"
    "objectclass:top\n"
    "objectclass:extensibleObject\n"
    "cn:monitor\n",

    "dn:cn=database, cn=monitor, cn=%s, cn=plugins, cn=config\n"
    "objectclass:top\n"
    "objectclass:extensibleObject\n"
    "cn:database\n",

    ""
};

/* Used to add an array of entries, like the one above and 
 * ldbm_instance_skeleton_entries in ldbm_instance_config.c, to the dse. 
 * Returns 0 on success.
 */
int ldbm_config_add_dse_entries(struct ldbminfo *li, char **entries, char *string1, char *string2, char *string3, int flags)
{
    int x;
    Slapi_Entry *e;
    Slapi_PBlock *util_pb = NULL;
    int rc;
    int result;
    char entry_string[512];
    int dont_write_file = 0;
    char ebuf[BUFSIZ];

    if (flags & LDBM_INSTANCE_CONFIG_DONT_WRITE) {
        dont_write_file = 1;
    }

    for(x = 0; strlen(entries[x]) > 0; x++) {
        util_pb = slapi_pblock_new();
        PR_snprintf(entry_string, 512, entries[x], string1, string2, string3);
        e = slapi_str2entry(entry_string, 0);
        PL_strncpyz(ebuf, slapi_entry_get_dn_const(e), sizeof(ebuf)); /* for logging */
        slapi_add_entry_internal_set_pb(util_pb, e, NULL, li->li_identity, 0);
        slapi_pblock_set(util_pb, SLAPI_DSE_DONT_WRITE_WHEN_ADDING, 
                         &dont_write_file);
        rc = slapi_add_internal_pb(util_pb);
        slapi_pblock_get(util_pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
        if (!rc && (result == LDAP_SUCCESS)) {
            LDAPDebug(LDAP_DEBUG_CONFIG, "Added database config entry [%s]\n",
                      ebuf, 0, 0);
        } else if (result == LDAP_ALREADY_EXISTS) {
            LDAPDebug(LDAP_DEBUG_TRACE, "Database config entry [%s] already exists - skipping\n",
                      ebuf, 0, 0);
        } else {
            LDAPDebug(LDAP_DEBUG_ANY, "Unable to add config entry [%s] to the DSE: %d %d\n",
                      ebuf, result, rc);
        }
        slapi_pblock_destroy(util_pb);
    }

    return 0;
}

/* used to add a single entry, special case of above */
int ldbm_config_add_dse_entry(struct ldbminfo *li, char *entry, int flags)
{
    char *entries[] = { "%s", "" };

    return ldbm_config_add_dse_entries(li, entries, entry, NULL, NULL, flags);
}

/* Finds an entry in a config_info array with the given name.  Returns
 * the entry on success and NULL when not found.
 */
config_info *get_config_info(config_info *config_array, char *attr_name)
{
    int x;

    for(x = 0; config_array[x].config_name != NULL; x++) {
        if (!strcasecmp(config_array[x].config_name, attr_name)) {
            return &(config_array[x]);
        }
    }
    return NULL;
}

/*------------------------------------------------------------------------
 * Get and set functions for ldbm and dblayer variables
 *----------------------------------------------------------------------*/
static void *ldbm_config_lookthroughlimit_get(void *arg) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;

    return (void *) ((uintptr_t)(li->li_lookthroughlimit));
}

static int ldbm_config_lookthroughlimit_set(void *arg, void *value, char *errorbuf, int phase, int apply) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    int val = (int) ((uintptr_t)value);

    /* Do whatever we can to make sure the data is ok. */

    if (apply) {
        li->li_lookthroughlimit = val;
    }

    return retval;
}

static void *ldbm_config_pagedlookthroughlimit_get(void *arg)
{
    struct ldbminfo *li = (struct ldbminfo *) arg;

    return (void *) ((uintptr_t)(li->li_pagedlookthroughlimit));
}

static int ldbm_config_pagedlookthroughlimit_set(void *arg, void *value, char *errorbuf, int phase, int apply) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    int val = (int) ((uintptr_t)value);

    /* Do whatever we can to make sure the data is ok. */

    if (apply) {
        li->li_pagedlookthroughlimit = val;
    }

    return retval;
}

static void *ldbm_config_rangelookthroughlimit_get(void *arg) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;

    return (void *) ((uintptr_t)(li->li_rangelookthroughlimit));
}

static int ldbm_config_rangelookthroughlimit_set(void *arg, void *value, char *errorbuf, int phase, int apply) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    int val = (int) ((uintptr_t)value);

    /* Do whatever we can to make sure the data is ok. */

    if (apply) {
        li->li_rangelookthroughlimit = val;
    }

    return retval;
}

static void *ldbm_config_backend_opt_level_get(void *arg) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;

    return (void *) ((uintptr_t)(li->li_backend_opt_level));
}

static int ldbm_config_backend_opt_level_set(void *arg, void *value, char *errorbuf, int phase, int apply) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    int val = (int) ((uintptr_t)value);

    /* Do whatever we can to make sure the data is ok. */

    if (apply) {
        li->li_backend_opt_level = val;
    }

    return retval;
}
static void *ldbm_config_mode_get(void *arg) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;

    return (void *) ((uintptr_t)(li->li_mode));
}

static int ldbm_config_mode_set(void *arg, void *value, char *errorbuf, int phase, int apply) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    int val = (int) ((uintptr_t)value);

    /* Do whatever we can to make sure the data is ok. */

    if (apply) {
        li->li_mode = val;
    }

    return retval;
}

static void *ldbm_config_allidsthreshold_get(void *arg) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;

    return (void *) ((uintptr_t)(li->li_allidsthreshold));
}

static int ldbm_config_allidsthreshold_set(void *arg, void *value, char *errorbuf, int phase, int apply) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    int val = (int) ((uintptr_t)value);

    /* Do whatever we can to make sure the data is ok. */

    /* Catch attempts to configure a stupidly low allidsthreshold */
    if ((val > -1) && (val < 100)) {
        val = 100;
    }

    if (apply) {
        li->li_allidsthreshold = val;
    }

    return retval;
}

static void *ldbm_config_pagedallidsthreshold_get(void *arg) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;

    return (void *) ((uintptr_t)(li->li_pagedallidsthreshold));
}

static int ldbm_config_pagedallidsthreshold_set(void *arg, void *value, char *errorbuf, int phase, int apply) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    int val = (int) ((uintptr_t)value);

    /* Do whatever we can to make sure the data is ok. */

    /* Catch attempts to configure a stupidly low pagedallidsthreshold */
    /* value of 0 means turn off separate paged value and use regular allids value */
    if ((val > 0) && (val < 100)) {
        val = 100;
    }

    if (apply) {
        li->li_pagedallidsthreshold = val;
    }

    return retval;
}

static void *ldbm_config_directory_get(void *arg) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;

    /* Remember get functions of type string need to return
     * alloced memory. */
    return (void *) slapi_ch_strdup(li->li_new_directory);
}

static int ldbm_config_directory_set(void *arg, void *value, char *errorbuf, int phase, int apply) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    char *val = (char *) value;
    char tmpbuf[BUFSIZ];

    errorbuf[0] = '\0';

    if (!apply) {
        /* we should really do some error checking here. */
        return retval;
    }

    if (CONFIG_PHASE_RUNNING == phase) {
        slapi_ch_free((void **) &(li->li_new_directory));
        li->li_new_directory = rel2abspath(val); /* normalize the path;
                                                    strdup'ed in rel2abspath */
        LDAPDebug(LDAP_DEBUG_ANY, "New db directory location will not take affect until the server is restarted\n", 0, 0, 0);
    } else {
        slapi_ch_free((void **) &(li->li_new_directory));
        slapi_ch_free((void **) &(li->li_directory));
        if (NULL == val || '\0' == *val) {
            LDAPDebug(LDAP_DEBUG_ANY, 
                "ERROR: db directory is not set; check %s in the db config: %s\n",
                CONFIG_DIRECTORY, CONFIG_LDBM_DN, 0);
            retval = LDAP_PARAM_ERROR;
        } else {
            if (0 == strcmp(val, "get default")) {
                /* We use this funky "get default" string for the caller to 
                 * tell us that it has no idea what the db directory should
                 * be.  This code figures it out be reading "cn=config,cn=ldbm
                 * database,cn=plugins,cn=config" entry. */
                Slapi_PBlock *search_pb;
                Slapi_Entry **entries = NULL;
                Slapi_Attr *attr = NULL;
                Slapi_Value *v = NULL;
                const char *s = NULL;
                int res;
    
                search_pb = slapi_pblock_new();
                slapi_search_internal_set_pb(search_pb, CONFIG_LDBM_DN,
                        LDAP_SCOPE_BASE, "objectclass=*", NULL, 0, NULL, NULL,
                        li->li_identity, 0);
                slapi_search_internal_pb(search_pb);
                slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &res);
    
                if (res != LDAP_SUCCESS) {
                    LDAPDebug(LDAP_DEBUG_ANY, 
                              "ERROR: ldbm plugin unable to read %s\n",
                              CONFIG_LDBM_DN, 0, 0);
                    retval = res;
                    goto done;
                }
    
                slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
                if (NULL == entries) {
                    LDAPDebug(LDAP_DEBUG_ANY,
                              "ERROR: ldbm plugin unable to read %s\n",
                              CONFIG_LDBM_DN, 0, 0);
                    retval = LDAP_OPERATIONS_ERROR;
                    goto done;
                }
                
                res = slapi_entry_attr_find(entries[0], "nsslapd-directory", &attr);
                if (res != 0 || attr == NULL) {
                    LDAPDebug(LDAP_DEBUG_ANY, 
                              "ERROR: ldbm plugin unable to read attribute nsslapd-directory from %s\n",
                              CONFIG_LDBM_DN, 0, 0);
                    retval = LDAP_OPERATIONS_ERROR;
                    goto done;
                }
    
                if ( slapi_attr_first_value(attr,&v) != 0
                        || ( NULL == v )
                        || ( NULL == ( s = slapi_value_get_string( v )))) {
                    LDAPDebug(LDAP_DEBUG_ANY, 
                              "ERROR: ldbm plugin unable to read attribute nsslapd-directory from %s\n",
                              CONFIG_LDBM_DN, 0, 0);
                    retval = LDAP_OPERATIONS_ERROR;
                    goto done;
                }
                slapi_pblock_destroy(search_pb);
                if (NULL == s || '\0' == s || 0 == PL_strcmp(s, "(null)")) {
                    LDAPDebug(LDAP_DEBUG_ANY, 
                        "ERROR: db directory is not set; check %s in the db config: %s\n",
                        CONFIG_DIRECTORY, CONFIG_LDBM_DN, 0);
                    retval = LDAP_PARAM_ERROR;
                    goto done;
                }
                PR_snprintf(tmpbuf, BUFSIZ, "%s", s);
                val = tmpbuf;
            }
            li->li_new_directory = rel2abspath(val); /* normalize the path;
                                                        strdup'ed in 
                                                        rel2abspath */
            li->li_directory = rel2abspath(val);     /* ditto */
        }
    }
done:
    return retval;
}

static void *ldbm_config_dbcachesize_get(void *arg) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    
    return (void *) (li->li_new_dbcachesize);
}

static int ldbm_config_dbcachesize_set(void *arg, void *value, char *errorbuf, int phase, int apply) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    size_t val = (size_t)value;

    if (apply) {
        /* Stop the user configuring a stupidly small cache */
        /* min: 8KB (page size) * def thrd cnts (threadnumber==20). */
#define DBDEFMINSIZ     500000
        if (val < DBDEFMINSIZ) {
            LDAPDebug( LDAP_DEBUG_ANY,"WARNING: cache too small, increasing to %dK bytes\n",
                    DBDEFMINSIZ/1000, 0, 0);
            val = DBDEFMINSIZ;
        } else if (!dblayer_is_cachesize_sane(&val)){
            PR_snprintf(errorbuf, SLAPI_DSE_RETURNTEXT_SIZE,
                    "Error: dbcachememsize value is too large.");
            LDAPDebug( LDAP_DEBUG_ANY,"Error: dbcachememsize value is too large.\n",
                    0, 0, 0);
            return LDAP_UNWILLING_TO_PERFORM;
        }
        if (CONFIG_PHASE_RUNNING == phase) {
            li->li_new_dbcachesize = val;
            LDAPDebug(LDAP_DEBUG_ANY, "New db cache size will not take affect until the server is restarted\n",
                    0, 0, 0);
        } else {
            li->li_new_dbcachesize = val;
            li->li_dbcachesize = val;
        }
    }
    
    return retval;
}

static void *ldbm_config_maxpassbeforemerge_get(void *arg) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;

    return (void *) ((uintptr_t)(li->li_maxpassbeforemerge));
}

static int ldbm_config_maxpassbeforemerge_set(void *arg, void *value, char *errorbuf, int phase, int apply) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    int val = (int) ((uintptr_t)value);
    
    if (apply) {
        if (val < 0) {
            LDAPDebug( LDAP_DEBUG_ANY,"WARNING: maxpassbeforemerge will not take negative value\n", 0, 0, 0);
            val = 100;
        } 
        
        li->li_maxpassbeforemerge = val;
    }

    return retval;
}


static void *ldbm_config_dbncache_get(void *arg) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;

    return (void *) ((uintptr_t)(li->li_new_dbncache));
}

static int ldbm_config_dbncache_set(void *arg, void *value, char *errorbuf, int phase, int apply) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    size_t val = (size_t) ((uintptr_t)value);
    
    if (apply) {
        if (!dblayer_is_cachesize_sane(&val)){
            PR_snprintf(errorbuf, SLAPI_DSE_RETURNTEXT_SIZE,
                    "Error: dbncache size value is too large.");
            LDAPDebug( LDAP_DEBUG_ANY,"Error: dbncache size value is too large.\n",
                    val, 0, 0);
            return LDAP_UNWILLING_TO_PERFORM;
        }
        
        if (CONFIG_PHASE_RUNNING == phase) {
            li->li_new_dbncache = val;
            LDAPDebug(LDAP_DEBUG_ANY, "New db ncache will not take affect until the server is restarted\n", 0, 0, 0);
        } else {
            li->li_new_dbncache = val;
            li->li_dbncache = val;
        }
        
    }

    return retval;
}

void *ldbm_config_db_logdirectory_get(void *arg)
{
    struct ldbminfo *li = (struct ldbminfo *) arg;

    /* Remember get functions of type string need to return
     * alloced memory. */
    /* if dblayer_log_directory is set to a string different from "" 
     * then it has been set, return this variable
     * otherwise it is set to default, use the instance home directory
     */
    if (strlen(li->li_dblayer_private->dblayer_log_directory) > 0)
        return (void *) slapi_ch_strdup(li->li_dblayer_private->dblayer_log_directory);
    else
        return (void *) slapi_ch_strdup(li->li_new_directory);
}

/* Does not return a copy of the string - used by disk space monitoring feature */
void *ldbm_config_db_logdirectory_get_ext(void *arg)
{
    struct ldbminfo *li = (struct ldbminfo *) arg;

    if (strlen(li->li_dblayer_private->dblayer_log_directory) > 0)
        return (void *)li->li_dblayer_private->dblayer_log_directory;
    else
        return (void *)li->li_new_directory;
}

static int ldbm_config_db_logdirectory_set(void *arg, void *value, char *errorbuf, int phase, int apply) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    char *val = (char *) value;
    
    if (apply) {
        slapi_ch_free((void **) &(li->li_dblayer_private->dblayer_log_directory));
        li->li_dblayer_private->dblayer_log_directory = slapi_ch_strdup(val);
    }
    
    return retval;
}

static void *ldbm_config_db_durable_transactions_get(void *arg) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    
    return (void *) ((uintptr_t)li->li_dblayer_private->dblayer_durable_transactions);
}

static int ldbm_config_db_durable_transactions_set(void *arg, void *value, char *errorbuf, int phase, int apply) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    int val = (int) ((uintptr_t)value);
        
    if (apply) {
        li->li_dblayer_private->dblayer_durable_transactions = val;
    }
        
    return retval;
}

static void *ldbm_config_db_lockdown_get(void *arg) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    
    return (void *) ((uintptr_t)li->li_dblayer_private->dblayer_lockdown);
}

static int ldbm_config_db_lockdown_set(
    void *arg, 
    void *value, 
    char *errorbuf, 
    int phase, 
    int apply
) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    int val = (int) ((uintptr_t)value);

    if (apply) {
        li->li_dblayer_private->dblayer_lockdown = val;
    }

    return retval;
}

static void *ldbm_config_db_circular_logging_get(void *arg) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    
    return (void *) ((uintptr_t)li->li_dblayer_private->dblayer_circular_logging);
}

static int ldbm_config_db_circular_logging_set(void *arg, void *value, char *errorbuf, int phase, int apply) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    int val = (int) ((uintptr_t)value);
    
    if (apply) {
        li->li_dblayer_private->dblayer_circular_logging = val;
    }
    
    return retval;
}

static void *ldbm_config_db_transaction_logging_get(void *arg) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    
    return (void *) ((uintptr_t)li->li_dblayer_private->dblayer_enable_transactions);
}

static int ldbm_config_db_transaction_logging_set(void *arg, void *value, char *errorbuf, int phase, int apply) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    int val = (int) ((uintptr_t)value);
    
    if (apply) {
        li->li_dblayer_private->dblayer_enable_transactions = val;
    }
    
    return retval;
}

static void *ldbm_config_db_logbuf_size_get(void *arg) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    
    return (void *) li->li_dblayer_private->dblayer_logbuf_size;
}

static int ldbm_config_db_logbuf_size_set(void *arg, void *value, char *errorbuf, int phase, int apply) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    size_t val = (size_t) value;
    
    if (apply) {
        li->li_dblayer_private->dblayer_logbuf_size = val;
    }
    
    return retval;
}

static void *ldbm_config_db_checkpoint_interval_get(void *arg) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    
    return (void *) ((uintptr_t)li->li_dblayer_private->dblayer_checkpoint_interval);
}

static int ldbm_config_db_checkpoint_interval_set(void *arg, void *value, char *errorbuf, int phase, int apply) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    int val = (int) ((uintptr_t)value);
    
    if (apply) {
        li->li_dblayer_private->dblayer_checkpoint_interval = val;
    }
    
    return retval;
}

static void *ldbm_config_db_compactdb_interval_get(void *arg) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    
    return (void *) ((uintptr_t)li->li_dblayer_private->dblayer_compactdb_interval);
}

static int ldbm_config_db_compactdb_interval_set(void *arg, void *value, char *errorbuf, int phase, int apply) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    int val = (int) ((uintptr_t)value);
    
    if (apply) {
        li->li_dblayer_private->dblayer_compactdb_interval = val;
    }
    
    return retval;
}

static void *ldbm_config_db_page_size_get(void *arg) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    
    return (void *) li->li_dblayer_private->dblayer_page_size;
}

static int ldbm_config_db_page_size_set(void *arg, void *value, char *errorbuf, int phase, int apply) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    size_t val = (size_t) value;
    
    if (apply) {
        li->li_dblayer_private->dblayer_page_size = val;
    }
    
    return retval;
}

static void *ldbm_config_db_index_page_size_get(void *arg) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    
    return (void *) li->li_dblayer_private->dblayer_index_page_size;
}

static int ldbm_config_db_index_page_size_set(void *arg, void *value, char *errorbuf, int phase, int apply) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    size_t val = (size_t) value;
    
    if (apply) {
        li->li_dblayer_private->dblayer_index_page_size = val;
    }
    
    return retval;
}

static void *ldbm_config_db_idl_divisor_get(void *arg) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    
    return (void *) ((uintptr_t)li->li_dblayer_private->dblayer_idl_divisor);
}

static int ldbm_config_db_idl_divisor_set(void *arg, void *value, char *errorbuf, int phase, int apply) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    int val = (int) ((uintptr_t)value);
    
    if (apply) {
        li->li_dblayer_private->dblayer_idl_divisor = val;
    }
    
    return retval;
}

static void *ldbm_config_db_old_idl_maxids_get(void *arg)
{
    struct ldbminfo *li = (struct ldbminfo *) arg;

    return (void *) ((uintptr_t)li->li_old_idl_maxids);
}

static int ldbm_config_db_old_idl_maxids_set(void *arg, void *value, char *errorbuf, int phase, int apply)
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    int val = (int) ((uintptr_t)value);

    if (apply) {
        if(val >= 0){
            li->li_old_idl_maxids = val;
        } else {
            PR_snprintf(errorbuf, SLAPI_DSE_RETURNTEXT_SIZE,
        	            "Error: Invalid value for %s (%d). Value must be equal or greater than zero.",
        	            CONFIG_DB_OLD_IDL_MAXIDS, val);
            return LDAP_UNWILLING_TO_PERFORM;
        }
    }

    return retval;
}

static void *ldbm_config_db_logfile_size_get(void *arg) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    
    return (void *) li->li_dblayer_private->dblayer_logfile_size;
}

static int ldbm_config_db_logfile_size_set(void *arg, void *value, char *errorbuf, int phase, int apply) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    size_t val = (size_t) value;
    
    if (apply) {
        li->li_dblayer_private->dblayer_logfile_size = val;
    }
    
    return retval;
}

static void *ldbm_config_db_spin_count_get(void *arg) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    
    return (void *) ((uintptr_t)li->li_dblayer_private->dblayer_spin_count);
}

static int ldbm_config_db_spin_count_set(void *arg, void *value, char *errorbuf, int phase, int apply) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    int val = (int) ((uintptr_t)value);
    
    if (apply) {
        li->li_dblayer_private->dblayer_spin_count = val;
    }
    
    return retval;
}

static void *ldbm_config_db_trickle_percentage_get(void *arg) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    
    return (void *) ((uintptr_t)li->li_dblayer_private->dblayer_trickle_percentage);
}

static int ldbm_config_db_trickle_percentage_set(void *arg, void *value, char *errorbuf, int phase, int apply) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    int val = (int) ((uintptr_t)value);
    
    if (val < 0 || val > 100) {
        PR_snprintf(errorbuf, SLAPI_DSE_RETURNTEXT_SIZE,
                    "Error: Invalid value for %s (%d). Must be between 0 and 100\n", CONFIG_DB_TRICKLE_PERCENTAGE, val);
        LDAPDebug(LDAP_DEBUG_ANY, "%s", errorbuf, 0, 0);
            return LDAP_UNWILLING_TO_PERFORM;
    }
    
    if (apply) {
        li->li_dblayer_private->dblayer_trickle_percentage = val;
    }
    
    return retval;
}

static void *ldbm_config_db_verbose_get(void *arg) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    
    return (void *) ((uintptr_t)li->li_dblayer_private->dblayer_verbose);
}

static int ldbm_config_db_verbose_set(void *arg, void *value, char *errorbuf, int phase, int apply) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    int val = (int) ((uintptr_t)value);
    
    if (apply) {
        li->li_dblayer_private->dblayer_verbose = val;
    }
    
    return retval;
}

static void *ldbm_config_db_debug_get(void *arg) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    
    return (void *) ((uintptr_t)li->li_dblayer_private->dblayer_debug);
}

static int ldbm_config_db_debug_set(void *arg, void *value, char *errorbuf, int phase, int apply) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    int val = (int) ((uintptr_t)value);
    
    if (apply) {
        li->li_dblayer_private->dblayer_debug = val;
    }
    
    return retval;
}

static void *ldbm_config_db_named_regions_get(void *arg) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    
    return (void *) ((uintptr_t)li->li_dblayer_private->dblayer_named_regions);
}

static int ldbm_config_db_named_regions_set(void *arg, void *value, char *errorbuf, int phase, int apply) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    int val = (int) ((uintptr_t)value);
    
    if (apply) {
        li->li_dblayer_private->dblayer_named_regions = val;
    }
    
    return retval;
}

static void *ldbm_config_db_private_mem_get(void *arg) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    
    return (void *) ((uintptr_t)li->li_dblayer_private->dblayer_private_mem);
}

static int ldbm_config_db_private_mem_set(void *arg, void *value, char *errorbuf, int phase, int apply) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    int val = (int) ((uintptr_t)value);
    
    if (apply) {
        li->li_dblayer_private->dblayer_private_mem = val;
    }
    
    return retval;
}

static void *ldbm_config_db_online_import_encrypt_get(void *arg) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    
    return (void *) ((uintptr_t)li->li_online_import_encrypt);
}

static int ldbm_config_db_online_import_encrypt_set(void *arg, void *value, char *errorbuf, int phase, int apply) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    int val = (int) ((uintptr_t)value);
    
    if (apply) {
        li->li_online_import_encrypt = val;
    }
    
    return retval;
}

static void *ldbm_config_db_private_import_mem_get(void *arg) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    
    return (void *) ((uintptr_t)li->li_dblayer_private->dblayer_private_import_mem);
}

static void *ldbm_config_idl_get_update(void *arg)
{
    struct ldbminfo *li = (struct ldbminfo *) arg;

    return (void *) ((uintptr_t)li->li_idl_update);
}

static int ldbm_config_idl_set_update(void *arg, void *value, char *errorbuf, int phase, int apply)
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    int val = (int) ((uintptr_t)value);

    if (apply) {
        li->li_idl_update = val;
    }

    return retval;
}

static int ldbm_config_db_private_import_mem_set(void *arg, void *value, char *errorbuf, int phase, int apply) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    int val = (int) ((uintptr_t)value);
    
    if (apply) {
        li->li_dblayer_private->dblayer_private_import_mem = val;
    }
    
    return retval;
}

static void *ldbm_config_db_shm_key_get(void *arg) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;

    return (void *) li->li_dblayer_private->dblayer_shm_key;
}

static int ldbm_config_db_shm_key_set(
    void *arg, 
    void *value, 
    char *errorbuf, 
    int phase, 
    int apply
) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    int val = (int) ((uintptr_t)value);

    if (apply) {
        li->li_dblayer_private->dblayer_shm_key = val;
    }

    return retval;
}

static void *ldbm_config_db_lock_get(void *arg) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    
    return (void *) ((uintptr_t)li->li_dblayer_private->dblayer_lock_config);
}


static int ldbm_config_db_lock_set(void *arg, void *value, char *errorbuf, int phase, int apply) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    size_t val = (size_t) value;

    if (apply) {
        if (CONFIG_PHASE_RUNNING == phase) {
            li->li_dblayer_private->dblayer_lock_config = val;
            LDAPDebug(LDAP_DEBUG_ANY, "New db max lock count will not take affect until the server is restarted\n", 0, 0, 0);
        } else {
            li->li_dblayer_private->dblayer_lock_config = val;
        }
        
    }

    return retval;
}
static void *ldbm_config_db_cache_get(void *arg) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    
    return (void *) ((uintptr_t)li->li_dblayer_private->dblayer_cache_config);
}

static int ldbm_config_db_cache_set(void *arg, void *value, char *errorbuf, int phase, int apply) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    size_t val = (size_t) ((uintptr_t)value);
    
    if (apply) {
        if (!dblayer_is_cachesize_sane(&val)){
            PR_snprintf(errorbuf, SLAPI_DSE_RETURNTEXT_SIZE,
                    "Error: db cachesize value is too large");
            LDAPDebug( LDAP_DEBUG_ANY,"Error: db cachesize value is too large.\n",
                    val, 0, 0);
            return LDAP_UNWILLING_TO_PERFORM;
        }
        li->li_dblayer_private->dblayer_cache_config = val;
    }
    
    return retval;
}

static void *ldbm_config_db_debug_checkpointing_get(void *arg) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    
    return (void *) ((uintptr_t)li->li_dblayer_private->db_debug_checkpointing);
}

static int ldbm_config_db_debug_checkpointing_set(void *arg, void *value, char *errorbuf, int phase, int apply) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    int val = (int) ((uintptr_t)value);
    
    if (apply) {
        li->li_dblayer_private->db_debug_checkpointing = val;
    }
    
    return retval;
}

static void *ldbm_config_db_home_directory_get(void *arg) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    
    /* Remember get functions of type string need to return
     * alloced memory. */
    return (void *) slapi_ch_strdup(li->li_dblayer_private->dblayer_dbhome_directory);
}

static int ldbm_config_db_home_directory_set(void *arg, void *value, char *errorbuf, int phase, int apply) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    char *val = (char *) value;
    
    if (apply) {
        slapi_ch_free((void **) &(li->li_dblayer_private->dblayer_dbhome_directory));
        li->li_dblayer_private->dblayer_dbhome_directory = slapi_ch_strdup(val);
    }
    
    return retval;
}

static void *ldbm_config_import_cache_autosize_get(void *arg)
{
    struct ldbminfo *li = (struct ldbminfo *)arg;

    return (void *)((uintptr_t)(li->li_import_cache_autosize));
}

static int ldbm_config_import_cache_autosize_set(void *arg, void *value, char *errorbuf,
                   int phase, int apply)
{
    struct ldbminfo *li = (struct ldbminfo *)arg;

    if (apply)
    li->li_import_cache_autosize = (int)((uintptr_t)value);
    return LDAP_SUCCESS;
}

static void *ldbm_config_cache_autosize_get(void *arg)
{
    struct ldbminfo *li = (struct ldbminfo *)arg;

    return (void *)((uintptr_t)(li->li_cache_autosize));
}

static int ldbm_config_cache_autosize_set(void *arg, void *value, char *errorbuf,
                   int phase, int apply)
{
    struct ldbminfo *li = (struct ldbminfo *)arg;

    if (apply)
    li->li_cache_autosize = (int)((uintptr_t)value);
    return LDAP_SUCCESS;
}

static void *ldbm_config_cache_autosize_split_get(void *arg)
{
    struct ldbminfo *li = (struct ldbminfo *)arg;

    return (void *)((uintptr_t)(li->li_cache_autosize_split));
}

static int ldbm_config_cache_autosize_split_set(void *arg, void *value, char *errorbuf,
                     int phase, int apply)
{
    struct ldbminfo *li = (struct ldbminfo *)arg;

    if (apply)
    li->li_cache_autosize_split = (int)((uintptr_t)value);
    return LDAP_SUCCESS;
}

static void *ldbm_config_import_cachesize_get(void *arg)
{
    struct ldbminfo *li = (struct ldbminfo *)arg;

    return (void *)(li->li_import_cachesize);
}

static int ldbm_config_import_cachesize_set(void *arg, void *value, char *errorbuf,
                                     int phase, int apply)
{
    struct ldbminfo *li = (struct ldbminfo *)arg;
    size_t val = (size_t)value;
    if (apply){
        if (!dblayer_is_cachesize_sane(&val)){
            PR_snprintf(errorbuf, SLAPI_DSE_RETURNTEXT_SIZE,
                    "Error: import cachesize value is too large.");
            LDAPDebug( LDAP_DEBUG_ANY,"Error: import cachesize value is too large.\n",
                    0, 0, 0);
            return LDAP_UNWILLING_TO_PERFORM;
        }
        li->li_import_cachesize = val;
    }
    return LDAP_SUCCESS;
}

static void *ldbm_config_index_buffer_size_get(void *arg)
{
    return (void *)import_get_index_buffer_size();
}

static int ldbm_config_index_buffer_size_set(void *arg, void *value, char *errorbuf,
                                     int phase, int apply)
{
    if (apply)
        import_configure_index_buffer_size((size_t)value);
    return LDAP_SUCCESS;
}

static void *ldbm_config_idl_get_idl_new(void *arg)
{
    if (idl_get_idl_new())
        return slapi_ch_strdup("new");
    else
        return slapi_ch_strdup("old");
}

static int ldbm_config_idl_set_tune(void *arg, void *value, char *errorbuf,
                             int phase, int apply)
{
    if (!strcasecmp("new", value))
        idl_set_tune(4096);
    else
        idl_set_tune(0);
    return LDAP_SUCCESS;
}

static void *ldbm_config_serial_lock_get(void *arg)
{
    struct ldbminfo *li = (struct ldbminfo *) arg;

    return (void *) ((uintptr_t)li->li_fat_lock);
}

static int ldbm_config_serial_lock_set(void *arg, void *value, char *errorbuf,
                             int phase, int apply)
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    
    if (apply) {
        li->li_fat_lock = (int) ((uintptr_t)value);
    }
    
    return LDAP_SUCCESS;
}

static void *ldbm_config_entryrdn_switch_get(void *arg)
{
    return (void *)((uintptr_t)entryrdn_get_switch());
}

static int ldbm_config_entryrdn_switch_set(void *arg, void *value,
                                           char *errorbuf, int phase, int apply)
{
    if (apply) {
        entryrdn_set_switch((int)((uintptr_t)value));
    }
    return LDAP_SUCCESS;
}

static void *ldbm_config_entryrdn_noancestorid_get(void *arg)
{
    return (void *)((uintptr_t)entryrdn_get_noancestorid());
}

static int ldbm_config_entryrdn_noancestorid_set(void *arg, void *value,
                                           char *errorbuf, int phase, int apply)
{
    if (apply) {
        entryrdn_set_noancestorid((int)((uintptr_t)value));
    }
    return LDAP_SUCCESS;
}

static void *ldbm_config_legacy_errcode_get(void *arg)
{
    struct ldbminfo *li = (struct ldbminfo *) arg;

    return (void *) ((uintptr_t)li->li_legacy_errcode);
}

static int ldbm_config_legacy_errcode_set(void *arg, void *value, char *errorbuf,
                             int phase, int apply)
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    
    if (apply) {
        li->li_legacy_errcode = (int) ((uintptr_t)value);
    }
    
    return LDAP_SUCCESS;
}

static int
ldbm_config_set_bypass_filter_test(void *arg, void *value, char *errorbuf,
                             int phase, int apply)
{
    struct ldbminfo *li = (struct ldbminfo *)arg;

    if (apply) {
        char *myvalue = (char *)value;

        if (0 == strcasecmp(myvalue, "on")) {
            li->li_filter_bypass = 1;
            li->li_filter_bypass_check = 0;
        } else if (0 == strcasecmp(myvalue, "verify")) {
            li->li_filter_bypass = 1;
            li->li_filter_bypass_check = 1;
        } else {
            li->li_filter_bypass = 0;
            li->li_filter_bypass_check = 0;
        }
    }
    return LDAP_SUCCESS;
}

static void *ldbm_config_get_bypass_filter_test(void *arg)
{
    struct ldbminfo *li = (struct ldbminfo *)arg;
    char *retstr = NULL;

    if (li->li_filter_bypass) {
        if (li->li_filter_bypass_check) {
            /* meaningful only if is bypass filter test called */
            retstr = slapi_ch_strdup("verify");
        } else {
            retstr = slapi_ch_strdup("on");
        }
    } else {
        retstr = slapi_ch_strdup("off");
    }
    return (void *)retstr;
}

static int ldbm_config_set_use_vlv_index(void *arg, void *value, char *errorbuf, int phase, int apply) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int val = (int) ((uintptr_t)value);
        
    if (apply) {
        if (val) {
            li->li_use_vlv = 1;
        } else {
            li->li_use_vlv = 0;
        }
    }
    return LDAP_SUCCESS;
}

static void *ldbm_config_get_use_vlv_index(void *arg) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;

    return (void *) ((uintptr_t)li->li_use_vlv);
}

static int
ldbm_config_exclude_from_export_set( void *arg, void *value, char *errorbuf,
        int phase, int apply)
{
    struct ldbminfo *li = (struct ldbminfo *)arg;

    if ( apply ) {
        if ( NULL != li->li_attrs_to_exclude_from_export ) {
            charray_free( li->li_attrs_to_exclude_from_export );
            li->li_attrs_to_exclude_from_export = NULL;
        }
        
        if ( NULL != value ) {
            char *dupvalue = slapi_ch_strdup( value );
            li->li_attrs_to_exclude_from_export = slapi_str2charray( dupvalue, " " );
            slapi_ch_free((void**)&dupvalue);
        }
    }
    
    return LDAP_SUCCESS;
}

static void *
ldbm_config_exclude_from_export_get( void *arg )
{
    struct ldbminfo *li = (struct ldbminfo *)arg;
    char    *p, *retstr = NULL;
    size_t    len = 0;
    
    if ( NULL != li->li_attrs_to_exclude_from_export &&
         NULL != li->li_attrs_to_exclude_from_export[0] ) {
        int        i;
        
        for ( i = 0; li->li_attrs_to_exclude_from_export[i] != NULL; ++i ) {
            len += strlen( li->li_attrs_to_exclude_from_export[i] ) + 1;
        }
        p = retstr = slapi_ch_malloc( len );
        for ( i = 0; li->li_attrs_to_exclude_from_export[i] != NULL; ++i ) {
            if ( i > 0 ) {
                *p++ = ' ';
            }
            strcpy( p, li->li_attrs_to_exclude_from_export[i] );
            p += strlen( p );
        }
        *p = '\0';
    } else {
        retstr = slapi_ch_strdup( "" );
    }
    
    return (void *)retstr;
}

static void *ldbm_config_db_tx_max_get(void *arg) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;

    return (void *) ((uintptr_t)li->li_dblayer_private->dblayer_tx_max);
}

static int ldbm_config_db_tx_max_set(
    void *arg, 
    void *value, 
    char *errorbuf, 
    int phase, 
    int apply
) 
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    int val = (int) ((uintptr_t)value);

    if (apply) {
        li->li_dblayer_private->dblayer_tx_max = val;
    }

    return retval;
}

static void *ldbm_config_db_deadlock_policy_get(void *arg)
{
    struct ldbminfo *li = (struct ldbminfo *) arg;

    return (void *) ((uintptr_t)li->li_dblayer_private->dblayer_deadlock_policy);
}

static int ldbm_config_db_deadlock_policy_set(void *arg, void *value, char *errorbuf, int phase, int apply)
{
    struct ldbminfo *li = (struct ldbminfo *) arg;
    int retval = LDAP_SUCCESS;
    u_int32_t val = (u_int32_t) ((uintptr_t)value);

    if (val > DB_LOCK_YOUNGEST) {
	    PR_snprintf(errorbuf, SLAPI_DSE_RETURNTEXT_SIZE,
	                "Error: Invalid value for %s (%d). Must be between %d and %d inclusive",
	                CONFIG_DB_DEADLOCK_POLICY, val, DB_LOCK_DEFAULT, DB_LOCK_YOUNGEST);
	    LDAPDebug1Arg(LDAP_DEBUG_ANY, "%s\n", errorbuf);
	    return LDAP_UNWILLING_TO_PERFORM;
    }
    if (val == DB_LOCK_NORUN) {
	    PR_snprintf(errorbuf, SLAPI_DSE_RETURNTEXT_SIZE,
	                "Warning: Setting value for %s to (%d) will disable deadlock detection",
	                CONFIG_DB_DEADLOCK_POLICY, val);
	    LDAPDebug1Arg(LDAP_DEBUG_ANY, "%s\n", errorbuf);
    }

    if (apply) {
        li->li_dblayer_private->dblayer_deadlock_policy = val;
    }

    return retval;
}


/*------------------------------------------------------------------------
 * Configuration array for ldbm and dblayer variables
 *----------------------------------------------------------------------*/
static config_info ldbm_config[] = {
    {CONFIG_LOOKTHROUGHLIMIT, CONFIG_TYPE_INT, "5000", &ldbm_config_lookthroughlimit_get, &ldbm_config_lookthroughlimit_set, CONFIG_FLAG_ALWAYS_SHOW|CONFIG_FLAG_ALLOW_RUNNING_CHANGE},
    {CONFIG_MODE, CONFIG_TYPE_INT_OCTAL, "0600", &ldbm_config_mode_get, &ldbm_config_mode_set, CONFIG_FLAG_ALWAYS_SHOW|CONFIG_FLAG_ALLOW_RUNNING_CHANGE},
    {CONFIG_IDLISTSCANLIMIT, CONFIG_TYPE_INT, "4000", &ldbm_config_allidsthreshold_get, &ldbm_config_allidsthreshold_set, CONFIG_FLAG_ALWAYS_SHOW|CONFIG_FLAG_ALLOW_RUNNING_CHANGE},
    {CONFIG_DIRECTORY, CONFIG_TYPE_STRING, "", &ldbm_config_directory_get, &ldbm_config_directory_set, CONFIG_FLAG_ALWAYS_SHOW|CONFIG_FLAG_ALLOW_RUNNING_CHANGE|CONFIG_FLAG_SKIP_DEFAULT_SETTING},
    {CONFIG_DBCACHESIZE, CONFIG_TYPE_SIZE_T, "10000000", &ldbm_config_dbcachesize_get, &ldbm_config_dbcachesize_set, CONFIG_FLAG_ALWAYS_SHOW|CONFIG_FLAG_ALLOW_RUNNING_CHANGE},
    {CONFIG_DBNCACHE, CONFIG_TYPE_INT, "0", &ldbm_config_dbncache_get, &ldbm_config_dbncache_set, CONFIG_FLAG_ALLOW_RUNNING_CHANGE},
    {CONFIG_MAXPASSBEFOREMERGE, CONFIG_TYPE_INT, "100", &ldbm_config_maxpassbeforemerge_get, &ldbm_config_maxpassbeforemerge_set, 0},
    
    /* dblayer config attributes */
    {CONFIG_DB_LOGDIRECTORY, CONFIG_TYPE_STRING, "", &ldbm_config_db_logdirectory_get, &ldbm_config_db_logdirectory_set, CONFIG_FLAG_ALWAYS_SHOW},
    {CONFIG_DB_DURABLE_TRANSACTIONS, CONFIG_TYPE_ONOFF, "on", &ldbm_config_db_durable_transactions_get, &ldbm_config_db_durable_transactions_set, CONFIG_FLAG_ALWAYS_SHOW},
    {CONFIG_DB_CIRCULAR_LOGGING, CONFIG_TYPE_ONOFF, "on", &ldbm_config_db_circular_logging_get, &ldbm_config_db_circular_logging_set, 0},
    {CONFIG_DB_TRANSACTION_LOGGING, CONFIG_TYPE_ONOFF, "on", &ldbm_config_db_transaction_logging_get, &ldbm_config_db_transaction_logging_set, 0},
    {CONFIG_DB_CHECKPOINT_INTERVAL, CONFIG_TYPE_INT, "60", &ldbm_config_db_checkpoint_interval_get, &ldbm_config_db_checkpoint_interval_set, CONFIG_FLAG_ALWAYS_SHOW|CONFIG_FLAG_ALLOW_RUNNING_CHANGE},
    {CONFIG_DB_COMPACTDB_INTERVAL, CONFIG_TYPE_INT, "2592000"/*30days*/, &ldbm_config_db_compactdb_interval_get, &ldbm_config_db_compactdb_interval_set, CONFIG_FLAG_ALWAYS_SHOW|CONFIG_FLAG_ALLOW_RUNNING_CHANGE},
    {CONFIG_DB_TRANSACTION_BATCH, CONFIG_TYPE_INT, "0", &dblayer_get_batch_transactions, &dblayer_set_batch_transactions, CONFIG_FLAG_ALWAYS_SHOW|CONFIG_FLAG_ALLOW_RUNNING_CHANGE},
    {CONFIG_DB_TRANSACTION_BATCH_MIN_SLEEP, CONFIG_TYPE_INT, "50", &dblayer_get_batch_txn_min_sleep, &dblayer_set_batch_txn_min_sleep, CONFIG_FLAG_ALWAYS_SHOW|CONFIG_FLAG_ALLOW_RUNNING_CHANGE},
    {CONFIG_DB_TRANSACTION_BATCH_MAX_SLEEP, CONFIG_TYPE_INT, "50", &dblayer_get_batch_txn_max_sleep, &dblayer_set_batch_txn_max_sleep, CONFIG_FLAG_ALWAYS_SHOW|CONFIG_FLAG_ALLOW_RUNNING_CHANGE},
    {CONFIG_DB_LOGBUF_SIZE, CONFIG_TYPE_SIZE_T, "0", &ldbm_config_db_logbuf_size_get, &ldbm_config_db_logbuf_size_set, CONFIG_FLAG_ALWAYS_SHOW},
    {CONFIG_DB_PAGE_SIZE, CONFIG_TYPE_SIZE_T, "0", &ldbm_config_db_page_size_get, &ldbm_config_db_page_size_set, 0},
    {CONFIG_DB_INDEX_PAGE_SIZE, CONFIG_TYPE_SIZE_T, "0", &ldbm_config_db_index_page_size_get, &ldbm_config_db_index_page_size_set, 0},
    {CONFIG_DB_IDL_DIVISOR, CONFIG_TYPE_INT, "0", &ldbm_config_db_idl_divisor_get, &ldbm_config_db_idl_divisor_set, 0},
    {CONFIG_DB_OLD_IDL_MAXIDS, CONFIG_TYPE_INT, "0", &ldbm_config_db_old_idl_maxids_get, &ldbm_config_db_old_idl_maxids_set, 0},
    {CONFIG_DB_LOGFILE_SIZE, CONFIG_TYPE_SIZE_T, "0", &ldbm_config_db_logfile_size_get, &ldbm_config_db_logfile_size_set, 0},
    {CONFIG_DB_TRICKLE_PERCENTAGE, CONFIG_TYPE_INT, "5", &ldbm_config_db_trickle_percentage_get, &ldbm_config_db_trickle_percentage_set, 0},
    {CONFIG_DB_SPIN_COUNT, CONFIG_TYPE_INT, "0", &ldbm_config_db_spin_count_get, &ldbm_config_db_spin_count_set, 0},
    {CONFIG_DB_VERBOSE, CONFIG_TYPE_ONOFF, "off", &ldbm_config_db_verbose_get, &ldbm_config_db_verbose_set, 0},
    {CONFIG_DB_DEBUG, CONFIG_TYPE_ONOFF, "on", &ldbm_config_db_debug_get, &ldbm_config_db_debug_set, 0},
    {CONFIG_DB_NAMED_REGIONS, CONFIG_TYPE_ONOFF, "off", &ldbm_config_db_named_regions_get, &ldbm_config_db_named_regions_set, 0},
    {CONFIG_DB_LOCK, CONFIG_TYPE_INT, "10000", &ldbm_config_db_lock_get, &ldbm_config_db_lock_set, CONFIG_FLAG_ALWAYS_SHOW},
    {CONFIG_DB_PRIVATE_MEM, CONFIG_TYPE_ONOFF, "off", &ldbm_config_db_private_mem_get, &ldbm_config_db_private_mem_set, 0},
    {CONFIG_DB_PRIVATE_IMPORT_MEM, CONFIG_TYPE_ONOFF, "on", &ldbm_config_db_private_import_mem_get, &ldbm_config_db_private_import_mem_set, CONFIG_FLAG_ALWAYS_SHOW|CONFIG_FLAG_ALLOW_RUNNING_CHANGE},
    {CONDIF_DB_ONLINE_IMPORT_ENCRYPT, CONFIG_TYPE_ONOFF, "on", &ldbm_config_db_online_import_encrypt_get, &ldbm_config_db_online_import_encrypt_set, 0},
    {CONFIG_DB_SHM_KEY, CONFIG_TYPE_LONG, "389389", &ldbm_config_db_shm_key_get, &ldbm_config_db_shm_key_set, 0},
    {CONFIG_DB_CACHE, CONFIG_TYPE_INT, "0", &ldbm_config_db_cache_get, &ldbm_config_db_cache_set, 0},
    {CONFIG_DB_DEBUG_CHECKPOINTING, CONFIG_TYPE_ONOFF, "off", &ldbm_config_db_debug_checkpointing_get, &ldbm_config_db_debug_checkpointing_set, 0},
    {CONFIG_DB_HOME_DIRECTORY, CONFIG_TYPE_STRING, "", &ldbm_config_db_home_directory_get, &ldbm_config_db_home_directory_set, 0},
    {CONFIG_IMPORT_CACHE_AUTOSIZE, CONFIG_TYPE_INT, "-1", &ldbm_config_import_cache_autosize_get, &ldbm_config_import_cache_autosize_set, CONFIG_FLAG_ALWAYS_SHOW|CONFIG_FLAG_ALLOW_RUNNING_CHANGE},
    {CONFIG_CACHE_AUTOSIZE, CONFIG_TYPE_INT, "0", &ldbm_config_cache_autosize_get, &ldbm_config_cache_autosize_set, 0},
    {CONFIG_CACHE_AUTOSIZE_SPLIT, CONFIG_TYPE_INT, "50", &ldbm_config_cache_autosize_split_get, &ldbm_config_cache_autosize_split_set, 0},
    {CONFIG_IMPORT_CACHESIZE, CONFIG_TYPE_SIZE_T, "20000000", &ldbm_config_import_cachesize_get, &ldbm_config_import_cachesize_set, CONFIG_FLAG_ALWAYS_SHOW|CONFIG_FLAG_ALLOW_RUNNING_CHANGE},
    {CONFIG_IDL_SWITCH, CONFIG_TYPE_STRING, "new", &ldbm_config_idl_get_idl_new, &ldbm_config_idl_set_tune, CONFIG_FLAG_ALWAYS_SHOW},
    {CONFIG_IDL_UPDATE, CONFIG_TYPE_ONOFF, "on", &ldbm_config_idl_get_update, &ldbm_config_idl_set_update, 0},
    {CONFIG_BYPASS_FILTER_TEST, CONFIG_TYPE_STRING, "on", &ldbm_config_get_bypass_filter_test, &ldbm_config_set_bypass_filter_test, CONFIG_FLAG_ALWAYS_SHOW|CONFIG_FLAG_ALLOW_RUNNING_CHANGE},
    {CONFIG_USE_VLV_INDEX, CONFIG_TYPE_ONOFF, "on", &ldbm_config_get_use_vlv_index, &ldbm_config_set_use_vlv_index, CONFIG_FLAG_ALWAYS_SHOW|CONFIG_FLAG_ALLOW_RUNNING_CHANGE},
    {CONFIG_DB_LOCKDOWN, CONFIG_TYPE_ONOFF, "off", &ldbm_config_db_lockdown_get, &ldbm_config_db_lockdown_set, 0},
    {CONFIG_INDEX_BUFFER_SIZE, CONFIG_TYPE_INT, "0", &ldbm_config_index_buffer_size_get, &ldbm_config_index_buffer_size_set, 0},
    {CONFIG_EXCLUDE_FROM_EXPORT, CONFIG_TYPE_STRING,
            CONFIG_EXCLUDE_FROM_EXPORT_DEFAULT_VALUE,
            &ldbm_config_exclude_from_export_get,
            &ldbm_config_exclude_from_export_set, CONFIG_FLAG_ALWAYS_SHOW},
    {CONFIG_DB_TX_MAX, CONFIG_TYPE_INT, "200", &ldbm_config_db_tx_max_get, &ldbm_config_db_tx_max_set, 0},
    {CONFIG_SERIAL_LOCK, CONFIG_TYPE_ONOFF, "on", &ldbm_config_serial_lock_get, &ldbm_config_serial_lock_set, CONFIG_FLAG_ALWAYS_SHOW|CONFIG_FLAG_ALLOW_RUNNING_CHANGE},
    {CONFIG_USE_LEGACY_ERRORCODE, CONFIG_TYPE_ONOFF, "off", &ldbm_config_legacy_errcode_get, &ldbm_config_legacy_errcode_set, 0},
    {CONFIG_ENTRYRDN_SWITCH, CONFIG_TYPE_ONOFF, "on", &ldbm_config_entryrdn_switch_get, &ldbm_config_entryrdn_switch_set, CONFIG_FLAG_ALWAYS_SHOW},
    {CONFIG_ENTRYRDN_NOANCESTORID, CONFIG_TYPE_ONOFF, "off", &ldbm_config_entryrdn_noancestorid_get, &ldbm_config_entryrdn_noancestorid_set, 0 /* no show */},
    {CONFIG_PAGEDLOOKTHROUGHLIMIT, CONFIG_TYPE_INT, "0", &ldbm_config_pagedlookthroughlimit_get, &ldbm_config_pagedlookthroughlimit_set, CONFIG_FLAG_ALWAYS_SHOW|CONFIG_FLAG_ALLOW_RUNNING_CHANGE},
    {CONFIG_PAGEDIDLISTSCANLIMIT, CONFIG_TYPE_INT, "0", &ldbm_config_pagedallidsthreshold_get, &ldbm_config_pagedallidsthreshold_set, CONFIG_FLAG_ALWAYS_SHOW|CONFIG_FLAG_ALLOW_RUNNING_CHANGE},
    {CONFIG_RANGELOOKTHROUGHLIMIT, CONFIG_TYPE_INT, "5000", &ldbm_config_rangelookthroughlimit_get, &ldbm_config_rangelookthroughlimit_set, CONFIG_FLAG_ALWAYS_SHOW|CONFIG_FLAG_ALLOW_RUNNING_CHANGE},
    {CONFIG_BACKEND_OPT_LEVEL, CONFIG_TYPE_INT, "1", &ldbm_config_backend_opt_level_get, &ldbm_config_backend_opt_level_set, CONFIG_FLAG_ALWAYS_SHOW},
    {CONFIG_DB_DEADLOCK_POLICY, CONFIG_TYPE_INT, STRINGIFYDEFINE(DB_LOCK_YOUNGEST), &ldbm_config_db_deadlock_policy_get, &ldbm_config_db_deadlock_policy_set, CONFIG_FLAG_ALWAYS_SHOW|CONFIG_FLAG_ALLOW_RUNNING_CHANGE},
    {NULL, 0, NULL, NULL, NULL, 0}
};

void ldbm_config_setup_default(struct ldbminfo *li) 
{
    config_info *config;
    char err_buf[SLAPI_DSE_RETURNTEXT_SIZE];
    
    for (config = ldbm_config; config->config_name != NULL; config++) {
        ldbm_config_set((void *)li, config->config_name, ldbm_config, NULL /* use default */, err_buf, CONFIG_PHASE_INITIALIZATION, 1 /* apply */, LDAP_MOD_REPLACE);
    }
}

int
ldbm_config_read_instance_entries(struct ldbminfo *li, const char *backend_type)
{
    Slapi_PBlock *tmp_pb;
    Slapi_Entry **entries = NULL;
    char *basedn = NULL;
    int rc = 0;

    /* Construct the base dn of the subtree that holds the instance entries. */
    basedn = slapi_create_dn_string("cn=%s,cn=plugins,cn=config", backend_type);
    if (NULL == basedn) {
        LDAPDebug1Arg(LDAP_DEBUG_ANY,
                      "ldbm_config_read_instance_entries: "
                      "failed to create backend dn for %s\n", backend_type);
        return 1;
    }

    /* Do a search of the subtree containing the instance entries */
    tmp_pb = slapi_pblock_new();
    slapi_search_internal_set_pb(tmp_pb, basedn, LDAP_SCOPE_SUBTREE, "(objectclass=nsBackendInstance)", NULL, 0, NULL, NULL, li->li_identity, 0);
    slapi_search_internal_pb (tmp_pb);
    slapi_pblock_get(tmp_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
    if (entries!=NULL) {
        int i;
        for (i=0; entries[i]!=NULL; i++) {
            rc = ldbm_instance_add_instance_entry_callback(NULL,
                                             entries[i], NULL, NULL, NULL, li);
            if (SLAPI_DSE_CALLBACK_ERROR == rc) {
                LDAPDebug1Arg(LDAP_DEBUG_ANY,
                      "ldbm_config_read_instance_entries: "
                      "failed to add instance entry %s\n", 
                      slapi_entry_get_dn_const(entries[i]));
                break;
            }
            rc = 0;
        }
    }

    slapi_free_search_results_internal(tmp_pb);
    slapi_pblock_destroy(tmp_pb);
    slapi_ch_free_string(&basedn);

    return rc;
}

/* Reads in any config information held in the dse for the ldbm plugin.  
 * Creates dse entries used to configure the ldbm plugin and dblayer
 * if they don't already exist.  Registers dse callback functions to
 * maintain those dse entries.  Returns 0 on success.
 */
int ldbm_config_load_dse_info(struct ldbminfo *li)
{
    Slapi_PBlock *search_pb;
    Slapi_Entry **entries = NULL;
    char *dn = NULL;
    int rval = 0;

    /* We try to read the entry 
     * cn=config, cn=ldbm database, cn=plugins, cn=config.  If the entry is
     * there, then we process the config information it stores.
     */
    dn = slapi_create_dn_string("cn=config,cn=%s,cn=plugins,cn=config", 
                                li->li_plugin->plg_name);
    if (NULL == dn) {
        LDAPDebug1Arg(LDAP_DEBUG_ANY,
                      "ldbm_config_load_dse_info: "
                      "failed create config dn for %s\n",
                      li->li_plugin->plg_name); 
        rval = 1;
        goto bail;
    }

    search_pb = slapi_pblock_new();
    if (!search_pb) {
        LDAPDebug(LDAP_DEBUG_ANY,
                      "ldbm_config_load_dse_info: Out of memory\n",
                      0, 0, 0);
        rval = 1;
        goto bail;
    }

    slapi_search_internal_set_pb(search_pb, dn, LDAP_SCOPE_BASE, 
        "objectclass=*", NULL, 0, NULL, NULL, li->li_identity, 0);
    slapi_search_internal_pb (search_pb);
    slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_RESULT, &rval);

    if (LDAP_NO_SUCH_OBJECT == rval) {
        /* Add skeleten dse entries for the ldbm plugin */
        ldbm_config_add_dse_entries(li, ldbm_skeleton_entries,
                                    li->li_plugin->plg_name, NULL, NULL, 0);
    } else if (rval != LDAP_SUCCESS) {
        LDAPDebug(LDAP_DEBUG_ANY, "Error accessing the ldbm config DSE\n",
                  0, 0, 0);
        rval = 1;
        goto bail;
    } else {
        /* Need to parse the configuration information for the ldbm
         * plugin that is held in the DSE. */
        slapi_pblock_get(search_pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES,
                         &entries);
        if (NULL == entries || entries[0] == NULL) {
            LDAPDebug(LDAP_DEBUG_ANY, "Error accessing the ldbm config DSE\n",
                      0, 0, 0);
            rval = 1;
            goto bail;
        }
        if (0 != parse_ldbm_config_entry(li, entries[0], ldbm_config)) {
            LDAPDebug(LDAP_DEBUG_ANY, "Error parsing the ldbm config DSE\n",
                      0, 0, 0);
            rval = 1;
            goto bail;
        }
    }

    if (search_pb) {
        slapi_free_search_results_internal(search_pb);
        slapi_pblock_destroy(search_pb);
    }
    
    /* Find all the instance entries and create a Slapi_Backend and an
     * ldbm_instance for each */
    rval = ldbm_config_read_instance_entries(li, li->li_plugin->plg_name);
    if (rval) {
        LDAPDebug0Args(LDAP_DEBUG_ANY,
                       "ldbm_config_load_dse_info: "
                       "failed to read instance entries\n");
        goto bail;
    }

    /* setup the dse callback functions for the ldbm backend config entry */
    slapi_config_register_callback(SLAPI_OPERATION_SEARCH, DSE_FLAG_PREOP, dn,
        LDAP_SCOPE_BASE, "(objectclass=*)", ldbm_config_search_entry_callback,
        (void *) li);
    slapi_config_register_callback(SLAPI_OPERATION_MODIFY, DSE_FLAG_PREOP, dn,
        LDAP_SCOPE_BASE, "(objectclass=*)", ldbm_config_modify_entry_callback,
        (void *) li);
    slapi_config_register_callback(DSE_OPERATION_WRITE, DSE_FLAG_PREOP, dn,
        LDAP_SCOPE_BASE, "(objectclass=*)", ldbm_config_search_entry_callback,
        (void *) li);
    slapi_ch_free_string(&dn);

    /* setup the dse callback functions for the ldbm backend monitor entry */
    dn = slapi_create_dn_string("cn=monitor,cn=%s,cn=plugins,cn=config",
                                li->li_plugin->plg_name);
    if (NULL == dn) {
        LDAPDebug1Arg(LDAP_DEBUG_ANY,
                      "ldbm_config_load_dse_info: "
                      "failed to create monitor dn for %s\n",
                      li->li_plugin->plg_name); 
        rval = 1;
        goto bail;
    }
    slapi_config_register_callback(SLAPI_OPERATION_SEARCH, DSE_FLAG_PREOP, dn,
        LDAP_SCOPE_BASE, "(objectclass=*)", ldbm_back_monitor_search,
        (void *)li);
    slapi_ch_free_string(&dn);

    /* And the ldbm backend database monitor entry */
    dn = slapi_create_dn_string("cn=database,cn=monitor,cn=%s,cn=plugins,cn=config",
                                li->li_plugin->plg_name);
    if (NULL == dn) {
        LDAPDebug1Arg(LDAP_DEBUG_ANY,
                      "ldbm_config_load_dse_info: "
                      "failed create monitor database dn for %s\n",
                      li->li_plugin->plg_name); 
        rval = 1;
        goto bail;
    }
    slapi_config_register_callback(SLAPI_OPERATION_SEARCH, DSE_FLAG_PREOP, dn,
        LDAP_SCOPE_BASE, "(objectclass=*)", ldbm_back_dbmonitor_search,
        (void *)li);
    slapi_ch_free_string(&dn);

    /* setup the dse callback functions for the ldbm backend instance
     * entries */
    dn = slapi_create_dn_string("cn=%s,cn=plugins,cn=config", 
                                li->li_plugin->plg_name);
    if (NULL == dn) {
        LDAPDebug1Arg(LDAP_DEBUG_ANY,
                      "ldbm_config_load_dse_info: "
                      "failed create plugin dn for %s\n",
                      li->li_plugin->plg_name); 
        rval = 1;
        goto bail;
    }
    slapi_config_register_callback(SLAPI_OPERATION_ADD, DSE_FLAG_PREOP, dn,
        LDAP_SCOPE_SUBTREE, "(objectclass=nsBackendInstance)",
        ldbm_instance_add_instance_entry_callback, (void *) li);
    slapi_config_register_callback(SLAPI_OPERATION_ADD, DSE_FLAG_POSTOP, dn,
        LDAP_SCOPE_SUBTREE, "(objectclass=nsBackendInstance)",
        ldbm_instance_postadd_instance_entry_callback, (void *) li);
    slapi_config_register_callback(SLAPI_OPERATION_DELETE, DSE_FLAG_PREOP, dn,
        LDAP_SCOPE_SUBTREE, "(objectclass=nsBackendInstance)",
        ldbm_instance_delete_instance_entry_callback, (void *) li);
    slapi_config_register_callback(SLAPI_OPERATION_DELETE, DSE_FLAG_POSTOP, dn,
        LDAP_SCOPE_SUBTREE, "(objectclass=nsBackendInstance)",
        ldbm_instance_post_delete_instance_entry_callback, (void *) li);
bail:
    slapi_ch_free_string(&dn);
    return rval;
}


/* Utility function used in creating config entries.  Using the
 * config_info, this function gets info and formats in the correct
 * way.
 * buf is char[BUFSIZ]
 */
void ldbm_config_get(void *arg, config_info *config, char *buf)
{
    char *tmp_string;
    size_t val = 0;
    
    if (config == NULL) {
        buf[0] = '\0';
        return;
    }
    
    switch(config->config_type) {
    case CONFIG_TYPE_INT:
            sprintf(buf, "%d", (int) ((uintptr_t)config->config_get_fn(arg)));
            break;
    case CONFIG_TYPE_INT_OCTAL:
        sprintf(buf, "%o", (int) ((uintptr_t)config->config_get_fn(arg)));
        break;
    case CONFIG_TYPE_LONG:
        sprintf(buf, "%ld", (long) config->config_get_fn(arg));
        break;
    case CONFIG_TYPE_SIZE_T:
        val = (size_t) config->config_get_fn(arg);
        sprintf(buf, "%lu", (long unsigned int)val);
        break;
    case CONFIG_TYPE_STRING:
        /* Remember the get function for strings returns memory
         * that must be freed. */
        tmp_string = (char *) config->config_get_fn(arg);
        PR_snprintf(buf, BUFSIZ, "%s", (char *) tmp_string);
        slapi_ch_free((void **)&tmp_string);
        break;
    case CONFIG_TYPE_ONOFF:
        if ((int) ((uintptr_t)config->config_get_fn(arg))) {
            sprintf(buf, "on");
        } else {
            sprintf(buf, "off");
        }
        break;
    }
}

/*
 * Returns:
 *   SLAPI_DSE_CALLBACK_ERROR on failure
 *   SLAPI_DSE_CALLBACK_OK on success
 */
int ldbm_config_search_entry_callback(Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Entry *entryAfter, int *returncode, char *returntext, void *arg)
{
    char buf[BUFSIZ];
    struct berval *vals[2];
    struct berval val;
    struct ldbminfo *li= (struct ldbminfo *) arg;
    config_info *config;
    
    vals[0] = &val;
    vals[1] = NULL;  
    
    returntext[0] = '\0';
    
    PR_Lock(li->li_config_mutex);
    
    for(config = ldbm_config; config->config_name != NULL; config++) {
        /* Go through the ldbm_config table and fill in the entry. */

        if (!(config->config_flags & (CONFIG_FLAG_ALWAYS_SHOW | CONFIG_FLAG_PREVIOUSLY_SET))) {
            /* This config option shouldn't be shown */
            continue;
        }
        
        ldbm_config_get((void *) li, config, buf);
        
        val.bv_val = buf;
        val.bv_len = strlen(buf);
        slapi_entry_attr_replace(e, config->config_name, vals);
    }
    
    PR_Unlock(li->li_config_mutex);

    *returncode = LDAP_SUCCESS;
    return SLAPI_DSE_CALLBACK_OK;
}


int ldbm_config_ignored_attr(char *attr_name)
{
    /* These are the names of attributes that are in the
     * config entries but are not config attributes. */
    if (!strcasecmp("objectclass", attr_name) ||
        !strcasecmp("cn", attr_name) ||
        !strcasecmp("creatorsname", attr_name) ||
        !strcasecmp("createtimestamp", attr_name) ||
        !strcasecmp(LDBM_NUMSUBORDINATES_STR, attr_name) ||
        slapi_attr_is_last_mod(attr_name)){
        return 1;
    } else {
        return 0;
    }
}

/* Returns LDAP_SUCCESS on success */
int ldbm_config_set(void *arg, char *attr_name, config_info *config_array, struct berval *bval, char *err_buf, int phase, int apply_mod, int mod_op)
{
    config_info *config;
    int use_default;
    int int_val;
    long long_val;
    size_t sz_val;
    PRInt64 llval;
    int maxint = (int)(((unsigned int)~0)>>1);
    int minint = ~maxint;
    PRInt64 llmaxint;
    PRInt64 llminint;
    int err = 0;
    char *str_val;
    int retval = 0;

    LL_I2L(llmaxint, maxint);
    LL_I2L(llminint, minint);

    config = get_config_info(config_array, attr_name);
    if (NULL == config) {
        LDAPDebug(LDAP_DEBUG_CONFIG, "Unknown config attribute %s\n", attr_name, 0, 0);
        PR_snprintf(err_buf, SLAPI_DSE_RETURNTEXT_SIZE, "Unknown config attribute %s\n", attr_name);
        return LDAP_SUCCESS; /* Ignore unknown attributes */
    }

    /* Some config attrs can't be changed while the server is running. */
    if (phase == CONFIG_PHASE_RUNNING && 
        !(config->config_flags & CONFIG_FLAG_ALLOW_RUNNING_CHANGE)) {
        PR_snprintf(err_buf, SLAPI_DSE_RETURNTEXT_SIZE, "%s can't be modified while the server is running.\n", attr_name);
        LDAPDebug(LDAP_DEBUG_ANY, "%s", err_buf, 0, 0);
        return LDAP_UNWILLING_TO_PERFORM;
    }
    
    /* If the config phase is initialization or if bval is NULL or if we are deleting
       the value, we will use the default value for the attribute. */
    if ((CONFIG_PHASE_INITIALIZATION == phase) || (NULL == bval) || SLAPI_IS_MOD_DELETE(mod_op)) {
        if (CONFIG_FLAG_SKIP_DEFAULT_SETTING & config->config_flags) {
            return LDAP_SUCCESS; /* Skipping the default config setting */
        }
        use_default = 1;
    } else {
        use_default = 0;

        /* cannot use mod add on a single valued attribute if the attribute was
           previously set to a non-default value */
        if (SLAPI_IS_MOD_ADD(mod_op) && apply_mod &&
            (config->config_flags & CONFIG_FLAG_PREVIOUSLY_SET)) {
            PR_snprintf(err_buf, SLAPI_DSE_RETURNTEXT_SIZE,
                        "cannot add a value to single valued attribute %s.\n",
                        attr_name);
            return LDAP_OBJECT_CLASS_VIOLATION;
        }
    }

    /* if delete, and a specific value was provided to delete, the existing value must
       match that value, or return LDAP_NO_SUCH_ATTRIBUTE */
    if (SLAPI_IS_MOD_DELETE(mod_op) && bval && bval->bv_len && bval->bv_val) {
        char buf[BUFSIZ];
        ldbm_config_get(arg, config, buf);
        if (PL_strncmp(buf, bval->bv_val, bval->bv_len)) {
            PR_snprintf(err_buf, SLAPI_DSE_RETURNTEXT_SIZE,
                        "value [%s] for attribute %s does not match existing value [%s].\n",
                        bval->bv_val, attr_name, buf);
            return LDAP_NO_SUCH_ATTRIBUTE;
        }
    }
    
    switch(config->config_type) {
    case CONFIG_TYPE_INT:
        if (use_default) {
            str_val = config->config_default_value;
        } else {
            str_val = bval->bv_val;
        }
        /* get the value as a 64 bit value */
        llval = db_atoi(str_val, &err);
        /* check for parsing error (e.g. not a number) */
        if (err) {
            PR_snprintf(err_buf, SLAPI_DSE_RETURNTEXT_SIZE, "Error: value %s for attr %s is not a number\n",
                    str_val, attr_name);
            LDAPDebug(LDAP_DEBUG_ANY, "%s", err_buf, 0, 0);
            return LDAP_UNWILLING_TO_PERFORM;
        /* check for overflow */
        } else if (LL_CMP(llval, >, llmaxint)) {
            PR_snprintf(err_buf, SLAPI_DSE_RETURNTEXT_SIZE, "Error: value %s for attr %s is greater than the maximum %d\n",
                    str_val, attr_name, maxint);
            LDAPDebug(LDAP_DEBUG_ANY, "%s", err_buf, 0, 0);
            return LDAP_UNWILLING_TO_PERFORM;
        /* check for underflow */
        } else if (LL_CMP(llval, <, llminint)) {
            PR_snprintf(err_buf, SLAPI_DSE_RETURNTEXT_SIZE, "Error: value %s for attr %s is less than the minimum %d\n",
                    str_val, attr_name, minint);
            LDAPDebug(LDAP_DEBUG_ANY, "%s", err_buf, 0, 0);
            return LDAP_UNWILLING_TO_PERFORM;
        }
        /* convert 64 bit value to 32 bit value */
        LL_L2I(int_val, llval);
        retval = config->config_set_fn(arg, (void *) ((uintptr_t)int_val), err_buf, phase, apply_mod);
        break;
    case CONFIG_TYPE_INT_OCTAL:
        if (use_default) {
            int_val = (int) strtol(config->config_default_value, NULL, 8);
        } else {
            int_val = (int) strtol((char *)bval->bv_val, NULL, 8);
        }
        retval = config->config_set_fn(arg, (void *) ((uintptr_t)int_val), err_buf, phase, apply_mod);
        break;
    case CONFIG_TYPE_LONG:
        if (use_default) {
            str_val = config->config_default_value;
        } else {
            str_val = bval->bv_val;
        }
        /* get the value as a 64 bit value */
        llval = db_atoi(str_val, &err);
        /* check for parsing error (e.g. not a number) */
        if (err) {
            PR_snprintf(err_buf, SLAPI_DSE_RETURNTEXT_SIZE, "Error: value %s for attr %s is not a number\n",
                    str_val, attr_name);
            LDAPDebug(LDAP_DEBUG_ANY, "%s", err_buf, 0, 0);
            return LDAP_UNWILLING_TO_PERFORM;
        /* check for overflow */
        } else if (LL_CMP(llval, >, llmaxint)) {
            PR_snprintf(err_buf, SLAPI_DSE_RETURNTEXT_SIZE, "Error: value %s for attr %s is greater than the maximum %d\n",
                    str_val, attr_name, maxint);
            LDAPDebug(LDAP_DEBUG_ANY, "%s", err_buf, 0, 0);
            return LDAP_UNWILLING_TO_PERFORM;
        /* check for underflow */
        } else if (LL_CMP(llval, <, llminint)) {
            PR_snprintf(err_buf, SLAPI_DSE_RETURNTEXT_SIZE, "Error: value %s for attr %s is less than the minimum %d\n",
                    str_val, attr_name, minint);
            LDAPDebug(LDAP_DEBUG_ANY, "%s", err_buf, 0, 0);
            return LDAP_UNWILLING_TO_PERFORM;
        }
        /* convert 64 bit value to 32 bit value */
        LL_L2I(long_val, llval);
        retval = config->config_set_fn(arg, (void *) long_val, err_buf, phase, apply_mod);
        break;
    case CONFIG_TYPE_SIZE_T:
        if (use_default) {
            str_val = config->config_default_value;
        } else {
            str_val = bval->bv_val;
        }

        /* get the value as a size_t value */
        sz_val = db_strtoul(str_val, &err);

        /* check for parsing error (e.g. not a number) */
        if (err == EINVAL) {
            PR_snprintf(err_buf, SLAPI_DSE_RETURNTEXT_SIZE, "Error: value %s for attr %s is not a number\n",
                    str_val, attr_name);
            LDAPDebug(LDAP_DEBUG_ANY, "%s", err_buf, 0, 0);
            return LDAP_UNWILLING_TO_PERFORM;
            /* check for overflow */
        } else if (err == ERANGE) {
            PR_snprintf(err_buf, SLAPI_DSE_RETURNTEXT_SIZE, "Error: value %s for attr %s is outside the range of representable values\n",
                    str_val, attr_name);
            LDAPDebug(LDAP_DEBUG_ANY, "%s", err_buf, 0, 0);
            return LDAP_UNWILLING_TO_PERFORM;
        }
        retval = config->config_set_fn(arg, (void *) sz_val, err_buf, phase, apply_mod);
        break;
    case CONFIG_TYPE_STRING:
        if (use_default) {
            retval = config->config_set_fn(arg, config->config_default_value, err_buf, phase, apply_mod);
        } else {
            retval = config->config_set_fn(arg, bval->bv_val, err_buf, phase, apply_mod);
        }
        break;
    case CONFIG_TYPE_ONOFF:
        if (use_default) {
            int_val = !strcasecmp(config->config_default_value, "on");
        } else {
            int_val = !strcasecmp((char *) bval->bv_val, "on");
        }
        retval = config->config_set_fn(arg, (void *) ((uintptr_t)int_val), err_buf, phase, apply_mod);
        break;
    }

    /* operation was successful and we applied the value? */
    if (!retval && apply_mod) {
        /* Since we are setting the value for the config attribute, we
         * need to turn on the CONFIG_FLAG_PREVIOUSLY_SET flag to make
         * sure this attribute is shown. */
        if (use_default) {
            /* attr deleted or we are using the default value */
            config->config_flags &= ~CONFIG_FLAG_PREVIOUSLY_SET;
        } else {
            /* attr set explicitly */
            config->config_flags |= CONFIG_FLAG_PREVIOUSLY_SET;
        }
    }
    
    return retval;
}


static int parse_ldbm_config_entry(struct ldbminfo *li, Slapi_Entry *e, config_info *config_array)
{
    Slapi_Attr *attr = NULL;
    
    for (slapi_entry_first_attr(e, &attr); attr; slapi_entry_next_attr(e, attr, &attr)) { 
        char *attr_name = NULL;
        Slapi_Value *sval = NULL;
        struct berval *bval;
        char err_buf[SLAPI_DSE_RETURNTEXT_SIZE];
        
        slapi_attr_get_type(attr, &attr_name);
        
        /* There are some attributes that we don't care about, like objectclass. */
        if (ldbm_config_ignored_attr(attr_name)) {
            continue;
        }
        slapi_attr_first_value(attr, &sval);
        bval = (struct berval *) slapi_value_get_berval(sval);
        
        if (ldbm_config_set(li, attr_name, config_array, bval, err_buf, CONFIG_PHASE_STARTUP, 1 /* apply */, LDAP_MOD_REPLACE) != LDAP_SUCCESS) {
            LDAPDebug(LDAP_DEBUG_ANY, "Error with config attribute %s : %s\n", attr_name, err_buf, 0);
            return 1;
        }
    }
    return 0;
}

/* helper for deleting mods (we do not want to be applied) from the mods array */
static void
mod_free(LDAPMod *mod)
{
	ber_bvecfree(mod->mod_bvalues);
	slapi_ch_free((void**)&(mod->mod_type));
	slapi_ch_free((void**)&mod);
}

/*
 * Returns:
 *   SLAPI_DSE_CALLBACK_ERROR on failure
 *   SLAPI_DSE_CALLBACK_OK on success
 */        
int ldbm_config_modify_entry_callback(Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry* e, int *returncode, char *returntext, void *arg) 
{ 
    int i; 
    char *attr_name; 
    LDAPMod **mods; 
    int rc = LDAP_SUCCESS; 
    int apply_mod = 0; 
    struct ldbminfo *li= (struct ldbminfo *) arg;
    int reapply_mods = 0;
    int idx = 0;

    /* This lock is probably way too conservative, but we don't expect much
     * contention for it. */
    PR_Lock(li->li_config_mutex);
        
    slapi_pblock_get( pb, SLAPI_MODIFY_MODS, &mods ); 
    
    returntext[0] = '\0'; 

    /* 
     * First pass: set apply mods to 0 so only input validation will be done; 
     * 2nd pass: set apply mods to 1 to apply changes to internal storage 
     */ 
    for ( apply_mod = 0; apply_mod <= 1 && LDAP_SUCCESS == rc; apply_mod++ ) {
        for (i = 0; mods && mods[i] && LDAP_SUCCESS == rc; i++) {
            attr_name = mods[i]->mod_type;

            /* There are some attributes that we don't care about, like modifiersname. */
            if (ldbm_config_ignored_attr(attr_name)) {
                if (apply_mod) {
                    Slapi_Attr *origattr = NULL;
                    Slapi_ValueSet *origvalues = NULL;
                    mods[idx++] = mods[i];
                    /* we also need to restore the entryAfter e to its original
                       state, because the dse code will attempt to reapply
                       the mods again */
                    slapi_entry_attr_find(entryBefore, attr_name, &origattr);
                    if (NULL != origattr) {
                        slapi_attr_get_valueset(origattr, &origvalues);
                        if (NULL != origvalues) {
                            slapi_entry_add_valueset(e, attr_name, origvalues);
                            slapi_valueset_free(origvalues);
                        }
                    }
                }
                continue;
            }

            reapply_mods = 1; /* there is at least one mod we removed */
            /* when deleting a value, and this is the last or only value, set
               the config param to its default value
               when adding a value, if the value is set to its default value, replace
               it with the new value - otherwise, if it is single valued, reject the
               operation with TYPE_OR_VALUE_EXISTS */
            /* This assumes there is only one bval for this mod. */
            rc = ldbm_config_set((void *) li, attr_name, ldbm_config,
                                 ( mods[i]->mod_bvalues == NULL ) ? NULL
                                 : mods[i]->mod_bvalues[0], returntext,
                                 ((li->li_flags&LI_FORCE_MOD_CONFIG)?
                                  CONFIG_PHASE_INTERNAL:CONFIG_PHASE_RUNNING),
                                 apply_mod, mods[i]->mod_op); 
            if (apply_mod) {
                mod_free(mods[i]);
                mods[i] = NULL;
            }
        } 
    } 
    
    PR_Unlock(li->li_config_mutex);

    if (reapply_mods) {
        mods[idx] = NULL;
        slapi_pblock_set(pb, SLAPI_DSE_REAPPLY_MODS, &reapply_mods);
    }
    
    *returncode= rc; 
    if(LDAP_SUCCESS == rc) { 
        return SLAPI_DSE_CALLBACK_OK; 
    } 
    else { 
        return SLAPI_DSE_CALLBACK_ERROR; 
    } 
} 


/* This function is used to set config attributes. It can be used as a 
 * shortcut to doing an internal modify operation on the config DSE.
 */
void ldbm_config_internal_set(struct ldbminfo *li, char *attrname, char *value)
{
    char err_buf[SLAPI_DSE_RETURNTEXT_SIZE];
    struct berval bval;
    
    bval.bv_val = value;
    bval.bv_len = strlen(value);
    
    if (ldbm_config_set((void *) li, attrname, ldbm_config, &bval, 
                        err_buf, CONFIG_PHASE_INTERNAL, 1 /* apply */,
                        LDAP_MOD_REPLACE) != LDAP_SUCCESS) {
        LDAPDebug(LDAP_DEBUG_ANY, 
                  "Internal Error: Error setting instance config attr %s to %s: %s\n", 
                  attrname, value, err_buf);
        exit(1);
    }
}

/*
 * replace_ldbm_config_value:
 * - update an ldbm database config value
 */
void replace_ldbm_config_value(char *conftype, char *val, struct ldbminfo *li)
{
    Slapi_PBlock pb;
    Slapi_Mods smods;

    pblock_init(&pb);
    slapi_mods_init(&smods, 1);
    slapi_mods_add(&smods, LDAP_MOD_REPLACE, conftype, strlen(val), val);
    slapi_modify_internal_set_pb(&pb, CONFIG_LDBM_DN,
                   slapi_mods_get_ldapmods_byref(&smods),
                   NULL, NULL, li->li_identity, 0);
    slapi_modify_internal_pb(&pb);
    slapi_mods_done(&smods);
    pblock_done(&pb);
}
