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>
40 EntryInfo **suffix_ei,
46 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
47 struct berval ctxcsn_ndn = { 0, NULL };
48 EntryInfo *ctxcsn_ei = NULL;
50 struct berval *max_committed_csn = NULL;
55 char textbuf[SLAP_TEXT_BUFLEN];
56 size_t textlen = sizeof textbuf;
57 EntryInfo *eip = NULL;
63 build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0],
64 (struct berval *)&slap_ldapsync_cn_bv );
66 rc = bdb_dn2entry( op, tid, &ctxcsn_ndn, &ctxcsn_ei,
67 1, locker, &ctxcsn_lock );
69 *ctxcsn_e = ctxcsn_ei->bei_e;
71 max_committed_csn = slap_get_commit_csn( op );
73 if ( max_committed_csn == NULL ) {
74 return BDB_CSN_COMMIT;
82 rs->sr_err = LDAP_OTHER;
83 rs->sr_text = "context csn not present";
84 ber_bvfree( max_committed_csn );
88 struct berval modvals[2];
90 modvals[0] = *max_committed_csn;
91 modvals[1].bv_val = NULL;
92 modvals[1].bv_len = 0;
94 mod.sml_op = LDAP_MOD_REPLACE;
95 mod.sml_bvalues = modvals;
96 mod.sml_nvalues = NULL;
97 mod.sml_desc = slap_schema.si_ad_contextCSN;
98 mod.sml_type = mod.sml_desc->ad_cname;
101 bdb_cache_entry_db_relock( bdb->bi_dbenv, locker, ctxcsn_ei, 1, 0, &ctxcsn_lock );
103 ret = bdb_modify_internal( op, tid, &mod, *ctxcsn_e,
104 &rs->sr_text, textbuf, textlen );
105 ber_bvfree( max_committed_csn );
106 if ( ret != LDAP_SUCCESS ) {
108 LDAP_LOG ( OPERATION, ERR,
109 "bdb_csn_commit: modify failed (%d)\n", rs->sr_err, 0, 0 );
111 Debug( LDAP_DEBUG_TRACE,
112 "bdb_csn_commit: modify failed (%d)\n", rs->sr_err, 0, 0 );
115 case DB_LOCK_DEADLOCK:
116 case DB_LOCK_NOTGRANTED:
119 return BDB_CSN_ABORT;
123 ret = bdb_id2entry_update( op->o_bd, tid, *ctxcsn_e );
127 case DB_LOCK_DEADLOCK :
128 case DB_LOCK_NOTGRANTED :
132 rs->sr_text = "context csn update failed";
133 return BDB_CSN_ABORT;
138 if ( op->o_tag == LDAP_REQ_ADD &&
139 be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname )) {
141 eip = (EntryInfo *) ch_calloc( 1, sizeof( EntryInfo ));
142 eip->bei_id = op->oq_add.rs_e->e_id;
144 eip = *suffix_ei = ctxcsn_ei;
147 /* This serializes add. But this case is very rare : only once. */
148 rs->sr_err = bdb_next_id( op->o_bd, tid, &ctxcsn_id );
149 if ( rs->sr_err != 0 ) {
151 LDAP_LOG ( OPERATION, ERR,
152 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
154 Debug( LDAP_DEBUG_TRACE,
155 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
157 rs->sr_err = LDAP_OTHER;
158 rs->sr_text = "internal error";
159 return BDB_CSN_ABORT;
162 *ctxcsn_e = slap_create_context_csn_entry( op->o_bd, max_committed_csn );
163 ber_bvfree( max_committed_csn );
164 (*ctxcsn_e)->e_id = ctxcsn_id;
167 ret = bdb_dn2id_add( op, tid, eip, *ctxcsn_e );
171 case DB_LOCK_DEADLOCK :
172 case DB_LOCK_NOTGRANTED :
175 rs->sr_err = LDAP_OTHER;
176 rs->sr_text = "context csn exists before contex prefix does";
177 return BDB_CSN_ABORT;
179 rs->sr_err = LDAP_OTHER;
180 rs->sr_text = "context csn store failed";
181 return BDB_CSN_ABORT;
184 if ( *suffix_ei == NULL ) {
188 ret = bdb_id2entry_add( op->o_bd, tid, *ctxcsn_e );
192 case DB_LOCK_DEADLOCK :
193 case DB_LOCK_NOTGRANTED :
196 rs->sr_err = LDAP_OTHER;
197 rs->sr_text = "context csn store failed";
198 return BDB_CSN_ABORT;
200 ret = bdb_index_entry_add( op, tid, *ctxcsn_e );
204 case DB_LOCK_DEADLOCK :
205 case DB_LOCK_NOTGRANTED :
208 rs->sr_err = LDAP_OTHER;
209 rs->sr_text = "context csn indexing failed";
210 return BDB_CSN_ABORT;
213 case DB_LOCK_DEADLOCK:
214 case DB_LOCK_NOTGRANTED:
216 LDAP_LOG( OPERATION, ERR,
217 "bdb_csn_commit : bdb_dn2entry retry\n", 0, 0, 0 );
219 Debug( LDAP_DEBUG_TRACE,
220 "bdb_csn_commit : bdb_dn2entry retry\n", 0, 0, 0 );
225 rs->sr_text = "ldap server busy";
226 return BDB_CSN_ABORT;
228 rs->sr_err = LDAP_OTHER;
229 rs->sr_text = "internal error";
230 return BDB_CSN_ABORT;
233 return BDB_CSN_COMMIT;
236 slap_rewind_commit_csn( op );
237 return BDB_CSN_RETRY;
244 struct berval **search_context_csn,
249 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
250 struct berval ctxcsn_ndn = BER_BVNULL;
251 struct berval csn = BER_BVNULL;
252 EntryInfo *ctxcsn_ei = NULL;
253 EntryInfo *suffix_ei = NULL;
254 Entry *ctxcsn_e = NULL;
257 char gid[DB_XIDDATASIZE];
258 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
260 int ctxcsn_added = 0;
263 if ( op->o_sync_mode != SLAP_SYNC_NONE ) {
264 if ( op->o_bd->syncinfo ) {
267 sprintf( substr, "cn=syncrepl%d", op->o_bd->syncinfo->id );
268 ber_str2bv( substr, 0, 0, &bv );
269 build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0], &bv );
271 build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0], (struct berval *)&slap_ldapsync_cn_bv );
275 rs->sr_err = bdb_dn2entry( op, NULL, &ctxcsn_ndn, &ctxcsn_ei,
276 0, locker, ctxcsn_lock );
279 ch_free( ctxcsn_ndn.bv_val );
281 ctxcsn_e = ctxcsn_ei->bei_e;
285 ch_free( ctxcsn_ndn.bv_val );
286 LOCK_ID_FREE (bdb->bi_dbenv, locker );
288 case DB_LOCK_DEADLOCK:
289 case DB_LOCK_NOTGRANTED:
292 if ( !op->o_bd->syncinfo ) {
293 snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
294 bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
296 slap_get_csn( op, csnbuf, sizeof(csnbuf), &csn, 1 );
300 rs->sr_err = TXN_ABORT( ltid );
301 if ( rs->sr_err != 0 ) {
302 rs->sr_err = LDAP_OTHER;
306 bdb_trans_backoff( ++num_retries );
307 ldap_pvt_thread_yield();
309 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, <id, bdb->bi_db_opflags );
310 if ( rs->sr_err != 0 ) {
311 rs->sr_err = LDAP_OTHER;
315 rs->sr_err = bdb_csn_commit( op, rs, ltid, NULL, &suffix_ei,
316 &ctxcsn_e, &ctxcsn_added, locker );
317 switch( rs->sr_err ) {
319 LOCK_ID_FREE( bdb->bi_dbenv, locker );
325 rs->sr_err = TXN_PREPARE( ltid, gid );
326 if ( rs->sr_err != 0 ) {
327 rs->sr_err = LDAP_OTHER;
331 bdb_cache_add( bdb, suffix_ei, ctxcsn_e, (struct berval *)&slap_ldapsync_cn_bv, locker );
333 rs->sr_err = TXN_COMMIT( ltid, 0 );
334 if ( rs->sr_err != 0 ) {
335 rs->sr_err = LDAP_OTHER;
340 rs->sr_err = bdb_dn2entry( op, NULL, &ctxcsn_ndn, &ctxcsn_ei,
341 0, locker, ctxcsn_lock );
342 ch_free( ctxcsn_ndn.bv_val );
345 ctxcsn_e = ctxcsn_ei->bei_e;
348 LOCK_ID_FREE( bdb->bi_dbenv, locker );
354 LOCK_ID_FREE (bdb->bi_dbenv, locker );
359 if ( op->o_bd->syncinfo ) {
360 csn_a = attr_find( ctxcsn_e->e_attrs, slap_schema.si_ad_syncreplCookie );
362 csn_a = attr_find( ctxcsn_e->e_attrs, slap_schema.si_ad_contextCSN );
365 *search_context_csn = ber_dupbv( NULL, &csn_a->a_vals[0] );
367 *search_context_csn = NULL;
370 *search_context_csn = NULL;