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