]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/ctxcsn.c
Update BDB 4.3 errcall prototype again to silence warnings
[openldap] / servers / slapd / back-bdb / ctxcsn.c
1 /* ctxcsn.c -- back-bdb Context CSN Management Routines */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2003-2004 The OpenLDAP Foundation.
6  * Portions Copyright 2003 IBM Corporation.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
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>.
16  */
17
18 #include "portable.h"
19
20 #include <stdio.h>
21
22 #include <ac/string.h>
23 #include <ac/time.h>
24
25 #include "lutil.h"
26 #include "back-bdb.h"
27
28 int
29 bdb_csn_commit(
30         Operation *op,
31         SlapReply *rs,
32         DB_TXN *tid,
33         EntryInfo *ei,
34         EntryInfo **suffix_ei,
35         Entry **ctxcsn_e,
36         int *ctxcsn_added,
37         u_int32_t locker
38 )
39 {
40         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
41         EntryInfo               *ctxcsn_ei = NULL;
42         DB_LOCK                 ctxcsn_lock;
43         struct berval   max_committed_csn;
44         DB_LOCK                 suffix_lock;
45         int                             rc, ret;
46         ID                              ctxcsn_id;
47         Entry                   *e;
48         char                    textbuf[SLAP_TEXT_BUFLEN];
49         size_t                  textlen = sizeof textbuf;
50         EntryInfo               *eip = NULL;
51
52         assert( !BER_BVISNULL( &op->o_bd->be_context_csn ) );
53
54         if ( ei ) {
55                 e = ei->bei_e;
56         }
57
58         rc =  bdb_dn2entry( op, tid, &op->o_bd->be_context_csn, &ctxcsn_ei,
59                         1, locker, &ctxcsn_lock );
60         switch( rc ) {
61         case 0:
62         case DB_NOTFOUND:
63                 break;
64         case DB_LOCK_DEADLOCK:
65         case DB_LOCK_NOTGRANTED:
66                 return BDB_CSN_RETRY;
67         default:
68                 return BDB_CSN_ABORT;
69         }
70         
71         *ctxcsn_e = ctxcsn_ei->bei_e;
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                         op->o_tmpfree( max_committed_csn.bv_val, op->o_tmpmemctx );
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_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;
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                         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 );
112                                 switch( ret ) {
113                                 case DB_LOCK_DEADLOCK:
114                                 case DB_LOCK_NOTGRANTED:
115                                         goto rewind;
116                                 default:
117                                         return BDB_CSN_ABORT;
118                                 }
119                         }
120
121                         ret = bdb_id2entry_update( op->o_bd, tid, &dummy );
122                         switch ( ret ) {
123                         case 0 :
124                                 break;
125                         case DB_LOCK_DEADLOCK :
126                         case DB_LOCK_NOTGRANTED :
127                                 if ( dummy.e_attrs != e->e_attrs ) attrs_free( dummy.e_attrs );
128                                 goto rewind;
129                         default :
130                                 if ( dummy.e_attrs != e->e_attrs ) attrs_free( dummy.e_attrs );
131                                 rs->sr_err = ret;
132                                 rs->sr_text = "context csn update failed";
133                                 return BDB_CSN_ABORT;
134                         }
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 );
138                                 switch( ret ) {
139                                 case DB_LOCK_DEADLOCK:
140                                 case DB_LOCK_NOTGRANTED:
141                                         goto rewind;
142                                 }
143                         }
144                 }
145                 break;
146         case DB_NOTFOUND:
147                 if ( op->o_tag == LDAP_REQ_ADD &&
148                                                 be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname )) {
149                         *suffix_ei = NULL;
150                         eip = (EntryInfo *) ch_calloc( 1, sizeof( EntryInfo ));
151                         eip->bei_id = op->oq_add.rs_e->e_id;
152                 } else {
153                         eip = *suffix_ei = ctxcsn_ei;
154                 }
155
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;
164                 }
165
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;
169                 *ctxcsn_added = 1;
170
171                 ret = bdb_dn2id_add( op, tid, eip, *ctxcsn_e );
172                 switch ( ret ) {
173                 case 0 :
174                         break;
175                 case DB_LOCK_DEADLOCK :
176                 case DB_LOCK_NOTGRANTED :
177                         goto rewind;
178                 case DB_KEYEXIST :
179                         rs->sr_err = LDAP_OTHER;
180                         rs->sr_text = "context csn exists before context prefix does";
181                         return BDB_CSN_ABORT;
182                 default :
183                         rs->sr_err = LDAP_OTHER;
184                         rs->sr_text = "context csn store failed";
185                         return BDB_CSN_ABORT;
186                 }
187
188                 if ( *suffix_ei == NULL ) {
189                         ch_free( eip );
190                 }
191
192                 ret = bdb_id2entry_add( op->o_bd, tid, *ctxcsn_e );
193                 switch ( ret ) {
194                 case 0 :
195                         break;
196                 case DB_LOCK_DEADLOCK :
197                 case DB_LOCK_NOTGRANTED :
198                         goto rewind;
199                 default :
200                         rs->sr_err = LDAP_OTHER;
201                         rs->sr_text = "context csn store failed";
202                         return BDB_CSN_ABORT;
203                 }
204                 ret = bdb_index_entry_add( op, tid, *ctxcsn_e );
205                 switch ( ret ) {
206                 case 0 :
207                         break;
208                 case DB_LOCK_DEADLOCK :
209                 case DB_LOCK_NOTGRANTED :
210                         goto rewind;
211                 default :
212                         rs->sr_err = LDAP_OTHER;
213                         rs->sr_text = "context csn indexing failed";
214                         return BDB_CSN_ABORT;
215                 }
216                 break;
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 );
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 csn = BER_BVNULL;
250         EntryInfo       *ctxcsn_ei = NULL;
251         EntryInfo       *suffix_ei = NULL;
252         Entry           *ctxcsn_e = NULL;
253         DB_TXN          *ltid = NULL;
254         Attribute       *csn_a;
255         char            gid[DB_XIDDATASIZE];
256         char            csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
257         int                     num_retries = 0;
258         int                     ctxcsn_added = 0;
259         int                     rc;
260         struct sync_cookie syncCookie = { NULL, -1, NULL};
261         syncinfo_t      *si;
262         u_int32_t       ctxcsn_locker = 0;
263
264         if ( op->o_sync_mode != SLAP_SYNC_NONE &&
265                  !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
266                 char substr[67];
267                 struct berval ctxcsn_ndn = BER_BVNULL;
268                 struct berval bv;
269
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 );
274
275 consumer_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 DB_LOCK_DEADLOCK:
280                         case DB_LOCK_NOTGRANTED:
281                                 goto consumer_ctxcsn_retry;
282                         case 0:
283                                 op->o_tmpfree( ctxcsn_ndn.bv_val, op->o_tmpmemctx );
284                                 ctxcsn_ndn.bv_val = NULL;
285                                 if ( ctxcsn_ei ) {
286                                         ctxcsn_e = ctxcsn_ei->bei_e;
287                                 }
288                                 break;
289                         case DB_NOTFOUND:
290                         default:
291                                 rs->sr_err = LDAP_OTHER;
292                         case LDAP_BUSY:
293                                 op->o_tmpfree( ctxcsn_ndn.bv_val, op->o_tmpmemctx );
294                                 ctxcsn_ndn.bv_val = NULL;
295                                 goto done;
296                         }
297
298                         if ( ctxcsn_e ) {
299                                 csn_a = attr_find( ctxcsn_e->e_attrs,
300                                                         slap_schema.si_ad_syncreplCookie );
301                                 if ( csn_a ) {
302                                         struct berval cookie;
303                                         const char *text;
304                                         int match = -1;
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 )
310                                         {
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 );
315                                         }
316                                         if ( match < 0 ) {
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 );
322                                                 }
323                                                 *search_context_csn = ber_dupbv( NULL,
324                                                         syncCookie.ctxcsn );
325                                         }
326                                         slap_sync_cookie_free( &syncCookie, 0 );
327                                 } else {
328                                         *search_context_csn = NULL;
329                                 } 
330                         } else {
331                                 *search_context_csn = NULL;
332                         }
333                 }
334         } else if ( op->o_sync_mode != SLAP_SYNC_NONE &&
335                  LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
336
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 );
340                 switch(rs->sr_err) {
341                 case 0:
342                         if ( ctxcsn_ei ) {
343                                 ctxcsn_e = ctxcsn_ei->bei_e;
344                         }
345                         break;
346                 case LDAP_BUSY:
347                         goto done;
348                 case DB_LOCK_DEADLOCK:
349                 case DB_LOCK_NOTGRANTED:
350                         goto provider_ctxcsn_retry;
351                 case DB_NOTFOUND:
352                         snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
353                                 bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
354
355                         slap_get_csn( op, csnbuf, sizeof(csnbuf), &csn, 1 );
356
357                         if ( 0 ) {
358 txn_retry:
359                                 rs->sr_err = TXN_ABORT( ltid );
360                                 ltid = NULL;
361                                 if ( rs->sr_err != 0 ) {
362                                         rs->sr_err = LDAP_OTHER;
363                                         goto done;
364                                 }
365                                 ldap_pvt_thread_yield();
366                                 bdb_trans_backoff( ++num_retries );
367                         }
368                         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL,
369                                                                 &ltid, bdb->bi_db_opflags );
370                         if ( rs->sr_err != 0 ) {
371                                 rs->sr_err = LDAP_OTHER;
372                                 goto done;
373                         }
374
375                         ctxcsn_locker = TXN_ID ( ltid );
376
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 ) {
381                         case BDB_CSN_ABORT:
382                                 rs->sr_err = LDAP_OTHER;
383                                 goto done;      
384                         case BDB_CSN_RETRY:
385                                 goto txn_retry;
386                         }
387
388                         rs->sr_err = TXN_PREPARE( ltid, gid );
389                         if ( rs->sr_err != 0 ) {
390                                 rs->sr_err = LDAP_OTHER;
391                                 goto done;
392                         }
393
394                         bdb_cache_add( bdb, suffix_ei, ctxcsn_e,
395                                         (struct berval *)&slap_ldapsync_cn_bv, ctxcsn_locker );
396
397                         rs->sr_err = TXN_COMMIT( ltid, 0 );
398                         if ( rs->sr_err != 0 ) {
399                                 rs->sr_err = LDAP_OTHER;
400                                 goto done;
401                         }
402
403                         rs->sr_err = bdb_dn2entry( op, NULL,
404                                                 &op->o_bd->be_context_csn,
405                                                 &ctxcsn_ei, 0, ctxcsn_locker,
406                                                 ctxcsn_lock );
407
408                         if ( ctxcsn_ei ) {
409                                 ctxcsn_e = ctxcsn_ei->bei_e;
410                         }
411                         break;
412
413                 default:
414                         rs->sr_err = LDAP_OTHER;
415                         goto done;
416                 }
417
418                 if ( ctxcsn_e ) {
419                         csn_a = attr_find( ctxcsn_e->e_attrs,
420                                                 slap_schema.si_ad_contextCSN );
421                         if ( csn_a ) {
422                                 *search_context_csn = ber_dupbv( NULL, &csn_a->a_vals[0] );
423                         } else {
424                                 *search_context_csn = NULL;
425                         }
426                 } else {
427                         *search_context_csn = NULL;
428                 }
429         }
430
431         ltid = NULL;
432         rs->sr_err = LDAP_SUCCESS;
433
434 done:
435     if( ltid != NULL ) {
436         TXN_ABORT( ltid );
437     }
438
439         return rs->sr_err;
440 }