]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/ctxcsn.c
CSN, LDAPsync and related fixes
[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         switch( rc ) {
62         case 0:
63         case DB_NOTFOUND:
64                 break;
65         case DB_LOCK_DEADLOCK:
66         case DB_LOCK_NOTGRANTED:
67                 return BDB_CSN_RETRY;
68         default:
69                 return BDB_CSN_ABORT;
70         }
71         
72         *ctxcsn_e = ctxcsn_ei->bei_e;
73
74         slap_get_commit_csn( op, &max_committed_csn );
75
76         if ( max_committed_csn.bv_val == NULL ) {
77                 return BDB_CSN_COMMIT;
78         }
79
80         *ctxcsn_added = 0;
81
82         switch( rc ) {
83         case 0:
84                 if ( !*ctxcsn_e ) {
85                         rs->sr_err = LDAP_OTHER;
86                         rs->sr_text = "context csn not present";
87                         op->o_tmpfree( max_committed_csn.bv_val, op->o_tmpmemctx );
88                         return BDB_CSN_ABORT;
89                 } else {
90                         Modifications mod;
91                         struct berval modvals[2];
92                         Entry dummy;
93
94                         modvals[0] = max_committed_csn;
95                         modvals[1].bv_val = NULL;
96                         modvals[1].bv_len = 0;
97
98                         mod.sml_op = LDAP_MOD_REPLACE;
99                         mod.sml_values = modvals;
100                         mod.sml_nvalues = NULL;
101                         mod.sml_desc = slap_schema.si_ad_contextCSN;
102                         mod.sml_type = mod.sml_desc->ad_cname;
103                         mod.sml_next = NULL;
104
105                         dummy = **ctxcsn_e;
106                         ret = bdb_modify_internal( op, tid, &mod, &dummy,
107                                                                         &rs->sr_text, textbuf, textlen );                                                      
108                         op->o_tmpfree( max_committed_csn.bv_val, op->o_tmpmemctx );
109                         if ( ret != LDAP_SUCCESS ) {
110 #ifdef NEW_LOGGING
111                                 LDAP_LOG ( OPERATION, ERR,
112                                                 "bdb_csn_commit: modify failed (%d)\n", rs->sr_err, 0, 0 );
113 #else
114                                 Debug( LDAP_DEBUG_TRACE,
115                                                 "bdb_csn_commit: modify failed (%d)\n", rs->sr_err, 0, 0 );
116 #endif
117                                 switch( ret ) {
118                                 case DB_LOCK_DEADLOCK:
119                                 case DB_LOCK_NOTGRANTED:
120                                         goto rewind;
121                                 default:
122                                         return BDB_CSN_ABORT;
123                                 }
124                         }
125
126                         ret = bdb_id2entry_update( op->o_bd, tid, &dummy );
127                         switch ( ret ) {
128                         case 0 :
129                                 break;
130                         case DB_LOCK_DEADLOCK :
131                         case DB_LOCK_NOTGRANTED :
132                                 goto rewind;
133                         default :
134                                 rs->sr_err = ret;
135                                 rs->sr_text = "context csn update failed";
136                                 return BDB_CSN_ABORT;
137                         }
138                         bdb_cache_modify( *ctxcsn_e, dummy.e_attrs, bdb->bi_dbenv, locker, &ctxcsn_lock );
139                 }
140                 break;
141         case DB_NOTFOUND:
142                 if ( op->o_tag == LDAP_REQ_ADD &&
143                                                 be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname )) {
144                         *suffix_ei = NULL;
145                         eip = (EntryInfo *) ch_calloc( 1, sizeof( EntryInfo ));
146                         eip->bei_id = op->oq_add.rs_e->e_id;
147                 } else {
148                         eip = *suffix_ei = ctxcsn_ei;
149                 }
150
151                 /* This serializes add. But this case is very rare : only once. */
152                 rs->sr_err = bdb_next_id( op->o_bd, tid, &ctxcsn_id );
153                 if ( rs->sr_err != 0 ) {
154 #ifdef NEW_LOGGING
155                         LDAP_LOG ( OPERATION, ERR,
156                                 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
157 #else
158                         Debug( LDAP_DEBUG_TRACE,
159                                 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
160 #endif
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 #ifdef NEW_LOGGING
220                 LDAP_LOG( OPERATION, ERR,
221                                 "bdb_csn_commit : bdb_dn2entry retry\n", 0, 0, 0 );
222 #else
223                 Debug( LDAP_DEBUG_TRACE,
224                                 "bdb_csn_commit : bdb_dn2entry retry\n", 0, 0, 0 );
225 #endif
226                 goto rewind;
227         case LDAP_BUSY:
228                 rs->sr_err = rc;
229                 rs->sr_text = "ldap server busy";
230                 return BDB_CSN_ABORT;
231         default:
232                 rs->sr_err = LDAP_OTHER;
233                 rs->sr_text = "internal error";
234                 return BDB_CSN_ABORT;
235         }
236
237         return BDB_CSN_COMMIT;
238
239 rewind :
240         slap_rewind_commit_csn( op );
241         return BDB_CSN_RETRY;
242 }
243
244 int
245 bdb_get_commit_csn(
246         Operation       *op,
247         SlapReply       *rs,
248         struct berval   **search_context_csn,
249         u_int32_t       locker,
250         DB_LOCK         *ctxcsn_lock
251 )
252 {
253         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
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         struct sync_cookie syncCookie = { NULL, -1, NULL};
266         syncinfo_t      *si;
267         u_int32_t       ctxcsn_locker = 0;
268
269         if ( op->o_sync_mode != SLAP_SYNC_NONE &&
270                  !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
271                 char substr[67];
272                 struct berval ctxcsn_ndn = BER_BVNULL;
273                 struct berval bv;
274
275                 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
276                         sprintf( substr, "cn=syncrepl%ld", si->si_rid );
277                         ber_str2bv( substr, 0, 0, &bv );
278                         build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0], &bv, op->o_tmpmemctx );
279
280 consumer_ctxcsn_retry :
281                         rs->sr_err = bdb_dn2entry( op, NULL, &ctxcsn_ndn, &ctxcsn_ei,
282                                                                                 0, locker, ctxcsn_lock );
283                         switch(rs->sr_err) {
284                         case DB_LOCK_DEADLOCK:
285                         case DB_LOCK_NOTGRANTED:
286                                 goto consumer_ctxcsn_retry;
287                         case 0:
288                                 op->o_tmpfree( ctxcsn_ndn.bv_val, op->o_tmpmemctx );
289                                 ctxcsn_ndn.bv_val = NULL;
290                                 if ( ctxcsn_ei ) {
291                                         ctxcsn_e = ctxcsn_ei->bei_e;
292                                 }
293                                 break;
294                         case DB_NOTFOUND:
295                         default:
296                                 rs->sr_err = LDAP_OTHER;
297                         case LDAP_BUSY:
298                                 op->o_tmpfree( ctxcsn_ndn.bv_val, op->o_tmpmemctx );
299                                 ctxcsn_ndn.bv_val = NULL;
300                                 goto done;
301                         }
302
303                         if ( ctxcsn_e ) {
304                                 csn_a = attr_find( ctxcsn_e->e_attrs,
305                                                         slap_schema.si_ad_syncreplCookie );
306                                 if ( csn_a ) {
307                                         struct berval cookie;
308                                         const char *text;
309                                         int match = -1;
310                                         ber_dupbv( &cookie, &csn_a->a_vals[0] );
311                                         ber_bvarray_add( &syncCookie.octet_str, &cookie );
312                                         slap_parse_sync_cookie( &syncCookie );
313                                         if ( *search_context_csn &&
314                                                 (*search_context_csn)->bv_val != NULL )
315                                         {
316                                                 value_match( &match, slap_schema.si_ad_entryCSN,
317                                                         slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
318                                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
319                                                         syncCookie.ctxcsn, *search_context_csn, &text );
320                                         }
321                                         if ( match < 0 ) {
322                                                 /* set search_context_csn to the
323                                                    smallest syncrepl cookie value */
324                                                 if ( *search_context_csn ) {
325                                                         ch_free( (*search_context_csn)->bv_val );
326                                                         ch_free( *search_context_csn );
327                                                 }
328                                                 *search_context_csn = ber_dupbv( NULL,
329                                                         syncCookie.ctxcsn );
330                                         }
331                                         slap_sync_cookie_free( &syncCookie, 0 );
332                                 } else {
333                                         *search_context_csn = NULL;
334                                 } 
335                         } else {
336                                 *search_context_csn = NULL;
337                         }
338                 }
339         } else if ( op->o_sync_mode != SLAP_SYNC_NONE &&
340                  LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
341
342 provider_ctxcsn_retry :
343                 rs->sr_err = bdb_dn2entry( op, NULL, &op->o_bd->be_context_csn, &ctxcsn_ei,
344                                                                         0, locker, ctxcsn_lock );
345                 switch(rs->sr_err) {
346                 case 0:
347                         if ( ctxcsn_ei ) {
348                                 ctxcsn_e = ctxcsn_ei->bei_e;
349                         }
350                         break;
351                 case LDAP_BUSY:
352                         goto done;
353                 case DB_LOCK_DEADLOCK:
354                 case DB_LOCK_NOTGRANTED:
355                         goto provider_ctxcsn_retry;
356                 case DB_NOTFOUND:
357                         snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
358                                 bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
359
360                         slap_get_csn( op, csnbuf, sizeof(csnbuf), &csn, 1 );
361
362                         if ( 0 ) {
363 txn_retry:
364                                 rs->sr_err = TXN_ABORT( ltid );
365                                 ltid = NULL;
366                                 if ( rs->sr_err != 0 ) {
367                                         rs->sr_err = LDAP_OTHER;
368                                         goto done;
369                                 }
370                                 ldap_pvt_thread_yield();
371                                 bdb_trans_backoff( ++num_retries );
372                         }
373                         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL,
374                                                                 &ltid, bdb->bi_db_opflags );
375                         if ( rs->sr_err != 0 ) {
376                                 rs->sr_err = LDAP_OTHER;
377                                 goto done;
378                         }
379
380                         ctxcsn_locker = TXN_ID ( ltid );
381
382                         rs->sr_err = bdb_csn_commit( op, rs, ltid, NULL, &suffix_ei,
383                                                                         &ctxcsn_e, &ctxcsn_added, ctxcsn_locker );
384                         switch( rs->sr_err ) {
385                         case BDB_CSN_ABORT:
386                                 rs->sr_err = LDAP_OTHER;
387                                 goto done;      
388                         case BDB_CSN_RETRY:
389                                 goto txn_retry;
390                         }
391
392                         rs->sr_err = TXN_PREPARE( ltid, gid );
393                         if ( rs->sr_err != 0 ) {
394                                 rs->sr_err = LDAP_OTHER;
395                                 goto done;
396                         }
397
398                         bdb_cache_add( bdb, suffix_ei, ctxcsn_e,
399                                         (struct berval *)&slap_ldapsync_cn_bv, ctxcsn_locker );
400
401                         rs->sr_err = TXN_COMMIT( ltid, 0 );
402                         if ( rs->sr_err != 0 ) {
403                                 rs->sr_err = LDAP_OTHER;
404                                 goto done;
405                         }
406
407                         rs->sr_err = bdb_dn2entry( op, NULL, &op->o_bd->be_context_csn, &ctxcsn_ei,
408                                     0, ctxcsn_locker, ctxcsn_lock );
409
410                         if ( ctxcsn_ei ) {
411                                 ctxcsn_e = ctxcsn_ei->bei_e;
412                         }
413                         break;
414
415                 default:
416                         rs->sr_err = LDAP_OTHER;
417                         goto done;
418                 }
419
420                 if ( ctxcsn_e ) {
421                         csn_a = attr_find( ctxcsn_e->e_attrs,
422                                                 slap_schema.si_ad_contextCSN );
423                         if ( csn_a ) {
424                                 *search_context_csn = ber_dupbv( NULL, &csn_a->a_vals[0] );
425                         } else {
426                                 *search_context_csn = NULL;
427                         }
428                 } else {
429                         *search_context_csn = NULL;
430                 }
431         }
432
433         ltid = NULL;
434         rs->sr_err = LDAP_SUCCESS;
435
436 done:
437     if( ltid != NULL ) {
438         TXN_ABORT( ltid );
439     }
440
441         return rs->sr_err;
442 }