]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/ctxcsn.c
misc updates
[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;
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, op->o_tmpmemctx );
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         op->o_tmpfree( ctxcsn_ndn.bv_val, op->o_tmpmemctx );
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                         ch_free( max_committed_csn.bv_val );
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_bvalues = 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                         ch_free( max_committed_csn.bv_val );
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                 ch_free( max_committed_csn.bv_val );
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 contex 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 ctxcsn_ndn = BER_BVNULL;
254         struct berval csn = BER_BVNULL;
255         EntryInfo       *ctxcsn_ei = NULL;
256         EntryInfo       *suffix_ei = NULL;
257         Entry           *ctxcsn_e = NULL;
258         DB_TXN          *ltid = NULL;
259         Attribute       *csn_a;
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         struct sync_cookie syncCookie = { NULL, -1, NULL};
266         syncinfo_t      *si;
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 bv;
272
273                 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
274                         sprintf( substr, "cn=syncrepl%d", si->si_rid );
275                         ber_str2bv( substr, 0, 0, &bv );
276                         build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0], &bv, NULL );
277
278 consumer_ctxcsn_retry :
279                         rs->sr_err = bdb_dn2entry( op, NULL, &ctxcsn_ndn, &ctxcsn_ei,
280                                                                                 0, locker, ctxcsn_lock );
281                         switch(rs->sr_err) {
282                         case 0:
283                                 ch_free( ctxcsn_ndn.bv_val );
284                                 ctxcsn_ndn.bv_val = NULL;
285                                 if ( ctxcsn_ei ) {
286                                         ctxcsn_e = ctxcsn_ei->bei_e;
287                                 }
288                                 break;
289                         case LDAP_BUSY:
290                                 ch_free( ctxcsn_ndn.bv_val );
291                                 LOCK_ID_FREE (bdb->bi_dbenv, locker );
292                                 return LDAP_BUSY;
293                         case DB_LOCK_DEADLOCK:
294                         case DB_LOCK_NOTGRANTED:
295                                 goto consumer_ctxcsn_retry;
296                         case DB_NOTFOUND:
297                                 ch_free( ctxcsn_ndn.bv_val );
298                                 LOCK_ID_FREE( bdb->bi_dbenv, locker );
299                                 return LDAP_OTHER;
300                         default:
301                                 ch_free( ctxcsn_ndn.bv_val );
302                                 ctxcsn_ndn.bv_val = NULL;
303                                 LOCK_ID_FREE (bdb->bi_dbenv, locker );
304                                 return LDAP_OTHER;
305                         }
306
307                         if ( ctxcsn_e ) {
308                                 csn_a = attr_find( ctxcsn_e->e_attrs,
309                                                         slap_schema.si_ad_syncreplCookie );
310                                 if ( csn_a ) {
311                                         struct berval cookie;
312                                         const char *text;
313                                         int match = -1;
314                                         ber_dupbv( &cookie, &csn_a->a_vals[0] );
315                                         ber_bvarray_add( &syncCookie.octet_str, &cookie );
316                                         slap_parse_sync_cookie( &syncCookie );
317                                         if ( *search_context_csn &&
318                                                  (*search_context_csn)->bv_val != NULL ) {
319                                                 value_match( &match, slap_schema.si_ad_entryCSN,
320                                                         slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
321                                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
322                                                         &syncCookie.ctxcsn, *search_context_csn, &text );
323                                         }
324                                         if ( match < 0 ) {
325                                                 /* set search_context_csn to the
326                                                    smallest syncrepl cookie value */
327                                                 if ( *search_context_csn ) {
328                                                         ch_free( (*search_context_csn)->bv_val );
329                                                         ch_free( *search_context_csn );
330                                                 }
331                                                 *search_context_csn = ber_dupbv( NULL,
332                                                                                                 syncCookie.ctxcsn );
333                                         }
334                                         slap_sync_cookie_free( &syncCookie, 0 );
335                                 } else {
336                                         *search_context_csn = NULL;
337                                 } 
338                         } else {
339                                 *search_context_csn = NULL;
340                         }
341                 }
342         } else if ( op->o_sync_mode != SLAP_SYNC_NONE &&
343                  LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
344                 build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0],
345                                         (struct berval *)&slap_ldapsync_cn_bv, NULL );
346
347 provider_ctxcsn_retry :
348                 rs->sr_err = bdb_dn2entry( op, NULL, &ctxcsn_ndn, &ctxcsn_ei,
349                                                                         0, locker, ctxcsn_lock );
350                 switch(rs->sr_err) {
351                 case 0:
352                         ch_free( ctxcsn_ndn.bv_val );
353                         if ( ctxcsn_ei ) {
354                                 ctxcsn_e = ctxcsn_ei->bei_e;
355                         }
356                         break;
357                 case LDAP_BUSY:
358                         ch_free( ctxcsn_ndn.bv_val );
359                         LOCK_ID_FREE (bdb->bi_dbenv, locker );
360                         return LDAP_BUSY;
361                 case DB_LOCK_DEADLOCK:
362                 case DB_LOCK_NOTGRANTED:
363                         goto consumer_ctxcsn_retry;
364                 case DB_NOTFOUND:
365                         snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
366                                 bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
367
368                         slap_get_csn( op, csnbuf, sizeof(csnbuf), &csn, 1 );
369
370                         if ( 0 ) {
371 txn_retry:
372                                 rs->sr_err = TXN_ABORT( ltid );
373                                 if ( rs->sr_err != 0 ) {
374                                         rs->sr_err = LDAP_OTHER;
375                                         return rs->sr_err;
376                                 }
377                                 ldap_pvt_thread_yield();
378                                 bdb_trans_backoff( ++num_retries );
379                         }
380                         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL,
381                                                                 &ltid, bdb->bi_db_opflags );
382                         if ( rs->sr_err != 0 ) {
383                                 rs->sr_err = LDAP_OTHER;
384                                 return rs->sr_err;
385                         }
386
387                         rs->sr_err = bdb_csn_commit( op, rs, ltid, NULL, &suffix_ei,
388                                                                         &ctxcsn_e, &ctxcsn_added, locker );
389                         switch( rs->sr_err ) {
390                         case BDB_CSN_ABORT:
391                                 LOCK_ID_FREE( bdb->bi_dbenv, locker );
392                                 return LDAP_OTHER;
393                         case BDB_CSN_RETRY:
394                                 goto txn_retry;
395                         }
396
397                         rs->sr_err = TXN_PREPARE( ltid, gid );
398                         if ( rs->sr_err != 0 ) {
399                                 rs->sr_err = LDAP_OTHER;
400                                 return rs->sr_err;
401                         }
402
403                         bdb_cache_add( bdb, suffix_ei, ctxcsn_e,
404                                         (struct berval *)&slap_ldapsync_cn_bv, locker );
405
406                         rs->sr_err = TXN_COMMIT( ltid, 0 );
407                         if ( rs->sr_err != 0 ) {
408                                 rs->sr_err = LDAP_OTHER;
409                                 return rs->sr_err;
410                         }
411
412                         rs->sr_err = bdb_dn2entry( op, NULL, &ctxcsn_ndn, &ctxcsn_ei,
413                                     0, locker, ctxcsn_lock );
414                         ch_free( ctxcsn_ndn.bv_val );
415
416                         if ( ctxcsn_ei ) {
417                                 ctxcsn_e = ctxcsn_ei->bei_e;
418                         }
419                         break;
420
421                 default:
422                         ch_free( ctxcsn_ndn.bv_val );
423                         LOCK_ID_FREE (bdb->bi_dbenv, locker );
424                         return LDAP_OTHER;
425                 }
426
427                 if ( ctxcsn_e ) {
428                         csn_a = attr_find( ctxcsn_e->e_attrs,
429                                                 slap_schema.si_ad_contextCSN );
430                         if ( csn_a ) {
431                                 *search_context_csn = ber_dupbv( NULL, &csn_a->a_vals[0] );
432                         } else {
433                                 *search_context_csn = NULL;
434                         }
435                 } else {
436                         *search_context_csn = NULL;
437                 }
438         }
439
440         return LDAP_SUCCESS;
441 }