]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/ctxcsn.c
ITS#3226: Clear attribute flags after schema_check failed
[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         struct berval   ctxcsn_ndn = BER_BVNULL;
43         EntryInfo               *ctxcsn_ei = NULL;
44         DB_LOCK                 ctxcsn_lock;
45         struct berval   max_committed_csn;
46         DB_LOCK                 suffix_lock;
47         int                             rc, ret;
48         ID                              ctxcsn_id;
49         Entry                   *e;
50         char                    textbuf[SLAP_TEXT_BUFLEN];
51         size_t                  textlen = sizeof textbuf;
52         EntryInfo               *eip = NULL;
53
54         if ( ei ) {
55                 e = ei->bei_e;
56         }
57
58         build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0],
59                 (struct berval *)&slap_ldapsync_cn_bv, op->o_tmpmemctx );
60
61         rc =  bdb_dn2entry( op, tid, &ctxcsn_ndn, &ctxcsn_ei,
62                         1, locker, &ctxcsn_lock );
63         
64         *ctxcsn_e = ctxcsn_ei->bei_e;
65
66         op->o_tmpfree( ctxcsn_ndn.bv_val, op->o_tmpmemctx );
67
68         slap_get_commit_csn( op, &max_committed_csn );
69
70         if ( max_committed_csn.bv_val == NULL ) {
71                 return BDB_CSN_COMMIT;
72         }
73
74         *ctxcsn_added = 0;
75
76         switch( rc ) {
77         case 0:
78                 if ( !*ctxcsn_e ) {
79                         rs->sr_err = LDAP_OTHER;
80                         rs->sr_text = "context csn not present";
81                         ch_free( max_committed_csn.bv_val );
82                         return BDB_CSN_ABORT;
83                 } else {
84                         Modifications mod;
85                         struct berval modvals[2];
86                         Entry dummy;
87
88                         modvals[0] = max_committed_csn;
89                         modvals[1].bv_val = NULL;
90                         modvals[1].bv_len = 0;
91
92                         mod.sml_op = LDAP_MOD_REPLACE;
93                         mod.sml_values = modvals;
94                         mod.sml_nvalues = NULL;
95                         mod.sml_desc = slap_schema.si_ad_contextCSN;
96                         mod.sml_type = mod.sml_desc->ad_cname;
97                         mod.sml_next = NULL;
98
99                         dummy = **ctxcsn_e;
100                         ret = bdb_modify_internal( op, tid, &mod, &dummy,
101                                                                         &rs->sr_text, textbuf, textlen );                                                      
102                         ch_free( max_committed_csn.bv_val );
103                         if ( ret != LDAP_SUCCESS ) {
104 #ifdef NEW_LOGGING
105                                 LDAP_LOG ( OPERATION, ERR,
106                                                 "bdb_csn_commit: modify failed (%d)\n", rs->sr_err, 0, 0 );
107 #else
108                                 Debug( LDAP_DEBUG_TRACE,
109                                                 "bdb_csn_commit: modify failed (%d)\n", rs->sr_err, 0, 0 );
110 #endif
111                                 switch( ret ) {
112                                 case DB_LOCK_DEADLOCK:
113                                 case DB_LOCK_NOTGRANTED:
114                                         goto rewind;
115                                 default:
116                                         return BDB_CSN_ABORT;
117                                 }
118                         }
119
120                         ret = bdb_id2entry_update( op->o_bd, tid, &dummy );
121                         switch ( ret ) {
122                         case 0 :
123                                 break;
124                         case DB_LOCK_DEADLOCK :
125                         case DB_LOCK_NOTGRANTED :
126                                 goto rewind;
127                         default :
128                                 rs->sr_err = ret;
129                                 rs->sr_text = "context csn update failed";
130                                 return BDB_CSN_ABORT;
131                         }
132                         bdb_cache_modify( *ctxcsn_e, dummy.e_attrs, bdb->bi_dbenv, locker, &ctxcsn_lock );
133                 }
134                 break;
135         case DB_NOTFOUND:
136                 if ( op->o_tag == LDAP_REQ_ADD &&
137                                                 be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname )) {
138                         *suffix_ei = NULL;
139                         eip = (EntryInfo *) ch_calloc( 1, sizeof( EntryInfo ));
140                         eip->bei_id = op->oq_add.rs_e->e_id;
141                 } else {
142                         eip = *suffix_ei = ctxcsn_ei;
143                 }
144
145                 /* This serializes add. But this case is very rare : only once. */
146                 rs->sr_err = bdb_next_id( op->o_bd, tid, &ctxcsn_id );
147                 if ( rs->sr_err != 0 ) {
148 #ifdef NEW_LOGGING
149                         LDAP_LOG ( OPERATION, ERR,
150                                 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
151 #else
152                         Debug( LDAP_DEBUG_TRACE,
153                                 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
154 #endif
155                         rs->sr_err = LDAP_OTHER;
156                         rs->sr_text = "internal error";
157                         return BDB_CSN_ABORT;
158                 }
159
160                 *ctxcsn_e = slap_create_context_csn_entry( op->o_bd, &max_committed_csn );
161                 ch_free( max_committed_csn.bv_val );
162                 (*ctxcsn_e)->e_id = ctxcsn_id;
163                 *ctxcsn_added = 1;
164
165                 ret = bdb_dn2id_add( op, tid, eip, *ctxcsn_e );
166                 switch ( ret ) {
167                 case 0 :
168                         break;
169                 case DB_LOCK_DEADLOCK :
170                 case DB_LOCK_NOTGRANTED :
171                         goto rewind;
172                 case DB_KEYEXIST :
173                         rs->sr_err = LDAP_OTHER;
174                         rs->sr_text = "context csn exists before context prefix does";
175                         return BDB_CSN_ABORT;
176                 default :
177                         rs->sr_err = LDAP_OTHER;
178                         rs->sr_text = "context csn store failed";
179                         return BDB_CSN_ABORT;
180                 }
181
182                 if ( *suffix_ei == NULL ) {
183                         ch_free( eip );
184                 }
185
186                 ret = bdb_id2entry_add( op->o_bd, tid, *ctxcsn_e );
187                 switch ( ret ) {
188                 case 0 :
189                         break;
190                 case DB_LOCK_DEADLOCK :
191                 case DB_LOCK_NOTGRANTED :
192                         goto rewind;
193                 default :
194                         rs->sr_err = LDAP_OTHER;
195                         rs->sr_text = "context csn store failed";
196                         return BDB_CSN_ABORT;
197                 }
198                 ret = bdb_index_entry_add( op, tid, *ctxcsn_e );
199                 switch ( ret ) {
200                 case 0 :
201                         break;
202                 case DB_LOCK_DEADLOCK :
203                 case DB_LOCK_NOTGRANTED :
204                         goto rewind;
205                 default :
206                         rs->sr_err = LDAP_OTHER;
207                         rs->sr_text = "context csn indexing failed";
208                         return BDB_CSN_ABORT;
209                 }
210                 break;
211         case DB_LOCK_DEADLOCK:
212         case DB_LOCK_NOTGRANTED:
213 #ifdef NEW_LOGGING
214                 LDAP_LOG( OPERATION, ERR,
215                                 "bdb_csn_commit : bdb_dn2entry retry\n", 0, 0, 0 );
216 #else
217                 Debug( LDAP_DEBUG_TRACE,
218                                 "bdb_csn_commit : bdb_dn2entry retry\n", 0, 0, 0 );
219 #endif
220                 goto rewind;
221         case LDAP_BUSY:
222                 rs->sr_err = rc;
223                 rs->sr_text = "ldap server busy";
224                 return BDB_CSN_ABORT;
225         default:
226                 rs->sr_err = LDAP_OTHER;
227                 rs->sr_text = "internal error";
228                 return BDB_CSN_ABORT;
229         }
230
231         return BDB_CSN_COMMIT;
232
233 rewind :
234         slap_rewind_commit_csn( op );
235         return BDB_CSN_RETRY;
236 }
237
238 int
239 bdb_get_commit_csn(
240         Operation       *op,
241         SlapReply       *rs,
242         struct berval   **search_context_csn,
243         u_int32_t       locker,
244         DB_LOCK         *ctxcsn_lock
245 )
246 {
247         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
248         struct berval ctxcsn_ndn = BER_BVNULL;
249         struct berval csn = BER_BVNULL;
250         EntryInfo       *ctxcsn_ei = NULL;
251         EntryInfo       *suffix_ei = NULL;
252         Entry           *ctxcsn_e = NULL;
253         DB_TXN          *ltid = NULL;
254         Attribute       *csn_a;
255         char            gid[DB_XIDDATASIZE];
256         char            csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
257         int                     num_retries = 0;
258         int                     ctxcsn_added = 0;
259         int                     rc;
260         struct sync_cookie syncCookie = { NULL, -1, NULL};
261         syncinfo_t      *si;
262         u_int32_t       ctxcsn_locker = 0;
263
264         if ( op->o_sync_mode != SLAP_SYNC_NONE &&
265                  !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
266                 char substr[67];
267                 struct berval bv;
268
269                 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
270                         sprintf( substr, "cn=syncrepl%ld", si->si_rid );
271                         ber_str2bv( substr, 0, 0, &bv );
272                         build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0], &bv, NULL );
273
274 consumer_ctxcsn_retry :
275                         rs->sr_err = bdb_dn2entry( op, NULL, &ctxcsn_ndn, &ctxcsn_ei,
276                                                                                 0, locker, ctxcsn_lock );
277                         switch(rs->sr_err) {
278                         case 0:
279                                 ch_free( ctxcsn_ndn.bv_val );
280                                 ctxcsn_ndn.bv_val = NULL;
281                                 if ( ctxcsn_ei ) {
282                                         ctxcsn_e = ctxcsn_ei->bei_e;
283                                 }
284                                 break;
285                         case LDAP_BUSY:
286                                 goto done;
287                         case DB_LOCK_DEADLOCK:
288                         case DB_LOCK_NOTGRANTED:
289                                 goto consumer_ctxcsn_retry;
290                         case DB_NOTFOUND:
291                                 rs->sr_err = LDAP_OTHER;
292                                 goto done;
293                         default:
294                                 rs->sr_err = LDAP_OTHER;
295                                 goto done;
296                         }
297
298                         if ( ctxcsn_e ) {
299                                 csn_a = attr_find( ctxcsn_e->e_attrs,
300                                                         slap_schema.si_ad_syncreplCookie );
301                                 if ( csn_a ) {
302                                         struct berval cookie;
303                                         const char *text;
304                                         int match = -1;
305                                         ber_dupbv( &cookie, &csn_a->a_vals[0] );
306                                         ber_bvarray_add( &syncCookie.octet_str, &cookie );
307                                         slap_parse_sync_cookie( &syncCookie );
308                                         if ( *search_context_csn &&
309                                                 (*search_context_csn)->bv_val != NULL )
310                                         {
311                                                 value_match( &match, slap_schema.si_ad_entryCSN,
312                                                         slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
313                                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
314                                                         syncCookie.ctxcsn, *search_context_csn, &text );
315                                         }
316                                         if ( match < 0 ) {
317                                                 /* set search_context_csn to the
318                                                    smallest syncrepl cookie value */
319                                                 if ( *search_context_csn ) {
320                                                         ch_free( (*search_context_csn)->bv_val );
321                                                         ch_free( *search_context_csn );
322                                                 }
323                                                 *search_context_csn = ber_dupbv( NULL,
324                                                         syncCookie.ctxcsn );
325                                         }
326                                         slap_sync_cookie_free( &syncCookie, 0 );
327                                 } else {
328                                         *search_context_csn = NULL;
329                                 } 
330                         } else {
331                                 *search_context_csn = NULL;
332                         }
333                 }
334         } else if ( op->o_sync_mode != SLAP_SYNC_NONE &&
335                  LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
336                 build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0],
337                                         (struct berval *)&slap_ldapsync_cn_bv, NULL );
338
339 provider_ctxcsn_retry :
340                 rs->sr_err = bdb_dn2entry( op, NULL, &ctxcsn_ndn, &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, &ctxcsn_ndn, &ctxcsn_ei,
405                                     0, ctxcsn_locker, ctxcsn_lock );
406                         ch_free( ctxcsn_ndn.bv_val );
407                         ctxcsn_ndn.bv_val = NULL;
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         if ( ctxcsn_ndn.bv_val != NULL )
441                 ch_free( ctxcsn_ndn.bv_val );
442
443         return rs->sr_err;
444 }