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