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