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