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