3 * back-bdb Context CSN Management Routines
5 /* Copyright (c) 2003 by International Business Machines, Inc.
7 * International Business Machines, Inc. (hereinafter called IBM) grants
8 * permission under its copyrights to use, copy, modify, and distribute this
9 * Software with or without fee, provided that the above copyright notice and
10 * all paragraphs of this notice appear in all copies, and that the name of IBM
11 * not be used in connection with the marketing of any product incorporating
12 * the Software or modifications thereof, without specific, written prior
15 * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
17 * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
18 * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
19 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
20 * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
27 #include <ac/string.h>
40 EntryInfo **suffix_ei,
46 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
47 struct berval ctxcsn_ndn = { 0, NULL };
48 EntryInfo *ctxcsn_ei = NULL;
50 struct berval max_committed_csn;
55 char textbuf[SLAP_TEXT_BUFLEN];
56 size_t textlen = sizeof textbuf;
57 EntryInfo *eip = NULL;
63 build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0],
64 (struct berval *)&slap_ldapsync_cn_bv, op->o_tmpmemctx );
66 rc = bdb_dn2entry( op, tid, &ctxcsn_ndn, &ctxcsn_ei,
67 1, locker, &ctxcsn_lock );
69 *ctxcsn_e = ctxcsn_ei->bei_e;
71 op->o_tmpfree( ctxcsn_ndn.bv_val, op->o_tmpmemctx );
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 ch_free( max_committed_csn.bv_val );
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_bvalues = 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 ch_free( max_committed_csn.bv_val );
108 if ( ret != LDAP_SUCCESS ) {
110 LDAP_LOG ( OPERATION, ERR,
111 "bdb_csn_commit: modify failed (%d)\n", rs->sr_err, 0, 0 );
113 Debug( LDAP_DEBUG_TRACE,
114 "bdb_csn_commit: modify failed (%d)\n", rs->sr_err, 0, 0 );
117 case DB_LOCK_DEADLOCK:
118 case DB_LOCK_NOTGRANTED:
121 return BDB_CSN_ABORT;
125 ret = bdb_id2entry_update( op->o_bd, tid, &dummy );
129 case DB_LOCK_DEADLOCK :
130 case DB_LOCK_NOTGRANTED :
134 rs->sr_text = "context csn update failed";
135 return BDB_CSN_ABORT;
137 bdb_cache_modify( *ctxcsn_e, dummy.e_attrs, bdb->bi_dbenv, locker, &ctxcsn_lock );
141 if ( op->o_tag == LDAP_REQ_ADD &&
142 be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname )) {
144 eip = (EntryInfo *) ch_calloc( 1, sizeof( EntryInfo ));
145 eip->bei_id = op->oq_add.rs_e->e_id;
147 eip = *suffix_ei = ctxcsn_ei;
150 /* This serializes add. But this case is very rare : only once. */
151 rs->sr_err = bdb_next_id( op->o_bd, tid, &ctxcsn_id );
152 if ( rs->sr_err != 0 ) {
154 LDAP_LOG ( OPERATION, ERR,
155 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
157 Debug( LDAP_DEBUG_TRACE,
158 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
160 rs->sr_err = LDAP_OTHER;
161 rs->sr_text = "internal error";
162 return BDB_CSN_ABORT;
165 *ctxcsn_e = slap_create_context_csn_entry( op->o_bd, &max_committed_csn );
166 ch_free( max_committed_csn.bv_val );
167 (*ctxcsn_e)->e_id = ctxcsn_id;
170 ret = bdb_dn2id_add( op, tid, eip, *ctxcsn_e );
174 case DB_LOCK_DEADLOCK :
175 case DB_LOCK_NOTGRANTED :
178 rs->sr_err = LDAP_OTHER;
179 rs->sr_text = "context csn exists before contex prefix does";
180 return BDB_CSN_ABORT;
182 rs->sr_err = LDAP_OTHER;
183 rs->sr_text = "context csn store failed";
184 return BDB_CSN_ABORT;
187 if ( *suffix_ei == NULL ) {
191 ret = bdb_id2entry_add( op->o_bd, tid, *ctxcsn_e );
195 case DB_LOCK_DEADLOCK :
196 case DB_LOCK_NOTGRANTED :
199 rs->sr_err = LDAP_OTHER;
200 rs->sr_text = "context csn store failed";
201 return BDB_CSN_ABORT;
203 ret = bdb_index_entry_add( op, tid, *ctxcsn_e );
207 case DB_LOCK_DEADLOCK :
208 case DB_LOCK_NOTGRANTED :
211 rs->sr_err = LDAP_OTHER;
212 rs->sr_text = "context csn indexing failed";
213 return BDB_CSN_ABORT;
216 case DB_LOCK_DEADLOCK:
217 case DB_LOCK_NOTGRANTED:
219 LDAP_LOG( OPERATION, ERR,
220 "bdb_csn_commit : bdb_dn2entry retry\n", 0, 0, 0 );
222 Debug( LDAP_DEBUG_TRACE,
223 "bdb_csn_commit : bdb_dn2entry retry\n", 0, 0, 0 );
228 rs->sr_text = "ldap server busy";
229 return BDB_CSN_ABORT;
231 rs->sr_err = LDAP_OTHER;
232 rs->sr_text = "internal error";
233 return BDB_CSN_ABORT;
236 return BDB_CSN_COMMIT;
239 slap_rewind_commit_csn( op );
240 return BDB_CSN_RETRY;
247 struct berval **search_context_csn,
252 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
253 struct berval ctxcsn_ndn = BER_BVNULL;
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 if ( op->o_sync_mode != SLAP_SYNC_NONE ) {
268 if ( op->o_bd->be_syncinfo ) {
271 sprintf( substr, "cn=syncrepl%d", op->o_bd->be_syncinfo->si_id );
272 ber_str2bv( substr, 0, 0, &bv );
273 build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0], &bv, NULL );
275 build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0],
276 (struct berval *)&slap_ldapsync_cn_bv, NULL );
280 rs->sr_err = bdb_dn2entry( op, NULL, &ctxcsn_ndn, &ctxcsn_ei,
281 0, locker, ctxcsn_lock );
284 ch_free( ctxcsn_ndn.bv_val );
286 ctxcsn_e = ctxcsn_ei->bei_e;
290 ch_free( ctxcsn_ndn.bv_val );
291 LOCK_ID_FREE (bdb->bi_dbenv, locker );
293 case DB_LOCK_DEADLOCK:
294 case DB_LOCK_NOTGRANTED:
297 if ( !op->o_bd->be_syncinfo ) {
298 snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
299 bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
301 slap_get_csn( op, csnbuf, sizeof(csnbuf), &csn, 1 );
305 rs->sr_err = TXN_ABORT( ltid );
306 if ( rs->sr_err != 0 ) {
307 rs->sr_err = LDAP_OTHER;
310 ldap_pvt_thread_yield();
311 bdb_trans_backoff( ++num_retries );
313 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, <id, bdb->bi_db_opflags );
314 if ( rs->sr_err != 0 ) {
315 rs->sr_err = LDAP_OTHER;
319 rs->sr_err = bdb_csn_commit( op, rs, ltid, NULL, &suffix_ei,
320 &ctxcsn_e, &ctxcsn_added, locker );
321 switch( rs->sr_err ) {
323 LOCK_ID_FREE( bdb->bi_dbenv, locker );
329 rs->sr_err = TXN_PREPARE( ltid, gid );
330 if ( rs->sr_err != 0 ) {
331 rs->sr_err = LDAP_OTHER;
335 bdb_cache_add( bdb, suffix_ei, ctxcsn_e,
336 (struct berval *)&slap_ldapsync_cn_bv, locker );
338 rs->sr_err = TXN_COMMIT( ltid, 0 );
339 if ( rs->sr_err != 0 ) {
340 rs->sr_err = LDAP_OTHER;
345 rs->sr_err = bdb_dn2entry( op, NULL, &ctxcsn_ndn, &ctxcsn_ei,
346 0, locker, ctxcsn_lock );
347 ch_free( ctxcsn_ndn.bv_val );
350 ctxcsn_e = ctxcsn_ei->bei_e;
353 LOCK_ID_FREE( bdb->bi_dbenv, locker );
359 LOCK_ID_FREE (bdb->bi_dbenv, locker );
364 if ( op->o_bd->be_syncinfo ) {
365 csn_a = attr_find( ctxcsn_e->e_attrs,
366 slap_schema.si_ad_syncreplCookie );
368 struct berval cookie;
369 ber_dupbv( &cookie, &csn_a->a_vals[0] );
370 ber_bvarray_add( &syncCookie.octet_str, &cookie );
371 slap_parse_sync_cookie( &syncCookie );
372 *search_context_csn = ber_dupbv( NULL, syncCookie.ctxcsn );
373 slap_sync_cookie_free( &syncCookie, 0 );
375 *search_context_csn = NULL;
378 csn_a = attr_find( ctxcsn_e->e_attrs,
379 slap_schema.si_ad_contextCSN );
381 *search_context_csn = ber_dupbv( NULL, &csn_a->a_vals[0] );
383 *search_context_csn = NULL;
387 *search_context_csn = NULL;