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 struct berval ctxcsn_ndn = BER_BVNULL;
43 EntryInfo *ctxcsn_ei = NULL;
45 struct berval max_committed_csn;
50 char textbuf[SLAP_TEXT_BUFLEN];
51 size_t textlen = sizeof textbuf;
52 EntryInfo *eip = NULL;
58 build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0],
59 (struct berval *)&slap_ldapsync_cn_bv, op->o_tmpmemctx );
61 rc = bdb_dn2entry( op, tid, &ctxcsn_ndn, &ctxcsn_ei,
62 1, locker, &ctxcsn_lock );
64 *ctxcsn_e = ctxcsn_ei->bei_e;
66 op->o_tmpfree( ctxcsn_ndn.bv_val, op->o_tmpmemctx );
68 slap_get_commit_csn( op, &max_committed_csn );
70 if ( max_committed_csn.bv_val == NULL ) {
71 return BDB_CSN_COMMIT;
79 rs->sr_err = LDAP_OTHER;
80 rs->sr_text = "context csn not present";
81 ch_free( max_committed_csn.bv_val );
85 struct berval modvals[2];
88 modvals[0] = max_committed_csn;
89 modvals[1].bv_val = NULL;
90 modvals[1].bv_len = 0;
92 mod.sml_op = LDAP_MOD_REPLACE;
93 mod.sml_values = modvals;
94 mod.sml_nvalues = NULL;
95 mod.sml_desc = slap_schema.si_ad_contextCSN;
96 mod.sml_type = mod.sml_desc->ad_cname;
100 ret = bdb_modify_internal( op, tid, &mod, &dummy,
101 &rs->sr_text, textbuf, textlen );
102 ch_free( max_committed_csn.bv_val );
103 if ( ret != LDAP_SUCCESS ) {
105 LDAP_LOG ( OPERATION, ERR,
106 "bdb_csn_commit: modify failed (%d)\n", rs->sr_err, 0, 0 );
108 Debug( LDAP_DEBUG_TRACE,
109 "bdb_csn_commit: modify failed (%d)\n", rs->sr_err, 0, 0 );
112 case DB_LOCK_DEADLOCK:
113 case DB_LOCK_NOTGRANTED:
116 return BDB_CSN_ABORT;
120 ret = bdb_id2entry_update( op->o_bd, tid, &dummy );
124 case DB_LOCK_DEADLOCK :
125 case DB_LOCK_NOTGRANTED :
129 rs->sr_text = "context csn update failed";
130 return BDB_CSN_ABORT;
132 bdb_cache_modify( *ctxcsn_e, dummy.e_attrs, bdb->bi_dbenv, locker, &ctxcsn_lock );
136 if ( op->o_tag == LDAP_REQ_ADD &&
137 be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname )) {
139 eip = (EntryInfo *) ch_calloc( 1, sizeof( EntryInfo ));
140 eip->bei_id = op->oq_add.rs_e->e_id;
142 eip = *suffix_ei = ctxcsn_ei;
145 /* This serializes add. But this case is very rare : only once. */
146 rs->sr_err = bdb_next_id( op->o_bd, tid, &ctxcsn_id );
147 if ( rs->sr_err != 0 ) {
149 LDAP_LOG ( OPERATION, ERR,
150 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
152 Debug( LDAP_DEBUG_TRACE,
153 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
155 rs->sr_err = LDAP_OTHER;
156 rs->sr_text = "internal error";
157 return BDB_CSN_ABORT;
160 *ctxcsn_e = slap_create_context_csn_entry( op->o_bd, &max_committed_csn );
161 ch_free( max_committed_csn.bv_val );
162 (*ctxcsn_e)->e_id = ctxcsn_id;
165 ret = bdb_dn2id_add( op, tid, eip, *ctxcsn_e );
169 case DB_LOCK_DEADLOCK :
170 case DB_LOCK_NOTGRANTED :
173 rs->sr_err = LDAP_OTHER;
174 rs->sr_text = "context csn exists before context prefix does";
175 return BDB_CSN_ABORT;
177 rs->sr_err = LDAP_OTHER;
178 rs->sr_text = "context csn store failed";
179 return BDB_CSN_ABORT;
182 if ( *suffix_ei == NULL ) {
186 ret = bdb_id2entry_add( op->o_bd, tid, *ctxcsn_e );
190 case DB_LOCK_DEADLOCK :
191 case DB_LOCK_NOTGRANTED :
194 rs->sr_err = LDAP_OTHER;
195 rs->sr_text = "context csn store failed";
196 return BDB_CSN_ABORT;
198 ret = bdb_index_entry_add( op, tid, *ctxcsn_e );
202 case DB_LOCK_DEADLOCK :
203 case DB_LOCK_NOTGRANTED :
206 rs->sr_err = LDAP_OTHER;
207 rs->sr_text = "context csn indexing failed";
208 return BDB_CSN_ABORT;
211 case DB_LOCK_DEADLOCK:
212 case DB_LOCK_NOTGRANTED:
214 LDAP_LOG( OPERATION, ERR,
215 "bdb_csn_commit : bdb_dn2entry retry\n", 0, 0, 0 );
217 Debug( LDAP_DEBUG_TRACE,
218 "bdb_csn_commit : bdb_dn2entry retry\n", 0, 0, 0 );
223 rs->sr_text = "ldap server busy";
224 return BDB_CSN_ABORT;
226 rs->sr_err = LDAP_OTHER;
227 rs->sr_text = "internal error";
228 return BDB_CSN_ABORT;
231 return BDB_CSN_COMMIT;
234 slap_rewind_commit_csn( op );
235 return BDB_CSN_RETRY;
242 struct berval **search_context_csn,
247 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
248 struct berval ctxcsn_ndn = BER_BVNULL;
249 struct berval csn = BER_BVNULL;
250 EntryInfo *ctxcsn_ei = NULL;
251 EntryInfo *suffix_ei = NULL;
252 Entry *ctxcsn_e = NULL;
255 char gid[DB_XIDDATASIZE];
256 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
258 int ctxcsn_added = 0;
260 struct sync_cookie syncCookie = { NULL, -1, NULL};
262 u_int32_t ctxcsn_locker = 0;
264 if ( op->o_sync_mode != SLAP_SYNC_NONE &&
265 !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
269 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
270 sprintf( substr, "cn=syncrepl%ld", si->si_rid );
271 ber_str2bv( substr, 0, 0, &bv );
272 build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0], &bv, NULL );
274 consumer_ctxcsn_retry :
275 rs->sr_err = bdb_dn2entry( op, NULL, &ctxcsn_ndn, &ctxcsn_ei,
276 0, locker, ctxcsn_lock );
279 ch_free( ctxcsn_ndn.bv_val );
280 ctxcsn_ndn.bv_val = NULL;
282 ctxcsn_e = ctxcsn_ei->bei_e;
287 case DB_LOCK_DEADLOCK:
288 case DB_LOCK_NOTGRANTED:
289 goto consumer_ctxcsn_retry;
291 rs->sr_err = LDAP_OTHER;
294 rs->sr_err = LDAP_OTHER;
299 csn_a = attr_find( ctxcsn_e->e_attrs,
300 slap_schema.si_ad_syncreplCookie );
302 struct berval cookie;
305 ber_dupbv( &cookie, &csn_a->a_vals[0] );
306 ber_bvarray_add( &syncCookie.octet_str, &cookie );
307 slap_parse_sync_cookie( &syncCookie );
308 if ( *search_context_csn &&
309 (*search_context_csn)->bv_val != NULL )
311 value_match( &match, slap_schema.si_ad_entryCSN,
312 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
313 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
314 syncCookie.ctxcsn, *search_context_csn, &text );
317 /* set search_context_csn to the
318 smallest syncrepl cookie value */
319 if ( *search_context_csn ) {
320 ch_free( (*search_context_csn)->bv_val );
321 ch_free( *search_context_csn );
323 *search_context_csn = ber_dupbv( NULL,
326 slap_sync_cookie_free( &syncCookie, 0 );
328 *search_context_csn = NULL;
331 *search_context_csn = NULL;
334 } else if ( op->o_sync_mode != SLAP_SYNC_NONE &&
335 LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
336 build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0],
337 (struct berval *)&slap_ldapsync_cn_bv, NULL );
339 provider_ctxcsn_retry :
340 rs->sr_err = bdb_dn2entry( op, NULL, &ctxcsn_ndn, &ctxcsn_ei,
341 0, locker, ctxcsn_lock );
345 ctxcsn_e = ctxcsn_ei->bei_e;
350 case DB_LOCK_DEADLOCK:
351 case DB_LOCK_NOTGRANTED:
352 goto provider_ctxcsn_retry;
354 snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
355 bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
357 slap_get_csn( op, csnbuf, sizeof(csnbuf), &csn, 1 );
361 rs->sr_err = TXN_ABORT( ltid );
363 if ( rs->sr_err != 0 ) {
364 rs->sr_err = LDAP_OTHER;
367 ldap_pvt_thread_yield();
368 bdb_trans_backoff( ++num_retries );
370 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL,
371 <id, bdb->bi_db_opflags );
372 if ( rs->sr_err != 0 ) {
373 rs->sr_err = LDAP_OTHER;
377 ctxcsn_locker = TXN_ID ( ltid );
379 rs->sr_err = bdb_csn_commit( op, rs, ltid, NULL, &suffix_ei,
380 &ctxcsn_e, &ctxcsn_added, ctxcsn_locker );
381 switch( rs->sr_err ) {
383 rs->sr_err = LDAP_OTHER;
389 rs->sr_err = TXN_PREPARE( ltid, gid );
390 if ( rs->sr_err != 0 ) {
391 rs->sr_err = LDAP_OTHER;
395 bdb_cache_add( bdb, suffix_ei, ctxcsn_e,
396 (struct berval *)&slap_ldapsync_cn_bv, ctxcsn_locker );
398 rs->sr_err = TXN_COMMIT( ltid, 0 );
399 if ( rs->sr_err != 0 ) {
400 rs->sr_err = LDAP_OTHER;
404 rs->sr_err = bdb_dn2entry( op, NULL, &ctxcsn_ndn, &ctxcsn_ei,
405 0, ctxcsn_locker, ctxcsn_lock );
406 ch_free( ctxcsn_ndn.bv_val );
407 ctxcsn_ndn.bv_val = NULL;
410 ctxcsn_e = ctxcsn_ei->bei_e;
415 rs->sr_err = LDAP_OTHER;
420 csn_a = attr_find( ctxcsn_e->e_attrs,
421 slap_schema.si_ad_contextCSN );
423 *search_context_csn = ber_dupbv( NULL, &csn_a->a_vals[0] );
425 *search_context_csn = NULL;
428 *search_context_csn = NULL;
433 rs->sr_err = LDAP_SUCCESS;
440 if ( ctxcsn_ndn.bv_val != NULL )
441 ch_free( ctxcsn_ndn.bv_val );