]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/ctxcsn.c
make sure we only get here when everything is fine
[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 #include "external.h"
28
29 int
30 bdb_csn_commit(
31         Operation *op,
32         SlapReply *rs,
33         DB_TXN *tid,
34         EntryInfo *ei,
35         EntryInfo **suffix_ei,
36         Entry **ctxcsn_e,
37         int *ctxcsn_added,
38         u_int32_t locker
39 )
40 {
41         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
42         EntryInfo               *ctxcsn_ei = NULL;
43         DB_LOCK                 ctxcsn_lock;
44         struct berval   max_committed_csn;
45         DB_LOCK                 suffix_lock;
46         int                             rc, ret;
47         ID                              ctxcsn_id;
48         Entry                   *e;
49         char                    textbuf[SLAP_TEXT_BUFLEN];
50         size_t                  textlen = sizeof textbuf;
51         EntryInfo               *eip = NULL;
52
53         assert( !BER_BVISNULL( &op->o_bd->be_context_csn ) );
54
55         if ( ei ) {
56                 e = ei->bei_e;
57         }
58
59         rc =  bdb_dn2entry( op, tid, &op->o_bd->be_context_csn, &ctxcsn_ei,
60                         1, locker, &ctxcsn_lock );
61         
62         *ctxcsn_e = ctxcsn_ei->bei_e;
63
64         slap_get_commit_csn( op, &max_committed_csn );
65
66         if ( max_committed_csn.bv_val == NULL ) {
67                 return BDB_CSN_COMMIT;
68         }
69
70         *ctxcsn_added = 0;
71
72         switch( rc ) {
73         case 0:
74                 if ( !*ctxcsn_e ) {
75                         rs->sr_err = LDAP_OTHER;
76                         rs->sr_text = "context csn not present";
77                         op->o_tmpfree( max_committed_csn.bv_val, op->o_tmpmemctx );
78                         return BDB_CSN_ABORT;
79                 } else {
80                         Modifications mod;
81                         struct berval modvals[2];
82                         Entry dummy;
83
84                         modvals[0] = max_committed_csn;
85                         modvals[1].bv_val = NULL;
86                         modvals[1].bv_len = 0;
87
88                         mod.sml_op = LDAP_MOD_REPLACE;
89                         mod.sml_values = modvals;
90                         mod.sml_nvalues = NULL;
91                         mod.sml_desc = slap_schema.si_ad_contextCSN;
92                         mod.sml_type = mod.sml_desc->ad_cname;
93                         mod.sml_next = NULL;
94
95                         dummy = **ctxcsn_e;
96                         ret = bdb_modify_internal( op, tid, &mod, &dummy,
97                                                                         &rs->sr_text, textbuf, textlen );                                                      
98                         op->o_tmpfree( max_committed_csn.bv_val, op->o_tmpmemctx );
99                         if ( ret != LDAP_SUCCESS ) {
100 #ifdef NEW_LOGGING
101                                 LDAP_LOG ( OPERATION, ERR,
102                                                 "bdb_csn_commit: modify failed (%d)\n", rs->sr_err, 0, 0 );
103 #else
104                                 Debug( LDAP_DEBUG_TRACE,
105                                                 "bdb_csn_commit: modify failed (%d)\n", rs->sr_err, 0, 0 );
106 #endif
107                                 switch( ret ) {
108                                 case DB_LOCK_DEADLOCK:
109                                 case DB_LOCK_NOTGRANTED:
110                                         goto rewind;
111                                 default:
112                                         return BDB_CSN_ABORT;
113                                 }
114                         }
115
116                         ret = bdb_id2entry_update( op->o_bd, tid, &dummy );
117                         switch ( ret ) {
118                         case 0 :
119                                 break;
120                         case DB_LOCK_DEADLOCK :
121                         case DB_LOCK_NOTGRANTED :
122                                 goto rewind;
123                         default :
124                                 rs->sr_err = ret;
125                                 rs->sr_text = "context csn update failed";
126                                 return BDB_CSN_ABORT;
127                         }
128                         bdb_cache_modify( *ctxcsn_e, dummy.e_attrs, bdb->bi_dbenv, locker, &ctxcsn_lock );
129                 }
130                 break;
131         case DB_NOTFOUND:
132                 if ( op->o_tag == LDAP_REQ_ADD &&
133                                                 be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname )) {
134                         *suffix_ei = NULL;
135                         eip = (EntryInfo *) ch_calloc( 1, sizeof( EntryInfo ));
136                         eip->bei_id = op->oq_add.rs_e->e_id;
137                 } else {
138                         eip = *suffix_ei = ctxcsn_ei;
139                 }
140
141                 /* This serializes add. But this case is very rare : only once. */
142                 rs->sr_err = bdb_next_id( op->o_bd, tid, &ctxcsn_id );
143                 if ( rs->sr_err != 0 ) {
144 #ifdef NEW_LOGGING
145                         LDAP_LOG ( OPERATION, ERR,
146                                 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
147 #else
148                         Debug( LDAP_DEBUG_TRACE,
149                                 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
150 #endif
151                         rs->sr_err = LDAP_OTHER;
152                         rs->sr_text = "internal error";
153                         return BDB_CSN_ABORT;
154                 }
155
156                 *ctxcsn_e = slap_create_context_csn_entry( op->o_bd, &max_committed_csn );
157                 op->o_tmpfree( max_committed_csn.bv_val, op->o_tmpmemctx );
158                 (*ctxcsn_e)->e_id = ctxcsn_id;
159                 *ctxcsn_added = 1;
160
161                 ret = bdb_dn2id_add( op, tid, eip, *ctxcsn_e );
162                 switch ( ret ) {
163                 case 0 :
164                         break;
165                 case DB_LOCK_DEADLOCK :
166                 case DB_LOCK_NOTGRANTED :
167                         goto rewind;
168                 case DB_KEYEXIST :
169                         rs->sr_err = LDAP_OTHER;
170                         rs->sr_text = "context csn exists before context prefix does";
171                         return BDB_CSN_ABORT;
172                 default :
173                         rs->sr_err = LDAP_OTHER;
174                         rs->sr_text = "context csn store failed";
175                         return BDB_CSN_ABORT;
176                 }
177
178                 if ( *suffix_ei == NULL ) {
179                         ch_free( eip );
180                 }
181
182                 ret = bdb_id2entry_add( op->o_bd, tid, *ctxcsn_e );
183                 switch ( ret ) {
184                 case 0 :
185                         break;
186                 case DB_LOCK_DEADLOCK :
187                 case DB_LOCK_NOTGRANTED :
188                         goto rewind;
189                 default :
190                         rs->sr_err = LDAP_OTHER;
191                         rs->sr_text = "context csn store failed";
192                         return BDB_CSN_ABORT;
193                 }
194                 ret = bdb_index_entry_add( op, tid, *ctxcsn_e );
195                 switch ( ret ) {
196                 case 0 :
197                         break;
198                 case DB_LOCK_DEADLOCK :
199                 case DB_LOCK_NOTGRANTED :
200                         goto rewind;
201                 default :
202                         rs->sr_err = LDAP_OTHER;
203                         rs->sr_text = "context csn indexing failed";
204                         return BDB_CSN_ABORT;
205                 }
206                 break;
207         case DB_LOCK_DEADLOCK:
208         case DB_LOCK_NOTGRANTED:
209 #ifdef NEW_LOGGING
210                 LDAP_LOG( OPERATION, ERR,
211                                 "bdb_csn_commit : bdb_dn2entry retry\n", 0, 0, 0 );
212 #else
213                 Debug( LDAP_DEBUG_TRACE,
214                                 "bdb_csn_commit : bdb_dn2entry retry\n", 0, 0, 0 );
215 #endif
216                 goto rewind;
217         case LDAP_BUSY:
218                 rs->sr_err = rc;
219                 rs->sr_text = "ldap server busy";
220                 return BDB_CSN_ABORT;
221         default:
222                 rs->sr_err = LDAP_OTHER;
223                 rs->sr_text = "internal error";
224                 return BDB_CSN_ABORT;
225         }
226
227         return BDB_CSN_COMMIT;
228
229 rewind :
230         slap_rewind_commit_csn( op );
231         return BDB_CSN_RETRY;
232 }
233
234 int
235 bdb_get_commit_csn(
236         Operation       *op,
237         SlapReply       *rs,
238         struct berval   **search_context_csn,
239         u_int32_t       locker,
240         DB_LOCK         *ctxcsn_lock
241 )
242 {
243         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
244         struct berval csn = BER_BVNULL;
245         EntryInfo       *ctxcsn_ei = NULL;
246         EntryInfo       *suffix_ei = NULL;
247         Entry           *ctxcsn_e = NULL;
248         DB_TXN          *ltid = NULL;
249         Attribute       *csn_a;
250         char            gid[DB_XIDDATASIZE];
251         char            csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
252         int                     num_retries = 0;
253         int                     ctxcsn_added = 0;
254         int                     rc;
255         struct sync_cookie syncCookie = { NULL, -1, NULL};
256         syncinfo_t      *si;
257         u_int32_t       ctxcsn_locker = 0;
258
259         if ( op->o_sync_mode != SLAP_SYNC_NONE &&
260                  !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
261                 char substr[67];
262                 struct berval ctxcsn_ndn = BER_BVNULL;
263                 struct berval bv;
264
265                 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
266                         sprintf( substr, "cn=syncrepl%ld", si->si_rid );
267                         ber_str2bv( substr, 0, 0, &bv );
268                         build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0], &bv, op->o_tmpmemctx );
269
270 consumer_ctxcsn_retry :
271                         rs->sr_err = bdb_dn2entry( op, NULL, &ctxcsn_ndn, &ctxcsn_ei,
272                                                                                 0, locker, ctxcsn_lock );
273                         switch(rs->sr_err) {
274                         case DB_LOCK_DEADLOCK:
275                         case DB_LOCK_NOTGRANTED:
276                                 goto consumer_ctxcsn_retry;
277                         case 0:
278                                 op->o_tmpfree( ctxcsn_ndn.bv_val, op->o_tmpmemctx );
279                                 ctxcsn_ndn.bv_val = NULL;
280                                 if ( ctxcsn_ei ) {
281                                         ctxcsn_e = ctxcsn_ei->bei_e;
282                                 }
283                                 break;
284                         case DB_NOTFOUND:
285                         default:
286                                 rs->sr_err = LDAP_OTHER;
287                         case LDAP_BUSY:
288                                 op->o_tmpfree( ctxcsn_ndn.bv_val, op->o_tmpmemctx );
289                                 ctxcsn_ndn.bv_val = NULL;
290                                 goto done;
291                         }
292
293                         if ( ctxcsn_e ) {
294                                 csn_a = attr_find( ctxcsn_e->e_attrs,
295                                                         slap_schema.si_ad_syncreplCookie );
296                                 if ( csn_a ) {
297                                         struct berval cookie;
298                                         const char *text;
299                                         int match = -1;
300                                         ber_dupbv( &cookie, &csn_a->a_vals[0] );
301                                         ber_bvarray_add( &syncCookie.octet_str, &cookie );
302                                         slap_parse_sync_cookie( &syncCookie );
303                                         if ( *search_context_csn &&
304                                                 (*search_context_csn)->bv_val != NULL )
305                                         {
306                                                 value_match( &match, slap_schema.si_ad_entryCSN,
307                                                         slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
308                                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
309                                                         syncCookie.ctxcsn, *search_context_csn, &text );
310                                         }
311                                         if ( match < 0 ) {
312                                                 /* set search_context_csn to the
313                                                    smallest syncrepl cookie value */
314                                                 if ( *search_context_csn ) {
315                                                         ch_free( (*search_context_csn)->bv_val );
316                                                         ch_free( *search_context_csn );
317                                                 }
318                                                 *search_context_csn = ber_dupbv( NULL,
319                                                         syncCookie.ctxcsn );
320                                         }
321                                         slap_sync_cookie_free( &syncCookie, 0 );
322                                 } else {
323                                         *search_context_csn = NULL;
324                                 } 
325                         } else {
326                                 *search_context_csn = NULL;
327                         }
328                 }
329         } else if ( op->o_sync_mode != SLAP_SYNC_NONE &&
330                  LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
331
332 provider_ctxcsn_retry :
333                 rs->sr_err = bdb_dn2entry( op, NULL, &op->o_bd->be_context_csn, &ctxcsn_ei,
334                                                                         0, locker, ctxcsn_lock );
335                 switch(rs->sr_err) {
336                 case 0:
337                         if ( ctxcsn_ei ) {
338                                 ctxcsn_e = ctxcsn_ei->bei_e;
339                         }
340                         break;
341                 case LDAP_BUSY:
342                         goto done;
343                 case DB_LOCK_DEADLOCK:
344                 case DB_LOCK_NOTGRANTED:
345                         goto provider_ctxcsn_retry;
346                 case DB_NOTFOUND:
347                         snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
348                                 bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
349
350                         slap_get_csn( op, csnbuf, sizeof(csnbuf), &csn, 1 );
351
352                         if ( 0 ) {
353 txn_retry:
354                                 rs->sr_err = TXN_ABORT( ltid );
355                                 ltid = NULL;
356                                 if ( rs->sr_err != 0 ) {
357                                         rs->sr_err = LDAP_OTHER;
358                                         goto done;
359                                 }
360                                 ldap_pvt_thread_yield();
361                                 bdb_trans_backoff( ++num_retries );
362                         }
363                         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL,
364                                                                 &ltid, bdb->bi_db_opflags );
365                         if ( rs->sr_err != 0 ) {
366                                 rs->sr_err = LDAP_OTHER;
367                                 goto done;
368                         }
369
370                         ctxcsn_locker = TXN_ID ( ltid );
371
372                         rs->sr_err = bdb_csn_commit( op, rs, ltid, NULL, &suffix_ei,
373                                                                         &ctxcsn_e, &ctxcsn_added, ctxcsn_locker );
374                         switch( rs->sr_err ) {
375                         case BDB_CSN_ABORT:
376                                 rs->sr_err = LDAP_OTHER;
377                                 goto done;      
378                         case BDB_CSN_RETRY:
379                                 goto txn_retry;
380                         }
381
382                         rs->sr_err = TXN_PREPARE( ltid, gid );
383                         if ( rs->sr_err != 0 ) {
384                                 rs->sr_err = LDAP_OTHER;
385                                 goto done;
386                         }
387
388                         bdb_cache_add( bdb, suffix_ei, ctxcsn_e,
389                                         (struct berval *)&slap_ldapsync_cn_bv, ctxcsn_locker );
390
391                         rs->sr_err = TXN_COMMIT( ltid, 0 );
392                         if ( rs->sr_err != 0 ) {
393                                 rs->sr_err = LDAP_OTHER;
394                                 goto done;
395                         }
396
397                         rs->sr_err = bdb_dn2entry( op, NULL, &op->o_bd->be_context_csn, &ctxcsn_ei,
398                                     0, ctxcsn_locker, ctxcsn_lock );
399
400                         if ( ctxcsn_ei ) {
401                                 ctxcsn_e = ctxcsn_ei->bei_e;
402                         }
403                         break;
404
405                 default:
406                         rs->sr_err = LDAP_OTHER;
407                         goto done;
408                 }
409
410                 if ( ctxcsn_e ) {
411                         csn_a = attr_find( ctxcsn_e->e_attrs,
412                                                 slap_schema.si_ad_contextCSN );
413                         if ( csn_a ) {
414                                 *search_context_csn = ber_dupbv( NULL, &csn_a->a_vals[0] );
415                         } else {
416                                 *search_context_csn = NULL;
417                         }
418                 } else {
419                         *search_context_csn = NULL;
420                 }
421         }
422
423         ltid = NULL;
424         rs->sr_err = LDAP_SUCCESS;
425
426 done:
427     if( ltid != NULL ) {
428         TXN_ABORT( ltid );
429     }
430
431         return rs->sr_err;
432 }