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;
57 rc = bdb_dn2entry( op, tid, &op->o_bd->be_context_csn, &ctxcsn_ei,
58 1, locker, &ctxcsn_lock );
60 *ctxcsn_e = ctxcsn_ei->bei_e;
62 slap_get_commit_csn( op, &max_committed_csn );
64 if ( max_committed_csn.bv_val == NULL ) {
65 return BDB_CSN_COMMIT;
73 rs->sr_err = LDAP_OTHER;
74 rs->sr_text = "context csn not present";
75 op->o_tmpfree( max_committed_csn.bv_val, op->o_tmpmemctx );
79 struct berval modvals[2];
82 modvals[0] = max_committed_csn;
83 modvals[1].bv_val = NULL;
84 modvals[1].bv_len = 0;
86 mod.sml_op = LDAP_MOD_REPLACE;
87 mod.sml_values = modvals;
88 mod.sml_nvalues = NULL;
89 mod.sml_desc = slap_schema.si_ad_contextCSN;
90 mod.sml_type = mod.sml_desc->ad_cname;
94 ret = bdb_modify_internal( op, tid, &mod, &dummy,
95 &rs->sr_text, textbuf, textlen );
96 op->o_tmpfree( max_committed_csn.bv_val, op->o_tmpmemctx );
97 if ( ret != LDAP_SUCCESS ) {
99 LDAP_LOG ( OPERATION, ERR,
100 "bdb_csn_commit: modify failed (%d)\n", rs->sr_err, 0, 0 );
102 Debug( LDAP_DEBUG_TRACE,
103 "bdb_csn_commit: modify failed (%d)\n", rs->sr_err, 0, 0 );
106 case DB_LOCK_DEADLOCK:
107 case DB_LOCK_NOTGRANTED:
110 return BDB_CSN_ABORT;
114 ret = bdb_id2entry_update( op->o_bd, tid, &dummy );
118 case DB_LOCK_DEADLOCK :
119 case DB_LOCK_NOTGRANTED :
123 rs->sr_text = "context csn update failed";
124 return BDB_CSN_ABORT;
126 bdb_cache_modify( *ctxcsn_e, dummy.e_attrs, bdb->bi_dbenv, locker, &ctxcsn_lock );
130 if ( op->o_tag == LDAP_REQ_ADD &&
131 be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname )) {
133 eip = (EntryInfo *) ch_calloc( 1, sizeof( EntryInfo ));
134 eip->bei_id = op->oq_add.rs_e->e_id;
136 eip = *suffix_ei = ctxcsn_ei;
139 /* This serializes add. But this case is very rare : only once. */
140 rs->sr_err = bdb_next_id( op->o_bd, tid, &ctxcsn_id );
141 if ( rs->sr_err != 0 ) {
143 LDAP_LOG ( OPERATION, ERR,
144 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
146 Debug( LDAP_DEBUG_TRACE,
147 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
149 rs->sr_err = LDAP_OTHER;
150 rs->sr_text = "internal error";
151 return BDB_CSN_ABORT;
154 *ctxcsn_e = slap_create_context_csn_entry( op->o_bd, &max_committed_csn );
155 op->o_tmpfree( max_committed_csn.bv_val, op->o_tmpmemctx );
156 (*ctxcsn_e)->e_id = ctxcsn_id;
159 ret = bdb_dn2id_add( op, tid, eip, *ctxcsn_e );
163 case DB_LOCK_DEADLOCK :
164 case DB_LOCK_NOTGRANTED :
167 rs->sr_err = LDAP_OTHER;
168 rs->sr_text = "context csn exists before context prefix does";
169 return BDB_CSN_ABORT;
171 rs->sr_err = LDAP_OTHER;
172 rs->sr_text = "context csn store failed";
173 return BDB_CSN_ABORT;
176 if ( *suffix_ei == NULL ) {
180 ret = bdb_id2entry_add( op->o_bd, tid, *ctxcsn_e );
184 case DB_LOCK_DEADLOCK :
185 case DB_LOCK_NOTGRANTED :
188 rs->sr_err = LDAP_OTHER;
189 rs->sr_text = "context csn store failed";
190 return BDB_CSN_ABORT;
192 ret = bdb_index_entry_add( op, tid, *ctxcsn_e );
196 case DB_LOCK_DEADLOCK :
197 case DB_LOCK_NOTGRANTED :
200 rs->sr_err = LDAP_OTHER;
201 rs->sr_text = "context csn indexing failed";
202 return BDB_CSN_ABORT;
205 case DB_LOCK_DEADLOCK:
206 case DB_LOCK_NOTGRANTED:
208 LDAP_LOG( OPERATION, ERR,
209 "bdb_csn_commit : bdb_dn2entry retry\n", 0, 0, 0 );
211 Debug( LDAP_DEBUG_TRACE,
212 "bdb_csn_commit : bdb_dn2entry retry\n", 0, 0, 0 );
217 rs->sr_text = "ldap server busy";
218 return BDB_CSN_ABORT;
220 rs->sr_err = LDAP_OTHER;
221 rs->sr_text = "internal error";
222 return BDB_CSN_ABORT;
225 return BDB_CSN_COMMIT;
228 slap_rewind_commit_csn( op );
229 return BDB_CSN_RETRY;
236 struct berval **search_context_csn,
241 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
242 struct berval csn = BER_BVNULL;
243 EntryInfo *ctxcsn_ei = NULL;
244 EntryInfo *suffix_ei = NULL;
245 Entry *ctxcsn_e = NULL;
248 char gid[DB_XIDDATASIZE];
249 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
251 int ctxcsn_added = 0;
253 struct sync_cookie syncCookie = { NULL, -1, NULL};
255 u_int32_t ctxcsn_locker = 0;
257 if ( op->o_sync_mode != SLAP_SYNC_NONE &&
258 !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
260 struct berval ctxcsn_ndn = BER_BVNULL;
263 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
264 sprintf( substr, "cn=syncrepl%ld", si->si_rid );
265 ber_str2bv( substr, 0, 0, &bv );
266 build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0], &bv, op->o_tmpmemctx );
268 consumer_ctxcsn_retry :
269 rs->sr_err = bdb_dn2entry( op, NULL, &ctxcsn_ndn, &ctxcsn_ei,
270 0, locker, ctxcsn_lock );
272 case DB_LOCK_DEADLOCK:
273 case DB_LOCK_NOTGRANTED:
274 goto consumer_ctxcsn_retry;
276 op->o_tmpfree( ctxcsn_ndn.bv_val, op->o_tmpmemctx );
277 ctxcsn_ndn.bv_val = NULL;
279 ctxcsn_e = ctxcsn_ei->bei_e;
284 rs->sr_err = LDAP_OTHER;
286 op->o_tmpfree( ctxcsn_ndn.bv_val, op->o_tmpmemctx );
287 ctxcsn_ndn.bv_val = NULL;
292 csn_a = attr_find( ctxcsn_e->e_attrs,
293 slap_schema.si_ad_syncreplCookie );
295 struct berval cookie;
298 ber_dupbv( &cookie, &csn_a->a_vals[0] );
299 ber_bvarray_add( &syncCookie.octet_str, &cookie );
300 slap_parse_sync_cookie( &syncCookie );
301 if ( *search_context_csn &&
302 (*search_context_csn)->bv_val != NULL )
304 value_match( &match, slap_schema.si_ad_entryCSN,
305 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
306 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
307 syncCookie.ctxcsn, *search_context_csn, &text );
310 /* set search_context_csn to the
311 smallest syncrepl cookie value */
312 if ( *search_context_csn ) {
313 ch_free( (*search_context_csn)->bv_val );
314 ch_free( *search_context_csn );
316 *search_context_csn = ber_dupbv( NULL,
319 slap_sync_cookie_free( &syncCookie, 0 );
321 *search_context_csn = NULL;
324 *search_context_csn = NULL;
327 } else if ( op->o_sync_mode != SLAP_SYNC_NONE &&
328 LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
330 provider_ctxcsn_retry :
331 rs->sr_err = bdb_dn2entry( op, NULL, &op->o_bd->be_context_csn, &ctxcsn_ei,
332 0, locker, ctxcsn_lock );
336 ctxcsn_e = ctxcsn_ei->bei_e;
341 case DB_LOCK_DEADLOCK:
342 case DB_LOCK_NOTGRANTED:
343 goto provider_ctxcsn_retry;
345 snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
346 bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
348 slap_get_csn( op, csnbuf, sizeof(csnbuf), &csn, 1 );
352 rs->sr_err = TXN_ABORT( ltid );
354 if ( rs->sr_err != 0 ) {
355 rs->sr_err = LDAP_OTHER;
358 ldap_pvt_thread_yield();
359 bdb_trans_backoff( ++num_retries );
361 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL,
362 <id, bdb->bi_db_opflags );
363 if ( rs->sr_err != 0 ) {
364 rs->sr_err = LDAP_OTHER;
368 ctxcsn_locker = TXN_ID ( ltid );
370 rs->sr_err = bdb_csn_commit( op, rs, ltid, NULL, &suffix_ei,
371 &ctxcsn_e, &ctxcsn_added, ctxcsn_locker );
372 switch( rs->sr_err ) {
374 rs->sr_err = LDAP_OTHER;
380 rs->sr_err = TXN_PREPARE( ltid, gid );
381 if ( rs->sr_err != 0 ) {
382 rs->sr_err = LDAP_OTHER;
386 bdb_cache_add( bdb, suffix_ei, ctxcsn_e,
387 (struct berval *)&slap_ldapsync_cn_bv, ctxcsn_locker );
389 rs->sr_err = TXN_COMMIT( ltid, 0 );
390 if ( rs->sr_err != 0 ) {
391 rs->sr_err = LDAP_OTHER;
395 rs->sr_err = bdb_dn2entry( op, NULL, &op->o_bd->be_context_csn, &ctxcsn_ei,
396 0, ctxcsn_locker, ctxcsn_lock );
399 ctxcsn_e = ctxcsn_ei->bei_e;
404 rs->sr_err = LDAP_OTHER;
409 csn_a = attr_find( ctxcsn_e->e_attrs,
410 slap_schema.si_ad_contextCSN );
412 *search_context_csn = ber_dupbv( NULL, &csn_a->a_vals[0] );
414 *search_context_csn = NULL;
417 *search_context_csn = NULL;
422 rs->sr_err = LDAP_SUCCESS;