]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/ctxcsn.c
dbb64928966a0b82b33465a1eca6b10f9d5e87da
[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                                 Debug( LDAP_DEBUG_TRACE,
111                                                 "bdb_csn_commit: modify failed (%d)\n", rs->sr_err, 0, 0 );
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                                 goto rewind;
128                         default :
129                                 rs->sr_err = ret;
130                                 rs->sr_text = "context csn update failed";
131                                 return BDB_CSN_ABORT;
132                         }
133                         bdb_cache_modify( *ctxcsn_e, dummy.e_attrs, bdb->bi_dbenv, locker, &ctxcsn_lock );
134                 }
135                 break;
136         case DB_NOTFOUND:
137                 if ( op->o_tag == LDAP_REQ_ADD &&
138                                                 be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname )) {
139                         *suffix_ei = NULL;
140                         eip = (EntryInfo *) ch_calloc( 1, sizeof( EntryInfo ));
141                         eip->bei_id = op->oq_add.rs_e->e_id;
142                 } else {
143                         eip = *suffix_ei = ctxcsn_ei;
144                 }
145
146                 /* This serializes add. But this case is very rare : only once. */
147                 rs->sr_err = bdb_next_id( op->o_bd, tid, &ctxcsn_id );
148                 if ( rs->sr_err != 0 ) {
149                         Debug( LDAP_DEBUG_TRACE,
150                                 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
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                 Debug( LDAP_DEBUG_TRACE,
210                                 "bdb_csn_commit : bdb_dn2entry retry\n", 0, 0, 0 );
211                 goto rewind;
212         case LDAP_BUSY:
213                 rs->sr_err = rc;
214                 rs->sr_text = "ldap server busy";
215                 return BDB_CSN_ABORT;
216         default:
217                 rs->sr_err = LDAP_OTHER;
218                 rs->sr_text = "internal error";
219                 return BDB_CSN_ABORT;
220         }
221
222         return BDB_CSN_COMMIT;
223
224 rewind :
225         slap_rewind_commit_csn( op );
226         return BDB_CSN_RETRY;
227 }
228
229 int
230 bdb_get_commit_csn(
231         Operation       *op,
232         SlapReply       *rs,
233         struct berval   **search_context_csn,
234         u_int32_t       locker,
235         DB_LOCK         *ctxcsn_lock
236 )
237 {
238         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
239         struct berval csn = BER_BVNULL;
240         EntryInfo       *ctxcsn_ei = NULL;
241         EntryInfo       *suffix_ei = NULL;
242         Entry           *ctxcsn_e = NULL;
243         DB_TXN          *ltid = NULL;
244         Attribute       *csn_a;
245         char            gid[DB_XIDDATASIZE];
246         char            csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
247         int                     num_retries = 0;
248         int                     ctxcsn_added = 0;
249         int                     rc;
250         struct sync_cookie syncCookie = { NULL, -1, NULL};
251         syncinfo_t      *si;
252         u_int32_t       ctxcsn_locker = 0;
253
254         if ( op->o_sync_mode != SLAP_SYNC_NONE &&
255                  !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
256                 char substr[67];
257                 struct berval ctxcsn_ndn = BER_BVNULL;
258                 struct berval bv;
259
260                 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
261                         sprintf( substr, "cn=syncrepl%ld", si->si_rid );
262                         ber_str2bv( substr, 0, 0, &bv );
263                         build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0], &bv, op->o_tmpmemctx );
264
265 consumer_ctxcsn_retry :
266                         rs->sr_err = bdb_dn2entry( op, NULL, &ctxcsn_ndn, &ctxcsn_ei,
267                                                                                 0, locker, ctxcsn_lock );
268                         switch(rs->sr_err) {
269                         case DB_LOCK_DEADLOCK:
270                         case DB_LOCK_NOTGRANTED:
271                                 goto consumer_ctxcsn_retry;
272                         case 0:
273                                 op->o_tmpfree( ctxcsn_ndn.bv_val, op->o_tmpmemctx );
274                                 ctxcsn_ndn.bv_val = NULL;
275                                 if ( ctxcsn_ei ) {
276                                         ctxcsn_e = ctxcsn_ei->bei_e;
277                                 }
278                                 break;
279                         case DB_NOTFOUND:
280                         default:
281                                 rs->sr_err = LDAP_OTHER;
282                         case LDAP_BUSY:
283                                 op->o_tmpfree( ctxcsn_ndn.bv_val, op->o_tmpmemctx );
284                                 ctxcsn_ndn.bv_val = NULL;
285                                 goto done;
286                         }
287
288                         if ( ctxcsn_e ) {
289                                 csn_a = attr_find( ctxcsn_e->e_attrs,
290                                                         slap_schema.si_ad_syncreplCookie );
291                                 if ( csn_a ) {
292                                         struct berval cookie;
293                                         const char *text;
294                                         int match = -1;
295                                         ber_dupbv( &cookie, &csn_a->a_vals[0] );
296                                         ber_bvarray_add( &syncCookie.octet_str, &cookie );
297                                         slap_parse_sync_cookie( &syncCookie );
298                                         if ( *search_context_csn &&
299                                                 (*search_context_csn)->bv_val != NULL )
300                                         {
301                                                 value_match( &match, slap_schema.si_ad_entryCSN,
302                                                         slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
303                                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
304                                                         syncCookie.ctxcsn, *search_context_csn, &text );
305                                         }
306                                         if ( match < 0 ) {
307                                                 /* set search_context_csn to the
308                                                    smallest syncrepl cookie value */
309                                                 if ( *search_context_csn ) {
310                                                         ch_free( (*search_context_csn)->bv_val );
311                                                         ch_free( *search_context_csn );
312                                                 }
313                                                 *search_context_csn = ber_dupbv( NULL,
314                                                         syncCookie.ctxcsn );
315                                         }
316                                         slap_sync_cookie_free( &syncCookie, 0 );
317                                 } else {
318                                         *search_context_csn = NULL;
319                                 } 
320                         } else {
321                                 *search_context_csn = NULL;
322                         }
323                 }
324         } else if ( op->o_sync_mode != SLAP_SYNC_NONE &&
325                  LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
326
327 provider_ctxcsn_retry :
328                 rs->sr_err = bdb_dn2entry( op, NULL, &op->o_bd->be_context_csn, &ctxcsn_ei,
329                                                                         0, locker, ctxcsn_lock );
330                 switch(rs->sr_err) {
331                 case 0:
332                         if ( ctxcsn_ei ) {
333                                 ctxcsn_e = ctxcsn_ei->bei_e;
334                         }
335                         break;
336                 case LDAP_BUSY:
337                         goto done;
338                 case DB_LOCK_DEADLOCK:
339                 case DB_LOCK_NOTGRANTED:
340                         goto provider_ctxcsn_retry;
341                 case DB_NOTFOUND:
342                         snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
343                                 bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
344
345                         slap_get_csn( op, csnbuf, sizeof(csnbuf), &csn, 1 );
346
347                         if ( 0 ) {
348 txn_retry:
349                                 rs->sr_err = TXN_ABORT( ltid );
350                                 ltid = NULL;
351                                 if ( rs->sr_err != 0 ) {
352                                         rs->sr_err = LDAP_OTHER;
353                                         goto done;
354                                 }
355                                 ldap_pvt_thread_yield();
356                                 bdb_trans_backoff( ++num_retries );
357                         }
358                         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL,
359                                                                 &ltid, bdb->bi_db_opflags );
360                         if ( rs->sr_err != 0 ) {
361                                 rs->sr_err = LDAP_OTHER;
362                                 goto done;
363                         }
364
365                         ctxcsn_locker = TXN_ID ( ltid );
366
367                         rs->sr_err = bdb_csn_commit( op, rs, ltid, NULL, &suffix_ei,
368                                                                         &ctxcsn_e, &ctxcsn_added, ctxcsn_locker );
369                         switch( rs->sr_err ) {
370                         case BDB_CSN_ABORT:
371                                 rs->sr_err = LDAP_OTHER;
372                                 goto done;      
373                         case BDB_CSN_RETRY:
374                                 goto txn_retry;
375                         }
376
377                         rs->sr_err = TXN_PREPARE( ltid, gid );
378                         if ( rs->sr_err != 0 ) {
379                                 rs->sr_err = LDAP_OTHER;
380                                 goto done;
381                         }
382
383                         bdb_cache_add( bdb, suffix_ei, ctxcsn_e,
384                                         (struct berval *)&slap_ldapsync_cn_bv, ctxcsn_locker );
385
386                         rs->sr_err = TXN_COMMIT( ltid, 0 );
387                         if ( rs->sr_err != 0 ) {
388                                 rs->sr_err = LDAP_OTHER;
389                                 goto done;
390                         }
391
392                         rs->sr_err = bdb_dn2entry( op, NULL, &op->o_bd->be_context_csn, &ctxcsn_ei,
393                                     0, ctxcsn_locker, ctxcsn_lock );
394
395                         if ( ctxcsn_ei ) {
396                                 ctxcsn_e = ctxcsn_ei->bei_e;
397                         }
398                         break;
399
400                 default:
401                         rs->sr_err = LDAP_OTHER;
402                         goto done;
403                 }
404
405                 if ( ctxcsn_e ) {
406                         csn_a = attr_find( ctxcsn_e->e_attrs,
407                                                 slap_schema.si_ad_contextCSN );
408                         if ( csn_a ) {
409                                 *search_context_csn = ber_dupbv( NULL, &csn_a->a_vals[0] );
410                         } else {
411                                 *search_context_csn = NULL;
412                         }
413                 } else {
414                         *search_context_csn = NULL;
415                 }
416         }
417
418         ltid = NULL;
419         rs->sr_err = LDAP_SUCCESS;
420
421 done:
422     if( ltid != NULL ) {
423         TXN_ABORT( ltid );
424     }
425
426         return rs->sr_err;
427 }