]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/ctxcsn.c
misc cleanup
[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 #ifdef LDAP_SYNC
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_rdn = { 0, NULL };
48         struct berval   ctxcsn_ndn = { 0, NULL };
49         EntryInfo               *ctxcsn_ei = NULL;
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
57         if ( ei ) {
58                 e = ei->bei_e;
59         }
60
61         ber_str2bv( "cn=ldapsync", strlen("cn=ldapsync"), 0, &ctxcsn_rdn );
62         build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0], &ctxcsn_rdn );
63
64         rc = bdb_dn2entry( op, tid, &ctxcsn_ndn, &ctxcsn_ei,
65                                                            0, locker, &ctxcsn_lock );
66
67         if ( ctxcsn_ei ) {
68                 *ctxcsn_e = ctxcsn_ei->bei_e;
69                 bdb_cache_entry_db_relock( bdb->bi_dbenv, locker, ctxcsn_ei, 1, 0, &ctxcsn_lock );
70         }
71
72         max_committed_csn = slap_get_commit_csn( op );
73
74         if ( max_committed_csn == NULL )
75                 return BDB_CSN_COMMIT;
76
77         *ctxcsn_added = 0;
78
79         switch( rc ) {
80         case 0:
81                 if ( !*ctxcsn_e ) {
82                         rs->sr_err = LDAP_OTHER;
83                         rs->sr_text = "context csn not present";
84                         return BDB_CSN_ABORT;
85                 } else {
86                         attr_delete( &(*ctxcsn_e)->e_attrs, slap_schema.si_ad_contextCSN );
87                         attr_merge_normalize_one( *ctxcsn_e, slap_schema.si_ad_contextCSN,
88                                                         max_committed_csn, NULL );
89                         ret = bdb_id2entry_update( op->o_bd, tid, *ctxcsn_e );
90                         switch ( ret ) {
91                         case 0 :
92                                 break;
93                         case DB_LOCK_DEADLOCK :
94                         case DB_LOCK_NOTGRANTED :
95                                 goto rewind;
96                         default :
97                                 rs->sr_err = ret;
98                                 rs->sr_text = "context csn update failed";
99                                 return BDB_CSN_ABORT;
100                         }
101                         ret = bdb_index_entry_add( op, tid, *ctxcsn_e );
102                         switch ( ret ) {
103                         case 0 :
104                                 break;
105                         case DB_LOCK_DEADLOCK :
106                         case DB_LOCK_NOTGRANTED :
107                                 goto rewind;
108                         default :
109                                 rs->sr_err = LDAP_OTHER;
110                                 rs->sr_text = "context csn indexing failed";
111                                 return BDB_CSN_ABORT;
112                         }
113                 }
114                 break;
115         case DB_NOTFOUND:
116                 if ( op->o_tag == LDAP_REQ_ADD && !be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname )) {
117                         rc = bdb_dn2entry( op, tid, &op->o_bd->be_nsuffix[0], suffix_ei,
118                                                                         0, locker, &suffix_lock );
119                 } else if ( op->o_tag != LDAP_REQ_ADD && !be_issuffix( op->o_bd, &e->e_nname )) {
120                         rc = bdb_dn2entry( op, tid, &op->o_bd->be_nsuffix[0], suffix_ei,
121                                                                         0, locker, &suffix_lock );
122                 } else {
123                         *suffix_ei = ei;
124                 }
125
126                 /* This serializes add. But this case is very rare : only once. */
127                 rs->sr_err = bdb_next_id( op->o_bd, NULL, &ctxcsn_id );
128                 if ( rs->sr_err != 0 ) {
129 #ifdef NEW_LOGGING
130                         LDAP_LOG ( OPERATION, ERR,
131                                 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
132 #else
133                         Debug( LDAP_DEBUG_TRACE,
134                                 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
135 #endif
136                         rs->sr_err = LDAP_OTHER;
137                         rs->sr_text = "internal error";
138                         return BDB_CSN_ABORT;
139                 }
140
141                 *ctxcsn_e = slap_create_context_csn_entry( op->o_bd, max_committed_csn );
142                 (*ctxcsn_e)->e_id = ctxcsn_id;
143                 *ctxcsn_added = 1;
144                 ret = bdb_dn2id_add( op, tid, *suffix_ei, *ctxcsn_e );
145                 switch ( ret ) {
146                 case 0 :
147                         break;
148                 case DB_LOCK_DEADLOCK :
149                 case DB_LOCK_NOTGRANTED :
150                         goto rewind;
151                 case DB_KEYEXIST :
152                         rs->sr_err = LDAP_OTHER;
153                         rs->sr_text = "context csn exists before contex prefix does";
154                         return BDB_CSN_ABORT;
155                 default :
156                         rs->sr_err = LDAP_OTHER;
157                         rs->sr_text = "context csn store failed";
158                         return BDB_CSN_ABORT;
159                 }
160                 ret = bdb_id2entry_add( op->o_bd, tid, *ctxcsn_e );
161                 switch ( ret ) {
162                 case 0 :
163                         break;
164                 case DB_LOCK_DEADLOCK :
165                 case DB_LOCK_NOTGRANTED :
166                         goto rewind;
167                 default :
168                         rs->sr_err = LDAP_OTHER;
169                         rs->sr_text = "context csn store failed";
170                         return BDB_CSN_ABORT;
171                 }
172                 ret = bdb_index_entry_add( op, tid, *ctxcsn_e );
173                 switch ( ret ) {
174                 case 0 :
175                         break;
176                 case DB_LOCK_DEADLOCK :
177                 case DB_LOCK_NOTGRANTED :
178                         goto rewind;
179                 default :
180                         rs->sr_err = LDAP_OTHER;
181                         rs->sr_text = "context csn indexing failed";
182                         return BDB_CSN_ABORT;
183                 }
184                 break;
185         case DB_LOCK_DEADLOCK:
186         case DB_LOCK_NOTGRANTED:
187                 goto rewind;
188         case LDAP_BUSY:
189                 rs->sr_err = rc;
190                 rs->sr_text = "ldap server busy";
191                 return BDB_CSN_ABORT;
192         default:
193                 rs->sr_err = LDAP_OTHER;
194                 rs->sr_text = "internal error";
195                 return BDB_CSN_ABORT;
196         }
197
198         return BDB_CSN_COMMIT;
199
200 rewind :
201         slap_rewind_commit_csn( op );
202         return BDB_CSN_RETRY;
203 }
204 #endif