]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/ctxcsn.c
Construct ctxcsn entries directly, plug memory leaks, remove
[openldap] / servers / slapd / back-bdb / ctxcsn.c
1 /* $OpenLDAP$ */
2 /*
3  * back-bdb Context CSN Management Routines
4  */
5 /* Copyright (c) 2003 by International Business Machines, Inc.
6  *
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
13  * permission.
14  *
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.
21  */
22
23 #include "portable.h"
24
25 #include <stdio.h>
26
27 #include <ac/string.h>
28 #include <ac/time.h>
29
30 #include "lutil.h"
31 #include "back-bdb.h"
32 #include "external.h"
33
34 int
35 bdb_csn_commit(
36         Operation *op,
37         SlapReply *rs,
38         DB_TXN *tid,
39         EntryInfo *ei,
40         EntryInfo **suffix_ei,
41         Entry **ctxcsn_e,
42         int *ctxcsn_added,
43         u_int32_t locker
44 )
45 {
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;
49         DB_LOCK                 ctxcsn_lock;
50         struct berval   *max_committed_csn = NULL;
51         DB_LOCK                 suffix_lock;
52         int                             rc, ret;
53         ID                              ctxcsn_id;
54         Entry                   *e;
55         char                    textbuf[SLAP_TEXT_BUFLEN];
56         size_t                  textlen = sizeof textbuf;
57         EntryInfo               *eip = NULL;
58
59         if ( ei ) {
60                 e = ei->bei_e;
61         }
62
63         build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0],
64                 (struct berval *)&slap_ldapsync_cn_bv );
65
66         rc = bdb_dn2entry( op, tid, &ctxcsn_ndn, &ctxcsn_ei,
67                                                            1, locker, &ctxcsn_lock );
68
69         *ctxcsn_e = ctxcsn_ei->bei_e;
70
71         max_committed_csn = slap_get_commit_csn( op );
72
73         if ( max_committed_csn == NULL ) {
74                 return BDB_CSN_COMMIT;
75         }
76
77         *ctxcsn_added = 0;
78
79         switch( rc ) {
80         case 0:
81                 if ( !*ctxcsn_e ) {
82                         rs->sr_err = LDAP_OTHER;
83                         rs->sr_text = "context csn not present";
84                         ber_bvfree( max_committed_csn );
85                         return BDB_CSN_ABORT;
86                 } else {
87                         Modifications mod;
88                         struct berval modvals[2];
89
90                         modvals[0] = *max_committed_csn;
91                         modvals[1].bv_val = NULL;
92                         modvals[1].bv_len = 0;
93
94                         mod.sml_op = LDAP_MOD_REPLACE;
95                         mod.sml_bvalues = modvals;
96                         mod.sml_desc = slap_schema.si_ad_contextCSN;
97                         mod.sml_type = mod.sml_desc->ad_cname;
98                         mod.sml_next = NULL;
99
100                         bdb_cache_entry_db_relock( bdb->bi_dbenv, locker, ctxcsn_ei, 1, 0, &ctxcsn_lock );
101
102                         ret = bdb_modify_internal( op, tid, &mod, *ctxcsn_e,
103                                                                         &rs->sr_text, textbuf, textlen );                                                               
104                         ber_bvfree( max_committed_csn );
105                         if ( ret != LDAP_SUCCESS ) {
106 #ifdef NEW_LOGGING
107                                 LDAP_LOG ( OPERATION, ERR,
108                                                 "bdb_csn_commit: modify failed (%d)\n", rs->sr_err, 0, 0 );
109 #else
110                                 Debug( LDAP_DEBUG_TRACE,
111                                                 "bdb_csn_commit: modify failed (%d)\n", rs->sr_err, 0, 0 );
112 #endif
113                                 switch( ret ) {
114                                 case DB_LOCK_DEADLOCK:
115                                 case DB_LOCK_NOTGRANTED:
116                                         goto rewind;
117                                 default:
118                                         return BDB_CSN_ABORT;
119                                 }
120                         }
121
122                         ret = bdb_id2entry_update( op->o_bd, tid, *ctxcsn_e );
123                         switch ( ret ) {
124                         case 0 :
125                                 break;
126                         case DB_LOCK_DEADLOCK :
127                         case DB_LOCK_NOTGRANTED :
128                                 goto rewind;
129                         default :
130                                 rs->sr_err = ret;
131                                 rs->sr_text = "context csn update failed";
132                                 return BDB_CSN_ABORT;
133                         }
134                 }
135                 break;
136         case DB_NOTFOUND:
137                 if ( op->o_tag == LDAP_REQ_ADD &&
138                                                 be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname )) {
139                         *suffix_ei = NULL;
140                         eip = (EntryInfo *) ch_calloc( 1, sizeof( EntryInfo ));
141                         eip->bei_id = op->oq_add.rs_e->e_id;
142                 } else {
143                         eip = *suffix_ei = ctxcsn_ei;
144                 }
145
146                 /* This serializes add. But this case is very rare : only once. */
147                 rs->sr_err = bdb_next_id( op->o_bd, tid, &ctxcsn_id );
148                 if ( rs->sr_err != 0 ) {
149 #ifdef NEW_LOGGING
150                         LDAP_LOG ( OPERATION, ERR,
151                                 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
152 #else
153                         Debug( LDAP_DEBUG_TRACE,
154                                 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
155 #endif
156                         rs->sr_err = LDAP_OTHER;
157                         rs->sr_text = "internal error";
158                         return BDB_CSN_ABORT;
159                 }
160
161                 *ctxcsn_e = slap_create_context_csn_entry( op->o_bd, max_committed_csn );
162                 ber_bvfree( max_committed_csn );
163                 (*ctxcsn_e)->e_id = ctxcsn_id;
164                 *ctxcsn_added = 1;
165
166                 ret = bdb_dn2id_add( op, tid, eip, *ctxcsn_e );
167                 switch ( ret ) {
168                 case 0 :
169                         break;
170                 case DB_LOCK_DEADLOCK :
171                 case DB_LOCK_NOTGRANTED :
172                         goto rewind;
173                 case DB_KEYEXIST :
174                         rs->sr_err = LDAP_OTHER;
175                         rs->sr_text = "context csn exists before contex prefix does";
176                         return BDB_CSN_ABORT;
177                 default :
178                         rs->sr_err = LDAP_OTHER;
179                         rs->sr_text = "context csn store failed";
180                         return BDB_CSN_ABORT;
181                 }
182
183                 if ( *suffix_ei == NULL ) {
184                         ch_free( eip );
185                 }
186
187                 ret = bdb_id2entry_add( op->o_bd, tid, *ctxcsn_e );
188                 switch ( ret ) {
189                 case 0 :
190                         break;
191                 case DB_LOCK_DEADLOCK :
192                 case DB_LOCK_NOTGRANTED :
193                         goto rewind;
194                 default :
195                         rs->sr_err = LDAP_OTHER;
196                         rs->sr_text = "context csn store failed";
197                         return BDB_CSN_ABORT;
198                 }
199                 ret = bdb_index_entry_add( op, tid, *ctxcsn_e );
200                 switch ( ret ) {
201                 case 0 :
202                         break;
203                 case DB_LOCK_DEADLOCK :
204                 case DB_LOCK_NOTGRANTED :
205                         goto rewind;
206                 default :
207                         rs->sr_err = LDAP_OTHER;
208                         rs->sr_text = "context csn indexing failed";
209                         return BDB_CSN_ABORT;
210                 }
211                 break;
212         case DB_LOCK_DEADLOCK:
213         case DB_LOCK_NOTGRANTED:
214 #ifdef NEW_LOGGING
215                 LDAP_LOG( OPERATION, ERR,
216                                 "bdb_csn_commit : bdb_dn2entry retry\n", 0, 0, 0 );
217 #else
218                 Debug( LDAP_DEBUG_TRACE,
219                                 "bdb_csn_commit : bdb_dn2entry retry\n", 0, 0, 0 );
220 #endif
221                 goto rewind;
222         case LDAP_BUSY:
223                 rs->sr_err = rc;
224                 rs->sr_text = "ldap server busy";
225                 return BDB_CSN_ABORT;
226         default:
227                 rs->sr_err = LDAP_OTHER;
228                 rs->sr_text = "internal error";
229                 return BDB_CSN_ABORT;
230         }
231
232         return BDB_CSN_COMMIT;
233
234 rewind :
235         slap_rewind_commit_csn( op );
236         return BDB_CSN_RETRY;
237 }
238
239 int
240 bdb_get_commit_csn(
241         Operation       *op,
242         SlapReply       *rs,
243         struct berval   **search_context_csn,
244         u_int32_t       locker,
245         DB_LOCK         *ctxcsn_lock
246 )
247 {
248         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
249         struct berval ctxcsn_rdn = BER_BVNULL;
250         struct berval ctxcsn_ndn = BER_BVNULL;
251         struct berval csn = BER_BVNULL;
252         struct berval ctx_nrdn = BER_BVC( "cn=ldapsync" );
253         EntryInfo       *ctxcsn_ei = NULL;
254         EntryInfo       *suffix_ei = NULL;
255         Entry           *ctxcsn_e = NULL;
256         DB_TXN          *ltid = NULL;
257         Attribute       *csn_a;
258         char            substr[67];
259         char            gid[DB_XIDDATASIZE];
260         char            csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
261         int                     num_retries = 0;
262         int                     ctxcsn_added = 0;
263         int                     rc;
264
265         if ( op->o_sync_mode != SLAP_SYNC_NONE ) {
266                 if ( op->o_bd->syncinfo ) {
267                         sprintf( substr, "cn=syncrepl%d", op->o_bd->syncinfo->id );
268                         ber_str2bv( substr, strlen( substr ), 0, &ctxcsn_rdn );
269                         build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0], &ctxcsn_rdn );
270                 } else {
271                         ber_str2bv( "cn=ldapsync", strlen("cn=ldapsync"), 0, &ctxcsn_rdn );
272                         build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0], &ctxcsn_rdn );
273                 }
274
275 ctxcsn_retry :
276                 rs->sr_err = bdb_dn2entry( op, NULL, &ctxcsn_ndn, &ctxcsn_ei,
277                                                                         0, locker, ctxcsn_lock );
278                 switch(rs->sr_err) {
279                 case 0:
280                         ch_free( ctxcsn_ndn.bv_val );
281                         if ( ctxcsn_ei ) {
282                                 ctxcsn_e = ctxcsn_ei->bei_e;
283                         }
284                         break;
285         case LDAP_BUSY:
286                         ch_free( ctxcsn_ndn.bv_val );
287                         LOCK_ID_FREE (bdb->bi_dbenv, locker );
288                         return LDAP_BUSY;
289         case DB_LOCK_DEADLOCK:
290         case DB_LOCK_NOTGRANTED:
291                         goto ctxcsn_retry;
292         case DB_NOTFOUND:
293                         if ( !op->o_bd->syncinfo ) {
294                                 snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
295                                                         bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
296
297                                 slap_get_csn( op, csnbuf, sizeof(csnbuf), &csn, 1 );
298
299                                 if ( 0 ) {
300 txn_retry:
301                                         rs->sr_err = TXN_ABORT( ltid );
302                                         if ( rs->sr_err != 0 ) {
303                                                 rs->sr_err = LDAP_OTHER;
304                                                 return rs->sr_err;
305                                         }
306
307                                         bdb_trans_backoff( ++num_retries );
308                                         ldap_pvt_thread_yield();
309                                 }
310                                 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, &ltid, bdb->bi_db_opflags );
311                                 if ( rs->sr_err != 0 ) {
312                                         rs->sr_err = LDAP_OTHER;
313                                         return rs->sr_err;
314                                 }
315
316                                 rs->sr_err = bdb_csn_commit( op, rs, ltid, NULL, &suffix_ei,
317                                                                                 &ctxcsn_e, &ctxcsn_added, locker );
318                                 switch( rs->sr_err ) {
319                                 case BDB_CSN_ABORT:
320                                         LOCK_ID_FREE( bdb->bi_dbenv, locker );
321                                         return LDAP_OTHER;
322                                 case BDB_CSN_RETRY:
323                                         goto txn_retry;
324                                 }
325
326                                 rs->sr_err = TXN_PREPARE( ltid, gid );
327                                 if ( rs->sr_err != 0 ) {
328                                         rs->sr_err = LDAP_OTHER;
329                                         return rs->sr_err;
330                                 }
331
332                                 bdb_cache_add( bdb, suffix_ei, ctxcsn_e, &ctx_nrdn, locker );
333
334                                 rs->sr_err = TXN_COMMIT( ltid, 0 );
335                                 if ( rs->sr_err != 0 ) {
336                                         rs->sr_err = LDAP_OTHER;
337                                         return rs->sr_err;
338                                 }
339
340                                 ctxcsn_ei = NULL;
341                                 rs->sr_err = bdb_dn2entry( op, NULL, &ctxcsn_ndn, &ctxcsn_ei,
342                                                                                 0, locker, ctxcsn_lock );
343                                 ch_free( ctxcsn_ndn.bv_val );
344
345                                 if ( ctxcsn_ei ) {
346                                         ctxcsn_e = ctxcsn_ei->bei_e;
347                                 }
348                         } else {
349                                 LOCK_ID_FREE( bdb->bi_dbenv, locker );
350                                 return LDAP_OTHER;
351                         }
352                         break;
353
354                 default:
355                         LOCK_ID_FREE (bdb->bi_dbenv, locker );
356                         return LDAP_OTHER;
357                 }
358
359                 if ( ctxcsn_e ) {
360                         if ( op->o_bd->syncinfo ) {
361                                 csn_a = attr_find( ctxcsn_e->e_attrs, slap_schema.si_ad_syncreplCookie );
362                         } else {
363                                 csn_a = attr_find( ctxcsn_e->e_attrs, slap_schema.si_ad_contextCSN );
364                         }
365                         if ( csn_a ) {
366                                 *search_context_csn = ber_dupbv( NULL, &csn_a->a_vals[0] );
367                         } else {
368                                 *search_context_csn = NULL;
369                         }
370                 } else {
371                         *search_context_csn = NULL;
372                 }
373         }
374
375         return LDAP_SUCCESS;
376 }