]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/ctxcsn.c
add bdb_get_commit_csn() for use in bdb_search
[openldap] / servers / slapd / back-bdb / ctxcsn.c
1 /* $OpenLDAP$ */
2 /*
3  * back-bdb Context CSN Management Routines
4  */
5 /* Copyright (c) 2003 by International Business Machines, Inc.
6  *
7  * International Business Machines, Inc. (hereinafter called IBM) grants
8  * permission under its copyrights to use, copy, modify, and distribute this
9  * Software with or without fee, provided that the above copyright notice and
10  * all paragraphs of this notice appear in all copies, and that the name of IBM
11  * not be used in connection with the marketing of any product incorporating
12  * the Software or modifications thereof, without specific, written prior
13  * permission.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
16  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
17  * PARTICULAR PURPOSE.  IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
18  * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
19  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
20  * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
21  */
22
23 #include "portable.h"
24
25 #include <stdio.h>
26
27 #include <ac/string.h>
28 #include <ac/time.h>
29
30 #include "lutil.h"
31 #include "back-bdb.h"
32 #include "external.h"
33
34 int
35 bdb_csn_commit(
36         Operation *op,
37         SlapReply *rs,
38         DB_TXN *tid,
39         EntryInfo *ei,
40         EntryInfo **suffix_ei,
41         Entry **ctxcsn_e,
42         int *ctxcsn_added,
43         u_int32_t locker
44 )
45 {
46         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
47         struct berval   ctxcsn_rdn = { 0, NULL };
48         struct berval   ctxcsn_ndn = { 0, NULL };
49         EntryInfo               *ctxcsn_ei = NULL;
50         DB_LOCK                 ctxcsn_lock;
51         struct berval   *max_committed_csn = NULL;
52         DB_LOCK                 suffix_lock;
53         int                             rc, ret;
54         ID                              ctxcsn_id;
55         Entry                   *e;
56         char                    textbuf[SLAP_TEXT_BUFLEN];
57         size_t                  textlen = sizeof textbuf;
58         Modifications   *ml, *mlnext, *mod, *modlist;
59         Modifications   **modtail = &modlist;
60         struct berval   *csnbva = NULL;
61         EntryInfo               *eip = NULL;
62
63         if ( ei ) {
64                 e = ei->bei_e;
65         }
66
67         ber_str2bv( "cn=ldapsync", strlen("cn=ldapsync"), 0, &ctxcsn_rdn );
68         build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0], &ctxcsn_rdn );
69
70         rc = bdb_dn2entry( op, tid, &ctxcsn_ndn, &ctxcsn_ei,
71                                                            1, locker, &ctxcsn_lock );
72
73         *ctxcsn_e = ctxcsn_ei->bei_e;
74
75         max_committed_csn = slap_get_commit_csn( op );
76
77         if ( max_committed_csn == NULL ) {
78                 return BDB_CSN_COMMIT;
79         }
80
81         *ctxcsn_added = 0;
82
83         switch( rc ) {
84         case 0:
85                 if ( !*ctxcsn_e ) {
86                         rs->sr_err = LDAP_OTHER;
87                         rs->sr_text = "context csn not present";
88                         ber_bvfree( max_committed_csn );
89                         return BDB_CSN_ABORT;
90                 } else {
91                         csnbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
92                         ber_dupbv( &csnbva[0], max_committed_csn );
93                         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
94                         mod->sml_op = LDAP_MOD_REPLACE;
95                         ber_str2bv( "contextCSN", strlen("contextCSN"), 1, &mod->sml_type );
96                         mod->sml_bvalues = csnbva;
97                         *modtail = mod;
98                         modtail = &mod->sml_next;
99
100                         ret = slap_mods_check( modlist, 1, &rs->sr_text, textbuf, textlen, NULL );
101
102                         if ( ret != LDAP_SUCCESS ) {
103 #ifdef NEW_LOGGING
104                                 LDAP_LOG( OPERATION, ERR,
105                                                 "bdb_csn_commit: mods check (%s)\n", rs->sr_text, 0, 0 );
106 #else
107                                 Debug( LDAP_DEBUG_ANY,
108                                                 "bdb_csn_commit: mods check (%s)\n", rs->sr_text, 0, 0 );
109 #endif
110                         }
111
112                         bdb_cache_entry_db_relock( bdb->bi_dbenv, locker, ctxcsn_ei, 1, 0, &ctxcsn_lock );
113
114                         ret = bdb_modify_internal( op, tid, modlist, *ctxcsn_e,
115                                                                         &rs->sr_text, textbuf, textlen );                                                               
116                         if ( ret != LDAP_SUCCESS ) {
117 #ifdef NEW_LOGGING
118                                 LDAP_LOG ( OPERATION, ERR,
119                                                 "bdb_csn_commit: modify failed (%d)\n", rs->sr_err, 0, 0 );
120 #else
121                                 Debug( LDAP_DEBUG_TRACE,
122                                                 "bdb_csn_commit: modify failed (%d)\n", rs->sr_err, 0, 0 );
123 #endif
124                                 switch( ret ) {
125                                 case DB_LOCK_DEADLOCK:
126                                 case DB_LOCK_NOTGRANTED:
127                                         goto rewind;
128                                 default:
129                                         return BDB_CSN_ABORT;
130                                 }
131                         }
132
133                         for ( ml = modlist; ml != NULL; ml = mlnext ) {
134                                 mlnext = ml->sml_next;
135                                 free( ml );
136                         }
137
138                         ret = bdb_id2entry_update( op->o_bd, tid, *ctxcsn_e );
139                         switch ( ret ) {
140                         case 0 :
141                                 break;
142                         case DB_LOCK_DEADLOCK :
143                         case DB_LOCK_NOTGRANTED :
144                                 goto rewind;
145                         default :
146                                 rs->sr_err = ret;
147                                 rs->sr_text = "context csn update failed";
148                                 return BDB_CSN_ABORT;
149                         }
150                 }
151                 break;
152         case DB_NOTFOUND:
153                 if ( op->o_tag == LDAP_REQ_ADD &&
154                                                 be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname )) {
155                         *suffix_ei = NULL;
156                         eip = (EntryInfo *) ch_calloc( 1, sizeof( EntryInfo ));
157                         eip->bei_id = op->oq_add.rs_e->e_id;
158                 } else {
159                         eip = *suffix_ei = ctxcsn_ei;
160                 }
161
162                 /* This serializes add. But this case is very rare : only once. */
163                 rs->sr_err = bdb_next_id( op->o_bd, tid, &ctxcsn_id );
164                 if ( rs->sr_err != 0 ) {
165 #ifdef NEW_LOGGING
166                         LDAP_LOG ( OPERATION, ERR,
167                                 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
168 #else
169                         Debug( LDAP_DEBUG_TRACE,
170                                 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
171 #endif
172                         rs->sr_err = LDAP_OTHER;
173                         rs->sr_text = "internal error";
174                         return BDB_CSN_ABORT;
175                 }
176
177                 *ctxcsn_e = slap_create_context_csn_entry( op->o_bd, max_committed_csn );
178                 ber_bvfree( max_committed_csn );
179                 (*ctxcsn_e)->e_id = ctxcsn_id;
180                 *ctxcsn_added = 1;
181
182                 ret = bdb_dn2id_add( op, tid, eip, *ctxcsn_e );
183                 switch ( ret ) {
184                 case 0 :
185                         break;
186                 case DB_LOCK_DEADLOCK :
187                 case DB_LOCK_NOTGRANTED :
188                         goto rewind;
189                 case DB_KEYEXIST :
190                         rs->sr_err = LDAP_OTHER;
191                         rs->sr_text = "context csn exists before contex prefix does";
192                         return BDB_CSN_ABORT;
193                 default :
194                         rs->sr_err = LDAP_OTHER;
195                         rs->sr_text = "context csn store failed";
196                         return BDB_CSN_ABORT;
197                 }
198
199                 if ( *suffix_ei == NULL ) {
200                         ch_free( eip );
201                 }
202
203                 ret = bdb_id2entry_add( op->o_bd, tid, *ctxcsn_e );
204                 switch ( ret ) {
205                 case 0 :
206                         break;
207                 case DB_LOCK_DEADLOCK :
208                 case DB_LOCK_NOTGRANTED :
209                         goto rewind;
210                 default :
211                         rs->sr_err = LDAP_OTHER;
212                         rs->sr_text = "context csn store failed";
213                         return BDB_CSN_ABORT;
214                 }
215                 ret = bdb_index_entry_add( op, tid, *ctxcsn_e );
216                 switch ( ret ) {
217                 case 0 :
218                         break;
219                 case DB_LOCK_DEADLOCK :
220                 case DB_LOCK_NOTGRANTED :
221                         goto rewind;
222                 default :
223                         rs->sr_err = LDAP_OTHER;
224                         rs->sr_text = "context csn indexing failed";
225                         return BDB_CSN_ABORT;
226                 }
227                 break;
228         case DB_LOCK_DEADLOCK:
229         case DB_LOCK_NOTGRANTED:
230 #ifdef NEW_LOGGING
231                 LDAP_LOG( OPERATION, ERR,
232                                 "bdb_csn_commit : bdb_dn2entry retry\n", 0, 0, 0 );
233 #else
234                 Debug( LDAP_DEBUG_TRACE,
235                                 "bdb_csn_commit : bdb_dn2entry retry\n", 0, 0, 0 );
236 #endif
237                 goto rewind;
238         case LDAP_BUSY:
239                 rs->sr_err = rc;
240                 rs->sr_text = "ldap server busy";
241                 return BDB_CSN_ABORT;
242         default:
243                 rs->sr_err = LDAP_OTHER;
244                 rs->sr_text = "internal error";
245                 return BDB_CSN_ABORT;
246         }
247
248         return BDB_CSN_COMMIT;
249
250 rewind :
251         slap_rewind_commit_csn( op );
252         return BDB_CSN_RETRY;
253 }
254
255 int
256 bdb_get_commit_csn(
257         Operation       *op,
258         SlapReply       *rs,
259         struct berval   **search_context_csn,
260         u_int32_t       locker,
261         DB_LOCK         *ctxcsn_lock
262 )
263 {
264         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
265         struct berval ctxcsn_rdn = BER_BVNULL;
266         struct berval ctxcsn_ndn = BER_BVNULL;
267         struct berval csn = BER_BVNULL;
268         struct berval ctx_nrdn = BER_BVC( "cn=ldapsync" );
269         EntryInfo       *ctxcsn_ei = NULL;
270         EntryInfo       *suffix_ei = NULL;
271         Entry           *ctxcsn_e = NULL;
272         DB_TXN          *ltid = NULL;
273         Attribute       *csn_a;
274         char            substr[67];
275         char            gid[DB_XIDDATASIZE];
276         char            csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
277         int                     num_retries = 0;
278         int                     ctxcsn_added = 0;
279         int                     rc;
280
281         if ( op->o_sync_mode != SLAP_SYNC_NONE ) {
282                 if ( op->o_bd->syncinfo ) {
283                         sprintf( substr, "cn=syncrepl%d", op->o_bd->syncinfo->id );
284                         ber_str2bv( substr, strlen( substr ), 0, &ctxcsn_rdn );
285                         build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0], &ctxcsn_rdn );
286                 } else {
287                         ber_str2bv( "cn=ldapsync", strlen("cn=ldapsync"), 0, &ctxcsn_rdn );
288                         build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0], &ctxcsn_rdn );
289                 }
290
291 ctxcsn_retry :
292                 rs->sr_err = bdb_dn2entry( op, NULL, &ctxcsn_ndn, &ctxcsn_ei,
293                                                                         0, locker, ctxcsn_lock );
294                 switch(rs->sr_err) {
295                 case 0:
296                         ch_free( ctxcsn_ndn.bv_val );
297                         if ( ctxcsn_ei ) {
298                                 ctxcsn_e = ctxcsn_ei->bei_e;
299                         }
300                         break;
301         case LDAP_BUSY:
302                         ch_free( ctxcsn_ndn.bv_val );
303                         LOCK_ID_FREE (bdb->bi_dbenv, locker );
304                         return LDAP_BUSY;
305         case DB_LOCK_DEADLOCK:
306         case DB_LOCK_NOTGRANTED:
307                         goto ctxcsn_retry;
308         case DB_NOTFOUND:
309                         if ( !op->o_bd->syncinfo ) {
310                                 snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
311                                                         bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
312
313                                 slap_get_csn( op, csnbuf, sizeof(csnbuf), &csn, 1 );
314
315                                 if ( 0 ) {
316 txn_retry:
317                                         rs->sr_err = TXN_ABORT( ltid );
318                                         if ( rs->sr_err != 0 ) {
319                                                 rs->sr_err = LDAP_OTHER;
320                                                 return rs->sr_err;
321                                         }
322
323                                         bdb_trans_backoff( ++num_retries );
324                                         ldap_pvt_thread_yield();
325                                 }
326                                 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, &ltid, bdb->bi_db_opflags );
327                                 if ( rs->sr_err != 0 ) {
328                                         rs->sr_err = LDAP_OTHER;
329                                         return rs->sr_err;
330                                 }
331
332                                 rs->sr_err = bdb_csn_commit( op, rs, ltid, NULL, &suffix_ei,
333                                                                                 &ctxcsn_e, &ctxcsn_added, locker );
334                                 switch( rs->sr_err ) {
335                                 case BDB_CSN_ABORT:
336                                         LOCK_ID_FREE( bdb->bi_dbenv, locker );
337                                         return LDAP_OTHER;
338                                 case BDB_CSN_RETRY:
339                                         goto txn_retry;
340                                 }
341
342                                 rs->sr_err = TXN_PREPARE( ltid, gid );
343                                 if ( rs->sr_err != 0 ) {
344                                         rs->sr_err = LDAP_OTHER;
345                                         return rs->sr_err;
346                                 }
347
348                                 bdb_cache_add( bdb, suffix_ei, ctxcsn_e, &ctx_nrdn, locker );
349
350                                 rs->sr_err = TXN_COMMIT( ltid, 0 );
351                                 if ( rs->sr_err != 0 ) {
352                                         rs->sr_err = LDAP_OTHER;
353                                         return rs->sr_err;
354                                 }
355
356                                 ctxcsn_ei = NULL;
357                                 rs->sr_err = bdb_dn2entry( op, NULL, &ctxcsn_ndn, &ctxcsn_ei,
358                                                                                 0, locker, ctxcsn_lock );
359                                 ch_free( ctxcsn_ndn.bv_val );
360
361                                 if ( ctxcsn_ei ) {
362                                         ctxcsn_e = ctxcsn_ei->bei_e;
363                                 }
364                         } else {
365                                 LOCK_ID_FREE( bdb->bi_dbenv, locker );
366                                 return LDAP_OTHER;
367                         }
368                         break;
369
370                 default:
371                         LOCK_ID_FREE (bdb->bi_dbenv, locker );
372                         return LDAP_OTHER;
373                 }
374
375                 if ( ctxcsn_e ) {
376                         if ( op->o_bd->syncinfo ) {
377                                 csn_a = attr_find( ctxcsn_e->e_attrs, slap_schema.si_ad_syncreplCookie );
378                         } else {
379                                 csn_a = attr_find( ctxcsn_e->e_attrs, slap_schema.si_ad_contextCSN );
380                         }
381                         if ( csn_a ) {
382                                 *search_context_csn = ber_dupbv( NULL, &csn_a->a_vals[0] );
383                         } else {
384                                 *search_context_csn = NULL;
385                         }
386                 } else {
387                         *search_context_csn = NULL;
388                 }
389         }
390
391         return LDAP_SUCCESS;
392 }