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