]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/ctxcsn.c
more EntryInfo handling fix code (overriding the prev fix)
[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 "back-bdb.h"
31 #include "external.h"
32
33 int
34 bdb_csn_commit(
35         Operation *op,
36         SlapReply *rs,
37         DB_TXN *tid,
38         EntryInfo *ei,
39         EntryInfo **suffix_ei,
40         Entry **ctxcsn_e,
41         int *ctxcsn_added,
42         u_int32_t locker
43 )
44 {
45         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
46         struct berval   ctxcsn_rdn = { 0, NULL };
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         Modifications   *ml, *mlnext, *mod, *modlist;
58         Modifications   **modtail = &modlist;
59         struct berval   *csnbva = NULL;
60         EntryInfo               *eip = NULL;
61
62         if ( ei ) {
63                 e = ei->bei_e;
64         }
65
66         ber_str2bv( "cn=ldapsync", strlen("cn=ldapsync"), 0, &ctxcsn_rdn );
67         build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0], &ctxcsn_rdn );
68
69         rc = bdb_dn2entry( op, tid, &ctxcsn_ndn, &ctxcsn_ei,
70                                                            1, locker, &ctxcsn_lock );
71
72         *ctxcsn_e = ctxcsn_ei->bei_e;
73         bdb_cache_entry_db_relock( bdb->bi_dbenv, locker, ctxcsn_ei, 1, 0, &ctxcsn_lock );
74
75         max_committed_csn = slap_get_commit_csn( op );
76
77         if ( max_committed_csn == NULL )
78                 return BDB_CSN_COMMIT;
79
80         *ctxcsn_added = 0;
81
82         switch( rc ) {
83         case 0:
84                 if ( !*ctxcsn_e ) {
85                         rs->sr_err = LDAP_OTHER;
86                         rs->sr_text = "context csn not present";
87                         ber_bvfree( max_committed_csn );
88                         return BDB_CSN_ABORT;
89                 } else {
90                         csnbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
91                         ber_dupbv( &csnbva[0], max_committed_csn );
92                         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
93                         mod->sml_op = LDAP_MOD_REPLACE;
94                         ber_str2bv( "contextCSN", strlen("contextCSN"), 1, &mod->sml_type );
95                         mod->sml_bvalues = csnbva;
96                         *modtail = mod;
97                         modtail = &mod->sml_next;
98
99                         ret = slap_mods_check( modlist, 1, &rs->sr_text, textbuf, textlen, NULL );
100
101                         if ( rc != LDAP_SUCCESS ) {
102 #ifdef NEW_LOGGING
103                                 LDAP_LOG( OPERATION, ERR,
104                                                 "bdb_csn_commit: mods check (%s)\n", rs->sr_text, 0, 0 );
105 #else
106                                 Debug( LDAP_DEBUG_ANY,
107                                                 "bdb_csn_commit: mods check (%s)\n", rs->sr_text, 0, 0 );
108 #endif
109                         }
110
111                         ret = bdb_modify_internal( op, tid, modlist, *ctxcsn_e,
112                                                                         &rs->sr_text, textbuf, textlen );                                                               
113                         if ( ret != LDAP_SUCCESS ) {
114 #ifdef NEW_LOGGING
115                                 LDAP_LOG ( OPERATION, ERR,
116                                                 "bdb_csn_commit: modify failed (%d)\n", rs->sr_err, 0, 0 );
117 #else
118                                 Debug( LDAP_DEBUG_TRACE,
119                                                 "bdb_csn_commit: modify failed (%d)\n", rs->sr_err, 0, 0 );
120 #endif
121                                 switch( ret ) {
122                                 case DB_LOCK_DEADLOCK:
123                                 case DB_LOCK_NOTGRANTED:
124                                         return BDB_CSN_ABORT;
125                                 default:
126                                         goto rewind;
127                                 }
128                         }
129
130                         for ( ml = modlist; ml != NULL; ml = mlnext ) {
131                                 mlnext = ml->sml_next;
132                                 free( ml );
133                         }
134
135                         ret = bdb_id2entry_update( op->o_bd, tid, *ctxcsn_e );
136                         switch ( ret ) {
137                         case 0 :
138                                 break;
139                         case DB_LOCK_DEADLOCK :
140                         case DB_LOCK_NOTGRANTED :
141                                 goto rewind;
142                         default :
143                                 rs->sr_err = ret;
144                                 rs->sr_text = "context csn update failed";
145                                 return BDB_CSN_ABORT;
146                         }
147                 }
148                 break;
149         case DB_NOTFOUND:
150                 if ( op->o_tag == LDAP_REQ_ADD &&
151                                                 be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname )) {
152                         *suffix_ei = NULL;
153                         eip = (EntryInfo *) ch_calloc( 1, sizeof( EntryInfo ));
154                         eip->bei_id = op->oq_add.rs_e->e_id;
155                 } else {
156                         eip = *suffix_ei = ctxcsn_ei;
157                 }
158
159                 /* This serializes add. But this case is very rare : only once. */
160                 rs->sr_err = bdb_next_id( op->o_bd, tid, &ctxcsn_id );
161                 if ( rs->sr_err != 0 ) {
162 #ifdef NEW_LOGGING
163                         LDAP_LOG ( OPERATION, ERR,
164                                 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
165 #else
166                         Debug( LDAP_DEBUG_TRACE,
167                                 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
168 #endif
169                         rs->sr_err = LDAP_OTHER;
170                         rs->sr_text = "internal error";
171                         return BDB_CSN_ABORT;
172                 }
173
174                 *ctxcsn_e = slap_create_context_csn_entry( op->o_bd, max_committed_csn );
175                 ber_bvfree( max_committed_csn );
176                 (*ctxcsn_e)->e_id = ctxcsn_id;
177                 *ctxcsn_added = 1;
178
179                 ret = bdb_dn2id_add( op, tid, eip, *ctxcsn_e );
180                 switch ( ret ) {
181                 case 0 :
182                         break;
183                 case DB_LOCK_DEADLOCK :
184                 case DB_LOCK_NOTGRANTED :
185                         goto rewind;
186                 case DB_KEYEXIST :
187                         rs->sr_err = LDAP_OTHER;
188                         rs->sr_text = "context csn exists before contex prefix does";
189                         return BDB_CSN_ABORT;
190                 default :
191                         rs->sr_err = LDAP_OTHER;
192                         rs->sr_text = "context csn store failed";
193                         return BDB_CSN_ABORT;
194                 }
195
196                 if ( *suffix_ei == NULL ) {
197                         ch_free( eip );
198                 }
199
200                 ret = bdb_id2entry_add( op->o_bd, 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 store failed";
210                         return BDB_CSN_ABORT;
211                 }
212                 ret = bdb_index_entry_add( op, tid, *ctxcsn_e );
213                 switch ( ret ) {
214                 case 0 :
215                         break;
216                 case DB_LOCK_DEADLOCK :
217                 case DB_LOCK_NOTGRANTED :
218                         goto rewind;
219                 default :
220                         rs->sr_err = LDAP_OTHER;
221                         rs->sr_text = "context csn indexing failed";
222                         return BDB_CSN_ABORT;
223                 }
224                 break;
225         case DB_LOCK_DEADLOCK:
226         case DB_LOCK_NOTGRANTED:
227                 goto rewind;
228         case LDAP_BUSY:
229                 rs->sr_err = rc;
230                 rs->sr_text = "ldap server busy";
231                 return BDB_CSN_ABORT;
232         default:
233                 rs->sr_err = LDAP_OTHER;
234                 rs->sr_text = "internal error";
235                 return BDB_CSN_ABORT;
236         }
237
238         return BDB_CSN_COMMIT;
239
240 rewind :
241         slap_rewind_commit_csn( op );
242         return BDB_CSN_RETRY;
243 }