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 ) {
111 LDAP_LOG ( OPERATION, ERR,
112 "bdb_csn_commit: modify failed (%d)\n", rs->sr_err, 0, 0 );
114 Debug( LDAP_DEBUG_TRACE,
115 "bdb_csn_commit: modify failed (%d)\n", rs->sr_err, 0, 0 );
118 case DB_LOCK_DEADLOCK:
119 case DB_LOCK_NOTGRANTED:
122 return BDB_CSN_ABORT;
126 ret = bdb_id2entry_update( op->o_bd, tid, &dummy );
130 case DB_LOCK_DEADLOCK :
131 case DB_LOCK_NOTGRANTED :
135 rs->sr_text = "context csn update failed";
136 return BDB_CSN_ABORT;
138 bdb_cache_modify( *ctxcsn_e, dummy.e_attrs, bdb->bi_dbenv, locker, &ctxcsn_lock );
142 if ( op->o_tag == LDAP_REQ_ADD &&
143 be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname )) {
145 eip = (EntryInfo *) ch_calloc( 1, sizeof( EntryInfo ));
146 eip->bei_id = op->oq_add.rs_e->e_id;
148 eip = *suffix_ei = ctxcsn_ei;
151 /* This serializes add. But this case is very rare : only once. */
152 rs->sr_err = bdb_next_id( op->o_bd, tid, &ctxcsn_id );
153 if ( rs->sr_err != 0 ) {
155 LDAP_LOG ( OPERATION, ERR,
156 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
158 Debug( LDAP_DEBUG_TRACE,
159 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
161 rs->sr_err = LDAP_OTHER;
162 rs->sr_text = "internal error";
163 return BDB_CSN_ABORT;
166 *ctxcsn_e = slap_create_context_csn_entry( op->o_bd, &max_committed_csn );
167 op->o_tmpfree( max_committed_csn.bv_val, op->o_tmpmemctx );
168 (*ctxcsn_e)->e_id = ctxcsn_id;
171 ret = bdb_dn2id_add( op, tid, eip, *ctxcsn_e );
175 case DB_LOCK_DEADLOCK :
176 case DB_LOCK_NOTGRANTED :
179 rs->sr_err = LDAP_OTHER;
180 rs->sr_text = "context csn exists before context prefix does";
181 return BDB_CSN_ABORT;
183 rs->sr_err = LDAP_OTHER;
184 rs->sr_text = "context csn store failed";
185 return BDB_CSN_ABORT;
188 if ( *suffix_ei == NULL ) {
192 ret = bdb_id2entry_add( op->o_bd, 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 store failed";
202 return BDB_CSN_ABORT;
204 ret = bdb_index_entry_add( op, tid, *ctxcsn_e );
208 case DB_LOCK_DEADLOCK :
209 case DB_LOCK_NOTGRANTED :
212 rs->sr_err = LDAP_OTHER;
213 rs->sr_text = "context csn indexing failed";
214 return BDB_CSN_ABORT;
217 case DB_LOCK_DEADLOCK:
218 case DB_LOCK_NOTGRANTED:
220 LDAP_LOG( OPERATION, ERR,
221 "bdb_csn_commit : bdb_dn2entry retry\n", 0, 0, 0 );
223 Debug( LDAP_DEBUG_TRACE,
224 "bdb_csn_commit : bdb_dn2entry retry\n", 0, 0, 0 );
229 rs->sr_text = "ldap server busy";
230 return BDB_CSN_ABORT;
232 rs->sr_err = LDAP_OTHER;
233 rs->sr_text = "internal error";
234 return BDB_CSN_ABORT;
237 return BDB_CSN_COMMIT;
240 slap_rewind_commit_csn( op );
241 return BDB_CSN_RETRY;
248 struct berval **search_context_csn,
253 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
254 struct berval csn = BER_BVNULL;
255 EntryInfo *ctxcsn_ei = NULL;
256 EntryInfo *suffix_ei = NULL;
257 Entry *ctxcsn_e = NULL;
260 char gid[DB_XIDDATASIZE];
261 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
263 int ctxcsn_added = 0;
265 struct sync_cookie syncCookie = { NULL, -1, NULL};
267 u_int32_t ctxcsn_locker = 0;
269 if ( op->o_sync_mode != SLAP_SYNC_NONE &&
270 !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
272 struct berval ctxcsn_ndn = BER_BVNULL;
275 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
276 sprintf( substr, "cn=syncrepl%ld", si->si_rid );
277 ber_str2bv( substr, 0, 0, &bv );
278 build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0], &bv, op->o_tmpmemctx );
280 consumer_ctxcsn_retry :
281 rs->sr_err = bdb_dn2entry( op, NULL, &ctxcsn_ndn, &ctxcsn_ei,
282 0, locker, ctxcsn_lock );
284 case DB_LOCK_DEADLOCK:
285 case DB_LOCK_NOTGRANTED:
286 goto consumer_ctxcsn_retry;
288 op->o_tmpfree( ctxcsn_ndn.bv_val, op->o_tmpmemctx );
289 ctxcsn_ndn.bv_val = NULL;
291 ctxcsn_e = ctxcsn_ei->bei_e;
296 rs->sr_err = LDAP_OTHER;
298 op->o_tmpfree( ctxcsn_ndn.bv_val, op->o_tmpmemctx );
299 ctxcsn_ndn.bv_val = NULL;
304 csn_a = attr_find( ctxcsn_e->e_attrs,
305 slap_schema.si_ad_syncreplCookie );
307 struct berval cookie;
310 ber_dupbv( &cookie, &csn_a->a_vals[0] );
311 ber_bvarray_add( &syncCookie.octet_str, &cookie );
312 slap_parse_sync_cookie( &syncCookie );
313 if ( *search_context_csn &&
314 (*search_context_csn)->bv_val != NULL )
316 value_match( &match, slap_schema.si_ad_entryCSN,
317 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
318 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
319 syncCookie.ctxcsn, *search_context_csn, &text );
322 /* set search_context_csn to the
323 smallest syncrepl cookie value */
324 if ( *search_context_csn ) {
325 ch_free( (*search_context_csn)->bv_val );
326 ch_free( *search_context_csn );
328 *search_context_csn = ber_dupbv( NULL,
331 slap_sync_cookie_free( &syncCookie, 0 );
333 *search_context_csn = NULL;
336 *search_context_csn = NULL;
339 } else if ( op->o_sync_mode != SLAP_SYNC_NONE &&
340 LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
342 provider_ctxcsn_retry :
343 rs->sr_err = bdb_dn2entry( op, NULL, &op->o_bd->be_context_csn, &ctxcsn_ei,
344 0, locker, ctxcsn_lock );
348 ctxcsn_e = ctxcsn_ei->bei_e;
353 case DB_LOCK_DEADLOCK:
354 case DB_LOCK_NOTGRANTED:
355 goto provider_ctxcsn_retry;
357 snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
358 bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
360 slap_get_csn( op, csnbuf, sizeof(csnbuf), &csn, 1 );
364 rs->sr_err = TXN_ABORT( ltid );
366 if ( rs->sr_err != 0 ) {
367 rs->sr_err = LDAP_OTHER;
370 ldap_pvt_thread_yield();
371 bdb_trans_backoff( ++num_retries );
373 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL,
374 <id, bdb->bi_db_opflags );
375 if ( rs->sr_err != 0 ) {
376 rs->sr_err = LDAP_OTHER;
380 ctxcsn_locker = TXN_ID ( ltid );
382 rs->sr_err = bdb_csn_commit( op, rs, ltid, NULL, &suffix_ei,
383 &ctxcsn_e, &ctxcsn_added, ctxcsn_locker );
384 switch( rs->sr_err ) {
386 rs->sr_err = LDAP_OTHER;
392 rs->sr_err = TXN_PREPARE( ltid, gid );
393 if ( rs->sr_err != 0 ) {
394 rs->sr_err = LDAP_OTHER;
398 bdb_cache_add( bdb, suffix_ei, ctxcsn_e,
399 (struct berval *)&slap_ldapsync_cn_bv, ctxcsn_locker );
401 rs->sr_err = TXN_COMMIT( ltid, 0 );
402 if ( rs->sr_err != 0 ) {
403 rs->sr_err = LDAP_OTHER;
407 rs->sr_err = bdb_dn2entry( op, NULL, &op->o_bd->be_context_csn, &ctxcsn_ei,
408 0, ctxcsn_locker, ctxcsn_lock );
411 ctxcsn_e = ctxcsn_ei->bei_e;
416 rs->sr_err = LDAP_OTHER;
421 csn_a = attr_find( ctxcsn_e->e_attrs,
422 slap_schema.si_ad_contextCSN );
424 *search_context_csn = ber_dupbv( NULL, &csn_a->a_vals[0] );
426 *search_context_csn = NULL;
429 *search_context_csn = NULL;
434 rs->sr_err = LDAP_SUCCESS;