3 * back-bdb Context CSN Management Routines
5 /* Copyright (c) 2003 by International Business Machines, Inc.
7 * International Business Machines, Inc. (hereinafter called IBM) grants
8 * permission under its copyrights to use, copy, modify, and distribute this
9 * Software with or without fee, provided that the above copyright notice and
10 * all paragraphs of this notice appear in all copies, and that the name of IBM
11 * not be used in connection with the marketing of any product incorporating
12 * the Software or modifications thereof, without specific, written prior
15 * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
17 * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
18 * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
19 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
20 * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
27 #include <ac/string.h>
39 EntryInfo **suffix_ei,
45 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
46 struct berval ctxcsn_rdn = { 0, NULL };
47 struct berval ctxcsn_ndn = { 0, NULL };
48 EntryInfo *ctxcsn_ei = NULL;
50 struct berval *max_committed_csn = NULL;
60 ber_str2bv( "cn=ldapsync", strlen("cn=ldapsync"), 0, &ctxcsn_rdn );
61 build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0], &ctxcsn_rdn );
63 rc = bdb_dn2entry( op, tid, &ctxcsn_ndn, &ctxcsn_ei,
64 0, locker, &ctxcsn_lock );
67 *ctxcsn_e = ctxcsn_ei->bei_e;
68 bdb_cache_entry_db_relock( bdb->bi_dbenv, locker, ctxcsn_ei, 1, 0, &ctxcsn_lock );
71 max_committed_csn = slap_get_commit_csn( op );
73 if ( max_committed_csn == NULL )
74 return BDB_CSN_COMMIT;
81 rs->sr_err = LDAP_OTHER;
82 rs->sr_text = "context csn not present";
83 ber_bvfree( max_committed_csn );
86 attr_delete( &(*ctxcsn_e)->e_attrs, slap_schema.si_ad_contextCSN );
87 attr_merge_normalize_one( *ctxcsn_e, slap_schema.si_ad_contextCSN,
88 max_committed_csn, NULL );
89 ret = bdb_id2entry_update( op->o_bd, tid, *ctxcsn_e );
93 case DB_LOCK_DEADLOCK :
94 case DB_LOCK_NOTGRANTED :
98 rs->sr_text = "context csn update failed";
101 ret = bdb_index_entry_add( op, tid, *ctxcsn_e );
105 case DB_LOCK_DEADLOCK :
106 case DB_LOCK_NOTGRANTED :
109 rs->sr_err = LDAP_OTHER;
110 rs->sr_text = "context csn indexing failed";
111 return BDB_CSN_ABORT;
116 if ( op->o_tag == LDAP_REQ_ADD && !be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname )) {
117 rc = bdb_dn2entry( op, tid, &op->o_bd->be_nsuffix[0], suffix_ei,
118 0, locker, &suffix_lock );
119 } else if ( op->o_tag != LDAP_REQ_ADD && !be_issuffix( op->o_bd, &e->e_nname )) {
120 rc = bdb_dn2entry( op, tid, &op->o_bd->be_nsuffix[0], suffix_ei,
121 0, locker, &suffix_lock );
126 /* This serializes add. But this case is very rare : only once. */
127 rs->sr_err = bdb_next_id( op->o_bd, tid, &ctxcsn_id );
128 if ( rs->sr_err != 0 ) {
130 LDAP_LOG ( OPERATION, ERR,
131 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
133 Debug( LDAP_DEBUG_TRACE,
134 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
136 rs->sr_err = LDAP_OTHER;
137 rs->sr_text = "internal error";
138 return BDB_CSN_ABORT;
141 *ctxcsn_e = slap_create_context_csn_entry( op->o_bd, max_committed_csn );
142 ber_bvfree( max_committed_csn );
143 (*ctxcsn_e)->e_id = ctxcsn_id;
145 ret = bdb_dn2id_add( op, tid, *suffix_ei, *ctxcsn_e );
149 case DB_LOCK_DEADLOCK :
150 case DB_LOCK_NOTGRANTED :
153 rs->sr_err = LDAP_OTHER;
154 rs->sr_text = "context csn exists before contex prefix does";
155 return BDB_CSN_ABORT;
157 rs->sr_err = LDAP_OTHER;
158 rs->sr_text = "context csn store failed";
159 return BDB_CSN_ABORT;
161 ret = bdb_id2entry_add( op->o_bd, tid, *ctxcsn_e );
165 case DB_LOCK_DEADLOCK :
166 case DB_LOCK_NOTGRANTED :
169 rs->sr_err = LDAP_OTHER;
170 rs->sr_text = "context csn store failed";
171 return BDB_CSN_ABORT;
173 ret = bdb_index_entry_add( op, tid, *ctxcsn_e );
177 case DB_LOCK_DEADLOCK :
178 case DB_LOCK_NOTGRANTED :
181 rs->sr_err = LDAP_OTHER;
182 rs->sr_text = "context csn indexing failed";
183 return BDB_CSN_ABORT;
186 case DB_LOCK_DEADLOCK:
187 case DB_LOCK_NOTGRANTED:
191 rs->sr_text = "ldap server busy";
192 return BDB_CSN_ABORT;
194 rs->sr_err = LDAP_OTHER;
195 rs->sr_text = "internal error";
196 return BDB_CSN_ABORT;
199 return BDB_CSN_COMMIT;
202 slap_rewind_commit_csn( op );
203 return BDB_CSN_RETRY;