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>
39 EntryInfo **suffix_ei,
45 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
46 struct berval ctxcsn_rdn = { 0, NULL };
47 struct berval ctxcsn_ndn = { 0, NULL };
48 EntryInfo *ctxcsn_ei = NULL;
50 struct berval *max_committed_csn = NULL;
55 char textbuf[SLAP_TEXT_BUFLEN];
56 size_t textlen = sizeof textbuf;
57 Modifications *ml, *mlnext, *mod, *modlist;
58 Modifications **modtail = &modlist;
59 struct berval *csnbva = NULL;
60 EntryInfo *eip = NULL;
66 ber_str2bv( "cn=ldapsync", strlen("cn=ldapsync"), 0, &ctxcsn_rdn );
67 build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0], &ctxcsn_rdn );
69 rc = bdb_dn2entry( op, tid, &ctxcsn_ndn, &ctxcsn_ei,
70 1, locker, &ctxcsn_lock );
72 *ctxcsn_e = ctxcsn_ei->bei_e;
73 bdb_cache_entry_db_relock( bdb->bi_dbenv, locker, ctxcsn_ei, 1, 0, &ctxcsn_lock );
75 max_committed_csn = slap_get_commit_csn( op );
77 if ( max_committed_csn == NULL )
78 return BDB_CSN_COMMIT;
85 rs->sr_err = LDAP_OTHER;
86 rs->sr_text = "context csn not present";
87 ber_bvfree( max_committed_csn );
90 csnbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
91 ber_dupbv( &csnbva[0], max_committed_csn );
92 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
93 mod->sml_op = LDAP_MOD_REPLACE;
94 ber_str2bv( "contextCSN", strlen("contextCSN"), 1, &mod->sml_type );
95 mod->sml_bvalues = csnbva;
97 modtail = &mod->sml_next;
99 ret = slap_mods_check( modlist, 1, &rs->sr_text, textbuf, textlen, NULL );
101 if ( rc != LDAP_SUCCESS ) {
103 LDAP_LOG( OPERATION, ERR,
104 "bdb_csn_commit: mods check (%s)\n", rs->sr_text, 0, 0 );
106 Debug( LDAP_DEBUG_ANY,
107 "bdb_csn_commit: mods check (%s)\n", rs->sr_text, 0, 0 );
111 ret = bdb_modify_internal( op, tid, modlist, *ctxcsn_e,
112 &rs->sr_text, textbuf, textlen );
113 if ( ret != LDAP_SUCCESS ) {
115 LDAP_LOG ( OPERATION, ERR,
116 "bdb_csn_commit: modify failed (%d)\n", rs->sr_err, 0, 0 );
118 Debug( LDAP_DEBUG_TRACE,
119 "bdb_csn_commit: modify failed (%d)\n", rs->sr_err, 0, 0 );
122 case DB_LOCK_DEADLOCK:
123 case DB_LOCK_NOTGRANTED:
124 return BDB_CSN_ABORT;
130 for ( ml = modlist; ml != NULL; ml = mlnext ) {
131 mlnext = ml->sml_next;
135 ret = bdb_id2entry_update( op->o_bd, tid, *ctxcsn_e );
139 case DB_LOCK_DEADLOCK :
140 case DB_LOCK_NOTGRANTED :
144 rs->sr_text = "context csn update failed";
145 return BDB_CSN_ABORT;
150 if ( op->o_tag == LDAP_REQ_ADD &&
151 be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname )) {
153 eip = (EntryInfo *) ch_calloc( 1, sizeof( EntryInfo ));
154 eip->bei_id = op->oq_add.rs_e->e_id;
156 eip = *suffix_ei = ctxcsn_ei;
159 /* This serializes add. But this case is very rare : only once. */
160 rs->sr_err = bdb_next_id( op->o_bd, tid, &ctxcsn_id );
161 if ( rs->sr_err != 0 ) {
163 LDAP_LOG ( OPERATION, ERR,
164 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
166 Debug( LDAP_DEBUG_TRACE,
167 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
169 rs->sr_err = LDAP_OTHER;
170 rs->sr_text = "internal error";
171 return BDB_CSN_ABORT;
174 *ctxcsn_e = slap_create_context_csn_entry( op->o_bd, max_committed_csn );
175 ber_bvfree( max_committed_csn );
176 (*ctxcsn_e)->e_id = ctxcsn_id;
179 ret = bdb_dn2id_add( op, tid, eip, *ctxcsn_e );
183 case DB_LOCK_DEADLOCK :
184 case DB_LOCK_NOTGRANTED :
187 rs->sr_err = LDAP_OTHER;
188 rs->sr_text = "context csn exists before contex prefix does";
189 return BDB_CSN_ABORT;
191 rs->sr_err = LDAP_OTHER;
192 rs->sr_text = "context csn store failed";
193 return BDB_CSN_ABORT;
196 if ( *suffix_ei == NULL ) {
200 ret = bdb_id2entry_add( op->o_bd, tid, *ctxcsn_e );
204 case DB_LOCK_DEADLOCK :
205 case DB_LOCK_NOTGRANTED :
208 rs->sr_err = LDAP_OTHER;
209 rs->sr_text = "context csn store failed";
210 return BDB_CSN_ABORT;
212 ret = bdb_index_entry_add( op, tid, *ctxcsn_e );
216 case DB_LOCK_DEADLOCK :
217 case DB_LOCK_NOTGRANTED :
220 rs->sr_err = LDAP_OTHER;
221 rs->sr_text = "context csn indexing failed";
222 return BDB_CSN_ABORT;
225 case DB_LOCK_DEADLOCK:
226 case DB_LOCK_NOTGRANTED:
230 rs->sr_text = "ldap server busy";
231 return BDB_CSN_ABORT;
233 rs->sr_err = LDAP_OTHER;
234 rs->sr_text = "internal error";
235 return BDB_CSN_ABORT;
238 return BDB_CSN_COMMIT;
241 slap_rewind_commit_csn( op );
242 return BDB_CSN_RETRY;