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>
34 EntryInfo **suffix_ei,
40 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
41 EntryInfo *ctxcsn_ei = NULL;
43 struct berval max_committed_csn;
48 char textbuf[SLAP_TEXT_BUFLEN];
49 size_t textlen = sizeof textbuf;
50 EntryInfo *eip = NULL;
52 assert( !BER_BVISNULL( &op->o_bd->be_context_csn ) );
58 rc = bdb_dn2entry( op, tid, &op->o_bd->be_context_csn, &ctxcsn_ei,
59 1, locker, &ctxcsn_lock );
64 case DB_LOCK_DEADLOCK:
65 case DB_LOCK_NOTGRANTED:
71 *ctxcsn_e = ctxcsn_ei->bei_e;
73 slap_get_commit_csn( op, &max_committed_csn );
75 if ( max_committed_csn.bv_val == NULL ) {
76 return BDB_CSN_COMMIT;
84 rs->sr_err = LDAP_OTHER;
85 rs->sr_text = "context csn not present";
86 op->o_tmpfree( max_committed_csn.bv_val, op->o_tmpmemctx );
90 struct berval modvals[2];
93 modvals[0] = max_committed_csn;
94 modvals[1].bv_val = NULL;
95 modvals[1].bv_len = 0;
97 mod.sml_op = LDAP_MOD_REPLACE;
98 mod.sml_values = modvals;
99 mod.sml_nvalues = NULL;
100 mod.sml_desc = slap_schema.si_ad_contextCSN;
101 mod.sml_type = mod.sml_desc->ad_cname;
105 ret = bdb_modify_internal( op, tid, &mod, &dummy,
106 &rs->sr_text, textbuf, textlen );
107 op->o_tmpfree( max_committed_csn.bv_val, op->o_tmpmemctx );
108 if ( ret != LDAP_SUCCESS ) {
109 Debug( LDAP_DEBUG_TRACE,
110 "bdb_csn_commit: modify failed (%d)\n", rs->sr_err, 0, 0 );
111 if ( dummy.e_attrs != e->e_attrs ) attrs_free( dummy.e_attrs );
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 :
127 if ( dummy.e_attrs != e->e_attrs ) attrs_free( dummy.e_attrs );
130 if ( dummy.e_attrs != e->e_attrs ) attrs_free( dummy.e_attrs );
132 rs->sr_text = "context csn update failed";
133 return BDB_CSN_ABORT;
135 ret = bdb_cache_modify( *ctxcsn_e, dummy.e_attrs, bdb->bi_dbenv, locker, &ctxcsn_lock );
136 if ( ret != LDAP_SUCCESS ) {
137 if ( dummy.e_attrs != e->e_attrs ) attrs_free( dummy.e_attrs );
139 case DB_LOCK_DEADLOCK:
140 case DB_LOCK_NOTGRANTED:
147 if ( op->o_tag == LDAP_REQ_ADD &&
148 be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname )) {
150 eip = (EntryInfo *) ch_calloc( 1, sizeof( EntryInfo ));
151 eip->bei_id = op->oq_add.rs_e->e_id;
153 eip = *suffix_ei = ctxcsn_ei;
156 /* This serializes add. But this case is very rare : only once. */
157 rs->sr_err = bdb_next_id( op->o_bd, tid, &ctxcsn_id );
158 if ( rs->sr_err != 0 ) {
159 Debug( LDAP_DEBUG_TRACE,
160 "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:
219 Debug( LDAP_DEBUG_TRACE,
220 "bdb_csn_commit : bdb_dn2entry retry\n", 0, 0, 0 );
224 rs->sr_text = "ldap server busy";
225 return BDB_CSN_ABORT;
227 rs->sr_err = LDAP_OTHER;
228 rs->sr_text = "internal error";
229 return BDB_CSN_ABORT;
232 return BDB_CSN_COMMIT;
235 slap_rewind_commit_csn( op );
236 return BDB_CSN_RETRY;
243 struct berval **search_context_csn,
248 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
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};
262 u_int32_t ctxcsn_locker = 0;
264 if ( op->o_sync_mode != SLAP_SYNC_NONE &&
265 !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
267 struct berval ctxcsn_ndn = BER_BVNULL;
270 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
271 sprintf( substr, "cn=syncrepl%ld", si->si_rid );
272 ber_str2bv( substr, 0, 0, &bv );
273 build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0], &bv, op->o_tmpmemctx );
275 consumer_ctxcsn_retry :
276 rs->sr_err = bdb_dn2entry( op, NULL, &ctxcsn_ndn, &ctxcsn_ei,
277 0, locker, ctxcsn_lock );
279 case DB_LOCK_DEADLOCK:
280 case DB_LOCK_NOTGRANTED:
281 goto consumer_ctxcsn_retry;
283 op->o_tmpfree( ctxcsn_ndn.bv_val, op->o_tmpmemctx );
284 ctxcsn_ndn.bv_val = NULL;
286 ctxcsn_e = ctxcsn_ei->bei_e;
291 rs->sr_err = LDAP_OTHER;
293 op->o_tmpfree( ctxcsn_ndn.bv_val, op->o_tmpmemctx );
294 ctxcsn_ndn.bv_val = NULL;
299 csn_a = attr_find( ctxcsn_e->e_attrs,
300 slap_schema.si_ad_syncreplCookie );
302 struct berval cookie;
305 ber_dupbv( &cookie, &csn_a->a_vals[0] );
306 ber_bvarray_add( &syncCookie.octet_str, &cookie );
307 slap_parse_sync_cookie( &syncCookie );
308 if ( *search_context_csn &&
309 (*search_context_csn)->bv_val != NULL )
311 value_match( &match, slap_schema.si_ad_entryCSN,
312 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
313 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
314 syncCookie.ctxcsn, *search_context_csn, &text );
317 /* set search_context_csn to the
318 smallest syncrepl cookie value */
319 if ( *search_context_csn ) {
320 ch_free( (*search_context_csn)->bv_val );
321 ch_free( *search_context_csn );
323 *search_context_csn = ber_dupbv( NULL,
326 slap_sync_cookie_free( &syncCookie, 0 );
328 *search_context_csn = NULL;
331 *search_context_csn = NULL;
334 } else if ( op->o_sync_mode != SLAP_SYNC_NONE &&
335 LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
337 provider_ctxcsn_retry :
338 rs->sr_err = bdb_dn2entry( op, NULL, &op->o_bd->be_context_csn, &ctxcsn_ei,
339 0, locker, ctxcsn_lock );
343 ctxcsn_e = ctxcsn_ei->bei_e;
348 case DB_LOCK_DEADLOCK:
349 case DB_LOCK_NOTGRANTED:
350 goto provider_ctxcsn_retry;
352 snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
353 bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
355 slap_get_csn( op, csnbuf, sizeof(csnbuf), &csn, 1 );
359 rs->sr_err = TXN_ABORT( ltid );
361 if ( rs->sr_err != 0 ) {
362 rs->sr_err = LDAP_OTHER;
365 ldap_pvt_thread_yield();
366 bdb_trans_backoff( ++num_retries );
368 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL,
369 <id, bdb->bi_db_opflags );
370 if ( rs->sr_err != 0 ) {
371 rs->sr_err = LDAP_OTHER;
375 ctxcsn_locker = TXN_ID ( ltid );
377 rs->sr_err = bdb_csn_commit( op, rs, ltid, NULL,
378 &suffix_ei, &ctxcsn_e,
379 &ctxcsn_added, ctxcsn_locker );
380 switch( rs->sr_err ) {
382 rs->sr_err = LDAP_OTHER;
388 rs->sr_err = TXN_PREPARE( ltid, gid );
389 if ( rs->sr_err != 0 ) {
390 rs->sr_err = LDAP_OTHER;
394 bdb_cache_add( bdb, suffix_ei, ctxcsn_e,
395 (struct berval *)&slap_ldapsync_cn_bv, ctxcsn_locker );
397 rs->sr_err = TXN_COMMIT( ltid, 0 );
398 if ( rs->sr_err != 0 ) {
399 rs->sr_err = LDAP_OTHER;
403 rs->sr_err = bdb_dn2entry( op, NULL,
404 &op->o_bd->be_context_csn,
405 &ctxcsn_ei, 0, ctxcsn_locker,
409 ctxcsn_e = ctxcsn_ei->bei_e;
414 rs->sr_err = LDAP_OTHER;
419 csn_a = attr_find( ctxcsn_e->e_attrs,
420 slap_schema.si_ad_contextCSN );
422 *search_context_csn = ber_dupbv( NULL, &csn_a->a_vals[0] );
424 *search_context_csn = NULL;
427 *search_context_csn = NULL;
432 rs->sr_err = LDAP_SUCCESS;