]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-bdb/ctxcsn.c
Move MSVC port to the Attic
[openldap] / servers / slapd / back-bdb / ctxcsn.c
index 6d9af8c80b686ee4a791159babfb88b7839ccf6e..1127995de39c5344c260871abe6c3dc270687bbe 100644 (file)
@@ -1,23 +1,18 @@
+/* ctxcsn.c -- back-bdb Context CSN Management Routines */
 /* $OpenLDAP$ */
-/*
- * back-bdb Context CSN Management Routines
- */
-/* Copyright (c) 2003 by International Business Machines, Inc.
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 2003-2004 The OpenLDAP Foundation.
+ * Portions Copyright 2003 IBM Corporation.
+ * All rights reserved.
  *
- * International Business Machines, Inc. (hereinafter called IBM) grants
- * permission under its copyrights to use, copy, modify, and distribute this
- * Software with or without fee, provided that the above copyright notice and
- * all paragraphs of this notice appear in all copies, and that the name of IBM
- * not be used in connection with the marketing of any product incorporating
- * the Software or modifications thereof, without specific, written prior
- * permission.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
  *
- * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
- * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
- * PARTICULAR PURPOSE.  IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
- * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
- * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
- * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
+ * A copy of this license is available in the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
  */
 
 #include "portable.h"
