1 /* ctxcsn.c -- back-bdb Context CSN Management Routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2004 The OpenLDAP Foundation.
6 * Portions Copyright 2003 IBM Corporation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
22 #include <ac/string.h>
35 EntryInfo **suffix_ei,
41 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
42 EntryInfo *ctxcsn_ei = NULL;
44 struct berval max_committed_csn;
49 char textbuf[SLAP_TEXT_BUFLEN];
50 size_t textlen = sizeof textbuf;
51 EntryInfo *eip = NULL;
53 assert( !BER_BVISNULL( &op->o_bd->be_context_csn ) );
59 rc = bdb_dn2entry( op, tid, &op->o_bd->be_context_csn, &ctxcsn_ei,
60 1, locker, &ctxcsn_lock );
62 *ctxcsn_e = ctxcsn_ei->bei_e;
64 slap_get_commit_csn( op, &max_committed_csn );
66 if ( max_committed_csn.bv_val == NULL ) {
67 return BDB_CSN_COMMIT;
75 rs->sr_err = LDAP_OTHER;
76 rs->sr_text = "context csn not present";
77 op->o_tmpfree( max_committed_csn.bv_val, op->o_tmpmemctx );
81 struct berval modvals[2];
84 modvals[0] = max_committed_csn;
85 modvals[1].bv_val = NULL;
86 modvals[1].bv_len = 0;
88 mod.sml_op = LDAP_MOD_REPLACE;
89 mod.sml_values = modvals;
90 mod.sml_nvalues = NULL;
91 mod.sml_desc = slap_schema.si_ad_contextCSN;
92 mod.sml_type = mod.sml_desc->ad_cname;
96 ret = bdb_modify_internal( op, tid, &mod, &dummy,
97 &rs->sr_text, textbuf, textlen );
98 op->o_tmpfree( max_committed_csn.bv_val, op->o_tmpmemctx );
99 if ( ret != LDAP_SUCCESS ) {
101 LDAP_LOG ( OPERATION, ERR,
102 "bdb_csn_commit: modify failed (%d)\n", rs->sr_err, 0, 0 );
104 Debug( LDAP_DEBUG_TRACE,
105 "bdb_csn_commit: modify failed (%d)\n", rs->sr_err, 0, 0 );
108 case DB_LOCK_DEADLOCK:
109 case DB_LOCK_NOTGRANTED:
112 return BDB_CSN_ABORT;
116 ret = bdb_id2entry_update( op->o_bd, tid, &dummy );
120 case DB_LOCK_DEADLOCK :
121 case DB_LOCK_NOTGRANTED :
125 rs->sr_text = "context csn update failed";
126 return BDB_CSN_ABORT;
128 bdb_cache_modify( *ctxcsn_e, dummy.e_attrs, bdb->bi_dbenv, locker, &ctxcsn_lock );
132 if ( op->o_tag == LDAP_REQ_ADD &&
133 be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname )) {
135 eip = (EntryInfo *) ch_calloc( 1, sizeof( EntryInfo ));
136 eip->bei_id = op->oq_add.rs_e->e_id;
138 eip = *suffix_ei = ctxcsn_ei;
141 /* This serializes add. But this case is very rare : only once. */
142 rs->sr_err = bdb_next_id( op->o_bd, tid, &ctxcsn_id );
143 if ( rs->sr_err != 0 ) {
145 LDAP_LOG ( OPERATION, ERR,
146 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
148 Debug( LDAP_DEBUG_TRACE,
149 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
151 rs->sr_err = LDAP_OTHER;
152 rs->sr_text = "internal error";
153 return BDB_CSN_ABORT;
156 *ctxcsn_e = slap_create_context_csn_entry( op->o_bd, &max_committed_csn );
157 op->o_tmpfree( max_committed_csn.bv_val, op->o_tmpmemctx );
158 (*ctxcsn_e)->e_id = ctxcsn_id;
161 ret = bdb_dn2id_add( op, tid, eip, *ctxcsn_e );
165 case DB_LOCK_DEADLOCK :
166 case DB_LOCK_NOTGRANTED :
169 rs->sr_err = LDAP_OTHER;
170 rs->sr_text = "context csn exists before context prefix does";
171 return BDB_CSN_ABORT;
173 rs->sr_err = LDAP_OTHER;
174 rs->sr_text = "context csn store failed";
175 return BDB_CSN_ABORT;
178 if ( *suffix_ei == NULL ) {
182 ret = bdb_id2entry_add( op->o_bd, tid, *ctxcsn_e );
186 case DB_LOCK_DEADLOCK :
187 case DB_LOCK_NOTGRANTED :
190 rs->sr_err = LDAP_OTHER;
191 rs->sr_text = "context csn store failed";
192 return BDB_CSN_ABORT;
194 ret = bdb_index_entry_add( op, tid, *ctxcsn_e );
198 case DB_LOCK_DEADLOCK :
199 case DB_LOCK_NOTGRANTED :
202 rs->sr_err = LDAP_OTHER;
203 rs->sr_text = "context csn indexing failed";
204 return BDB_CSN_ABORT;
207 case DB_LOCK_DEADLOCK:
208 case DB_LOCK_NOTGRANTED:
210 LDAP_LOG( OPERATION, ERR,
211 "bdb_csn_commit : bdb_dn2entry retry\n", 0, 0, 0 );
213 Debug( LDAP_DEBUG_TRACE,
214 "bdb_csn_commit : bdb_dn2entry retry\n", 0, 0, 0 );
219 rs->sr_text = "ldap server busy";
220 return BDB_CSN_ABORT;
222 rs->sr_err = LDAP_OTHER;
223 rs->sr_text = "internal error";
224 return BDB_CSN_ABORT;
227 return BDB_CSN_COMMIT;
230 slap_rewind_commit_csn( op );
231 return BDB_CSN_RETRY;
238 struct berval **search_context_csn,
243 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
244 struct berval csn = BER_BVNULL;
245 EntryInfo *ctxcsn_ei = NULL;
246 EntryInfo *suffix_ei = NULL;
247 Entry *ctxcsn_e = NULL;
250 char gid[DB_XIDDATASIZE];
251 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
253 int ctxcsn_added = 0;
255 struct sync_cookie syncCookie = { NULL, -1, NULL};
257 u_int32_t ctxcsn_locker = 0;
259 if ( op->o_sync_mode != SLAP_SYNC_NONE &&
260 !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
262 struct berval ctxcsn_ndn = BER_BVNULL;
265 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
266 sprintf( substr, "cn=syncrepl%ld", si->si_rid );
267 ber_str2bv( substr, 0, 0, &bv );
268 build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0], &bv, op->o_tmpmemctx );
270 consumer_ctxcsn_retry :
271 rs->sr_err = bdb_dn2entry( op, NULL, &ctxcsn_ndn, &ctxcsn_ei,
272 0, locker, ctxcsn_lock );
274 case DB_LOCK_DEADLOCK:
275 case DB_LOCK_NOTGRANTED:
276 goto consumer_ctxcsn_retry;
278 op->o_tmpfree( ctxcsn_ndn.bv_val, op->o_tmpmemctx );
279 ctxcsn_ndn.bv_val = NULL;
281 ctxcsn_e = ctxcsn_ei->bei_e;
286 rs->sr_err = LDAP_OTHER;
288 op->o_tmpfree( ctxcsn_ndn.bv_val, op->o_tmpmemctx );
289 ctxcsn_ndn.bv_val = NULL;
294 csn_a = attr_find( ctxcsn_e->e_attrs,
295 slap_schema.si_ad_syncreplCookie );
297 struct berval cookie;
300 ber_dupbv( &cookie, &csn_a->a_vals[0] );
301 ber_bvarray_add( &syncCookie.octet_str, &cookie );
302 slap_parse_sync_cookie( &syncCookie );
303 if ( *search_context_csn &&
304 (*search_context_csn)->bv_val != NULL )
306 value_match( &match, slap_schema.si_ad_entryCSN,
307 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
308 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
309 syncCookie.ctxcsn, *search_context_csn, &text );
312 /* set search_context_csn to the
313 smallest syncrepl cookie value */
314 if ( *search_context_csn ) {
315 ch_free( (*search_context_csn)->bv_val );
316 ch_free( *search_context_csn );
318 *search_context_csn = ber_dupbv( NULL,
321 slap_sync_cookie_free( &syncCookie, 0 );
323 *search_context_csn = NULL;
326 *search_context_csn = NULL;
329 } else if ( op->o_sync_mode != SLAP_SYNC_NONE &&
330 LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
332 provider_ctxcsn_retry :
333 rs->sr_err = bdb_dn2entry( op, NULL, &op->o_bd->be_context_csn, &ctxcsn_ei,
334 0, locker, ctxcsn_lock );
338 ctxcsn_e = ctxcsn_ei->bei_e;
343 case DB_LOCK_DEADLOCK:
344 case DB_LOCK_NOTGRANTED:
345 goto provider_ctxcsn_retry;
347 snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
348 bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
350 slap_get_csn( op, csnbuf, sizeof(csnbuf), &csn, 1 );
354 rs->sr_err = TXN_ABORT( ltid );
356 if ( rs->sr_err != 0 ) {
357 rs->sr_err = LDAP_OTHER;
360 ldap_pvt_thread_yield();
361 bdb_trans_backoff( ++num_retries );
363 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL,
364 <id, bdb->bi_db_opflags );
365 if ( rs->sr_err != 0 ) {
366 rs->sr_err = LDAP_OTHER;
370 ctxcsn_locker = TXN_ID ( ltid );
372 rs->sr_err = bdb_csn_commit( op, rs, ltid, NULL, &suffix_ei,
373 &ctxcsn_e, &ctxcsn_added, ctxcsn_locker );
374 switch( rs->sr_err ) {
376 rs->sr_err = LDAP_OTHER;
382 rs->sr_err = TXN_PREPARE( ltid, gid );
383 if ( rs->sr_err != 0 ) {
384 rs->sr_err = LDAP_OTHER;
388 bdb_cache_add( bdb, suffix_ei, ctxcsn_e,
389 (struct berval *)&slap_ldapsync_cn_bv, ctxcsn_locker );
391 rs->sr_err = TXN_COMMIT( ltid, 0 );
392 if ( rs->sr_err != 0 ) {
393 rs->sr_err = LDAP_OTHER;
397 rs->sr_err = bdb_dn2entry( op, NULL, &op->o_bd->be_context_csn, &ctxcsn_ei,
398 0, ctxcsn_locker, ctxcsn_lock );
401 ctxcsn_e = ctxcsn_ei->bei_e;
406 rs->sr_err = LDAP_OTHER;
411 csn_a = attr_find( ctxcsn_e->e_attrs,
412 slap_schema.si_ad_contextCSN );
414 *search_context_csn = ber_dupbv( NULL, &csn_a->a_vals[0] );
416 *search_context_csn = NULL;
419 *search_context_csn = NULL;
424 rs->sr_err = LDAP_SUCCESS;