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