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};
263 if ( op->o_sync_mode != SLAP_SYNC_NONE &&
264 !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
268 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
269 sprintf( substr, "cn=syncrepl%ld", si->si_rid );
270 ber_str2bv( substr, 0, 0, &bv );
271 build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0], &bv, NULL );
273 consumer_ctxcsn_retry :
274 rs->sr_err = bdb_dn2entry( op, NULL, &ctxcsn_ndn, &ctxcsn_ei,
275 0, locker, ctxcsn_lock );
278 ch_free( ctxcsn_ndn.bv_val );
279 ctxcsn_ndn.bv_val = NULL;
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:
290 goto consumer_ctxcsn_retry;
292 ch_free( ctxcsn_ndn.bv_val );
293 LOCK_ID_FREE( bdb->bi_dbenv, locker );
296 ch_free( ctxcsn_ndn.bv_val );
297 ctxcsn_ndn.bv_val = NULL;
298 LOCK_ID_FREE (bdb->bi_dbenv, locker );
303 csn_a = attr_find( ctxcsn_e->e_attrs,
304 slap_schema.si_ad_syncreplCookie );
306 struct berval cookie;
309 ber_dupbv( &cookie, &csn_a->a_vals[0] );
310 ber_bvarray_add( &syncCookie.octet_str, &cookie );
311 slap_parse_sync_cookie( &syncCookie );
312 if ( *search_context_csn &&
313 (*search_context_csn)->bv_val != NULL )
315 value_match( &match, slap_schema.si_ad_entryCSN,
316 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
317 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
318 syncCookie.ctxcsn, *search_context_csn, &text );
321 /* set search_context_csn to the
322 smallest syncrepl cookie value */
323 if ( *search_context_csn ) {
324 ch_free( (*search_context_csn)->bv_val );
325 ch_free( *search_context_csn );
327 *search_context_csn = ber_dupbv( NULL,
330 slap_sync_cookie_free( &syncCookie, 0 );
332 *search_context_csn = NULL;
335 *search_context_csn = NULL;
338 } else if ( op->o_sync_mode != SLAP_SYNC_NONE &&
339 LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
340 build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0],
341 (struct berval *)&slap_ldapsync_cn_bv, NULL );
343 provider_ctxcsn_retry :
344 rs->sr_err = bdb_dn2entry( op, NULL, &ctxcsn_ndn, &ctxcsn_ei,
345 0, locker, ctxcsn_lock );
348 ch_free( ctxcsn_ndn.bv_val );
350 ctxcsn_e = ctxcsn_ei->bei_e;
354 ch_free( ctxcsn_ndn.bv_val );
355 LOCK_ID_FREE (bdb->bi_dbenv, locker );
357 case DB_LOCK_DEADLOCK:
358 case DB_LOCK_NOTGRANTED:
359 goto provider_ctxcsn_retry;
361 snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
362 bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
364 slap_get_csn( op, csnbuf, sizeof(csnbuf), &csn, 1 );
368 rs->sr_err = TXN_ABORT( ltid );
369 if ( rs->sr_err != 0 ) {
370 rs->sr_err = LDAP_OTHER;
373 ldap_pvt_thread_yield();
374 bdb_trans_backoff( ++num_retries );
376 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL,
377 <id, bdb->bi_db_opflags );
378 if ( rs->sr_err != 0 ) {
379 rs->sr_err = LDAP_OTHER;
383 rs->sr_err = bdb_csn_commit( op, rs, ltid, NULL, &suffix_ei,
384 &ctxcsn_e, &ctxcsn_added, locker );
385 switch( rs->sr_err ) {
387 LOCK_ID_FREE( bdb->bi_dbenv, locker );
393 rs->sr_err = TXN_PREPARE( ltid, gid );
394 if ( rs->sr_err != 0 ) {
395 rs->sr_err = LDAP_OTHER;
399 bdb_cache_add( bdb, suffix_ei, ctxcsn_e,
400 (struct berval *)&slap_ldapsync_cn_bv, locker );
402 rs->sr_err = TXN_COMMIT( ltid, 0 );
403 if ( rs->sr_err != 0 ) {
404 rs->sr_err = LDAP_OTHER;
408 rs->sr_err = bdb_dn2entry( op, NULL, &ctxcsn_ndn, &ctxcsn_ei,
409 0, locker, ctxcsn_lock );
410 ch_free( ctxcsn_ndn.bv_val );
413 ctxcsn_e = ctxcsn_ei->bei_e;
418 ch_free( ctxcsn_ndn.bv_val );
419 LOCK_ID_FREE (bdb->bi_dbenv, locker );
424 csn_a = attr_find( ctxcsn_e->e_attrs,
425 slap_schema.si_ad_contextCSN );
427 *search_context_csn = ber_dupbv( NULL, &csn_a->a_vals[0] );
429 *search_context_csn = NULL;
432 *search_context_csn = NULL;