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 );
112 if ( dummy.e_attrs != e->e_attrs ) attrs_free( dummy.e_attrs );
114 case DB_LOCK_DEADLOCK:
115 case DB_LOCK_NOTGRANTED:
118 return BDB_CSN_ABORT;
122 ret = bdb_id2entry_update( op->o_bd, tid, &dummy );
126 case DB_LOCK_DEADLOCK :
127 case DB_LOCK_NOTGRANTED :
128 if ( dummy.e_attrs != e->e_attrs ) attrs_free( dummy.e_attrs );
131 if ( dummy.e_attrs != e->e_attrs ) attrs_free( dummy.e_attrs );
133 rs->sr_text = "context csn update failed";
134 return BDB_CSN_ABORT;
136 ret = bdb_cache_modify( *ctxcsn_e, dummy.e_attrs, bdb->bi_dbenv, locker, &ctxcsn_lock );
137 if ( ret != LDAP_SUCCESS ) {
138 if ( dummy.e_attrs != e->e_attrs ) attrs_free( dummy.e_attrs );
140 case DB_LOCK_DEADLOCK:
141 case DB_LOCK_NOTGRANTED:
148 if ( op->o_tag == LDAP_REQ_ADD &&
149 be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname )) {
151 eip = (EntryInfo *) ch_calloc( 1, sizeof( EntryInfo ));
152 eip->bei_id = op->oq_add.rs_e->e_id;
154 eip = *suffix_ei = ctxcsn_ei;
157 /* This serializes add. But this case is very rare : only once. */
158 rs->sr_err = bdb_next_id( op->o_bd, tid, &ctxcsn_id );
159 if ( rs->sr_err != 0 ) {
160 Debug( LDAP_DEBUG_TRACE,
161 "bdb_csn_commit: next_id failed (%d)\n",
163 rs->sr_err = LDAP_OTHER;
164 rs->sr_text = "internal error";
165 return BDB_CSN_ABORT;
168 *ctxcsn_e = slap_create_context_csn_entry( op->o_bd, &max_committed_csn );
169 op->o_tmpfree( max_committed_csn.bv_val, op->o_tmpmemctx );
170 (*ctxcsn_e)->e_id = ctxcsn_id;
173 ret = bdb_dn2id_add( op, tid, eip, *ctxcsn_e );
177 case DB_LOCK_DEADLOCK :
178 case DB_LOCK_NOTGRANTED :
181 rs->sr_err = LDAP_OTHER;
182 rs->sr_text = "context csn exists before context prefix does";
183 return BDB_CSN_ABORT;
185 rs->sr_err = LDAP_OTHER;
186 rs->sr_text = "context csn store failed";
187 return BDB_CSN_ABORT;
190 if ( *suffix_ei == NULL ) {
194 ret = bdb_id2entry_add( op->o_bd, 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 store failed";
204 return BDB_CSN_ABORT;
206 ret = bdb_index_entry_add( op, tid, *ctxcsn_e );
210 case DB_LOCK_DEADLOCK :
211 case DB_LOCK_NOTGRANTED :
214 rs->sr_err = LDAP_OTHER;
215 rs->sr_text = "context csn indexing failed";
216 return BDB_CSN_ABORT;
219 case DB_LOCK_DEADLOCK:
220 case DB_LOCK_NOTGRANTED:
221 Debug( LDAP_DEBUG_TRACE,
222 "bdb_csn_commit : bdb_dn2entry retry\n", 0, 0, 0 );
226 rs->sr_text = "ldap server busy";
227 return BDB_CSN_ABORT;
229 rs->sr_err = LDAP_OTHER;
230 rs->sr_text = "internal error";
231 return BDB_CSN_ABORT;
234 return BDB_CSN_COMMIT;
237 slap_rewind_commit_csn( op );
238 return BDB_CSN_RETRY;
245 struct berval **search_context_csn,
250 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
251 struct berval csn = BER_BVNULL;
252 EntryInfo *ctxcsn_ei = NULL;
253 EntryInfo *suffix_ei = NULL;
254 Entry *ctxcsn_e = NULL;
257 char gid[DB_XIDDATASIZE];
258 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
260 int ctxcsn_added = 0;
262 struct sync_cookie syncCookie = { NULL, -1, NULL};
264 u_int32_t ctxcsn_locker = 0;
266 if ( (op->o_sync_mode & SLAP_SYNC_REFRESH) &&
267 !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
269 struct berval ctxcsn_ndn = BER_BVNULL;
272 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
273 sprintf( substr, "cn=syncrepl%ld", si->si_rid );
274 ber_str2bv( substr, 0, 0, &bv );
275 build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0], &bv, op->o_tmpmemctx );
277 consumer_ctxcsn_retry :
278 rs->sr_err = bdb_dn2entry( op, NULL, &ctxcsn_ndn, &ctxcsn_ei,
279 0, locker, ctxcsn_lock );
281 case DB_LOCK_DEADLOCK:
282 case DB_LOCK_NOTGRANTED:
283 goto consumer_ctxcsn_retry;
285 op->o_tmpfree( ctxcsn_ndn.bv_val, op->o_tmpmemctx );
286 ctxcsn_ndn.bv_val = NULL;
288 ctxcsn_e = ctxcsn_ei->bei_e;
293 rs->sr_err = LDAP_OTHER;
295 op->o_tmpfree( ctxcsn_ndn.bv_val, op->o_tmpmemctx );
296 ctxcsn_ndn.bv_val = NULL;
301 csn_a = attr_find( ctxcsn_e->e_attrs,
302 slap_schema.si_ad_syncreplCookie );
304 struct berval cookie;
307 ber_dupbv( &cookie, &csn_a->a_vals[0] );
308 ber_bvarray_add( &syncCookie.octet_str, &cookie );
309 slap_parse_sync_cookie( &syncCookie );
310 if ( *search_context_csn &&
311 (*search_context_csn)->bv_val != NULL )
313 value_match( &match, slap_schema.si_ad_entryCSN,
314 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
315 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
316 syncCookie.ctxcsn, *search_context_csn, &text );
319 /* set search_context_csn to the
320 smallest syncrepl cookie value */
321 if ( *search_context_csn ) {
322 ch_free( (*search_context_csn)->bv_val );
323 ch_free( *search_context_csn );
325 *search_context_csn = ber_dupbv( NULL,
328 slap_sync_cookie_free( &syncCookie, 0 );
330 *search_context_csn = NULL;
333 *search_context_csn = NULL;
336 } else if ( (op->o_sync_mode & SLAP_SYNC_REFRESH) &&
337 LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
339 provider_ctxcsn_retry :
340 rs->sr_err = bdb_dn2entry( op, NULL, &op->o_bd->be_context_csn, &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,
380 &suffix_ei, &ctxcsn_e,
381 &ctxcsn_added, ctxcsn_locker );
382 switch( rs->sr_err ) {
384 rs->sr_err = LDAP_OTHER;
390 rs->sr_err = TXN_PREPARE( ltid, gid );
391 if ( rs->sr_err != 0 ) {
392 rs->sr_err = LDAP_OTHER;
396 bdb_cache_add( bdb, suffix_ei, ctxcsn_e,
397 (struct berval *)&slap_ldapsync_cn_bv, ctxcsn_locker );
399 rs->sr_err = TXN_COMMIT( ltid, 0 );
400 if ( rs->sr_err != 0 ) {
401 rs->sr_err = LDAP_OTHER;
405 rs->sr_err = bdb_dn2entry( op, NULL,
406 &op->o_bd->be_context_csn,
407 &ctxcsn_ei, 0, ctxcsn_locker,
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;