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 );
65 case DB_LOCK_DEADLOCK:
66 case DB_LOCK_NOTGRANTED:
72 *ctxcsn_e = ctxcsn_ei->bei_e;
74 slap_get_commit_csn( op, &max_committed_csn );
76 if ( max_committed_csn.bv_val == NULL ) {
77 return BDB_CSN_COMMIT;
85 rs->sr_err = LDAP_OTHER;
86 rs->sr_text = "context csn not present";
87 op->o_tmpfree( max_committed_csn.bv_val, op->o_tmpmemctx );
91 struct berval modvals[2];
94 modvals[0] = max_committed_csn;
95 modvals[1].bv_val = NULL;
96 modvals[1].bv_len = 0;
98 mod.sml_op = LDAP_MOD_REPLACE;
99 mod.sml_values = modvals;
100 mod.sml_nvalues = NULL;
101 mod.sml_desc = slap_schema.si_ad_contextCSN;
102 mod.sml_type = mod.sml_desc->ad_cname;
106 ret = bdb_modify_internal( op, tid, &mod, &dummy,
107 &rs->sr_text, textbuf, textlen );
108 op->o_tmpfree( max_committed_csn.bv_val, op->o_tmpmemctx );
109 if ( ret != LDAP_SUCCESS ) {
110 Debug( LDAP_DEBUG_TRACE,
111 "bdb_csn_commit: modify failed (%d)\n", rs->sr_err, 0, 0 );
113 case DB_LOCK_DEADLOCK:
114 case DB_LOCK_NOTGRANTED:
117 return BDB_CSN_ABORT;
121 ret = bdb_id2entry_update( op->o_bd, tid, &dummy );
125 case DB_LOCK_DEADLOCK :
126 case DB_LOCK_NOTGRANTED :
130 rs->sr_text = "context csn update failed";
131 return BDB_CSN_ABORT;
133 bdb_cache_modify( *ctxcsn_e, dummy.e_attrs, bdb->bi_dbenv, locker, &ctxcsn_lock );
137 if ( op->o_tag == LDAP_REQ_ADD &&
138 be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname )) {
140 eip = (EntryInfo *) ch_calloc( 1, sizeof( EntryInfo ));
141 eip->bei_id = op->oq_add.rs_e->e_id;
143 eip = *suffix_ei = ctxcsn_ei;
146 /* This serializes add. But this case is very rare : only once. */
147 rs->sr_err = bdb_next_id( op->o_bd, tid, &ctxcsn_id );
148 if ( rs->sr_err != 0 ) {
149 Debug( LDAP_DEBUG_TRACE,
150 "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:
209 Debug( LDAP_DEBUG_TRACE,
210 "bdb_csn_commit : bdb_dn2entry retry\n", 0, 0, 0 );
214 rs->sr_text = "ldap server busy";
215 return BDB_CSN_ABORT;
217 rs->sr_err = LDAP_OTHER;
218 rs->sr_text = "internal error";
219 return BDB_CSN_ABORT;
222 return BDB_CSN_COMMIT;
225 slap_rewind_commit_csn( op );
226 return BDB_CSN_RETRY;
233 struct berval **search_context_csn,
238 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
239 struct berval csn = BER_BVNULL;
240 EntryInfo *ctxcsn_ei = NULL;
241 EntryInfo *suffix_ei = NULL;
242 Entry *ctxcsn_e = NULL;
245 char gid[DB_XIDDATASIZE];
246 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
248 int ctxcsn_added = 0;
250 struct sync_cookie syncCookie = { NULL, -1, NULL};
252 u_int32_t ctxcsn_locker = 0;
254 if ( op->o_sync_mode != SLAP_SYNC_NONE &&
255 !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
257 struct berval ctxcsn_ndn = BER_BVNULL;
260 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
261 sprintf( substr, "cn=syncrepl%ld", si->si_rid );
262 ber_str2bv( substr, 0, 0, &bv );
263 build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0], &bv, op->o_tmpmemctx );
265 consumer_ctxcsn_retry :
266 rs->sr_err = bdb_dn2entry( op, NULL, &ctxcsn_ndn, &ctxcsn_ei,
267 0, locker, ctxcsn_lock );
269 case DB_LOCK_DEADLOCK:
270 case DB_LOCK_NOTGRANTED:
271 goto consumer_ctxcsn_retry;
273 op->o_tmpfree( ctxcsn_ndn.bv_val, op->o_tmpmemctx );
274 ctxcsn_ndn.bv_val = NULL;
276 ctxcsn_e = ctxcsn_ei->bei_e;
281 rs->sr_err = LDAP_OTHER;
283 op->o_tmpfree( ctxcsn_ndn.bv_val, op->o_tmpmemctx );
284 ctxcsn_ndn.bv_val = NULL;
289 csn_a = attr_find( ctxcsn_e->e_attrs,
290 slap_schema.si_ad_syncreplCookie );
292 struct berval cookie;
295 ber_dupbv( &cookie, &csn_a->a_vals[0] );
296 ber_bvarray_add( &syncCookie.octet_str, &cookie );
297 slap_parse_sync_cookie( &syncCookie );
298 if ( *search_context_csn &&
299 (*search_context_csn)->bv_val != NULL )
301 value_match( &match, slap_schema.si_ad_entryCSN,
302 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
303 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
304 syncCookie.ctxcsn, *search_context_csn, &text );
307 /* set search_context_csn to the
308 smallest syncrepl cookie value */
309 if ( *search_context_csn ) {
310 ch_free( (*search_context_csn)->bv_val );
311 ch_free( *search_context_csn );
313 *search_context_csn = ber_dupbv( NULL,
316 slap_sync_cookie_free( &syncCookie, 0 );
318 *search_context_csn = NULL;
321 *search_context_csn = NULL;
324 } else if ( op->o_sync_mode != SLAP_SYNC_NONE &&
325 LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
327 provider_ctxcsn_retry :
328 rs->sr_err = bdb_dn2entry( op, NULL, &op->o_bd->be_context_csn, &ctxcsn_ei,
329 0, locker, ctxcsn_lock );
333 ctxcsn_e = ctxcsn_ei->bei_e;
338 case DB_LOCK_DEADLOCK:
339 case DB_LOCK_NOTGRANTED:
340 goto provider_ctxcsn_retry;
342 snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
343 bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
345 slap_get_csn( op, csnbuf, sizeof(csnbuf), &csn, 1 );
349 rs->sr_err = TXN_ABORT( ltid );
351 if ( rs->sr_err != 0 ) {
352 rs->sr_err = LDAP_OTHER;
355 ldap_pvt_thread_yield();
356 bdb_trans_backoff( ++num_retries );
358 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL,
359 <id, bdb->bi_db_opflags );
360 if ( rs->sr_err != 0 ) {
361 rs->sr_err = LDAP_OTHER;
365 ctxcsn_locker = TXN_ID ( ltid );
367 rs->sr_err = bdb_csn_commit( op, rs, ltid, NULL, &suffix_ei,
368 &ctxcsn_e, &ctxcsn_added, ctxcsn_locker );
369 switch( rs->sr_err ) {
371 rs->sr_err = LDAP_OTHER;
377 rs->sr_err = TXN_PREPARE( ltid, gid );
378 if ( rs->sr_err != 0 ) {
379 rs->sr_err = LDAP_OTHER;
383 bdb_cache_add( bdb, suffix_ei, ctxcsn_e,
384 (struct berval *)&slap_ldapsync_cn_bv, ctxcsn_locker );
386 rs->sr_err = TXN_COMMIT( ltid, 0 );
387 if ( rs->sr_err != 0 ) {
388 rs->sr_err = LDAP_OTHER;
392 rs->sr_err = bdb_dn2entry( op, NULL, &op->o_bd->be_context_csn, &ctxcsn_ei,
393 0, ctxcsn_locker, ctxcsn_lock );
396 ctxcsn_e = ctxcsn_ei->bei_e;
401 rs->sr_err = LDAP_OTHER;
406 csn_a = attr_find( ctxcsn_e->e_attrs,
407 slap_schema.si_ad_contextCSN );
409 *search_context_csn = ber_dupbv( NULL, &csn_a->a_vals[0] );
411 *search_context_csn = NULL;
414 *search_context_csn = NULL;
419 rs->sr_err = LDAP_SUCCESS;