@@ -44,7 +39,6 @@ bdb_csn_commit(
 )
 {
        struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
-       struct berval   ctxcsn_ndn = { 0, NULL };
        EntryInfo               *ctxcsn_ei = NULL;
        DB_LOCK                 ctxcsn_lock;
        struct berval   max_committed_csn;
@@ -56,20 +50,27 @@ bdb_csn_commit(
        size_t                  textlen = sizeof textbuf;
        EntryInfo               *eip = NULL;
 
+       assert( !BER_BVISNULL( &op->o_bd->be_context_csn ) );
+
        if ( ei ) {
                e = ei->bei_e;
        }
 
-       build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0],
-               (struct berval *)&slap_ldapsync_cn_bv, op->o_tmpmemctx );
-
-       rc =  bdb_dn2entry( op, tid, &ctxcsn_ndn, &ctxcsn_ei,
+       rc =  bdb_dn2entry( op, tid, &op->o_bd->be_context_csn, &ctxcsn_ei,
                        1, locker, &ctxcsn_lock );
+       switch( rc ) {
+       case 0:
+       case DB_NOTFOUND:
+               break;
+       case DB_LOCK_DEADLOCK:
+       case DB_LOCK_NOTGRANTED:
+               return BDB_CSN_RETRY;
+       default:
+               return BDB_CSN_ABORT;
+       }
        
        *ctxcsn_e = ctxcsn_ei->bei_e;
 
-       op->o_tmpfree( ctxcsn_ndn.bv_val, op->o_tmpmemctx );
-
        slap_get_commit_csn( op, &max_committed_csn );
 
        if ( max_committed_csn.bv_val == NULL ) {
@@ -83,7 +84,7 @@ bdb_csn_commit(
                if ( !*ctxcsn_e ) {
                        rs->sr_err = LDAP_OTHER;
                        rs->sr_text = "context csn not present";
-                       ch_free( max_committed_csn.bv_val );
+                       op->o_tmpfree( max_committed_csn.bv_val, op->o_tmpmemctx );
                        return BDB_CSN_ABORT;
                } else {
                        Modifications mod;
@@ -95,7 +96,7 @@ bdb_csn_commit(
                        modvals[1].bv_len = 0;
 
                        mod.sml_op = LDAP_MOD_REPLACE;
-                       mod.sml_bvalues = modvals;
+                       mod.sml_values = modvals;
                        mod.sml_nvalues = NULL;
                        mod.sml_desc = slap_schema.si_ad_contextCSN;
                        mod.sml_type = mod.sml_desc->ad_cname;
@@ -104,7 +105,7 @@ bdb_csn_commit(
                        dummy = **ctxcsn_e;
                        ret = bdb_modify_internal( op, tid, &mod, &dummy,
                                                                        &rs->sr_text, textbuf, textlen );                                                      
-                       ch_free( max_committed_csn.bv_val );
+                       op->o_tmpfree( max_committed_csn.bv_val, op->o_tmpmemctx );
                        if ( ret != LDAP_SUCCESS ) {
 #ifdef NEW_LOGGING
                                LDAP_LOG ( OPERATION, ERR,
@@ -163,7 +164,7 @@ bdb_csn_commit(
                }
 
                *ctxcsn_e = slap_create_context_csn_entry( op->o_bd, &max_committed_csn );
-               ch_free( max_committed_csn.bv_val );
+               op->o_tmpfree( max_committed_csn.bv_val, op->o_tmpmemctx );
                (*ctxcsn_e)->e_id = ctxcsn_id;
                *ctxcsn_added = 1;
 
@@ -176,7 +177,7 @@ bdb_csn_commit(
                        goto rewind;
                case DB_KEYEXIST :
                        rs->sr_err = LDAP_OTHER;
-                       rs->sr_text = "context csn exists before contex prefix does";
+                       rs->sr_text = "context csn exists before context prefix does";
                        return BDB_CSN_ABORT;
                default :
                        rs->sr_err = LDAP_OTHER;
@@ -250,7 +251,6 @@ bdb_get_commit_csn(
 )
 {
        struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
-       struct berval ctxcsn_ndn = BER_BVNULL;
        struct berval csn = BER_BVNULL;
        EntryInfo       *ctxcsn_ei = NULL;
        EntryInfo       *suffix_ei = NULL;
@@ -263,130 +263,180 @@ bdb_get_commit_csn(
        int                     ctxcsn_added = 0;
        int                     rc;
        struct sync_cookie syncCookie = { NULL, -1, NULL};
+       syncinfo_t      *si;
+       u_int32_t       ctxcsn_locker = 0;
+
+       if ( op->o_sync_mode != SLAP_SYNC_NONE &&
+                !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
+               char substr[67];
+               struct berval ctxcsn_ndn = BER_BVNULL;
+               struct berval bv;
 
-       if ( op->o_sync_mode != SLAP_SYNC_NONE ) {
-               if ( op->o_bd->be_syncinfo ) {
-                       char substr[67];
-                       struct berval bv;
-                       sprintf( substr, "cn=syncrepl%d", op->o_bd->be_syncinfo->si_id );
+               LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
+                       sprintf( substr, "cn=syncrepl%ld", si->si_rid );
                        ber_str2bv( substr, 0, 0, &bv );
-                       build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0], &bv, NULL );
-               } else {
-                       build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0],
-                                               (struct berval *)&slap_ldapsync_cn_bv, NULL );
+                       build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0], &bv, op->o_tmpmemctx );
+
+consumer_ctxcsn_retry :
+                       rs->sr_err = bdb_dn2entry( op, NULL, &ctxcsn_ndn, &ctxcsn_ei,
+                                                                               0, locker, ctxcsn_lock );
+                       switch(rs->sr_err) {
+                       case DB_LOCK_DEADLOCK:
+                       case DB_LOCK_NOTGRANTED:
+                               goto consumer_ctxcsn_retry;
+                       case 0:
+                               op->o_tmpfree( ctxcsn_ndn.bv_val, op->o_tmpmemctx );
+                               ctxcsn_ndn.bv_val = NULL;
+                               if ( ctxcsn_ei ) {
+                                       ctxcsn_e = ctxcsn_ei->bei_e;
+                               }
+                               break;
+                       case DB_NOTFOUND:
+                       default:
+                               rs->sr_err = LDAP_OTHER;
+                       case LDAP_BUSY:
+                               op->o_tmpfree( ctxcsn_ndn.bv_val, op->o_tmpmemctx );
+                               ctxcsn_ndn.bv_val = NULL;
+                               goto done;
+                       }
+
+                       if ( ctxcsn_e ) {
+                               csn_a = attr_find( ctxcsn_e->e_attrs,
+                                                       slap_schema.si_ad_syncreplCookie );
+                               if ( csn_a ) {
+                                       struct berval cookie;
+                                       const char *text;
+                                       int match = -1;
+                                       ber_dupbv( &cookie, &csn_a->a_vals[0] );
+                                       ber_bvarray_add( &syncCookie.octet_str, &cookie );
+                                       slap_parse_sync_cookie( &syncCookie );
+                                       if ( *search_context_csn &&
+                                               (*search_context_csn)->bv_val != NULL )
+                                       {
+                                               value_match( &match, slap_schema.si_ad_entryCSN,
+                                                       slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
+                                                       SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
+                                                       syncCookie.ctxcsn, *search_context_csn, &text );
+                                       }
+                                       if ( match < 0 ) {
+                                               /* set search_context_csn to the
+                                                  smallest syncrepl cookie value */
+                                               if ( *search_context_csn ) {
+                                                       ch_free( (*search_context_csn)->bv_val );
+                                                       ch_free( *search_context_csn );
+                                               }
+                                               *search_context_csn = ber_dupbv( NULL,
+                                                       syncCookie.ctxcsn );
+                                       }
+                                       slap_sync_cookie_free( &syncCookie, 0 );
+                               } else {
+                                       *search_context_csn = NULL;
+                               } 
+                       } else {
+                               *search_context_csn = NULL;
+                       }
                }
+       } else if ( op->o_sync_mode != SLAP_SYNC_NONE &&
+                LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
 
-ctxcsn_retry :
-               rs->sr_err = bdb_dn2entry( op, NULL, &ctxcsn_ndn, &ctxcsn_ei,
+provider_ctxcsn_retry :
+               rs->sr_err = bdb_dn2entry( op, NULL, &op->o_bd->be_context_csn, &ctxcsn_ei,
                                                                        0, locker, ctxcsn_lock );
                switch(rs->sr_err) {
                case 0:
-                       ch_free( ctxcsn_ndn.bv_val );
                        if ( ctxcsn_ei ) {
                                ctxcsn_e = ctxcsn_ei->bei_e;
                        }
                        break;
-        case LDAP_BUSY:
-                       ch_free( ctxcsn_ndn.bv_val );
-                       LOCK_ID_FREE (bdb->bi_dbenv, locker );
-                       return LDAP_BUSY;
-        case DB_LOCK_DEADLOCK:
-        case DB_LOCK_NOTGRANTED:
-                       goto ctxcsn_retry;
-        case DB_NOTFOUND:
-                       if ( !op->o_bd->be_syncinfo ) {
-                               snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
-                                       bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
-
-                               slap_get_csn( op, csnbuf, sizeof(csnbuf), &csn, 1 );
-
-                               if ( 0 ) {
+               case LDAP_BUSY:
+                       goto done;
+               case DB_LOCK_DEADLOCK:
+               case DB_LOCK_NOTGRANTED:
+                       goto provider_ctxcsn_retry;
+               case DB_NOTFOUND:
+                       snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
+                               bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
+
+                       slap_get_csn( op, csnbuf, sizeof(csnbuf), &csn, 1 );
+
+                       if ( 0 ) {
 txn_retry:
-                                       rs->sr_err = TXN_ABORT( ltid );
-                                       if ( rs->sr_err != 0 ) {
-                                               rs->sr_err = LDAP_OTHER;
-                                               return rs->sr_err;
-                                       }
-                                       ldap_pvt_thread_yield();
-                                       bdb_trans_backoff( ++num_retries );
-                               }
-                               rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, &ltid, bdb->bi_db_opflags );
+                               rs->sr_err = TXN_ABORT( ltid );
+                               ltid = NULL;
                                if ( rs->sr_err != 0 ) {
                                        rs->sr_err = LDAP_OTHER;
-                                       return rs->sr_err;
+                                       goto done;
                                }
+                               ldap_pvt_thread_yield();
+                               bdb_trans_backoff( ++num_retries );
+                       }
+                       rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL,
+                                                               &ltid, bdb->bi_db_opflags );
+                       if ( rs->sr_err != 0 ) {
+                               rs->sr_err = LDAP_OTHER;
+                               goto done;
+                       }
 
-                               rs->sr_err = bdb_csn_commit( op, rs, ltid, NULL, &suffix_ei,
-                                                                               &ctxcsn_e, &ctxcsn_added, locker );
-                               switch( rs->sr_err ) {
-                               case BDB_CSN_ABORT:
-                                       LOCK_ID_FREE( bdb->bi_dbenv, locker );
-                                       return LDAP_OTHER;
-                               case BDB_CSN_RETRY:
-                                       goto txn_retry;
-                               }
+                       ctxcsn_locker = TXN_ID ( ltid );
 
-                               rs->sr_err = TXN_PREPARE( ltid, gid );
-                               if ( rs->sr_err != 0 ) {
-                                       rs->sr_err = LDAP_OTHER;
-                                       return rs->sr_err;
-                               }
+                       rs->sr_err = bdb_csn_commit( op, rs, ltid, NULL, &suffix_ei,
+                                                                       &ctxcsn_e, &ctxcsn_added, ctxcsn_locker );
+                       switch( rs->sr_err ) {
+                       case BDB_CSN_ABORT:
+                               rs->sr_err = LDAP_OTHER;
+                               goto done;      
+                       case BDB_CSN_RETRY:
+                               goto txn_retry;
+                       }
 
-                               bdb_cache_add( bdb, suffix_ei, ctxcsn_e,
-                                               (struct berval *)&slap_ldapsync_cn_bv, locker );
+                       rs->sr_err = TXN_PREPARE( ltid, gid );
+                       if ( rs->sr_err != 0 ) {
+                               rs->sr_err = LDAP_OTHER;
+                               goto done;
+                       }
 
-                               rs->sr_err = TXN_COMMIT( ltid, 0 );
-                               if ( rs->sr_err != 0 ) {
-                                       rs->sr_err = LDAP_OTHER;
-                                       return rs->sr_err;
-                               }
+                       bdb_cache_add( bdb, suffix_ei, ctxcsn_e,
+                                       (struct berval *)&slap_ldapsync_cn_bv, ctxcsn_locker );
 
-                               ctxcsn_ei = NULL;
-                               rs->sr_err = bdb_dn2entry( op, NULL, &ctxcsn_ndn, &ctxcsn_ei,
-                                                                               0, locker, ctxcsn_lock );
-                               ch_free( ctxcsn_ndn.bv_val );
+                       rs->sr_err = TXN_COMMIT( ltid, 0 );
+                       if ( rs->sr_err != 0 ) {
+                               rs->sr_err = LDAP_OTHER;
+                               goto done;
+                       }
 
-                               if ( ctxcsn_ei ) {
-                                       ctxcsn_e = ctxcsn_ei->bei_e;
-                               }
-                       } else {
-                               LOCK_ID_FREE( bdb->bi_dbenv, locker );
-                               return LDAP_OTHER;
+                       rs->sr_err = bdb_dn2entry( op, NULL, &op->o_bd->be_context_csn, &ctxcsn_ei,
+                                    0, ctxcsn_locker, ctxcsn_lock );
+
+                       if ( ctxcsn_ei ) {
+                               ctxcsn_e = ctxcsn_ei->bei_e;
                        }
                        break;
 
                default:
-                       LOCK_ID_FREE (bdb->bi_dbenv, locker );
-                       return LDAP_OTHER;
+                       rs->sr_err = LDAP_OTHER;
+                       goto done;
                }
 
                if ( ctxcsn_e ) {
-                       if ( op->o_bd->be_syncinfo ) {
-                               csn_a = attr_find( ctxcsn_e->e_attrs,
-                                       slap_schema.si_ad_syncreplCookie );
-                               if ( csn_a ) {
-                                       struct berval cookie;
-                                       ber_dupbv( &cookie, &csn_a->a_vals[0] );
-                                       ber_bvarray_add( &syncCookie.octet_str, &cookie );
-                                       slap_parse_sync_cookie( &syncCookie );
-                                       *search_context_csn = ber_dupbv( NULL, syncCookie.ctxcsn );
-                                       slap_sync_cookie_free( &syncCookie, 0 );
-                               } else {
-                                       *search_context_csn = NULL;
-                               }
+                       csn_a = attr_find( ctxcsn_e->e_attrs,
+                                               slap_schema.si_ad_contextCSN );
+                       if ( csn_a ) {
+                               *search_context_csn = ber_dupbv( NULL, &csn_a->a_vals[0] );
                        } else {
-                               csn_a = attr_find( ctxcsn_e->e_attrs,
-                                       slap_schema.si_ad_contextCSN );
-                               if ( csn_a ) {
-                                       *search_context_csn = ber_dupbv( NULL, &csn_a->a_vals[0] );
-                               } else {
-                                       *search_context_csn = NULL;
-                               }
+                               *search_context_csn = NULL;
                        }
                } else {
                        *search_context_csn = NULL;
                }
        }
 
-       return LDAP_SUCCESS;
+       ltid = NULL;
+       rs->sr_err = LDAP_SUCCESS;
+
+done:
+    if( ltid != NULL ) {
+        TXN_ABORT( ltid );
+    }
+
+       return rs->sr_err;
 }